bluez 0.4.0

Control Bluetooth on Linux.
Documentation
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
use crate::AddressType;
use std::collections::HashMap;

use crate::management::interface::ControllerInfoExt;
use crate::util::BufExt;

use super::*;

/// This command returns the Management version and revision.
///	Besides, being informational the information can be used to
///	determine whether certain behavior has changed or bugs fixed
///	when interacting with the kernel.
pub async fn get_mgmt_version(
    socket: &mut ManagementStream,
    event_tx: Option<mpsc::Sender<Response>>,
) -> Result<ManagementVersion> {
    let (_, param) = exec_command(
        socket,
        Command::ReadVersionInfo,
        Controller::none(),
        None,
        event_tx,
    )
    .await?;

    let mut param = param.ok_or(Error::NoData)?;
    Ok(ManagementVersion {
        version: param.get_u8(),
        revision: param.get_u16_le(),
    })
}

/// This command returns the list of currently known controllers.
///	Controllers added or removed after calling this command can be
///	monitored using the Index Added and Index Removed events.
pub async fn get_controller_list(
    socket: &mut ManagementStream,
    event_tx: Option<mpsc::Sender<Response>>,
) -> Result<Vec<Controller>> {
    let (_, param) = exec_command(
        socket,
        Command::ReadControllerIndexList,
        Controller::none(),
        None,
        event_tx,
    )
    .await?;

    let mut param = param.unwrap();
    let count = param.get_u16_le() as usize;
    let mut controllers = vec![Controller::none(); count];
    for i in 0..count {
        controllers[i] = Controller(param.get_u16_le());
    }

    Ok(controllers)
}

/// This command is used to retrieve the current state and basic
///	information of a controller. It is typically used right after
///	getting the response to the Read Controller Index List command
///	or an Index Added event.
///
///	The `address` parameter describes the controllers public address
///	and it can be expected that it is set. However in case of single
///	mode Low Energy only controllers it can be `00:00:00:00:00:00`. To
///	power on the controller in this case, it is required to configure
///	a static address using Set Static `address` command first.
///
///	If the public address is set, then it will be used as identity
///	address for the controller. If no public address is available,
///	then the configured static address will be used as identity
///	address.
///
///	In the case of a dual-mode controller with public address that
///	is configured as Low Energy only device (BR/EDR switched off),
///	the static address is used when set and public address otherwise.
///
///	If no short name is set the Short_Name parameter will be all zeroes.
pub async fn get_controller_info(
    socket: &mut ManagementStream,
    controller: Controller,
    event_tx: Option<mpsc::Sender<Response>>,
) -> Result<ControllerInfo> {
    let (_, param) = exec_command(
        socket,
        Command::ReadControllerInfo,
        controller,
        None,
        event_tx,
    )
    .await?;

    let mut param = param.ok_or(Error::NoData)?;

    Ok(ControllerInfo {
        address: param.get_address(),
        bluetooth_version: param.get_u8(),
        manufacturer: param.get_u16_le(),
        supported_settings: param.get_flags_u32_le(),
        current_settings: param.get_flags_u32_le(),
        class_of_device: device_class_from_bytes(param.split_to(3)),
        name: param.split_to(249).get_c_string(),
        short_name: param.get_c_string(),
    })
}

///	This command is used to retrieve a list of currently connected
///	devices.
///
///	For devices using resolvable random addresses with a known
///	identity resolving key, the `address` and `address_type` will
///	contain the identity information.
///
///	This command can only be used when the controller is powered.
pub async fn get_connections(
    socket: &mut ManagementStream,
    controller: Controller,
    event_tx: Option<mpsc::Sender<Response>>,
) -> Result<Vec<(Address, AddressType)>> {
    let (_, param) =
        exec_command(socket, Command::GetConnections, controller, None, event_tx).await?;

    let mut param = param.ok_or(Error::NoData)?;
    let count = param.get_u16_le() as usize;
    let mut connections = Vec::with_capacity(count);

    for _ in 0..count {
        connections.push((param.get_address(), param.get_primitive_u8()));
    }

    Ok(connections)
}

/// This command is used to get connection information.
pub async fn get_connection_info(
    socket: &mut ManagementStream,
    controller: Controller,
    address: Address,
    address_type: AddressType,
    event_tx: Option<mpsc::Sender<Response>>,
) -> Result<ConnectionInfo> {
    let mut param = BytesMut::with_capacity(7);
    param.put_slice(address.as_ref());
    param.put_u8(address_type as u8);

    let (_, param) = exec_command(
        socket,
        Command::GetConnectionInfo,
        controller,
        Some(param.freeze()),
        event_tx,
    )
    .await?;

    let mut param = param.ok_or(Error::NoData)?;
    Ok(ConnectionInfo {
        address: param.get_address(),
        address_type: param.get_primitive_u8(),
        rssi: if param[0] != 127 {
            Some(param.get_i8())
        } else {
            None
        },
        tx_power: if param[0] != 127 {
            Some(param.get_i8())
        } else {
            None
        },
        max_tx_power: if param[0] != 127 {
            Some(param.get_i8())
        } else {
            None
        },
    })
}

