tauri-plugin-blec 0.8.1

BLE-Client plugin for Tauri
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use crate::ALLOW_IBEACONS;
use async_trait::async_trait;
use base64::Engine;
use btleplug::{
    api::{
        BDAddr, CentralEvent, CentralState, CharPropFlags, Characteristic, Descriptor,
        PeripheralProperties, Service, ValueNotification, WriteType,
    },
    platform::PeripheralId,
};
use futures::Stream;
use once_cell::sync::{Lazy, OnceCell};
use serde::Deserialize;
use std::time::Duration;
use std::{
    collections::{BTreeSet, HashMap},
    pin::Pin,
    vec,
};
use tauri::{
    ipc::{Channel, InvokeResponseBody},
    plugin::PluginHandle,
    AppHandle, Wry,
};
use tokio::sync::RwLock;
use tokio_stream::wrappers::ReceiverStream;
use tracing::{debug, info};
use uuid::Uuid;

use crate::models::BondingPeripheral;

type Result<T> = std::result::Result<T, btleplug::Error>;

static HANDLE: OnceCell<PluginHandle<Wry>> = OnceCell::new();

fn get_handle() -> &'static PluginHandle<Wry> {
    HANDLE.get().expect("plugin handle not initialized")
}

pub fn init<C: serde::de::DeserializeOwned>(
    _app: &AppHandle<Wry>,
    api: tauri::plugin::PluginApi<Wry, C>,
) -> std::result::Result<(), crate::error::Error> {
    let handle = api.register_android_plugin("com.plugin.blec", "BleClientPlugin")?;
    HANDLE.set(handle).unwrap();
    Ok(())
}

#[derive(Debug, Clone)]
pub struct Adapter;
static DEVICES: Lazy<RwLock<HashMap<PeripheralId, Peripheral>>> =
    Lazy::new(|| RwLock::new(HashMap::new()));

#[derive(serde::Deserialize)]
struct PeripheralResult {
    result: Peripheral,
}

fn on_device_callback(response: InvokeResponseBody) -> std::result::Result<(), tauri::Error> {
    let device = match response.deserialize::<PeripheralResult>() {
        Ok(PeripheralResult { result }) => result,
        Err(e) => {
            tracing::error!("failed to deserialize peripheral: {:?}", e);
            return Err(tauri::Error::from(e));
        }
    };
    let mut devices = DEVICES.blocking_write();
    tracing::trace!("device: {device:?}");
    if let Some(enty) = devices.get_mut(&device.id) {
        *enty = device;
    } else {
        devices.insert(device.id.clone(), device);
    }
    Ok(())
}

pub fn check_permissions(
    ask_if_denied: bool,
) -> std::result::Result<bool, tauri::plugin::mobile::PluginInvokeError> {
    let result: BoolResult = get_handle().run_mobile_plugin(
        "check_permissions",
        serde_json::json!({
            "askIfDenied": ask_if_denied
        }),
    )?;
    Ok(result.result)
}

#[allow(dependency_on_unit_never_type_fallback)]
#[async_trait]
impl btleplug::api::Central for Adapter {
    type Peripheral = Peripheral;

    async fn events(&self) -> Result<Pin<Box<dyn Stream<Item = CentralEvent> + Send>>> {
        let (tx, rx) = tokio::sync::mpsc::channel::<CentralEvent>(1);
        let stream = ReceiverStream::new(rx);
        let channel: Channel = Channel::new(move |response| {
            let event = response
                .deserialize::<CentralEvent>()
                .expect("failed to deserialize event");
            debug!("sending event: {event:?}");
            tx.blocking_send(event)
                .expect("failed to send notification");
            Ok(())
        });
        get_handle()
            .run_mobile_plugin("events", channel)
            .map_err(|e| btleplug::Error::RuntimeError(e.to_string()))?;
        Ok(Box::pin(stream))
    }

