diff options
Diffstat (limited to 'components/script/dom/bluetoothremotegattcharacteristic.rs')
-rw-r--r-- | components/script/dom/bluetoothremotegattcharacteristic.rs | 52 |
1 files changed, 39 insertions, 13 deletions
diff --git a/components/script/dom/bluetoothremotegattcharacteristic.rs b/components/script/dom/bluetoothremotegattcharacteristic.rs index 0fae7548ede..1b2a16c1a4b 100644 --- a/components/script/dom/bluetoothremotegattcharacteristic.rs +++ b/components/script/dom/bluetoothremotegattcharacteristic.rs @@ -2,8 +2,8 @@ * 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_blacklist::{Blacklist, uuid_is_blacklisted}; use bluetooth_traits::BluetoothMethodMsg; +use bluetooth_traits::blacklist::{Blacklist, uuid_is_blacklisted}; use dom::bindings::cell::DOMRefCell; use dom::bindings::codegen::Bindings::BluetoothCharacteristicPropertiesBinding:: BluetoothCharacteristicPropertiesMethods; @@ -87,16 +87,26 @@ impl BluetoothRemoteGATTCharacteristic { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) } + if !self.Service().Device().Gatt().Connected() { + return Err(Network) + } 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(), - self, - DOMString::from(descriptor.uuid), - descriptor.instance_id)) + let context = self.service.get().get_device().get_context(); + let mut descriptor_map = context.get_descriptor_map().borrow_mut(); + if let Some(existing_descriptor) = descriptor_map.get(&descriptor.instance_id) { + return Ok(existing_descriptor.get()); + } + let bt_descriptor = BluetoothRemoteGATTDescriptor::new(&self.global(), + self, + DOMString::from(descriptor.uuid), + descriptor.instance_id.clone()); + descriptor_map.insert(descriptor.instance_id, MutHeap::new(&bt_descriptor)); + Ok(bt_descriptor) }, Err(error) => { Err(Error::from(error)) @@ -117,18 +127,34 @@ impl BluetoothRemoteGATTCharacteristic { } } }; + if !self.Service().Device().Gatt().Connected() { + return Err(Network) + } + let mut descriptors = vec!(); 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(), - self, - DOMString::from(desc.uuid), - desc.instance_id)) - .collect()) + let context = self.service.get().get_device().get_context(); + let mut descriptor_map = context.get_descriptor_map().borrow_mut(); + for descriptor in descriptor_vec { + let bt_descriptor = match descriptor_map.get(&descriptor.instance_id) { + Some(existing_descriptor) => existing_descriptor.get(), + None => { + BluetoothRemoteGATTDescriptor::new(&self.global(), + self, + DOMString::from(descriptor.uuid), + descriptor.instance_id.clone()) + }, + }; + if !descriptor_map.contains_key(&descriptor.instance_id) { + descriptor_map.insert(descriptor.instance_id, MutHeap::new(&bt_descriptor)); + } + descriptors.push(bt_descriptor); + } + Ok(descriptors) }, Err(error) => { Err(Error::from(error)) @@ -182,10 +208,10 @@ impl BluetoothRemoteGATTCharacteristic { } let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( - BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap(); + BluetoothMethodMsg::WriteValue(self.get_instance_id(), value.clone(), sender)).unwrap(); let result = receiver.recv().unwrap(); match result { - Ok(_) => Ok(()), + Ok(_) => Ok(*self.value.borrow_mut() = Some(ByteString::new(value))), Err(error) => { Err(Error::from(error)) }, |