/// This command is used to get local and piconet clock information.
pub async fn get_clock_info(
    socket: &mut ManagementStream,
    controller: Controller,
    address: Address,
    address_type: AddressType,
    event_tx: Option<mpsc::Sender<Response>>,
) -> Result<ClockInfo> {
    let mut param = BytesMut::with_capacity(7);
    param.put_slice(address.as_ref());
    param.put_u8(address_type as u8);

    let (_, param) = exec_command(
        socket,
        Command::GetClockInfo,
        controller,
        Some(param.freeze()),
        event_tx,
    )
    .await?;

    let mut param = param.ok_or(Error::NoData)?;

    let address = param.get_address();
    let address_type = param.get_primitive_u8();
    let local_clock = param.get_u32_le();

    let mut piconet_clock = None;
    let mut accuracy = None;

    if address != Address::zero() {
        piconet_clock = Some(param.get_u32_le());
        let accuracy_tmp = param.get_u16_le();
        if accuracy_tmp != 0xFFFF {
            accuracy = Some(accuracy_tmp);
        }
    }

    Ok(ClockInfo {
        address,
        address_type,
        local_clock,
        piconet_clock,
        accuracy,
    })
}

///	This command returns the list of currently unconfigured controllers.
///	Unconfigured controllers added after calling this command can be
///	monitored using the Unconfigured Index Added event.
///
///	An unconfigured controller can either move to a configured state
///	by indicating Unconfigured Index Removed event followed by an
///	Index Added event; or it can be removed from the system which
///	would be indicated by the Unconfigured Index Removed event.
///
///	Only controllers that require configuration will be listed with
///	this command. A controller that is fully configured will not
///	be listed even if it supports configuration changes.
pub async fn get_unconfigured_controller_list(
    socket: &mut ManagementStream,
    event_tx: Option<mpsc::Sender<Response>>,
) -> Result<Vec<Controller>> {
    let (_, param) = exec_command(
        socket,
        Command::ReadUnconfiguredControllerIndexList,
        Controller::none(),
        None,
        event_tx,
    )
    .await?;

    let mut param = param.ok_or(Error::NoData)?;
    let count = param.get_u16_le() as usize;
    let mut controllers = vec![Controller::none(); count];
    for i in 0..count {
        controllers[i] = Controller(param.get_u16_le());
    }

    Ok(controllers)
}

///	This command is used to retrieve the supported configuration
///	options of a controller and the missing configuration options.
///
///	The missing options are required to be configured before the
///	controller is considered fully configured and ready for standard
///	operation. The command is typically used right after getting the
///	response to Read Unconfigured Controller Index List command or
///	Unconfigured Index Added event.
///
///	Supported_Options and Missing_Options is a bitmask with currently
///	the following available bits:
///
///	0	External configuration
///	1	Bluetooth public address configuration
///
///	It is valid to call this command on controllers that do not
///	require any configuration. It is possible that a fully configured
///	controller offers additional support for configuration.
///
///	For example a controller may contain a valid Bluetooth public
///	device address, but also allows to configure it from the host
///	stack. In this case the general support for configurations will
///	be indicated by the Controller Configuration settings. For
///	controllers where no configuration options are available that
///	setting option will not be present.
///
///	When all configurations have been completed and as a result the
///	Missing_Options mask would become empty, then the now ready
///	controller will be announced via Index Added event.
pub async fn get_controller_config_info(
    socket: &mut ManagementStream,
    controller: Controller,
    event_tx: Option<mpsc::Sender<Response>>,
) -> Result<ControllerConfigInfo> {
    let (_, param) = exec_command(
        socket,
        Command::ReadControllerConfigInfo,
        controller,
        None,
        event_tx,
    )
    .await?;

    let mut param = param.ok_or(Error::NoData)?;
    Ok(ControllerConfigInfo {
        manufacturer: param.get_u16_le(),
        supported_options: param.get_flags_u32_le(),
        missing_options: param.get_flags_u32_le(),
    })
}

