Skip to main content

browser_protocol/bluetoothemulation/
mod.rs

1//! This domain allows configuring virtual Bluetooth devices to test
2//! the web-bluetooth API.
3use serde::{Serialize, Deserialize};
4use serde_json::Value as JsonValue;
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
154impl EnableParams { pub const METHOD: &'static str = "BluetoothEmulation.enable"; }
155
156impl crate::CdpCommand for EnableParams {
157    const METHOD: &'static str = "BluetoothEmulation.enable";
158    type Response = crate::EmptyReturns;
159}
160
161/// Set the state of the simulated central.
162
163#[derive(Debug, Clone, Serialize, Deserialize, Default)]
164#[serde(rename_all = "camelCase")]
165pub struct SetSimulatedCentralStateParams {
166    /// State of the simulated central.
167
168    pub state: CentralState,
169}
170
171impl SetSimulatedCentralStateParams { pub const METHOD: &'static str = "BluetoothEmulation.setSimulatedCentralState"; }
172
173impl crate::CdpCommand for SetSimulatedCentralStateParams {
174    const METHOD: &'static str = "BluetoothEmulation.setSimulatedCentralState";
175    type Response = crate::EmptyReturns;
176}
177
178#[derive(Debug, Clone, Serialize, Deserialize, Default)]
179pub struct DisableParams {}
180
181impl DisableParams { pub const METHOD: &'static str = "BluetoothEmulation.disable"; }
182
183impl crate::CdpCommand for DisableParams {
184    const METHOD: &'static str = "BluetoothEmulation.disable";
185    type Response = crate::EmptyReturns;
186}
187
188/// Simulates a peripheral with |address|, |name| and |knownServiceUuids|
189/// that has already been connected to the system.
190
191#[derive(Debug, Clone, Serialize, Deserialize, Default)]
192#[serde(rename_all = "camelCase")]
193pub struct SimulatePreconnectedPeripheralParams {
194
195    pub address: String,
196
197    pub name: String,
198
199    pub manufacturerData: Vec<ManufacturerData>,
200
201    pub knownServiceUuids: Vec<String>,
202}
203
204impl SimulatePreconnectedPeripheralParams { pub const METHOD: &'static str = "BluetoothEmulation.simulatePreconnectedPeripheral"; }
205
206impl crate::CdpCommand for SimulatePreconnectedPeripheralParams {
207    const METHOD: &'static str = "BluetoothEmulation.simulatePreconnectedPeripheral";
208    type Response = crate::EmptyReturns;
209}
210
211/// Simulates an advertisement packet described in |entry| being received by
212/// the central.
213
214#[derive(Debug, Clone, Serialize, Deserialize, Default)]
215#[serde(rename_all = "camelCase")]
216pub struct SimulateAdvertisementParams {
217
218    pub entry: ScanEntry,
219}
220
221impl SimulateAdvertisementParams { pub const METHOD: &'static str = "BluetoothEmulation.simulateAdvertisement"; }
222
223impl crate::CdpCommand for SimulateAdvertisementParams {
224    const METHOD: &'static str = "BluetoothEmulation.simulateAdvertisement";
225    type Response = crate::EmptyReturns;
226}
227
228/// Simulates the response code from the peripheral with |address| for a
229/// GATT operation of |type|. The |code| value follows the HCI Error Codes from
230/// Bluetooth Core Specification Vol 2 Part D 1.3 List Of Error Codes.
231
232#[derive(Debug, Clone, Serialize, Deserialize, Default)]
233#[serde(rename_all = "camelCase")]
234pub struct SimulateGATTOperationResponseParams {
235
236    pub address: String,
237
238    #[serde(rename = "type")]
239    pub type_: GATTOperationType,
240
241    pub code: i64,
242}
243
244impl SimulateGATTOperationResponseParams { pub const METHOD: &'static str = "BluetoothEmulation.simulateGATTOperationResponse"; }
245
246impl crate::CdpCommand for SimulateGATTOperationResponseParams {
247    const METHOD: &'static str = "BluetoothEmulation.simulateGATTOperationResponse";
248    type Response = crate::EmptyReturns;
249}
250
251/// Simulates the response from the characteristic with |characteristicId| for a
252/// characteristic operation of |type|. The |code| value follows the Error
253/// Codes from Bluetooth Core Specification Vol 3 Part F 3.4.1.1 Error Response.
254/// The |data| is expected to exist when simulating a successful read operation
255/// response.
256
257#[derive(Debug, Clone, Serialize, Deserialize, Default)]
258#[serde(rename_all = "camelCase")]
259pub struct SimulateCharacteristicOperationResponseParams {
260
261    pub characteristicId: String,
262
263    #[serde(rename = "type")]
264    pub type_: CharacteristicOperationType,
265
266    pub code: i64,
267
268    #[serde(skip_serializing_if = "Option::is_none")]
269    pub data: Option<String>,
270}
271
272impl SimulateCharacteristicOperationResponseParams { pub const METHOD: &'static str = "BluetoothEmulation.simulateCharacteristicOperationResponse"; }
273
274impl crate::CdpCommand for SimulateCharacteristicOperationResponseParams {
275    const METHOD: &'static str = "BluetoothEmulation.simulateCharacteristicOperationResponse";
276    type Response = crate::EmptyReturns;
277}
278
279/// Simulates the response from the descriptor with |descriptorId| for a
280/// descriptor operation of |type|. The |code| value follows the Error
281/// Codes from Bluetooth Core Specification Vol 3 Part F 3.4.1.1 Error Response.
282/// The |data| is expected to exist when simulating a successful read operation
283/// response.
284
285#[derive(Debug, Clone, Serialize, Deserialize, Default)]
286#[serde(rename_all = "camelCase")]
287pub struct SimulateDescriptorOperationResponseParams {
288
289    pub descriptorId: String,
290
291    #[serde(rename = "type")]
292    pub type_: DescriptorOperationType,
293
294    pub code: i64,
295
296    #[serde(skip_serializing_if = "Option::is_none")]
297    pub data: Option<String>,
298}
299
300impl SimulateDescriptorOperationResponseParams { pub const METHOD: &'static str = "BluetoothEmulation.simulateDescriptorOperationResponse"; }
301
302impl crate::CdpCommand for SimulateDescriptorOperationResponseParams {
303    const METHOD: &'static str = "BluetoothEmulation.simulateDescriptorOperationResponse";
304    type Response = crate::EmptyReturns;
305}
306
307/// Adds a service with |serviceUuid| to the peripheral with |address|.
308
309#[derive(Debug, Clone, Serialize, Deserialize, Default)]
310#[serde(rename_all = "camelCase")]
311pub struct AddServiceParams {
312
313    pub address: String,
314
315    pub serviceUuid: String,
316}
317
318/// Adds a service with |serviceUuid| to the peripheral with |address|.
319
320#[derive(Debug, Clone, Serialize, Deserialize, Default)]
321#[serde(rename_all = "camelCase")]
322pub struct AddServiceReturns {
323    /// An identifier that uniquely represents this service.
324
325    pub serviceId: String,
326}
327
328impl AddServiceParams { pub const METHOD: &'static str = "BluetoothEmulation.addService"; }
329
330impl crate::CdpCommand for AddServiceParams {
331    const METHOD: &'static str = "BluetoothEmulation.addService";
332    type Response = AddServiceReturns;
333}
334
335/// Removes the service respresented by |serviceId| from the simulated central.
336
337#[derive(Debug, Clone, Serialize, Deserialize, Default)]
338#[serde(rename_all = "camelCase")]
339pub struct RemoveServiceParams {
340
341    pub serviceId: String,
342}
343
344impl RemoveServiceParams { pub const METHOD: &'static str = "BluetoothEmulation.removeService"; }
345
346impl crate::CdpCommand for RemoveServiceParams {
347    const METHOD: &'static str = "BluetoothEmulation.removeService";
348    type Response = crate::EmptyReturns;
349}
350
351/// Adds a characteristic with |characteristicUuid| and |properties| to the
352/// service represented by |serviceId|.
353
354#[derive(Debug, Clone, Serialize, Deserialize, Default)]
355#[serde(rename_all = "camelCase")]
356pub struct AddCharacteristicParams {
357
358    pub serviceId: String,
359
360    pub characteristicUuid: String,
361
362    pub properties: CharacteristicProperties,
363}
364
365/// Adds a characteristic with |characteristicUuid| and |properties| to the
366/// service represented by |serviceId|.
367
368#[derive(Debug, Clone, Serialize, Deserialize, Default)]
369#[serde(rename_all = "camelCase")]
370pub struct AddCharacteristicReturns {
371    /// An identifier that uniquely represents this characteristic.
372
373    pub characteristicId: String,
374}
375
376impl AddCharacteristicParams { pub const METHOD: &'static str = "BluetoothEmulation.addCharacteristic"; }
377
378impl crate::CdpCommand for AddCharacteristicParams {
379    const METHOD: &'static str = "BluetoothEmulation.addCharacteristic";
380    type Response = AddCharacteristicReturns;
381}
382
383/// Removes the characteristic respresented by |characteristicId| from the
384/// simulated central.
385
386#[derive(Debug, Clone, Serialize, Deserialize, Default)]
387#[serde(rename_all = "camelCase")]
388pub struct RemoveCharacteristicParams {
389
390    pub characteristicId: String,
391}
392
393impl RemoveCharacteristicParams { pub const METHOD: &'static str = "BluetoothEmulation.removeCharacteristic"; }
394
395impl crate::CdpCommand for RemoveCharacteristicParams {
396    const METHOD: &'static str = "BluetoothEmulation.removeCharacteristic";
397    type Response = crate::EmptyReturns;
398}
399
400/// Adds a descriptor with |descriptorUuid| to the characteristic respresented
401/// by |characteristicId|.
402
403#[derive(Debug, Clone, Serialize, Deserialize, Default)]
404#[serde(rename_all = "camelCase")]
405pub struct AddDescriptorParams {
406
407    pub characteristicId: String,
408
409    pub descriptorUuid: String,
410}
411
412/// Adds a descriptor with |descriptorUuid| to the characteristic respresented
413/// by |characteristicId|.
414
415#[derive(Debug, Clone, Serialize, Deserialize, Default)]
416#[serde(rename_all = "camelCase")]
417pub struct AddDescriptorReturns {
418    /// An identifier that uniquely represents this descriptor.
419
420    pub descriptorId: String,
421}
422
423impl AddDescriptorParams { pub const METHOD: &'static str = "BluetoothEmulation.addDescriptor"; }
424
425impl crate::CdpCommand for AddDescriptorParams {
426    const METHOD: &'static str = "BluetoothEmulation.addDescriptor";
427    type Response = AddDescriptorReturns;
428}
429
430/// Removes the descriptor with |descriptorId| from the simulated central.
431
432#[derive(Debug, Clone, Serialize, Deserialize, Default)]
433#[serde(rename_all = "camelCase")]
434pub struct RemoveDescriptorParams {
435
436    pub descriptorId: String,
437}
438
439impl RemoveDescriptorParams { pub const METHOD: &'static str = "BluetoothEmulation.removeDescriptor"; }
440
441impl crate::CdpCommand for RemoveDescriptorParams {
442    const METHOD: &'static str = "BluetoothEmulation.removeDescriptor";
443    type Response = crate::EmptyReturns;
444}
445
446/// Simulates a GATT disconnection from the peripheral with |address|.
447
448#[derive(Debug, Clone, Serialize, Deserialize, Default)]
449#[serde(rename_all = "camelCase")]
450pub struct SimulateGATTDisconnectionParams {
451
452    pub address: String,
453}
454
455impl SimulateGATTDisconnectionParams { pub const METHOD: &'static str = "BluetoothEmulation.simulateGATTDisconnection"; }
456
457impl crate::CdpCommand for SimulateGATTDisconnectionParams {
458    const METHOD: &'static str = "BluetoothEmulation.simulateGATTDisconnection";
459    type Response = crate::EmptyReturns;
460}