    async fn start_scan(&self, filter: btleplug::api::ScanFilter) -> Result<()> {
        #[derive(serde::Serialize)]
        #[serde(rename_all = "camelCase")]
        struct ScanParams {
            services: Vec<Uuid>,
            allow_ibeacons: bool,
            on_device: Channel<serde_json::Value>,
        }
        DEVICES.write().await.clear();
        let on_device = Channel::new(on_device_callback);
        get_handle()
            .run_mobile_plugin(
                "start_scan",
                ScanParams {
                    services: filter.services,
                    allow_ibeacons: *ALLOW_IBEACONS.lock().await,
                    on_device,
                },
            )
            .map_err(|e| btleplug::Error::RuntimeError(e.to_string()))?;
        Ok(())
    }

    async fn stop_scan(&self) -> Result<()> {
        get_handle()
            .run_mobile_plugin("stop_scan", serde_json::Value::Null)
            .map_err(|e| btleplug::Error::RuntimeError(e.to_string()))?;
        Ok(())
    }

    async fn peripherals(&self) -> Result<Vec<Self::Peripheral>> {
        Ok(DEVICES.read().await.values().cloned().collect())
    }

    async fn peripheral(&self, id: &PeripheralId) -> Result<Self::Peripheral> {
        DEVICES
            .read()
            .await
            .get(&id)
            .cloned()
            .ok_or(btleplug::Error::DeviceNotFound)
    }

    async fn add_peripheral(&self, _address: &PeripheralId) -> Result<Self::Peripheral> {
        Err(btleplug::Error::NotSupported("add_peripheral".to_string()))
    }

    async fn adapter_info(&self) -> Result<String> {
        todo!()
    }

    async fn adapter_state(&self) -> Result<CentralState> {
        let res: StringResult = get_handle()
            .run_mobile_plugin("adapter_state", serde_json::Value::Null)
            .map_err(|e| btleplug::Error::RuntimeError(e.to_string()))?;
        match res.result.as_str() {
            "unknown" => Ok(CentralState::Unknown),
            "off" => Ok(CentralState::PoweredOff),
            "on" => Ok(CentralState::PoweredOn),
            _ => Err(btleplug::Error::RuntimeError(format!(
                "unknown adapter state: {}",
                res.result
            ))),
        }
    }
}

pub struct Manager;

impl Manager {
    pub async fn new() -> Result<Self> {
        Ok(Manager)
    }
}

#[allow(dependency_on_unit_never_type_fallback)]
#[async_trait]
impl btleplug::api::Manager for Manager {
    type Adapter = Adapter;

    async fn adapters(&self) -> Result<Vec<Adapter>> {
        Ok(vec![Adapter])
    }
}

fn deserialize_base64<'a, D>(deserializer: D) -> std::result::Result<Vec<u8>, D::Error>
where
    D: serde::Deserializer<'a>,
{
    let s = String::deserialize(deserializer)?;
    Ok(base64::engine::general_purpose::STANDARD
        .decode(s)
        .map_err(serde::de::Error::custom)?)
}

fn deserialize_base64_map<'a, D, K>(
    deserializer: D,
) -> std::result::Result<HashMap<K, Vec<u8>>, D::Error>
where
    D: serde::Deserializer<'a>,
    K: serde::Deserialize<'a> + std::hash::Hash + std::cmp::Eq,
{
    let map: HashMap<K, String> = serde::Deserialize::deserialize(deserializer)?;
    let mut res = HashMap::new();
    for (k, v) in map {
        res.insert(
            k,
            base64::engine::general_purpose::STANDARD
                .decode(v)
                .map_err(serde::de::Error::custom)?,
        );
    }
    Ok(res)
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Peripheral {
    id: PeripheralId,
    address: BDAddr,
    name: String,
    rssi: i16,
    #[serde(default, deserialize_with = "deserialize_base64_map")]
    manufacturer_data: HashMap<u16, Vec<u8>>,
    #[serde(default, deserialize_with = "deserialize_base64_map")]
    service_data: HashMap<Uuid, Vec<u8>>,
    #[serde(default)]
    services: Vec<Uuid>,
}
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct ConnectParams {
    address: BDAddr,
}
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct MtuParams {
    address: BDAddr,
    mtu: u16,
}

#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct MtuResponse {
    mtu: u16,
}

#[derive(serde::Deserialize)]
struct BoolResult {
    result: bool,
}

#[derive(serde::Deserialize)]
struct StringResult {
    result: String,
}

