diff --git a/components/net/bluetooth_thread.rs b/components/net/bluetooth_thread.rs index d5bd8bf58c4..14bb72f2bcf 100644 --- a/components/net/bluetooth_thread.rs +++ b/components/net/bluetooth_thread.rs @@ -9,7 +9,10 @@ use device::bluetooth::BluetoothGATTDescriptor; use device::bluetooth::BluetoothGATTService; use ipc_channel::ipc::{self, IpcReceiver, IpcSender}; 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::collections::HashMap; 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( ($cache:expr, $key:expr) => ( if $cache.contains_key($key) { @@ -351,14 +348,16 @@ impl BluetoothManager { // Methods - fn request_device(&mut self, options: RequestDeviceoptions, sender: IpcSender) { + fn request_device(&mut self, + options: RequestDeviceoptions, + sender: IpcSender>) { let mut adapter = match self.get_or_create_adapter() { 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); if devices.is_empty() { - send_error!(sender, DEVICE_ERROR); + return drop(sender.send(Err(String::from(DEVICE_ERROR)))); } let matched_devices: Vec = devices.into_iter() @@ -366,34 +365,28 @@ impl BluetoothManager { .collect(); for device in matched_devices { if let Ok(address) = device.get_address() { - let message = BluetoothObjectMsg::BluetoothDevice { - id: address, - name: device.get_name().ok(), - device_class: device.get_class().ok(), - vendor_id_source: device.get_vendor_id_source().ok(), - vendor_id: device.get_vendor_id().ok(), - product_id: device.get_product_id().ok(), - product_version: device.get_device_id().ok(), - appearance: device.get_appearance().ok(), - tx_power: match device.get_tx_power() { - Ok(p) => Some(p as i8), - Err(_) => None, - }, - rssi: match device.get_rssi() { - Ok(p) => Some(p as i8), - Err(_) => None, - }, - }; - return sender.send(message).unwrap(); + let message = Ok(BluetoothDeviceMsg { + id: address, + name: device.get_name().ok(), + device_class: device.get_class().ok(), + vendor_id_source: device.get_vendor_id_source().ok(), + vendor_id: device.get_vendor_id().ok(), + product_id: device.get_product_id().ok(), + product_version: device.get_device_id().ok(), + appearance: device.get_appearance().ok(), + tx_power: device.get_tx_power().ok().map(|p| p as i8), + rssi: device.get_rssi().ok().map(|p| p as i8), + }); + return drop(sender.send(message)); } } - 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) { + fn gatt_server_connect(&mut self, device_id: String, sender: IpcSender>) { let mut adapter = match self.get_or_create_adapter() { 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) { @@ -404,19 +397,16 @@ impl BluetoothManager { d.connect().is_ok() } }, - None => send_error!(sender, DEVICE_ERROR), + None => return drop(sender.send(Err(String::from(DEVICE_ERROR)))), }; - let message = BluetoothObjectMsg::BluetoothServer { - connected: connected - }; - sender.send(message).unwrap(); + let _ = sender.send(Ok(connected)); } - fn gatt_server_disconnect(&mut self, device_id: String, sender: IpcSender) { + fn gatt_server_disconnect(&mut self, device_id: String, sender: IpcSender>) { let mut adapter = match self.get_or_create_adapter() { 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) { @@ -427,201 +417,205 @@ impl BluetoothManager { false } }, - None => send_error!(sender, DEVICE_ERROR), + None => return drop(sender.send(Err(String::from(DEVICE_ERROR)))), }; - let message = BluetoothObjectMsg::BluetoothServer { - connected: connected - }; - sender.send(message).unwrap(); + let _ = sender.send(Ok(connected)); } - fn get_primary_service(&mut self, device_id: String, uuid: String, sender: IpcSender) { + fn get_primary_service(&mut self, + device_id: String, + uuid: String, + sender: IpcSender>) { let mut adapter = match self.get_or_create_adapter() { 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); if services.is_empty() { - send_error!(sender, PRIMARY_SERVICE_ERROR); + return drop(sender.send(Err(String::from(PRIMARY_SERVICE_ERROR)))); } for service in services { if service.is_primary().unwrap_or(false) { if let Ok(uuid) = service.get_uuid() { - let message = BluetoothObjectMsg::BluetoothService { - uuid: uuid, - is_primary: true, - instance_id: service.get_object_path(), - }; - return sender.send(message).unwrap(); + return drop(sender.send(Ok(BluetoothServiceMsg { + uuid: uuid, + is_primary: true, + instance_id: service.get_object_path(), + }))); } } } - send_error!(sender, PRIMARY_SERVICE_ERROR); + return drop(sender.send(Err(String::from(PRIMARY_SERVICE_ERROR)))); } fn get_primary_services(&mut self, device_id: String, uuid: Option, - sender: IpcSender) { + sender: IpcSender>) { let mut adapter = match self.get_or_create_adapter() { Some(a) => a, - None => send_error!(sender, ADAPTER_ERROR), + None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))), }; let services = match uuid { Some(id) => self.get_gatt_services_by_uuid(&mut adapter, &device_id, &id), None => self.get_and_cache_gatt_services(&mut adapter, &device_id), }; 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!(); for service in services { if service.is_primary().unwrap_or(false) { if let Ok(uuid) = service.get_uuid() { - services_vec.push(BluetoothObjectMsg::BluetoothService { - uuid: uuid, - is_primary: true, - instance_id: service.get_object_path(), - }); + services_vec.push(BluetoothServiceMsg { + uuid: uuid, + is_primary: true, + instance_id: service.get_object_path(), + }); } } } 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) { + fn get_characteristic(&mut self, + service_id: String, + uuid: String, + sender: IpcSender>) { let mut adapter = match self.get_or_create_adapter() { 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); if characteristics.is_empty() { - send_error!(sender, CHARACTERISTIC_ERROR); + return drop(sender.send(Err(String::from(CHARACTERISTIC_ERROR)))); } for characteristic in characteristics { if let Ok(uuid) = characteristic.get_uuid() { let properties = self.get_characteristic_properties(&characteristic); - let message = BluetoothObjectMsg::BluetoothCharacteristic { - uuid: uuid, - instance_id: characteristic.get_object_path(), - broadcast: properties.contains(BROADCAST), - read: properties.contains(READ), - write_without_response: properties.contains(WRITE_WITHOUT_RESPONSE), - write: properties.contains(WRITE), - notify: properties.contains(NOTIFY), - indicate: properties.contains(INDICATE), - authenticated_signed_writes: properties.contains(AUTHENTICATED_SIGNED_WRITES), - reliable_write: properties.contains(RELIABLE_WRITE), - writable_auxiliaries: properties.contains(WRITABLE_AUXILIARIES), - }; - return sender.send(message).unwrap(); + let message = Ok(BluetoothCharacteristicMsg { + uuid: uuid, + instance_id: characteristic.get_object_path(), + broadcast: properties.contains(BROADCAST), + read: properties.contains(READ), + write_without_response: properties.contains(WRITE_WITHOUT_RESPONSE), + write: properties.contains(WRITE), + notify: properties.contains(NOTIFY), + indicate: properties.contains(INDICATE), + authenticated_signed_writes: properties.contains(AUTHENTICATED_SIGNED_WRITES), + reliable_write: properties.contains(RELIABLE_WRITE), + writable_auxiliaries: properties.contains(WRITABLE_AUXILIARIES), + }); + return drop(sender.send(message)); } } - send_error!(sender, CHARACTERISTIC_ERROR); + return drop(sender.send(Err(String::from(CHARACTERISTIC_ERROR)))); } fn get_characteristics(&mut self, service_id: String, uuid: Option, - sender: IpcSender) { + sender: IpcSender>) { let mut adapter = match self.get_or_create_adapter() { Some(a) => a, - None => send_error!(sender, ADAPTER_ERROR), + None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))), }; let characteristics = match uuid { Some(id) => self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &id), None => self.get_and_cache_gatt_characteristics(&mut adapter, &service_id), }; if characteristics.is_empty() { - send_error!(sender, CHARACTERISTIC_ERROR); + return drop(sender.send(Err(String::from(CHARACTERISTIC_ERROR)))); } let mut characteristics_vec = vec!(); for characteristic in characteristics { if let Ok(uuid) = characteristic.get_uuid() { let properties = self.get_characteristic_properties(&characteristic); - characteristics_vec.push(BluetoothObjectMsg::BluetoothCharacteristic { - uuid: uuid, - instance_id: characteristic.get_object_path(), - broadcast: properties.contains(BROADCAST), - read: properties.contains(READ), - write_without_response: properties.contains(WRITE_WITHOUT_RESPONSE), - write: properties.contains(WRITE), - notify: properties.contains(NOTIFY), - indicate: properties.contains(INDICATE), - authenticated_signed_writes: properties.contains(AUTHENTICATED_SIGNED_WRITES), - reliable_write: properties.contains(RELIABLE_WRITE), - writable_auxiliaries: properties.contains(WRITABLE_AUXILIARIES), - }); + characteristics_vec.push( + BluetoothCharacteristicMsg { + uuid: uuid, + instance_id: characteristic.get_object_path(), + broadcast: properties.contains(BROADCAST), + read: properties.contains(READ), + write_without_response: properties.contains(WRITE_WITHOUT_RESPONSE), + write: properties.contains(WRITE), + notify: properties.contains(NOTIFY), + indicate: properties.contains(INDICATE), + authenticated_signed_writes: properties.contains(AUTHENTICATED_SIGNED_WRITES), + reliable_write: properties.contains(RELIABLE_WRITE), + writable_auxiliaries: properties.contains(WRITABLE_AUXILIARIES), + }); } } 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) { + fn get_descriptor(&mut self, + characteristic_id: String, + uuid: String, + sender: IpcSender>) { let mut adapter = match self.get_or_create_adapter() { 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); if descriptors.is_empty() { - send_error!(sender, DESCRIPTOR_ERROR); + return drop(sender.send(Err(String::from(DESCRIPTOR_ERROR)))); } for descriptor in descriptors { if let Ok(uuid) = descriptor.get_uuid() { - let message = BluetoothObjectMsg::BluetoothDescriptor { - uuid: uuid, - instance_id: descriptor.get_object_path(), - }; - return sender.send(message).unwrap(); + return drop(sender.send(Ok(BluetoothDescriptorMsg { + uuid: uuid, + instance_id: descriptor.get_object_path(), + }))); } } - send_error!(sender, DESCRIPTOR_ERROR); + return drop(sender.send(Err(String::from(DESCRIPTOR_ERROR)))); } fn get_descriptors(&mut self, characteristic_id: String, uuid: Option, - sender: IpcSender) { + sender: IpcSender>) { let mut adapter = match self.get_or_create_adapter() { Some(a) => a, - None => send_error!(sender, ADAPTER_ERROR), + None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))), }; let descriptors = match uuid { Some(id) => self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &id), None => self.get_and_cache_gatt_descriptors(&mut adapter, &characteristic_id), }; if descriptors.is_empty() { - send_error!(sender, DESCRIPTOR_ERROR); + return drop(sender.send(Err(String::from(DESCRIPTOR_ERROR)))); } let mut descriptors_vec = vec!(); for descriptor in descriptors { if let Ok(uuid) = descriptor.get_uuid() { - descriptors_vec.push(BluetoothObjectMsg::BluetoothDescriptor { - uuid: uuid, - instance_id: descriptor.get_object_path(), - }); + descriptors_vec.push(BluetoothDescriptorMsg { + uuid: uuid, + instance_id: descriptor.get_object_path(), + }); } } 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 }; - sender.send(message).unwrap(); + let _ = sender.send(Ok(descriptors_vec)); } - fn read_value(&mut self, id: String, sender: IpcSender) { + fn read_value(&mut self, id: String, sender: IpcSender>>) { let mut adapter = match self.get_or_create_adapter() { 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) .map(|c| c.read_value().unwrap_or(vec![])); @@ -629,19 +623,13 @@ impl BluetoothManager { value = self.get_gatt_descriptor(&mut adapter, &id) .map(|d| d.read_value().unwrap_or(vec![])); } - - let message = match value { - Some(v) => BluetoothObjectMsg::BluetoothReadValue { value: v }, - None => send_error!(sender, VALUE_ERROR), - }; - - sender.send(message).unwrap(); + let _ = sender.send(value.ok_or(String::from(VALUE_ERROR))); } - fn write_value(&mut self, id: String, value: Vec, sender: IpcSender) { + fn write_value(&mut self, id: String, value: Vec, sender: IpcSender>) { let mut adapter = match self.get_or_create_adapter() { 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) .map(|c| c.write_value(value.clone())); @@ -649,15 +637,13 @@ impl BluetoothManager { result = self.get_gatt_descriptor(&mut adapter, &id) .map(|d| d.write_value(value.clone())); } - let message = match result { Some(v) => match v { - Ok(_) => BluetoothObjectMsg::BluetoothWriteValue, - Err(e) => send_error!(sender, e.to_string()), + Ok(_) => Ok(true), + 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)))), }; - - sender.send(message).unwrap(); + let _ = sender.send(message); } } diff --git a/components/net_traits/bluetooth_thread.rs b/components/net_traits/bluetooth_thread.rs index 6437027e144..34019528a1e 100644 --- a/components/net_traits/bluetooth_thread.rs +++ b/components/net_traits/bluetooth_thread.rs @@ -5,78 +5,71 @@ use bluetooth_scanfilter::RequestDeviceoptions; use ipc_channel::ipc::IpcSender; #[derive(Deserialize, Serialize)] -pub enum BluetoothMethodMsg { - RequestDevice(RequestDeviceoptions, IpcSender), - GATTServerConnect(String, IpcSender), - GATTServerDisconnect(String, IpcSender), - GetPrimaryService(String, String, IpcSender), - GetPrimaryServices(String, Option, IpcSender), - GetCharacteristic(String, String, IpcSender), - GetCharacteristics(String, Option, IpcSender), - GetDescriptor(String, String, IpcSender), - GetDescriptors(String, Option, IpcSender), - ReadValue(String, IpcSender), - WriteValue(String, Vec, IpcSender), - Exit, +pub struct BluetoothDeviceMsg { + // Bluetooth Device properties + pub id: String, + pub name: Option, + pub device_class: Option, + pub vendor_id_source: Option, + pub vendor_id: Option, + pub product_id: Option, + pub product_version: Option, + // Advertisiong Data properties + pub appearance: Option, + pub tx_power: Option, + pub rssi: Option, } #[derive(Deserialize, Serialize)] -pub enum BluetoothObjectMsg { - BluetoothDevice { - // Bluetooth Device properties - id: String, - name: Option, - device_class: Option, - vendor_id_source: Option, - vendor_id: Option, - product_id: Option, - product_version: Option, - // Advertisiong Data properties - appearance: Option, - tx_power: Option, - rssi: Option - }, - BluetoothServer { - connected: bool - }, - BluetoothService { - uuid: String, - is_primary: bool, - instance_id: String - }, - BluetoothServices { - services_vec: Vec - }, - BluetoothCharacteristic { - // Characteristic - uuid: String, - instance_id: String, - // Characteristic properties - broadcast: bool, - read: bool, - write_without_response: bool, - write: bool, - notify: bool, - indicate: bool, - authenticated_signed_writes: bool, - reliable_write: bool, - writable_auxiliaries: bool - }, - BluetoothCharacteristics { - characteristics_vec: Vec - }, - BluetoothDescriptor { - uuid: String, - instance_id: String - }, - BluetoothDescriptors { - descriptors_vec: Vec, - }, - BluetoothReadValue { - value: Vec - }, - BluetoothWriteValue, - Error { - error: String - }, +pub struct BluetoothServiceMsg { + pub uuid: String, + pub is_primary: bool, + pub instance_id: String, +} + +#[derive(Deserialize, Serialize)] +pub struct BluetoothCharacteristicMsg { + // Characteristic + pub uuid: String, + pub instance_id: String, + // Characteristic properties + pub broadcast: bool, + pub read: bool, + pub write_without_response: bool, + pub write: bool, + pub notify: bool, + pub indicate: bool, + pub authenticated_signed_writes: bool, + pub reliable_write: bool, + pub writable_auxiliaries: bool, +} + +#[derive(Deserialize, Serialize)] +pub struct BluetoothDescriptorMsg { + pub uuid: String, + pub instance_id: String, +} + +pub type BluetoothServicesMsg = Vec; + +pub type BluetoothCharacteristicsMsg = Vec; + +pub type BluetoothDescriptorsMsg = Vec; + +pub type BluetoothResult = Result; + +#[derive(Deserialize, Serialize)] +pub enum BluetoothMethodMsg { + RequestDevice(RequestDeviceoptions, IpcSender>), + GATTServerConnect(String, IpcSender>), + GATTServerDisconnect(String, IpcSender>), + GetPrimaryService(String, String, IpcSender>), + GetPrimaryServices(String, Option, IpcSender>), + GetCharacteristic(String, String, IpcSender>), + GetCharacteristics(String, Option, IpcSender>), + GetDescriptor(String, String, IpcSender>), + GetDescriptors(String, Option, IpcSender>), + ReadValue(String, IpcSender>>), + WriteValue(String, Vec, IpcSender>), + Exit, } diff --git a/components/script/dom/bluetooth.rs b/components/script/dom/bluetooth.rs index 46602b4bbf3..1e1ae3b147d 100644 --- a/components/script/dom/bluetooth.rs +++ b/components/script/dom/bluetooth.rs @@ -18,7 +18,7 @@ use dom::bluetoothuuid::BluetoothUUID; use ipc_channel::ipc::{self, IpcSender}; use net_traits::bluetooth_scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence}; use net_traits::bluetooth_scanfilter::{RequestDeviceoptions, ServiceUUIDSequence}; -use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg}; +use net_traits::bluetooth_thread::BluetoothMethodMsg; use util::str::DOMString; 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 fn RequestDevice(&self, option: &RequestDeviceOptions) -> Fallible> { let (sender, receiver) = ipc::channel().unwrap(); - match try!(convert_request_device_options(option, self.global().r())) { - option => { - self.get_bluetooth_thread().send( - BluetoothMethodMsg::RequestDevice(option, sender)).unwrap(); - let device = receiver.recv().unwrap(); - match device { - BluetoothObjectMsg::BluetoothDevice { - id, - name, - device_class, - vendor_id_source, - vendor_id, - product_id, - product_version, - appearance, - tx_power, - rssi, - } => { - let ad_data = &BluetoothAdvertisingData::new(self.global().r(), - appearance, - tx_power, - rssi); - let vendor_id_source = vendor_id_source.map(|vid| match vid.as_str() { - "bluetooth" => VendorIDSource::Bluetooth, - "usb" => VendorIDSource::Usb, - _ => VendorIDSource::Unknown, - }); - 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!() - } + let option = try!(convert_request_device_options(option, self.global().r())); + self.get_bluetooth_thread().send(BluetoothMethodMsg::RequestDevice(option, sender)).unwrap(); + let device = receiver.recv().unwrap(); + match device { + Ok(device) => { + let ad_data = BluetoothAdvertisingData::new(self.global().r(), + device.appearance, + device.tx_power, + device.rssi); + let vendor_id_source = device.vendor_id_source.map(|vid| match vid.as_str() { + "bluetooth" => VendorIDSource::Bluetooth, + "usb" => VendorIDSource::Usb, + _ => VendorIDSource::Unknown, + }); + Ok(BluetoothDevice::new(self.global().r(), + DOMString::from(device.id), + device.name.map(DOMString::from), + &ad_data, + device.device_class, + vendor_id_source, + device.vendor_id, + device.product_id, + device.product_version)) + }, + Err(error) => { + Err(Type(error)) }, } } diff --git a/components/script/dom/bluetoothremotegattcharacteristic.rs b/components/script/dom/bluetoothremotegattcharacteristic.rs index 1a0e9c02010..9ae6c6596ba 100644 --- a/components/script/dom/bluetoothremotegattcharacteristic.rs +++ b/components/script/dom/bluetoothremotegattcharacteristic.rs @@ -20,7 +20,7 @@ use dom::bluetoothremotegattdescriptor::BluetoothRemoteGATTDescriptor; use dom::bluetoothremotegattservice::BluetoothRemoteGATTService; use dom::bluetoothuuid::{BluetoothDescriptorUUID, BluetoothUUID}; use ipc_channel::ipc::{self, IpcSender}; -use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg}; +use net_traits::bluetooth_thread::BluetoothMethodMsg; use util::str::DOMString; // https://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattcharacteristic @@ -94,30 +94,21 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor fn GetDescriptor(&self, descriptor: BluetoothDescriptorUUID) -> Fallible> { - let uuid: String = match BluetoothUUID::GetDescriptor(self.global().r(), descriptor.clone()) { - Ok(domstring) => domstring.to_string(), - Err(error) => return Err(error), - }; + let uuid = try!(BluetoothUUID::GetDescriptor(self.global().r(), descriptor)).to_string(); let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GetDescriptor(self.get_instance_id(), uuid, sender)).unwrap(); let descriptor = receiver.recv().unwrap(); match descriptor { - BluetoothObjectMsg::BluetoothDescriptor { - uuid, - instance_id - } => { + Ok(descriptor) => { Ok(BluetoothRemoteGATTDescriptor::new(self.global().r(), - &self, - DOMString::from(uuid), - instance_id)) + self, + DOMString::from(descriptor.uuid), + descriptor.instance_id)) }, - BluetoothObjectMsg::Error { - error - } => { + Err(error) => { Err(Type(error)) }, - _ => unreachable!() } } @@ -126,43 +117,25 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris descriptor: Option) -> Fallible>> { let mut uuid: Option = None; - if let Some(d)= descriptor { - match BluetoothUUID::GetCharacteristic(self.global().r(), d.clone()) { - Ok(domstring) => uuid = Some(domstring.to_string()), - Err(error) => return Err(error), - } + if let Some(d) = descriptor { + uuid = Some(try!(BluetoothUUID::GetDescriptor(self.global().r(), d)).to_string()) }; let (sender, receiver) = ipc::channel().unwrap(); - let mut descriptors: Vec> = vec!(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GetDescriptors(self.get_instance_id(), uuid, sender)).unwrap(); let descriptors_vec = receiver.recv().unwrap(); match descriptors_vec { - BluetoothObjectMsg::BluetoothDescriptors { - descriptors_vec - } => { - for d in descriptors_vec { - match d { - BluetoothObjectMsg::BluetoothDescriptor { - uuid, - instance_id, - } => { - descriptors.push(BluetoothRemoteGATTDescriptor::new(self.global().r(), - &self, - DOMString::from(uuid), - instance_id)); - }, - _ => unreachable!(), - } - } - Ok(descriptors) + Ok(descriptor_vec) => { + Ok(descriptor_vec.into_iter() + .map(|desc| BluetoothRemoteGATTDescriptor::new(self.global().r(), + self, + DOMString::from(desc.uuid), + desc.instance_id)) + .collect()) }, - BluetoothObjectMsg::Error { - error - } => { + Err(error) => { Err(Type(error)) }, - _ => unreachable!(), } } @@ -175,27 +148,21 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris fn ReadValue(&self) -> Fallible { let (sender, receiver) = ipc::channel().unwrap(); if !self.Service().Device().Gatt().Connected() { - 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()) + return Err(Network) } + 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 @@ -205,13 +172,10 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap(); let result = receiver.recv().unwrap(); match result { - BluetoothObjectMsg::BluetoothWriteValue => Ok(()), - BluetoothObjectMsg::Error { - error - } => { + Ok(_) => Ok(()), + Err(error) => { Err(Type(error)) }, - _ => unreachable!() } } } diff --git a/components/script/dom/bluetoothremotegattdescriptor.rs b/components/script/dom/bluetoothremotegattdescriptor.rs index edbc61f9e3a..8e5a106cd05 100644 --- a/components/script/dom/bluetoothremotegattdescriptor.rs +++ b/components/script/dom/bluetoothremotegattdescriptor.rs @@ -18,7 +18,7 @@ use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::str::ByteString; use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic; use ipc_channel::ipc::{self, IpcSender}; -use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg}; +use net_traits::bluetooth_thread::BluetoothMethodMsg; use util::str::DOMString; // http://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattdescriptor @@ -89,27 +89,21 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { fn ReadValue(&self) -> Fallible { let (sender, receiver) = ipc::channel().unwrap(); if !self.Characteristic().Service().Device().Gatt().Connected() { - 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()) + return Err(Network) } + 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 @@ -119,13 +113,10 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap(); let result = receiver.recv().unwrap(); match result { - BluetoothObjectMsg::BluetoothWriteValue => Ok(()), - BluetoothObjectMsg::Error { - error - } => { + Ok(_) => Ok(()), + Err(error) => { Err(Type(error)) }, - _ => unreachable!() } } } diff --git a/components/script/dom/bluetoothremotegattserver.rs b/components/script/dom/bluetoothremotegattserver.rs index f34261eb54b..e4f4b20a9dc 100644 --- a/components/script/dom/bluetoothremotegattserver.rs +++ b/components/script/dom/bluetoothremotegattserver.rs @@ -14,7 +14,7 @@ use dom::bluetoothdevice::BluetoothDevice; use dom::bluetoothremotegattservice::BluetoothRemoteGATTService; use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID}; use ipc_channel::ipc::{self, IpcSender}; -use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg}; +use net_traits::bluetooth_thread::BluetoothMethodMsg; use std::cell::Cell; use util::str::DOMString; @@ -67,18 +67,13 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { BluetoothMethodMsg::GATTServerConnect(String::from(self.Device().Id()), sender)).unwrap(); let server = receiver.recv().unwrap(); match server { - BluetoothObjectMsg::BluetoothServer { - connected - } => { + Ok(connected) => { self.connected.set(connected); Ok(Root::from_ref(self)) }, - BluetoothObjectMsg::Error { - error - } => { + Err(error) => { Err(Type(error)) }, - _ => unreachable!() } } @@ -89,49 +84,34 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { BluetoothMethodMsg::GATTServerDisconnect(String::from(self.Device().Id()), sender)).unwrap(); let server = receiver.recv().unwrap(); match server { - BluetoothObjectMsg::BluetoothServer { - connected - } => { + Ok(connected) => { self.connected.set(connected); Ok(()) }, - BluetoothObjectMsg::Error { - error - } => { + Err(error) => { Err(Type(error)) }, - _ => unreachable!() } } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice fn GetPrimaryService(&self, service: BluetoothServiceUUID) -> Fallible> { - let uuid: String = match BluetoothUUID::GetService(self.global().r(), service.clone()) { - Ok(domstring) => domstring.to_string(), - Err(error) => return Err(error), - }; + let uuid = try!(BluetoothUUID::GetService(self.global().r(), service)).to_string(); let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GetPrimaryService(String::from(self.Device().Id()), uuid, sender)).unwrap(); let service = receiver.recv().unwrap(); match service { - BluetoothObjectMsg::BluetoothService { - uuid, - is_primary, - instance_id, - } => { + Ok(service) => { Ok(BluetoothRemoteGATTService::new(self.global().r(), &self.device.get(), - DOMString::from(uuid), - is_primary, - instance_id)) + DOMString::from(service.uuid), + service.is_primary, + service.instance_id)) }, - BluetoothObjectMsg::Error { - error - } => { + Err(error) => { Err(Type(error)) }, - _ => unreachable!(), } } @@ -140,45 +120,26 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { service: Option) -> Fallible>> { let mut uuid: Option = None; - if let Some(s)= service { - match BluetoothUUID::GetService(self.global().r(), s.clone()) { - Ok(domstring) => uuid = Some(domstring.to_string()), - Err(error) => return Err(error), - } + if let Some(s) = service { + uuid = Some(try!(BluetoothUUID::GetService(self.global().r(), s)).to_string()) }; - let mut services: Vec> = vec!(); let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap(); let services_vec = receiver.recv().unwrap(); match services_vec { - BluetoothObjectMsg::BluetoothServices { - services_vec - } => { - for s in services_vec { - match s { - BluetoothObjectMsg::BluetoothService { - uuid, - is_primary, - instance_id, - } => { - services.push(BluetoothRemoteGATTService::new(self.global().r(), - &self.device.get(), - DOMString::from(uuid), - is_primary, - instance_id)); - }, - _ => unreachable!(), - } - } - Ok(services) + Ok(service_vec) => { + Ok(service_vec.into_iter() + .map(|service| BluetoothRemoteGATTService::new(self.global().r(), + &self.device.get(), + DOMString::from(service.uuid), + service.is_primary, + service.instance_id)) + .collect()) }, - BluetoothObjectMsg::Error { - error - } => { + Err(error) => { Err(Type(error)) }, - _ => unreachable!(), } } } diff --git a/components/script/dom/bluetoothremotegattservice.rs b/components/script/dom/bluetoothremotegattservice.rs index c8d46c29904..92a5edc2f28 100644 --- a/components/script/dom/bluetoothremotegattservice.rs +++ b/components/script/dom/bluetoothremotegattservice.rs @@ -14,7 +14,7 @@ use dom::bluetoothdevice::BluetoothDevice; use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic; use dom::bluetoothuuid::{BluetoothCharacteristicUUID, BluetoothUUID}; use ipc_channel::ipc::{self, IpcSender}; -use net_traits::bluetooth_thread::{BluetoothMethodMsg, BluetoothObjectMsg}; +use net_traits::bluetooth_thread::BluetoothMethodMsg; use util::str::DOMString; // https://webbluetoothcg.github.io/web-bluetooth/#bluetoothremotegattservice @@ -87,50 +87,32 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { fn GetCharacteristic(&self, characteristic: BluetoothCharacteristicUUID) -> Fallible> { - let uuid: String = match BluetoothUUID::GetCharacteristic(self.global().r(), characteristic.clone()) { - Ok(domstring) => domstring.to_string(), - Err(error) => return Err(error), - }; + let uuid = try!(BluetoothUUID::GetCharacteristic(self.global().r(), characteristic)).to_string(); let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), uuid, sender)).unwrap(); let characteristic = receiver.recv().unwrap(); match characteristic { - BluetoothObjectMsg::BluetoothCharacteristic { - uuid, - instance_id, - broadcast, - read, - write_without_response, - write, - notify, - indicate, - authenticated_signed_writes, - reliable_write, - writable_auxiliaries, - } => { - let properties = &BluetoothCharacteristicProperties::new(self.global().r(), - broadcast, - read, - write_without_response, - write, - notify, - indicate, - authenticated_signed_writes, - reliable_write, - writable_auxiliaries); + Ok(characteristic) => { + let properties = BluetoothCharacteristicProperties::new(self.global().r(), + characteristic.broadcast, + characteristic.read, + characteristic.write_without_response, + characteristic.write, + characteristic.notify, + characteristic.indicate, + characteristic.authenticated_signed_writes, + characteristic.reliable_write, + characteristic.writable_auxiliaries); Ok(BluetoothRemoteGATTCharacteristic::new(self.global().r(), - &self, - DOMString::from(uuid), - properties, - instance_id)) + self, + DOMString::from(characteristic.uuid), + &properties, + characteristic.instance_id)) }, - BluetoothObjectMsg::Error { - error - } => { + Err(error) => { Err(Type(error)) }, - _ => unreachable!(), } } @@ -139,63 +121,38 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { characteristic: Option) -> Fallible>> { let mut uuid: Option = None; - if let Some(c)= characteristic { - match BluetoothUUID::GetCharacteristic(self.global().r(), c.clone()) { - Ok(domstring) => uuid = Some(domstring.to_string()), - Err(error) => return Err(error), - } + if let Some(c) = characteristic { + uuid = Some(try!(BluetoothUUID::GetCharacteristic(self.global().r(), c)).to_string()) }; - let mut characteristics: Vec> = vec!(); + let mut characteristics = vec!(); let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GetCharacteristics(self.get_instance_id(), uuid, sender)).unwrap(); let characteristics_vec = receiver.recv().unwrap(); match characteristics_vec { - BluetoothObjectMsg::BluetoothCharacteristics { - characteristics_vec - } => { - for characteristic in characteristics_vec { - match characteristic { - BluetoothObjectMsg::BluetoothCharacteristic { - uuid, - instance_id, - broadcast, - read, - write_without_response, - write, - notify, - indicate, - authenticated_signed_writes, - reliable_write, - writable_auxiliaries, - } => { - let properties = &BluetoothCharacteristicProperties::new(self.global().r(), - broadcast, - read, - write_without_response, - write, - notify, - indicate, - authenticated_signed_writes, - reliable_write, - writable_auxiliaries); - characteristics.push(BluetoothRemoteGATTCharacteristic::new(self.global().r(), - &self, - DOMString::from(uuid), - properties, - instance_id)); - }, - _ => unreachable!(), - } + Ok(characteristic_vec) => { + for characteristic in characteristic_vec { + let properties = BluetoothCharacteristicProperties::new(self.global().r(), + characteristic.broadcast, + characteristic.read, + characteristic.write_without_response, + characteristic.write, + characteristic.notify, + characteristic.indicate, + characteristic.authenticated_signed_writes, + characteristic.reliable_write, + characteristic.writable_auxiliaries); + characteristics.push(BluetoothRemoteGATTCharacteristic::new(self.global().r(), + self, + DOMString::from(characteristic.uuid), + &properties, + characteristic.instance_id)); } Ok(characteristics) }, - BluetoothObjectMsg::Error { - error - } => { + Err(error) => { Err(Type(error)) }, - _ => unreachable!(), } } }