Skip to main content

zmk_studio_api/transport/
mod.rs

1#[cfg(feature = "ble")]
2use std::time::Duration;
3
4/// A discoverable ZMK Studio BLE device.
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct BleDeviceInfo {
7    pub device_id: String,
8    pub local_name: Option<String>,
9}
10
11impl BleDeviceInfo {
12    pub fn display_name(&self) -> String {
13        match &self.local_name {
14            Some(name) if !name.is_empty() => format!("{} [{}]", name, self.device_id),
15            _ => self.device_id.clone(),
16        }
17    }
18}
19
20/// BLE discovery intent.
21///
22/// The bluest backend supports `Connected` and `Any` on all platforms.
23/// `Advertising` is not supported and will return an error.
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum BleDiscoveryMode {
26    /// Discover devices currently advertising ZMK Studio BLE service data.
27    Advertising,
28    /// Discover devices already paired/connected in the OS BLE device store.
29    Connected,
30    /// Use the best available backend strategy for the current platform.
31    Any,
32}
33
34#[cfg(feature = "ble")]
35pub(crate) const ZMK_SERVICE_UUID_STR: &str = "00000000-0196-6107-c967-c5cfb1c2482a";
36#[cfg(feature = "ble")]
37pub(crate) const ZMK_RPC_CHAR_UUID_STR: &str = "00000001-0196-6107-c967-c5cfb1c2482a";
38#[cfg(feature = "ble")]
39pub(crate) const DEFAULT_BLE_READ_TIMEOUT: Duration = Duration::from_secs(5);
40#[cfg(feature = "ble")]
41pub(crate) const DEFAULT_BLE_SETUP_TIMEOUT: Duration = Duration::from_secs(15);
42#[cfg(feature = "ble")]
43pub(crate) const DEFAULT_BLE_WRITE_QUEUE_CAPACITY: usize = 32;
44
45#[cfg(feature = "ble")]
46pub mod ble;
47#[cfg(feature = "ble")]
48mod blocking_ble;
49#[cfg(feature = "serial")]
50pub mod serial;
51
52#[cfg(feature = "ble")]
53pub type PlatformBleTransport = ble::BluestTransport;
54
55#[cfg(feature = "ble")]
56pub type PlatformBleError = ble::BluestTransportError;
57
58#[cfg(feature = "ble")]
59pub fn discover_platform_ble_devices(
60    mode: BleDiscoveryMode,
61) -> Result<Vec<BleDeviceInfo>, PlatformBleError> {
62    ble::discover_devices_with_mode(mode)
63}