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

@ -9,7 +9,8 @@ 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::codegen::UnionTypes::StringOrUnsignedLong;
use dom::bindings::error::Error::{Network, Type};
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
@ -18,6 +19,7 @@ use dom::bindings::str::ByteString;
use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties;
use dom::bluetoothremotegattdescriptor::BluetoothRemoteGATTDescriptor;
use dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
use dom::bluetoothuuid::BluetoothUUID;
use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg};
use util::str::DOMString;
@ -92,31 +94,77 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor
fn GetDescriptor(&self) -> Option<Root<BluetoothRemoteGATTDescriptor>> {
fn GetDescriptor(&self,
descriptor: StringOrUnsignedLong)
-> Fallible<Root<BluetoothRemoteGATTDescriptor>> {
let uuid: String = match BluetoothUUID::GetDescriptor(self.global().r(), descriptor.clone()) {
Ok(domstring) => domstring.to_string(),
Err(error) => return Err(error),
};
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetDescriptor(self.get_instance_id(), sender)).unwrap();
BluetoothMethodMsg::GetDescriptor(self.get_instance_id(), uuid, 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))
Ok(BluetoothRemoteGATTDescriptor::new(self.global().r(),
&self,
DOMString::from(uuid),
instance_id))
},
BluetoothObjectMsg::Error {
error
} => {
println!("{}", error);
None
},
} => Err(Type(error)),
_ => unreachable!()
}
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptors
fn GetDescriptors(&self,
descriptor: Option<StringOrUnsignedLong>)
-> Fallible<Vec<Root<BluetoothRemoteGATTDescriptor>>> {
let uuid: Option<String> = match descriptor {
Some(d) => match BluetoothUUID::GetDescriptor(self.global().r(), d.clone()) {
Ok(domstring) => Some(domstring.to_string()),
Err(error) => return Err(error),
},
None => None,
};
let (sender, receiver) = ipc::channel().unwrap();
let mut descriptors: Vec<Root<BluetoothRemoteGATTDescriptor>> = vec!();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetDescriptors(self.get_instance_id(), uuid, sender)).unwrap();
let descriptors_vec = receiver.recv().unwrap();
match descriptors_vec {
BluetoothObjectMsg::BluetoothDescriptors {
descriptors_vec
} => {
for d in descriptors_vec {
match d {
BluetoothObjectMsg::BluetoothDescriptor {
uuid,
instance_id,
} => {
descriptors.push(BluetoothRemoteGATTDescriptor::new(self.global().r(),
&self,
DOMString::from(uuid),
instance_id))
},
_ => unreachable!(),
}
}
Ok(descriptors)
},
BluetoothObjectMsg::Error {
error
} => Err(Type(error)),
_ => unreachable!(),
}
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-value
fn GetValue(&self) -> Option<ByteString> {
self.value.borrow().clone()

View file

@ -5,11 +5,13 @@
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
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::bluetoothdevice::BluetoothDevice;
use dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
use dom::bluetoothuuid::BluetoothUUID;
use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg};
use std::cell::Cell;
@ -101,16 +103,23 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice
fn GetPrimaryService(&self) -> Option<Root<BluetoothRemoteGATTService>> {
fn GetPrimaryService(&self, service: StringOrUnsignedLong) -> Option<Root<BluetoothRemoteGATTService>> {
let uuid: String = match BluetoothUUID::GetService(self.global().r(), service.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::GetPrimaryService(String::from(self.Device().Id()), sender)).unwrap();
BluetoothMethodMsg::GetPrimaryService(String::from(self.Device().Id()), uuid, sender)).unwrap();
let service = receiver.recv().unwrap();
match service {
BluetoothObjectMsg::BluetoothService {
uuid,
is_primary,
instance_id
instance_id,
} => {
Some(BluetoothRemoteGATTService::new(self.global().r(),
&self.device.get(),
@ -124,7 +133,54 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
println!("{}", error);
None
},
_ => unreachable!()
_ => unreachable!(),
}
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices
fn GetPrimaryServices(&self, service: Option<StringOrUnsignedLong>)
-> Option<Vec<Root<BluetoothRemoteGATTService>>> {
let uuid: Option<String> = match service {
Some(s) => match BluetoothUUID::GetService(self.global().r(), s.clone()) {
Ok(domstring) => Some(domstring.to_string()),
Err(_) => None,
},
None => None,
};
let mut services: Vec<Root<BluetoothRemoteGATTService>> = vec!();
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap();
let services_vec = receiver.recv().unwrap();
match services_vec {
BluetoothObjectMsg::BluetoothServices {
services_vec
} => {
for s in services_vec {
match s {
BluetoothObjectMsg::BluetoothService {
uuid,
is_primary,
instance_id,
} => {
services.push(BluetoothRemoteGATTService::new(self.global().r(),
&self.device.get(),
DOMString::from(uuid),
is_primary,
instance_id))
},
_ => unreachable!(),
}
}
Some(services)
},
BluetoothObjectMsg::Error {
error
} => {
println!("{}", error);
None
},
_ => unreachable!(),
}
}
}

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!(),
}
}
}

