Skip to main content

fips_core/config/transport/
ble.rs

1use super::*;
2
3// ============================================================================
4// BLE Transport Configuration
5// ============================================================================
6
7/// Default BLE L2CAP PSM (dynamic range).
8const DEFAULT_BLE_PSM: u16 = 0x0085;
9
10/// Default BLE MTU for L2CAP CoC connections.
11const DEFAULT_BLE_MTU: u16 = 2048;
12
13/// Default maximum concurrent BLE connections.
14const DEFAULT_BLE_MAX_CONNECTIONS: usize = 7;
15
16/// Default BLE connect timeout in milliseconds.
17const DEFAULT_BLE_CONNECT_TIMEOUT_MS: u64 = 10_000;
18
19/// Default BLE probe cooldown in seconds. After probing an address
20/// (success or failure), wait this long before probing it again.
21const DEFAULT_BLE_PROBE_COOLDOWN_SECS: u64 = 30;
22
23/// BLE transport instance configuration.
24///
25/// BleConfig is always compiled (for config parsing on any platform),
26/// but the transport runtime requires Linux and the `ble` feature.
27#[derive(Debug, Clone, Default, Serialize, Deserialize)]
28#[serde(deny_unknown_fields)]
29pub struct BleConfig {
30    /// HCI adapter name (e.g., "hci0"). Required.
31    #[serde(default, skip_serializing_if = "Option::is_none")]
32    pub adapter: Option<String>,
33
34    /// L2CAP PSM for FIPS connections. Default: 0x0085 (133).
35    #[serde(default, skip_serializing_if = "Option::is_none")]
36    pub psm: Option<u16>,
37
38    /// Default MTU for BLE connections. Default: 2048.
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    pub mtu: Option<u16>,
41
42    /// Maximum concurrent BLE connections. Default: 7.
43    #[serde(default, skip_serializing_if = "Option::is_none")]
44    pub max_connections: Option<usize>,
45
46    /// Outbound connect timeout in milliseconds. Default: 10000.
47    #[serde(default, skip_serializing_if = "Option::is_none")]
48    pub connect_timeout_ms: Option<u64>,
49
50    /// Broadcast BLE advertisements. Default: true.
51    #[serde(default, skip_serializing_if = "Option::is_none")]
52    pub advertise: Option<bool>,
53
54    /// Listen for BLE advertisements. Default: true.
55    #[serde(default, skip_serializing_if = "Option::is_none")]
56    pub scan: Option<bool>,
57
58    /// Auto-connect to discovered BLE peers. Default: false.
59    #[serde(default, skip_serializing_if = "Option::is_none")]
60    pub auto_connect: Option<bool>,
61
62    /// Accept incoming BLE connections. Default: true.
63    #[serde(default, skip_serializing_if = "Option::is_none")]
64    pub accept_connections: Option<bool>,
65
66    /// Probe cooldown in seconds. After probing a BLE address, wait
67    /// this long before probing the same address again. Default: 30.
68    #[serde(default, skip_serializing_if = "Option::is_none")]
69    pub probe_cooldown_secs: Option<u64>,
70}
71
72impl BleConfig {
73    /// Get the adapter name. Default: "hci0".
74    pub fn adapter(&self) -> &str {
75        self.adapter.as_deref().unwrap_or("hci0")
76    }
77
78    /// Get the L2CAP PSM. Default: 0x0085.
79    pub fn psm(&self) -> u16 {
80        self.psm.unwrap_or(DEFAULT_BLE_PSM)
81    }
82
83    /// Get the default MTU. Default: 2048.
84    pub fn mtu(&self) -> u16 {
85        self.mtu.unwrap_or(DEFAULT_BLE_MTU)
86    }
87
88    /// Get the maximum concurrent connections. Default: 7.
89    pub fn max_connections(&self) -> usize {
90        self.max_connections.unwrap_or(DEFAULT_BLE_MAX_CONNECTIONS)
91    }
92
93    /// Get the connect timeout in milliseconds. Default: 10000.
94    pub fn connect_timeout_ms(&self) -> u64 {
95        self.connect_timeout_ms
96            .unwrap_or(DEFAULT_BLE_CONNECT_TIMEOUT_MS)
97    }
98
99    /// Whether to broadcast advertisements. Default: true.
100    pub fn advertise(&self) -> bool {
101        self.advertise.unwrap_or(true)
102    }
103
104    /// Whether to scan for advertisements. Default: true.
105    pub fn scan(&self) -> bool {
106        self.scan.unwrap_or(true)
107    }
108
109    /// Whether to auto-connect to discovered peers. Default: false.
110    pub fn auto_connect(&self) -> bool {
111        self.auto_connect.unwrap_or(false)
112    }
113
114    /// Whether to accept incoming connections. Default: true.
115    pub fn accept_connections(&self) -> bool {
116        self.accept_connections.unwrap_or(true)
117    }
118
119    /// Get the probe cooldown in seconds. Default: 30.
120    pub fn probe_cooldown_secs(&self) -> u64 {
121        self.probe_cooldown_secs
122            .unwrap_or(DEFAULT_BLE_PROBE_COOLDOWN_SECS)
123    }
124}