diff options
author | Valentin Fokin <fokinv@inf.u-szeged.hu> | 2016-12-01 11:46:23 +0100 |
---|---|---|
committer | Attila Dusnoki <dati91@users.noreply.github.com> | 2016-12-02 12:50:53 +0100 |
commit | c33d89c92c3ed6fe1cddcc57e24b03090926bfe2 (patch) | |
tree | d6950c1e907312fe8718911b8d37014353fedff2 /components/script/dom/bluetoothdevice.rs | |
parent | 0d896a8d820e31037d536f133b5a8b8c43c2e1a2 (diff) | |
download | servo-c33d89c92c3ed6fe1cddcc57e24b03090926bfe2.tar.gz servo-c33d89c92c3ed6fe1cddcc57e24b03090926bfe2.zip |
Moved the AttributeInstanceMaps from bluetooth to bluetoothDevice.
Diffstat (limited to 'components/script/dom/bluetoothdevice.rs')
-rw-r--r-- | components/script/dom/bluetoothdevice.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/components/script/dom/bluetoothdevice.rs b/components/script/dom/bluetoothdevice.rs index 4af6b2a4f59..7db09e95c91 100644 --- a/components/script/dom/bluetoothdevice.rs +++ b/components/script/dom/bluetoothdevice.rs @@ -2,17 +2,26 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use bluetooth_traits::{BluetoothCharacteristicMsg, BluetoothDescriptorMsg, BluetoothServiceMsg}; +use dom::bindings::cell::DOMRefCell; use dom::bindings::codegen::Bindings::BluetoothDeviceBinding; use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods; +use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods; use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull; use dom::bindings::js::{JS, Root, MutHeap, MutNullableHeap}; use dom::bindings::reflector::{Reflectable, reflect_dom_object}; use dom::bindings::str::DOMString; use dom::bluetooth::Bluetooth; use dom::bluetoothadvertisingdata::BluetoothAdvertisingData; +use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties; +use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic; +use dom::bluetoothremotegattdescriptor::BluetoothRemoteGATTDescriptor; use dom::bluetoothremotegattserver::BluetoothRemoteGATTServer; +use dom::bluetoothremotegattservice::BluetoothRemoteGATTService; use dom::eventtarget::EventTarget; use dom::globalscope::GlobalScope; +use std::collections::HashMap; + // https://webbluetoothcg.github.io/web-bluetooth/#bluetoothdevice #[dom_struct] @@ -23,6 +32,9 @@ pub struct BluetoothDevice { ad_data: MutHeap<JS<BluetoothAdvertisingData>>, gatt: MutNullableHeap<JS<BluetoothRemoteGATTServer>>, context: MutHeap<JS<Bluetooth>>, + attribute_instance_map: (DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTService>>>>, + DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTCharacteristic>>>>, + DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTDescriptor>>>>), } impl BluetoothDevice { @@ -38,6 +50,9 @@ impl BluetoothDevice { ad_data: MutHeap::new(ad_data), gatt: Default::default(), context: MutHeap::new(context), + attribute_instance_map: (DOMRefCell::new(HashMap::new()), + DOMRefCell::new(HashMap::new()), + DOMRefCell::new(HashMap::new())), } } @@ -58,6 +73,70 @@ impl BluetoothDevice { pub fn get_context(&self) -> Root<Bluetooth> { self.context.get() } + + pub fn get_or_create_service(&self, + service: &BluetoothServiceMsg, + server: &BluetoothRemoteGATTServer) + -> Root<BluetoothRemoteGATTService> { + let (ref service_map_ref, _, _) = self.attribute_instance_map; + let mut service_map = service_map_ref.borrow_mut(); + if let Some(existing_service) = service_map.get(&service.instance_id) { + return existing_service.get(); + } + let bt_service = BluetoothRemoteGATTService::new(&server.global(), + &server.Device(), + DOMString::from(service.uuid.clone()), + service.is_primary, + service.instance_id.clone()); + service_map.insert(service.instance_id.clone(), MutHeap::new(&bt_service)); + return bt_service; + } + + pub fn get_or_create_characteristic(&self, + characteristic: &BluetoothCharacteristicMsg, + service: &BluetoothRemoteGATTService) + -> Root<BluetoothRemoteGATTCharacteristic> { + let (_, ref characteristic_map_ref, _) = self.attribute_instance_map; + let mut characteristic_map = characteristic_map_ref.borrow_mut(); + if let Some(existing_characteristic) = characteristic_map.get(&characteristic.instance_id) { + return existing_characteristic.get(); + } + let properties = + BluetoothCharacteristicProperties::new(&service.global(), + characteristic.broadcast, + characteristic.read, + characteristic.write_without_response, + characteristic.write, + characteristic.notify, + characteristic.indicate, + characteristic.authenticated_signed_writes, + characteristic.reliable_write, + characteristic.writable_auxiliaries); + let bt_characteristic = BluetoothRemoteGATTCharacteristic::new(&service.global(), + service, + DOMString::from(characteristic.uuid.clone()), + &properties, + characteristic.instance_id.clone()); + characteristic_map.insert(characteristic.instance_id.clone(), MutHeap::new(&bt_characteristic)); + return bt_characteristic; + } + + pub fn get_or_create_descriptor(&self, + descriptor: &BluetoothDescriptorMsg, + characteristic: &BluetoothRemoteGATTCharacteristic) + -> Root<BluetoothRemoteGATTDescriptor> { + let (_, _, ref descriptor_map_ref) = self.attribute_instance_map; + let mut descriptor_map = descriptor_map_ref.borrow_mut(); + if let Some(existing_descriptor) = descriptor_map.get(&descriptor.instance_id) { + return existing_descriptor.get(); + } + let bt_descriptor = BluetoothRemoteGATTDescriptor::new(&characteristic.global(), + characteristic, + DOMString::from(descriptor.uuid.clone()), + descriptor.instance_id.clone()); + descriptor_map.insert(descriptor.instance_id.clone(), MutHeap::new(&bt_descriptor)); + return bt_descriptor; + } } impl BluetoothDeviceMethods for BluetoothDevice { |