Add Bluetooth IPC support

This commit is contained in:
fokinv 2016-03-17 11:57:20 +01:00 committed by Attila Dusnoki
parent 9d55748de2
commit 9825ea41b4
20 changed files with 1037 additions and 46 deletions

View file

@ -2,16 +2,24 @@
* 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;
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::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties;
use dom::bluetoothremotegattdescriptor::BluetoothRemoteGATTDescriptor;
use dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg};
use util::str::DOMString;
// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattcharacteristic
@ -21,34 +29,49 @@ 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),
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()
}
}
impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteristic {
@ -70,18 +93,78 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor
fn GetDescriptor(&self) -> Option<Root<BluetoothRemoteGATTDescriptor>> {
//UNIMPLEMENTED
None
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetDescriptor(self.get_instance_id(), sender)).unwrap();
let descriptor = receiver.recv().unwrap();
match descriptor {
BluetoothObjectMsg::BluetoothDescriptor {
uuid,
instance_id
} => {
Some(BluetoothRemoteGATTDescriptor::new(self.global().r(),
&self,
DOMString::from(uuid),
instance_id))
},
BluetoothObjectMsg::Error {
error
} => {
println!("{}", error);
None
},
_ => unreachable!()
}
}
// 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() {
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-bluetoothremotegattcharacteristic-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!()
};
}
}