Refactor IPC-message sending

This commit is contained in:
fokinv 2016-04-25 13:54:06 +02:00 committed by Attila Dusnoki
parent ecf4c942da
commit c8672ed0af
7 changed files with 326 additions and 493 deletions

View file

@ -9,7 +9,10 @@ use device::bluetooth::BluetoothGATTDescriptor;
use device::bluetooth::BluetoothGATTService; use device::bluetooth::BluetoothGATTService;
use ipc_channel::ipc::{self, IpcReceiver, IpcSender}; use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use net_traits::bluetooth_scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence, RequestDeviceoptions}; use net_traits::bluetooth_scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence, RequestDeviceoptions};
use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg}; use net_traits::bluetooth_thread::{BluetoothCharacteristicMsg, BluetoothCharacteristicsMsg};
use net_traits::bluetooth_thread::{BluetoothDescriptorMsg, BluetoothDescriptorsMsg};
use net_traits::bluetooth_thread::{BluetoothDeviceMsg, BluetoothMethodMsg};
use net_traits::bluetooth_thread::{BluetoothResult, BluetoothServiceMsg, BluetoothServicesMsg};
use std::borrow::ToOwned; use std::borrow::ToOwned;
use std::collections::HashMap; use std::collections::HashMap;
use std::string::String; use std::string::String;
@ -37,12 +40,6 @@ bitflags! {
} }
} }
macro_rules! send_error(
($sender:expr, $error:expr) => (
return $sender.send(BluetoothObjectMsg::Error { error: String::from($error) }).unwrap();
);
);
macro_rules! return_if_cached( macro_rules! return_if_cached(
($cache:expr, $key:expr) => ( ($cache:expr, $key:expr) => (
if $cache.contains_key($key) { if $cache.contains_key($key) {
@ -351,14 +348,16 @@ impl BluetoothManager {
// Methods // Methods
fn request_device(&mut self, options: RequestDeviceoptions, sender: IpcSender<BluetoothObjectMsg>) { fn request_device(&mut self,
options: RequestDeviceoptions,
sender: IpcSender<BluetoothResult<BluetoothDeviceMsg>>) {
let mut adapter = match self.get_or_create_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
}; };
let devices = self.get_and_cache_devices(&mut adapter); let devices = self.get_and_cache_devices(&mut adapter);
if devices.is_empty() { if devices.is_empty() {
send_error!(sender, DEVICE_ERROR); return drop(sender.send(Err(String::from(DEVICE_ERROR))));
} }
let matched_devices: Vec<BluetoothDevice> = devices.into_iter() let matched_devices: Vec<BluetoothDevice> = devices.into_iter()
@ -366,34 +365,28 @@ impl BluetoothManager {
.collect(); .collect();
for device in matched_devices { for device in matched_devices {
if let Ok(address) = device.get_address() { if let Ok(address) = device.get_address() {
let message = BluetoothObjectMsg::BluetoothDevice { let message = Ok(BluetoothDeviceMsg {
id: address, id: address,
name: device.get_name().ok(), name: device.get_name().ok(),
device_class: device.get_class().ok(), device_class: device.get_class().ok(),
vendor_id_source: device.get_vendor_id_source().ok(), vendor_id_source: device.get_vendor_id_source().ok(),
vendor_id: device.get_vendor_id().ok(), vendor_id: device.get_vendor_id().ok(),
product_id: device.get_product_id().ok(), product_id: device.get_product_id().ok(),
product_version: device.get_device_id().ok(), product_version: device.get_device_id().ok(),
appearance: device.get_appearance().ok(), appearance: device.get_appearance().ok(),
tx_power: match device.get_tx_power() { tx_power: device.get_tx_power().ok().map(|p| p as i8),
Ok(p) => Some(p as i8), rssi: device.get_rssi().ok().map(|p| p as i8),
Err(_) => None, });
}, return drop(sender.send(message));
rssi: match device.get_rssi() {
Ok(p) => Some(p as i8),
Err(_) => None,
},
};
return sender.send(message).unwrap();
} }
} }
send_error!(sender, DEVICE_MATCH_ERROR); return drop(sender.send(Err(String::from(DEVICE_MATCH_ERROR))));
} }
fn gatt_server_connect(&mut self, device_id: String, sender: IpcSender<BluetoothObjectMsg>) { fn gatt_server_connect(&mut self, device_id: String, sender: IpcSender<BluetoothResult<bool>>) {
let mut adapter = match self.get_or_create_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
}; };
let connected = match self.get_device(&mut adapter, &device_id) { let connected = match self.get_device(&mut adapter, &device_id) {
@ -404,19 +397,16 @@ impl BluetoothManager {
d.connect().is_ok() d.connect().is_ok()
} }
}, },
None => send_error!(sender, DEVICE_ERROR), None => return drop(sender.send(Err(String::from(DEVICE_ERROR)))),
}; };
let message = BluetoothObjectMsg::BluetoothServer { let _ = sender.send(Ok(connected));
connected: connected
};
sender.send(message).unwrap();
} }
fn gatt_server_disconnect(&mut self, device_id: String, sender: IpcSender<BluetoothObjectMsg>) { fn gatt_server_disconnect(&mut self, device_id: String, sender: IpcSender<BluetoothResult<bool>>) {
let mut adapter = match self.get_or_create_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
}; };
let connected = match self.get_device(&mut adapter, &device_id) { let connected = match self.get_device(&mut adapter, &device_id) {
@ -427,201 +417,205 @@ impl BluetoothManager {
false false
} }
}, },
None => send_error!(sender, DEVICE_ERROR), None => return drop(sender.send(Err(String::from(DEVICE_ERROR)))),
}; };
let message = BluetoothObjectMsg::BluetoothServer { let _ = sender.send(Ok(connected));
connected: connected
};
sender.send(message).unwrap();
} }
fn get_primary_service(&mut self, device_id: String, uuid: String, sender: IpcSender<BluetoothObjectMsg>) { fn get_primary_service(&mut self,
device_id: String,
uuid: String,
sender: IpcSender<BluetoothResult<BluetoothServiceMsg>>) {
let mut adapter = match self.get_or_create_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => return drop(sender.send(Err(String::from(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, PRIMARY_SERVICE_ERROR); return drop(sender.send(Err(String::from(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) {
if let Ok(uuid) = service.get_uuid() { if let Ok(uuid) = service.get_uuid() {
let message = BluetoothObjectMsg::BluetoothService { return drop(sender.send(Ok(BluetoothServiceMsg {
uuid: uuid, uuid: uuid,
is_primary: true, is_primary: true,
instance_id: service.get_object_path(), instance_id: service.get_object_path(),
}; })));
return sender.send(message).unwrap();
} }
} }
} }
send_error!(sender, PRIMARY_SERVICE_ERROR); return drop(sender.send(Err(String::from(PRIMARY_SERVICE_ERROR))));
} }
fn get_primary_services(&mut self, fn get_primary_services(&mut self,
device_id: String, device_id: String,
uuid: Option<String>, uuid: Option<String>,
sender: IpcSender<BluetoothObjectMsg>) { sender: IpcSender<BluetoothResult<BluetoothServicesMsg>>) {
let mut adapter = match self.get_or_create_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
}; };
let services = match uuid { let services = 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_and_cache_gatt_services(&mut adapter, &device_id), None => self.get_and_cache_gatt_services(&mut adapter, &device_id),
}; };
if services.is_empty() { if services.is_empty() {
send_error!(sender, PRIMARY_SERVICE_ERROR); return drop(sender.send(Err(String::from(PRIMARY_SERVICE_ERROR))));
} }
let mut services_vec = vec!(); let mut services_vec = vec!();
for service in services { for service in services {
if service.is_primary().unwrap_or(false) { if service.is_primary().unwrap_or(false) {
if let Ok(uuid) = service.get_uuid() { if let Ok(uuid) = service.get_uuid() {
services_vec.push(BluetoothObjectMsg::BluetoothService { services_vec.push(BluetoothServiceMsg {
uuid: uuid, uuid: uuid,
is_primary: true, is_primary: true,
instance_id: service.get_object_path(), instance_id: service.get_object_path(),
}); });
} }
} }
} }
if services_vec.is_empty() { if services_vec.is_empty() {
send_error!(sender, PRIMARY_SERVICE_ERROR); return drop(sender.send(Err(String::from(PRIMARY_SERVICE_ERROR))));
} }
let message = BluetoothObjectMsg::BluetoothServices { services_vec: services_vec };
sender.send(message).unwrap(); let _ = sender.send(Ok(services_vec));
} }
fn get_characteristic(&mut self, service_id: String, uuid: String, sender: IpcSender<BluetoothObjectMsg>) { fn get_characteristic(&mut self,
service_id: String,
uuid: String,
sender: IpcSender<BluetoothResult<BluetoothCharacteristicMsg>>) {
let mut adapter = match self.get_or_create_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => return drop(sender.send(Err(String::from(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, CHARACTERISTIC_ERROR); return drop(sender.send(Err(String::from(CHARACTERISTIC_ERROR))));
} }
for characteristic in characteristics { for characteristic in characteristics {
if let Ok(uuid) = characteristic.get_uuid() { if let Ok(uuid) = characteristic.get_uuid() {
let properties = self.get_characteristic_properties(&characteristic); let properties = self.get_characteristic_properties(&characteristic);
let message = BluetoothObjectMsg::BluetoothCharacteristic { let message = Ok(BluetoothCharacteristicMsg {
uuid: uuid, uuid: uuid,
instance_id: characteristic.get_object_path(), instance_id: characteristic.get_object_path(),
broadcast: properties.contains(BROADCAST), broadcast: properties.contains(BROADCAST),
read: properties.contains(READ), read: properties.contains(READ),
write_without_response: properties.contains(WRITE_WITHOUT_RESPONSE), write_without_response: properties.contains(WRITE_WITHOUT_RESPONSE),
write: properties.contains(WRITE), write: properties.contains(WRITE),
notify: properties.contains(NOTIFY), notify: properties.contains(NOTIFY),
indicate: properties.contains(INDICATE), indicate: properties.contains(INDICATE),
authenticated_signed_writes: properties.contains(AUTHENTICATED_SIGNED_WRITES), authenticated_signed_writes: properties.contains(AUTHENTICATED_SIGNED_WRITES),
reliable_write: properties.contains(RELIABLE_WRITE), reliable_write: properties.contains(RELIABLE_WRITE),
writable_auxiliaries: properties.contains(WRITABLE_AUXILIARIES), writable_auxiliaries: properties.contains(WRITABLE_AUXILIARIES),
}; });
return sender.send(message).unwrap(); return drop(sender.send(message));
} }
} }
send_error!(sender, CHARACTERISTIC_ERROR); return drop(sender.send(Err(String::from(CHARACTERISTIC_ERROR))));
} }
fn get_characteristics(&mut self, fn get_characteristics(&mut self,
service_id: String, service_id: String,
uuid: Option<String>, uuid: Option<String>,
sender: IpcSender<BluetoothObjectMsg>) { sender: IpcSender<BluetoothResult<BluetoothCharacteristicsMsg>>) {
let mut adapter = match self.get_or_create_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => return drop(sender.send(Err(String::from(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_and_cache_gatt_characteristics(&mut adapter, &service_id), None => self.get_and_cache_gatt_characteristics(&mut adapter, &service_id),
}; };
if characteristics.is_empty() { if characteristics.is_empty() {
send_error!(sender, CHARACTERISTIC_ERROR); return drop(sender.send(Err(String::from(CHARACTERISTIC_ERROR))));
} }
let mut characteristics_vec = vec!(); let mut characteristics_vec = vec!();
for characteristic in characteristics { for characteristic in characteristics {
if let Ok(uuid) = characteristic.get_uuid() { if let Ok(uuid) = characteristic.get_uuid() {
let properties = self.get_characteristic_properties(&characteristic); let properties = self.get_characteristic_properties(&characteristic);
characteristics_vec.push(BluetoothObjectMsg::BluetoothCharacteristic { characteristics_vec.push(
uuid: uuid, BluetoothCharacteristicMsg {
instance_id: characteristic.get_object_path(), uuid: uuid,
broadcast: properties.contains(BROADCAST), instance_id: characteristic.get_object_path(),
read: properties.contains(READ), broadcast: properties.contains(BROADCAST),
write_without_response: properties.contains(WRITE_WITHOUT_RESPONSE), read: properties.contains(READ),
write: properties.contains(WRITE), write_without_response: properties.contains(WRITE_WITHOUT_RESPONSE),
notify: properties.contains(NOTIFY), write: properties.contains(WRITE),
indicate: properties.contains(INDICATE), notify: properties.contains(NOTIFY),
authenticated_signed_writes: properties.contains(AUTHENTICATED_SIGNED_WRITES), indicate: properties.contains(INDICATE),
reliable_write: properties.contains(RELIABLE_WRITE), authenticated_signed_writes: properties.contains(AUTHENTICATED_SIGNED_WRITES),
writable_auxiliaries: properties.contains(WRITABLE_AUXILIARIES), reliable_write: properties.contains(RELIABLE_WRITE),
}); writable_auxiliaries: properties.contains(WRITABLE_AUXILIARIES),
});
} }
} }
if characteristics_vec.is_empty() { if characteristics_vec.is_empty() {
send_error!(sender, CHARACTERISTIC_ERROR); return drop(sender.send(Err(String::from(CHARACTERISTIC_ERROR))));
} }
let message = BluetoothObjectMsg::BluetoothCharacteristics { characteristics_vec: characteristics_vec };
sender.send(message).unwrap(); let _ = sender.send(Ok(characteristics_vec));
} }
fn get_descriptor(&mut self, characteristic_id: String, uuid: String, sender: IpcSender<BluetoothObjectMsg>) { fn get_descriptor(&mut self,
characteristic_id: String,
uuid: String,
sender: IpcSender<BluetoothResult<BluetoothDescriptorMsg>>) {
let mut adapter = match self.get_or_create_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => return drop(sender.send(Err(String::from(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, DESCRIPTOR_ERROR); return drop(sender.send(Err(String::from(DESCRIPTOR_ERROR))));
} }
for descriptor in descriptors { for descriptor in descriptors {
if let Ok(uuid) = descriptor.get_uuid() { if let Ok(uuid) = descriptor.get_uuid() {
let message = BluetoothObjectMsg::BluetoothDescriptor { return drop(sender.send(Ok(BluetoothDescriptorMsg {
uuid: uuid, uuid: uuid,
instance_id: descriptor.get_object_path(), instance_id: descriptor.get_object_path(),
}; })));
return sender.send(message).unwrap();
} }
} }
send_error!(sender, DESCRIPTOR_ERROR); return drop(sender.send(Err(String::from(DESCRIPTOR_ERROR))));
} }
fn get_descriptors(&mut self, fn get_descriptors(&mut self,
characteristic_id: String, characteristic_id: String,
uuid: Option<String>, uuid: Option<String>,
sender: IpcSender<BluetoothObjectMsg>) { sender: IpcSender<BluetoothResult<BluetoothDescriptorsMsg>>) {
let mut adapter = match self.get_or_create_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => return drop(sender.send(Err(String::from(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_and_cache_gatt_descriptors(&mut adapter, &characteristic_id), None => self.get_and_cache_gatt_descriptors(&mut adapter, &characteristic_id),
}; };
if descriptors.is_empty() { if descriptors.is_empty() {
send_error!(sender, DESCRIPTOR_ERROR); return drop(sender.send(Err(String::from(DESCRIPTOR_ERROR))));
} }
let mut descriptors_vec = vec!(); let mut descriptors_vec = vec!();
for descriptor in descriptors { for descriptor in descriptors {
if let Ok(uuid) = descriptor.get_uuid() { if let Ok(uuid) = descriptor.get_uuid() {
descriptors_vec.push(BluetoothObjectMsg::BluetoothDescriptor { descriptors_vec.push(BluetoothDescriptorMsg {
uuid: uuid, uuid: uuid,
instance_id: descriptor.get_object_path(), instance_id: descriptor.get_object_path(),
}); });
} }
} }
if descriptors_vec.is_empty() { if descriptors_vec.is_empty() {
send_error!(sender, DESCRIPTOR_ERROR); return drop(sender.send(Err(String::from(DESCRIPTOR_ERROR))));
} }
let message = BluetoothObjectMsg::BluetoothDescriptors { descriptors_vec: descriptors_vec }; let _ = sender.send(Ok(descriptors_vec));
sender.send(message).unwrap();
} }
fn read_value(&mut self, id: String, sender: IpcSender<BluetoothObjectMsg>) { fn read_value(&mut self, id: String, sender: IpcSender<BluetoothResult<Vec<u8>>>) {
let mut adapter = match self.get_or_create_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
}; };
let mut value = self.get_gatt_characteristic(&mut adapter, &id) let mut value = self.get_gatt_characteristic(&mut adapter, &id)
.map(|c| c.read_value().unwrap_or(vec![])); .map(|c| c.read_value().unwrap_or(vec![]));
@ -629,19 +623,13 @@ impl BluetoothManager {
value = self.get_gatt_descriptor(&mut adapter, &id) value = self.get_gatt_descriptor(&mut adapter, &id)
.map(|d| d.read_value().unwrap_or(vec![])); .map(|d| d.read_value().unwrap_or(vec![]));
} }
let _ = sender.send(value.ok_or(String::from(VALUE_ERROR)));
let message = match value {
Some(v) => BluetoothObjectMsg::BluetoothReadValue { value: v },
None => send_error!(sender, VALUE_ERROR),
};
sender.send(message).unwrap();
} }
fn write_value(&mut self, id: String, value: Vec<u8>, sender: IpcSender<BluetoothObjectMsg>) { fn write_value(&mut self, id: String, value: Vec<u8>, sender: IpcSender<BluetoothResult<bool>>) {
let mut adapter = match self.get_or_create_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
}; };
let mut result = self.get_gatt_characteristic(&mut adapter, &id) let mut result = self.get_gatt_characteristic(&mut adapter, &id)
.map(|c| c.write_value(value.clone())); .map(|c| c.write_value(value.clone()));
@ -649,15 +637,13 @@ impl BluetoothManager {
result = self.get_gatt_descriptor(&mut adapter, &id) result = self.get_gatt_descriptor(&mut adapter, &id)
.map(|d| d.write_value(value.clone())); .map(|d| d.write_value(value.clone()));
} }
let message = match result { let message = match result {
Some(v) => match v { Some(v) => match v {
Ok(_) => BluetoothObjectMsg::BluetoothWriteValue, Ok(_) => Ok(true),
Err(e) => send_error!(sender, e.to_string()), Err(e) => return drop(sender.send(Err(e.to_string()))),
}, },
None => send_error!(sender, VALUE_ERROR), None => return drop(sender.send(Err(String::from(VALUE_ERROR)))),
}; };
let _ = sender.send(message);
sender.send(message).unwrap();
} }
} }

