Auto merge of #10632 - szeged:bluetooth-ipc, r=jdm

WebBluetooth impementation

Update the current WebBluetooth implementation.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10632)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-05-04 08:06:06 -07:00
commit 944a8dc25a
36 changed files with 2178 additions and 125 deletions

View file

@ -2,12 +2,38 @@
* 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 core::clone::Clone;
use dom::bindings::codegen::Bindings::BluetoothBinding;
use dom::bindings::codegen::Bindings::BluetoothBinding::BluetoothMethods;
use dom::bindings::codegen::Bindings::BluetoothBinding::RequestDeviceOptions;
use dom::bindings::codegen::Bindings::BluetoothBinding::{BluetoothScanFilter, BluetoothMethods};
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::VendorIDSource;
use dom::bindings::error::Error::Type;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothadvertisingdata::BluetoothAdvertisingData;
use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothuuid::BluetoothUUID;
use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence};
use net_traits::bluetooth_scanfilter::{RequestDeviceoptions, ServiceUUIDSequence};
use net_traits::bluetooth_thread::BluetoothMethodMsg;
use util::str::DOMString;
const FILTER_EMPTY_ERROR: &'static str = "'filters' member must be non - empty to find any devices.";
const FILTER_ERROR: &'static str = "A filter must restrict the devices in some way.";
const FILTER_NAME_TOO_LONG_ERROR: &'static str = "A 'name' or 'namePrefix' can't be longer then 29 bytes.";
// 248 is the maximum number of UTF-8 code units in a Bluetooth Device Name.
const MAX_DEVICE_NAME_LENGTH: usize = 248;
// A device name can never be longer than 29 bytes.
// An advertising packet is at most 31 bytes long.
// The length and identifier of the length field take 2 bytes.
// That leaves 29 bytes for the name.
const MAX_FILTER_NAME_LENGTH: usize = 29;
const NAME_PREFIX_ERROR: &'static str = "'namePrefix', if present, must be non - empty.";
const NAME_TOO_LONG_ERROR: &'static str = "A device name can't be longer than 248 bytes.";
const SERVICE_ERROR: &'static str = "'services', if present, must contain at least one service.";
// https://webbluetoothcg.github.io/web-bluetooth/#bluetooth
#[dom_struct]
@ -27,13 +53,113 @@ impl Bluetooth {
global,
BluetoothBinding::Wrap)
}
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> {
let global_root = self.global();
let global_ref = global_root.r();
global_ref.as_window().bluetooth_thread()
}
}
fn canonicalize_filter(filter: &BluetoothScanFilter, global: GlobalRef) -> Fallible<BluetoothScanfilter> {
if filter.services.is_none() && filter.name.is_none() && filter.namePrefix.is_none() {
return Err(Type(FILTER_ERROR.to_owned()));
}
let mut services_vec = vec!();
if let Some(ref services) = filter.services {
if services.is_empty() {
return Err(Type(SERVICE_ERROR.to_owned()));
}
for service in services {
services_vec.push(try!(BluetoothUUID::GetService(global, service.clone())).to_string());
}
}
let mut name = String::new();
if let Some(ref filter_name) = filter.name {
//NOTE: DOMString::len() gives back the size in bytes
if filter_name.len() > MAX_DEVICE_NAME_LENGTH {
return Err(Type(NAME_TOO_LONG_ERROR.to_owned()));
}
if filter_name.len() > MAX_FILTER_NAME_LENGTH {
return Err(Type(FILTER_NAME_TOO_LONG_ERROR.to_owned()));
}
name = filter_name.to_string();
}
let mut name_prefix = String::new();
if let Some(ref filter_name_prefix) = filter.namePrefix {
if filter_name_prefix.is_empty() {
return Err(Type(NAME_PREFIX_ERROR.to_owned()));
}
if filter_name_prefix.len() > MAX_DEVICE_NAME_LENGTH {
return Err(Type(NAME_TOO_LONG_ERROR.to_owned()));
}
if filter_name_prefix.len() > MAX_FILTER_NAME_LENGTH {
return Err(Type(FILTER_NAME_TOO_LONG_ERROR.to_owned()));
}
name_prefix = filter_name_prefix.to_string();
}
Ok(BluetoothScanfilter::new(name, name_prefix, services_vec))
}
fn convert_request_device_options(options: &RequestDeviceOptions,
global: GlobalRef)
-> Fallible<RequestDeviceoptions> {
if options.filters.is_empty() {
return Err(Type(FILTER_EMPTY_ERROR.to_owned()));
}
let mut filters = vec!();
for filter in &options.filters {
filters.push(try!(canonicalize_filter(&filter, global)));
}
let mut optional_services = vec!();
if let Some(ref opt_services) = options.optionalServices {
for opt_service in opt_services {
optional_services.push(try!(BluetoothUUID::GetService(global, opt_service.clone())).to_string());
}
}
Ok(RequestDeviceoptions::new(BluetoothScanfilterSequence::new(filters),
ServiceUUIDSequence::new(optional_services)))
}
impl BluetoothMethods for Bluetooth {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice
fn RequestDevice(&self) -> Option<Root<BluetoothDevice>> {
//UNIMPLEMENTED
None
fn RequestDevice(&self, option: &RequestDeviceOptions) -> Fallible<Root<BluetoothDevice>> {
let (sender, receiver) = ipc::channel().unwrap();
let option = try!(convert_request_device_options(option, self.global().r()));
self.get_bluetooth_thread().send(BluetoothMethodMsg::RequestDevice(option, sender)).unwrap();
let device = receiver.recv().unwrap();
match device {
Ok(device) => {
let ad_data = BluetoothAdvertisingData::new(self.global().r(),
device.appearance,
device.tx_power,
device.rssi);
let vendor_id_source = device.vendor_id_source.map(|vid| match vid.as_str() {
"bluetooth" => VendorIDSource::Bluetooth,
"usb" => VendorIDSource::Usb,
_ => VendorIDSource::Unknown,
});
Ok(BluetoothDevice::new(self.global().r(),
DOMString::from(device.id),
device.name.map(DOMString::from),
&ad_data,
device.device_class,
vendor_id_source,
device.vendor_id,
device.product_id,
device.product_version))
},
Err(error) => {
Err(Type(error))
},
}
}
}

View file

@ -12,13 +12,16 @@ use dom::bindings::reflector::{Reflector, reflect_dom_object};
#[dom_struct]
pub struct BluetoothAdvertisingData {
reflector_: Reflector,
appearance: u16,
txPower: i8,
rssi: i8,
appearance: Option<u16>,
txPower: Option<i8>,
rssi: Option<i8>,
}
impl BluetoothAdvertisingData {
pub fn new_inherited(appearance: u16, txPower: i8, rssi: i8) -> BluetoothAdvertisingData {
pub fn new_inherited(appearance: Option<u16>,
txPower: Option<i8>,
rssi: Option<i8>)
-> BluetoothAdvertisingData {
BluetoothAdvertisingData {
reflector_: Reflector::new(),
appearance: appearance,
@ -27,7 +30,11 @@ impl BluetoothAdvertisingData {
}
}
pub fn new(global: GlobalRef, appearance: u16, txPower: i8, rssi: i8) -> Root<BluetoothAdvertisingData> {
pub fn new(global: GlobalRef,
appearance: Option<u16>,
txPower: Option<i8>,
rssi: Option<i8>)
-> Root<BluetoothAdvertisingData> {
reflect_dom_object(box BluetoothAdvertisingData::new_inherited(appearance,
txPower,
rssi),
@ -39,16 +46,16 @@ impl BluetoothAdvertisingData {
impl BluetoothAdvertisingDataMethods for BluetoothAdvertisingData {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingdata-appearance
fn GetAppearance(&self) -> Option<u16> {
Some(self.appearance)
self.appearance
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingdata-txpower
fn GetTxPower(&self) -> Option<i8> {
Some(self.txPower)
self.txPower
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingdata-rssi
fn GetRssi(&self) -> Option<i8> {
Some(self.rssi)
self.rssi
}
}

View file

@ -5,8 +5,8 @@
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding;
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::{BluetoothDeviceMethods, VendorIDSource};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, Root, MutHeap};
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::js::{JS, Root, MutHeap, MutNullableHeap};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothadvertisingdata::BluetoothAdvertisingData;
use dom::bluetoothremotegattserver::BluetoothRemoteGATTServer;
use util::str::DOMString;
@ -16,26 +16,25 @@ use util::str::DOMString;
pub struct BluetoothDevice {
reflector_: Reflector,
id: DOMString,
name: DOMString,
name: Option<DOMString>,
adData: MutHeap<JS<BluetoothAdvertisingData>>,
deviceClass: u32,
vendorIDSource: VendorIDSource,
vendorID: u32,
productID: u32,
productVersion: u32,
gatt: MutHeap<JS<BluetoothRemoteGATTServer>>,
deviceClass: Option<u32>,
vendorIDSource: Option<VendorIDSource>,
vendorID: Option<u32>,
productID: Option<u32>,
productVersion: Option<u32>,
gatt: MutNullableHeap<JS<BluetoothRemoteGATTServer>>,
}
impl BluetoothDevice {
pub fn new_inherited(id: DOMString,
name: DOMString,
name: Option<DOMString>,
adData: &BluetoothAdvertisingData,
deviceClass: u32,
vendorIDSource: VendorIDSource,
vendorID: u32,
productID: u32,
productVersion: u32,
gatt: &BluetoothRemoteGATTServer)
deviceClass: Option<u32>,
vendorIDSource: Option<VendorIDSource>,
vendorID: Option<u32>,
productID: Option<u32>,
productVersion: Option<u32>)
-> BluetoothDevice {
BluetoothDevice {
reflector_: Reflector::new(),
@ -47,21 +46,20 @@ impl BluetoothDevice {
vendorID: vendorID,
productID: productID,
productVersion: productVersion,
gatt: MutHeap::new(gatt),
gatt: Default::default(),
}
}
pub fn new(global: GlobalRef,
id: DOMString,
name: DOMString,
adData: &BluetoothAdvertisingData,
deviceClass: u32,
vendorIDSource: VendorIDSource,
vendorID: u32,
productID: u32,
productVersion: u32,
gatt: &BluetoothRemoteGATTServer)
-> Root<BluetoothDevice> {
id: DOMString,
name: Option<DOMString>,
adData: &BluetoothAdvertisingData,
deviceClass: Option<u32>,
vendorIDSource: Option<VendorIDSource>,
vendorID: Option<u32>,
productID: Option<u32>,
productVersion: Option<u32>)
-> Root<BluetoothDevice> {
reflect_dom_object(box BluetoothDevice::new_inherited(id,
name,
adData,
@ -69,8 +67,7 @@ impl BluetoothDevice {
vendorIDSource,
vendorID,
productID,
productVersion,
gatt),
productVersion),
global,
BluetoothDeviceBinding::Wrap)
}
@ -85,7 +82,7 @@ impl BluetoothDeviceMethods for BluetoothDevice {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-name
fn GetName(&self) -> Option<DOMString> {
Some(self.name.clone())
self.name.clone()
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-addata
@ -95,31 +92,31 @@ impl BluetoothDeviceMethods for BluetoothDevice {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-deviceclass
fn GetDeviceClass(&self) -> Option<u32> {
Some(self.deviceClass)
self.deviceClass
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-vendoridsource
fn GetVendorIDSource(&self) -> Option<VendorIDSource> {
Some(self.vendorIDSource)
self.vendorIDSource
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-vendorid
fn GetVendorID(&self) -> Option<u32> {
Some(self.vendorID)
self.vendorID
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-productid
fn GetProductID(&self) -> Option<u32> {
Some(self.productID)
self.productID
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-productversion
fn GetProductVersion(&self) -> Option<u32> {
Some(self.productVersion)
self.productVersion
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-gatt
fn Gatt(&self) -> Root<BluetoothRemoteGATTServer> {
self.gatt.get()
self.gatt.or_init(|| BluetoothRemoteGATTServer::new(self.global().r(), self))
}
}

View file

@ -2,16 +2,25 @@
* 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 dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding::
BluetoothRemoteGATTCharacteristicMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
use dom::bindings::error::Error::{Network, Type};
use dom::bindings::error::{Fallible, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::ByteString;
use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties;
use dom::bluetoothremotegattdescriptor::BluetoothRemoteGATTDescriptor;
use dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
use dom::bluetoothuuid::{BluetoothDescriptorUUID, BluetoothUUID};
use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_thread::BluetoothMethodMsg;
use util::str::DOMString;
// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattcharacteristic
@ -21,33 +30,48 @@ pub struct BluetoothRemoteGATTCharacteristic {
service: MutHeap<JS<BluetoothRemoteGATTService>>,
uuid: DOMString,
properties: MutHeap<JS<BluetoothCharacteristicProperties>>,
value: Option<ByteString>,
value: DOMRefCell<Option<ByteString>>,
instanceID: String,
}
impl BluetoothRemoteGATTCharacteristic {
pub fn new_inherited(service: &BluetoothRemoteGATTService,
uuid: DOMString,
properties: &BluetoothCharacteristicProperties)
properties: &BluetoothCharacteristicProperties,
instanceID: String)
-> BluetoothRemoteGATTCharacteristic {
BluetoothRemoteGATTCharacteristic {
reflector_: Reflector::new(),
service: MutHeap::new(service),
uuid: uuid,
properties: MutHeap::new(properties),
value: None,
value: DOMRefCell::new(None),
instanceID: instanceID,
}
}
pub fn new(global: GlobalRef,
service: &BluetoothRemoteGATTService,
uuid: DOMString,
properties: &BluetoothCharacteristicProperties)
properties: &BluetoothCharacteristicProperties,
instanceID: String)
-> Root<BluetoothRemoteGATTCharacteristic> {
reflect_dom_object(box BluetoothRemoteGATTCharacteristic::new_inherited(service,
uuid,
properties),
global,
BluetoothRemoteGATTCharacteristicBinding::Wrap)
properties,
instanceID),
global,
BluetoothRemoteGATTCharacteristicBinding::Wrap)
}
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> {
let global_root = self.global();
let global_ref = global_root.r();
global_ref.as_window().bluetooth_thread()
}
pub fn get_instance_id(&self) -> String {
self.instanceID.clone()
}
}
@ -69,19 +93,89 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor
fn GetDescriptor(&self) -> Option<Root<BluetoothRemoteGATTDescriptor>> {
//UNIMPLEMENTED
None
fn GetDescriptor(&self, descriptor: BluetoothDescriptorUUID) -> Fallible<Root<BluetoothRemoteGATTDescriptor>> {
let uuid = try!(BluetoothUUID::GetDescriptor(self.global().r(), descriptor)).to_string();
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetDescriptor(self.get_instance_id(), uuid, sender)).unwrap();
let descriptor = receiver.recv().unwrap();
match descriptor {
Ok(descriptor) => {
Ok(BluetoothRemoteGATTDescriptor::new(self.global().r(),
self,
DOMString::from(descriptor.uuid),
descriptor.instance_id))
},
Err(error) => {
Err(Type(error))
},
}
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptors
fn GetDescriptors(&self,
descriptor: Option<BluetoothDescriptorUUID>)
-> Fallible<Vec<Root<BluetoothRemoteGATTDescriptor>>> {
let mut uuid: Option<String> = None;
if let Some(d) = descriptor {
uuid = Some(try!(BluetoothUUID::GetDescriptor(self.global().r(), d)).to_string())
};
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetDescriptors(self.get_instance_id(), uuid, sender)).unwrap();
let descriptors_vec = receiver.recv().unwrap();
match descriptors_vec {
Ok(descriptor_vec) => {
Ok(descriptor_vec.into_iter()
.map(|desc| BluetoothRemoteGATTDescriptor::new(self.global().r(),
self,
DOMString::from(desc.uuid),
desc.instance_id))
.collect())
},
Err(error) => {
Err(Type(error))
},
}
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-value
fn GetValue(&self) -> Option<ByteString> {
self.value.clone()
self.value.borrow().clone()
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-readvalue
fn ReadValue(&self) -> ByteString {
//UNIMPLEMENTED
ByteString::new(vec!())
fn ReadValue(&self) -> Fallible<ByteString> {
let (sender, receiver) = ipc::channel().unwrap();
if !self.Service().Device().Gatt().Connected() {
return Err(Network)
}
self.get_bluetooth_thread().send(
BluetoothMethodMsg::ReadValue(self.get_instance_id(), sender)).unwrap();
let result = receiver.recv().unwrap();
let value = match result {
Ok(val) => {
ByteString::new(val)
},
Err(error) => {
return Err(Type(error))
},
};
*self.value.borrow_mut() = Some(value.clone());
Ok(value)
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue
fn WriteValue(&self, value: Vec<u8>) -> ErrorResult {
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap();
let result = receiver.recv().unwrap();
match result {
Ok(_) => Ok(()),
Err(error) => {
Err(Type(error))
},
}
}
}

View file

@ -2,13 +2,23 @@
* 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 dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding::
BluetoothRemoteGATTCharacteristicMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTDescriptorBinding;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTDescriptorBinding::BluetoothRemoteGATTDescriptorMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
use dom::bindings::error::Error::{Type, Network};
use dom::bindings::error::{Fallible, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::ByteString;
use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic;
use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_thread::BluetoothMethodMsg;
use util::str::DOMString;
// http://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattdescriptor
@ -17,30 +27,45 @@ pub struct BluetoothRemoteGATTDescriptor {
reflector_: Reflector,
characteristic: MutHeap<JS<BluetoothRemoteGATTCharacteristic>>,
uuid: DOMString,
value: Option<ByteString>,
value: DOMRefCell<Option<ByteString>>,
instanceID: String,
}
impl BluetoothRemoteGATTDescriptor {
pub fn new_inherited(characteristic: &BluetoothRemoteGATTCharacteristic,
uuid: DOMString)
uuid: DOMString,
instanceID: String)
-> BluetoothRemoteGATTDescriptor {
BluetoothRemoteGATTDescriptor {
reflector_: Reflector::new(),
characteristic: MutHeap::new(characteristic),
uuid: uuid,
value: None,
value: DOMRefCell::new(None),
instanceID: instanceID,
}
}
pub fn new(global: GlobalRef,
characteristic: &BluetoothRemoteGATTCharacteristic,
uuid: DOMString)
uuid: DOMString,
instanceID: String)
-> Root<BluetoothRemoteGATTDescriptor>{
reflect_dom_object(box BluetoothRemoteGATTDescriptor::new_inherited(characteristic,
uuid),
uuid,
instanceID),
global,
BluetoothRemoteGATTDescriptorBinding::Wrap)
}
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> {
let global_root = self.global();
let global_ref = global_root.r();
global_ref.as_window().bluetooth_thread()
}
pub fn get_instance_id(&self) -> String {
self.instanceID.clone()
}
}
impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
@ -57,12 +82,41 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-value
fn GetValue(&self) -> Option<ByteString> {
self.value.clone()
self.value.borrow().clone()
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-readvalue
fn ReadValue(&self) -> ByteString {
//UNIMPLEMENTED
ByteString::new(vec!())
fn ReadValue(&self) -> Fallible<ByteString> {
let (sender, receiver) = ipc::channel().unwrap();
if !self.Characteristic().Service().Device().Gatt().Connected() {
return Err(Network)
}
self.get_bluetooth_thread().send(
BluetoothMethodMsg::ReadValue(self.get_instance_id(), sender)).unwrap();
let result = receiver.recv().unwrap();
let value = match result {
Ok(val) => {
ByteString::new(val)
},
Err(error) => {
return Err(Type(error))
},
};
*self.value.borrow_mut() = Some(value.clone());
Ok(value)
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue
fn WriteValue(&self, value: Vec<u8>) -> ErrorResult {
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap();
let result = receiver.recv().unwrap();
match result {
Ok(_) => Ok(()),
Err(error) => {
Err(Type(error))
},
}
}
}

View file

@ -2,14 +2,21 @@
* 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 dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
use dom::bindings::error::Error::Type;
use dom::bindings::error::{Fallible, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID};
use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_thread::BluetoothMethodMsg;
use std::cell::Cell;
use util::str::DOMString;
// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattserver
#[dom_struct]
@ -20,21 +27,25 @@ pub struct BluetoothRemoteGATTServer {
}
impl BluetoothRemoteGATTServer {
pub fn new_inherited(device: &BluetoothDevice, is_connected: bool) -> BluetoothRemoteGATTServer {
pub fn new_inherited(device: &BluetoothDevice) -> BluetoothRemoteGATTServer {
BluetoothRemoteGATTServer {
reflector_: Reflector::new(),
device: MutHeap::new(device),
connected: Cell::new(is_connected),
connected: Cell::new(false),
}
}
pub fn new(global: GlobalRef, device: &BluetoothDevice, connected: bool) -> Root<BluetoothRemoteGATTServer> {
reflect_dom_object(box BluetoothRemoteGATTServer::new_inherited(
device,
connected),
pub fn new(global: GlobalRef, device: &BluetoothDevice) -> Root<BluetoothRemoteGATTServer> {
reflect_dom_object(box BluetoothRemoteGATTServer::new_inherited(device),
global,
BluetoothRemoteGATTServerBinding::Wrap)
}
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> {
let global_root = self.global();
let global_ref = global_root.r();
global_ref.as_window().bluetooth_thread()
}
}
impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
@ -50,21 +61,85 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect
fn Connect(&self) -> Root<BluetoothRemoteGATTServer> {
if !self.connected.get() {
self.connected.set(true);
fn Connect(&self) -> Fallible<Root<BluetoothRemoteGATTServer>> {
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GATTServerConnect(String::from(self.Device().Id()), sender)).unwrap();
let server = receiver.recv().unwrap();
match server {
Ok(connected) => {
self.connected.set(connected);
Ok(Root::from_ref(self))
},
Err(error) => {
Err(Type(error))
},
}
Root::from_ref(self)
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-disconnect
fn Disconnect(&self) {
self.connected.set(false);
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(Type(error))
},
}
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice
fn GetPrimaryService(&self) -> Option<Root<BluetoothRemoteGATTService>> {
//UNIMPLEMENTED
None
fn GetPrimaryService(&self, service: BluetoothServiceUUID) -> Fallible<Root<BluetoothRemoteGATTService>> {
let uuid = try!(BluetoothUUID::GetService(self.global().r(), service)).to_string();
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetPrimaryService(String::from(self.Device().Id()), uuid, sender)).unwrap();
let service = receiver.recv().unwrap();
match service {
Ok(service) => {
Ok(BluetoothRemoteGATTService::new(self.global().r(),
&self.device.get(),
DOMString::from(service.uuid),
service.is_primary,
service.instance_id))
},
Err(error) => {
Err(Type(error))
},
}
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices
fn GetPrimaryServices(&self,
service: Option<BluetoothServiceUUID>)
-> Fallible<Vec<Root<BluetoothRemoteGATTService>>> {
let mut uuid: Option<String> = None;
if let Some(s) = service {
uuid = Some(try!(BluetoothUUID::GetService(self.global().r(), s)).to_string())
};
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap();
let services_vec = receiver.recv().unwrap();
match services_vec {
Ok(service_vec) => {
Ok(service_vec.into_iter()
.map(|service| BluetoothRemoteGATTService::new(self.global().r(),
&self.device.get(),
DOMString::from(service.uuid),
service.is_primary,
service.instance_id))
.collect())
},
Err(error) => {
Err(Type(error))
},
}
}
}

View file

@ -4,11 +4,17 @@
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
use dom::bindings::error::Error::Type;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties;
use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic;
use dom::bluetoothuuid::{BluetoothCharacteristicUUID, BluetoothUUID};
use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_thread::BluetoothMethodMsg;
use util::str::DOMString;
// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattservice
@ -18,32 +24,47 @@ pub struct BluetoothRemoteGATTService {
device: MutHeap<JS<BluetoothDevice>>,
uuid: DOMString,
isPrimary: bool,
instanceID: String,
}
impl BluetoothRemoteGATTService {
pub fn new_inherited(device: &BluetoothDevice,
uuid: DOMString,
isPrimary: bool)
isPrimary: bool,
instanceID: String)
-> BluetoothRemoteGATTService {
BluetoothRemoteGATTService {
reflector_: Reflector::new(),
device: MutHeap::new(device),
uuid: uuid,
isPrimary: isPrimary,
instanceID: instanceID,
}
}
pub fn new(global: GlobalRef,
device: &BluetoothDevice,
uuid: DOMString,
isPrimary: bool)
isPrimary: bool,
instanceID: String)
-> Root<BluetoothRemoteGATTService> {
reflect_dom_object(box BluetoothRemoteGATTService::new_inherited(device,
uuid,
isPrimary),
isPrimary,
instanceID),
global,
BluetoothRemoteGATTServiceBinding::Wrap)
}
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> {
let global_root = self.global();
let global_ref = global_root.r();
global_ref.as_window().bluetooth_thread()
}
pub fn get_instance_id(&self) -> String {
self.instanceID.clone()
}
}
impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
@ -63,8 +84,75 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristic
fn GetCharacteristic(&self) -> Option<Root<BluetoothRemoteGATTCharacteristic>> {
// UNIMPLEMENTED
None
fn GetCharacteristic(&self,
characteristic: BluetoothCharacteristicUUID)
-> Fallible<Root<BluetoothRemoteGATTCharacteristic>> {
let uuid = try!(BluetoothUUID::GetCharacteristic(self.global().r(), characteristic)).to_string();
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), uuid, sender)).unwrap();
let characteristic = receiver.recv().unwrap();
match characteristic {
Ok(characteristic) => {
let properties = BluetoothCharacteristicProperties::new(self.global().r(),
characteristic.broadcast,
characteristic.read,
characteristic.write_without_response,
characteristic.write,
characteristic.notify,
characteristic.indicate,
characteristic.authenticated_signed_writes,
characteristic.reliable_write,
characteristic.writable_auxiliaries);
Ok(BluetoothRemoteGATTCharacteristic::new(self.global().r(),
self,
DOMString::from(characteristic.uuid),
&properties,
characteristic.instance_id))
},
Err(error) => {
Err(Type(error))
},
}
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics
fn GetCharacteristics(&self,
characteristic: Option<BluetoothCharacteristicUUID>)
-> Fallible<Vec<Root<BluetoothRemoteGATTCharacteristic>>> {
let mut uuid: Option<String> = None;
if let Some(c) = characteristic {
uuid = Some(try!(BluetoothUUID::GetCharacteristic(self.global().r(), c)).to_string())
};
let mut characteristics = vec!();
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetCharacteristics(self.get_instance_id(), uuid, sender)).unwrap();
let characteristics_vec = receiver.recv().unwrap();
match characteristics_vec {
Ok(characteristic_vec) => {
for characteristic in characteristic_vec {
let properties = BluetoothCharacteristicProperties::new(self.global().r(),
characteristic.broadcast,
characteristic.read,
characteristic.write_without_response,
characteristic.write,
characteristic.notify,
characteristic.indicate,
characteristic.authenticated_signed_writes,
characteristic.reliable_write,
characteristic.writable_auxiliaries);
characteristics.push(BluetoothRemoteGATTCharacteristic::new(self.global().r(),
self,
DOMString::from(characteristic.uuid),
&properties,
characteristic.instance_id));
}
Ok(characteristics)
},
Err(error) => {
Err(Type(error))
},
}
}
}

View file

@ -11,6 +11,9 @@ use regex::Regex;
use util::str::DOMString;
pub type UUID = DOMString;
pub type BluetoothServiceUUID = StringOrUnsignedLong;
pub type BluetoothCharacteristicUUID = StringOrUnsignedLong;
pub type BluetoothDescriptorUUID = StringOrUnsignedLong;
// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothuuid
#[dom_struct]
@ -18,27 +21,246 @@ pub struct BluetoothUUID {
reflector_: Reflector,
}
const BLUETOOTH_ASSIGNED_SERVICES: &'static [(&'static str, u32)] = &[
//TODO(zakorgy) create all the services
//https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
const BLUETOOTH_ASSIGNED_SERVICES: &'static [(&'static str, u32)] = &[
("org.bluetooth.service.alert_notification", 0x1811_u32),
("org.bluetooth.service.automation_io", 0x1815_u32),
("org.bluetooth.service.battery_service", 0x180f_u32)
("org.bluetooth.service.battery_service", 0x180f_u32),
("org.bluetooth.service.blood_pressure", 0x1810_u32),
("org.bluetooth.service.body_composition", 0x181b_u32),
("org.bluetooth.service.bond_management", 0x181e_u32),
("org.bluetooth.service.continuous_glucose_monitoring", 0x181f_u32),
("org.bluetooth.service.current_time", 0x1805_u32),
("org.bluetooth.service.cycling_power", 0x1818_u32),
("org.bluetooth.service.cycling_speed_and_cadence", 0x1816_u32),
("org.bluetooth.service.device_information", 0x180a_u32),
("org.bluetooth.service.environmental_sensing", 0x181a_u32),
("org.bluetooth.service.generic_access", 0x1800_u32),
("org.bluetooth.service.generic_attribute", 0x1801_u32),
("org.bluetooth.service.glucose", 0x1808_u32),
("org.bluetooth.service.health_thermometer", 0x1809_u32),
("org.bluetooth.service.heart_rate", 0x180d_u32),
("org.bluetooth.service.http_proxy", 0x1823_u32),
("org.bluetooth.service.human_interface_device", 0x1812_u32),
("org.bluetooth.service.immediate_alert", 0x1802_u32),
("org.bluetooth.service.indoor_positioning", 0x1821_u32),
("org.bluetooth.service.internet_protocol_support", 0x1820_u32),
("org.bluetooth.service.link_loss", 0x1803_u32),
("org.bluetooth.service.location_and_navigation", 0x1819_u32),
("org.bluetooth.service.next_dst_change", 0x1807_u32),
("org.bluetooth.service.object_transfer", 0x1825_u32),
("org.bluetooth.service.phone_alert_status", 0x180e_u32),
("org.bluetooth.service.pulse_oximeter", 0x1822_u32),
("org.bluetooth.service.reference_time_update", 0x1806_u32),
("org.bluetooth.service.running_speed_and_cadence", 0x1814_u32),
("org.bluetooth.service.scan_parameters", 0x1813_u32),
("org.bluetooth.service.transport_discovery", 0x1824),
("org.bluetooth.service.tx_power", 0x1804_u32),
("org.bluetooth.service.user_data", 0x181c_u32),
("org.bluetooth.service.weight_scale", 0x181d_u32),
];
const BLUETOOTH_ASSIGNED_CHARCTERISTICS: &'static [(&'static str, u32)] = &[
//TODO(zakorgy) create all the characteristics
//https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
const BLUETOOTH_ASSIGNED_CHARCTERISTICS: &'static [(&'static str, u32)] = &[
("org.bluetooth.characteristic.aerobic_heart_rate_lower_limit", 0x2a7e_u32),
("org.bluetooth.characteristic.aerobic_heart_rate_upper_limit", 0x2a84_u32),
("org.bluetooth.characteristic.battery_level", 0x2a19_u32)
("org.bluetooth.characteristic.aerobic_threshold", 0x2a7f_u32),
("org.bluetooth.characteristic.age", 0x2a80_u32),
("org.bluetooth.characteristic.aggregate", 0x2a5a_u32),
("org.bluetooth.characteristic.alert_category_id", 0x2a43_u32),
("org.bluetooth.characteristic.alert_category_id_bit_mask", 0x2a42_u32),
("org.bluetooth.characteristic.alert_level", 0x2a06_u32),
("org.bluetooth.characteristic.alert_notification_control_point", 0x2a44_u32),
("org.bluetooth.characteristic.alert_status", 0x2a3f_u32),
("org.bluetooth.characteristic.altitude", 0x2ab3_u32),
("org.bluetooth.characteristic.anaerobic_heart_rate_lower_limit", 0x2a81_u32),
("org.bluetooth.characteristic.anaerobic_heart_rate_upper_limit", 0x2a82_u32),
("org.bluetooth.characteristic.anaerobic_threshold", 0x2a83_u32),
("org.bluetooth.characteristic.analog", 0x2a58_u32),
("org.bluetooth.characteristic.apparent_wind_direction", 0x2a73_u32),
("org.bluetooth.characteristic.apparent_wind_speed", 0x2a72_u32),
("org.bluetooth.characteristic.gap.appearance", 0x2a01_u32),
("org.bluetooth.characteristic.barometric_pressure_trend", 0x2aa3_u32),
("org.bluetooth.characteristic.battery_level", 0x2a19_u32),
("org.bluetooth.characteristic.blood_pressure_feature", 0x2a49_u32),
("org.bluetooth.characteristic.blood_pressure_measurement", 0x2a35_u32),
("org.bluetooth.characteristic.body_composition_feature", 0x2a9b_u32),
("org.bluetooth.characteristic.body_composition_measurement", 0x2a9c_u32),
("org.bluetooth.characteristic.body_sensor_location", 0x2a38_u32),
("org.bluetooth.characteristic.bond_management_control_point", 0x2aa4_u32),
("org.bluetooth.characteristic.bond_management_feature", 0x2aa5_u32),
("org.bluetooth.characteristic.boot_keyboard_input_report", 0x2a22_u32),
("org.bluetooth.characteristic.boot_keyboard_output_report", 0x2a32_u32),
("org.bluetooth.characteristic.boot_mouse_input_report", 0x2a33_u32),
("org.bluetooth.characteristic.gap.central_address_resolution_support", 0x2aa6_u32),
("org.bluetooth.characteristic.cgm_feature", 0x2aa8_u32),
("org.bluetooth.characteristic.cgm_measurement", 0x2aa7_u32),
("org.bluetooth.characteristic.cgm_session_run_time", 0x2aab_u32),
("org.bluetooth.characteristic.cgm_session_start_time", 0x2aaa_u32),
("org.bluetooth.characteristic.cgm_specific_ops_control_point", 0x2aac_u32),
("org.bluetooth.characteristic.cgm_status", 0x2aa9_u32),
("org.bluetooth.characteristic.csc_feature", 0x2a5c_u32),
("org.bluetooth.characteristic.csc_measurement", 0x2a5b_u32),
("org.bluetooth.characteristic.current_time", 0x2a2b_u32),
("org.bluetooth.characteristic.cycling_power_control_point", 0x2a66_u32),
("org.bluetooth.characteristic.cycling_power_feature", 0x2a65_u32),
("org.bluetooth.characteristic.cycling_power_measurement", 0x2a63_u32),
("org.bluetooth.characteristic.cycling_power_vector", 0x2a64_u32),
("org.bluetooth.characteristic.database_change_increment", 0x2a99_u32),
("org.bluetooth.characteristic.date_of_birth", 0x2a85_u32),
("org.bluetooth.characteristic.date_of_threshold_assessment", 0x2a86_u32),
("org.bluetooth.characteristic.date_time", 0x2a08_u32),
("org.bluetooth.characteristic.day_date_time", 0x2a0a_u32),
("org.bluetooth.characteristic.day_of_week", 0x2a09_u32),
("org.bluetooth.characteristic.descriptor_value_changed", 0x2a7d_u32),
("org.bluetooth.characteristic.gap.device_name", 0x2a00_u32),
("org.bluetooth.characteristic.dew_point", 0x2a7b_u32),
("org.bluetooth.characteristic.digital", 0x2a56_u32),
("org.bluetooth.characteristic.dst_offset", 0x2a0d_u32),
("org.bluetooth.characteristic.elevation", 0x2a6c_u32),
("org.bluetooth.characteristic.email_address", 0x2a87_u32),
("org.bluetooth.characteristic.exact_time_256", 0x2a0c_u32),
("org.bluetooth.characteristic.fat_burn_heart_rate_lower_limit", 0x2a88_u32),
("org.bluetooth.characteristic.fat_burn_heart_rate_upper_limit", 0x2a89_u32),
("org.bluetooth.characteristic.firmware_revision_string", 0x2a26_u32),
("org.bluetooth.characteristic.first_name", 0x2a8a_u32),
("org.bluetooth.characteristic.five_zone_heart_rate_limits", 0x2a8b_u32),
("org.bluetooth.characteristic.floor_number", 0x2ab2_u32),
("org.bluetooth.characteristic.gender", 0x2a8c_u32),
("org.bluetooth.characteristic.glucose_feature", 0x2a51_u32),
("org.bluetooth.characteristic.glucose_measurement", 0x2a18_u32),
("org.bluetooth.characteristic.glucose_measurement_context", 0x2a34_u32),
("org.bluetooth.characteristic.gust_factor", 0x2a74_u32),
("org.bluetooth.characteristic.hardware_revision_string", 0x2a27_u32),
("org.bluetooth.characteristic.heart_rate_control_point", 0x2a39_u32),
("org.bluetooth.characteristic.heart_rate_max", 0x2a8d_u32),
("org.bluetooth.characteristic.heart_rate_measurement", 0x2a37_u32),
("org.bluetooth.characteristic.heat_index", 0x2a7a_u32),
("org.bluetooth.characteristic.height", 0x2a8e_u32),
("org.bluetooth.characteristic.hid_control_point", 0x2a4c_u32),
("org.bluetooth.characteristic.hid_information", 0x2a4a_u32),
("org.bluetooth.characteristic.hip_circumference", 0x2a8f_u32),
("org.bluetooth.characteristic.http_control_point", 0x2aba_u32),
("org.bluetooth.characteristic.http_entity_body", 0x2ab9_u32),
("org.bluetooth.characteristic.http_headers", 0x2ab7_u32),
("org.bluetooth.characteristic.http_status_code", 0x2ab8_u32),
("org.bluetooth.characteristic.https_security", 0x2abb_u32),
("org.bluetooth.characteristic.humidity", 0x2a6f_u32),
("org.bluetooth.characteristic.ieee_11073-20601_regulatory_certification_data_list", 0x2a2a_u32),
("org.bluetooth.characteristic.indoor_positioning_configuration", 0x2aad_u32),
("org.bluetooth.characteristic.intermediate_cuff_pressure", 0x2a36_u32),
("org.bluetooth.characteristic.intermediate_temperature", 0x2a1e_u32),
("org.bluetooth.characteristic.irradiance", 0x2a77_u32),
("org.bluetooth.characteristic.language", 0x2aa2_u32),
("org.bluetooth.characteristic.last_name", 0x2a90_u32),
("org.bluetooth.characteristic.latitude", 0x2aae_u32),
("org.bluetooth.characteristic.ln_control_point", 0x2a6b_u32),
("org.bluetooth.characteristic.ln_feature", 0x2a6a_u32),
("org.bluetooth.characteristic.local_east_coordinate.xml", 0x2ab1_u32),
("org.bluetooth.characteristic.local_north_coordinate", 0x2ab0_u32),
("org.bluetooth.characteristic.local_time_information", 0x2a0f_u32),
("org.bluetooth.characteristic.location_and_speed", 0x2a67_u32),
("org.bluetooth.characteristic.location_name", 0x2ab5_u32),
("org.bluetooth.characteristic.longitude", 0x2aaf_u32),
("org.bluetooth.characteristic.magnetic_declination", 0x2a2c_u32),
("org.bluetooth.characteristic.magnetic_flux_density_2d", 0x2aa0_u32),
("org.bluetooth.characteristic.magnetic_flux_density_3d", 0x2aa1_u32),
("org.bluetooth.characteristic.manufacturer_name_string", 0x2a29_u32),
("org.bluetooth.characteristic.maximum_recommended_heart_rate", 0x2a91_u32),
("org.bluetooth.characteristic.measurement_interval", 0x2a21_u32),
("org.bluetooth.characteristic.model_number_string", 0x2a24_u32),
("org.bluetooth.characteristic.navigation", 0x2a68_u32),
("org.bluetooth.characteristic.new_alert", 0x2a46_u32),
("org.bluetooth.characteristic.object_action_control_point", 0x2ac5_u32),
("org.bluetooth.characteristic.object_changed", 0x2ac8_u32),
("org.bluetooth.characteristic.object_first_created", 0x2ac1_u32),
("org.bluetooth.characteristic.object_id", 0x2ac3_u32),
("org.bluetooth.characteristic.object_last_modified", 0x2ac2_u32),
("org.bluetooth.characteristic.object_list_control_point", 0x2ac6_u32),
("org.bluetooth.characteristic.object_list_filter", 0x2ac7_u32),
("org.bluetooth.characteristic.object_name", 0x2abe_u32),
("org.bluetooth.characteristic.object_properties", 0x2ac4_u32),
("org.bluetooth.characteristic.object_size", 0x2ac0_u32),
("org.bluetooth.characteristic.object_type", 0x2abf_u32),
("org.bluetooth.characteristic.ots_feature", 0x2abd_u32),
("org.bluetooth.characteristic.gap.peripheral_preferred_connection_parameters", 0x2a04_u32),
("org.bluetooth.characteristic.gap.peripheral_privacy_flag", 0x2a02_u32),
("org.bluetooth.characteristic.plx_continuous_measurement", 0x2a5f_u32),
("org.bluetooth.characteristic.plx_features", 0x2a60_u32),
("org.bluetooth.characteristic.plx_spot_check_measurement", 0x2a5e_u32),
("org.bluetooth.characteristic.pnp_id", 0x2a50_u32),
("org.bluetooth.characteristic.pollen_concentration", 0x2a75_u32),
("org.bluetooth.characteristic.position_quality", 0x2a69_u32),
("org.bluetooth.characteristic.pressure", 0x2a6d_u32),
("org.bluetooth.characteristic.protocol_mode", 0x2a4e_u32),
("org.bluetooth.characteristic.rainfall", 0x2a78_u32),
("org.bluetooth.characteristic.gap.reconnection_address", 0x2a03_u32),
("org.bluetooth.characteristic.record_access_control_point", 0x2a52_u32),
("org.bluetooth.characteristic.reference_time_information", 0x2a14_u32),
("org.bluetooth.characteristic.report", 0x2a4d_u32),
("org.bluetooth.characteristic.report_map", 0x2a4b_u32),
("org.bluetooth.characteristic.resting_heart_rate", 0x2a92_u32),
("org.bluetooth.characteristic.ringer_control_point", 0x2a40_u32),
("org.bluetooth.characteristic.ringer_setting", 0x2a41_u32),
("org.bluetooth.characteristic.rsc_feature", 0x2a54_u32),
("org.bluetooth.characteristic.rsc_measurement", 0x2a53_u32),
("org.bluetooth.characteristic.sc_control_point", 0x2a55_u32),
("org.bluetooth.characteristic.scan_interval_window", 0x2a4f_u32),
("org.bluetooth.characteristic.scan_refresh", 0x2a31_u32),
("org.bluetooth.characteristic.sensor_location", 0x2a5d_u32),
("org.bluetooth.characteristic.serial_number_string", 0x2a25_u32),
("org.bluetooth.characteristic.gatt.service_changed", 0x2a05_u32),
("org.bluetooth.characteristic.software_revision_string", 0x2a28_u32),
("org.bluetooth.characteristic.sport_type_for_aerobic_and_anaerobic_thresholds", 0x2a93_u32),
("org.bluetooth.characteristic.supported_new_alert_category", 0x2a47_u32),
("org.bluetooth.characteristic.supported_unread_alert_category", 0x2a48_u32),
("org.bluetooth.characteristic.system_id", 0x2a23_u32),
("org.bluetooth.characteristic.tds_control_point", 0x2abc_u32),
("org.bluetooth.characteristic.temperature", 0x2a6e_u32),
("org.bluetooth.characteristic.temperature_measurement", 0x2a1c_u32),
("org.bluetooth.characteristic.temperature_type", 0x2a1d_u32),
("org.bluetooth.characteristic.three_zone_heart_rate_limits", 0x2a94_u32),
("org.bluetooth.characteristic.time_accuracy", 0x2a12_u32),
("org.bluetooth.characteristic.time_source", 0x2a13_u32),
("org.bluetooth.characteristic.time_update_control_point", 0x2a16_u32),
("org.bluetooth.characteristic.time_update_state", 0x2a17_u32),
("org.bluetooth.characteristic.time_with_dst", 0x2a11_u32),
("org.bluetooth.characteristic.time_zone", 0x2a0e_u32),
("org.bluetooth.characteristic.true_wind_direction", 0x2a71_u32),
("org.bluetooth.characteristic.true_wind_speed", 0x2a70_u32),
("org.bluetooth.characteristic.two_zone_heart_rate_limit", 0x2a95_u32),
("org.bluetooth.characteristic.tx_power_level", 0x2a07_u32),
("org.bluetooth.characteristic.uncertainty", 0x2ab4_u32),
("org.bluetooth.characteristic.unread_alert_status", 0x2a45_u32),
("org.bluetooth.characteristic.uri", 0x2ab6_u32),
("org.bluetooth.characteristic.user_control_point", 0x2a9f_u32),
("org.bluetooth.characteristic.user_index", 0x2a9a_u32),
("org.bluetooth.characteristic.uv_index", 0x2a76_u32),
("org.bluetooth.characteristic.vo2_max", 0x2a96_u32),
("org.bluetooth.characteristic.waist_circumference", 0x2a97_u32),
("org.bluetooth.characteristic.weight", 0x2a98_u32),
("org.bluetooth.characteristic.weight_measurement", 0x2a9d_u32),
("org.bluetooth.characteristic.weight_scale_feature", 0x2a9e_u32),
("org.bluetooth.characteristic.wind_chill", 0x2a79_u32),
];
const BLUETOOTH_ASSIGNED_DESCRIPTORS: &'static [(&'static str, u32)] = &[
//TODO(zakorgy) create all the descriptors
//https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
const BLUETOOTH_ASSIGNED_DESCRIPTORS: &'static [(&'static str, u32)] = &[
("org.bluetooth.descriptor.gatt.characteristic_extended_properties", 0x2900_u32),
("org.bluetooth.descriptor.gatt.characteristic_user_description", 0x2901_u32)
("org.bluetooth.descriptor.gatt.characteristic_user_description", 0x2901_u32),
("org.bluetooth.descriptor.gatt.client_characteristic_configuration", 0x2902_u32),
("org.bluetooth.descriptor.gatt.server_characteristic_configuration", 0x2903_u32),
("org.bluetooth.descriptor.gatt.characteristic_presentation_format", 0x2904_u32),
("org.bluetooth.descriptor.gatt.characteristic_aggregate_format", 0x2905_u32),
("org.bluetooth.descriptor.valid_range", 0x2906_u32),
("org.bluetooth.descriptor.external_report_reference", 0x2907_u32),
("org.bluetooth.descriptor.report_reference", 0x2908_u32),
("org.bluetooth.descriptor.number_of_digitals", 0x2909_u32),
("org.bluetooth.descriptor.value_trigger_setting", 0x290a_u32),
("org.bluetooth.descriptor.es_configuration", 0x290b_u32),
("org.bluetooth.descriptor.es_measurement", 0x290c_u32),
("org.bluetooth.descriptor.es_trigger_setting", 0x290d_u32),
("org.bluetooth.descriptor.time_trigger_setting", 0x290e_u32),
];
const BASE_UUID: &'static str = "-0000-1000-8000-00805f9b34fb";
@ -55,9 +277,7 @@ impl BluetoothUUID {
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getservice
pub fn GetService(globalref: GlobalRef,
name: StringOrUnsignedLong)
-> Fallible<UUID> {
pub fn GetService(globalref: GlobalRef, name: BluetoothServiceUUID) -> Fallible<UUID> {
BluetoothUUID::resolve_uuid_name(globalref,
name,
BLUETOOTH_ASSIGNED_SERVICES,
@ -65,9 +285,7 @@ impl BluetoothUUID {
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getcharacteristic
pub fn GetCharacteristic(globalref: GlobalRef,
name: StringOrUnsignedLong)
-> Fallible<UUID> {
pub fn GetCharacteristic(globalref: GlobalRef, name: BluetoothCharacteristicUUID) -> Fallible<UUID> {
BluetoothUUID::resolve_uuid_name(globalref,
name,
BLUETOOTH_ASSIGNED_CHARCTERISTICS,
@ -75,9 +293,7 @@ impl BluetoothUUID {
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getdescriptor
pub fn GetDescriptor(globalref: GlobalRef,
name: StringOrUnsignedLong)
-> Fallible<UUID> {
pub fn GetDescriptor(globalref: GlobalRef, name: BluetoothDescriptorUUID) -> Fallible<UUID> {
BluetoothUUID::resolve_uuid_name(globalref,
name,
BLUETOOTH_ASSIGNED_DESCRIPTORS,
@ -92,7 +308,7 @@ impl BluetoothUUID {
-> Fallible<DOMString> {
match name {
// Step 1
StringOrUnsignedLong::UnsignedLong(unsigned32) =>{
StringOrUnsignedLong::UnsignedLong(unsigned32) => {
Ok(BluetoothUUID::CanonicalUUID(globalref, unsigned32))
},
StringOrUnsignedLong::String(dstring) => {
@ -103,8 +319,7 @@ impl BluetoothUUID {
} else {
// Step 3
let concatenated = format!("{}.{}", prefix, dstring);
let is_in_table = assigned_numbers_table.iter()
.find(|p| p.0 == concatenated);
let is_in_table = assigned_numbers_table.iter().find(|p| p.0 == concatenated);
match is_in_table {
Some(&(_, alias)) => Ok(BluetoothUUID::CanonicalUUID(globalref, alias)),
None => Err(Syntax),
@ -114,3 +329,12 @@ impl BluetoothUUID {
}
}
}
impl Clone for StringOrUnsignedLong {
fn clone(&self) -> StringOrUnsignedLong {
match self {
&StringOrUnsignedLong::String(ref s) => StringOrUnsignedLong::String(s.clone()),
&StringOrUnsignedLong::UnsignedLong(ul) => StringOrUnsignedLong::UnsignedLong(ul),
}
}
}

View file

@ -4,10 +4,22 @@
// https://webbluetoothcg.github.io/web-bluetooth/#bluetooth
dictionary BluetoothScanFilter {
sequence<BluetoothServiceUUID> services;
DOMString name;
DOMString namePrefix;
};
dictionary RequestDeviceOptions {
required sequence<BluetoothScanFilter> filters;
sequence<BluetoothServiceUUID> optionalServices /*= []*/;
};
[Pref="dom.bluetooth.enabled"]
interface Bluetooth {
// Promise<BluetoothDevice> requestDevice(RequestDeviceOptions options);
BluetoothDevice? requestDevice(/*RequestDeviceOptions options*/);
[Throws]
BluetoothDevice requestDevice(RequestDeviceOptions options);
};
// Bluetooth implements EventTarget;

View file

@ -7,7 +7,8 @@
// Allocation authorities for Vendor IDs:
enum VendorIDSource {
"bluetooth",
"usb"
"usb",
"unknown"
};
[Pref="dom.bluetooth.enabled"]

View file

@ -10,12 +10,18 @@ interface BluetoothRemoteGATTCharacteristic {
readonly attribute DOMString uuid;
readonly attribute BluetoothCharacteristicProperties properties;
readonly attribute ByteString? value;
BluetoothRemoteGATTDescriptor? getDescriptor(/*BluetoothDescriptorUUID descriptor*/);
[Throws]
BluetoothRemoteGATTDescriptor getDescriptor(BluetoothDescriptorUUID descriptor);
[Throws]
sequence<BluetoothRemoteGATTDescriptor> getDescriptors(optional BluetoothDescriptorUUID descriptor);
//Promise<BluetoothRemoteGATTDescriptor> getDescriptor(BluetoothDescriptorUUID descriptor);
//Promise<sequence<BluetoothRemoteGATTDescriptor>>
//getDescriptors(optional BluetoothDescriptorUUID descriptor);
//Promise<DataView> readValue();
[Throws]
ByteString readValue();
//Promise<DataView> readValue();
[Throws]
void writeValue(sequence<octet> value);
//Promise<void> writeValue(BufferSource value);
//Promise<void> startNotifications();
//Promise<void> stopNotifications();

View file

@ -9,8 +9,10 @@ interface BluetoothRemoteGATTDescriptor {
readonly attribute BluetoothRemoteGATTCharacteristic characteristic;
readonly attribute DOMString uuid;
readonly attribute ByteString? value;
[Throws]
ByteString readValue();
//Promise<DataView> readValue();
[Throws]
void writeValue(sequence<octet> value);
//Promise<void> writeValue(BufferSource value);
};

View file

@ -8,9 +8,14 @@
interface BluetoothRemoteGATTServer {
readonly attribute BluetoothDevice device;
readonly attribute boolean connected;
[Throws]
BluetoothRemoteGATTServer connect();
[Throws]
void disconnect();
BluetoothRemoteGATTService? getPrimaryService();
[Throws]
BluetoothRemoteGATTService getPrimaryService(BluetoothServiceUUID service);
[Throws]
sequence<BluetoothRemoteGATTService> getPrimaryServices(optional BluetoothServiceUUID service);
//Promise<BluetoothRemoteGATTService> getPrimaryService(BluetoothServiceUUID service);
//Promise<sequence<BluetoothRemoteGATTService>>getPrimaryServices(optional BluetoothServiceUUID service);
//Promise<BluetoothRemoteGATTServer> connect();

View file

@ -9,7 +9,11 @@ interface BluetoothRemoteGATTService {
readonly attribute BluetoothDevice device;
readonly attribute DOMString uuid;
readonly attribute boolean isPrimary;
BluetoothRemoteGATTCharacteristic? getCharacteristic(/*DOMString characteristic*/);
[Throws]
BluetoothRemoteGATTCharacteristic getCharacteristic(BluetoothCharacteristicUUID characteristic);
[Throws]
sequence<BluetoothRemoteGATTCharacteristic> getCharacteristics
(optional BluetoothCharacteristicUUID characteristic);
//Promise<BluetoothRemoteGATTCharacteristic>getCharacteristic(BluetoothCharacteristicUUID characteristic);
//Promise<sequence<BluetoothRemoteGATTCharacteristic>>
//getCharacteristics(optional BluetoothCharacteristicUUID characteristic);

View file

@ -47,6 +47,7 @@ use msg::constellation_msg::{ConstellationChan, LoadData, PipelineId, SubpageId}
use msg::constellation_msg::{WindowSizeData, WindowSizeType};
use msg::webdriver_msg::{WebDriverJSError, WebDriverJSResult};
use net_traits::ResourceThread;
use net_traits::bluetooth_thread::BluetoothMethodMsg;
use net_traits::image_cache_thread::{ImageCacheChan, ImageCacheThread};
use net_traits::storage_thread::{StorageThread, StorageType};
use num_traits::ToPrimitive;
@ -212,6 +213,10 @@ pub struct Window {
#[ignore_heap_size_of = "channels are hard"]
resource_thread: Arc<ResourceThread>,
/// A handle for communicating messages to the bluetooth thread.
#[ignore_heap_size_of = "channels are hard"]
bluetooth_thread: IpcSender<BluetoothMethodMsg>,
/// A handle for communicating messages to the storage thread.
#[ignore_heap_size_of = "channels are hard"]
storage_thread: StorageThread,
@ -334,6 +339,10 @@ impl Window {
&*self.page
}
pub fn bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> {
self.bluetooth_thread.clone()
}
pub fn storage_thread(&self) -> StorageThread {
self.storage_thread.clone()
}
@ -1407,6 +1416,7 @@ impl Window {
compositor: IpcSender<ScriptToCompositorMsg>,
image_cache_thread: ImageCacheThread,
resource_thread: Arc<ResourceThread>,
bluetooth_thread: IpcSender<BluetoothMethodMsg>,
storage_thread: StorageThread,
mem_profiler_chan: mem::ProfilerChan,
devtools_chan: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
@ -1462,6 +1472,7 @@ impl Window {
dom_static: GlobalStaticData::new(),
js_runtime: DOMRefCell::new(Some(runtime.clone())),
resource_thread: resource_thread,
bluetooth_thread: bluetooth_thread,
storage_thread: storage_thread,
constellation_chan: constellation_chan,
page_clip_rect: Cell::new(MAX_RECT),

View file

@ -64,6 +64,7 @@ use msg::constellation_msg::{PipelineId, PipelineNamespace};
use msg::constellation_msg::{SubpageId, WindowSizeData, WindowSizeType};
use msg::webdriver_msg::WebDriverScriptCommand;
use net_traits::LoadData as NetLoadData;
use net_traits::bluetooth_thread::BluetoothMethodMsg;
use net_traits::image_cache_thread::{ImageCacheChan, ImageCacheResult, ImageCacheThread};
use net_traits::storage_thread::StorageThread;
use net_traits::{AsyncResponseTarget, ControlMsg, LoadConsumer, LoadContext, Metadata, ResourceThread};
@ -310,6 +311,8 @@ pub struct ScriptThread {
/// A handle to the resource thread. This is an `Arc` to avoid running out of file descriptors if
/// there are many iframes.
resource_thread: Arc<ResourceThread>,
/// A handle to the bluetooth thread.
bluetooth_thread: IpcSender<BluetoothMethodMsg>,
/// A handle to the storage thread.
storage_thread: StorageThread,
@ -554,6 +557,7 @@ impl ScriptThread {
image_cache_port: image_cache_port,
resource_thread: Arc::new(state.resource_thread),
bluetooth_thread: state.bluetooth_thread,
storage_thread: state.storage_thread,
port: port,
@ -1496,6 +1500,7 @@ impl ScriptThread {
self.compositor.borrow_mut().clone(),
self.image_cache_thread.clone(),
self.resource_thread.clone(),
self.bluetooth_thread.clone(),
self.storage_thread.clone(),
self.mem_profiler_chan.clone(),
self.devtools_chan.clone(),