Skip to main content

browser_protocol/bluetoothemulation/
mod.rs

1//! This domain allows configuring virtual Bluetooth devices to test
2//! the web-bluetooth API.
3
4use serde::{Serialize, Deserialize};
5
6/// Indicates the various states of Central.
7
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
9pub enum CentralState {
10    #[default]
11    Absent,
12    PoweredOff,
13    PoweredOn,
14}
15
16/// Indicates the various types of GATT event.
17
18#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
19pub enum GATTOperationType {
20    #[default]
21    Connection,
22    Discovery,
23}
24
25/// Indicates the various types of characteristic write.
26
27#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
28pub enum CharacteristicWriteType {
29    #[default]
30    WriteDefaultDeprecated,
31    WriteWithResponse,
32    WriteWithoutResponse,
33}
34
35/// Indicates the various types of characteristic operation.
36
37#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
38pub enum CharacteristicOperationType {
39    #[default]
40    Read,
41    Write,
42    SubscribeToNotifications,
43    UnsubscribeFromNotifications,
44}
45
46/// Indicates the various types of descriptor operation.
47
48#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
49pub enum DescriptorOperationType {
50    #[default]
51    Read,
52    Write,
53}
54
55/// Stores the manufacturer data
56
57#[derive(Debug, Clone, Serialize, Deserialize, Default)]
58#[serde(rename_all = "camelCase")]
59pub struct ManufacturerData {
60    /// Company identifier
61    /// <https://bitbucket.org/bluetooth-SIG/public/src/main/assigned_numbers/company_identifiers/company_identifiers.yaml>
62    /// <https://usb.org/developers>
63
64    pub key: i64,
65    /// Manufacturer-specific data (Encoded as a base64 string when passed over JSON)
66
67    pub data: String,
68}
69
70/// Stores the byte data of the advertisement packet sent by a Bluetooth device.
71
72#[derive(Debug, Clone, Serialize, Deserialize, Default)]
73#[serde(rename_all = "camelCase")]
74pub struct ScanRecord {
75
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub name: Option<String>,
78
79    #[serde(skip_serializing_if = "Option::is_none")]
80    pub uuids: Option<Vec<String>>,
81    /// Stores the external appearance description of the device.
82
83    #[serde(skip_serializing_if = "Option::is_none")]
84    pub appearance: Option<i64>,
85    /// Stores the transmission power of a broadcasting device.
86
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub txPower: Option<i64>,
89    /// Key is the company identifier and the value is an array of bytes of
90    /// manufacturer specific data.
91
92    #[serde(skip_serializing_if = "Option::is_none")]
93    pub manufacturerData: Option<Vec<ManufacturerData>>,
94}
95
96/// Stores the advertisement packet information that is sent by a Bluetooth device.
97
98#[derive(Debug, Clone, Serialize, Deserialize, Default)]
99#[serde(rename_all = "camelCase")]
100pub struct ScanEntry {
101
102    pub deviceAddress: String,
103
104    pub rssi: i64,
105
106    pub scanRecord: ScanRecord,
107}
108
109/// Describes the properties of a characteristic. This follows Bluetooth Core
110/// Specification BT 4.2 Vol 3 Part G 3.3.1. Characteristic Properties.
111
112#[derive(Debug, Clone, Serialize, Deserialize, Default)]
113#[serde(rename_all = "camelCase")]
114pub struct CharacteristicProperties {
115
116    #[serde(skip_serializing_if = "Option::is_none")]
117    pub broadcast: Option<bool>,
118
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub read: Option<bool>,
121
122    #[serde(skip_serializing_if = "Option::is_none")]
123    pub writeWithoutResponse: Option<bool>,
124
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub write: Option<bool>,
127
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub notify: Option<bool>,
130
131    #[serde(skip_serializing_if = "Option::is_none")]
132    pub indicate: Option<bool>,
133
134    #[serde(skip_serializing_if = "Option::is_none")]
135    pub authenticatedSignedWrites: Option<bool>,
136
137    #[serde(skip_serializing_if = "Option::is_none")]
138    pub extendedProperties: Option<bool>,
139}
140
141/// Enable the BluetoothEmulation domain.
142
143#[derive(Debug, Clone, Serialize, Deserialize, Default)]
144#[serde(rename_all = "camelCase")]
145pub struct EnableParams {
146    /// State of the simulated central.
147
148    pub state: CentralState,
149    /// If the simulated central supports low-energy.
150
151    pub leSupported: bool,
152}
153
154/// Set the state of the simulated central.
155
156#[derive(Debug, Clone, Serialize, Deserialize, Default)]
157#[serde(rename_all = "camelCase")]
158pub struct SetSimulatedCentralStateParams {
159    /// State of the simulated central.
160
161    pub state: CentralState,
162}
163
164/// Simulates a peripheral with |address|, |name| and |knownServiceUuids|
165/// that has already been connected to the system.
166
167#[derive(Debug, Clone, Serialize, Deserialize, Default)]
168#[serde(rename_all = "camelCase")]
169pub struct SimulatePreconnectedPeripheralParams {
170
171    pub address: String,
172
173    pub name: String,
174
175    pub manufacturerData: Vec<ManufacturerData>,
176
177    pub knownServiceUuids: Vec<String>,
178}
179
180/// Simulates an advertisement packet described in |entry| being received by
181/// the central.
182
183#[derive(Debug, Clone, Serialize, Deserialize, Default)]
184#[serde(rename_all = "camelCase")]
185pub struct SimulateAdvertisementParams {
186
187    pub entry: ScanEntry,
188}
189
190/// Simulates the response code from the peripheral with |address| for a
191/// GATT operation of |type|. The |code| value follows the HCI Error Codes from
192/// Bluetooth Core Specification Vol 2 Part D 1.3 List Of Error Codes.
193
194#[derive(Debug, Clone, Serialize, Deserialize, Default)]
195#[serde(rename_all = "camelCase")]
196pub struct SimulateGATTOperationResponseParams {
197
198    pub address: String,
199
200    #[serde(rename = "type")]
201    pub type_: GATTOperationType,
202
203    pub code: i64,
204}
205
206/// Simulates the response from the characteristic with |characteristicId| for a
207/// characteristic operation of |type|. The |code| value follows the Error
208/// Codes from Bluetooth Core Specification Vol 3 Part F 3.4.1.1 Error Response.
209/// The |data| is expected to exist when simulating a successful read operation
210/// response.
211
212#[derive(Debug, Clone, Serialize, Deserialize, Default)]
213#[serde(rename_all = "camelCase")]
214pub struct SimulateCharacteristicOperationResponseParams {
215
216    pub characteristicId: String,
217
218    #[serde(rename = "type")]
219    pub type_: CharacteristicOperationType,
220
221    pub code: i64,
222
223    #[serde(skip_serializing_if = "Option::is_none")]
224    pub data: Option<String>,
225}
226
227/// Simulates the response from the descriptor with |descriptorId| for a
228/// descriptor operation of |type|. The |code| value follows the Error
229/// Codes from Bluetooth Core Specification Vol 3 Part F 3.4.1.1 Error Response.
230/// The |data| is expected to exist when simulating a successful read operation
231/// response.
232
233#[derive(Debug, Clone, Serialize, Deserialize, Default)]
234#[serde(rename_all = "camelCase")]
235pub struct SimulateDescriptorOperationResponseParams {
236
237    pub descriptorId: String,
238
239    #[serde(rename = "type")]
240    pub type_: DescriptorOperationType,
241
242    pub code: i64,
243
244    #[serde(skip_serializing_if = "Option::is_none")]
245    pub data: Option<String>,
246}
247
248/// Adds a service with |serviceUuid| to the peripheral with |address|.
249
250#[derive(Debug, Clone, Serialize, Deserialize, Default)]
251#[serde(rename_all = "camelCase")]
252pub struct AddServiceParams {
253
254    pub address: String,
255
256    pub serviceUuid: String,
257}
258
259/// Adds a service with |serviceUuid| to the peripheral with |address|.
260
261#[derive(Debug, Clone, Serialize, Deserialize, Default)]
262#[serde(rename_all = "camelCase")]
263pub struct AddServiceReturns {
264    /// An identifier that uniquely represents this service.
265
266    pub serviceId: String,
267}
268
269/// Removes the service respresented by |serviceId| from the simulated central.
270
271#[derive(Debug, Clone, Serialize, Deserialize, Default)]
272#[serde(rename_all = "camelCase")]
273pub struct RemoveServiceParams {
274
275    pub serviceId: String,
276}
277
278/// Adds a characteristic with |characteristicUuid| and |properties| to the
279/// service represented by |serviceId|.
280
281#[derive(Debug, Clone, Serialize, Deserialize, Default)]
282#[serde(rename_all = "camelCase")]
283pub struct AddCharacteristicParams {
284
285    pub serviceId: String,
286
287    pub characteristicUuid: String,
288
289    pub properties: CharacteristicProperties,
290}
291
292/// Adds a characteristic with |characteristicUuid| and |properties| to the
293/// service represented by |serviceId|.
294
295#[derive(Debug, Clone, Serialize, Deserialize, Default)]
296#[serde(rename_all = "camelCase")]
297pub struct AddCharacteristicReturns {
298    /// An identifier that uniquely represents this characteristic.
299
300    pub characteristicId: String,
301}
302
303/// Removes the characteristic respresented by |characteristicId| from the
304/// simulated central.
305
306#[derive(Debug, Clone, Serialize, Deserialize, Default)]
307#[serde(rename_all = "camelCase")]
308pub struct RemoveCharacteristicParams {
309
310    pub characteristicId: String,
311}
312
313/// Adds a descriptor with |descriptorUuid| to the characteristic respresented
314/// by |characteristicId|.
315
316#[derive(Debug, Clone, Serialize, Deserialize, Default)]
317#[serde(rename_all = "camelCase")]
318pub struct AddDescriptorParams {
319
320    pub characteristicId: String,
321
322    pub descriptorUuid: String,
323}
324
325/// Adds a descriptor with |descriptorUuid| to the characteristic respresented
326/// by |characteristicId|.
327
328#[derive(Debug, Clone, Serialize, Deserialize, Default)]
329#[serde(rename_all = "camelCase")]
330pub struct AddDescriptorReturns {
331    /// An identifier that uniquely represents this descriptor.
332
333    pub descriptorId: String,
334}
335
336/// Removes the descriptor with |descriptorId| from the simulated central.
337
338#[derive(Debug, Clone, Serialize, Deserialize, Default)]
339#[serde(rename_all = "camelCase")]
340pub struct RemoveDescriptorParams {
341
342    pub descriptorId: String,
343}
344
345/// Simulates a GATT disconnection from the peripheral with |address|.
346
347#[derive(Debug, Clone, Serialize, Deserialize, Default)]
348#[serde(rename_all = "camelCase")]
349pub struct SimulateGATTDisconnectionParams {
350
351    pub address: String,
352}