summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: a26f02f3331cd1d53808305dedd228c9205d952a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
mod crc32;
mod hid;

pub mod devices;
pub mod features;

use std::fmt;

pub type Error = Box<dyn std::error::Error>;

const VID_NITROKEY: u16 = 0x20a0;
const PID_NITROKEY_PRO: u16 = 0x4108;
const PID_NITROKEY_STORAGE: u16 = 0x4109;

pub fn connect() -> Result<devices::Device, Error> {
    let manager = Manager::new()?;
    if let Some(device) = manager.devices().first() {
        device.connect()
    } else {
        Err("No device connected".into())
    }
}

pub fn connect_model(model: Model) -> Result<devices::Device, Error> {
    let manager = Manager::new()?;
    if let Some(device) = manager.devices().iter().filter(|i| i.model == model).next() {
        device.connect()
    } else {
        Err(format!("No {:?} device connected", model).into())
    }
}

pub struct Manager {
    hidapi: hidapi::HidApi,
}

impl Manager {
    pub fn new() -> Result<Self, Error> {
        let hidapi = hidapi::HidApi::new()?;
        Ok(Manager { hidapi })
    }

    pub fn devices(&self) -> Vec<DeviceInfo<'_>> {
        self.hidapi
            .device_list()
            .flat_map(|device_info| {
                Model::from_vid_pid(device_info.vendor_id(), device_info.product_id()).map(
                    |model| DeviceInfo {
                        hidapi: &self.hidapi,
                        device_info,
                        model,
                    },
                )
            })
            .collect()
    }
}

#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Model {
    Pro,
    Storage,
}

impl Model {
    fn from_vid_pid(vid: u16, pid: u16) -> Option<Model> {
        match vid {
            VID_NITROKEY => match pid {
                PID_NITROKEY_PRO => Some(Model::Pro),
                PID_NITROKEY_STORAGE => Some(Model::Storage),
                _ => None,
            },
            _ => None,
        }
    }
}

pub struct DeviceInfo<'a> {
    hidapi: &'a hidapi::HidApi,
    device_info: &'a hidapi::DeviceInfo,
    model: Model,
}

impl<'a> DeviceInfo<'a> {
    pub fn connect(&self) -> Result<devices::Device, Error> {
        let hid_device = self.device_info.open_device(self.hidapi)?;
        match self.model {
            Model::Pro => Ok(devices::Pro::new(hid_device, self).into()),
            Model::Storage => unimplemented!(),
        }
    }
}

impl<'a> fmt::Debug for DeviceInfo<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DeviceInfo")
            .field("model", &self.model)
            .field("path", &self.device_info.path())
            .finish()
    }
}