mirror of
https://github.com/servo/servo.git
synced 2025-06-17 21:04:28 +00:00
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:
commit
6efea399ed
6 changed files with 258 additions and 705 deletions
|
@ -2,7 +2,7 @@
|
|||
* 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::{BluetoothError, BluetoothRequest};
|
||||
use bluetooth_traits::{BluetoothError, BluetoothRequest, GATTType};
|
||||
use bluetooth_traits::{BluetoothResponse, BluetoothResponseListener, BluetoothResponseResult};
|
||||
use bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted};
|
||||
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::EventHandlerBinding::EventHandlerNonNull;
|
||||
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::js::{MutJS, Root};
|
||||
use dom::bindings::refcounted::{Trusted, TrustedPromise};
|
||||
use dom::bindings::reflector::{DomObject, reflect_dom_object};
|
||||
use dom::bindings::str::DOMString;
|
||||
use dom::bluetoothdevice::BluetoothDevice;
|
||||
use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID};
|
||||
use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID, UUID};
|
||||
use dom::eventtarget::EventTarget;
|
||||
use dom::globalscope::GlobalScope;
|
||||
use dom::promise::Promise;
|
||||
|
@ -193,6 +193,57 @@ pub fn response_async<T: AsyncBluetoothListener + DomObject + 'static>(
|
|||
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
|
||||
fn canonicalize_filter(filter: &BluetoothLEScanFilterInit) -> Fallible<BluetoothScanfilter> {
|
||||
// Step 1.
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* 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::{BluetoothRequest, BluetoothResponse, GATTType};
|
||||
use bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted};
|
||||
use dom::bindings::cell::DOMRefCell;
|
||||
use dom::bindings::codegen::Bindings::BluetoothCharacteristicPropertiesBinding::
|
||||
|
@ -19,7 +19,7 @@ use dom::bindings::inheritance::Castable;
|
|||
use dom::bindings::js::{MutJS, Root};
|
||||
use dom::bindings::reflector::{DomObject, reflect_dom_object};
|
||||
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::bluetoothremotegattservice::BluetoothRemoteGATTService;
|
||||
use dom::bluetoothuuid::{BluetoothDescriptorUUID, BluetoothUUID};
|
||||
|
@ -102,79 +102,18 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
|
|||
|
||||
#[allow(unrooted_must_root)]
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
|
||||
fn GetDescriptor(&self, descriptor: BluetoothDescriptorUUID) -> Rc<Promise> {
|
||||
let p = Promise::new(&self.global());
|
||||
let p_cx = p.global().get_cx();
|
||||
|
||||
// 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;
|
||||
get_gatt_children(self, true, BluetoothUUID::descriptor, Some(descriptor), self.get_instance_id(),
|
||||
self.Service().Device().Gatt().Connected(), GATTType::Descriptor)
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root)]
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptors
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
|
||||
fn GetDescriptors(&self,
|
||||
descriptor: Option<BluetoothDescriptorUUID>)
|
||||
-> Rc<Promise> {
|
||||
let p = Promise::new(&self.global());
|
||||
let p_cx = p.global().get_cx();
|
||||
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;
|
||||
get_gatt_children(self, false, BluetoothUUID::descriptor, descriptor, self.get_instance_id(),
|
||||
self.Service().Device().Gatt().Connected(), GATTType::Descriptor)
|
||||
}
|
||||
|
||||
// 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>) {
|
||||
let device = self.Service().Device();
|
||||
match response {
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
|
||||
// Step 7.
|
||||
BluetoothResponse::GetDescriptor(descriptor) => {
|
||||
let bt_descriptor = device.get_or_create_descriptor(&descriptor, &self);
|
||||
promise.resolve_native(promise_cx, &bt_descriptor);
|
||||
},
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptors
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
|
||||
// Step 7.
|
||||
BluetoothResponse::GetDescriptors(descriptors_vec) => {
|
||||
BluetoothResponse::GetDescriptors(descriptors_vec, single) => {
|
||||
if single {
|
||||
promise.resolve_native(promise_cx, &device.get_or_create_descriptor(&descriptors_vec[0], &self));
|
||||
return;
|
||||
}
|
||||
let mut descriptors = vec!();
|
||||
for descriptor in descriptors_vec {
|
||||
let bt_descriptor = device.get_or_create_descriptor(&descriptor, &self);
|
||||
|
|
|
@ -2,16 +2,15 @@
|
|||
* 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::blocklist::{Blocklist, uuid_is_blocklisted};
|
||||
use bluetooth_traits::{BluetoothRequest, BluetoothResponse, GATTType};
|
||||
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
|
||||
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding;
|
||||
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::js::{MutJS, Root};
|
||||
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::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID};
|
||||
use dom::globalscope::GlobalScope;
|
||||
|
@ -114,84 +113,24 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
|
|||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice
|
||||
fn GetPrimaryService(&self, service: BluetoothServiceUUID) -> Rc<Promise> {
|
||||
// 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.
|
||||
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 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;
|
||||
get_gatt_children(self, true, BluetoothUUID::service, Some(service), String::from(self.Device().Id()),
|
||||
self.Device().Gatt().Connected(), GATTType::PrimaryService)
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root)]
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices
|
||||
fn GetPrimaryServices(&self, service: Option<BluetoothServiceUUID>) -> Rc<Promise> {
|
||||
// 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 2.
|
||||
get_gatt_children(self, false, BluetoothUUID::service, service, String::from(self.Device().Id()),
|
||||
self.Connected(), GATTType::PrimaryService)
|
||||
|
||||
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 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 {
|
||||
fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) {
|
||||
let device = self.Device();
|
||||
match response {
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect
|
||||
BluetoothResponse::GATTServerConnect(connected) => {
|
||||
|
@ -201,17 +140,14 @@ impl AsyncBluetoothListener for BluetoothRemoteGATTServer {
|
|||
// Step 5.2.5.
|
||||
promise.resolve_native(promise_cx, self);
|
||||
},
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
|
||||
// Step 7.
|
||||
BluetoothResponse::GetPrimaryService(service) => {
|
||||
let bt_service = device.get_or_create_service(&service, &self);
|
||||
promise.resolve_native(promise_cx, &bt_service);
|
||||
},
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
|
||||
// Step 7.
|
||||
BluetoothResponse::GetPrimaryServices(services_vec) => {
|
||||
BluetoothResponse::GetPrimaryServices(services_vec, single) => {
|
||||
let device = self.Device();
|
||||
if single {
|
||||
promise.resolve_native(promise_cx, &device.get_or_create_service(&services_vec[0], &self));
|
||||
return;
|
||||
}
|
||||
let mut services = vec!();
|
||||
for service in services_vec {
|
||||
let bt_service = device.get_or_create_service(&service, &self);
|
||||
|
|
|
@ -2,24 +2,22 @@
|
|||
* 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::blocklist::{Blocklist, uuid_is_blocklisted};
|
||||
use bluetooth_traits::{BluetoothResponse, GATTType};
|
||||
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::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::reflector::{DomObject, reflect_dom_object};
|
||||
use dom::bindings::reflector::reflect_dom_object;
|
||||
use dom::bindings::str::DOMString;
|
||||
use dom::bluetooth::{AsyncBluetoothListener, response_async};
|
||||
use dom::bluetooth::{AsyncBluetoothListener, get_gatt_children};
|
||||
use dom::bluetoothdevice::BluetoothDevice;
|
||||
use dom::bluetoothuuid::{BluetoothCharacteristicUUID, BluetoothServiceUUID, BluetoothUUID};
|
||||
use dom::eventtarget::EventTarget;
|
||||
use dom::globalscope::GlobalScope;
|
||||
use dom::promise::Promise;
|
||||
use ipc_channel::ipc::IpcSender;
|
||||
use js::jsapi::JSContext;
|
||||
use std::rc::Rc;
|
||||
|
||||
|
@ -62,10 +60,6 @@ impl BluetoothRemoteGATTService {
|
|||
BluetoothRemoteGATTServiceBinding::Wrap)
|
||||
}
|
||||
|
||||
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothRequest> {
|
||||
self.global().as_window().bluetooth_thread()
|
||||
}
|
||||
|
||||
fn get_instance_id(&self) -> String {
|
||||
self.instance_id.clone()
|
||||
}
|
||||
|
@ -89,165 +83,39 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
|
|||
|
||||
#[allow(unrooted_must_root)]
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristic
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
|
||||
fn GetCharacteristic(&self,
|
||||
characteristic: BluetoothCharacteristicUUID)
|
||||
-> Rc<Promise> {
|
||||
let p = Promise::new(&self.global());
|
||||
let p_cx = p.global().get_cx();
|
||||
|
||||
// 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;
|
||||
get_gatt_children(self, true, BluetoothUUID::characteristic, Some(characteristic), self.get_instance_id(),
|
||||
self.Device().Gatt().Connected(), GATTType::Characteristic)
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root)]
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
|
||||
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 {
|
||||
// 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;
|
||||
get_gatt_children(self, false, BluetoothUUID::characteristic, characteristic, self.get_instance_id(),
|
||||
self.Device().Gatt().Connected(), GATTType::Characteristic)
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root)]
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservice
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
|
||||
fn GetIncludedService(&self,
|
||||
service: BluetoothServiceUUID)
|
||||
-> Rc<Promise> {
|
||||
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.
|
||||
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;
|
||||
get_gatt_children(self, false, BluetoothUUID::service, Some(service), self.get_instance_id(),
|
||||
self.Device().Gatt().Connected(), GATTType::IncludedService)
|
||||
}
|
||||
|
||||
|
||||
#[allow(unrooted_must_root)]
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservices
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
|
||||
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 {
|
||||
// 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;
|
||||
get_gatt_children(self, false, BluetoothUUID::service, service, self.get_instance_id(),
|
||||
self.Device().Gatt().Connected(), GATTType::IncludedService)
|
||||
}
|
||||
|
||||
// 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>) {
|
||||
let device = self.Device();
|
||||
match response {
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristic
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
|
||||
// Step 7.
|
||||
BluetoothResponse::GetCharacteristic(characteristic) => {
|
||||
let bt_characteristic = device.get_or_create_characteristic(&characteristic, &self);
|
||||
promise.resolve_native(promise_cx, &bt_characteristic);
|
||||
},
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
|
||||
// Step 7.
|
||||
BluetoothResponse::GetCharacteristics(characteristics_vec) => {
|
||||
BluetoothResponse::GetCharacteristics(characteristics_vec, single) => {
|
||||
if single {
|
||||
promise.resolve_native(promise_cx,
|
||||
&device.get_or_create_characteristic(&characteristics_vec[0], &self));
|
||||
return;
|
||||
}
|
||||
let mut characteristics = vec!();
|
||||
for characteristic in characteristics_vec {
|
||||
let bt_characteristic = device.get_or_create_characteristic(&characteristic, &self);
|
||||
|
@ -282,31 +147,19 @@ impl AsyncBluetoothListener for BluetoothRemoteGATTService {
|
|||
}
|
||||
promise.resolve_native(promise_cx, &characteristics);
|
||||
},
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservice
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
|
||||
// Step 7.
|
||||
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);
|
||||
},
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservices
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
|
||||
// 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);
|
||||
BluetoothResponse::GetIncludedServices(services_vec, single) => {
|
||||
if single {
|
||||
promise.resolve_native(promise_cx, &device.get_or_create_service(&services_vec[0], &device.Gatt()));
|
||||
return;
|
||||
}
|
||||
let mut services = vec!();
|
||||
for service in services_vec {
|
||||
let bt_service = device.get_or_create_service(&service, &device.Gatt());
|
||||
services.push(bt_service);
|
||||
}
|
||||
promise.resolve_native(promise_cx, &services);
|
||||
},
|
||||
_ => promise.reject_error(promise_cx, Error::Type("Something went wrong...".to_owned())),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue