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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use bluetooth_traits::{BluetoothRequest, BluetoothResponse};
use bluetooth_traits::blacklist::{Blacklist, uuid_is_blacklisted};
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
use dom::bindings::error::Error::{self, Network, Security};
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::DOMString;
use dom::bluetooth::{AsyncBluetoothListener, response_async};
use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties;
use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic;
use dom::bluetoothuuid::{BluetoothCharacteristicUUID, BluetoothServiceUUID, BluetoothUUID};
use dom::globalscope::GlobalScope;
use dom::promise::Promise;
use ipc_channel::ipc::IpcSender;
use js::jsapi::JSContext;
use std::rc::Rc;
// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattservice
#[dom_struct]
pub struct BluetoothRemoteGATTService {
reflector_: Reflector,
device: MutHeap<JS<BluetoothDevice>>,
uuid: DOMString,
is_primary: bool,
instance_id: String,
}
impl BluetoothRemoteGATTService {
pub fn new_inherited(device: &BluetoothDevice,
uuid: DOMString,
is_primary: bool,
instance_id: String)
-> BluetoothRemoteGATTService {
BluetoothRemoteGATTService {
reflector_: Reflector::new(),
device: MutHeap::new(device),
uuid: uuid,
is_primary: is_primary,
instance_id: instance_id,
}
}
pub fn new(global: &GlobalScope,
device: &BluetoothDevice,
uuid: DOMString,
isPrimary: bool,
instanceID: String)
-> Root<BluetoothRemoteGATTService> {
reflect_dom_object(box BluetoothRemoteGATTService::new_inherited(device,
uuid,
isPrimary,
instanceID),
global,
BluetoothRemoteGATTServiceBinding::Wrap)
}
pub fn get_device(&self) -> Root<BluetoothDevice> {
self.device.get()
}
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothRequest> {
self.global().as_window().bluetooth_thread()
}
fn get_instance_id(&self) -> String {
self.instance_id.clone()
}
}
impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-device
fn Device(&self) -> Root<BluetoothDevice> {
self.device.get()
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-isprimary
fn IsPrimary(&self) -> bool {
self.is_primary
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-uuid
fn Uuid(&self) -> DOMString {
self.uuid.clone()
}
#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristic
fn GetCharacteristic(&self,
characteristic: BluetoothCharacteristicUUID)
-> Rc<Promise> {
let p = Promise::new(&self.global());
let p_cx = p.global().get_cx();
let uuid = match BluetoothUUID::characteristic(characteristic) {
Ok(uuid) => uuid.to_string(),
Err(e) => {
p.reject_error(p_cx, e);
return p;
}
};
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
p.reject_error(p_cx, Security);
return p;
}
if !self.Device().Gatt().Connected() {
p.reject_error(p_cx, Network);
return p;
}
let sender = response_async(&p, self);
self.get_bluetooth_thread().send(
BluetoothRequest::GetCharacteristic(self.get_instance_id(), uuid, sender)).unwrap();
return p;
}
#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics
fn GetCharacteristics(&self,
characteristic: Option<BluetoothCharacteristicUUID>)
-> Rc<Promise> {
let p = Promise::new(&self.global());
let p_cx = p.global().get_cx();
let mut uuid: Option<String> = None;
if let Some(c) = characteristic {
uuid = match BluetoothUUID::characteristic(c) {
Ok(uuid) => Some(uuid.to_string()),
Err(e) => {
p.reject_error(p_cx, e);
return p;
}
};
if let Some(ref uuid) = uuid {
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
p.reject_error(p_cx, Security);
return p;
}
}
};
if !self.Device().Gatt().Connected() {
p.reject_error(p_cx, Network);
return p;
}
let sender = response_async(&p, self);
self.get_bluetooth_thread().send(
BluetoothRequest::GetCharacteristics(self.get_instance_id(), uuid, sender)).unwrap();
return p;
}
#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservice
fn GetIncludedService(&self,
service: BluetoothServiceUUID)
-> Rc<Promise> {
let p = Promise::new(&self.global());
let p_cx = p.global().get_cx();
let uuid = match BluetoothUUID::service(service) {
Ok(uuid) => uuid.to_string(),
Err(e) => {
p.reject_error(p_cx, e);
return p;
}
};
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
p.reject_error(p_cx, Security);
return p;
}
if !self.Device().Gatt().Connected() {
p.reject_error(p_cx, Network);
return p;
}
let sender = response_async(&p, self);
self.get_bluetooth_thread().send(
BluetoothRequest::GetIncludedService(self.get_instance_id(),
uuid,
sender)).unwrap();
return p;
}
#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservices
fn GetIncludedServices(&self,
service: Option<BluetoothServiceUUID>)
-> Rc<Promise> {
let p = Promise::new(&self.global());
let p_cx = p.global().get_cx();
let mut uuid: Option<String> = None;
if let Some(s) = service {
uuid = match BluetoothUUID::service(s) {
Ok(uuid) => Some(uuid.to_string()),
Err(e) => {
p.reject_error(p_cx, e);
return p;
}
};
if let Some(ref uuid) = uuid {
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
p.reject_error(p_cx, Security);
return p;
}
}
};
if !self.Device().Gatt().Connected() {
p.reject_error(p_cx, Network);
return p;
}
let sender = response_async(&p, self);
self.get_bluetooth_thread().send(
BluetoothRequest::GetIncludedServices(self.get_instance_id(),
uuid,
sender)).unwrap();
return p;
}
}
impl AsyncBluetoothListener for BluetoothRemoteGATTService {
fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) {
match response {
BluetoothResponse::GetCharacteristic(characteristic) => {
let context = self.device.get().get_context();
let mut characteristic_map = context.get_characteristic_map().borrow_mut();
if let Some(existing_characteristic) = characteristic_map.get(&characteristic.instance_id) {
return promise.resolve_native(promise_cx, &existing_characteristic.get());
}
let properties =
BluetoothCharacteristicProperties::new(&self.global(),
characteristic.broadcast,
characteristic.read,
characteristic.write_without_response,
characteristic.write,
characteristic.notify,
characteristic.indicate,
characteristic.authenticated_signed_writes,
characteristic.reliable_write,
characteristic.writable_auxiliaries);
let bt_characteristic = BluetoothRemoteGATTCharacteristic::new(&self.global(),
self,
DOMString::from(characteristic.uuid),
&properties,
characteristic.instance_id.clone());
characteristic_map.insert(characteristic.instance_id, MutHeap::new(&bt_characteristic));
promise.resolve_native(promise_cx, &bt_characteristic);
},
BluetoothResponse::GetCharacteristics(characteristics_vec) => {
let mut characteristics = vec!();
let context = self.device.get().get_context();
let mut characteristic_map = context.get_characteristic_map().borrow_mut();
for characteristic in characteristics_vec {
let bt_characteristic = match characteristic_map.get(&characteristic.instance_id) {
Some(existing_characteristic) => existing_characteristic.get(),
None => {
let properties =
BluetoothCharacteristicProperties::new(&self.global(),
characteristic.broadcast,
characteristic.read,
characteristic.write_without_response,
characteristic.write,
characteristic.notify,
characteristic.indicate,
characteristic.authenticated_signed_writes,
characteristic.reliable_write,
characteristic.writable_auxiliaries);
BluetoothRemoteGATTCharacteristic::new(&self.global(),
self,
DOMString::from(characteristic.uuid),
&properties,
characteristic.instance_id.clone())
},
};
if !characteristic_map.contains_key(&characteristic.instance_id) {
characteristic_map.insert(characteristic.instance_id, MutHeap::new(&bt_characteristic));
}
characteristics.push(bt_characteristic);
}
promise.resolve_native(promise_cx, &characteristics);
},
BluetoothResponse::GetIncludedService(service) => {
let s =
BluetoothRemoteGATTService::new(&self.global(),
&self.device.get(),
DOMString::from(service.uuid),
service.is_primary,
service.instance_id);
promise.resolve_native(promise_cx, &s);
},
BluetoothResponse::GetIncludedServices(services_vec) => {
let s: Vec<Root<BluetoothRemoteGATTService>> =
services_vec.into_iter()
.map(|service| BluetoothRemoteGATTService::new(&self.global(),
&self.device.get(),
DOMString::from(service.uuid),
service.is_primary,
service.instance_id))
.collect();
promise.resolve_native(promise_cx, &s);
},
_ => promise.reject_error(promise_cx, Error::Type("Something went wrong...".to_owned())),
}
}
}
|