#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct ReadParams {
    address: BDAddr,
    characteristic: Uuid,
}

#[async_trait::async_trait]
impl BondingPeripheral for Peripheral {
    async fn is_bonded(&self) -> Result<bool> {
        let res: BoolResult = get_handle()
            .run_mobile_plugin(
                "is_bonded",
                ConnectParams {
                    address: self.address,
                },
            )
            .map_err(|e| btleplug::Error::RuntimeError(e.to_string()))?;
        Ok(res.result)
    }
}
#[allow(dependency_on_unit_never_type_fallback)]
#[async_trait::async_trait]
impl btleplug::api::Peripheral for Peripheral {
    fn id(&self) -> PeripheralId {
        self.id.clone()
    }

    fn address(&self) -> BDAddr {
        self.address
    }

    async fn properties(&self) -> Result<Option<PeripheralProperties>> {
        Ok(Some(PeripheralProperties {
            address: self.address,
            local_name: Some(self.name.clone()),
            rssi: Some(self.rssi),
            manufacturer_data: self.manufacturer_data.clone(),
            service_data: self.service_data.clone(),
            services: self.services.clone(),
            // TODO: implement the rest
            // at the moment not used by the handler or BleDevice struct so we can return default values
            address_type: Default::default(),
            class: Default::default(),
            tx_power_level: Default::default(),
        }))
    }

    fn services(&self) -> BTreeSet<Service> {
        #[derive(serde::Deserialize)]
        struct ResCharacteristic {
            uuid: Uuid,
            properties: u8,
            descriptors: Vec<Uuid>,
        }

        #[derive(serde::Deserialize)]
        struct ResService {
            uuid: Uuid,
            primary: bool,
            characs: Vec<ResCharacteristic>,
        }

        #[derive(serde::Deserialize)]
        struct ServicesResult {
            result: Vec<ResService>,
        }
        let res: ServicesResult = get_handle()
            .run_mobile_plugin(
                "services",
                ConnectParams {
                    address: self.address,
                },
            )
            .map_err(|e| btleplug::Error::RuntimeError(e.to_string()))
            .expect("failed to get services");
        let mut services = BTreeSet::new();
        for s in res.result {
            let mut characteristics = BTreeSet::new();
            for c in s.characs {
                let mut descriptors = BTreeSet::new();
                for d in c.descriptors {
                    descriptors.insert(Descriptor {
                        uuid: d,
                        characteristic_uuid: c.uuid,
                        service_uuid: s.uuid,
                    });
                }
                characteristics.insert(Characteristic {
                    uuid: c.uuid,
                    service_uuid: s.uuid,
                    properties: CharPropFlags::from_bits_truncate(c.properties),
                    descriptors,
                });
            }
            services.insert(Service {
                uuid: s.uuid,
                primary: s.primary,
                characteristics,
            });
        }
        services
    }

    async fn is_connected(&self) -> Result<bool> {
        let res: BoolResult = get_handle()
            .run_mobile_plugin(
                "is_connected",
                ConnectParams {
                    address: self.address,
                },
            )
            .map_err(|e| btleplug::Error::RuntimeError(e.to_string()))?;
        Ok(res.result)
    }

    async fn connect(&self) -> Result<()> {
        call_plugin_with_timeout(
            "connect",
            ConnectParams {
                address: self.address,
            },
        )
        .await?;
        info!("connected to: {:?}", self.address);
        debug!("requesting mtu");
        let mtu: MtuResponse = call_plugin_with_timeout(
            "request_mtu",
            MtuParams {
                address: self.address,
                mtu: 517,
            },
        )
        .await?;
        info!("mtu set to: {:?}", mtu.mtu);
        Ok(())
    }

    async fn disconnect(&self) -> Result<()> {
        call_plugin_with_timeout(
            "disconnect",
            ConnectParams {
                address: self.address,
            },
        )
        .await?;
        Ok(())
    }

    async fn discover_services(&self) -> Result<()> {
        call_plugin_with_timeout(
            "discover_services",
            ConnectParams {
                address: self.address,
            },
        )
        .await?;
        debug!("discover services plugin call returned");
        Ok(())
    }

