From e05a839d25377c9bb0f209f154f0ea998ddc2151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ty=C3=A1s=20Mustoha?= Date: Thu, 15 Sep 2016 10:44:17 +0200 Subject: [PATCH] Update WebBluetooth to use Promises --- components/script/dom/bluetooth.rs | 48 +++++++--- .../dom/bluetoothremotegattcharacteristic.rs | 85 +++++++++++------ .../dom/bluetoothremotegattdescriptor.rs | 53 +++++++---- .../script/dom/bluetoothremotegattserver.rs | 91 +++++++++++------- .../script/dom/bluetoothremotegattservice.rs | 93 +++++++++++++------ .../script/dom/webidls/Bluetooth.webidl | 4 +- .../BluetoothRemoteGATTCharacteristic.webidl | 16 +--- .../BluetoothRemoteGATTDescriptor.webidl | 6 +- .../webidls/BluetoothRemoteGATTServer.webidl | 12 +-- .../webidls/BluetoothRemoteGATTService.webidl | 19 +--- .../bluetooth/bluetooth_battery_level.html | 34 ++++--- .../bluetooth_battery_level_with_filter.html | 34 ++++--- .../bluetooth_characteristic_info.html | 35 ++++--- ..._characteristic_read_value_test_cases.html | 36 ++++--- ...characteristic_write_value_test_cases.html | 48 ++++++---- .../bluetooth/bluetooth_descriptor_info.html | 40 ++++---- ...ooth_descriptor_read_value_test_cases.html | 43 ++++++--- ...oth_descriptor_write_value_test_cases.html | 53 +++++++---- .../bluetooth_device_disconnect.html | 27 +++--- .../html/bluetooth/bluetooth_device_info.html | 12 +-- ...uetooth_get_characteristic_test_cases.html | 32 ++++--- ...etooth_get_characteristics_test_cases.html | 33 ++++--- .../bluetooth_get_descriptor_test_cases.html | 36 ++++--- .../bluetooth_get_descriptors_test_cases.html | 38 +++++--- ...tooth_get_included_service_test_cases.html | 27 +++--- ...ooth_get_included_services_test_cases.html | 27 +++--- ...etooth_get_primary_service_test_cases.html | 27 +++--- ...tooth_get_primary_services_test_cases.html | 28 +++--- .../bluetooth_included_service_info.html | 31 ++++--- .../bluetooth_primary_service_info.html | 28 +++--- .../bluetooth_primary_services_info.html | 35 +++---- .../bluetooth_request_device_test_cases.html | 11 ++- 32 files changed, 684 insertions(+), 458 deletions(-) diff --git a/components/script/dom/bluetooth.rs b/components/script/dom/bluetooth.rs index 2940a211bf3..eecaf6489be 100644 --- a/components/script/dom/bluetooth.rs +++ b/components/script/dom/bluetooth.rs @@ -15,10 +15,13 @@ use dom::bindings::str::DOMString; use dom::bluetoothadvertisingdata::BluetoothAdvertisingData; use dom::bluetoothdevice::BluetoothDevice; use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID}; +use dom::promise::Promise; use ipc_channel::ipc::{self, IpcSender}; +use js::conversions::ToJSValConvertible; use net_traits::bluetooth_scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence}; use net_traits::bluetooth_scanfilter::{RequestDeviceoptions, ServiceUUIDSequence}; use net_traits::bluetooth_thread::{BluetoothError, BluetoothMethodMsg}; +use std::rc::Rc; const FILTER_EMPTY_ERROR: &'static str = "'filters' member, if present, must be nonempty to find any devices."; const FILTER_ERROR: &'static str = "A filter must restrict the devices in some way."; @@ -61,6 +64,22 @@ impl Bluetooth { global_ref.as_window().bluetooth_thread() } + fn request_device(&self, option: &RequestDeviceOptions) -> Fallible> { + // Step 1. + // TODO(#4282): Reject promise. + if (option.filters.is_some() && option.acceptAllDevices) || + (option.filters.is_none() && !option.acceptAllDevices) { + return Err(Type(OPTIONS_ERROR.to_owned())); + } + // Step 2. + if !option.acceptAllDevices { + return self.request_bluetooth_devices(&option.filters, &option.optionalServices); + } + + self.request_bluetooth_devices(&None, &option.optionalServices) + // TODO(#4282): Step 3-5: Reject and resolve promise. + } + // https://webbluetoothcg.github.io/web-bluetooth/#request-bluetooth-devices fn request_bluetooth_devices(&self, filters: &Option>, @@ -252,6 +271,18 @@ fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter, global: GlobalRef) service_data_uuid)) } +#[allow(unrooted_must_root)] +pub fn result_to_promise(global_ref: GlobalRef, + bluetooth_result: Fallible) + -> Rc { + let p = Promise::new(global_ref); + match bluetooth_result { + Ok(v) => p.resolve_native(p.global().r().get_cx(), &v), + Err(e) => p.reject_error(p.global().r().get_cx(), e), + } + p +} + impl From for Error { fn from(error: BluetoothError) -> Self { match error { @@ -265,20 +296,9 @@ impl From for Error { } impl BluetoothMethods for Bluetooth { + #[allow(unrooted_must_root)] // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice - fn RequestDevice(&self, option: &RequestDeviceOptions) -> Fallible> { - // Step 1. - // TODO(#4282): Reject promise. - if (option.filters.is_some() && option.acceptAllDevices) || - (option.filters.is_none() && !option.acceptAllDevices) { - return Err(Type(OPTIONS_ERROR.to_owned())); - } - // Step 2. - if !option.acceptAllDevices { - return self.request_bluetooth_devices(&option.filters, &option.optionalServices); - } - - self.request_bluetooth_devices(&None, &option.optionalServices) - // TODO(#4282): Step 3-5: Reject and resolve promise. + fn RequestDevice(&self, option: &RequestDeviceOptions) -> Rc { + result_to_promise(self.global().r(), self.request_device(option)) } } diff --git a/components/script/dom/bluetoothremotegattcharacteristic.rs b/components/script/dom/bluetoothremotegattcharacteristic.rs index 15ca7af2235..633b0b060e2 100644 --- a/components/script/dom/bluetoothremotegattcharacteristic.rs +++ b/components/script/dom/bluetoothremotegattcharacteristic.rs @@ -18,12 +18,15 @@ use dom::bindings::global::GlobalRef; use dom::bindings::js::{JS, MutHeap, Root}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::str::{ByteString, DOMString}; +use dom::bluetooth::result_to_promise; use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties; use dom::bluetoothremotegattdescriptor::BluetoothRemoteGATTDescriptor; use dom::bluetoothremotegattservice::BluetoothRemoteGATTService; use dom::bluetoothuuid::{BluetoothDescriptorUUID, BluetoothUUID}; +use dom::promise::Promise; use ipc_channel::ipc::{self, IpcSender}; use net_traits::bluetooth_thread::BluetoothMethodMsg; +use std::rc::Rc; // Maximum length of an attribute value. // https://www.bluetooth.org/DocMan/handlers/DownloadDoc.ashx?doc_id=286439 (Vol. 3, page 2169) @@ -79,26 +82,9 @@ impl BluetoothRemoteGATTCharacteristic { fn get_instance_id(&self) -> String { self.instance_id.clone() } -} - -impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteristic { - // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-properties - fn Properties(&self) -> Root { - self.properties.get() - } - - // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-service - fn Service(&self) -> Root { - self.service.get() - } - - // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-uuid - fn Uuid(&self) -> DOMString { - self.uuid.clone() - } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor - fn GetDescriptor(&self, descriptor: BluetoothDescriptorUUID) -> Fallible> { + fn get_descriptor(&self, descriptor: BluetoothDescriptorUUID) -> Fallible> { let uuid = try!(BluetoothUUID::GetDescriptor(self.global().r(), descriptor)).to_string(); if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) @@ -121,9 +107,9 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptors - fn GetDescriptors(&self, - descriptor: Option) - -> Fallible>> { + fn get_descriptors(&self, + descriptor: Option) + -> Fallible>> { let mut uuid: Option = None; if let Some(d) = descriptor { uuid = Some(try!(BluetoothUUID::GetDescriptor(self.global().r(), d)).to_string()); @@ -152,13 +138,8 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris } } - // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-value - fn GetValue(&self) -> Option { - self.value.borrow().clone() - } - // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-readvalue - fn ReadValue(&self) -> Fallible { + fn read_value(&self) -> Fallible { if uuid_is_blacklisted(self.uuid.as_ref(), Blacklist::Reads) { return Err(Security) } @@ -185,7 +166,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue - fn WriteValue(&self, value: Vec) -> ErrorResult { + fn write_value(&self, value: Vec) -> ErrorResult { if uuid_is_blacklisted(self.uuid.as_ref(), Blacklist::Writes) { return Err(Security) } @@ -213,3 +194,51 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris } } } + +impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteristic { + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-properties + fn Properties(&self) -> Root { + self.properties.get() + } + + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-service + fn Service(&self) -> Root { + self.service.get() + } + + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-uuid + fn Uuid(&self) -> DOMString { + self.uuid.clone() + } + + #[allow(unrooted_must_root)] + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor + fn GetDescriptor(&self, descriptor: BluetoothDescriptorUUID) -> Rc { + result_to_promise(self.global().r(), self.get_descriptor(descriptor)) + } + + #[allow(unrooted_must_root)] + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptors + fn GetDescriptors(&self, + descriptor: Option) + -> Rc { + result_to_promise(self.global().r(), self.get_descriptors(descriptor)) + } + + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-value + fn GetValue(&self) -> Option { + self.value.borrow().clone() + } + + #[allow(unrooted_must_root)] + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-readvalue + fn ReadValue(&self) -> Rc { + result_to_promise(self.global().r(), self.read_value()) + } + + #[allow(unrooted_must_root)] + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue + fn WriteValue(&self, value: Vec) -> Rc { + result_to_promise(self.global().r(), self.write_value(value)) + } +} diff --git a/components/script/dom/bluetoothremotegattdescriptor.rs b/components/script/dom/bluetoothremotegattdescriptor.rs index 99065a9aec0..d036f8bd9ea 100644 --- a/components/script/dom/bluetoothremotegattdescriptor.rs +++ b/components/script/dom/bluetoothremotegattdescriptor.rs @@ -17,9 +17,12 @@ use dom::bindings::global::GlobalRef; use dom::bindings::js::{JS, MutHeap, Root}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::str::{ByteString, DOMString}; +use dom::bluetooth::result_to_promise; use dom::bluetoothremotegattcharacteristic::{BluetoothRemoteGATTCharacteristic, MAXIMUM_ATTRIBUTE_LENGTH}; +use dom::promise::Promise; use ipc_channel::ipc::{self, IpcSender}; use net_traits::bluetooth_thread::BluetoothMethodMsg; +use std::rc::Rc; // http://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattdescriptor #[dom_struct] @@ -66,26 +69,9 @@ impl BluetoothRemoteGATTDescriptor { fn get_instance_id(&self) -> String { self.instance_id.clone() } -} - -impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { - // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-characteristic - fn Characteristic(&self) -> Root { - self.characteristic.get() - } - - // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-uuid - fn Uuid(&self) -> DOMString { - self.uuid.clone() - } - - // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-value - fn GetValue(&self) -> Option { - self.value.borrow().clone() - } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-readvalue - fn ReadValue(&self) -> Fallible { + fn read_value(&self) -> Fallible { if uuid_is_blacklisted(self.uuid.as_ref(), Blacklist::Reads) { return Err(Security) } @@ -109,7 +95,7 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue - fn WriteValue(&self, value: Vec) -> ErrorResult { + fn write_value(&self, value: Vec) -> ErrorResult { if uuid_is_blacklisted(self.uuid.as_ref(), Blacklist::Writes) { return Err(Security) } @@ -131,3 +117,32 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { } } } + +impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-characteristic + fn Characteristic(&self) -> Root { + self.characteristic.get() + } + + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-uuid + fn Uuid(&self) -> DOMString { + self.uuid.clone() + } + + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-value + fn GetValue(&self) -> Option { + self.value.borrow().clone() + } + + #[allow(unrooted_must_root)] + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-readvalue + fn ReadValue(&self) -> Rc { + result_to_promise(self.global().r(), self.read_value()) + } + + #[allow(unrooted_must_root)] + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue + fn WriteValue(&self, value: Vec) -> Rc { + result_to_promise(self.global().r(), self.write_value(value)) + } +} diff --git a/components/script/dom/bluetoothremotegattserver.rs b/components/script/dom/bluetoothremotegattserver.rs index 615094623e9..c9fa5477f3f 100644 --- a/components/script/dom/bluetoothremotegattserver.rs +++ b/components/script/dom/bluetoothremotegattserver.rs @@ -12,12 +12,15 @@ use dom::bindings::global::GlobalRef; use dom::bindings::js::{JS, MutHeap, Root}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::str::DOMString; +use dom::bluetooth::result_to_promise; use dom::bluetoothdevice::BluetoothDevice; use dom::bluetoothremotegattservice::BluetoothRemoteGATTService; use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID}; +use dom::promise::Promise; use ipc_channel::ipc::{self, IpcSender}; use net_traits::bluetooth_thread::BluetoothMethodMsg; use std::cell::Cell; +use std::rc::Rc; // https://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattserver #[dom_struct] @@ -47,21 +50,9 @@ impl BluetoothRemoteGATTServer { let global_ref = global_root.r(); global_ref.as_window().bluetooth_thread() } -} - -impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { - // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-device - fn Device(&self) -> Root { - self.device.get() - } - - // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connected - fn Connected(&self) -> bool { - self.connected.get() - } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect - fn Connect(&self) -> Fallible> { + fn connect(&self) -> Fallible> { let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GATTServerConnect(String::from(self.Device().Id()), sender)).unwrap(); @@ -77,25 +68,8 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { } } - // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-disconnect - fn Disconnect(&self) -> ErrorResult { - let (sender, receiver) = ipc::channel().unwrap(); - self.get_bluetooth_thread().send( - BluetoothMethodMsg::GATTServerDisconnect(String::from(self.Device().Id()), sender)).unwrap(); - let server = receiver.recv().unwrap(); - match server { - Ok(connected) => { - self.connected.set(connected); - Ok(()) - }, - Err(error) => { - Err(Error::from(error)) - }, - } - } - // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice - fn GetPrimaryService(&self, service: BluetoothServiceUUID) -> Fallible> { + fn get_primary_service(&self, service: BluetoothServiceUUID) -> Fallible> { let uuid = try!(BluetoothUUID::GetService(self.global().r(), service)).to_string(); if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) @@ -119,9 +93,9 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices - fn GetPrimaryServices(&self, - service: Option) - -> Fallible>> { + fn get_primary_services(&self, + service: Option) + -> Fallible>> { let mut uuid: Option = None; if let Some(s) = service { uuid = Some(try!(BluetoothUUID::GetService(self.global().r(), s)).to_string()); @@ -151,3 +125,52 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { } } } + +impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-device + fn Device(&self) -> Root { + self.device.get() + } + + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connected + fn Connected(&self) -> bool { + self.connected.get() + } + + #[allow(unrooted_must_root)] + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect + fn Connect(&self) -> Rc { + result_to_promise(self.global().r(), self.connect()) + } + + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-disconnect + fn Disconnect(&self) -> ErrorResult { + let (sender, receiver) = ipc::channel().unwrap(); + self.get_bluetooth_thread().send( + BluetoothMethodMsg::GATTServerDisconnect(String::from(self.Device().Id()), sender)).unwrap(); + let server = receiver.recv().unwrap(); + match server { + Ok(connected) => { + self.connected.set(connected); + Ok(()) + }, + Err(error) => { + Err(Error::from(error)) + }, + } + } + + #[allow(unrooted_must_root)] + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice + fn GetPrimaryService(&self, service: BluetoothServiceUUID) -> Rc { + result_to_promise(self.global().r(), self.get_primary_service(service)) + } + + #[allow(unrooted_must_root)] + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices + fn GetPrimaryServices(&self, + service: Option) + -> Rc { + result_to_promise(self.global().r(), self.get_primary_services(service)) + } +} diff --git a/components/script/dom/bluetoothremotegattservice.rs b/components/script/dom/bluetoothremotegattservice.rs index a39bb4fdfd5..709de16cf5f 100644 --- a/components/script/dom/bluetoothremotegattservice.rs +++ b/components/script/dom/bluetoothremotegattservice.rs @@ -11,12 +11,15 @@ use dom::bindings::global::GlobalRef; use dom::bindings::js::{JS, MutHeap, Root}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::str::DOMString; +use dom::bluetooth::result_to_promise; use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties; use dom::bluetoothdevice::BluetoothDevice; use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic; use dom::bluetoothuuid::{BluetoothCharacteristicUUID, BluetoothServiceUUID, BluetoothUUID}; +use dom::promise::Promise; use ipc_channel::ipc::{self, IpcSender}; use net_traits::bluetooth_thread::BluetoothMethodMsg; +use std::rc::Rc; // https://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattservice #[dom_struct] @@ -66,28 +69,11 @@ impl BluetoothRemoteGATTService { 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 { - 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() - } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristic - fn GetCharacteristic(&self, - characteristic: BluetoothCharacteristicUUID) - -> Fallible> { + fn get_characteristic(&self, + characteristic: BluetoothCharacteristicUUID) + -> Fallible> { let uuid = try!(BluetoothUUID::GetCharacteristic(self.global().r(), characteristic)).to_string(); if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) @@ -121,9 +107,9 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics - fn GetCharacteristics(&self, - characteristic: Option) - -> Fallible>> { + fn get_characteristics(&self, + characteristic: Option) + -> Fallible>> { let mut uuid: Option = None; if let Some(c) = characteristic { uuid = Some(try!(BluetoothUUID::GetCharacteristic(self.global().r(), c)).to_string()); @@ -166,9 +152,9 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservice - fn GetIncludedService(&self, - service: BluetoothServiceUUID) - -> Fallible> { + fn get_included_service(&self, + service: BluetoothServiceUUID) + -> Fallible> { let uuid = try!(BluetoothUUID::GetService(self.global().r(), service)).to_string(); if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) @@ -194,9 +180,9 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservices - fn GetIncludedServices(&self, - service: Option) - -> Fallible>> { + fn get_included_services(&self, + service: Option) + -> Fallible>> { let mut uuid: Option = None; if let Some(s) = service { uuid = Some(try!(BluetoothUUID::GetService(self.global().r(), s)).to_string()); @@ -228,3 +214,52 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { } } } + +impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-device + fn Device(&self) -> Root { + 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 { + result_to_promise(self.global().r(), self.get_characteristic(characteristic)) + } + + #[allow(unrooted_must_root)] + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics + fn GetCharacteristics(&self, + characteristic: Option) + -> Rc { + result_to_promise(self.global().r(), self.get_characteristics(characteristic)) + } + + #[allow(unrooted_must_root)] + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservice + fn GetIncludedService(&self, + service: BluetoothServiceUUID) + -> Rc { + result_to_promise(self.global().r(), self.get_included_service(service)) + } + + #[allow(unrooted_must_root)] + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getincludedservices + fn GetIncludedServices(&self, + service: Option) + -> Rc { + result_to_promise(self.global().r(), self.get_included_services(service)) + } +} diff --git a/components/script/dom/webidls/Bluetooth.webidl b/components/script/dom/webidls/Bluetooth.webidl index 6c575db150c..16c30c770d4 100644 --- a/components/script/dom/webidls/Bluetooth.webidl +++ b/components/script/dom/webidls/Bluetooth.webidl @@ -23,9 +23,7 @@ interface Bluetooth { // [SecureContext] // readonly attribute BluetoothDevice? referringDevice; // [SecureContext] -// Promise requestDevice(RequestDeviceOptions options); - [Throws] - BluetoothDevice requestDevice(optional RequestDeviceOptions options); + Promise requestDevice(optional RequestDeviceOptions options); }; // Bluetooth implements EventTarget; diff --git a/components/script/dom/webidls/BluetoothRemoteGATTCharacteristic.webidl b/components/script/dom/webidls/BluetoothRemoteGATTCharacteristic.webidl index fdb3afa8b9a..293ac7f742c 100644 --- a/components/script/dom/webidls/BluetoothRemoteGATTCharacteristic.webidl +++ b/components/script/dom/webidls/BluetoothRemoteGATTCharacteristic.webidl @@ -10,18 +10,12 @@ interface BluetoothRemoteGATTCharacteristic { readonly attribute DOMString uuid; readonly attribute BluetoothCharacteristicProperties properties; readonly attribute ByteString? value; - [Throws] - BluetoothRemoteGATTDescriptor getDescriptor(BluetoothDescriptorUUID descriptor); - [Throws] - sequence getDescriptors(optional BluetoothDescriptorUUID descriptor); - //Promise getDescriptor(BluetoothDescriptorUUID descriptor); - //Promise> - //getDescriptors(optional BluetoothDescriptorUUID descriptor); - [Throws] - ByteString readValue(); + Promise getDescriptor(BluetoothDescriptorUUID descriptor); + Promise> + getDescriptors(optional BluetoothDescriptorUUID descriptor); + Promise readValue(); //Promise readValue(); - [Throws] - void writeValue(sequence value); + Promise writeValue(sequence value); //Promise writeValue(BufferSource value); //Promise startNotifications(); //Promise stopNotifications(); diff --git a/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl b/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl index 8c744929542..7ffd3f2ebb5 100644 --- a/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl +++ b/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl @@ -9,10 +9,8 @@ interface BluetoothRemoteGATTDescriptor { readonly attribute BluetoothRemoteGATTCharacteristic characteristic; readonly attribute DOMString uuid; readonly attribute ByteString? value; - [Throws] - ByteString readValue(); + Promise readValue(); //Promise readValue(); - [Throws] - void writeValue(sequence value); + Promise writeValue(sequence value); //Promise writeValue(BufferSource value); }; diff --git a/components/script/dom/webidls/BluetoothRemoteGATTServer.webidl b/components/script/dom/webidls/BluetoothRemoteGATTServer.webidl index 13314d7c6e1..45e3df198fe 100644 --- a/components/script/dom/webidls/BluetoothRemoteGATTServer.webidl +++ b/components/script/dom/webidls/BluetoothRemoteGATTServer.webidl @@ -8,15 +8,9 @@ interface BluetoothRemoteGATTServer { readonly attribute BluetoothDevice device; readonly attribute boolean connected; - [Throws] - BluetoothRemoteGATTServer connect(); + Promise connect(); [Throws] void disconnect(); - [Throws] - BluetoothRemoteGATTService getPrimaryService(BluetoothServiceUUID service); - [Throws] - sequence getPrimaryServices(optional BluetoothServiceUUID service); - //Promise getPrimaryService(BluetoothServiceUUID service); - //Promise>getPrimaryServices(optional BluetoothServiceUUID service); - //Promise connect(); + Promise getPrimaryService(BluetoothServiceUUID service); + Promise> getPrimaryServices(optional BluetoothServiceUUID service); }; diff --git a/components/script/dom/webidls/BluetoothRemoteGATTService.webidl b/components/script/dom/webidls/BluetoothRemoteGATTService.webidl index a7ee941232a..a484ae64f80 100644 --- a/components/script/dom/webidls/BluetoothRemoteGATTService.webidl +++ b/components/script/dom/webidls/BluetoothRemoteGATTService.webidl @@ -9,18 +9,9 @@ interface BluetoothRemoteGATTService { readonly attribute BluetoothDevice device; readonly attribute DOMString uuid; readonly attribute boolean isPrimary; - [Throws] - BluetoothRemoteGATTCharacteristic getCharacteristic(BluetoothCharacteristicUUID characteristic); - [Throws] - sequence getCharacteristics - (optional BluetoothCharacteristicUUID characteristic); - //PromisegetCharacteristic(BluetoothCharacteristicUUID characteristic); - //Promise> - //getCharacteristics(optional BluetoothCharacteristicUUID characteristic); - [Throws] - BluetoothRemoteGATTService getIncludedService(BluetoothServiceUUID service); - [Throws] - sequence getIncludedServices(optional BluetoothServiceUUID service); - //PromisegetIncludedService(BluetoothServiceUUID service); - //Promise>getIncludedServices(optional BluetoothServiceUUID service); + Promise getCharacteristic(BluetoothCharacteristicUUID characteristic); + Promise> + getCharacteristics(optional BluetoothCharacteristicUUID characteristic); + Promise getIncludedService(BluetoothServiceUUID service); + Promise> getIncludedServices(optional BluetoothServiceUUID service); }; diff --git a/tests/html/bluetooth/bluetooth_battery_level.html b/tests/html/bluetooth/bluetooth_battery_level.html index 77dccf1e40c..be52f6792d1 100644 --- a/tests/html/bluetooth/bluetooth_battery_level.html +++ b/tests/html/bluetooth/bluetooth_battery_level.html @@ -10,26 +10,30 @@ clear(); var options = {filters: [{services: ['battery_service']}], optionalServices: []}; - try { - log('Requesting Bluetooth Device...'); - var bluetooth = window.navigator.bluetooth; - var device = bluetooth.requestDevice(options); - + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice(options) + .then(device => { log('Connecting to GATT Server on device...'); - var server = device.gatt.connect(); - + return device.gatt.connect(); + }) + .then(server => { log('Getting Battery Service...'); - var service = server.getPrimaryService('battery_service'); - + return server.getPrimaryService('battery_service'); + }) + .then(service => { log('Getting Battery Level Characteristic...'); - var characteristic = service.getCharacteristic('battery_level'); - + return service.getCharacteristic('battery_level'); + }) + .then(characteristic => { log('Reading Battery Level...'); - var value = asciiToDecimal(characteristic.readValue()); - log('> Battery Level is ' + value + '%'); - } catch(err) { + return characteristic.readValue(); + }) + .then(value => { + log('> Battery Level is ' + asciiToDecimal(value) + '%'); + }) + .catch(err => { log(err); - } + }); } diff --git a/tests/html/bluetooth/bluetooth_battery_level_with_filter.html b/tests/html/bluetooth/bluetooth_battery_level_with_filter.html index 269aa4e5ab2..08593eba6da 100644 --- a/tests/html/bluetooth/bluetooth_battery_level_with_filter.html +++ b/tests/html/bluetooth/bluetooth_battery_level_with_filter.html @@ -20,26 +20,30 @@ if (filterNamePrefix) options.filters[0].namePrefix = filterNamePrefix; - try { - log('Requesting Bluetooth Device...'); - var bluetooth = window.navigator.bluetooth; - var device = bluetooth.requestDevice(options); - + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice(options) + .then(device => { log('Connecting to GATT Server on device...'); - var server = device.gatt.connect(); - + return device.gatt.connect(); + }) + .then(server => { log('Getting Battery Service...'); - var service = server.getPrimaryService('battery_service'); - + return server.getPrimaryService('battery_service'); + }) + .then(service => { log('Getting Battery Level Characteristic...'); - var characteristic = service.getCharacteristic('battery_level'); - + return service.getCharacteristic('battery_level'); + }) + .then(characteristic => { log('Reading Battery Level...'); - var value = asciiToDecimal(characteristic.readValue()); - log('> Battery Level is ' + value + '%'); - } catch(err) { + return characteristic.readValue(); + }) + .then(value => { + log('> Battery Level is ' + asciiToDecimal(value) + '%'); + }) + .catch(err => { log(err); - } + }); } diff --git a/tests/html/bluetooth/bluetooth_characteristic_info.html b/tests/html/bluetooth/bluetooth_characteristic_info.html index d3eb3a2d255..8370abba825 100644 --- a/tests/html/bluetooth/bluetooth_characteristic_info.html +++ b/tests/html/bluetooth/bluetooth_characteristic_info.html @@ -22,19 +22,21 @@ if (characteristicUuid.startsWith('0x')) characteristicUuid = parseInt(characteristicUuid, 16); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]}); - - log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); - + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]}) + .then(device => { + log('Connecting to GATT Server on device...'); + return device.gatt.connect(); + }) + .then(server => { log('Getting Primary Service...'); - var primaryService = server.getPrimaryService(serviceUuid); - + return server.getPrimaryService(serviceUuid); + }) + .then(service => { log('Getting Characteristic...'); - var characteristic = primaryService.getCharacteristic(characteristicUuid); - + return service.getCharacteristic(characteristicUuid); + }) + .then(characteristic => { log('Characteristic found!'); log('> Characteristic service: ' + characteristic.service.uuid); log('> Characteristic UUID: ' + characteristic.uuid); @@ -47,11 +49,14 @@ log('> Signed Write: ' + characteristic.properties.authenticatedSignedWrites); log('> Queued Write: ' + characteristic.properties.reliableWrite); log('> Writable Auxiliaries: ' + characteristic.properties.writableAuxiliaries); - characteristic.readValue(); - log('> Characteristic value: ' + asciiToDecimal(characteristic.value)); - } catch(err) { + return characteristic.readValue(); + }) + .then(value => { + log('> Characteristic value: ' + asciiToDecimal(value)); + }) + .catch(err => { log(err); - } + }); } diff --git a/tests/html/bluetooth/bluetooth_characteristic_read_value_test_cases.html b/tests/html/bluetooth/bluetooth_characteristic_read_value_test_cases.html index 45b60128de9..54ef538c6ec 100644 --- a/tests/html/bluetooth/bluetooth_characteristic_read_value_test_cases.html +++ b/tests/html/bluetooth/bluetooth_characteristic_read_value_test_cases.html @@ -32,32 +32,42 @@ function onButtonClick(testNumber) { clear(); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]}); + var bt_server; + + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]}) + .then(device => { log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); + return device.gatt.connect(); + }) + .then(server => { + bt_server = server; log('Getting Primary Service "heart_rate"...'); - var primaryService = server.getPrimaryService('heart_rate'); - + return server.getPrimaryService('heart_rate'); + }) + .then(service => { log('Getting Characteristic "' + testCases[testNumber].characteristic + '"...'); - var characteristic = primaryService.getCharacteristic(testCases[testNumber].characteristic); - + return service.getCharacteristic(testCases[testNumber].characteristic); + }) + .then(characteristic => { log('Characteristic found!'); if (testCases[testNumber].mustDisconnect) { log('Disconnecting from server...'); - device.gatt.disconnect(); + bt_server.disconnect(); } log('Reading the value of the Characteristic...'); - characteristic.readValue(); - log('> Characteristic value: ' + asciiToDecimal(characteristic.value)); - } catch(err) { + return characteristic.readValue(); + }) + .then(value => { + log('> Characteristic value: ' + asciiToDecimal(value)); + }) + .catch(err => { log(err); - } + }); } populate(testCases); diff --git a/tests/html/bluetooth/bluetooth_characteristic_write_value_test_cases.html b/tests/html/bluetooth/bluetooth_characteristic_write_value_test_cases.html index db9d9ce084f..84af2a29af0 100644 --- a/tests/html/bluetooth/bluetooth_characteristic_write_value_test_cases.html +++ b/tests/html/bluetooth/bluetooth_characteristic_write_value_test_cases.html @@ -34,28 +34,36 @@ function onButtonClick(testNumber) { clear(); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice({filters: [{services: [0x1234]}]}); - log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); + var bt_server; - log('Getting Primary Service "Test Service"...'); - var primaryService = server.getPrimaryService(0x1234); + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice({filters: [{services: [0x1234]}]}) + .then(device => { + log('Connecting to GATT Server on device...'); + return device.gatt.connect(); + }) + .then(server => { + bt_server = server; + log('Getting Primary Service...'); + return server.getPrimaryService(0x1234); + }) + .then(service => { log('Getting Characteristic "' + testCases[testNumber].characteristic + '"...'); - var characteristic = primaryService.getCharacteristic(testCases[testNumber].characteristic); - + return service.getCharacteristic(testCases[testNumber].characteristic); + }) + .then(characteristic => { log('Characteristic found!'); - log('Reading the old value of the Characteristic...'); - characteristic.readValue(); + return characteristic.readValue(); + }) + .then(value => { log('> Characteristic value: ' + asciiToDecimal(characteristic.value)); if (testCases[testNumber].mustDisconnect) { log('Disconnecting from server...'); - device.gatt.disconnect(); + bt_server.disconnect(); } if (testNumber !== 1) { @@ -64,14 +72,18 @@ log('Writing the value of the Characteristic with a 513 long array...'); } - characteristic.writeValue(testCases[testNumber].valueToWrite); - + return characteristic.writeValue(testCases[testNumber].valueToWrite); + }) + .then(_ => { log('Reading the new value of the Characteristic...'); - characteristic.readValue(); - log('> Characteristic value: ' + asciiToDecimal(characteristic.value)); - } catch(err) { + return characteristic.readValue(); + }) + .then(value => { + log('> Characteristic value: ' + asciiToDecimal(value)); + }) + .catch(err => { log(err); - } + }); } populate(testCases); diff --git a/tests/html/bluetooth/bluetooth_descriptor_info.html b/tests/html/bluetooth/bluetooth_descriptor_info.html index 47d9e294295..92a9a0504c8 100644 --- a/tests/html/bluetooth/bluetooth_descriptor_info.html +++ b/tests/html/bluetooth/bluetooth_descriptor_info.html @@ -27,30 +27,36 @@ if (descriptorUuid.startsWith('0x')) descriptorUuid = parseInt(descriptorUuid, 16); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]}); - - log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); - + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]}) + .then(device => { + log('Connecting to GATT Server on device...'); + return device.gatt.connect(); + }) + .then(server => { log('Getting Primary Service...'); - var primaryService = server.getPrimaryService(serviceUuid); - + return server.getPrimaryService(serviceUuid); + }) + .then(service => { log('Getting Characteristic...'); - var characteristic = primaryService.getCharacteristic(characteristicUuid); - + return service.getCharacteristic(characteristicUuid); + }) + .then(characteristic => { log('Getting Descriptor...'); - var descriptor = characteristic.getDescriptor(descriptorUuid); - + return characteristic.getDescriptor(descriptorUuid); + }) + .then(descriptor => { log('Descriptor found!'); log('> Descriptor characteristic: ' + descriptor.characteristic.uuid); log('> Descriptor UUID: ' + descriptor.uuid); - descriptor.readValue(); - log('> Descriptor value: ' + asciiToDecimal(descriptor.value)); - } catch(err) { + return descriptor.readValue(); + }) + .then(value => { + log('> Descriptor value: ' + asciiToDecimal(value)); + }) + .catch(err => { log(err); - } + }); } diff --git a/tests/html/bluetooth/bluetooth_descriptor_read_value_test_cases.html b/tests/html/bluetooth/bluetooth_descriptor_read_value_test_cases.html index 8a2e553652b..658c6af9583 100644 --- a/tests/html/bluetooth/bluetooth_descriptor_read_value_test_cases.html +++ b/tests/html/bluetooth/bluetooth_descriptor_read_value_test_cases.html @@ -22,33 +22,46 @@ function onButtonClick(testNumber) { clear(); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]}); + var bt_server; + + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]}) + .then(device => { log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); + return device.gatt.connect(); + }) + .then(server => { + bt_server = server; log('Getting Primary Service "heart_rate"...'); - var primaryService = server.getPrimaryService('heart_rate'); - + return server.getPrimaryService('heart_rate'); + }) + .then(service => { log('Getting Characteristic "heart_rate_measurement"...'); - var characteristic = primaryService.getCharacteristic('heart_rate_measurement'); - + return service.getCharacteristic('heart_rate_measurement'); + }) + .then(characteristic => { log('Getting Descriptor "' + testCases[testNumber].descriptor + '"...'); - var descriptor = characteristic.getDescriptor(testCases[testNumber].descriptor); - + return characteristic.getDescriptor(testCases[testNumber].descriptor); + }) + .then(descriptor => { log('Descriptor found!'); + if (testCases[testNumber].mustDisconnect) { log('Disconecting from GATTserver'); - device.gatt.disconnect(); + bt_server.disconnect(); } + log("Reading descriptor's value..."); - descriptor.readValue(); - log('> Descriptor value: ' + asciiToDecimal(descriptor.value)); - } catch(err) { + return descriptor.readValue(); + }) + .then(value => { + log('> Descriptor value: ' + asciiToDecimal(value)); + }) + .catch(err => { log(err); - } + }); } populate(testCases); diff --git a/tests/html/bluetooth/bluetooth_descriptor_write_value_test_cases.html b/tests/html/bluetooth/bluetooth_descriptor_write_value_test_cases.html index 0553023ca7c..d2309726527 100644 --- a/tests/html/bluetooth/bluetooth_descriptor_write_value_test_cases.html +++ b/tests/html/bluetooth/bluetooth_descriptor_write_value_test_cases.html @@ -22,31 +22,44 @@ function onButtonClick(testNumber) { clear(); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice({filters: [{services: [0x1234]}]}); + var bt_server; + var bt_descriptor; + + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice({filters: [{services: [0x1234]}]}) + .then(device => { log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); + return device.gatt.connect(); + }) + .then(server => { + bt_server = server; log('Getting Primary Service "Test Service"...'); - var primaryService = server.getPrimaryService(0x1234); - + return server.getPrimaryService(0x1234); + }) + .then(service => { log('Getting Characteristic "Test Characteristic (0x2345)"...'); - var characteristic = primaryService.getCharacteristic(0x2345); - + return service.getCharacteristic(0x2345); + }) + .then(characteristic => { log('Characteristic found!'); log('Getting Descriptor "' + testCases[testNumber].descriptor + '"...'); - var descriptor = characteristic.getDescriptor(testCases[testNumber].descriptor); + return characteristic.getDescriptor(testCases[testNumber].descriptor); + }) + .then(descriptor => { + bt_descriptor = descriptor; log('Reading the old value of the Descriptor...'); - descriptor.readValue(); - log('> Descriptor value: ' + asciiToDecimal(descriptor.value)); + return descriptor.readValue(); + }) + .then(value => { + log('> Descriptor value: ' + asciiToDecimal(value)); if (testCases[testNumber].mustDisconnect) { log('Disconnecting from server...'); - device.gatt.disconnect(); + bt_server.disconnect(); } if (testNumber !== 1) { @@ -55,14 +68,18 @@ log('Writing the value of the Descriptor with a 513 long array...'); } - descriptor.writeValue(testCases[testNumber].valueToWrite); - + return bt_descriptor.writeValue(testCases[testNumber].valueToWrite); + }) + .then(_ => { log('Reading the new value of the Descriptor...'); - descriptor.readValue(); - log('> Descriptor value: ' + asciiToDecimal(descriptor.value)); - } catch(err) { + return bt_descriptor.readValue(); + }) + .then(value => { + log('> Descriptor value: ' + asciiToDecimal(value)); + }) + .catch(err => { log(err); - } + }); } populate(testCases); diff --git a/tests/html/bluetooth/bluetooth_device_disconnect.html b/tests/html/bluetooth/bluetooth_device_disconnect.html index 3f9771dfee6..15dbb75db05 100644 --- a/tests/html/bluetooth/bluetooth_device_disconnect.html +++ b/tests/html/bluetooth/bluetooth_device_disconnect.html @@ -32,15 +32,18 @@ if (filterNamePrefix) options.filters[0].namePrefix = filterNamePrefix; - try { - clear(); - log('Requesting Bluetooth Device...'); - bluetoothDevice = window.navigator.bluetooth.requestDevice(options); + clear(); + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice(options) + .then(device => { + bluetoothDevice = device; + log('Connecting to Bluetooth Device...'); connect(); - } catch(err) { + }) + .catch(err => { log(err); - } + }); } function onDisconnectButtonClick() { @@ -75,12 +78,14 @@ } function connect() { - try { - log('Result of the connect() method of the GATT Server: ' + bluetoothDevice.gatt.connect()); - log('> Bluetooth Device connected: ' + bluetoothDevice.gatt.connected); - } catch(err) { + bluetoothDevice.gatt.connect() + .then(server => { + log('Result of the connect() method of the GATT Server: ' + server); + log('> Bluetooth Device connected: ' + server.connected); + }) + .catch(err => { log(err); - } + }); } diff --git a/tests/html/bluetooth/bluetooth_device_info.html b/tests/html/bluetooth/bluetooth_device_info.html index 6df38acbeae..b92cd5376cf 100644 --- a/tests/html/bluetooth/bluetooth_device_info.html +++ b/tests/html/bluetooth/bluetooth_device_info.html @@ -28,19 +28,19 @@ if (filterNamePrefix) options.filters.push({namePrefix: filterNamePrefix}); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice(options); - + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice(options) + .then(device => { log('Found a device!'); log('> Name: ' + device.name); log('> Id: ' + device.id); log('> Appearance: ' + device.adData.appearance); log('> Tx Power: ' + device.adData.txPower + ' dBm'); log('> RSSI: ' + device.adData.rssi + ' dBm'); - } catch(err) { + }) + .catch(err => { log(err); - } + }); } diff --git a/tests/html/bluetooth/bluetooth_get_characteristic_test_cases.html b/tests/html/bluetooth/bluetooth_get_characteristic_test_cases.html index 5c9c2fe4f73..c711d7c132b 100644 --- a/tests/html/bluetooth/bluetooth_get_characteristic_test_cases.html +++ b/tests/html/bluetooth/bluetooth_get_characteristic_test_cases.html @@ -44,19 +44,22 @@ function onButtonClick(testNumber) { clear(); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice(testCases[testNumber].options); + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice(testCases[testNumber].options) + .then(device => { log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); - + return device.gatt.connect(); + }) + .then(server => { log('Getting Primary Service "' + testCases[testNumber].service + '"...'); - var primaryService = server.getPrimaryService(testCases[testNumber].service); - + return server.getPrimaryService(testCases[testNumber].service); + }) + .then(service => { log('Getting Characteristic "' + testCases[testNumber].characteristic + '"...'); - var characteristic = primaryService.getCharacteristic(testCases[testNumber].characteristic); - + return service.getCharacteristic(testCases[testNumber].characteristic); + }) + .then(characteristic => { log('Characteristic found!'); log('> Characteristic service: ' + characteristic.service.uuid); log('> Characteristic UUID: ' + characteristic.uuid); @@ -69,11 +72,14 @@ log('> Signed Write: ' + characteristic.properties.authenticatedSignedWrites); log('> Queued Write: ' + characteristic.properties.reliableWrite); log('> Writable Auxiliaries: ' + characteristic.properties.writableAuxiliaries); - characteristic.readValue(); - log('> Characteristic value: ' + asciiToDecimal(characteristic.value)); - } catch(err) { + return characteristic.readValue(); + }) + .then(value => { + log('> Characteristic value: ' + asciiToDecimal(value)); + }) + .catch(err => { log(err); - } + }); } populate(testCases); diff --git a/tests/html/bluetooth/bluetooth_get_characteristics_test_cases.html b/tests/html/bluetooth/bluetooth_get_characteristics_test_cases.html index 81c3e42bb47..073ef4e1b5a 100644 --- a/tests/html/bluetooth/bluetooth_get_characteristics_test_cases.html +++ b/tests/html/bluetooth/bluetooth_get_characteristics_test_cases.html @@ -43,21 +43,25 @@ testCases.push({characteristic: '00002a03-0000-1000-8000-00805f9b34fb', service: 'heart_rate', options: {filters: [{services: ['heart_rate']}], optionalServices: ['cycling_power']} }); //Test 18 testCases.push({characteristic: '00002a25-0000-1000-8000-00805f9b34fb', service: 'heart_rate', options: {filters: [{services: ['heart_rate']}], optionalServices: ['cycling_power']} }); + function onButtonClick(testNumber) { clear(); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice(testCases[testNumber].options); + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice(testCases[testNumber].options) + .then(device => { log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); - + return device.gatt.connect(); + }) + .then(server => { log('Getting Primary Service "' + testCases[testNumber].service + '"...'); - var primaryService = server.getPrimaryService(testCases[testNumber].service); - + return server.getPrimaryService(testCases[testNumber].service); + }) + .then(service => { log('Getting Characteristic "' + testCases[testNumber].characteristic + '"...'); - var characteristics = primaryService.getCharacteristics(testCases[testNumber].characteristic); - + return service.getCharacteristics(testCases[testNumber].characteristic); + }) + .then(characteristics => { log('> List of Characteristics on the current device:'); for(i = 0; i < characteristics.length; ++i) { @@ -73,12 +77,15 @@ log('> Signed Write: ' + characteristics[i].properties.authenticatedSignedWrites); log('> Queued Write: ' + characteristics[i].properties.reliableWrite); log('> Writable Auxiliaries: ' + characteristics[i].properties.writableAuxiliaries); - characteristics[i].readValue(); - log('> Characteristic value: ' + asciiToDecimal(characteristics[i].value)); + characteristics[i].readValue() + .then(value => { + log('> #' + (i+1) + ' Characteristic value: ' + asciiToDecimal(characteristics[i].value)); + }); } - } catch(err) { + }) + .catch(err => { log(err); - } + }); } populate(testCases); diff --git a/tests/html/bluetooth/bluetooth_get_descriptor_test_cases.html b/tests/html/bluetooth/bluetooth_get_descriptor_test_cases.html index e37a0dabd28..bd0754c2b44 100644 --- a/tests/html/bluetooth/bluetooth_get_descriptor_test_cases.html +++ b/tests/html/bluetooth/bluetooth_get_descriptor_test_cases.html @@ -34,30 +34,38 @@ function onButtonClick(testNumber) { clear(); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]}); + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]}) + .then(device => { log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); - + return device.gatt.connect(); + }) + .then(server => { log('Getting Primary Service "heart_rate"...'); - var primaryService = server.getPrimaryService('heart_rate'); + return server.getPrimaryService('heart_rate'); + }) + .then(service => { log('Getting Characteristic "heart_rate_measurement"...'); - var characteristic = primaryService.getCharacteristic('heart_rate_measurement'); - + return service.getCharacteristic('heart_rate_measurement'); + }) + .then(characteristic => { log('Getting Descriptor "' + testCases[testNumber] + '"...'); - var descriptor = characteristic.getDescriptor(testCases[testNumber]); - + return characteristic.getDescriptor(testCases[testNumber]); + }) + .then(descriptor => { log('Descriptor found!'); log('> Descriptor characteristic: ' + descriptor.characteristic.uuid); log('> Descriptor UUID: ' + descriptor.uuid); - descriptor.readValue(); - log('> Descriptor value: ' + asciiToDecimal(descriptor.value)); - } catch(err) { + return descriptor.readValue(); + }) + .then(value => { + log('> Descriptor value: ' + asciiToDecimal(value)); + }) + .catch(err => { log(err); - } + }); } populate(testCases); diff --git a/tests/html/bluetooth/bluetooth_get_descriptors_test_cases.html b/tests/html/bluetooth/bluetooth_get_descriptors_test_cases.html index 3c71e27267c..66e624637e2 100644 --- a/tests/html/bluetooth/bluetooth_get_descriptors_test_cases.html +++ b/tests/html/bluetooth/bluetooth_get_descriptors_test_cases.html @@ -35,31 +35,39 @@ function onButtonClick(testNumber) { clear(); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]}); + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]}) + .then(device => { log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); - + return device.gatt.connect(); + }) + .then(server => { log('Getting Primary Service "heart_rate"...'); - var primaryService = server.getPrimaryService('heart_rate'); - + return server.getPrimaryService('heart_rate'); + }) + .then(service => { log('Getting Characteristic "heart_rate_measurement"...'); - var characteristic = primaryService.getCharacteristic('heart_rate_measurement'); - + return service.getCharacteristic('heart_rate_measurement'); + }) + .then(characteristic => { log('Getting Descriptors "' + testCases[testNumber] + '"...'); - var descriptors = characteristic.getDescriptors(testCases[testNumber]); - + return characteristic.getDescriptors(testCases[testNumber]); + }) + .then(descriptors => { for(i = 0; i < descriptors.length; ++i) { log('> #' + (i+1)); log('> UUID: ' + descriptors[i].uuid); - descriptors[i].readValue(); - log('> Descriptor value: ' + asciiToDecimal(descriptors[i].value)); + + descriptors[i].readValue() + .then(value => { + log('> #' + (i+1)) + 'Descriptor value: ' + asciiToDecimal(value); + }); } - } catch(err) { + }) + .catch(err => { log(err); - } + }); } populate(testCases); diff --git a/tests/html/bluetooth/bluetooth_get_included_service_test_cases.html b/tests/html/bluetooth/bluetooth_get_included_service_test_cases.html index 54e877546b5..a4c467187e0 100644 --- a/tests/html/bluetooth/bluetooth_get_included_service_test_cases.html +++ b/tests/html/bluetooth/bluetooth_get_included_service_test_cases.html @@ -37,27 +37,32 @@ testCases.push({ requestedService: 'battery_service', requestedIncludedService: 0xf000ffc0, options: {filters: [{services: ['battery_service']}]} }); //Test 15 testCases.push({ requestedService: 'battery_service', requestedIncludedService: 0x00001530, options: {filters: [{services: ['battery_service']}]} }); + function onButtonClick(testNumber) { clear(); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice(testCases[testNumber].options); + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice(testCases[testNumber].options) + .then(device => { log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); - + return device.gatt.connect(); + }) + .then(server => { log('Getting Primary Service "' + testCases[testNumber].requestedService + '"...'); - var primaryService = server.getPrimaryService(testCases[testNumber].requestedService); - + return server.getPrimaryService(testCases[testNumber].requestedService); + }) + .then(service => { log('Getting Included Service "' + testCases[testNumber].requestedIncludedService + '"...') - var includedService = primaryService.getIncludedService(testCases[testNumber].requestedIncludedService); - + return service.getIncludedService(testCases[testNumber].requestedIncludedService); + }) + .then(includedService => { log('Primary Service found on device: ' + includedService.device.name); log('> UUID: ' + includedService.uuid); log('> Is primary: ' + includedService.isPrimary); - } catch(err) { + }) + .catch(err => { log(err); - } + }); } populate(testCases); diff --git a/tests/html/bluetooth/bluetooth_get_included_services_test_cases.html b/tests/html/bluetooth/bluetooth_get_included_services_test_cases.html index f0291469de7..319cea24715 100644 --- a/tests/html/bluetooth/bluetooth_get_included_services_test_cases.html +++ b/tests/html/bluetooth/bluetooth_get_included_services_test_cases.html @@ -37,29 +37,34 @@ testCases.push({ requestedService: 'battery_service', requestedIncludedService: 0xf000ffc0, options: {filters: [{services: ['battery_service']}]} }); //Test 15 testCases.push({ requestedService: 'battery_service', requestedIncludedService: 0x00001530, options: {filters: [{services: ['battery_service']}]} }); + function onButtonClick(testNumber) { clear(); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice(testCases[testNumber].options); + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice(testCases[testNumber].options) + .then(device => { log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); - + return device.gatt.connect(); + }) + .then(server => { log('Getting Primary Service "' + testCases[testNumber].requestedService + '"...'); - var primaryService = server.getPrimaryService(testCases[testNumber].requestedService); - + return server.getPrimaryService(testCases[testNumber].requestedService); + }) + .then(service => { log('Getting Included Service "' + testCases[testNumber].requestedIncludedService + '"...') - var includedServices = primaryService.getIncludedServices(testCases[testNumber].requestedIncludedService); - + return service.getIncludedServices(testCases[testNumber].requestedIncludedService); + }) + .then(includedServices => { for(i = 0; i < includedServices.length; ++i) { log('> #' + (i+1)); log('> UUID: ' + includedServices[i].uuid); log('> Is primary: ' + includedServices[i].isPrimary); } - } catch(err) { + }) + .catch(err => { log(err); - } + }); } populate(testCases); diff --git a/tests/html/bluetooth/bluetooth_get_primary_service_test_cases.html b/tests/html/bluetooth/bluetooth_get_primary_service_test_cases.html index 160703754cf..f38da38da3b 100644 --- a/tests/html/bluetooth/bluetooth_get_primary_service_test_cases.html +++ b/tests/html/bluetooth/bluetooth_get_primary_service_test_cases.html @@ -50,22 +50,25 @@ function onButtonClick(testNumber) { clear(); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice(testCases[testNumber].options); + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice(testCases[testNumber].options) + .then(device => { log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); - + return device.gatt.connect(); + }) + .then(server => { log('Getting Primary Service "' + testCases[testNumber].requestedService + '"...'); - var primaryService = server.getPrimaryService(testCases[testNumber].requestedService); - - log('Primary Service found on device: ' + primaryService.device.name); - log('> UUID: ' + primaryService.uuid); - log('> Is primary: ' + primaryService.isPrimary); - } catch(err) { + return server.getPrimaryService(testCases[testNumber].requestedService); + }) + .then(service => { + log('Primary Service found on device: ' + service.device.name); + log('> UUID: ' + service.uuid); + log('> Is primary: ' + service.isPrimary); + }) + .catch(err => { log(err); - } + }); } populate(testCases); diff --git a/tests/html/bluetooth/bluetooth_get_primary_services_test_cases.html b/tests/html/bluetooth/bluetooth_get_primary_services_test_cases.html index 4a00c0efa32..9c72a074f05 100644 --- a/tests/html/bluetooth/bluetooth_get_primary_services_test_cases.html +++ b/tests/html/bluetooth/bluetooth_get_primary_services_test_cases.html @@ -47,26 +47,30 @@ testCases.push({ requestedService: 'cycling_power', options: {filters: [{services: ['battery_service']}], optionalServices: ['cycling_power']} }); //Test 20 testCases.push({ requestedService: '00001818-0000-1000-8000-00805f9b34fb', options: {filters: [{services: ['battery_service']}], optionalServices: ['cycling_power']} }); + function onButtonClick(testNumber) { clear(); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice(testCases[testNumber].options); + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice(testCases[testNumber].options) + .then(device => { log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); - + return device.gatt.connect(); + }) + .then(server => { log('Getting Primary Service "' + testCases[testNumber].requestedService + '"...'); - var primaryServices = server.getPrimaryServices(testCases[testNumber].requestedService); - - for(i = 0; i < primaryServices.length; ++i) { + return server.getPrimaryServices(testCases[testNumber].requestedService); + }) + .then(services => { + for(i = 0; i < services.length; ++i) { log('> #' + (i+1)); - log('> UUID: ' + primaryServices[i].uuid); - log('> Is primary: ' + primaryServices[i].isPrimary); + log('> UUID: ' + services[i].uuid); + log('> Is primary: ' + services[i].isPrimary); } - } catch(err) { + }) + .catch(err => { log(err); - } + }); } populate(testCases); diff --git a/tests/html/bluetooth/bluetooth_included_service_info.html b/tests/html/bluetooth/bluetooth_included_service_info.html index 094ce5fd6a5..6caff21ae52 100644 --- a/tests/html/bluetooth/bluetooth_included_service_info.html +++ b/tests/html/bluetooth/bluetooth_included_service_info.html @@ -28,27 +28,30 @@ if (filterNamePrefix) options.filters.push({namePrefix: filterNamePrefix}); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice(options); - + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice(options) + .then(device => { log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); - + return device.gatt.connect(); + }) + .then(server => { log('Getting Primary Service...'); - var primaryService = server.getPrimaryService(filterService); - - log('Primary Service found on device: ' + primaryService.device.name); - log('> UUID: ' + primaryService.uuid); + return server.getPrimaryService(filterService); + }) + .then(service => { + log('Primary Service found on device: ' + service.device.name); + log('> UUID: ' + service.uuid); log('Getting Included Services...'); - var includedServices = primaryService.getIncludedServices(); - + return service.getIncludedServices(); + }) + .then(includedServices => { log('> Included Services: ' + includedServices.map(s => s.uuid).join('\n' + ' '.repeat(21))); - } catch(err) { + }) + .catch(err => { log(err); - } + }); } diff --git a/tests/html/bluetooth/bluetooth_primary_service_info.html b/tests/html/bluetooth/bluetooth_primary_service_info.html index 3187400b00a..21ea6ff1e16 100644 --- a/tests/html/bluetooth/bluetooth_primary_service_info.html +++ b/tests/html/bluetooth/bluetooth_primary_service_info.html @@ -28,22 +28,24 @@ if (filterNamePrefix) options.filters.push({namePrefix: filterNamePrefix}); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice(options); - + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice(options) + .then(device => { log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); - + return device.gatt.connect(); + }) + .then(server => { log('Getting Primary Service...'); - var primaryService = server.getPrimaryService(filterService); - - log('Primary Service found on device: ' + primaryService.device.name); - log('> UUID: ' + primaryService.uuid); - log('> Is primary: ' + primaryService.isPrimary); - } catch(err) { + return server.getPrimaryService(filterService); + }) + .then(service => { + log('Primary Service found on device: ' + service.device.name); + log('> UUID: ' + service.uuid); + log('> Is primary: ' + service.isPrimary); + }) + .catch(err => { log(err); - } + }); } diff --git a/tests/html/bluetooth/bluetooth_primary_services_info.html b/tests/html/bluetooth/bluetooth_primary_services_info.html index cab413f8360..67f22e0ef7e 100644 --- a/tests/html/bluetooth/bluetooth_primary_services_info.html +++ b/tests/html/bluetooth/bluetooth_primary_services_info.html @@ -28,29 +28,30 @@ if (filterNamePrefix) options.filters.push({namePrefix: filterNamePrefix}); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice(options); - + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice(options) + .then(device => { log('Connecting to GATTserver on device...'); - var server = device.gatt.connect(); - + return device.gatt.connect(); + }) + .then(server => { log('Getting Primary Service...'); - var primaryServices; - if (filterService) { - primaryServices = server.getPrimaryServices(filterService); - } else { - primaryServices = server.getPrimaryServices(); - } + if (filterService) + return server.getPrimaryServices(filterService); + else + return server.getPrimaryServices(); + }) + .then(services => { log('> List of Services on the current device:'); - for(i = 0; i < primaryServices.length; ++i) { + for(i = 0; i < services.length; ++i) { log('> #' + (i+1)); - log('> UUID: ' + primaryServices[i].uuid); - log('> Is primary: ' + primaryServices[i].isPrimary); + log('> UUID: ' + services[i].uuid); + log('> Is primary: ' + services[i].isPrimary); } - } catch(err) { + }) + .catch(err => { log(err); - } + }); } diff --git a/tests/html/bluetooth/bluetooth_request_device_test_cases.html b/tests/html/bluetooth/bluetooth_request_device_test_cases.html index 4e42fdeeeab..93d20bec3c9 100644 --- a/tests/html/bluetooth/bluetooth_request_device_test_cases.html +++ b/tests/html/bluetooth/bluetooth_request_device_test_cases.html @@ -52,19 +52,20 @@ function onButtonClick(testNumber) { clear(); - try { - log('Requesting Bluetooth Device...'); - var device = window.navigator.bluetooth.requestDevice(testCases[testNumber]); + log('Requesting Bluetooth Device...'); + window.navigator.bluetooth.requestDevice(testCases[testNumber]) + .then(device => { log('Found a device!'); log('> Name: ' + device.name); log('> Id: ' + device.id); log('> Appearance: ' + device.adData.appearance); log('> Tx Power: ' + device.adData.txPower + ' dBm'); log('> RSSI: ' + device.adData.rssi + ' dBm'); - } catch(err) { + }) + .catch(err => { log(err); - } + }); } populate(testCases);