View file

@ -5,78 +5,71 @@ use bluetooth_scanfilter::RequestDeviceoptions;
use ipc_channel::ipc::IpcSender; use ipc_channel::ipc::IpcSender;
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub enum BluetoothMethodMsg { pub struct BluetoothDeviceMsg {
RequestDevice(RequestDeviceoptions, IpcSender<BluetoothObjectMsg>), // Bluetooth Device properties
GATTServerConnect(String, IpcSender<BluetoothObjectMsg>), pub id: String,
GATTServerDisconnect(String, IpcSender<BluetoothObjectMsg>), pub name: Option<String>,
GetPrimaryService(String, String, IpcSender<BluetoothObjectMsg>), pub device_class: Option<u32>,
GetPrimaryServices(String, Option<String>, IpcSender<BluetoothObjectMsg>), pub vendor_id_source: Option<String>,
GetCharacteristic(String, String, IpcSender<BluetoothObjectMsg>), pub vendor_id: Option<u32>,
GetCharacteristics(String, Option<String>, IpcSender<BluetoothObjectMsg>), pub product_id: Option<u32>,
GetDescriptor(String, String, IpcSender<BluetoothObjectMsg>), pub product_version: Option<u32>,
GetDescriptors(String, Option<String>, IpcSender<BluetoothObjectMsg>), // Advertisiong Data properties
ReadValue(String, IpcSender<BluetoothObjectMsg>), pub appearance: Option<u16>,
WriteValue(String, Vec<u8>, IpcSender<BluetoothObjectMsg>), pub tx_power: Option<i8>,
Exit, pub rssi: Option<i8>,
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub enum BluetoothObjectMsg { pub struct BluetoothServiceMsg {
BluetoothDevice { pub uuid: String,
// Bluetooth Device properties pub is_primary: bool,
id: String, pub instance_id: String,
name: Option<String>, }
device_class: Option<u32>,
vendor_id_source: Option<String>, #[derive(Deserialize, Serialize)]
vendor_id: Option<u32>, pub struct BluetoothCharacteristicMsg {
product_id: Option<u32>, // Characteristic
product_version: Option<u32>, pub uuid: String,
// Advertisiong Data properties pub instance_id: String,
appearance: Option<u16>, // Characteristic properties
tx_power: Option<i8>, pub broadcast: bool,
rssi: Option<i8> pub read: bool,
}, pub write_without_response: bool,
BluetoothServer { pub write: bool,
connected: bool pub notify: bool,
}, pub indicate: bool,
BluetoothService { pub authenticated_signed_writes: bool,
uuid: String, pub reliable_write: bool,
is_primary: bool, pub writable_auxiliaries: bool,
instance_id: String }
},
BluetoothServices { #[derive(Deserialize, Serialize)]
services_vec: Vec<BluetoothObjectMsg> pub struct BluetoothDescriptorMsg {
}, pub uuid: String,
BluetoothCharacteristic { pub instance_id: String,
// Characteristic }
uuid: String,
instance_id: String, pub type BluetoothServicesMsg = Vec<BluetoothServiceMsg>;
// Characteristic properties
broadcast: bool, pub type BluetoothCharacteristicsMsg = Vec<BluetoothCharacteristicMsg>;
read: bool,
write_without_response: bool, pub type BluetoothDescriptorsMsg = Vec<BluetoothDescriptorMsg>;
write: bool,
notify: bool, pub type BluetoothResult<T> = Result<T, String>;
indicate: bool,
authenticated_signed_writes: bool, #[derive(Deserialize, Serialize)]
reliable_write: bool, pub enum BluetoothMethodMsg {
writable_auxiliaries: bool RequestDevice(RequestDeviceoptions, IpcSender<BluetoothResult<BluetoothDeviceMsg>>),
}, GATTServerConnect(String, IpcSender<BluetoothResult<bool>>),
BluetoothCharacteristics { GATTServerDisconnect(String, IpcSender<BluetoothResult<bool>>),
characteristics_vec: Vec<BluetoothObjectMsg> GetPrimaryService(String, String, IpcSender<BluetoothResult<BluetoothServiceMsg>>),
}, GetPrimaryServices(String, Option<String>, IpcSender<BluetoothResult<BluetoothServicesMsg>>),
BluetoothDescriptor { GetCharacteristic(String, String, IpcSender<BluetoothResult<BluetoothCharacteristicMsg>>),
uuid: String, GetCharacteristics(String, Option<String>, IpcSender<BluetoothResult<BluetoothCharacteristicsMsg>>),
instance_id: String GetDescriptor(String, String, IpcSender<BluetoothResult<BluetoothDescriptorMsg>>),
}, GetDescriptors(String, Option<String>, IpcSender<BluetoothResult<BluetoothDescriptorsMsg>>),
BluetoothDescriptors { ReadValue(String, IpcSender<BluetoothResult<Vec<u8>>>),
descriptors_vec: Vec<BluetoothObjectMsg>, WriteValue(String, Vec<u8>, IpcSender<BluetoothResult<bool>>),
}, Exit,
BluetoothReadValue {
value: Vec<u8>
},
BluetoothWriteValue,
Error {
error: String
},
} }

View file

@ -18,7 +18,7 @@ use dom::bluetoothuuid::BluetoothUUID;
use ipc_channel::ipc::{self, IpcSender}; use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence}; use net_traits::bluetooth_scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence};
use net_traits::bluetooth_scanfilter::{RequestDeviceoptions, ServiceUUIDSequence}; use net_traits::bluetooth_scanfilter::{RequestDeviceoptions, ServiceUUIDSequence};
use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg}; use net_traits::bluetooth_thread::BluetoothMethodMsg;
use util::str::DOMString; use util::str::DOMString;
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.";
@ -133,51 +133,32 @@ 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, option: &RequestDeviceOptions) -> Fallible<Root<BluetoothDevice>> { fn RequestDevice(&self, option: &RequestDeviceOptions) -> Fallible<Root<BluetoothDevice>> {
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
match try!(convert_request_device_options(option, self.global().r())) { let option = try!(convert_request_device_options(option, self.global().r()));
option => { self.get_bluetooth_thread().send(BluetoothMethodMsg::RequestDevice(option, sender)).unwrap();
self.get_bluetooth_thread().send( let device = receiver.recv().unwrap();
BluetoothMethodMsg::RequestDevice(option, sender)).unwrap(); match device {
let device = receiver.recv().unwrap(); Ok(device) => {
match device { let ad_data = BluetoothAdvertisingData::new(self.global().r(),
BluetoothObjectMsg::BluetoothDevice { device.appearance,
id, device.tx_power,
name, device.rssi);
device_class, let vendor_id_source = device.vendor_id_source.map(|vid| match vid.as_str() {
vendor_id_source, "bluetooth" => VendorIDSource::Bluetooth,
vendor_id, "usb" => VendorIDSource::Usb,
product_id, _ => VendorIDSource::Unknown,
product_version, });
appearance, Ok(BluetoothDevice::new(self.global().r(),
tx_power, DOMString::from(device.id),
rssi, device.name.map(DOMString::from),
} => { &ad_data,
let ad_data = &BluetoothAdvertisingData::new(self.global().r(), device.device_class,
appearance, vendor_id_source,
tx_power, device.vendor_id,
rssi); device.product_id,
let vendor_id_source = vendor_id_source.map(|vid| match vid.as_str() { device.product_version))
"bluetooth" => VendorIDSource::Bluetooth, },
"usb" => VendorIDSource::Usb, Err(error) => {
_ => VendorIDSource::Unknown, Err(Type(error))
});
let name = name.map(DOMString::from);
Ok(BluetoothDevice::new(self.global().r(),
DOMString::from(id),
name,
ad_data,
device_class,
vendor_id_source,
vendor_id,
product_id,
product_version))
},
BluetoothObjectMsg::Error {
error
} => {
Err(Type(error))
},
_ => unreachable!()
}
}, },
} }
} }

