diff options
author | fokinv <fokin.valentin@stud.u-szeged.hu> | 2016-03-17 11:57:20 +0100 |
---|---|---|
committer | Attila Dusnoki <dati91@gmail.com> | 2016-05-03 10:17:41 +0200 |
commit | 9825ea41b49e4cff5ccccaef0dbeef9a982d7010 (patch) | |
tree | 965ee08b6168e0657106b58b79f092813dc8a3f4 /components/script/dom/bluetoothremotegattdescriptor.rs | |
parent | 9d55748de24fc140302ef4dffefe43acb998c269 (diff) | |
download | servo-9825ea41b49e4cff5ccccaef0dbeef9a982d7010.tar.gz servo-9825ea41b49e4cff5ccccaef0dbeef9a982d7010.zip |
Add Bluetooth IPC support
Diffstat (limited to 'components/script/dom/bluetoothremotegattdescriptor.rs')
-rw-r--r-- | components/script/dom/bluetoothremotegattdescriptor.rs | 85 |
1 files changed, 75 insertions, 10 deletions
diff --git a/components/script/dom/bluetoothremotegattdescriptor.rs b/components/script/dom/bluetoothremotegattdescriptor.rs index 5812a2ea832..7b55e1d962e 100644 --- a/components/script/dom/bluetoothremotegattdescriptor.rs +++ b/components/script/dom/bluetoothremotegattdescriptor.rs @@ -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::Network; +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::bindings::str::ByteString; use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic; +use ipc_channel::ipc::{self, IpcSender}; +use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg}; 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,52 @@ 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() { + Err(Network) + } + else { + self.get_bluetooth_thread().send( + BluetoothMethodMsg::ReadValue(self.get_instance_id(), sender)).unwrap(); + let result = receiver.recv().unwrap(); + let value = match result { + BluetoothObjectMsg::BluetoothReadValue { + value + } => { + Some(ByteString::new(value)) + }, + BluetoothObjectMsg::Error { + error + } => { + println!("{}", error); + None + }, + _ => unreachable!() + }; + *self.value.borrow_mut() = value; + Ok(self.GetValue().unwrap()) + } + } + + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue + fn WriteValue(&self, value: Vec<u8>) { + 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 { + BluetoothObjectMsg::BluetoothWriteValue => (), + BluetoothObjectMsg::Error { + error + } => { + println!("{}", error); + }, + _ => unreachable!() + }; } } |