-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathlib.rs
142 lines (124 loc) · 3.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
//! 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(async_fn_in_trait)]
#![allow(dead_code)]
#![allow(unused_variables)]
use advertise::AdvertisementDataError;
pub use bt_hci::param::{AddrKind, BdAddr, LeConnRole as Role};
use bt_hci::FromHciBytesError;
mod fmt;
mod att;
mod channel_manager;
mod codec;
mod connection_manager;
mod cursor;
mod packet_pool;
mod pdu;
pub mod types;
pub use packet_pool::Qos as PacketQos;
pub mod advertise;
pub mod connection;
pub mod l2cap;
pub mod scan;
pub(crate) mod host;
pub use host::*;
#[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 {
pub kind: AddrKind,
pub addr: BdAddr,
}
impl 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> {
Controller(E),
BleHost(Error),
}
/// Errors related to Host.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
HciEncode(bt_hci::param::Error),
HciDecode(FromHciBytesError),
Att(att::AttDecodeError),
InsufficientSpace,
InvalidValue,
Advertisement(AdvertisementDataError),
InvalidChannelId,
NoChannelAvailable,
NotFound,
InvalidState,
OutOfMemory,
NotSupported,
ChannelClosed,
Timeout,
Busy,
NoPermits,
Disconnected,
Other,
}
impl<E> From<Error> for BleHostError<E> {
fn from(value: Error) -> Self {
Self::BleHost(value)
}
}
impl From<att::AttDecodeError> for Error {
fn from(error: att::AttDecodeError) -> Self {
Self::Att(error)
}
}
impl From<FromHciBytesError> for Error {
fn from(error: FromHciBytesError) -> Self {
Self::HciDecode(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::HciEncode(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::HciEncode(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),
}
}
}