View file

@ -20,7 +20,7 @@ use dom::bluetoothremotegattdescriptor::BluetoothRemoteGATTDescriptor;
use dom::bluetoothremotegattservice::BluetoothRemoteGATTService; use dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
use dom::bluetoothuuid::{BluetoothDescriptorUUID, BluetoothUUID}; use dom::bluetoothuuid::{BluetoothDescriptorUUID, 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;
use util::str::DOMString; use util::str::DOMString;
// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattcharacteristic // https://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattcharacteristic
@ -94,30 +94,21 @@ 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, descriptor: BluetoothDescriptorUUID) -> Fallible<Root<BluetoothRemoteGATTDescriptor>> { fn GetDescriptor(&self, descriptor: BluetoothDescriptorUUID) -> Fallible<Root<BluetoothRemoteGATTDescriptor>> {
let uuid: String = match BluetoothUUID::GetDescriptor(self.global().r(), descriptor.clone()) { let uuid = try!(BluetoothUUID::GetDescriptor(self.global().r(), descriptor)).to_string();
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(), uuid, 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 { Ok(descriptor) => {
uuid,
instance_id
} => {
Ok(BluetoothRemoteGATTDescriptor::new(self.global().r(), Ok(BluetoothRemoteGATTDescriptor::new(self.global().r(),
&self, self,
DOMString::from(uuid), DOMString::from(descriptor.uuid),
instance_id)) descriptor.instance_id))
}, },
BluetoothObjectMsg::Error { Err(error) => {
error
} => {
Err(Type(error)) Err(Type(error))
}, },
_ => unreachable!()
} }
} }
@ -126,43 +117,25 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
descriptor: Option<BluetoothDescriptorUUID>) descriptor: Option<BluetoothDescriptorUUID>)
-> Fallible<Vec<Root<BluetoothRemoteGATTDescriptor>>> { -> Fallible<Vec<Root<BluetoothRemoteGATTDescriptor>>> {
let mut uuid: Option<String> = None; let mut uuid: Option<String> = None;
if let Some(d)= descriptor { if let Some(d) = descriptor {
match BluetoothUUID::GetCharacteristic(self.global().r(), d.clone()) { uuid = Some(try!(BluetoothUUID::GetDescriptor(self.global().r(), d)).to_string())
Ok(domstring) => uuid = Some(domstring.to_string()),
Err(error) => return Err(error),
}
}; };
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
let mut descriptors: Vec<Root<BluetoothRemoteGATTDescriptor>> = vec!();
self.get_bluetooth_thread().send( self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetDescriptors(self.get_instance_id(), uuid, sender)).unwrap(); BluetoothMethodMsg::GetDescriptors(self.get_instance_id(), uuid, sender)).unwrap();
let descriptors_vec = receiver.recv().unwrap(); let descriptors_vec = receiver.recv().unwrap();
match descriptors_vec { match descriptors_vec {
BluetoothObjectMsg::BluetoothDescriptors { Ok(descriptor_vec) => {
descriptors_vec Ok(descriptor_vec.into_iter()
} => { .map(|desc| BluetoothRemoteGATTDescriptor::new(self.global().r(),
for d in descriptors_vec { self,
match d { DOMString::from(desc.uuid),
BluetoothObjectMsg::BluetoothDescriptor { desc.instance_id))
uuid, .collect())
instance_id,
} => {
descriptors.push(BluetoothRemoteGATTDescriptor::new(self.global().r(),
&self,
DOMString::from(uuid),
instance_id));
},
_ => unreachable!(),
}
}
Ok(descriptors)
}, },
BluetoothObjectMsg::Error { Err(error) => {
error
} => {
Err(Type(error)) Err(Type(error))
}, },
_ => unreachable!(),
} }
} }
@ -175,27 +148,21 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
fn ReadValue(&self) -> Fallible<ByteString> { fn ReadValue(&self) -> Fallible<ByteString> {
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) return Err(Network)
} else {
self.get_bluetooth_thread().send(
BluetoothMethodMsg::ReadValue(self.get_instance_id(), sender)).unwrap();
let result = receiver.recv().unwrap();
let value = match result {
BluetoothObjectMsg::BluetoothReadValue {
value
} => {
Some(ByteString::new(value))
},
BluetoothObjectMsg::Error {
error
} => {
return Err(Type(error))
},
_ => unreachable!()
};
*self.value.borrow_mut() = value;
Ok(self.GetValue().unwrap())
} }
self.get_bluetooth_thread().send(
BluetoothMethodMsg::ReadValue(self.get_instance_id(), sender)).unwrap();
let result = receiver.recv().unwrap();
let value = match result {
Ok(val) => {
ByteString::new(val)
},
Err(error) => {
return Err(Type(error))
},
};
*self.value.borrow_mut() = Some(value.clone());
Ok(value)
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue
@ -205,13 +172,10 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap(); BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap();
let result = receiver.recv().unwrap(); let result = receiver.recv().unwrap();
match result { match result {
BluetoothObjectMsg::BluetoothWriteValue => Ok(()), Ok(_) => Ok(()),
BluetoothObjectMsg::Error { Err(error) => {
error
} => {
Err(Type(error)) Err(Type(error))
}, },
_ => unreachable!()
} }
} }
} }

View file

@ -18,7 +18,7 @@ use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::ByteString; use dom::bindings::str::ByteString;
use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic; use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic;
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;
use util::str::DOMString; use util::str::DOMString;
// http://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattdescriptor // http://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattdescriptor
@ -89,27 +89,21 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
fn ReadValue(&self) -> Fallible<ByteString> { fn ReadValue(&self) -> Fallible<ByteString> {
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) return Err(Network)
} else {
self.get_bluetooth_thread().send(
BluetoothMethodMsg::ReadValue(self.get_instance_id(), sender)).unwrap();
let result = receiver.recv().unwrap();
let value = match result {
BluetoothObjectMsg::BluetoothReadValue {
value
} => {
Some(ByteString::new(value))
},
BluetoothObjectMsg::Error {
error
} => {
return Err(Type(error))
},
_ => unreachable!()
};
*self.value.borrow_mut() = value;
Ok(self.GetValue().unwrap())
} }
self.get_bluetooth_thread().send(
BluetoothMethodMsg::ReadValue(self.get_instance_id(), sender)).unwrap();
let result = receiver.recv().unwrap();
let value = match result {
Ok(val) => {
ByteString::new(val)
},
Err(error) => {
return Err(Type(error))
},
};
*self.value.borrow_mut() = Some(value.clone());
Ok(value)
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue
@ -119,13 +113,10 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap(); BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap();
let result = receiver.recv().unwrap(); let result = receiver.recv().unwrap();
match result { match result {
BluetoothObjectMsg::BluetoothWriteValue => Ok(()), Ok(_) => Ok(()),
BluetoothObjectMsg::Error { Err(error) => {
error
} => {
Err(Type(error)) Err(Type(error))
}, },
_ => unreachable!()
} }
} }
} }

View file

@ -14,7 +14,7 @@ use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothremotegattservice::BluetoothRemoteGATTService; use dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID}; use dom::bluetoothuuid::{BluetoothServiceUUID, 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;
use std::cell::Cell; use std::cell::Cell;
use util::str::DOMString; use util::str::DOMString;
@ -67,18 +67,13 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
BluetoothMethodMsg::GATTServerConnect(String::from(self.Device().Id()), sender)).unwrap(); BluetoothMethodMsg::GATTServerConnect(String::from(self.Device().Id()), sender)).unwrap();
let server = receiver.recv().unwrap(); let server = receiver.recv().unwrap();
match server { match server {
BluetoothObjectMsg::BluetoothServer { Ok(connected) => {
connected
} => {
self.connected.set(connected); self.connected.set(connected);
Ok(Root::from_ref(self)) Ok(Root::from_ref(self))
}, },
BluetoothObjectMsg::Error { Err(error) => {
error
} => {
Err(Type(error)) Err(Type(error))
}, },
_ => unreachable!()
} }
} }
@ -89,49 +84,34 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
BluetoothMethodMsg::GATTServerDisconnect(String::from(self.Device().Id()), sender)).unwrap(); BluetoothMethodMsg::GATTServerDisconnect(String::from(self.Device().Id()), sender)).unwrap();
let server = receiver.recv().unwrap(); let server = receiver.recv().unwrap();
match server { match server {
BluetoothObjectMsg::BluetoothServer { Ok(connected) => {
connected
} => {
self.connected.set(connected); self.connected.set(connected);
Ok(()) Ok(())
}, },
BluetoothObjectMsg::Error { Err(error) => {
error
} => {
Err(Type(error)) Err(Type(error))
}, },
_ => unreachable!()
} }
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice
fn GetPrimaryService(&self, service: BluetoothServiceUUID) -> Fallible<Root<BluetoothRemoteGATTService>> { fn GetPrimaryService(&self, service: BluetoothServiceUUID) -> Fallible<Root<BluetoothRemoteGATTService>> {
let uuid: String = match BluetoothUUID::GetService(self.global().r(), service.clone()) { let uuid = try!(BluetoothUUID::GetService(self.global().r(), service)).to_string();
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::GetPrimaryService(String::from(self.Device().Id()), uuid, 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 { Ok(service) => {
uuid,
is_primary,
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(service.uuid),
is_primary, service.is_primary,
instance_id)) service.instance_id))
}, },
BluetoothObjectMsg::Error { Err(error) => {
error
} => {
Err(Type(error)) Err(Type(error))
}, },
_ => unreachable!(),
} }
} }
@ -140,45 +120,26 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
service: Option<BluetoothServiceUUID>) service: Option<BluetoothServiceUUID>)
-> Fallible<Vec<Root<BluetoothRemoteGATTService>>> { -> Fallible<Vec<Root<BluetoothRemoteGATTService>>> {
let mut uuid: Option<String> = None; let mut uuid: Option<String> = None;
if let Some(s)= service { if let Some(s) = service {
match BluetoothUUID::GetService(self.global().r(), s.clone()) { uuid = Some(try!(BluetoothUUID::GetService(self.global().r(), s)).to_string())
Ok(domstring) => uuid = Some(domstring.to_string()),
Err(error) => return Err(error),
}
}; };
let mut services: Vec<Root<BluetoothRemoteGATTService>> = vec!();
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send( self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap(); BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap();
let services_vec = receiver.recv().unwrap(); let services_vec = receiver.recv().unwrap();
match services_vec { match services_vec {
BluetoothObjectMsg::BluetoothServices { Ok(service_vec) => {
services_vec Ok(service_vec.into_iter()
} => { .map(|service| BluetoothRemoteGATTService::new(self.global().r(),
for s in services_vec { &self.device.get(),
match s { DOMString::from(service.uuid),
BluetoothObjectMsg::BluetoothService { service.is_primary,
uuid, service.instance_id))
is_primary, .collect())
instance_id,
} => {
services.push(BluetoothRemoteGATTService::new(self.global().r(),
&self.device.get(),
DOMString::from(uuid),
is_primary,
instance_id));
},
_ => unreachable!(),
}
}
Ok(services)
}, },
BluetoothObjectMsg::Error { Err(error) => {
error
} => {
Err(Type(error)) Err(Type(error))
}, },
_ => unreachable!(),
} }
} }
} }

