Skip to main content

rmk_types/
ble.rs

1//! BLE status types.
2
3use postcard::experimental::max_size::MaxSize;
4use serde::{Deserialize, Serialize};
5
6/// BLE state (what the BLE subsystem is currently doing).
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, MaxSize)]
8#[cfg_attr(feature = "defmt", derive(defmt::Format))]
9#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
10#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
11pub enum BleState {
12    /// The BLE is advertising.
13    Advertising,
14    /// The BLE is connected.
15    Connected,
16    /// The BLE is not in use (USB mode or sleep mode, default).
17    Inactive,
18}
19
20/// Unified BLE status: which profile is active and what the BLE is doing.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, MaxSize)]
22#[cfg_attr(feature = "defmt", derive(defmt::Format))]
23#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
24#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
25pub struct BleStatus {
26    pub profile: u8,
27    pub state: BleState,
28}
29
30impl Default for BleStatus {
31    fn default() -> Self {
32        Self {
33            profile: 0,
34            state: BleState::Inactive,
35        }
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::{BleState, BleStatus};
42
43    #[test]
44    fn default_ble_status_is_profile_zero_and_inactive() {
45        assert_eq!(
46            BleStatus::default(),
47            BleStatus {
48                profile: 0,
49                state: BleState::Inactive,
50            }
51        );
52    }
53
54    #[test]
55    fn ble_status_variants_are_copy_and_comparable() {
56        let advertising = BleStatus {
57            profile: 0,
58            state: BleState::Advertising,
59        };
60        let connected = BleStatus {
61            profile: 2,
62            state: BleState::Connected,
63        };
64        let inactive = BleStatus::default();
65
66        assert_ne!(advertising, connected);
67        assert_ne!(connected, inactive);
68        assert_eq!(
69            inactive,
70            BleStatus {
71                profile: 0,
72                state: BleState::Inactive,
73            }
74        );
75    }
76}