    async fn write(
        &self,
        characteristic: &Characteristic,
        data: &[u8],
        write_type: WriteType,
    ) -> Result<()> {
        get_handle()
            .run_mobile_plugin(
                "write",
                serde_json::json!({
                    "address": self.address,
                    "characteristic": characteristic.uuid,
                    "data": data,
                    "withResponse": matches!(write_type, WriteType::WithResponse),
                }),
            )
            .map_err(|e| btleplug::Error::RuntimeError(e.to_string()))?;
        Ok(())
    }

    async fn read(&self, characteristic: &Characteristic) -> Result<Vec<u8>> {
        #[derive(serde::Deserialize)]
        struct ReadResult {
            #[serde(deserialize_with = "deserialize_base64")]
            value: Vec<u8>,
        }
        let res: ReadResult = get_handle()
            .run_mobile_plugin(
                "read",
                ReadParams {
                    address: self.address,
                    characteristic: characteristic.uuid,
                },
            )
            .map_err(|e| btleplug::Error::RuntimeError(e.to_string()))?;
        debug!("read: {:?}", res.value);
        Ok(res.value)
    }

    async fn subscribe(&self, characteristic: &Characteristic) -> Result<()> {
        get_handle()
            .run_mobile_plugin(
                "subscribe",
                ReadParams {
                    address: self.address,
                    characteristic: characteristic.uuid,
                },
            )
            .map_err(|e| btleplug::Error::RuntimeError(e.to_string()))?;
        Ok(())
    }

    async fn unsubscribe(&self, characteristic: &Characteristic) -> Result<()> {
        get_handle()
            .run_mobile_plugin(
                "unsubscribe",
                ReadParams {
                    address: self.address,
                    characteristic: characteristic.uuid,
                },
            )
            .map_err(|e| btleplug::Error::RuntimeError(e.to_string()))?;
        Ok(())
    }

    async fn notifications(&self) -> Result<Pin<Box<dyn Stream<Item = ValueNotification> + Send>>> {
        #[derive(serde::Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct Notification {
            uuid: Uuid,
            #[serde(deserialize_with = "deserialize_base64")]
            data: Vec<u8>,
        }
        #[derive(serde::Serialize)]
        #[serde(rename_all = "camelCase")]
        struct NotifyParams {
            address: BDAddr,
            channel: Channel<Notification>,
        }
        let (tx, rx) = tokio::sync::mpsc::channel::<ValueNotification>(1);
        let stream = ReceiverStream::new(rx);
        let channel: Channel<Notification> = Channel::new(move |response| {
            match response.deserialize::<Notification>() {
                Ok(notification) => tx
                    .blocking_send(ValueNotification {
                        uuid: notification.uuid,
                        value: notification.data,
                    })
                    .expect("failed to send notification"),
                Err(e) => {
                    tracing::error!("failed to deserialize notification: {:?}", e);
                    return Err(tauri::Error::from(e));
                }
            };
            Ok(())
        });
        get_handle()
            .run_mobile_plugin(
                "notifications",
                NotifyParams {
                    address: self.address,
                    channel,
                },
            )
            .map_err(|e| btleplug::Error::RuntimeError(e.to_string()))?;
        Ok(Box::pin(stream))
    }

    async fn write_descriptor(&self, _descriptor: &Descriptor, _data: &[u8]) -> Result<()> {
        todo!()
    }

    async fn read_descriptor(&self, _descriptor: &Descriptor) -> Result<Vec<u8>> {
        todo!()
    }
}

async fn call_plugin_with_timeout<
    P: serde::Serialize + Send + 'static,
    R: serde::de::DeserializeOwned + Send + 'static,
>(
    func: &'static str,
    params: P,
) -> Result<R> {
    let handle = tokio::task::spawn_blocking(move || {
        get_handle()
            .run_mobile_plugin(func, params)
            .map_err(|e| btleplug::Error::RuntimeError(e.to_string()))
    });
    tokio::time::timeout(Duration::from_secs(5), handle)
        .await
        .map_err(|_| btleplug::Error::RuntimeError(format!("timeout during {func}")))?
        .map_err(|e| btleplug::Error::RuntimeError(format!("tokio join error: {e}")))?
}