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

@ -85,14 +85,23 @@ impl BluetoothManager {
BluetoothMethodMsg::GATTServerDisconnect(device_id, sender) => { BluetoothMethodMsg::GATTServerDisconnect(device_id, sender) => {
self.gatt_server_disconnect(device_id, sender) self.gatt_server_disconnect(device_id, sender)
} }
BluetoothMethodMsg::GetPrimaryService(device_id, sender) => { BluetoothMethodMsg::GetPrimaryService(device_id, uuid, sender) => {
self.get_primary_service(device_id, sender) self.get_primary_service(device_id, uuid, sender)
} }
BluetoothMethodMsg::GetCharacteristic(service_id, sender) => { BluetoothMethodMsg::GetPrimaryServices(device_id, uuid, sender) => {
self.get_characteristic(service_id, sender) self.get_primary_services(device_id, uuid, sender)
} }
BluetoothMethodMsg::GetDescriptor(characteristic_id, sender) => { BluetoothMethodMsg::GetCharacteristic(service_id, uuid, sender) => {
self.get_descriptor(characteristic_id, sender) self.get_characteristic(service_id, uuid, sender)
}
BluetoothMethodMsg::GetCharacteristics(service_id, uuid, sender) => {
self.get_characteristics(service_id, uuid, sender)
}
BluetoothMethodMsg::GetDescriptor(characteristic_id, uuid, sender) => {
self.get_descriptor(characteristic_id, uuid, sender)
}
BluetoothMethodMsg::GetDescriptors(characteristic_id, uuid, sender) => {
self.get_descriptors(characteristic_id, uuid, sender)
} }
BluetoothMethodMsg::ReadValue(id, sender) => { BluetoothMethodMsg::ReadValue(id, sender) => {
self.read_value(id, sender) self.read_value(id, sender)
@ -164,7 +173,6 @@ impl BluetoothManager {
None None
} }
#[allow(dead_code)]
fn get_gatt_service_by_uuid(&mut self, fn get_gatt_service_by_uuid(&mut self,
adapter: &mut BluetoothAdapter, adapter: &mut BluetoothAdapter,
device_id: &str, device_id: &str,
@ -185,6 +193,21 @@ impl BluetoothManager {
None None
} }
fn get_gatt_services_by_uuid(&mut self,
adapter: &mut BluetoothAdapter,
device_id: &str,
service_uuid: &str)
-> Vec<BluetoothGATTService> {
let mut services_vec: Vec<BluetoothGATTService> = vec!();
let services = self.get_gatt_services(adapter, device_id);
for service in services {
if service.get_uuid().unwrap_or("".to_owned()) == service_uuid {
services_vec.push(service.clone());
}
}
services_vec
}
// Characteristic // Characteristic
fn get_gatt_characteristics(&mut self, fn get_gatt_characteristics(&mut self,
@ -218,7 +241,6 @@ impl BluetoothManager {
None None
} }
#[allow(dead_code)]
fn get_gatt_characteristic_by_uuid(&mut self, fn get_gatt_characteristic_by_uuid(&mut self,
adapter: &mut BluetoothAdapter, adapter: &mut BluetoothAdapter,
service_id: &str, service_id: &str,
@ -239,6 +261,21 @@ impl BluetoothManager {
None None
} }
fn get_gatt_characteristics_by_uuid(&mut self,
adapter: &mut BluetoothAdapter,
service_id: &str,
characteristic_uuid: &str)
-> Vec<BluetoothGATTCharacteristic> {
let mut characteristics_vec: Vec<BluetoothGATTCharacteristic> = vec!();
let characteristics = self.get_gatt_characteristics(adapter, service_id);
for characteristic in characteristics {
if characteristic.get_uuid().unwrap_or("".to_owned()) == characteristic_uuid {
characteristics_vec.push(characteristic.clone());
}
}
characteristics_vec
}
fn get_characteristic_properties(&self, characteristic: &BluetoothGATTCharacteristic) -> [bool; 9] { fn get_characteristic_properties(&self, characteristic: &BluetoothGATTCharacteristic) -> [bool; 9] {
let mut props = [false; 9]; let mut props = [false; 9];
let flags = characteristic.get_flags().unwrap_or(vec!()); let flags = characteristic.get_flags().unwrap_or(vec!());
@ -292,7 +329,6 @@ impl BluetoothManager {
None None
} }
#[allow(dead_code)]
fn get_gatt_descriptor_by_uuid(&mut self, fn get_gatt_descriptor_by_uuid(&mut self,
adapter: &mut BluetoothAdapter, adapter: &mut BluetoothAdapter,
characteristic_id: &str, characteristic_id: &str,
@ -313,6 +349,21 @@ impl BluetoothManager {
None None
} }
fn get_gatt_descriptors_by_uuid(&mut self,
adapter: &mut BluetoothAdapter,
characteristic_id: &str,
descriptor_uuid: &str)
-> Vec<BluetoothGATTDescriptor> {
let mut descriptors_vec: Vec<BluetoothGATTDescriptor> = vec!();
let descriptors = self.get_gatt_descriptors(adapter, characteristic_id);
for descriptor in descriptors {
if descriptor.get_uuid().unwrap_or("".to_owned()) == descriptor_uuid {
descriptors_vec.push(descriptor.clone());
}
}
descriptors_vec
}
// Methods // Methods
fn request_device(&mut self, sender: IpcSender<BluetoothObjectMsg>) { fn request_device(&mut self, sender: IpcSender<BluetoothObjectMsg>) {
@ -395,44 +446,67 @@ impl BluetoothManager {
sender.send(message).unwrap(); sender.send(message).unwrap();
} }
pub fn get_primary_service(&mut self, device_id: String, sender: IpcSender<BluetoothObjectMsg>) { pub fn get_primary_service(&mut self, device_id: String, uuid: String, sender: IpcSender<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, "No adapter found"), None => send_error!(sender, "No adapter found"),
}; };
let service = match self.get_gatt_service_by_uuid(&mut adapter, &device_id, &uuid) {
Some(s) => s,
None => send_error!(sender, "No primary service found")
};
if !service.is_primary().unwrap_or(false) {
send_error!(sender, "No primary service found");
}
let message = BluetoothObjectMsg::BluetoothService {
uuid: service.get_uuid().unwrap_or("".to_owned()),
is_primary: true,
instance_id: service.get_object_path(),
};
sender.send(message).unwrap();
}
let services = self.get_gatt_services(&mut adapter, &device_id); pub fn get_primary_services(&mut self,
device_id: String,
uuid: Option<String>,
sender: IpcSender<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() {
Some(a) => a,
None => send_error!(sender, "No adapter found"),
};
let services: Vec<BluetoothGATTService> = match uuid {
Some(id) => self.get_gatt_services_by_uuid(&mut adapter, &device_id, &id),
None => self.get_gatt_services(&mut adapter, &device_id),
};
if services.is_empty() { if services.is_empty() {
send_error!(sender, "No service found"); send_error!(sender, "No service found");
} }
let mut services_vec: Vec<BluetoothObjectMsg> = vec!();
for service in services { for service in services {
if service.is_primary().unwrap_or(false) { if service.is_primary().unwrap_or(false) {
let message = BluetoothObjectMsg::BluetoothService { services_vec.push(BluetoothObjectMsg::BluetoothService {
uuid: service.get_uuid().unwrap_or("".to_owned()), uuid: service.get_uuid().unwrap_or("".to_owned()),
is_primary: true, is_primary: true,
instance_id: service.get_object_path() instance_id: service.get_object_path(),
}; });
sender.send(message).unwrap();
return;
} }
} }
if services_vec.is_empty() {
send_error!(sender, "No primary service found"); send_error!(sender, "No service found");
}
let message = BluetoothObjectMsg::BluetoothServices { services_vec: services_vec };
sender.send(message).unwrap();
} }
pub fn get_characteristic(&mut self, service_id: String, sender: IpcSender<BluetoothObjectMsg>) { pub fn get_characteristic(&mut self, service_id: String, uuid: String, sender: IpcSender<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, "No adapter found"), None => send_error!(sender, "No adapter found"),
}; };
let characteristic = match self.get_gatt_characteristic_by_uuid(&mut adapter, &service_id, &uuid) {
let characteristics = self.get_gatt_characteristics(&mut adapter, &service_id); Some(c) => c,
if characteristics.is_empty() { None => send_error!(sender, "No characteristic found"),
send_error!(sender, "No characteristic found"); };
}
let characteristic = &characteristics[0];
let properties = self.get_characteristic_properties(&characteristic); let properties = self.get_characteristic_properties(&characteristic);
let message = BluetoothObjectMsg::BluetoothCharacteristic { let message = BluetoothObjectMsg::BluetoothCharacteristic {
uuid: characteristic.get_uuid().unwrap_or("".to_owned()), uuid: characteristic.get_uuid().unwrap_or("".to_owned()),
@ -445,23 +519,63 @@ impl BluetoothManager {
indicate: properties[5], indicate: properties[5],
authenticated_signed_writes: properties[6], authenticated_signed_writes: properties[6],
reliable_write: properties[7], reliable_write: properties[7],
writable_auxiliaries: properties[8] writable_auxiliaries: properties[8],
}; };
sender.send(message).unwrap(); sender.send(message).unwrap();
} }
pub fn get_descriptor(&mut self, characteristic_id: String, sender: IpcSender<BluetoothObjectMsg>) { pub fn get_characteristics(&mut self,
service_id: String,
uuid: Option<String>,
sender: IpcSender<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() {
Some(a) => a,
None => send_error!(sender, "No adapter found"),
};
let characteristics = match uuid {
Some(id) => self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &id),
None => self.get_gatt_characteristics(&mut adapter, &service_id),
};
if characteristics.is_empty() {
send_error!(sender, "No characteristic found");
}
let mut characteristics_vec: Vec<BluetoothObjectMsg> = vec!();
for characteristic in characteristics {
let properties = self.get_characteristic_properties(&characteristic);
characteristics_vec.push(BluetoothObjectMsg::BluetoothCharacteristic {
uuid: characteristic.get_uuid().unwrap_or("".to_owned()),
instance_id: characteristic.get_object_path(),
broadcast: properties[0],
read: properties[1],
write_without_response: properties[2],
write: properties[3],
notify: properties[4],
indicate: properties[5],
authenticated_signed_writes: properties[6],
reliable_write: properties[7],
writable_auxiliaries: properties[8],
});
}
if characteristics_vec.is_empty() {
send_error!(sender, "No characteristic found");
}
let message = BluetoothObjectMsg::BluetoothCharacteristics { characteristics_vec: characteristics_vec };
sender.send(message).unwrap();
}
pub fn get_descriptor(&mut self,
characteristic_id: String,
uuid: String,
sender: IpcSender<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, "No adapter found"), None => send_error!(sender, "No adapter found"),
}; };
let descriptors = self.get_gatt_descriptors(&mut adapter, &characteristic_id); let descriptor = match self.get_gatt_descriptor_by_uuid(&mut adapter, &characteristic_id, &uuid) {
if descriptors.is_empty() { Some(d) => d,
send_error!(sender, "No descriptor found"); None => send_error!(sender, "No descriptor found"),
} };
let descriptor = &descriptors[0];
let message = BluetoothObjectMsg::BluetoothDescriptor { let message = BluetoothObjectMsg::BluetoothDescriptor {
uuid: descriptor.get_uuid().unwrap_or("".to_owned()), uuid: descriptor.get_uuid().unwrap_or("".to_owned()),
instance_id: descriptor.get_object_path(), instance_id: descriptor.get_object_path(),
@ -469,6 +583,35 @@ impl BluetoothManager {
sender.send(message).unwrap(); sender.send(message).unwrap();
} }
pub fn get_descriptors(&mut self,
characteristic_id: String,
uuid: Option<String>,
sender: IpcSender<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() {
Some(a) => a,
None => send_error!(sender, "No adapter found"),
};
let descriptors = match uuid {
Some(id) => self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &id),
None => self.get_gatt_descriptors(&mut adapter, &characteristic_id),
};
if descriptors.is_empty() {
send_error!(sender, "No descriptor found");
}
let mut descriptors_vec: Vec<BluetoothObjectMsg> = vec!();
for descriptor in descriptors {
descriptors_vec.push(BluetoothObjectMsg::BluetoothDescriptor {
uuid: descriptor.get_uuid().unwrap_or("".to_owned()),
instance_id: descriptor.get_object_path(),
});
}
if descriptors_vec.is_empty() {
send_error!(sender, "No descriptor found");
}
let message = BluetoothObjectMsg::BluetoothDescriptors { descriptors_vec: descriptors_vec };
sender.send(message).unwrap();
}
pub fn read_value(&mut self, id: String, sender: IpcSender<BluetoothObjectMsg>) { pub fn read_value(&mut self, id: String, sender: IpcSender<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_adapter() {
Some(a) => a, Some(a) => a,

View file

@ -8,9 +8,12 @@ pub enum BluetoothMethodMsg {
RequestDevice(IpcSender<BluetoothObjectMsg>), RequestDevice(IpcSender<BluetoothObjectMsg>),
GATTServerConnect(String, IpcSender<BluetoothObjectMsg>), GATTServerConnect(String, IpcSender<BluetoothObjectMsg>),
GATTServerDisconnect(String, IpcSender<BluetoothObjectMsg>), GATTServerDisconnect(String, IpcSender<BluetoothObjectMsg>),
GetPrimaryService(String, IpcSender<BluetoothObjectMsg>), GetPrimaryService(String, String, IpcSender<BluetoothObjectMsg>),
GetCharacteristic(String, IpcSender<BluetoothObjectMsg>), GetPrimaryServices(String, Option<String>, IpcSender<BluetoothObjectMsg>),
GetDescriptor(String, IpcSender<BluetoothObjectMsg>), GetCharacteristic(String, String, IpcSender<BluetoothObjectMsg>),
GetCharacteristics(String, Option<String>, IpcSender<BluetoothObjectMsg>),
GetDescriptor(String, String, IpcSender<BluetoothObjectMsg>),
GetDescriptors(String, Option<String>, IpcSender<BluetoothObjectMsg>),
ReadValue(String, IpcSender<BluetoothObjectMsg>), ReadValue(String, IpcSender<BluetoothObjectMsg>),
WriteValue(String, Vec<u8>, IpcSender<BluetoothObjectMsg>), WriteValue(String, Vec<u8>, IpcSender<BluetoothObjectMsg>),
Exit, Exit,
@ -40,6 +43,9 @@ pub enum BluetoothObjectMsg {
is_primary: bool, is_primary: bool,
instance_id: String instance_id: String
}, },
BluetoothServices {
services_vec: Vec<BluetoothObjectMsg>
},
BluetoothCharacteristic { BluetoothCharacteristic {
// Characteristic // Characteristic
uuid: String, uuid: String,
@ -55,10 +61,16 @@ pub enum BluetoothObjectMsg {
reliable_write: bool, reliable_write: bool,
writable_auxiliaries: bool writable_auxiliaries: bool
}, },
BluetoothCharacteristics {
characteristics_vec: Vec<BluetoothObjectMsg>
},
BluetoothDescriptor { BluetoothDescriptor {
uuid: String, uuid: String,
instance_id: String instance_id: String
}, },
BluetoothDescriptors {
descriptors_vec: Vec<BluetoothObjectMsg>,
},
BluetoothReadValue { BluetoothReadValue {
value: Vec<u8> value: Vec<u8>
}, },

View file

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

View file

@ -5,11 +5,13 @@
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods; use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong;
use dom::bindings::global::GlobalRef; use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root}; use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothdevice::BluetoothDevice; use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothremotegattservice::BluetoothRemoteGATTService; use dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
use dom::bluetoothuuid::BluetoothUUID;
use ipc_channel::ipc::{self, IpcSender}; use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg}; use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg};
use std::cell::Cell; use std::cell::Cell;
@ -101,16 +103,23 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice // 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(); let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send( 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(); let service = receiver.recv().unwrap();
match service { match service {
BluetoothObjectMsg::BluetoothService { BluetoothObjectMsg::BluetoothService {
uuid, uuid,
is_primary, is_primary,
instance_id instance_id,
} => { } => {
Some(BluetoothRemoteGATTService::new(self.global().r(), Some(BluetoothRemoteGATTService::new(self.global().r(),
&self.device.get(), &self.device.get(),
@ -124,7 +133,54 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
println!("{}", error); println!("{}", error);
None 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;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong;
use dom::bindings::global::GlobalRef; use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root}; use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties; use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties;
use dom::bluetoothdevice::BluetoothDevice; use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic; use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic;
use dom::bluetoothuuid::BluetoothUUID;
use ipc_channel::ipc::{self, IpcSender}; use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg}; use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg};
use util::str::DOMString; use util::str::DOMString;
@ -81,10 +83,19 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristic // 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(); let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send( 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(); let characteristic = receiver.recv().unwrap();
match characteristic { match characteristic {
BluetoothObjectMsg::BluetoothCharacteristic { BluetoothObjectMsg::BluetoothCharacteristic {
@ -122,7 +133,74 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
println!("{}", error); println!("{}", error);
None 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 DOMString uuid;
readonly attribute BluetoothCharacteristicProperties properties; readonly attribute BluetoothCharacteristicProperties properties;
readonly attribute ByteString? value; 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<BluetoothRemoteGATTDescriptor> getDescriptor(BluetoothDescriptorUUID descriptor);
//Promise<sequence<BluetoothRemoteGATTDescriptor>> //Promise<sequence<BluetoothRemoteGATTDescriptor>>
//getDescriptors(optional BluetoothDescriptorUUID descriptor); //getDescriptors(optional BluetoothDescriptorUUID descriptor);

View file

@ -10,7 +10,8 @@ interface BluetoothRemoteGATTServer {
readonly attribute boolean connected; readonly attribute boolean connected;
BluetoothRemoteGATTServer connect(); BluetoothRemoteGATTServer connect();
void disconnect(); 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<BluetoothRemoteGATTService> getPrimaryService(BluetoothServiceUUID service);
//Promise<sequence<BluetoothRemoteGATTService>>getPrimaryServices(optional BluetoothServiceUUID service); //Promise<sequence<BluetoothRemoteGATTService>>getPrimaryServices(optional BluetoothServiceUUID service);
//Promise<BluetoothRemoteGATTServer> connect(); //Promise<BluetoothRemoteGATTServer> connect();

View file

@ -9,7 +9,9 @@ interface BluetoothRemoteGATTService {
readonly attribute BluetoothDevice device; readonly attribute BluetoothDevice device;
readonly attribute DOMString uuid; readonly attribute DOMString uuid;
readonly attribute boolean isPrimary; 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<BluetoothRemoteGATTCharacteristic>getCharacteristic(BluetoothCharacteristicUUID characteristic);
//Promise<sequence<BluetoothRemoteGATTCharacteristic>> //Promise<sequence<BluetoothRemoteGATTCharacteristic>>
//getCharacteristics(optional BluetoothCharacteristicUUID characteristic); //getCharacteristics(optional BluetoothCharacteristicUUID characteristic);