/// This command returns the list of currently known controllers. It
///	includes configured, unconfigured and alternate controllers.
///
///	Controllers added or removed after calling this command can be
///	be monitored using the Extended Index Added and Extended Index
///	Removed events.
///
///	The existing Index Added, Index Removed, Unconfigured Index Added
///	and Unconfigured Index Removed are no longer sent after this command
///	has been used at least once.
///
///	Instead of calling Read Controller Index List and Read Unconfigured
///	Controller Index List, this command combines all the information
///	and can be used to retrieve the controller list.
///
/// Controllers marked as RAW only operation are currently not listed
///	by this command.
pub async fn get_ext_controller_list(
    socket: &mut ManagementStream,
    event_tx: Option<mpsc::Sender<Response>>,
) -> Result<Vec<(Controller, ControllerType, ControllerBus)>> {
    let (_, param) = exec_command(
        socket,
        Command::ReadExtendedControllerIndexList,
        Controller::none(),
        None,
        event_tx,
    )
    .await?;

    let mut param = param.ok_or(Error::NoData)?;
    let count = param.get_u16_le() as usize;
    let mut index = Vec::with_capacity(count);
    for _ in 0..count {
        index.push((
            Controller(param.get_u16_le()),
            param.get_primitive_u8(),
            param.get_primitive_u8(),
        ));
    }
    Ok(index)
}

/// This command is used to retrieve the current state and basic
///	information of a controller. It is typically used right after
///	getting the response to the Read Controller Index List command
///	or an Index Added event (or its extended counterparts).
///
///	The Address parameter describes the controllers public address
///	and it can be expected that it is set. However in case of single
///	mode Low Energy only controllers it can be 00:00:00:00:00:00. To
///	power on the controller in this case, it is required to configure
///	a static address using Set Static Address command first.
///
///	If the public address is set, then it will be used as identity
///	address for the controller. If no public address is available,
///	then the configured static address will be used as identity
///	address.
///
///	In the case of a dual-mode controller with public address that
///	is configured as Low Energy only device (BR/EDR switched off),
///	the static address is used when set and public address otherwise.
pub async fn get_ext_controller_info(
    socket: &mut ManagementStream,
    controller: Controller,
    event_tx: Option<mpsc::Sender<Response>>,
) -> Result<ControllerInfoExt> {
    let (_, param) = exec_command(
        socket,
        Command::ReadExtendedControllerInfo,
        controller,
        None,
        event_tx,
    )
    .await?;

    let mut param = param.ok_or(Error::NoData)?;

    Ok(ControllerInfoExt {
        address: param.get_address(),
        bluetooth_version: param.get_u8(),
        manufacturer: param.get_u16_le(),
        supported_settings: param.get_flags_u32_le(),
        current_settings: param.get_flags_u32_le(),
        eir_data: {
            let len = param.get_u16_le();
            param.split_to(len as usize)
        },
    })
}

/// If BR/EDR is supported, then BR 1M 1-Slot is supported by
///	default and can also not be deselected. If LE is supported,
///	then LE 1M TX and LE 1M RX are supported by default.
///
///	Disabling BR/EDR completely or respectively LE has no impact
///	on the PHY configuration. It is remembered over power cycles.
pub async fn get_phy_config(
    socket: &mut ManagementStream,
    controller: Controller,
    event_tx: Option<mpsc::Sender<Response>>,
) -> Result<PhyConfig> {
    let (_, param) =
        exec_command(socket, Command::GetPhyConfig, controller, None, event_tx).await?;

    let mut param = param.ok_or(Error::NoData)?;
    Ok(PhyConfig {
        supported_phys: param.get_flags_u32_le(),
        configurable_phys: param.get_flags_u32_le(),
        selected_phys: param.get_flags_u32_le(),
    })
}

/// Currently no Parameter_Type values are defined and an empty list
/// will be returned.
///
/// This command can be used at any time and will return a list of
/// supported default parameters as well as their current value.
pub async fn get_default_runtime_config(
    socket: &mut ManagementStream,
    controller: Controller,
    event_tx: Option<mpsc::Sender<Response>>,
) -> Result<HashMap<RuntimeConfigParameterType, Vec<u8>>> {
    let (_, param) = exec_command(
        socket,
        Command::ReadDefaultRuntimeConfig,
        controller,
        None,
        event_tx,
    )
    .await?;

    let mut param = param.ok_or(Error::NoData)?;
    Ok(param.get_tlv_map())
}

/// This command can be used at any time and will return a list of
/// supported default parameters as well as their current value.
pub async fn get_default_system_config(
    socket: &mut ManagementStream,
    controller: Controller,
    event_tx: Option<mpsc::Sender<Response>>,
) -> Result<HashMap<SystemConfigParameterType, Vec<u8>>> {
    let (_, param) = exec_command(
        socket,
        Command::ReadDefaultSystemConfig,
        controller,
        None,
        event_tx,
    )
    .await?;

    let mut param = param.ok_or(Error::NoData)?;
    Ok(param.get_tlv_map())
}