View file

@ -333,3 +333,12 @@ impl BluetoothUUID {
}
}
}
impl Clone for StringOrUnsignedLong {
fn clone(&self) -> StringOrUnsignedLong {
match self {
&StringOrUnsignedLong::String(ref s) => StringOrUnsignedLong::String(s.clone()),
&StringOrUnsignedLong::UnsignedLong(ul) => StringOrUnsignedLong::UnsignedLong(ul),
}
}
}

View file

@ -10,7 +10,10 @@ interface BluetoothRemoteGATTCharacteristic {
readonly attribute DOMString uuid;
readonly attribute BluetoothCharacteristicProperties properties;
readonly attribute ByteString? value;
BluetoothRemoteGATTDescriptor? getDescriptor(/*BluetoothDescriptorUUID descriptor*/);
[Throws]
BluetoothRemoteGATTDescriptor getDescriptor((DOMString or unsigned long) descriptor);
[Throws]
sequence<BluetoothRemoteGATTDescriptor> getDescriptors(optional (DOMString or unsigned long) descriptor);
//Promise<BluetoothRemoteGATTDescriptor> getDescriptor(BluetoothDescriptorUUID descriptor);
//Promise<sequence<BluetoothRemoteGATTDescriptor>>
//getDescriptors(optional BluetoothDescriptorUUID descriptor);

View file

@ -10,7 +10,8 @@ interface BluetoothRemoteGATTServer {
readonly attribute boolean connected;
BluetoothRemoteGATTServer connect();
void disconnect();
BluetoothRemoteGATTService? getPrimaryService();
BluetoothRemoteGATTService? getPrimaryService((DOMString or unsigned long) service);
sequence<BluetoothRemoteGATTService>? getPrimaryServices(optional (DOMString or unsigned long) service);
//Promise<BluetoothRemoteGATTService> getPrimaryService(BluetoothServiceUUID service);
//Promise<sequence<BluetoothRemoteGATTService>>getPrimaryServices(optional BluetoothServiceUUID service);
//Promise<BluetoothRemoteGATTServer> connect();

View file

@ -9,7 +9,9 @@ interface BluetoothRemoteGATTService {
readonly attribute BluetoothDevice device;
readonly attribute DOMString uuid;
readonly attribute boolean isPrimary;
BluetoothRemoteGATTCharacteristic? getCharacteristic(/*DOMString characteristic*/);
BluetoothRemoteGATTCharacteristic? getCharacteristic((DOMString or unsigned long) characteristic);
sequence<BluetoothRemoteGATTCharacteristic>? getCharacteristics
(optional (DOMString or unsigned long) characteristic);
//Promise<BluetoothRemoteGATTCharacteristic>getCharacteristic(BluetoothCharacteristicUUID characteristic);
//Promise<sequence<BluetoothRemoteGATTCharacteristic>>
//getCharacteristics(optional BluetoothCharacteristicUUID characteristic);