bluetooth_traits/
lib.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5pub mod blocklist;
6pub mod scanfilter;
7
8use ipc_channel::ipc::IpcSender;
9use serde::{Deserialize, Serialize};
10
11use crate::scanfilter::{BluetoothScanfilterSequence, RequestDeviceoptions};
12
13#[derive(Debug, Deserialize, Serialize)]
14pub enum BluetoothError {
15    Type(String),
16    Network,
17    NotFound,
18    NotSupported,
19    Security,
20    InvalidState,
21}
22
23#[derive(Debug, Deserialize, Serialize)]
24pub enum GATTType {
25    PrimaryService,
26    Characteristic,
27    IncludedService,
28    Descriptor,
29}
30
31#[derive(Debug, Deserialize, Serialize)]
32pub struct BluetoothDeviceMsg {
33    // Bluetooth Device properties
34    pub id: String,
35    pub name: Option<String>,
36}
37
38#[derive(Debug, Deserialize, Serialize)]
39pub struct BluetoothServiceMsg {
40    pub uuid: String,
41    pub is_primary: bool,
42    pub instance_id: String,
43}
44
45#[derive(Debug, Deserialize, Serialize)]
46pub struct BluetoothCharacteristicMsg {
47    // Characteristic
48    pub uuid: String,
49    pub instance_id: String,
50    // Characteristic properties
51    pub broadcast: bool,
52    pub read: bool,
53    pub write_without_response: bool,
54    pub write: bool,
55    pub notify: bool,
56    pub indicate: bool,
57    pub authenticated_signed_writes: bool,
58    pub reliable_write: bool,
59    pub writable_auxiliaries: bool,
60}
61
62#[derive(Debug, Deserialize, Serialize)]
63pub struct BluetoothDescriptorMsg {
64    pub uuid: String,
65    pub instance_id: String,
66}
67
68pub type BluetoothServicesMsg = Vec<BluetoothServiceMsg>;
69
70pub type BluetoothCharacteristicsMsg = Vec<BluetoothCharacteristicMsg>;
71
72pub type BluetoothDescriptorsMsg = Vec<BluetoothDescriptorMsg>;
73
74pub type BluetoothResult<T> = Result<T, BluetoothError>;
75
76pub type BluetoothResponseResult = Result<BluetoothResponse, BluetoothError>;
77
78#[derive(Debug, Deserialize, Serialize)]
79pub enum BluetoothRequest {
80    RequestDevice(RequestDeviceoptions, IpcSender<BluetoothResponseResult>),
81    GATTServerConnect(String, IpcSender<BluetoothResponseResult>),
82    GATTServerDisconnect(String, IpcSender<BluetoothResult<()>>),
83    GetGATTChildren(
84        String,
85        Option<String>,
86        bool,
87        GATTType,
88        IpcSender<BluetoothResponseResult>,
89    ),
90    ReadValue(String, IpcSender<BluetoothResponseResult>),
91    WriteValue(String, Vec<u8>, IpcSender<BluetoothResponseResult>),
92    EnableNotification(String, bool, IpcSender<BluetoothResponseResult>),
93    WatchAdvertisements(String, IpcSender<BluetoothResponseResult>),
94    SetRepresentedToNull(Vec<String>, Vec<String>, Vec<String>),
95    IsRepresentedDeviceNull(String, IpcSender<bool>),
96    GetAvailability(IpcSender<BluetoothResponseResult>),
97    MatchesFilter(
98        String,
99        BluetoothScanfilterSequence,
100        IpcSender<BluetoothResult<bool>>,
101    ),
102    Test(String, IpcSender<BluetoothResult<()>>),
103    Exit,
104}
105
106#[derive(Debug, Deserialize, Serialize)]
107pub enum BluetoothResponse {
108    RequestDevice(BluetoothDeviceMsg),
109    GATTServerConnect(bool),
110    GetPrimaryServices(BluetoothServicesMsg, bool),
111    GetIncludedServices(BluetoothServicesMsg, bool),
112    GetCharacteristics(BluetoothCharacteristicsMsg, bool),
113    GetDescriptors(BluetoothDescriptorsMsg, bool),
114    ReadValue(Vec<u8>),
115    WriteValue(Vec<u8>),
116    EnableNotification(()),
117    WatchAdvertisements(()),
118    GetAvailability(bool),
119}