-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathlib.rs
479 lines (432 loc) · 15.3 KB
/
lib.rs
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
//! Trouble is a Bluetooth Low Energy (BLE) Host implementation that communicates
//! with a controller over any transport implementing the traits from the `bt-hci`
//! crate.
//!
//! Trouble can run on embedded devices (`no_std`) and be configured to consume
//! as little resources are needed depending on your required configuration.
#![no_std]
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(clippy::needless_lifetimes)]
#![warn(missing_docs)]
use core::mem::MaybeUninit;
use advertise::AdvertisementDataError;
use bt_hci::cmd::status::ReadRssi;
use bt_hci::cmd::{AsyncCmd, SyncCmd};
use bt_hci::FromHciBytesError;
use crate::att::AttErrorCode;
use crate::channel_manager::{ChannelStorage, PacketChannel};
use crate::connection_manager::{ConnectionStorage, EventChannel};
use crate::l2cap::sar::SarType;
use crate::packet_pool::PacketPool;
use bt_hci::param::{AddrKind, BdAddr};
mod fmt;
#[cfg(not(any(feature = "central", feature = "peripheral")))]
compile_error!("Must enable at least one of the `central` or `peripheral` features");
pub mod att;
#[cfg(feature = "central")]
pub mod central;
mod channel_manager;
mod codec;
mod command;
pub mod config;
mod connection_manager;
mod cursor;
pub mod packet_pool;
mod pdu;
#[cfg(feature = "peripheral")]
pub mod peripheral;
pub mod types;
#[cfg(feature = "peripheral")]
use peripheral::*;
#[cfg(feature = "central")]
use central::*;
pub mod advertise;
pub mod connection;
#[cfg(feature = "gatt")]
pub mod gap;
pub mod l2cap;
#[cfg(feature = "scan")]
pub mod scan;
#[cfg(test)]
pub(crate) mod mock_controller;
pub(crate) mod host;
use host::{AdvHandleState, BleHost, HostMetrics, Runner};
#[allow(missing_docs)]
pub mod prelude {
pub use super::Host;
pub use bt_hci::param::{AddrKind, BdAddr, LeConnRole as Role};
pub use bt_hci::uuid::*;
#[cfg(feature = "derive")]
pub use heapless::String as HeaplessString;
#[cfg(feature = "derive")]
pub use trouble_host_macros::*;
pub use super::att::AttErrorCode;
pub use super::{BleHostError, Controller, Error, HostResources, Stack};
#[cfg(feature = "peripheral")]
pub use crate::advertise::*;
#[cfg(feature = "gatt")]
pub use crate::attribute::*;
#[cfg(feature = "gatt")]
pub use crate::attribute_server::*;
#[cfg(feature = "central")]
pub use crate::central::*;
pub use crate::connection::*;
#[cfg(feature = "gatt")]
pub use crate::gap::*;
#[cfg(feature = "gatt")]
pub use crate::gatt::*;
pub use crate::host::{ControlRunner, HostMetrics, Runner, RxRunner, TxRunner};
pub use crate::l2cap::*;
pub use crate::packet_pool::PacketPool;
#[cfg(feature = "peripheral")]
pub use crate::peripheral::*;
#[cfg(feature = "scan")]
pub use crate::scan::*;
#[cfg(feature = "gatt")]
pub use crate::types::gatt_traits::{FixedGattValue, GattValue};
pub use crate::Address;
}
#[cfg(feature = "gatt")]
pub mod attribute;
#[cfg(feature = "gatt")]
mod attribute_server;
#[cfg(feature = "gatt")]
pub mod gatt;
/// A BLE address.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Address {
/// Address type.
pub kind: AddrKind,
/// Address value.
pub addr: BdAddr,
}
impl Address {
/// Create a new random address.
pub fn random(val: [u8; 6]) -> Self {
Self {
kind: AddrKind::RANDOM,
addr: BdAddr::new(val),
}
}
}
/// Errors returned by the host.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum BleHostError<E> {
/// Error from the controller.
Controller(E),
/// Error from the host.
BleHost(Error),
}
/// Errors related to Host.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
/// Error encoding parameters for HCI commands.
Hci(bt_hci::param::Error),
/// Error decoding responses from HCI commands.
HciDecode(FromHciBytesError),
/// Error from the Attribute Protocol.
Att(AttErrorCode),
/// Insufficient space in the buffer.
InsufficientSpace,
/// Invalid value.
InvalidValue,
/// Error decoding advertisement data.
Advertisement(AdvertisementDataError),
/// Invalid l2cap channel id provided.
InvalidChannelId,
/// No l2cap channel available.
NoChannelAvailable,
/// Resource not found.
NotFound,
/// Invalid state.
InvalidState,
/// Out of memory.
OutOfMemory,
/// Unsupported operation.
NotSupported,
/// L2cap channel closed.
ChannelClosed,
/// Operation timed out.
Timeout,
/// Controller is busy.
Busy,
/// No send permits available.
NoPermits,
/// Connection is disconnected.
Disconnected,
/// Other error.
Other,
}
impl<E> From<Error> for BleHostError<E> {
fn from(value: Error) -> Self {
Self::BleHost(value)
}
}
impl From<FromHciBytesError> for Error {
fn from(error: FromHciBytesError) -> Self {
Self::HciDecode(error)
}
}
impl From<AttErrorCode> for Error {
fn from(error: AttErrorCode) -> Self {
Self::Att(error)
}
}
impl<E> From<bt_hci::cmd::Error<E>> for BleHostError<E> {
fn from(error: bt_hci::cmd::Error<E>) -> Self {
match error {
bt_hci::cmd::Error::Hci(p) => Self::BleHost(Error::Hci(p)),
bt_hci::cmd::Error::Io(p) => Self::Controller(p),
}
}
}
impl<E> From<bt_hci::param::Error> for BleHostError<E> {
fn from(error: bt_hci::param::Error) -> Self {
Self::BleHost(Error::Hci(error))
}
}
impl From<codec::Error> for Error {
fn from(error: codec::Error) -> Self {
match error {
codec::Error::InsufficientSpace => Error::InsufficientSpace,
codec::Error::InvalidValue => Error::InvalidValue,
}
}
}
impl<E> From<codec::Error> for BleHostError<E> {
fn from(error: codec::Error) -> Self {
match error {
codec::Error::InsufficientSpace => BleHostError::BleHost(Error::InsufficientSpace),
codec::Error::InvalidValue => BleHostError::BleHost(Error::InvalidValue),
}
}
}
use bt_hci::cmd::controller_baseband::*;
use bt_hci::cmd::le::*;
use bt_hci::cmd::link_control::*;
use bt_hci::controller::{ControllerCmdAsync, ControllerCmdSync};
/// Trait that defines the controller implementation required by the host.
///
/// The controller must implement the required commands and events to be able to be used with Trouble.
pub trait Controller:
bt_hci::controller::Controller
+ embedded_io::ErrorType
+ ControllerCmdSync<LeReadBufferSize>
+ ControllerCmdSync<Disconnect>
+ ControllerCmdSync<SetEventMask>
+ ControllerCmdSync<LeSetEventMask>
+ ControllerCmdSync<LeSetRandomAddr>
+ ControllerCmdSync<HostBufferSize>
+ ControllerCmdAsync<LeConnUpdate>
+ ControllerCmdSync<LeReadFilterAcceptListSize>
+ ControllerCmdSync<SetControllerToHostFlowControl>
+ ControllerCmdSync<Reset>
+ ControllerCmdSync<ReadRssi>
+ ControllerCmdSync<LeCreateConnCancel>
+ ControllerCmdSync<LeSetScanEnable>
+ ControllerCmdSync<LeSetExtScanEnable>
+ ControllerCmdAsync<LeCreateConn>
+ ControllerCmdSync<LeClearFilterAcceptList>
+ ControllerCmdSync<LeAddDeviceToFilterAcceptList>
+ for<'t> ControllerCmdSync<LeSetAdvEnable>
+ for<'t> ControllerCmdSync<LeSetExtAdvEnable<'t>>
+ for<'t> ControllerCmdSync<HostNumberOfCompletedPackets<'t>>
+ ControllerCmdSync<LeReadBufferSize>
+ for<'t> ControllerCmdSync<LeSetAdvData>
+ ControllerCmdSync<LeSetAdvParams>
+ for<'t> ControllerCmdSync<LeSetAdvEnable>
+ for<'t> ControllerCmdSync<LeSetScanResponseData>
{
}
impl<
C: bt_hci::controller::Controller
+ embedded_io::ErrorType
+ ControllerCmdSync<LeReadBufferSize>
+ ControllerCmdSync<Disconnect>
+ ControllerCmdSync<SetEventMask>
+ ControllerCmdSync<LeSetEventMask>
+ ControllerCmdSync<LeSetRandomAddr>
+ ControllerCmdSync<HostBufferSize>
+ ControllerCmdAsync<LeConnUpdate>
+ ControllerCmdSync<LeReadFilterAcceptListSize>
+ ControllerCmdSync<LeClearFilterAcceptList>
+ ControllerCmdSync<LeAddDeviceToFilterAcceptList>
+ ControllerCmdSync<SetControllerToHostFlowControl>
+ ControllerCmdSync<Reset>
+ ControllerCmdSync<ReadRssi>
+ ControllerCmdSync<LeSetScanEnable>
+ ControllerCmdSync<LeSetExtScanEnable>
+ ControllerCmdSync<LeCreateConnCancel>
+ ControllerCmdAsync<LeCreateConn>
+ for<'t> ControllerCmdSync<LeSetAdvEnable>
+ for<'t> ControllerCmdSync<LeSetExtAdvEnable<'t>>
+ for<'t> ControllerCmdSync<HostNumberOfCompletedPackets<'t>>
+ ControllerCmdSync<LeReadBufferSize>
+ for<'t> ControllerCmdSync<LeSetAdvData>
+ ControllerCmdSync<LeSetAdvParams>
+ for<'t> ControllerCmdSync<LeSetAdvEnable>
+ for<'t> ControllerCmdSync<LeSetScanResponseData>,
> Controller for C
{
}
/// HostResources holds the resources used by the host.
///
/// The l2cap packet pool is used by the host to handle inbound data, by allocating space for
/// incoming packets and dispatching to the appropriate connection and channel.
pub struct HostResources<const CONNS: usize, const CHANNELS: usize, const L2CAP_MTU: usize, const ADV_SETS: usize = 1> {
rx_pool: MaybeUninit<PacketPool<L2CAP_MTU, { config::L2CAP_RX_PACKET_POOL_SIZE }>>,
#[cfg(feature = "gatt")]
tx_pool: MaybeUninit<PacketPool<L2CAP_MTU, { config::L2CAP_TX_PACKET_POOL_SIZE }>>,
connections: MaybeUninit<[ConnectionStorage; CONNS]>,
events: MaybeUninit<[EventChannel; CONNS]>,
channels: MaybeUninit<[ChannelStorage; CHANNELS]>,
channels_rx: MaybeUninit<[PacketChannel<{ config::L2CAP_RX_QUEUE_SIZE }>; CHANNELS]>,
sar: MaybeUninit<[SarType; CONNS]>,
advertise_handles: MaybeUninit<[AdvHandleState; ADV_SETS]>,
}
impl<const CONNS: usize, const CHANNELS: usize, const L2CAP_MTU: usize, const ADV_SETS: usize> Default
for HostResources<CONNS, CHANNELS, L2CAP_MTU, ADV_SETS>
{
fn default() -> Self {
Self::new()
}
}
impl<const CONNS: usize, const CHANNELS: usize, const L2CAP_MTU: usize, const ADV_SETS: usize>
HostResources<CONNS, CHANNELS, L2CAP_MTU, ADV_SETS>
{
/// Create a new instance of host resources.
pub const fn new() -> Self {
Self {
rx_pool: MaybeUninit::uninit(),
#[cfg(feature = "gatt")]
tx_pool: MaybeUninit::uninit(),
connections: MaybeUninit::uninit(),
events: MaybeUninit::uninit(),
sar: MaybeUninit::uninit(),
channels: MaybeUninit::uninit(),
channels_rx: MaybeUninit::uninit(),
advertise_handles: MaybeUninit::uninit(),
}
}
}
/// Create a new instance of the BLE host using the provided controller implementation and
/// the resource configuration
pub fn new<
'resources,
C: Controller,
const CONNS: usize,
const CHANNELS: usize,
const L2CAP_MTU: usize,
const ADV_SETS: usize,
>(
controller: C,
resources: &'resources mut HostResources<CONNS, CHANNELS, L2CAP_MTU, ADV_SETS>,
) -> Stack<'resources, C> {
unsafe fn transmute_slice<T>(x: &mut [T]) -> &'static mut [T] {
core::mem::transmute(x)
}
// Safety:
// - HostResources has the exceeding lifetime as the returned Stack.
// - Internal lifetimes are elided (made 'static) to simplify API usage
// - This _should_ be OK, because there are no references held to the resources
// when the stack is shut down.
use crate::packet_pool::Pool;
let rx_pool: &'resources dyn Pool = &*resources.rx_pool.write(PacketPool::new());
let rx_pool = unsafe { core::mem::transmute::<&'resources dyn Pool, &'static dyn Pool>(rx_pool) };
#[cfg(feature = "gatt")]
let tx_pool: &'resources dyn Pool = &*resources.tx_pool.write(PacketPool::new());
#[cfg(feature = "gatt")]
let tx_pool = unsafe { core::mem::transmute::<&'resources dyn Pool, &'static dyn Pool>(tx_pool) };
use crate::l2cap::sar::AssembledPacket;
use crate::types::l2cap::L2capHeader;
use bt_hci::param::ConnHandle;
let connections: &mut [ConnectionStorage] =
&mut *resources.connections.write([ConnectionStorage::DISCONNECTED; CONNS]);
let connections: &'resources mut [ConnectionStorage] = unsafe { transmute_slice(connections) };
let events: &mut [EventChannel] = &mut *resources.events.write([EventChannel::NEW; CONNS]);
let events: &'resources mut [EventChannel] = unsafe { transmute_slice(events) };
let channels = &mut *resources.channels.write([ChannelStorage::DISCONNECTED; CHANNELS]);
let channels: &'static mut [ChannelStorage] = unsafe { transmute_slice(channels) };
let channels_rx: &mut [PacketChannel<{ config::L2CAP_RX_QUEUE_SIZE }>] =
&mut *resources.channels_rx.write([PacketChannel::NEW; CHANNELS]);
let channels_rx: &'static mut [PacketChannel<{ config::L2CAP_RX_QUEUE_SIZE }>] =
unsafe { transmute_slice(channels_rx) };
let sar = &mut *resources.sar.write([const { None }; CONNS]);
let sar: &'static mut [Option<(ConnHandle, L2capHeader, AssembledPacket)>] = unsafe { transmute_slice(sar) };
let advertise_handles = &mut *resources.advertise_handles.write([AdvHandleState::None; ADV_SETS]);
let advertise_handles: &'static mut [AdvHandleState] = unsafe { transmute_slice(advertise_handles) };
let host: BleHost<'_, C> = BleHost::new(
controller,
rx_pool,
#[cfg(feature = "gatt")]
tx_pool,
connections,
events,
channels,
channels_rx,
sar,
advertise_handles,
);
Stack { host }
}
/// Contains the host stack
pub struct Stack<'stack, C> {
host: BleHost<'stack, C>,
}
/// Host components.
#[non_exhaustive]
pub struct Host<'stack, C> {
/// Central role
#[cfg(feature = "central")]
pub central: Central<'stack, C>,
/// Peripheral role
#[cfg(feature = "peripheral")]
pub peripheral: Peripheral<'stack, C>,
/// Host runner
pub runner: Runner<'stack, C>,
}
impl<'stack, C: Controller> Stack<'stack, C> {
/// Set the random address used by this host.
pub fn set_random_address(mut self, address: Address) -> Self {
self.host.address.replace(address);
self
}
/// Build the stack.
pub fn build(&'stack self) -> Host<'stack, C> {
Host {
#[cfg(feature = "central")]
central: Central::new(self),
#[cfg(feature = "peripheral")]
peripheral: Peripheral::new(self),
runner: Runner::new(self),
}
}
/// Run a HCI command and return the response.
pub async fn command<T>(&self, cmd: T) -> Result<T::Return, BleHostError<C::Error>>
where
T: SyncCmd,
C: ControllerCmdSync<T>,
{
self.host.command(cmd).await
}
/// Run an async HCI command where the response will generate an event later.
pub async fn async_command<T>(&self, cmd: T) -> Result<(), BleHostError<C::Error>>
where
T: AsyncCmd,
C: ControllerCmdAsync<T>,
{
self.host.async_command(cmd).await
}
/// Read current host metrics
pub fn metrics(&self) -> HostMetrics {
self.host.metrics()
}
/// Log status information of the host
pub fn log_status(&self, verbose: bool) {
self.host.log_status(verbose);
}
}