Style fix

This commit is contained in:
fokinv 2016-04-15 15:32:13 +02:00 committed by Attila Dusnoki
parent 27ad1437a1
commit f47f8d1a5c
7 changed files with 220 additions and 200 deletions

View file

@ -15,6 +15,14 @@ use std::collections::HashMap;
use std::string::String; use std::string::String;
use util::thread::spawn_named; use util::thread::spawn_named;
const ADAPTER_ERROR: &'static str = "No adapter found";
const DEVICE_ERROR: &'static str = "No device found";
const DEVICE_MATCH_ERROR: &'static str = "No device found, that matches the given options";
const PRIMARY_SERVICE_ERROR: &'static str = "No primary service found";
const CHARACTERISTIC_ERROR: &'static str = "No characteristic found";
const DESCRIPTOR_ERROR: &'static str = "No descriptor found";
const VALUE_ERROR: &'static str = "No characteristic or descriptor found with that id";
macro_rules! send_error( macro_rules! send_error(
($sender:expr, $error:expr) => ( ($sender:expr, $error:expr) => (
return $sender.send(BluetoothObjectMsg::Error { error: String::from($error) }).unwrap(); return $sender.send(BluetoothObjectMsg::Error { error: String::from($error) }).unwrap();
@ -79,40 +87,40 @@ impl BluetoothManager {
match self.receiver.recv().unwrap() { match self.receiver.recv().unwrap() {
BluetoothMethodMsg::RequestDevice(options, sender) => { BluetoothMethodMsg::RequestDevice(options, sender) => {
self.request_device(options, sender) self.request_device(options, sender)
} },
BluetoothMethodMsg::GATTServerConnect(device_id, sender) => { BluetoothMethodMsg::GATTServerConnect(device_id, sender) => {
self.gatt_server_connect(device_id, sender) self.gatt_server_connect(device_id, sender)
} },
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, uuid, sender) => { BluetoothMethodMsg::GetPrimaryService(device_id, uuid, sender) => {
self.get_primary_service(device_id, uuid, sender) self.get_primary_service(device_id, uuid, sender)
} },
BluetoothMethodMsg::GetPrimaryServices(device_id, uuid, sender) => { BluetoothMethodMsg::GetPrimaryServices(device_id, uuid, sender) => {
self.get_primary_services(device_id, uuid, sender) self.get_primary_services(device_id, uuid, sender)
} },
BluetoothMethodMsg::GetCharacteristic(service_id, uuid, sender) => { BluetoothMethodMsg::GetCharacteristic(service_id, uuid, sender) => {
self.get_characteristic(service_id, uuid, sender) self.get_characteristic(service_id, uuid, sender)
} },
BluetoothMethodMsg::GetCharacteristics(service_id, uuid, sender) => { BluetoothMethodMsg::GetCharacteristics(service_id, uuid, sender) => {
self.get_characteristics(service_id, uuid, sender) self.get_characteristics(service_id, uuid, sender)
} },
BluetoothMethodMsg::GetDescriptor(characteristic_id, uuid, sender) => { BluetoothMethodMsg::GetDescriptor(characteristic_id, uuid, sender) => {
self.get_descriptor(characteristic_id, uuid, sender) self.get_descriptor(characteristic_id, uuid, sender)
} },
BluetoothMethodMsg::GetDescriptors(characteristic_id, uuid, sender) => { BluetoothMethodMsg::GetDescriptors(characteristic_id, uuid, sender) => {
self.get_descriptors(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)
} },
BluetoothMethodMsg::WriteValue(id, value, sender) => { BluetoothMethodMsg::WriteValue(id, value, sender) => {
self.write_value(id, value, sender) self.write_value(id, value, sender)
} },
BluetoothMethodMsg::Exit => { BluetoothMethodMsg::Exit => {
break break
} },
} }
} }
} }
@ -132,7 +140,9 @@ impl BluetoothManager {
fn get_devices(&mut self, adapter: &mut BluetoothAdapter) -> Vec<BluetoothDevice> { fn get_devices(&mut self, adapter: &mut BluetoothAdapter) -> Vec<BluetoothDevice> {
let devices = adapter.get_devices().unwrap_or(vec!()); let devices = adapter.get_devices().unwrap_or(vec!());
for device in &devices { for device in &devices {
self.cached_devices.insert(device.get_address().unwrap_or("".to_owned()), device.clone()); if let Ok(address) = device.get_address() {
self.cached_devices.insert(address, device.clone());
}
} }
devices devices
} }
@ -159,10 +169,7 @@ impl BluetoothManager {
services services
} }
fn get_gatt_service(&mut self, fn get_gatt_service(&mut self, adapter: &mut BluetoothAdapter, service_id: &str) -> Option<&BluetoothGATTService> {
adapter: &mut BluetoothAdapter,
service_id: &str)
-> Option<&BluetoothGATTService> {
check_cache!(self.cached_services, service_id); check_cache!(self.cached_services, service_id);
let device_id = match self.service_to_device.get_mut(service_id) { let device_id = match self.service_to_device.get_mut(service_id) {
Some(d) => d.clone(), Some(d) => d.clone(),
@ -182,8 +189,10 @@ impl BluetoothManager {
let mut services_vec: Vec<BluetoothGATTService> = vec!(); let mut services_vec: Vec<BluetoothGATTService> = vec!();
let services = self.get_gatt_services(adapter, device_id); let services = self.get_gatt_services(adapter, device_id);
for service in services { for service in services {
if service.get_uuid().unwrap_or("".to_owned()) == service_uuid { if let Ok(uuid) = service.get_uuid() {
services_vec.push(service.clone()); if uuid == service_uuid {
services_vec.push(service.clone());
}
} }
} }
services_vec services_vec
@ -230,8 +239,10 @@ impl BluetoothManager {
let mut characteristics_vec: Vec<BluetoothGATTCharacteristic> = vec!(); let mut characteristics_vec: Vec<BluetoothGATTCharacteristic> = vec!();
let characteristics = self.get_gatt_characteristics(adapter, service_id); let characteristics = self.get_gatt_characteristics(adapter, service_id);
for characteristic in characteristics { for characteristic in characteristics {
if characteristic.get_uuid().unwrap_or("".to_owned()) == characteristic_uuid { if let Ok(uuid) = characteristic.get_uuid() {
characteristics_vec.push(characteristic.clone()); if uuid == characteristic_uuid {
characteristics_vec.push(characteristic.clone());
}
} }
} }
characteristics_vec characteristics_vec
@ -298,8 +309,10 @@ impl BluetoothManager {
let mut descriptors_vec: Vec<BluetoothGATTDescriptor> = vec!(); let mut descriptors_vec: Vec<BluetoothGATTDescriptor> = vec!();
let descriptors = self.get_gatt_descriptors(adapter, characteristic_id); let descriptors = self.get_gatt_descriptors(adapter, characteristic_id);
for descriptor in descriptors { for descriptor in descriptors {
if descriptor.get_uuid().unwrap_or("".to_owned()) == descriptor_uuid { if let Ok(uuid) = descriptor.get_uuid() {
descriptors_vec.push(descriptor.clone()); if uuid == descriptor_uuid {
descriptors_vec.push(descriptor.clone());
}
} }
} }
descriptors_vec descriptors_vec
@ -307,16 +320,14 @@ impl BluetoothManager {
// Methods // Methods
fn request_device(&mut self, fn request_device(&mut self, options: RequestDeviceoptions, sender: IpcSender<BluetoothObjectMsg>) {
options: RequestDeviceoptions,
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, ADAPTER_ERROR),
}; };
let devices = self.get_devices(&mut adapter); let devices = self.get_devices(&mut adapter);
if devices.is_empty() { if devices.is_empty() {
send_error!(sender, "No device found"); send_error!(sender, DEVICE_ERROR);
} }
let matched_devices: Vec<BluetoothDevice> = devices.into_iter() let matched_devices: Vec<BluetoothDevice> = devices.into_iter()
@ -345,13 +356,13 @@ impl BluetoothManager {
return sender.send(message).unwrap(); return sender.send(message).unwrap();
} }
} }
send_error!(sender, "No device found, that matches the given options"); send_error!(sender, DEVICE_MATCH_ERROR);
} }
pub fn gatt_server_connect(&mut self, device_id: String, sender: IpcSender<BluetoothObjectMsg>) { pub fn gatt_server_connect(&mut self, device_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,
None => send_error!(sender, "No adapter found"), None => send_error!(sender, ADAPTER_ERROR),
}; };
let connected = match self.get_device(&mut adapter, &device_id) { let connected = match self.get_device(&mut adapter, &device_id) {
@ -361,8 +372,8 @@ impl BluetoothManager {
} else { } else {
!d.connect().is_err() !d.connect().is_err()
} }
} },
None => send_error!(sender, "No device found"), None => send_error!(sender, DEVICE_ERROR),
}; };
let message = BluetoothObjectMsg::BluetoothServer { let message = BluetoothObjectMsg::BluetoothServer {
@ -374,7 +385,7 @@ impl BluetoothManager {
pub fn gatt_server_disconnect(&mut self, device_id: String, sender: IpcSender<BluetoothObjectMsg>) { pub fn gatt_server_disconnect(&mut self, device_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,
None => send_error!(sender, "No adapter found"), None => send_error!(sender, ADAPTER_ERROR),
}; };
let connected = match self.get_device(&mut adapter, &device_id) { let connected = match self.get_device(&mut adapter, &device_id) {
@ -384,8 +395,8 @@ impl BluetoothManager {
} else { } else {
false false
} }
} },
None => send_error!(sender, "No device found"), None => send_error!(sender, DEVICE_ERROR),
}; };
let message = BluetoothObjectMsg::BluetoothServer { let message = BluetoothObjectMsg::BluetoothServer {
@ -397,11 +408,11 @@ impl BluetoothManager {
pub fn get_primary_service(&mut self, device_id: String, uuid: 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, ADAPTER_ERROR),
}; };
let services = self.get_gatt_services_by_uuid(&mut adapter, &device_id, &uuid); let services = self.get_gatt_services_by_uuid(&mut adapter, &device_id, &uuid);
if services.is_empty() { if services.is_empty() {
send_error!(sender, "No primary service found"); send_error!(sender, PRIMARY_SERVICE_ERROR);
} }
for service in services { for service in services {
if service.is_primary().unwrap_or(false) { if service.is_primary().unwrap_or(false) {
@ -415,7 +426,7 @@ impl BluetoothManager {
} }
} }
} }
send_error!(sender, "No primary service found"); send_error!(sender, PRIMARY_SERVICE_ERROR);
} }
pub fn get_primary_services(&mut self, pub fn get_primary_services(&mut self,
@ -424,27 +435,29 @@ impl BluetoothManager {
sender: IpcSender<BluetoothObjectMsg>) { 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, ADAPTER_ERROR),
}; };
let services: Vec<BluetoothGATTService> = match uuid { let services: Vec<BluetoothGATTService> = match uuid {
Some(id) => self.get_gatt_services_by_uuid(&mut adapter, &device_id, &id), Some(id) => self.get_gatt_services_by_uuid(&mut adapter, &device_id, &id),
None => self.get_gatt_services(&mut adapter, &device_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, PRIMARY_SERVICE_ERROR);
} }
let mut services_vec: Vec<BluetoothObjectMsg> = vec!(); 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) {
services_vec.push(BluetoothObjectMsg::BluetoothService { if let Ok(uuid) = service.get_uuid() {
uuid: service.get_uuid().unwrap_or("".to_owned()), services_vec.push(BluetoothObjectMsg::BluetoothService {
is_primary: true, uuid: uuid,
instance_id: service.get_object_path(), is_primary: true,
}); instance_id: service.get_object_path(),
});
}
} }
} }
if services_vec.is_empty() { if services_vec.is_empty() {
send_error!(sender, "No service found"); send_error!(sender, PRIMARY_SERVICE_ERROR);
} }
let message = BluetoothObjectMsg::BluetoothServices { services_vec: services_vec }; let message = BluetoothObjectMsg::BluetoothServices { services_vec: services_vec };
sender.send(message).unwrap(); sender.send(message).unwrap();
@ -453,11 +466,11 @@ impl BluetoothManager {
pub fn get_characteristic(&mut self, service_id: String, uuid: 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, ADAPTER_ERROR),
}; };
let characteristics = self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &uuid); let characteristics = self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &uuid);
if characteristics.is_empty() { if characteristics.is_empty() {
send_error!(sender, "No characteristic found"); send_error!(sender, CHARACTERISTIC_ERROR);
} }
for characteristic in characteristics { for characteristic in characteristics {
if let Ok(uuid) = characteristic.get_uuid() { if let Ok(uuid) = characteristic.get_uuid() {
@ -478,7 +491,7 @@ impl BluetoothManager {
return sender.send(message).unwrap(); return sender.send(message).unwrap();
} }
} }
send_error!(sender, "No characteristic found"); send_error!(sender, CHARACTERISTIC_ERROR);
} }
pub fn get_characteristics(&mut self, pub fn get_characteristics(&mut self,
@ -487,50 +500,49 @@ impl BluetoothManager {
sender: IpcSender<BluetoothObjectMsg>) { 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, ADAPTER_ERROR),
}; };
let characteristics = match uuid { let characteristics = match uuid {
Some(id) => self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &id), Some(id) => self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &id),
None => self.get_gatt_characteristics(&mut adapter, &service_id), None => self.get_gatt_characteristics(&mut adapter, &service_id),
}; };
if characteristics.is_empty() { if characteristics.is_empty() {
send_error!(sender, "No characteristic found"); send_error!(sender, CHARACTERISTIC_ERROR);
} }
let mut characteristics_vec: Vec<BluetoothObjectMsg> = vec!(); let mut characteristics_vec: Vec<BluetoothObjectMsg> = vec!();
for characteristic in characteristics { for characteristic in characteristics {
let properties = self.get_characteristic_properties(&characteristic); if let Ok(uuid) = characteristic.get_uuid() {
characteristics_vec.push(BluetoothObjectMsg::BluetoothCharacteristic { let properties = self.get_characteristic_properties(&characteristic);
uuid: characteristic.get_uuid().unwrap_or("".to_owned()), characteristics_vec.push(BluetoothObjectMsg::BluetoothCharacteristic {
instance_id: characteristic.get_object_path(), uuid: uuid,
broadcast: properties[0], instance_id: characteristic.get_object_path(),
read: properties[1], broadcast: properties[0],
write_without_response: properties[2], read: properties[1],
write: properties[3], write_without_response: properties[2],
notify: properties[4], write: properties[3],
indicate: properties[5], notify: properties[4],
authenticated_signed_writes: properties[6], indicate: properties[5],
reliable_write: properties[7], authenticated_signed_writes: properties[6],
writable_auxiliaries: properties[8], reliable_write: properties[7],
}); writable_auxiliaries: properties[8],
});
}
} }
if characteristics_vec.is_empty() { if characteristics_vec.is_empty() {
send_error!(sender, "No characteristic found"); send_error!(sender, CHARACTERISTIC_ERROR);
} }
let message = BluetoothObjectMsg::BluetoothCharacteristics { characteristics_vec: characteristics_vec }; let message = BluetoothObjectMsg::BluetoothCharacteristics { characteristics_vec: characteristics_vec };
sender.send(message).unwrap(); sender.send(message).unwrap();
} }
pub fn get_descriptor(&mut self, pub fn get_descriptor(&mut self, characteristic_id: String, uuid: String, sender: IpcSender<BluetoothObjectMsg>) {
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, ADAPTER_ERROR),
}; };
let descriptors = self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &uuid); let descriptors = self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &uuid);
if descriptors.is_empty() { if descriptors.is_empty() {
send_error!(sender, "No descriptor found"); send_error!(sender, DESCRIPTOR_ERROR);
} }
for descriptor in descriptors { for descriptor in descriptors {
if let Ok(uuid) = descriptor.get_uuid() { if let Ok(uuid) = descriptor.get_uuid() {
@ -541,7 +553,7 @@ impl BluetoothManager {
return sender.send(message).unwrap(); return sender.send(message).unwrap();
} }
} }
send_error!(sender, "No descriptor found"); send_error!(sender, DESCRIPTOR_ERROR);
} }
pub fn get_descriptors(&mut self, pub fn get_descriptors(&mut self,
@ -550,24 +562,26 @@ impl BluetoothManager {
sender: IpcSender<BluetoothObjectMsg>) { 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, ADAPTER_ERROR),
}; };
let descriptors = match uuid { let descriptors = match uuid {
Some(id) => self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &id), Some(id) => self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &id),
None => self.get_gatt_descriptors(&mut adapter, &characteristic_id), None => self.get_gatt_descriptors(&mut adapter, &characteristic_id),
}; };
if descriptors.is_empty() { if descriptors.is_empty() {
send_error!(sender, "No descriptor found"); send_error!(sender, DESCRIPTOR_ERROR);
} }
let mut descriptors_vec: Vec<BluetoothObjectMsg> = vec!(); let mut descriptors_vec: Vec<BluetoothObjectMsg> = vec!();
for descriptor in descriptors { for descriptor in descriptors {
descriptors_vec.push(BluetoothObjectMsg::BluetoothDescriptor { if let Ok(uuid) = descriptor.get_uuid() {
uuid: descriptor.get_uuid().unwrap_or("".to_owned()), descriptors_vec.push(BluetoothObjectMsg::BluetoothDescriptor {
instance_id: descriptor.get_object_path(), uuid: uuid,
}); instance_id: descriptor.get_object_path(),
});
}
} }
if descriptors_vec.is_empty() { if descriptors_vec.is_empty() {
send_error!(sender, "No descriptor found"); send_error!(sender, DESCRIPTOR_ERROR);
} }
let message = BluetoothObjectMsg::BluetoothDescriptors { descriptors_vec: descriptors_vec }; let message = BluetoothObjectMsg::BluetoothDescriptors { descriptors_vec: descriptors_vec };
sender.send(message).unwrap(); sender.send(message).unwrap();
@ -576,7 +590,7 @@ impl BluetoothManager {
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,
None => send_error!(sender, "No adapter found"), None => send_error!(sender, ADAPTER_ERROR),
}; };
let mut value = match self.get_gatt_characteristic(&mut adapter, &id) { let mut value = match self.get_gatt_characteristic(&mut adapter, &id) {
Some(c) => Some(c.read_value().unwrap_or(vec!())), Some(c) => Some(c.read_value().unwrap_or(vec!())),
@ -591,7 +605,7 @@ impl BluetoothManager {
let message = match value { let message = match value {
Some(v) => BluetoothObjectMsg::BluetoothReadValue { value: v }, Some(v) => BluetoothObjectMsg::BluetoothReadValue { value: v },
None => send_error!(sender, "No characteristic or descriptor found with that id"), None => send_error!(sender, VALUE_ERROR),
}; };
sender.send(message).unwrap(); sender.send(message).unwrap();
@ -600,7 +614,7 @@ impl BluetoothManager {
pub fn write_value(&mut self, id: String, value: Vec<u8>, sender: IpcSender<BluetoothObjectMsg>) { pub fn write_value(&mut self, id: String, value: Vec<u8>, 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, ADAPTER_ERROR),
}; };
let mut result = match self.get_gatt_characteristic(&mut adapter, &id) { let mut result = match self.get_gatt_characteristic(&mut adapter, &id) {
Some(c) => Some(c.write_value(value.clone())), Some(c) => Some(c.write_value(value.clone())),
@ -618,7 +632,7 @@ impl BluetoothManager {
Ok(_) => BluetoothObjectMsg::BluetoothWriteValue, Ok(_) => BluetoothObjectMsg::BluetoothWriteValue,
Err(e) => send_error!(sender, e.to_string()), Err(e) => send_error!(sender, e.to_string()),
}, },
None => send_error!(sender, "No characteristic or descriptor found with that id"), None => send_error!(sender, VALUE_ERROR),
}; };
sender.send(message).unwrap(); sender.send(message).unwrap();

View file

@ -23,13 +23,13 @@ use util::str::DOMString;
// A device name can never be longer than 29 bytes. An adv packet is at most // A device name can never be longer than 29 bytes. An adv packet is at most
// 31 bytes long. The length and identifier of the length field take 2 bytes. // 31 bytes long. The length and identifier of the length field take 2 bytes.
// That least 29 bytes for the name.
const MAX_FILTER_NAME_LENGTH: usize = 29;
// 248 is the maximum number of UTF-8 code units in a Bluetooth Device Name.
const MAX_DEVICE_NAME_LENGTH: usize = 248;
const FILTER_EMPTY_ERROR: &'static str = "'filters' member must be non - empty to find any devices."; const FILTER_EMPTY_ERROR: &'static str = "'filters' member must be non - empty to find any devices.";
const FILTER_ERROR: &'static str = "A filter must restrict the devices in some way."; const FILTER_ERROR: &'static str = "A filter must restrict the devices in some way.";
const FILTER_NAME_TOO_LONG_ERROR: &'static str = "A 'name' or 'namePrefix' can't be longer then 29 bytes."; const FILTER_NAME_TOO_LONG_ERROR: &'static str = "A 'name' or 'namePrefix' can't be longer then 29 bytes.";
// 248 is the maximum number of UTF-8 code units in a Bluetooth Device Name.
const MAX_DEVICE_NAME_LENGTH: usize = 248;
// That least 29 bytes for the name.
const MAX_FILTER_NAME_LENGTH: usize = 29;
const NAME_PREFIX_ERROR: &'static str = "'namePrefix', if present, must be non - empty."; const NAME_PREFIX_ERROR: &'static str = "'namePrefix', if present, must be non - empty.";
const NAME_TOO_LONG_ERROR: &'static str = "A device name can't be longer than 248 bytes."; const NAME_TOO_LONG_ERROR: &'static str = "A device name can't be longer than 248 bytes.";
const SERVICE_ERROR: &'static str = "'services', if present, must contain at least one service."; const SERVICE_ERROR: &'static str = "'services', if present, must contain at least one service.";
@ -79,11 +79,8 @@ impl Clone for RequestDeviceOptions {
} }
} }
fn canonicalize_filter(filter: &BluetoothScanFilter, global: GlobalRef) fn canonicalize_filter(filter: &BluetoothScanFilter, global: GlobalRef) -> Fallible<BluetoothScanfilter> {
-> Fallible<BluetoothScanfilter> { if !(filter.services.is_some() || filter.name.is_some() || filter.namePrefix.is_some()) {
if !(filter.services.is_some() ||
filter.name.is_some() ||
filter.namePrefix.is_some()) {
return Err(Type(FILTER_ERROR.to_owned())); return Err(Type(FILTER_ERROR.to_owned()));
} }
@ -129,8 +126,9 @@ fn canonicalize_filter(filter: &BluetoothScanFilter, global: GlobalRef)
Ok(BluetoothScanfilter::new(name, name_prefix, services_vec)) Ok(BluetoothScanfilter::new(name, name_prefix, services_vec))
} }
fn convert_request_device_options(options: &RequestDeviceOptions, global: GlobalRef) fn convert_request_device_options(options: &RequestDeviceOptions,
-> Fallible<RequestDeviceoptions> { global: GlobalRef)
-> Fallible<RequestDeviceoptions> {
if options.filters.is_empty() { if options.filters.is_empty() {
return Err(Type(FILTER_EMPTY_ERROR.to_owned())); return Err(Type(FILTER_EMPTY_ERROR.to_owned()));
} }
@ -160,15 +158,12 @@ fn convert_request_device_options(options: &RequestDeviceOptions, global: Global
impl BluetoothMethods for Bluetooth { impl BluetoothMethods for Bluetooth {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice
fn RequestDevice(&self, fn RequestDevice(&self, option: &RequestDeviceOptions) -> Fallible<Root<BluetoothDevice>> {
option: &RequestDeviceOptions)
-> Fallible<Root<BluetoothDevice>> {
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
match convert_request_device_options(option, self.global().r()) { match convert_request_device_options(option, self.global().r()) {
Ok(option) => { Ok(option) => {
self.get_bluetooth_thread() self.get_bluetooth_thread().send(
.send(BluetoothMethodMsg::RequestDevice(option, sender)) BluetoothMethodMsg::RequestDevice(option, sender)).unwrap();
.unwrap();
let device = receiver.recv().unwrap(); let device = receiver.recv().unwrap();
match device { match device {
BluetoothObjectMsg::BluetoothDevice { BluetoothObjectMsg::BluetoothDevice {
@ -211,7 +206,9 @@ impl BluetoothMethods for Bluetooth {
}, },
BluetoothObjectMsg::Error { BluetoothObjectMsg::Error {
error error
} => return Err(Type(error)), } => {
Err(Type(error))
},
_ => unreachable!() _ => unreachable!()
} }
}, },

View file

@ -11,7 +11,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::Bluetoot
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong; use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong;
use dom::bindings::error::Error::{Network, Type}; use dom::bindings::error::Error::{Network, Type};
use dom::bindings::error::Fallible; use dom::bindings::error::{Fallible, ErrorResult};
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};
@ -61,8 +61,8 @@ impl BluetoothRemoteGATTCharacteristic {
uuid, uuid,
properties, properties,
instanceID), instanceID),
global, global,
BluetoothRemoteGATTCharacteristicBinding::Wrap) BluetoothRemoteGATTCharacteristicBinding::Wrap)
} }
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> { fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> {
@ -94,9 +94,7 @@ 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, fn GetDescriptor(&self, descriptor: StringOrUnsignedLong) -> Fallible<Root<BluetoothRemoteGATTDescriptor>> {
descriptor: StringOrUnsignedLong)
-> Fallible<Root<BluetoothRemoteGATTDescriptor>> {
let uuid: String = match BluetoothUUID::GetDescriptor(self.global().r(), descriptor.clone()) { let uuid: String = match BluetoothUUID::GetDescriptor(self.global().r(), descriptor.clone()) {
Ok(domstring) => domstring.to_string(), Ok(domstring) => domstring.to_string(),
Err(error) => return Err(error), Err(error) => return Err(error),
@ -117,7 +115,9 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}, },
BluetoothObjectMsg::Error { BluetoothObjectMsg::Error {
error error
} => Err(Type(error)), } => {
Err(Type(error))
},
_ => unreachable!() _ => unreachable!()
} }
} }
@ -126,12 +126,12 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
fn GetDescriptors(&self, fn GetDescriptors(&self,
descriptor: Option<StringOrUnsignedLong>) descriptor: Option<StringOrUnsignedLong>)
-> Fallible<Vec<Root<BluetoothRemoteGATTDescriptor>>> { -> Fallible<Vec<Root<BluetoothRemoteGATTDescriptor>>> {
let uuid: Option<String> = match descriptor { let mut uuid: Option<String> = None;
Some(d) => match BluetoothUUID::GetDescriptor(self.global().r(), d.clone()) { if let Some(d)= descriptor {
Ok(domstring) => Some(domstring.to_string()), match BluetoothUUID::GetCharacteristic(self.global().r(), d.clone()) {
Ok(domstring) => uuid = Some(domstring.to_string()),
Err(error) => return Err(error), Err(error) => return Err(error),
}, }
None => None,
}; };
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
let mut descriptors: Vec<Root<BluetoothRemoteGATTDescriptor>> = vec!(); let mut descriptors: Vec<Root<BluetoothRemoteGATTDescriptor>> = vec!();
@ -151,7 +151,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
descriptors.push(BluetoothRemoteGATTDescriptor::new(self.global().r(), descriptors.push(BluetoothRemoteGATTDescriptor::new(self.global().r(),
&self, &self,
DOMString::from(uuid), DOMString::from(uuid),
instance_id)) instance_id));
}, },
_ => unreachable!(), _ => unreachable!(),
} }
@ -160,7 +160,9 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}, },
BluetoothObjectMsg::Error { BluetoothObjectMsg::Error {
error error
} => Err(Type(error)), } => {
Err(Type(error))
},
_ => unreachable!(), _ => unreachable!(),
} }
} }
@ -175,8 +177,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
if !self.Service().Device().Gatt().Connected() { if !self.Service().Device().Gatt().Connected() {
Err(Network) Err(Network)
} } else {
else {
self.get_bluetooth_thread().send( self.get_bluetooth_thread().send(
BluetoothMethodMsg::ReadValue(self.get_instance_id(), sender)).unwrap(); BluetoothMethodMsg::ReadValue(self.get_instance_id(), sender)).unwrap();
let result = receiver.recv().unwrap(); let result = receiver.recv().unwrap();
@ -188,7 +189,9 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}, },
BluetoothObjectMsg::Error { BluetoothObjectMsg::Error {
error error
} => return Err(Type(error)), } => {
return Err(Type(error))
},
_ => unreachable!() _ => unreachable!()
}; };
*self.value.borrow_mut() = value; *self.value.borrow_mut() = value;
@ -197,7 +200,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue
fn WriteValue(&self, value: Vec<u8>) -> Fallible<()> { fn WriteValue(&self, value: Vec<u8>) -> ErrorResult {
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send( self.get_bluetooth_thread().send(
BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap(); BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap();
@ -206,7 +209,9 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
BluetoothObjectMsg::BluetoothWriteValue => Ok(()), BluetoothObjectMsg::BluetoothWriteValue => Ok(()),
BluetoothObjectMsg::Error { BluetoothObjectMsg::Error {
error error
} => Err(Type(error)), } => {
Err(Type(error))
},
_ => unreachable!() _ => unreachable!()
} }
} }

View file

@ -11,7 +11,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTDescriptorBinding::Blue
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::{Type, Network}; use dom::bindings::error::Error::{Type, Network};
use dom::bindings::error::Fallible; use dom::bindings::error::{Fallible, ErrorResult};
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};
@ -90,8 +90,7 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
if !self.Characteristic().Service().Device().Gatt().Connected() { if !self.Characteristic().Service().Device().Gatt().Connected() {
Err(Network) Err(Network)
} } else {
else {
self.get_bluetooth_thread().send( self.get_bluetooth_thread().send(
BluetoothMethodMsg::ReadValue(self.get_instance_id(), sender)).unwrap(); BluetoothMethodMsg::ReadValue(self.get_instance_id(), sender)).unwrap();
let result = receiver.recv().unwrap(); let result = receiver.recv().unwrap();
@ -103,7 +102,9 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
}, },
BluetoothObjectMsg::Error { BluetoothObjectMsg::Error {
error error
} => return Err(Type(error)), } => {
return Err(Type(error))
},
_ => unreachable!() _ => unreachable!()
}; };
*self.value.borrow_mut() = value; *self.value.borrow_mut() = value;
@ -112,7 +113,7 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue
fn WriteValue(&self, value: Vec<u8>) -> Fallible<()> { fn WriteValue(&self, value: Vec<u8>) -> ErrorResult {
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send( self.get_bluetooth_thread().send(
BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap(); BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap();
@ -121,7 +122,9 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
BluetoothObjectMsg::BluetoothWriteValue => Ok(()), BluetoothObjectMsg::BluetoothWriteValue => Ok(()),
BluetoothObjectMsg::Error { BluetoothObjectMsg::Error {
error error
} => Err(Type(error)), } => {
Err(Type(error))
},
_ => unreachable!() _ => unreachable!()
} }
} }

View file

@ -7,7 +7,7 @@ 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::codegen::UnionTypes::StringOrUnsignedLong;
use dom::bindings::error::Error::Type; use dom::bindings::error::Error::Type;
use dom::bindings::error::Fallible; use dom::bindings::error::{Fallible, ErrorResult};
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};
@ -64,9 +64,8 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect
fn Connect(&self) -> Fallible<Root<BluetoothRemoteGATTServer>> { fn Connect(&self) -> Fallible<Root<BluetoothRemoteGATTServer>> {
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread() self.get_bluetooth_thread().send(
.send(BluetoothMethodMsg::GATTServerConnect(String::from(self.Device().Id()), sender)) BluetoothMethodMsg::GATTServerConnect(String::from(self.Device().Id()), sender)).unwrap();
.unwrap();
let server = receiver.recv().unwrap(); let server = receiver.recv().unwrap();
match server { match server {
BluetoothObjectMsg::BluetoothServer { BluetoothObjectMsg::BluetoothServer {
@ -77,13 +76,15 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
}, },
BluetoothObjectMsg::Error { BluetoothObjectMsg::Error {
error error
} => Err(Type(error)), } => {
Err(Type(error))
},
_ => unreachable!() _ => unreachable!()
} }
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-disconnect // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-disconnect
fn Disconnect(&self) -> Fallible<()>{ fn Disconnect(&self) -> ErrorResult {
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send( self.get_bluetooth_thread().send(
BluetoothMethodMsg::GATTServerDisconnect(String::from(self.Device().Id()), sender)).unwrap(); BluetoothMethodMsg::GATTServerDisconnect(String::from(self.Device().Id()), sender)).unwrap();
@ -97,7 +98,9 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
}, },
BluetoothObjectMsg::Error { BluetoothObjectMsg::Error {
error error
} => Err(Type(error)), } => {
Err(Type(error))
},
_ => unreachable!() _ => unreachable!()
} }
} }
@ -119,33 +122,35 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
instance_id, instance_id,
} => { } => {
Ok(BluetoothRemoteGATTService::new(self.global().r(), Ok(BluetoothRemoteGATTService::new(self.global().r(),
&self.device.get(), &self.device.get(),
DOMString::from(uuid), DOMString::from(uuid),
is_primary, is_primary,
instance_id)) instance_id))
}, },
BluetoothObjectMsg::Error { BluetoothObjectMsg::Error {
error error
} => Err(Type(error)), } => {
Err(Type(error))
},
_ => unreachable!(), _ => unreachable!(),
} }
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices
fn GetPrimaryServices(&self, service: Option<StringOrUnsignedLong>) fn GetPrimaryServices(&self,
service: Option<StringOrUnsignedLong>)
-> Fallible<Vec<Root<BluetoothRemoteGATTService>>> { -> Fallible<Vec<Root<BluetoothRemoteGATTService>>> {
let uuid: Option<String> = match service { let mut uuid: Option<String> = None;
Some(s) => match BluetoothUUID::GetService(self.global().r(), s.clone()) { if let Some(s)= service {
Ok(domstring) => Some(domstring.to_string()), match BluetoothUUID::GetService(self.global().r(), s.clone()) {
Ok(domstring) => uuid = Some(domstring.to_string()),
Err(error) => return Err(error), Err(error) => return Err(error),
}, }
None => None,
}; };
let mut services: Vec<Root<BluetoothRemoteGATTService>> = vec!(); let mut services: Vec<Root<BluetoothRemoteGATTService>> = vec!();
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread() self.get_bluetooth_thread().send(
.send(BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)) BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap();
.unwrap();
let services_vec = receiver.recv().unwrap(); let services_vec = receiver.recv().unwrap();
match services_vec { match services_vec {
BluetoothObjectMsg::BluetoothServices { BluetoothObjectMsg::BluetoothServices {
@ -162,7 +167,7 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
&self.device.get(), &self.device.get(),
DOMString::from(uuid), DOMString::from(uuid),
is_primary, is_primary,
instance_id)) instance_id));
}, },
_ => unreachable!(), _ => unreachable!(),
} }
@ -171,7 +176,9 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
}, },
BluetoothObjectMsg::Error { BluetoothObjectMsg::Error {
error error
} => Err(Type(error)), } => {
Err(Type(error))
},
_ => unreachable!(), _ => unreachable!(),
} }
} }

View file

@ -85,17 +85,16 @@ 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, characteristic: StringOrUnsignedLong) fn GetCharacteristic(&self,
characteristic: StringOrUnsignedLong)
-> Fallible<Root<BluetoothRemoteGATTCharacteristic>> { -> Fallible<Root<BluetoothRemoteGATTCharacteristic>> {
let uuid: String = match BluetoothUUID::GetCharacteristic(self.global().r(), let uuid: String = match BluetoothUUID::GetCharacteristic(self.global().r(), characteristic.clone()) {
characteristic.clone()) {
Ok(domstring) => domstring.to_string(), Ok(domstring) => domstring.to_string(),
Err(error) => return Err(error), Err(error) => return Err(error),
}; };
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread() self.get_bluetooth_thread().send(
.send(BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), uuid, sender)) BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), uuid, sender)).unwrap();
.unwrap();
let characteristic = receiver.recv().unwrap(); let characteristic = receiver.recv().unwrap();
match characteristic { match characteristic {
BluetoothObjectMsg::BluetoothCharacteristic { BluetoothObjectMsg::BluetoothCharacteristic {
@ -122,33 +121,35 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
reliable_write, reliable_write,
writable_auxiliaries); writable_auxiliaries);
Ok(BluetoothRemoteGATTCharacteristic::new(self.global().r(), Ok(BluetoothRemoteGATTCharacteristic::new(self.global().r(),
&self, &self,
DOMString::from(uuid), DOMString::from(uuid),
properties, properties,
instance_id)) instance_id))
}, },
BluetoothObjectMsg::Error { BluetoothObjectMsg::Error {
error error
} => return Err(Type(error)), } => {
Err(Type(error))
},
_ => unreachable!(), _ => unreachable!(),
} }
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics
fn GetCharacteristics(&self, characteristic: Option<StringOrUnsignedLong>) fn GetCharacteristics(&self,
characteristic: Option<StringOrUnsignedLong>)
-> Fallible<Vec<Root<BluetoothRemoteGATTCharacteristic>>> { -> Fallible<Vec<Root<BluetoothRemoteGATTCharacteristic>>> {
let uuid: Option<String> = match characteristic { let mut uuid: Option<String> = None;
Some(c) => match BluetoothUUID::GetCharacteristic(self.global().r(), c.clone()) { if let Some(c)= characteristic {
Ok(domstring) => Some(domstring.to_string()), match BluetoothUUID::GetCharacteristic(self.global().r(), c.clone()) {
Ok(domstring) => uuid = Some(domstring.to_string()),
Err(error) => return Err(error), Err(error) => return Err(error),
}, }
None => None,
}; };
let mut characteristics: Vec<Root<BluetoothRemoteGATTCharacteristic>> = vec!(); let mut characteristics: Vec<Root<BluetoothRemoteGATTCharacteristic>> = vec!();
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread() self.get_bluetooth_thread().send(
.send(BluetoothMethodMsg::GetCharacteristics(self.get_instance_id(), uuid, sender)) BluetoothMethodMsg::GetCharacteristics(self.get_instance_id(), uuid, sender)).unwrap();
.unwrap();
let characteristics_vec = receiver.recv().unwrap(); let characteristics_vec = receiver.recv().unwrap();
match characteristics_vec { match characteristics_vec {
BluetoothObjectMsg::BluetoothCharacteristics { BluetoothObjectMsg::BluetoothCharacteristics {
@ -169,23 +170,21 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
reliable_write, reliable_write,
writable_auxiliaries, writable_auxiliaries,
} => { } => {
let properties = &BluetoothCharacteristicProperties::new( let properties = &BluetoothCharacteristicProperties::new(self.global().r(),
self.global().r(), broadcast,
broadcast, read,
read, write_without_response,
write_without_response, write,
write, notify,
notify, indicate,
indicate, authenticated_signed_writes,
authenticated_signed_writes, reliable_write,
reliable_write, writable_auxiliaries);
writable_auxiliaries); characteristics.push(BluetoothRemoteGATTCharacteristic::new(self.global().r(),
characteristics.push(BluetoothRemoteGATTCharacteristic::new( &self,
self.global().r(), DOMString::from(uuid),
&self, properties,
DOMString::from(uuid), instance_id));
properties,
instance_id))
}, },
_ => unreachable!(), _ => unreachable!(),
} }
@ -194,7 +193,9 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
}, },
BluetoothObjectMsg::Error { BluetoothObjectMsg::Error {
error error
} => return Err(Type(error)), } => {
Err(Type(error))
},
_ => unreachable!(), _ => unreachable!(),
} }
} }

