Auto merge of #14593 - szeged:getgattchildren, r=jdm

Implement GetGATTChildren

<!-- Please describe your changes on the following line: -->
Implement the [GetGATTChildren](https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren) function for webbluetooth.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors

<!-- Either: -->
- [X] There are tests for these changes

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/14593)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-12-16 12:30:41 -08:00 committed by GitHub
commit 6efea399ed
6 changed files with 258 additions and 705 deletions

View file

@ -15,7 +15,7 @@ extern crate uuid;
pub mod test; pub mod test;
use bluetooth_traits::{BluetoothCharacteristicMsg, BluetoothDescriptorMsg, BluetoothServiceMsg}; use bluetooth_traits::{BluetoothCharacteristicMsg, BluetoothDescriptorMsg, BluetoothServiceMsg};
use bluetooth_traits::{BluetoothDeviceMsg, BluetoothRequest, BluetoothResponse}; use bluetooth_traits::{BluetoothDeviceMsg, BluetoothRequest, BluetoothResponse, GATTType};
use bluetooth_traits::{BluetoothError, BluetoothResponseResult, BluetoothResult}; use bluetooth_traits::{BluetoothError, BluetoothResponseResult, BluetoothResult};
use bluetooth_traits::blocklist::{uuid_is_blocklisted, Blocklist}; use bluetooth_traits::blocklist::{uuid_is_blocklisted, Blocklist};
use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence, RequestDeviceoptions}; use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence, RequestDeviceoptions};
@ -229,29 +229,8 @@ impl BluetoothManager {
BluetoothRequest::GATTServerDisconnect(device_id, sender) => { BluetoothRequest::GATTServerDisconnect(device_id, sender) => {
let _ = sender.send(self.gatt_server_disconnect(device_id)); let _ = sender.send(self.gatt_server_disconnect(device_id));
}, },
BluetoothRequest::GetPrimaryService(device_id, uuid, sender) => { BluetoothRequest::GetGATTChildren(id, uuid, single, child_type, sender) => {
let _ = sender.send(self.get_primary_service(device_id, uuid)); let _ = sender.send(self.get_gatt_children(id, uuid, single, child_type));
},
BluetoothRequest::GetPrimaryServices(device_id, uuid, sender) => {
let _ = sender.send(self.get_primary_services(device_id, uuid));
},
BluetoothRequest::GetIncludedService(service_id, uuid, sender) => {
let _ = sender.send(self.get_included_service(service_id, uuid));
},
BluetoothRequest::GetIncludedServices(service_id, uuid, sender) => {
let _ = sender.send(self.get_included_services(service_id, uuid));
},
BluetoothRequest::GetCharacteristic(service_id, uuid, sender) => {
let _ = sender.send(self.get_characteristic(service_id, uuid));
},
BluetoothRequest::GetCharacteristics(service_id, uuid, sender) => {
let _ = sender.send(self.get_characteristics(service_id, uuid));
},
BluetoothRequest::GetDescriptor(characteristic_id, uuid, sender) => {
let _ = sender.send(self.get_descriptor(characteristic_id, uuid));
},
BluetoothRequest::GetDescriptors(characteristic_id, uuid, sender) => {
let _ = sender.send(self.get_descriptors(characteristic_id, uuid));
}, },
BluetoothRequest::ReadValue(id, sender) => { BluetoothRequest::ReadValue(id, sender) => {
let _ = sender.send(self.read_value(id)); let _ = sender.send(self.read_value(id));
@ -450,15 +429,6 @@ impl BluetoothManager {
None None
} }
fn get_gatt_services_by_uuid(&mut self,
adapter: &mut BluetoothAdapter,
device_id: &str,
service_uuid: &str)
-> Vec<BluetoothGATTService> {
let services = self.get_and_cache_gatt_services(adapter, device_id);
services.into_iter().filter(|s| s.get_uuid().ok() == Some(service_uuid.to_string())).collect()
}
fn service_is_cached(&self, service_id: &str) -> bool { fn service_is_cached(&self, service_id: &str) -> bool {
self.cached_services.contains_key(service_id) && self.service_to_device.contains_key(service_id) self.cached_services.contains_key(service_id) && self.service_to_device.contains_key(service_id)
} }
@ -496,17 +466,6 @@ impl BluetoothManager {
None None
} }
fn get_gatt_characteristics_by_uuid(&mut self,
adapter: &mut BluetoothAdapter,
service_id: &str,
characteristic_uuid: &str)
-> Vec<BluetoothGATTCharacteristic> {
let characteristics = self.get_and_cache_gatt_characteristics(adapter, service_id);
characteristics.into_iter()
.filter(|c| c.get_uuid().ok() == Some(characteristic_uuid.to_string()))
.collect()
}
fn get_characteristic_properties(&self, characteristic: &BluetoothGATTCharacteristic) -> Flags { fn get_characteristic_properties(&self, characteristic: &BluetoothGATTCharacteristic) -> Flags {
let mut props: Flags = Flags::empty(); let mut props: Flags = Flags::empty();
let flags = characteristic.get_flags().unwrap_or(vec!()); let flags = characteristic.get_flags().unwrap_or(vec!());
@ -565,17 +524,6 @@ impl BluetoothManager {
None None
} }
fn get_gatt_descriptors_by_uuid(&mut self,
adapter: &mut BluetoothAdapter,
characteristic_id: &str,
descriptor_uuid: &str)
-> Vec<BluetoothGATTDescriptor> {
let descriptors = self.get_and_cache_gatt_descriptors(adapter, characteristic_id);
descriptors.into_iter()
.filter(|d| d.get_uuid().ok() == Some(descriptor_uuid.to_string()))
.collect()
}
// Methods // Methods
// https://webbluetoothcg.github.io/web-bluetooth/#request-bluetooth-devices // https://webbluetoothcg.github.io/web-bluetooth/#request-bluetooth-devices
@ -687,231 +635,61 @@ impl BluetoothManager {
} }
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren // https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
fn get_primary_service(&mut self, fn get_gatt_children(&mut self,
device_id: String, id: String,
uuid: String) uuid: Option<String>,
single: bool,
child_type: GATTType)
-> BluetoothResponseResult { -> BluetoothResponseResult {
let mut adapter = try!(self.get_adapter());
match child_type {
GATTType::PrimaryService => {
// Step 5. // Step 5.
if !self.device_is_cached(&device_id) { if !self.device_is_cached(&id) {
return Err(BluetoothError::InvalidState); return Err(BluetoothError::InvalidState);
} }
// Step 6.
let mut adapter = try!(self.get_adapter()); if let Some(ref uuid) = uuid {
if !self.allowed_services.get(&device_id).map_or(false, |s| s.contains(&uuid)) { if !self.allowed_services.get(&id).map_or(false, |s| s.contains(uuid)) {
return Err(BluetoothError::Security); return Err(BluetoothError::Security);
} }
let services = self.get_gatt_services_by_uuid(&mut adapter, &device_id, &uuid);
// Step 6.
for service in services {
if service.is_primary().unwrap_or(false) {
if let Ok(uuid) = service.get_uuid() {
return Ok(BluetoothResponse::GetPrimaryService(
BluetoothServiceMsg {
uuid: uuid,
is_primary: true,
instance_id: service.get_id(),
}
));
}
}
}
// Step 7.
return Err(BluetoothError::NotFound);
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
fn get_primary_services(&mut self,
device_id: String,
uuid: Option<String>)
-> BluetoothResponseResult {
// Step 5.
if !self.device_is_cached(&device_id) {
return Err(BluetoothError::InvalidState);
}
let mut adapter = try!(self.get_adapter());
let services = match uuid {
Some(ref id) => {
if !self.allowed_services.get(&device_id).map_or(false, |s| s.contains(id)) {
return Err(BluetoothError::Security)
}
self.get_gatt_services_by_uuid(&mut adapter, &device_id, id)
},
None => self.get_and_cache_gatt_services(&mut adapter, &device_id),
};
// Step 6.
let mut services_vec = vec!();
for service in services {
if service.is_primary().unwrap_or(false) {
if let Ok(uuid) = service.get_uuid() {
services_vec.push(
BluetoothServiceMsg {
uuid: uuid,
is_primary: true,
instance_id: service.get_id(),
}
);
}
}
}
// Step 7.
if services_vec.is_empty() {
return Err(BluetoothError::NotFound);
}
return Ok(BluetoothResponse::GetPrimaryServices(services_vec));
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservice
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
fn get_included_service(&mut self,
service_id: String,
uuid: String)
-> BluetoothResponseResult {
// Step 5.
if !self.service_is_cached(&service_id) {
return Err(BluetoothError::InvalidState);
}
let mut adapter = try!(self.get_adapter());
let device = match self.device_from_service_id(&service_id) {
Some(device) => device,
None => return Err(BluetoothError::NotFound),
};
let primary_service = match self.get_gatt_service(&mut adapter, &service_id) {
Some(s) => s,
None => return Err(BluetoothError::NotFound),
};
let services = primary_service.get_includes(device).unwrap_or(vec!());
// Step 6.
for service in services {
if let Ok(service_uuid) = service.get_uuid() {
if uuid == service_uuid {
return Ok(BluetoothResponse::GetIncludedService(
BluetoothServiceMsg {
uuid: uuid,
is_primary: service.is_primary().unwrap_or(false),
instance_id: service.get_id(),
}
));
}
}
}
// Step 7.
return Err(BluetoothError::NotFound);
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservices
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
fn get_included_services(&mut self,
service_id: String,
uuid: Option<String>)
-> BluetoothResponseResult {
// Step 5.
if !self.service_is_cached(&service_id) {
return Err(BluetoothError::InvalidState);
}
let mut adapter = try!(self.get_adapter());
let device = match self.device_from_service_id(&service_id) {
Some(device) => device,
None => return Err(BluetoothError::NotFound),
};
let primary_service = match self.get_gatt_service(&mut adapter, &service_id) {
Some(s) => s,
None => return Err(BluetoothError::NotFound),
};
let services = primary_service.get_includes(device).unwrap_or(vec!());
// Step 6.
let mut services_vec = vec!();
for service in services {
if let Ok(service_uuid) = service.get_uuid() {
services_vec.push(
BluetoothServiceMsg {
uuid: service_uuid,
is_primary: service.is_primary().unwrap_or(false),
instance_id: service.get_id(),
}
);
}
} }
let mut services = self.get_and_cache_gatt_services(&mut adapter, &id);
if let Some(uuid) = uuid { if let Some(uuid) = uuid {
services_vec.retain(|ref s| s.uuid == uuid); services.retain(|ref e| e.get_uuid().unwrap_or(String::new()) == uuid);
}
let mut services_vec = vec!();
for service in services {
if service.is_primary().unwrap_or(false) {
if let Ok(uuid) = service.get_uuid() {
services_vec.push(
BluetoothServiceMsg {
uuid: uuid,
is_primary: true,
instance_id: service.get_id(),
}
);
}
}
} }
services_vec.retain(|s| !uuid_is_blocklisted(&s.uuid, Blocklist::All));
// Step 7. // Step 7.
if services_vec.is_empty() { if services_vec.is_empty() {
return Err(BluetoothError::NotFound); return Err(BluetoothError::NotFound);
} }
return Ok(BluetoothResponse::GetIncludedServices(services_vec)); return Ok(BluetoothResponse::GetPrimaryServices(services_vec, single));
} },
GATTType::Characteristic => {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristic
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
fn get_characteristic(&mut self,
service_id: String,
uuid: String)
-> BluetoothResponseResult {
// Step 5. // Step 5.
if !self.service_is_cached(&service_id) { if !self.service_is_cached(&id) {
return Err(BluetoothError::InvalidState); return Err(BluetoothError::InvalidState);
} }
let mut adapter = try!(self.get_adapter());
let characteristics = self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &uuid);
// Step 6. // Step 6.
for characteristic in characteristics { let mut characteristics = self.get_and_cache_gatt_characteristics(&mut adapter, &id);
if let Ok(uuid) = characteristic.get_uuid() { if let Some(uuid) = uuid {
let properties = self.get_characteristic_properties(&characteristic); characteristics.retain(|ref e| e.get_uuid().unwrap_or(String::new()) == uuid);
let message = BluetoothCharacteristicMsg {
uuid: uuid,
instance_id: characteristic.get_id(),
broadcast: properties.contains(BROADCAST),
read: properties.contains(READ),
write_without_response: properties.contains(WRITE_WITHOUT_RESPONSE),
write: properties.contains(WRITE),
notify: properties.contains(NOTIFY),
indicate: properties.contains(INDICATE),
authenticated_signed_writes: properties.contains(AUTHENTICATED_SIGNED_WRITES),
reliable_write: properties.contains(RELIABLE_WRITE),
writable_auxiliaries: properties.contains(WRITABLE_AUXILIARIES),
};
return Ok(BluetoothResponse::GetCharacteristic(message));
} }
}
// Step 7.
return Err(BluetoothError::NotFound);
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
fn get_characteristics(&mut self,
service_id: String,
uuid: Option<String>)
-> BluetoothResponseResult {
// Step 5.
if !self.service_is_cached(&service_id) {
return Err(BluetoothError::InvalidState);
}
let mut adapter = try!(self.get_adapter());
let characteristics = match uuid {
Some(id) => self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &id),
None => self.get_and_cache_gatt_characteristics(&mut adapter, &service_id),
};
// Step 6.
let mut characteristics_vec = vec!(); let mut characteristics_vec = vec!();
for characteristic in characteristics { for characteristic in characteristics {
if let Ok(uuid) = characteristic.get_uuid() { if let Ok(uuid) = characteristic.get_uuid() {
@ -939,56 +717,57 @@ impl BluetoothManager {
return Err(BluetoothError::NotFound); return Err(BluetoothError::NotFound);
} }
return Ok(BluetoothResponse::GetCharacteristics(characteristics_vec)); return Ok(BluetoothResponse::GetCharacteristics(characteristics_vec, single));
} },
GATTType::IncludedService => {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
fn get_descriptor(&mut self,
characteristic_id: String,
uuid: String)
-> BluetoothResponseResult {
// Step 5. // Step 5.
if !self.characteristic_is_cached(&characteristic_id) { if !self.service_is_cached(&id) {
return Err(BluetoothError::InvalidState); return Err(BluetoothError::InvalidState);
} }
let mut adapter = try!(self.get_adapter());
let descriptors = self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &uuid);
// Step 6. // Step 6.
for descriptor in descriptors { let device = match self.device_from_service_id(&id) {
if let Ok(uuid) = descriptor.get_uuid() { Some(device) => device,
return Ok(BluetoothResponse::GetDescriptor( None => return Err(BluetoothError::NotFound),
BluetoothDescriptorMsg { };
uuid: uuid, let primary_service = match self.get_gatt_service(&mut adapter, &id) {
instance_id: descriptor.get_id(), Some(s) => s,
None => return Err(BluetoothError::NotFound),
};
let services = primary_service.get_includes(device).unwrap_or(vec!());
let mut services_vec = vec!();
for service in services {
if let Ok(service_uuid) = service.get_uuid() {
services_vec.push(
BluetoothServiceMsg {
uuid: service_uuid,
is_primary: service.is_primary().unwrap_or(false),
instance_id: service.get_id(),
} }
)); );
} }
} }
if let Some(uuid) = uuid {
services_vec.retain(|ref s| s.uuid == uuid);
}
services_vec.retain(|s| !uuid_is_blocklisted(&s.uuid, Blocklist::All));
// Step 7. // Step 7.
if services_vec.is_empty() {
return Err(BluetoothError::NotFound); return Err(BluetoothError::NotFound);
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptors return Ok(BluetoothResponse::GetIncludedServices(services_vec, single));
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren },
fn get_descriptors(&mut self, GATTType::Descriptor => {
characteristic_id: String,
uuid: Option<String>)
-> BluetoothResponseResult {
// Step 5. // Step 5.
if !self.characteristic_is_cached(&characteristic_id) { if !self.characteristic_is_cached(&id) {
return Err(BluetoothError::InvalidState); return Err(BluetoothError::InvalidState);
} }
let mut adapter = try!(self.get_adapter());
let descriptors = match uuid {
Some(id) => self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &id),
None => self.get_and_cache_gatt_descriptors(&mut adapter, &characteristic_id),
};
// Step 6. // Step 6.
let mut descriptors = self.get_and_cache_gatt_descriptors(&mut adapter, &id);
if let Some(uuid) = uuid {
descriptors.retain(|ref e| e.get_uuid().unwrap_or(String::new()) == uuid);
}
let mut descriptors_vec = vec!(); let mut descriptors_vec = vec!();
for descriptor in descriptors { for descriptor in descriptors {
if let Ok(uuid) = descriptor.get_uuid() { if let Ok(uuid) = descriptor.get_uuid() {
@ -1005,7 +784,9 @@ impl BluetoothManager {
if descriptors_vec.is_empty() { if descriptors_vec.is_empty() {
return Err(BluetoothError::NotFound); return Err(BluetoothError::NotFound);
} }
return Ok(BluetoothResponse::GetDescriptors(descriptors_vec)); return Ok(BluetoothResponse::GetDescriptors(descriptors_vec, single));
},
}
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-readvalue // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-readvalue

View file

@ -26,6 +26,14 @@ pub enum BluetoothError {
InvalidState, InvalidState,
} }
#[derive(Deserialize, Serialize)]
pub enum GATTType {
PrimaryService,
Characteristic,
IncludedService,
Descriptor,
}
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct BluetoothDeviceMsg { pub struct BluetoothDeviceMsg {
// Bluetooth Device properties // Bluetooth Device properties
@ -78,14 +86,7 @@ pub enum BluetoothRequest {
RequestDevice(RequestDeviceoptions, IpcSender<BluetoothResponseResult>), RequestDevice(RequestDeviceoptions, IpcSender<BluetoothResponseResult>),
GATTServerConnect(String, IpcSender<BluetoothResponseResult>), GATTServerConnect(String, IpcSender<BluetoothResponseResult>),
GATTServerDisconnect(String, IpcSender<BluetoothResult<bool>>), GATTServerDisconnect(String, IpcSender<BluetoothResult<bool>>),
GetPrimaryService(String, String, IpcSender<BluetoothResponseResult>), GetGATTChildren(String, Option<String>, bool, GATTType, IpcSender<BluetoothResponseResult>),
GetPrimaryServices(String, Option<String>, IpcSender<BluetoothResponseResult>),
GetIncludedService(String, String, IpcSender<BluetoothResponseResult>),
GetIncludedServices(String, Option<String>, IpcSender<BluetoothResponseResult>),
GetCharacteristic(String, String, IpcSender<BluetoothResponseResult>),
GetCharacteristics(String, Option<String>, IpcSender<BluetoothResponseResult>),
GetDescriptor(String, String, IpcSender<BluetoothResponseResult>),
GetDescriptors(String, Option<String>, IpcSender<BluetoothResponseResult>),
ReadValue(String, IpcSender<BluetoothResponseResult>), ReadValue(String, IpcSender<BluetoothResponseResult>),
WriteValue(String, Vec<u8>, IpcSender<BluetoothResponseResult>), WriteValue(String, Vec<u8>, IpcSender<BluetoothResponseResult>),
EnableNotification(String, bool, IpcSender<BluetoothResponseResult>), EnableNotification(String, bool, IpcSender<BluetoothResponseResult>),
@ -98,14 +99,10 @@ pub enum BluetoothRequest {
pub enum BluetoothResponse { pub enum BluetoothResponse {
RequestDevice(BluetoothDeviceMsg), RequestDevice(BluetoothDeviceMsg),
GATTServerConnect(bool), GATTServerConnect(bool),
GetPrimaryService(BluetoothServiceMsg), GetPrimaryServices(BluetoothServicesMsg, bool),
GetPrimaryServices(BluetoothServicesMsg), GetIncludedServices(BluetoothServicesMsg, bool),
GetIncludedService(BluetoothServiceMsg), GetCharacteristics(BluetoothCharacteristicsMsg, bool),
GetIncludedServices(BluetoothServicesMsg), GetDescriptors(BluetoothDescriptorsMsg, bool),
GetCharacteristic(BluetoothCharacteristicMsg),
GetCharacteristics(BluetoothCharacteristicsMsg),
GetDescriptor(BluetoothDescriptorMsg),
GetDescriptors(BluetoothDescriptorsMsg),
ReadValue(Vec<u8>), ReadValue(Vec<u8>),
WriteValue(Vec<u8>), WriteValue(Vec<u8>),
EnableNotification(()), EnableNotification(()),

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use bluetooth_traits::{BluetoothError, BluetoothRequest}; use bluetooth_traits::{BluetoothError, BluetoothRequest, GATTType};
use bluetooth_traits::{BluetoothResponse, BluetoothResponseListener, BluetoothResponseResult}; use bluetooth_traits::{BluetoothResponse, BluetoothResponseListener, BluetoothResponseResult};
use bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted}; use bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted};
use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence}; use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence};
@ -13,14 +13,14 @@ use dom::bindings::codegen::Bindings::BluetoothBinding::{self, BluetoothDataFilt
use dom::bindings::codegen::Bindings::BluetoothBinding::{BluetoothMethods, RequestDeviceOptions}; use dom::bindings::codegen::Bindings::BluetoothBinding::{BluetoothMethods, RequestDeviceOptions};
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull; use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong; use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong;
use dom::bindings::error::Error::{self, NotFound, Security, Type}; use dom::bindings::error::Error::{self, Network, NotFound, Security, Type};
use dom::bindings::error::Fallible; use dom::bindings::error::Fallible;
use dom::bindings::js::{MutJS, Root}; use dom::bindings::js::{MutJS, Root};
use dom::bindings::refcounted::{Trusted, TrustedPromise}; use dom::bindings::refcounted::{Trusted, TrustedPromise};
use dom::bindings::reflector::{DomObject, reflect_dom_object}; use dom::bindings::reflector::{DomObject, reflect_dom_object};
use dom::bindings::str::DOMString; use dom::bindings::str::DOMString;
use dom::bluetoothdevice::BluetoothDevice; use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID}; use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID, UUID};
use dom::eventtarget::EventTarget; use dom::eventtarget::EventTarget;
use dom::globalscope::GlobalScope; use dom::globalscope::GlobalScope;
use dom::promise::Promise; use dom::promise::Promise;
@ -193,6 +193,57 @@ pub fn response_async<T: AsyncBluetoothListener + DomObject + 'static>(
action_sender action_sender
} }
#[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
pub fn get_gatt_children<T, F> (
attribute: &T,
single: bool,
uuid_canonicalizer: F,
uuid: Option<StringOrUnsignedLong>,
instance_id: String,
connected: bool,
child_type: GATTType)
-> Rc<Promise>
where T: AsyncBluetoothListener + DomObject + 'static,
F: FnOnce(StringOrUnsignedLong) -> Fallible<UUID> {
let p = Promise::new(&attribute.global());
let p_cx = p.global().get_cx();
let result_uuid = if let Some(u) = uuid {
// Step 1.
let canonicalized = match uuid_canonicalizer(u) {
Ok(canonicalized_uuid) => canonicalized_uuid.to_string(),
Err(e) => {
p.reject_error(p_cx, e);
return p;
}
};
// Step 2.
if uuid_is_blocklisted(canonicalized.as_ref(), Blocklist::All) {
p.reject_error(p_cx, Security);
return p;
}
Some(canonicalized)
} else {
None
};
// Step 3 - 4.
if !connected {
p.reject_error(p_cx, Network);
return p;
}
// TODO: Step 5: Implement representedDevice internal slot for BluetoothDevice.
// Note: Steps 6 - 7 are implemented in components/bluetooth/lib.rs in get_descriptor function
// and in handle_response function.
let sender = response_async(&p, attribute);
attribute.global().as_window().bluetooth_thread().send(
BluetoothRequest::GetGATTChildren(instance_id, result_uuid, single, child_type, sender)).unwrap();
return p;
}
// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothlescanfilterinit-canonicalizing // https://webbluetoothcg.github.io/web-bluetooth/#bluetoothlescanfilterinit-canonicalizing
fn canonicalize_filter(filter: &BluetoothLEScanFilterInit) -> Fallible<BluetoothScanfilter> { fn canonicalize_filter(filter: &BluetoothLEScanFilterInit) -> Fallible<BluetoothScanfilter> {
// Step 1. // Step 1.

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use bluetooth_traits::{BluetoothRequest, BluetoothResponse}; use bluetooth_traits::{BluetoothRequest, BluetoothResponse, GATTType};
use bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted}; use bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted};
use dom::bindings::cell::DOMRefCell; use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::BluetoothCharacteristicPropertiesBinding:: use dom::bindings::codegen::Bindings::BluetoothCharacteristicPropertiesBinding::
@ -19,7 +19,7 @@ use dom::bindings::inheritance::Castable;
use dom::bindings::js::{MutJS, Root}; use dom::bindings::js::{MutJS, Root};
use dom::bindings::reflector::{DomObject, reflect_dom_object}; use dom::bindings::reflector::{DomObject, reflect_dom_object};
use dom::bindings::str::{ByteString, DOMString}; use dom::bindings::str::{ByteString, DOMString};
use dom::bluetooth::{AsyncBluetoothListener, response_async}; use dom::bluetooth::{AsyncBluetoothListener, get_gatt_children, response_async};
use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties; use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties;
use dom::bluetoothremotegattservice::BluetoothRemoteGATTService; use dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
use dom::bluetoothuuid::{BluetoothDescriptorUUID, BluetoothUUID}; use dom::bluetoothuuid::{BluetoothDescriptorUUID, BluetoothUUID};
@ -102,79 +102,18 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
fn GetDescriptor(&self, descriptor: BluetoothDescriptorUUID) -> Rc<Promise> { fn GetDescriptor(&self, descriptor: BluetoothDescriptorUUID) -> Rc<Promise> {
let p = Promise::new(&self.global()); get_gatt_children(self, true, BluetoothUUID::descriptor, Some(descriptor), self.get_instance_id(),
let p_cx = p.global().get_cx(); self.Service().Device().Gatt().Connected(), GATTType::Descriptor)
// Step 1.
let uuid = match BluetoothUUID::descriptor(descriptor) {
Ok(uuid) => uuid.to_string(),
Err(e) => {
p.reject_error(p_cx, e);
return p;
}
};
// Step 2.
if uuid_is_blocklisted(uuid.as_ref(), Blocklist::All) {
p.reject_error(p_cx, Security);
return p;
}
// Step 3 - 4.
if !self.Service().Device().Gatt().Connected() {
p.reject_error(p_cx, Network);
return p;
}
// Note: Steps 5 - 7 are implemented in components/bluetooth/lib.rs in get_descriptor function
// and in handle_response function.
let sender = response_async(&p, self);
self.get_bluetooth_thread().send(
BluetoothRequest::GetDescriptor(self.get_instance_id(), uuid, sender)).unwrap();
return p;
} }
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptors // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptors
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
fn GetDescriptors(&self, fn GetDescriptors(&self,
descriptor: Option<BluetoothDescriptorUUID>) descriptor: Option<BluetoothDescriptorUUID>)
-> Rc<Promise> { -> Rc<Promise> {
let p = Promise::new(&self.global()); get_gatt_children(self, false, BluetoothUUID::descriptor, descriptor, self.get_instance_id(),
let p_cx = p.global().get_cx(); self.Service().Device().Gatt().Connected(), GATTType::Descriptor)
let mut uuid: Option<String> = None;
if let Some(d) = descriptor {
// Step 1.
uuid = match BluetoothUUID::descriptor(d) {
Ok(uuid) => Some(uuid.to_string()),
Err(e) => {
p.reject_error(p_cx, e);
return p;
}
};
if let Some(ref uuid) = uuid {
// Step 2.
if uuid_is_blocklisted(uuid.as_ref(), Blocklist::All) {
p.reject_error(p_cx, Security);
return p;
}
}
};
// Step 3 - 4.
if !self.Service().Device().Gatt().Connected() {
p.reject_error(p_cx, Network);
return p;
}
// Note: Steps 5 - 7 are implemented in components/bluetooth/lib.rs in get_descriptors function
// and in handle_response function.
let sender = response_async(&p, self);
self.get_bluetooth_thread().send(
BluetoothRequest::GetDescriptors(self.get_instance_id(), uuid, sender)).unwrap();
return p;
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-value // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-value
@ -320,17 +259,13 @@ impl AsyncBluetoothListener for BluetoothRemoteGATTCharacteristic {
fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) { fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) {
let device = self.Service().Device(); let device = self.Service().Device();
match response { match response {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren // https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
// Step 7. // Step 7.
BluetoothResponse::GetDescriptor(descriptor) => { BluetoothResponse::GetDescriptors(descriptors_vec, single) => {
let bt_descriptor = device.get_or_create_descriptor(&descriptor, &self); if single {
promise.resolve_native(promise_cx, &bt_descriptor); promise.resolve_native(promise_cx, &device.get_or_create_descriptor(&descriptors_vec[0], &self));
}, return;
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptors }
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
// Step 7.
BluetoothResponse::GetDescriptors(descriptors_vec) => {
let mut descriptors = vec!(); let mut descriptors = vec!();
for descriptor in descriptors_vec { for descriptor in descriptors_vec {
let bt_descriptor = device.get_or_create_descriptor(&descriptor, &self); let bt_descriptor = device.get_or_create_descriptor(&descriptor, &self);

View file

@ -2,16 +2,15 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use bluetooth_traits::{BluetoothRequest, BluetoothResponse}; use bluetooth_traits::{BluetoothRequest, BluetoothResponse, GATTType};
use bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted};
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods; use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
use dom::bindings::error::Error::{self, Network, Security}; use dom::bindings::error::Error;
use dom::bindings::error::ErrorResult; use dom::bindings::error::ErrorResult;
use dom::bindings::js::{MutJS, Root}; use dom::bindings::js::{MutJS, Root};
use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object}; use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
use dom::bluetooth::{AsyncBluetoothListener, response_async}; use dom::bluetooth::{AsyncBluetoothListener, get_gatt_children, response_async};
use dom::bluetoothdevice::BluetoothDevice; use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID}; use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID};
use dom::globalscope::GlobalScope; use dom::globalscope::GlobalScope;
@ -114,84 +113,24 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice
fn GetPrimaryService(&self, service: BluetoothServiceUUID) -> Rc<Promise> { fn GetPrimaryService(&self, service: BluetoothServiceUUID) -> Rc<Promise> {
// TODO: Step 1: Implement the Permission API and the allowedServices BluetoothDevice internal slot. // TODO: Step 1: Implement the Permission API and the allowedServices BluetoothDevice internal slot.
// Subsequent steps are relative to https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
let p = Promise::new(&self.global());
let p_cx = p.global().get_cx();
// Step 1.
let uuid = match BluetoothUUID::service(service) {
Ok(uuid) => uuid.to_string(),
Err(e) => {
p.reject_error(p_cx, e);
return p;
}
};
// Step 2. // Step 2.
if uuid_is_blocklisted(uuid.as_ref(), Blocklist::All) { get_gatt_children(self, true, BluetoothUUID::service, Some(service), String::from(self.Device().Id()),
p.reject_error(p_cx, Security); self.Device().Gatt().Connected(), GATTType::PrimaryService)
return p;
}
// Step 3 - 4.
if !self.Device().Gatt().Connected() {
p.reject_error(p_cx, Network);
return p;
}
// Note: Steps 5 - 7 are implemented in components/bluetooth/lib.rs in get_primary_service function
// and in handle_response function.
let sender = response_async(&p, self);
self.get_bluetooth_thread().send(
BluetoothRequest::GetPrimaryService(String::from(self.Device().Id()), uuid, sender)).unwrap();
return p;
} }
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices
fn GetPrimaryServices(&self, service: Option<BluetoothServiceUUID>) -> Rc<Promise> { fn GetPrimaryServices(&self, service: Option<BluetoothServiceUUID>) -> Rc<Promise> {
// TODO: Step 1: Implement the Permission API and the allowedServices BluetoothDevice internal slot. // TODO: Step 1: Implement the Permission API and the allowedServices BluetoothDevice internal slot.
// Subsequent steps are relative to https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
let p = Promise::new(&self.global());
let p_cx = p.global().get_cx();
let mut uuid: Option<String> = None;
if let Some(s) = service {
// Step 1.
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 {
// Step 2. // Step 2.
if uuid_is_blocklisted(uuid.as_ref(), Blocklist::All) { get_gatt_children(self, false, BluetoothUUID::service, service, String::from(self.Device().Id()),
p.reject_error(p_cx, Security); self.Connected(), GATTType::PrimaryService)
return p;
}
}
};
// Step 3 - 4.
if !self.Device().Gatt().Connected() {
p.reject_error(p_cx, Network);
return p;
}
// Note: Steps 5 - 7 are implemented in components/bluetooth/lib.rs in get_primary_services function
// and in handle_response function.
let sender = response_async(&p, self);
self.get_bluetooth_thread().send(
BluetoothRequest::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap();
return p;
} }
} }
impl AsyncBluetoothListener for BluetoothRemoteGATTServer { impl AsyncBluetoothListener for BluetoothRemoteGATTServer {
fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) { fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) {
let device = self.Device();
match response { match response {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect
BluetoothResponse::GATTServerConnect(connected) => { BluetoothResponse::GATTServerConnect(connected) => {
@ -201,17 +140,14 @@ impl AsyncBluetoothListener for BluetoothRemoteGATTServer {
// Step 5.2.5. // Step 5.2.5.
promise.resolve_native(promise_cx, self); promise.resolve_native(promise_cx, self);
}, },
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren // https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
// Step 7. // Step 7.
BluetoothResponse::GetPrimaryService(service) => { BluetoothResponse::GetPrimaryServices(services_vec, single) => {
let bt_service = device.get_or_create_service(&service, &self); let device = self.Device();
promise.resolve_native(promise_cx, &bt_service); if single {
}, promise.resolve_native(promise_cx, &device.get_or_create_service(&services_vec[0], &self));
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices return;
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren }
// Step 7.
BluetoothResponse::GetPrimaryServices(services_vec) => {
let mut services = vec!(); let mut services = vec!();
for service in services_vec { for service in services_vec {
let bt_service = device.get_or_create_service(&service, &self); let bt_service = device.get_or_create_service(&service, &self);

View file

@ -2,24 +2,22 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use bluetooth_traits::{BluetoothRequest, BluetoothResponse}; use bluetooth_traits::{BluetoothResponse, GATTType};
use bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted};
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods; use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull; use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::error::Error::{self, Network, Security}; use dom::bindings::error::Error;
use dom::bindings::js::{MutJS, Root}; use dom::bindings::js::{MutJS, Root};
use dom::bindings::reflector::{DomObject, reflect_dom_object}; use dom::bindings::reflector::reflect_dom_object;
use dom::bindings::str::DOMString; use dom::bindings::str::DOMString;
use dom::bluetooth::{AsyncBluetoothListener, response_async}; use dom::bluetooth::{AsyncBluetoothListener, get_gatt_children};
use dom::bluetoothdevice::BluetoothDevice; use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothuuid::{BluetoothCharacteristicUUID, BluetoothServiceUUID, BluetoothUUID}; use dom::bluetoothuuid::{BluetoothCharacteristicUUID, BluetoothServiceUUID, BluetoothUUID};
use dom::eventtarget::EventTarget; use dom::eventtarget::EventTarget;
use dom::globalscope::GlobalScope; use dom::globalscope::GlobalScope;
use dom::promise::Promise; use dom::promise::Promise;
use ipc_channel::ipc::IpcSender;
use js::jsapi::JSContext; use js::jsapi::JSContext;
use std::rc::Rc; use std::rc::Rc;
@ -62,10 +60,6 @@ impl BluetoothRemoteGATTService {
BluetoothRemoteGATTServiceBinding::Wrap) BluetoothRemoteGATTServiceBinding::Wrap)
} }
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothRequest> {
self.global().as_window().bluetooth_thread()
}
fn get_instance_id(&self) -> String { fn get_instance_id(&self) -> String {
self.instance_id.clone() self.instance_id.clone()
} }
@ -89,165 +83,39 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristic // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristic
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
fn GetCharacteristic(&self, fn GetCharacteristic(&self,
characteristic: BluetoothCharacteristicUUID) characteristic: BluetoothCharacteristicUUID)
-> Rc<Promise> { -> Rc<Promise> {
let p = Promise::new(&self.global()); get_gatt_children(self, true, BluetoothUUID::characteristic, Some(characteristic), self.get_instance_id(),
let p_cx = p.global().get_cx(); self.Device().Gatt().Connected(), GATTType::Characteristic)
// Step 1.
let uuid = match BluetoothUUID::characteristic(characteristic) {
Ok(uuid) => uuid.to_string(),
Err(e) => {
p.reject_error(p_cx, e);
return p;
}
};
// Step 2.
if uuid_is_blocklisted(uuid.as_ref(), Blocklist::All) {
p.reject_error(p_cx, Security);
return p;
}
// Step 3 - 4.
if !self.Device().Gatt().Connected() {
p.reject_error(p_cx, Network);
return p;
}
// Note: Steps 5 - 7 are implemented is components/bluetooth/lib.rs in get_characteristic function
// and in handle_response function.
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)] #[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
fn GetCharacteristics(&self, fn GetCharacteristics(&self,
characteristic: Option<BluetoothCharacteristicUUID>) characteristic: Option<BluetoothCharacteristicUUID>)
-> Rc<Promise> { -> Rc<Promise> {
let p = Promise::new(&self.global()); get_gatt_children(self, false, BluetoothUUID::characteristic, characteristic, self.get_instance_id(),
let p_cx = p.global().get_cx(); self.Device().Gatt().Connected(), GATTType::Characteristic)
let mut uuid: Option<String> = None;
if let Some(c) = characteristic {
// Step 1.
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 {
// Step 2.
if uuid_is_blocklisted(uuid.as_ref(), Blocklist::All) {
p.reject_error(p_cx, Security);
return p;
}
}
};
// Step 3 - 4.
if !self.Device().Gatt().Connected() {
p.reject_error(p_cx, Network);
return p;
}
// Note: Steps 5 - 7 are implemented is components/bluetooth/lib.rs in get_characteristics function
// and in handle_response function.
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)] #[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservice // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservice
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
fn GetIncludedService(&self, fn GetIncludedService(&self,
service: BluetoothServiceUUID) service: BluetoothServiceUUID)
-> Rc<Promise> { -> Rc<Promise> {
let p = Promise::new(&self.global()); get_gatt_children(self, false, BluetoothUUID::service, Some(service), self.get_instance_id(),
let p_cx = p.global().get_cx(); self.Device().Gatt().Connected(), GATTType::IncludedService)
// Step 1.
let uuid = match BluetoothUUID::service(service) {
Ok(uuid) => uuid.to_string(),
Err(e) => {
p.reject_error(p_cx, e);
return p;
}
};
// Step 2.
if uuid_is_blocklisted(uuid.as_ref(), Blocklist::All) {
p.reject_error(p_cx, Security);
return p;
}
// Step 3 - 4.
if !self.Device().Gatt().Connected() {
p.reject_error(p_cx, Network);
return p;
}
// Note: Steps 5 - 7 are implemented is components/bluetooth/lib.rs in get_included_service function
// and in handle_response function.
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)] #[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservices // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservices
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
fn GetIncludedServices(&self, fn GetIncludedServices(&self,
service: Option<BluetoothServiceUUID>) service: Option<BluetoothServiceUUID>)
-> Rc<Promise> { -> Rc<Promise> {
let p = Promise::new(&self.global()); get_gatt_children(self, false, BluetoothUUID::service, service, self.get_instance_id(),
let p_cx = p.global().get_cx(); self.Device().Gatt().Connected(), GATTType::IncludedService)
let mut uuid: Option<String> = None;
if let Some(s) = service {
// Step 1.
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 {
// Step 2.
if uuid_is_blocklisted(uuid.as_ref(), Blocklist::All) {
p.reject_error(p_cx, Security);
return p;
}
}
};
// Step 3 - 4.
if !self.Device().Gatt().Connected() {
p.reject_error(p_cx, Network);
return p;
}
// Note: Steps 5 - 7 are implemented is components/bluetooth/lib.rs in get_included_services function
// and in handle_response function.
let sender = response_async(&p, self);
self.get_bluetooth_thread().send(
BluetoothRequest::GetIncludedServices(self.get_instance_id(),
uuid,
sender)).unwrap();
return p;
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-serviceeventhandlers-onserviceadded // https://webbluetoothcg.github.io/web-bluetooth/#dom-serviceeventhandlers-onserviceadded
@ -264,17 +132,14 @@ impl AsyncBluetoothListener for BluetoothRemoteGATTService {
fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) { fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) {
let device = self.Device(); let device = self.Device();
match response { match response {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristic
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren // https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
// Step 7. // Step 7.
BluetoothResponse::GetCharacteristic(characteristic) => { BluetoothResponse::GetCharacteristics(characteristics_vec, single) => {
let bt_characteristic = device.get_or_create_characteristic(&characteristic, &self); if single {
promise.resolve_native(promise_cx, &bt_characteristic); promise.resolve_native(promise_cx,
}, &device.get_or_create_characteristic(&characteristics_vec[0], &self));
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics return;
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren }
// Step 7.
BluetoothResponse::GetCharacteristics(characteristics_vec) => {
let mut characteristics = vec!(); let mut characteristics = vec!();
for characteristic in characteristics_vec { for characteristic in characteristics_vec {
let bt_characteristic = device.get_or_create_characteristic(&characteristic, &self); let bt_characteristic = device.get_or_create_characteristic(&characteristic, &self);
@ -282,31 +147,19 @@ impl AsyncBluetoothListener for BluetoothRemoteGATTService {
} }
promise.resolve_native(promise_cx, &characteristics); promise.resolve_native(promise_cx, &characteristics);
}, },
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservice
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren // https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
// Step 7. // Step 7.
BluetoothResponse::GetIncludedService(service) => { BluetoothResponse::GetIncludedServices(services_vec, single) => {
let s = if single {
BluetoothRemoteGATTService::new(&self.global(), promise.resolve_native(promise_cx, &device.get_or_create_service(&services_vec[0], &device.Gatt()));
&self.device.get(), return;
DOMString::from(service.uuid), }
service.is_primary, let mut services = vec!();
service.instance_id); for service in services_vec {
promise.resolve_native(promise_cx, &s); let bt_service = device.get_or_create_service(&service, &device.Gatt());
}, services.push(bt_service);
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservices }
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren promise.resolve_native(promise_cx, &services);
// Step 7.
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())), _ => promise.reject_error(promise_cx, Error::Type("Something went wrong...".to_owned())),
} }