ledger_lib/
info.rs

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! Device information types and connection filters

use strum::{Display, EnumString};

use crate::Filters;

use super::transport;

/// Ledger device information
#[derive(Clone, PartialEq, Debug)]
pub struct LedgerInfo {
    /// Device Model
    pub model: Model,

    /// Device connection information
    pub conn: ConnInfo,
}

impl std::fmt::Display for LedgerInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} ({})", self.model, self.conn)
    }
}

impl LedgerInfo {
    /// Fetch connection kind enumeration
    pub fn kind(&self) -> ConnType {
        match &self.conn {
            #[cfg(feature = "transport_usb")]
            ConnInfo::Usb(_) => ConnType::Usb,
            #[cfg(feature = "transport_tcp")]
            ConnInfo::Tcp(_) => ConnType::Tcp,
            #[cfg(feature = "transport_ble")]
            ConnInfo::Ble(_) => ConnType::Ble,
        }
    }
}

/// Ledger device models
#[derive(Clone, PartialEq, Debug, Display, EnumString)]
pub enum Model {
    /// Nano S
    NanoS,
    /// Nano S Plus
    NanoSPlus,
    /// Nano X
    NanoX,
    /// Stax
    Stax,
    /// Unknown model
    Unknown(u16),
}

impl Model {
    /// Convert a USB PID to a [Model] kind
    ///
    /// Note that ledger PIDs vary depending on the device state so only the top byte is used
    /// for matching.
    pub fn from_pid(pid: u16) -> Model {
        match pid & 0xFF00 {
            // TODO: support all the models
            //0x0001 => Ok(Model::NanoS),
            0x4000 => Model::NanoX,
            0x5000 => Model::NanoSPlus,
            //0x0006 => Ok(Model::Stax),
            _ => Model::Unknown(pid),
        }
    }
}

/// Ledger connection information
#[derive(Clone, PartialEq, Debug)]
pub enum ConnInfo {
    #[cfg(feature = "transport_usb")]
    Usb(transport::UsbInfo),
    #[cfg(feature = "transport_tcp")]
    Tcp(transport::TcpInfo),
    #[cfg(feature = "transport_ble")]
    Ble(transport::BleInfo),
}

/// Ledger connection types
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum ConnType {
    Usb,
    Tcp,
    Ble,
}

impl From<ConnType> for Filters {
    /// Convert a connection type to a discovery filter
    fn from(value: ConnType) -> Self {
        match value {
            ConnType::Usb => Filters::Hid,
            ConnType::Tcp => Filters::Tcp,
            ConnType::Ble => Filters::Ble,
        }
    }
}

impl std::fmt::Display for ConnInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            #[cfg(feature = "transport_usb")]
            Self::Usb(i) => write!(f, "HID {}", i),
            #[cfg(feature = "transport_tcp")]
            Self::Tcp(i) => write!(f, "TCP {}", i),
            #[cfg(feature = "transport_ble")]
            Self::Ble(i) => write!(f, "BLE {}", i),
        }
    }
}

#[cfg(feature = "transport_usb")]
impl From<transport::UsbInfo> for ConnInfo {
    fn from(value: transport::UsbInfo) -> Self {
        Self::Usb(value)
    }
}

#[cfg(feature = "transport_tcp")]
impl From<transport::TcpInfo> for ConnInfo {
    fn from(value: transport::TcpInfo) -> Self {
        Self::Tcp(value)
    }
}

#[cfg(feature = "transport_ble")]
impl From<transport::BleInfo> for ConnInfo {
    fn from(value: transport::BleInfo) -> Self {
        Self::Ble(value)
    }
}

/// Application info object
#[derive(Debug, Clone, PartialEq)]
pub struct AppInfo {
    pub name: String,
    pub version: String,
    pub flags: ledger_proto::apdus::AppFlags,
}

/// Device info object
#[derive(Debug, Clone, PartialEq)]
pub struct DeviceInfo {
    pub target_id: [u8; 4],
    pub se_version: String,
    pub mcu_version: String,
    pub flags: Vec<u8>,
}