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
use crate::interfaces::*;
use crate::introspect::*;
use crate::*;
use rustbus::params;
use rustbus::params::{Base, Param};
use rustbus::signature;
use std::time::Instant;
/// See the [Advertising API] for more details about what each field does.
///
/// [Advertising API]: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/advertising-api.txt
pub struct Advertisement {
pub typ: AdType,
pub service_uuids: Vec<UUID>,
pub manu_data: HashMap<u16, ([u8; 27], usize)>,
pub serv_dict: HashMap<UUID, ([u8; 27], usize)>,
pub solicit_uuids: Vec<UUID>,
pub includes: Vec<String>,
/// Defaults to `2`. Ignored if there is only one Advertisement active on the Bluez controller at once.
/// If there are multiple advertisements active on the Bluez controller at once
/// (including from other application), then they share time in a round-robin. This setting determines,
/// how long this advertisement will be active at a time in seconds, before handing of to the next
/// Advertisement.
pub duration: u16,
/// Defaults to `180`. The timeout of the advertisement in seconds. The timeout only counts time while the advertisement is active,
/// if there are multiple advertisement.
pub timeout: u16,
pub(crate) index: u16,
pub(crate) path: PathBuf,
pub appearance: u16,
pub localname: String,
pub(crate) active: bool,
}
impl Advertisement {
/// Creates a new advertisement that can be added the `Bluetooth` and registered with Bluez
/// using [`Bluetooth::start_adv()`].
///
/// [`Bluetooth::start_adv()`]: ./struct.Bluetooth.html#method.start_adv
pub fn new(typ: AdType, localname: String) -> Self {
Advertisement {
typ,
service_uuids: Vec::new(),
solicit_uuids: Vec::new(),
includes: Vec::new(),
timeout: 2,
duration: 180,
appearance: 0,
localname,
index: 0,
path: PathBuf::new(),
manu_data: HashMap::new(),
serv_dict: HashMap::new(),
active: false,
}
}
/// Validates the UUIDs in the advertisement.
pub fn validate(&self) -> Result<(), Error> {
for uuid in &self.service_uuids {
if !validate_uuid(uuid) {
return Err(Error::BadInput(format!(
"{} is an invalid uuid in service_uuids",
uuid
)));
}
}
for uuid in &self.solicit_uuids {
if !validate_uuid(uuid) {
return Err(Error::BadInput(format!(
"{} is an invalid uuid in solicit_uuids",
uuid
)));
}
}
Ok(())
}
}
pub enum AdType {
Peripheral,
Broadcast,
}
impl ToString for AdType {
fn to_string(&self) -> String {
match self {
AdType::Broadcast => "broadcast".to_string(),
AdType::Peripheral => "peripheral".to_string(),
}
}
}
impl AdType {
pub fn to_str(&self) -> &'static str {
match self {
AdType::Peripheral => "peripheral",
AdType::Broadcast => "broadcast",
}
}
}
impl Properties for Advertisement {
const INTERFACES: &'static [(&'static str, &'static [&'static str])] = &[LEAD_IF, PROP_IF];
fn get_inner<'a, 'b>(&mut self, interface: &str, prop: &str) -> Option<Param<'a, 'b>> {
match interface {
LEAD_IF_STR => match prop {
TYPE_PROP => Some(base_param_to_variant(self.typ.to_string().into())),
SERV_UUIDS_PROP => {
let service_uuids: Vec<Param> = self
.service_uuids
.iter()
.map(|x| Param::Base(x.to_string().into()))
.collect();
let array = params::Array {
values: service_uuids,
element_sig: signature::Type::Base(signature::Base::String),
};
Some(container_param_to_variant(Container::Array(array)))
}
MANU_DATA_PROP => {
//let manu_data = Param::Container(Container::Array(params::Array));
let base = signature::Type::Base(signature::Base::Byte);
let typ = signature::Type::Container(signature::Container::Array(Box::new(
base.clone(),
)));
let manu_data: HashMap<Base, Param> = self
.manu_data
.iter()
.map(|(key, (v, l))| {
let key = Base::Uint16(*key);
let byte_vec: Vec<Param> = v[..*l]
.iter()
.map(|x| Param::Base(Base::Byte(*x)))
.collect();
let array = Param::Container(Container::Array(params::Array {
element_sig: base.clone(),
values: byte_vec,
}));
(key, array)
})
.collect();
let manu_data_dict = params::Dict {
key_sig: signature::Base::Uint16,
value_sig: typ,
map: manu_data,
};
let cont = Container::Dict(manu_data_dict);
Some(container_param_to_variant(cont))
}
SOLICIT_UUIDS_PROP => {
let uuids: Vec<Param> = self
.solicit_uuids
.iter()
.map(|x| Param::Base(Base::String(x.to_string())))
.collect();
let array = params::Array {
values: uuids,
element_sig: signature::Type::Base(signature::Base::String),
};
Some(container_param_to_variant(Container::Array(array)))
}
SERV_DATA_PROP => {
let base = signature::Type::Base(signature::Base::Byte);
let typ = signature::Type::Container(signature::Container::Array(Box::new(
base.clone(),
)));
let serv_data: HashMap<Base, Param> = self
.serv_dict
.iter()
.map(|(key, (v, l))| {
let key = Base::String(key.to_string());
let byte_vec: Vec<Param> = v[..*l]
.iter()
.map(|x| Param::Base(Base::Byte(*x)))
.collect();
let array = Param::Container(Container::Array(params::Array {
element_sig: base.clone(),
values: byte_vec,
}));
(key, array)
})
.collect();
let serv_data_dict = params::Dict {
key_sig: signature::Base::String,
value_sig: typ,
map: serv_data,
};
let cont = Container::Dict(serv_data_dict);
Some(container_param_to_variant(cont))
}
DATA_PROP => unimplemented!(),
/*
DISCOVERABLE_PROP => unimplemented!(),
DISCOVERABLE_TO_PROP => unimplemented!(),*/
INCLUDES_PROP => {
let includes: Vec<Param> = self
.includes
.iter()
.map(|x| Param::Base(x.to_string().into()))
.collect();
let array = params::Array {
values: includes,
element_sig: signature::Type::Base(signature::Base::String),
};
Some(container_param_to_variant(Container::Array(array)))
}
LOCAL_NAME_PROP => Some(base_param_to_variant(self.localname.to_string().into())),
APPEARANCE_PROP => Some(base_param_to_variant(self.appearance.into())),
DURATION_PROP => {
Some(base_param_to_variant(self.duration.into()))
}
TO_PROP => {
Some(base_param_to_variant(self.timeout.into()))
}
//TODO:implement SND_CHANNEL_PROP => unimplemented!(),
_ => None,
},
_ => None,
}
}
fn set_inner(&mut self, _interface: &str, _prop: &str, _val: Variant) -> Option<String> {
unimplemented!()
}
}
impl Introspectable for Advertisement {
fn introspectable_str(&self) -> String {
let mut ret = String::new();
ret.push_str(INTROSPECT_FMT_P1);
ret.push_str(self.path.to_str().unwrap());
ret.push_str(INTROSPECT_FMT_P2);
ret.push_str(PROP_STR);
ret.push_str(ADV_STR);
ret.push_str(INTROSPECT_FMT_P3);
ret
}
}