Skip to main content

hackrf_nusb/
types.rs

1//! Public HackRF metadata types.
2
3/// HackRF board identity reported by firmware.
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5#[non_exhaustive]
6pub enum BoardId {
7    /// Pre-production Jellybean board.
8    Jellybean,
9    /// HackRF Jawbreaker beta board.
10    Jawbreaker,
11    /// Original HackRF One board.
12    HackRfOne,
13    /// rad1o badge variant.
14    Rad1o,
15    /// HackRF One revision 9 or later.
16    HackRfOneR9,
17    /// HackRF Pro / Praline platform.
18    HackRfPro,
19    /// Firmware explicitly reported an unrecognized board.
20    Unrecognized,
21    /// Firmware has not detected a board.
22    Undetected,
23    /// A future firmware board ID not known by this crate.
24    Unknown(u8),
25}
26
27impl BoardId {
28    pub(crate) const fn from_raw(value: u8) -> Self {
29        match value {
30            0 => Self::Jellybean,
31            1 => Self::Jawbreaker,
32            2 => Self::HackRfOne,
33            3 => Self::Rad1o,
34            4 => Self::HackRfOneR9,
35            5 => Self::HackRfPro,
36            0xfe => Self::Unrecognized,
37            0xff => Self::Undetected,
38            other => Self::Unknown(other),
39        }
40    }
41
42    /// Human-readable board name following libhackrf terminology.
43    pub const fn name(self) -> &'static str {
44        match self {
45            Self::Jellybean => "Jellybean",
46            Self::Jawbreaker => "Jawbreaker",
47            Self::HackRfOne | Self::HackRfOneR9 => "HackRF One",
48            Self::Rad1o => "rad1o",
49            Self::HackRfPro => "HackRF Pro",
50            Self::Unrecognized => "unrecognized",
51            Self::Undetected => "undetected",
52            Self::Unknown(_) => "unknown",
53        }
54    }
55}
56
57/// Device metadata collected while opening a HackRF.
58#[derive(Clone, Debug, Eq, PartialEq)]
59pub struct DeviceInfo {
60    /// Firmware-reported board identity.
61    pub board_id: BoardId,
62    /// Firmware version string.
63    pub firmware_version: String,
64    /// Raw HackRF USB API version from the USB `bcdDevice` field.
65    ///
66    /// The high byte is the major version and the low byte is the minor version,
67    /// matching libhackrf's hexadecimal `MM.mm` convention.
68    pub usb_api_version: u16,
69    /// Full 128-bit MCU serial number, when it is nonzero.
70    pub serial: Option<u128>,
71}
72
73impl DeviceInfo {
74    /// Human-readable board name.
75    pub const fn board_name(&self) -> &'static str {
76        self.board_id.name()
77    }
78}
79
80#[derive(Clone, Copy, Debug, Eq, PartialEq)]
81pub(crate) struct PartIdSerial {
82    pub(crate) part_id: [u32; 2],
83    pub(crate) serial: [u32; 4],
84}
85
86impl PartIdSerial {
87    pub(crate) const fn serial_u128(self) -> Option<u128> {
88        let [a, b, c, d] = self.serial;
89        let value = ((a as u128) << 96) | ((b as u128) << 64) | ((c as u128) << 32) | d as u128;
90        if value == 0 { None } else { Some(value) }
91    }
92}