View file

@ -274,9 +274,7 @@ impl BluetoothUUID {
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getservice // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getservice
pub fn GetService(globalref: GlobalRef, pub fn GetService(globalref: GlobalRef, name: StringOrUnsignedLong) -> Fallible<UUID> {
name: StringOrUnsignedLong)
-> Fallible<UUID> {
BluetoothUUID::resolve_uuid_name(globalref, BluetoothUUID::resolve_uuid_name(globalref,
name, name,
BLUETOOTH_ASSIGNED_SERVICES, BLUETOOTH_ASSIGNED_SERVICES,
@ -284,9 +282,7 @@ impl BluetoothUUID {
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getcharacteristic // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getcharacteristic
pub fn GetCharacteristic(globalref: GlobalRef, pub fn GetCharacteristic(globalref: GlobalRef, name: StringOrUnsignedLong) -> Fallible<UUID> {
name: StringOrUnsignedLong)
-> Fallible<UUID> {
BluetoothUUID::resolve_uuid_name(globalref, BluetoothUUID::resolve_uuid_name(globalref,
name, name,
BLUETOOTH_ASSIGNED_CHARCTERISTICS, BLUETOOTH_ASSIGNED_CHARCTERISTICS,
@ -294,9 +290,7 @@ impl BluetoothUUID {
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getdescriptor // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getdescriptor
pub fn GetDescriptor(globalref: GlobalRef, pub fn GetDescriptor(globalref: GlobalRef, name: StringOrUnsignedLong) -> Fallible<UUID> {
name: StringOrUnsignedLong)
-> Fallible<UUID> {
BluetoothUUID::resolve_uuid_name(globalref, BluetoothUUID::resolve_uuid_name(globalref,
name, name,
BLUETOOTH_ASSIGNED_DESCRIPTORS, BLUETOOTH_ASSIGNED_DESCRIPTORS,
@ -311,7 +305,7 @@ impl BluetoothUUID {
-> Fallible<DOMString> { -> Fallible<DOMString> {
match name { match name {
// Step 1 // Step 1
StringOrUnsignedLong::UnsignedLong(unsigned32) =>{ StringOrUnsignedLong::UnsignedLong(unsigned32) => {
Ok(BluetoothUUID::CanonicalUUID(globalref, unsigned32)) Ok(BluetoothUUID::CanonicalUUID(globalref, unsigned32))
}, },
StringOrUnsignedLong::String(dstring) => { StringOrUnsignedLong::String(dstring) => {
@ -322,8 +316,7 @@ impl BluetoothUUID {
} else { } else {
// Step 3 // Step 3
let concatenated = format!("{}.{}", prefix, dstring); let concatenated = format!("{}.{}", prefix, dstring);
let is_in_table = assigned_numbers_table.iter() let is_in_table = assigned_numbers_table.iter().find(|p| p.0 == concatenated);
.find(|p| p.0 == concatenated);
match is_in_table { match is_in_table {
Some(&(_, alias)) => Ok(BluetoothUUID::CanonicalUUID(globalref, alias)), Some(&(_, alias)) => Ok(BluetoothUUID::CanonicalUUID(globalref, alias)),
None => Err(Syntax), None => Err(Syntax),