View file

@ -14,7 +14,7 @@ use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic; use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic;
use dom::bluetoothuuid::{BluetoothCharacteristicUUID, BluetoothUUID}; use dom::bluetoothuuid::{BluetoothCharacteristicUUID, 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;
use util::str::DOMString; use util::str::DOMString;
// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattservice // https://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattservice
@ -87,50 +87,32 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
fn GetCharacteristic(&self, fn GetCharacteristic(&self,
characteristic: BluetoothCharacteristicUUID) characteristic: BluetoothCharacteristicUUID)
-> Fallible<Root<BluetoothRemoteGATTCharacteristic>> { -> Fallible<Root<BluetoothRemoteGATTCharacteristic>> {
let uuid: String = match BluetoothUUID::GetCharacteristic(self.global().r(), characteristic.clone()) { let uuid = try!(BluetoothUUID::GetCharacteristic(self.global().r(), characteristic)).to_string();
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::GetCharacteristic(self.get_instance_id(), uuid, 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 { Ok(characteristic) => {
uuid, let properties = BluetoothCharacteristicProperties::new(self.global().r(),
instance_id, characteristic.broadcast,
broadcast, characteristic.read,
read, characteristic.write_without_response,
write_without_response, characteristic.write,
write, characteristic.notify,
notify, characteristic.indicate,
indicate, characteristic.authenticated_signed_writes,
authenticated_signed_writes, characteristic.reliable_write,
reliable_write, characteristic.writable_auxiliaries);
writable_auxiliaries,
} => {
let properties = &BluetoothCharacteristicProperties::new(self.global().r(),
broadcast,
read,
write_without_response,
write,
notify,
indicate,
authenticated_signed_writes,
reliable_write,
writable_auxiliaries);
Ok(BluetoothRemoteGATTCharacteristic::new(self.global().r(), Ok(BluetoothRemoteGATTCharacteristic::new(self.global().r(),
&self, self,
DOMString::from(uuid), DOMString::from(characteristic.uuid),
properties, &properties,
instance_id)) characteristic.instance_id))
}, },
BluetoothObjectMsg::Error { Err(error) => {
error
} => {
Err(Type(error)) Err(Type(error))
}, },
_ => unreachable!(),
} }
} }
@ -139,63 +121,38 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
characteristic: Option<BluetoothCharacteristicUUID>) characteristic: Option<BluetoothCharacteristicUUID>)
-> Fallible<Vec<Root<BluetoothRemoteGATTCharacteristic>>> { -> Fallible<Vec<Root<BluetoothRemoteGATTCharacteristic>>> {
let mut uuid: Option<String> = None; let mut uuid: Option<String> = None;
if let Some(c)= characteristic { if let Some(c) = characteristic {
match BluetoothUUID::GetCharacteristic(self.global().r(), c.clone()) { uuid = Some(try!(BluetoothUUID::GetCharacteristic(self.global().r(), c)).to_string())
Ok(domstring) => uuid = Some(domstring.to_string()),
Err(error) => return Err(error),
}
}; };
let mut characteristics: Vec<Root<BluetoothRemoteGATTCharacteristic>> = vec!(); let mut characteristics = vec!();
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send( self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetCharacteristics(self.get_instance_id(), uuid, sender)).unwrap(); BluetoothMethodMsg::GetCharacteristics(self.get_instance_id(), uuid, sender)).unwrap();
let characteristics_vec = receiver.recv().unwrap(); let characteristics_vec = receiver.recv().unwrap();
match characteristics_vec { match characteristics_vec {
BluetoothObjectMsg::BluetoothCharacteristics { Ok(characteristic_vec) => {
characteristics_vec for characteristic in characteristic_vec {
} => { let properties = BluetoothCharacteristicProperties::new(self.global().r(),
for characteristic in characteristics_vec { characteristic.broadcast,
match characteristic { characteristic.read,
BluetoothObjectMsg::BluetoothCharacteristic { characteristic.write_without_response,
uuid, characteristic.write,
instance_id, characteristic.notify,
broadcast, characteristic.indicate,
read, characteristic.authenticated_signed_writes,
write_without_response, characteristic.reliable_write,
write, characteristic.writable_auxiliaries);
notify, characteristics.push(BluetoothRemoteGATTCharacteristic::new(self.global().r(),
indicate, self,
authenticated_signed_writes, DOMString::from(characteristic.uuid),
reliable_write, &properties,
writable_auxiliaries, characteristic.instance_id));
} => {
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!(),
}
} }
Ok(characteristics) Ok(characteristics)
}, },
BluetoothObjectMsg::Error { Err(error) => {
error
} => {
Err(Type(error)) Err(Type(error))
}, },
_ => unreachable!(),
} }
} }
} }