Skip to content
Permalink
Browse files

Refactor IPC-message sending

  • Loading branch information...
fokinv authored and dati91 committed Apr 25, 2016
1 parent ecf4c94 commit c8672ed0afafe36632f9c8ddd44b0b540fdc2a9f

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -5,78 +5,71 @@ use bluetooth_scanfilter::RequestDeviceoptions;
use ipc_channel::ipc::IpcSender;

#[derive(Deserialize, Serialize)]
pub enum BluetoothMethodMsg {
RequestDevice(RequestDeviceoptions, IpcSender<BluetoothObjectMsg>),
GATTServerConnect(String, IpcSender<BluetoothObjectMsg>),
GATTServerDisconnect(String, IpcSender<BluetoothObjectMsg>),
GetPrimaryService(String, String, IpcSender<BluetoothObjectMsg>),
GetPrimaryServices(String, Option<String>, IpcSender<BluetoothObjectMsg>),
GetCharacteristic(String, String, IpcSender<BluetoothObjectMsg>),
GetCharacteristics(String, Option<String>, IpcSender<BluetoothObjectMsg>),
GetDescriptor(String, String, IpcSender<BluetoothObjectMsg>),
GetDescriptors(String, Option<String>, IpcSender<BluetoothObjectMsg>),
ReadValue(String, IpcSender<BluetoothObjectMsg>),
WriteValue(String, Vec<u8>, IpcSender<BluetoothObjectMsg>),
Exit,
pub struct BluetoothDeviceMsg {
// Bluetooth Device properties
pub id: String,
pub name: Option<String>,
pub device_class: Option<u32>,
pub vendor_id_source: Option<String>,
pub vendor_id: Option<u32>,
pub product_id: Option<u32>,
pub product_version: Option<u32>,
// Advertisiong Data properties
pub appearance: Option<u16>,
pub tx_power: Option<i8>,
pub rssi: Option<i8>,
}

#[derive(Deserialize, Serialize)]
pub struct BluetoothServiceMsg {
pub uuid: String,
pub is_primary: bool,
pub instance_id: String,
}

#[derive(Deserialize, Serialize)]
pub enum BluetoothObjectMsg {
BluetoothDevice {
// Bluetooth Device properties
id: String,
name: Option<String>,
device_class: Option<u32>,
vendor_id_source: Option<String>,
vendor_id: Option<u32>,
product_id: Option<u32>,
product_version: Option<u32>,
// Advertisiong Data properties
appearance: Option<u16>,
tx_power: Option<i8>,
rssi: Option<i8>
},
BluetoothServer {
connected: bool
},
BluetoothService {
uuid: String,
is_primary: bool,
instance_id: String
},
BluetoothServices {
services_vec: Vec<BluetoothObjectMsg>
},
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<BluetoothObjectMsg>
},
BluetoothDescriptor {
uuid: String,
instance_id: String
},
BluetoothDescriptors {
descriptors_vec: Vec<BluetoothObjectMsg>,
},
BluetoothReadValue {
value: Vec<u8>
},
BluetoothWriteValue,
Error {
error: String
},
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<BluetoothServiceMsg>;

pub type BluetoothCharacteristicsMsg = Vec<BluetoothCharacteristicMsg>;

pub type BluetoothDescriptorsMsg = Vec<BluetoothDescriptorMsg>;

pub type BluetoothResult<T> = Result<T, String>;

#[derive(Deserialize, Serialize)]
pub enum BluetoothMethodMsg {
RequestDevice(RequestDeviceoptions, IpcSender<BluetoothResult<BluetoothDeviceMsg>>),
GATTServerConnect(String, IpcSender<BluetoothResult<bool>>),
GATTServerDisconnect(String, IpcSender<BluetoothResult<bool>>),
GetPrimaryService(String, String, IpcSender<BluetoothResult<BluetoothServiceMsg>>),
GetPrimaryServices(String, Option<String>, IpcSender<BluetoothResult<BluetoothServicesMsg>>),
GetCharacteristic(String, String, IpcSender<BluetoothResult<BluetoothCharacteristicMsg>>),
GetCharacteristics(String, Option<String>, IpcSender<BluetoothResult<BluetoothCharacteristicsMsg>>),
GetDescriptor(String, String, IpcSender<BluetoothResult<BluetoothDescriptorMsg>>),
GetDescriptors(String, Option<String>, IpcSender<BluetoothResult<BluetoothDescriptorsMsg>>),
ReadValue(String, IpcSender<BluetoothResult<Vec<u8>>>),
WriteValue(String, Vec<u8>, IpcSender<BluetoothResult<bool>>),
Exit,
}
@@ -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<Root<BluetoothDevice>> {
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))
},
}
}
@@ -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<Root<BluetoothRemoteGATTDescriptor>> {
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<BluetoothDescriptorUUID>)
-> Fallible<Vec<Root<BluetoothRemoteGATTDescriptor>>> {
let mut uuid: Option<String> = 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<Root<BluetoothRemoteGATTDescriptor>> = vec!();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetDescriptors(self.get_instance_id(), uuid, sender)).unwrap();
let descriptors_vec = receiver.recv().unwrap();
match descriptors_vec {
BluetoothObjectMsg::BluetoothDescriptors {
descriptors_vec
} => {
for d in descriptors_vec {
match d {
BluetoothObjectMsg::BluetoothDescriptor {
uuid,
instance_id,
} => {
descriptors.push(BluetoothRemoteGATTDescriptor::new(self.global().r(),
&self,
DOMString::from(uuid),
instance_id));
},
_ => unreachable!(),
}
}
Ok(descriptors)
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<ByteString> {
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!()
}
}
}
Oops, something went wrong.

0 comments on commit c8672ed

Please sign in to comment.
You can’t perform that action at this time.