Implementation of the getPrimaryService(s), the getCharacteristic(s) and the getDescriptor(s) functions.

This commit is contained in:
fokinv 2016-03-31 10:35:24 +02:00 committed by Attila Dusnoki
parent 9825ea41b4
commit b01c52c18f
9 changed files with 411 additions and 59 deletions

View file

@ -4,12 +4,14 @@
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties;
use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic;
use dom::bluetoothuuid::BluetoothUUID;
use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg};
use util::str::DOMString;
@ -81,10 +83,19 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristic
fn GetCharacteristic(&self) -> Option<Root<BluetoothRemoteGATTCharacteristic>> {
fn GetCharacteristic(&self, characteristic: StringOrUnsignedLong)
-> Option<Root<BluetoothRemoteGATTCharacteristic>> {
let uuid: String = match BluetoothUUID::GetCharacteristic(self.global().r(),
characteristic.clone()) {
Ok(domstring) => domstring.to_string(),
Err(_) => {
println!("No UUID provided!");
return None;
},
};
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), sender)).unwrap();
BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), uuid, sender)).unwrap();
let characteristic = receiver.recv().unwrap();
match characteristic {
BluetoothObjectMsg::BluetoothCharacteristic {
@ -122,7 +133,74 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
println!("{}", error);
None
},
_ => unreachable!()
_ => unreachable!(),
}
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics
fn GetCharacteristics(&self, characteristic: Option<StringOrUnsignedLong>)
-> Option<Vec<Root<BluetoothRemoteGATTCharacteristic>>> {
let uuid: Option<String> = match characteristic {
Some(c) => match BluetoothUUID::GetCharacteristic(self.global().r(), c.clone()) {
Ok(domstring) => Some(domstring.to_string()),
Err(_) => None,
},
None => None,
};
let mut characteristics: Vec<Root<BluetoothRemoteGATTCharacteristic>> = 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 {
BluetoothObjectMsg::BluetoothCharacteristics {
characteristics_vec
} => {
for characteristic in characteristics_vec {
match characteristic {
BluetoothObjectMsg::BluetoothCharacteristic {
uuid,
instance_id,
broadcast,
read,
write_without_response,
write,
notify,
indicate,
authenticated_signed_writes,
reliable_write,
writable_auxiliaries,
} => {
let properties = &BluetoothCharacteristicProperties::new(
self.global().r(),
broadcast,
read,
write_without_response,
write,
notify,
indicate,
authenticated_signed_writes,
reliable_write,
writable_auxiliaries);
characteristics.push(BluetoothRemoteGATTCharacteristic::new(
self.global().r(),
&self,
DOMString::from(uuid),
properties,
instance_id))
},
_ => unreachable!(),
}
}
Some(characteristics)
},
BluetoothObjectMsg::Error {
error
} => {
println!("{}", error);
None
},
_ => unreachable!(),
}
}
}