aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 573f45f..9f88be3 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -108,6 +108,7 @@ use nitrokey_sys;
pub use crate::auth::{Admin, Authenticate, User};
pub use crate::config::Config;
+#[allow(deprecated)]
pub use crate::device::{
connect, connect_model, Device, DeviceWrapper, Model, Pro, SdCardData, Storage,
StorageProductionInfo, StorageStatus, VolumeMode, VolumeStatus,
@@ -176,6 +177,43 @@ impl Manager {
marker: marker::PhantomData,
}
}
+
+ /// Connects to a Nitrokey device.
+ ///
+ /// This method can be used to connect to any connected device, both a Nitrokey Pro and a
+ /// Nitrokey Storage.
+ ///
+ /// # Errors
+ ///
+ /// - [`NotConnected`][] if no Nitrokey device is connected
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// use nitrokey::DeviceWrapper;
+ ///
+ /// fn do_something(device: DeviceWrapper) {}
+ ///
+ /// # fn main() -> Result<(), nitrokey::Error> {
+ /// match nitrokey::take()?.connect() {
+ /// Ok(device) => do_something(device),
+ /// Err(err) => println!("Could not connect to a Nitrokey: {}", err),
+ /// }
+ /// # Ok(())
+ /// # }
+ /// ```
+ ///
+ /// [`NotConnected`]: enum.CommunicationError.html#variant.NotConnected
+ pub fn connect(&mut self) -> Result<DeviceWrapper, Error> {
+ if unsafe { nitrokey_sys::NK_login_auto() } == 1 {
+ match device::get_connected_device() {
+ Some(wrapper) => Ok(wrapper),
+ None => Err(CommunicationError::NotConnected.into()),
+ }
+ } else {
+ Err(CommunicationError::NotConnected.into())
+ }
+ }
}
/// Take an instance of the connection manager, blocking until an instance is available.