BluetoothThread refactor

This commit is contained in:
Attila Dusnoki 2016-04-25 16:34:20 +02:00 committed by Attila Dusnoki
parent ef296b86e8
commit ecf4c942da
6 changed files with 162 additions and 159 deletions

View file

@ -18,6 +18,7 @@ msg = {path = "../msg"}
ipc-channel = {git = "https://github.com/servo/ipc-channel"} ipc-channel = {git = "https://github.com/servo/ipc-channel"}
webrender_traits = {git = "https://github.com/servo/webrender_traits"} webrender_traits = {git = "https://github.com/servo/webrender_traits"}
device = {git = "https://github.com/servo/devices"} device = {git = "https://github.com/servo/devices"}
bitflags = "0.6.0"
cookie = { version = "0.2.4", features = [ "serialize-rustc" ] } cookie = { version = "0.2.4", features = [ "serialize-rustc" ] }
flate2 = "0.2.0" flate2 = "0.2.0"
hyper = { version = "0.9", features = [ "serde-serialization" ] } hyper = { version = "0.9", features = [ "serde-serialization" ] }

View file

@ -23,13 +23,27 @@ const CHARACTERISTIC_ERROR: &'static str = "No characteristic found";
const DESCRIPTOR_ERROR: &'static str = "No descriptor found"; const DESCRIPTOR_ERROR: &'static str = "No descriptor found";
const VALUE_ERROR: &'static str = "No characteristic or descriptor found with that id"; const VALUE_ERROR: &'static str = "No characteristic or descriptor found with that id";
bitflags! {
flags Flags: u32 {
const BROADCAST = 0b000000001,
const READ = 0b000000010,
const WRITE_WITHOUT_RESPONSE = 0b000000100,
const WRITE = 0b000001000,
const NOTIFY = 0b000010000,
const INDICATE = 0b000100000,
const AUTHENTICATED_SIGNED_WRITES = 0b001000000,
const RELIABLE_WRITE = 0b010000000,
const WRITABLE_AUXILIARIES = 0b100000000,
}
}
macro_rules! send_error( macro_rules! send_error(
($sender:expr, $error:expr) => ( ($sender:expr, $error:expr) => (
return $sender.send(BluetoothObjectMsg::Error { error: String::from($error) }).unwrap(); return $sender.send(BluetoothObjectMsg::Error { error: String::from($error) }).unwrap();
); );
); );
macro_rules! check_cache( macro_rules! return_if_cached(
($cache:expr, $key:expr) => ( ($cache:expr, $key:expr) => (
if $cache.contains_key($key) { if $cache.contains_key($key) {
return $cache.get($key); return $cache.get($key);
@ -44,10 +58,7 @@ pub trait BluetoothThreadFactory {
impl BluetoothThreadFactory for IpcSender<BluetoothMethodMsg> { impl BluetoothThreadFactory for IpcSender<BluetoothMethodMsg> {
fn new() -> IpcSender<BluetoothMethodMsg> { fn new() -> IpcSender<BluetoothMethodMsg> {
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
let adapter = match BluetoothAdapter::init() { let adapter = BluetoothAdapter::init().ok();
Ok(a) => Some(a),
Err(_) => None,
};
spawn_named("BluetoothThread".to_owned(), move || { spawn_named("BluetoothThread".to_owned(), move || {
BluetoothManager::new(receiver, adapter).start(); BluetoothManager::new(receiver, adapter).start();
}); });
@ -124,8 +135,8 @@ impl BluetoothManager {
} }
fn start(&mut self) { fn start(&mut self) {
loop { while let Ok(msg) = self.receiver.recv() {
match self.receiver.recv().unwrap() { match msg {
BluetoothMethodMsg::RequestDevice(options, sender) => { BluetoothMethodMsg::RequestDevice(options, sender) => {
self.request_device(options, sender) self.request_device(options, sender)
}, },
@ -168,9 +179,9 @@ impl BluetoothManager {
// Adapter // Adapter
fn get_adapter(&mut self) -> Option<BluetoothAdapter> { fn get_or_create_adapter(&mut self) -> Option<BluetoothAdapter> {
if self.adapter.is_none() let adapter_valid = self.adapter.as_ref().map_or(false, |a| a.get_address().is_ok());
|| self.adapter.clone().unwrap().get_address().is_err() { if !adapter_valid {
self.adapter = BluetoothAdapter::init().ok(); self.adapter = BluetoothAdapter::init().ok();
} }
self.adapter.clone() self.adapter.clone()
@ -178,27 +189,29 @@ impl BluetoothManager {
// Device // Device
fn get_devices(&mut self, adapter: &mut BluetoothAdapter) -> Vec<BluetoothDevice> { fn get_and_cache_devices(&mut self, adapter: &mut BluetoothAdapter) -> Vec<BluetoothDevice> {
let devices = adapter.get_devices().unwrap_or(vec!()); let devices = adapter.get_devices().unwrap_or(vec!());
for device in &devices { for device in &devices {
if let Ok(address) = device.get_address() { if let Ok(address) = device.get_address() {
self.cached_devices.insert(address, device.clone()); self.cached_devices.insert(address, device.clone());
} }
} }
devices self.cached_devices.iter().map(|(_, d)| d.clone()).collect()
} }
fn get_device(&mut self, adapter: &mut BluetoothAdapter, device_id: &str) -> Option<&BluetoothDevice> { fn get_device(&mut self, adapter: &mut BluetoothAdapter, device_id: &str) -> Option<&BluetoothDevice> {
check_cache!(self.cached_devices, device_id); return_if_cached!(self.cached_devices, device_id);
// Update cache self.get_and_cache_devices(adapter);
self.get_devices(adapter); return_if_cached!(self.cached_devices, device_id);
check_cache!(self.cached_devices, device_id);
None None
} }
// Service // Service
fn get_gatt_services(&mut self, adapter: &mut BluetoothAdapter, device_id: &str) -> Vec<BluetoothGATTService> { fn get_and_cache_gatt_services(&mut self,
adapter: &mut BluetoothAdapter,
device_id: &str)
-> Vec<BluetoothGATTService> {
let services = match self.get_device(adapter, device_id) { let services = match self.get_device(adapter, device_id) {
Some(d) => d.get_gatt_services().unwrap_or(vec!()), Some(d) => d.get_gatt_services().unwrap_or(vec!()),
None => vec!(), None => vec!(),
@ -211,14 +224,13 @@ impl BluetoothManager {
} }
fn get_gatt_service(&mut self, adapter: &mut BluetoothAdapter, service_id: &str) -> Option<&BluetoothGATTService> { fn get_gatt_service(&mut self, adapter: &mut BluetoothAdapter, service_id: &str) -> Option<&BluetoothGATTService> {
check_cache!(self.cached_services, service_id); return_if_cached!(self.cached_services, service_id);
let device_id = match self.service_to_device.get_mut(service_id) { let device_id = match self.service_to_device.get(service_id) {
Some(d) => d.clone(), Some(d) => d.clone(),
None => return None, None => return None,
}; };
// Update cache self.get_and_cache_gatt_services(adapter, &device_id);
self.get_gatt_services(adapter, &device_id); return_if_cached!(self.cached_services, service_id);
check_cache!(self.cached_services, service_id);
None None
} }
@ -227,21 +239,13 @@ impl BluetoothManager {
device_id: &str, device_id: &str,
service_uuid: &str) service_uuid: &str)
-> Vec<BluetoothGATTService> { -> Vec<BluetoothGATTService> {
let mut services_vec: Vec<BluetoothGATTService> = vec!(); let services = self.get_and_cache_gatt_services(adapter, device_id);
let services = self.get_gatt_services(adapter, device_id); services.into_iter().filter(|s| s.get_uuid().ok() == Some(service_uuid.to_string())).collect()
for service in services {
if let Ok(uuid) = service.get_uuid() {
if uuid == service_uuid {
services_vec.push(service.clone());
}
}
}
services_vec
} }
// Characteristic // Characteristic
fn get_gatt_characteristics(&mut self, fn get_and_cache_gatt_characteristics(&mut self,
adapter: &mut BluetoothAdapter, adapter: &mut BluetoothAdapter,
service_id: &str) service_id: &str)
-> Vec<BluetoothGATTCharacteristic> { -> Vec<BluetoothGATTCharacteristic> {
@ -261,14 +265,13 @@ impl BluetoothManager {
adapter: &mut BluetoothAdapter, adapter: &mut BluetoothAdapter,
characteristic_id: &str) characteristic_id: &str)
-> Option<&BluetoothGATTCharacteristic> { -> Option<&BluetoothGATTCharacteristic> {
check_cache!(self.cached_characteristics, characteristic_id); return_if_cached!(self.cached_characteristics, characteristic_id);
let service_id = match self.characteristic_to_service.get_mut(characteristic_id) { let service_id = match self.characteristic_to_service.get(characteristic_id) {
Some(s) => s.clone(), Some(s) => s.clone(),
None => return None, None => return None,
}; };
// Update cache self.get_and_cache_gatt_characteristics(adapter, &service_id);
self.get_gatt_characteristics(adapter, &service_id); return_if_cached!(self.cached_characteristics, characteristic_id);
check_cache!(self.cached_characteristics, characteristic_id);
None None
} }
@ -277,32 +280,26 @@ impl BluetoothManager {
service_id: &str, service_id: &str,
characteristic_uuid: &str) characteristic_uuid: &str)
-> Vec<BluetoothGATTCharacteristic> { -> Vec<BluetoothGATTCharacteristic> {
let mut characteristics_vec: Vec<BluetoothGATTCharacteristic> = vec!(); let characteristics = self.get_and_cache_gatt_characteristics(adapter, service_id);
let characteristics = self.get_gatt_characteristics(adapter, service_id); characteristics.into_iter()
for characteristic in characteristics { .filter(|c| c.get_uuid().ok() == Some(characteristic_uuid.to_string()))
if let Ok(uuid) = characteristic.get_uuid() { .collect()
if uuid == characteristic_uuid {
characteristics_vec.push(characteristic.clone());
}
}
}
characteristics_vec
} }
fn get_characteristic_properties(&self, characteristic: &BluetoothGATTCharacteristic) -> [bool; 9] { fn get_characteristic_properties(&self, characteristic: &BluetoothGATTCharacteristic) -> Flags {
let mut props = [false; 9]; let mut props: Flags = Flags::empty();
let flags = characteristic.get_flags().unwrap_or(vec!()); let flags = characteristic.get_flags().unwrap_or(vec!());
for flag in flags { for flag in flags {
match flag.as_ref() { match flag.as_ref() {
"broadcast" => props[0] = true, "broadcast" => props.insert(BROADCAST),
"read" => props[1] = true, "read" => props.insert(READ),
"write_without_response" => props[2] = true, "write_without_response" => props.insert(WRITE_WITHOUT_RESPONSE),
"write" => props[3] = true, "write" => props.insert(WRITE),
"notify" => props[4] = true, "notify" => props.insert(NOTIFY),
"indicate" => props[5] = true, "indicate" => props.insert(INDICATE),
"authenticated_signed_writes" => props[6] = true, "authenticated_signed_writes" => props.insert(AUTHENTICATED_SIGNED_WRITES),
"reliable_write" => props[7] = true, "reliable_write" => props.insert(RELIABLE_WRITE),
"writable_auxiliaries" => props[8] = true, "writable_auxiliaries" => props.insert(WRITABLE_AUXILIARIES),
_ => (), _ => (),
} }
} }
@ -311,7 +308,7 @@ impl BluetoothManager {
// Descriptor // Descriptor
fn get_gatt_descriptors(&mut self, fn get_and_cache_gatt_descriptors(&mut self,
adapter: &mut BluetoothAdapter, adapter: &mut BluetoothAdapter,
characteristic_id: &str) characteristic_id: &str)
-> Vec<BluetoothGATTDescriptor> { -> Vec<BluetoothGATTDescriptor> {
@ -331,14 +328,13 @@ impl BluetoothManager {
adapter: &mut BluetoothAdapter, adapter: &mut BluetoothAdapter,
descriptor_id: &str) descriptor_id: &str)
-> Option<&BluetoothGATTDescriptor> { -> Option<&BluetoothGATTDescriptor> {
check_cache!(self.cached_descriptors, descriptor_id); return_if_cached!(self.cached_descriptors, descriptor_id);
let characteristic_id = match self.descriptor_to_characteristic.get_mut(descriptor_id) { let characteristic_id = match self.descriptor_to_characteristic.get(descriptor_id) {
Some(c) => c.clone(), Some(c) => c.clone(),
None => return None, None => return None,
}; };
// Update cache self.get_and_cache_gatt_descriptors(adapter, &characteristic_id);
self.get_gatt_descriptors(adapter, &characteristic_id); return_if_cached!(self.cached_descriptors, descriptor_id);
check_cache!(self.cached_descriptors, descriptor_id);
None None
} }
@ -347,26 +343,20 @@ impl BluetoothManager {
characteristic_id: &str, characteristic_id: &str,
descriptor_uuid: &str) descriptor_uuid: &str)
-> Vec<BluetoothGATTDescriptor> { -> Vec<BluetoothGATTDescriptor> {
let mut descriptors_vec: Vec<BluetoothGATTDescriptor> = vec!(); let descriptors = self.get_and_cache_gatt_descriptors(adapter, characteristic_id);
let descriptors = self.get_gatt_descriptors(adapter, characteristic_id); descriptors.into_iter()
for descriptor in descriptors { .filter(|d| d.get_uuid().ok() == Some(descriptor_uuid.to_string()))
if let Ok(uuid) = descriptor.get_uuid() { .collect()
if uuid == descriptor_uuid {
descriptors_vec.push(descriptor.clone());
}
}
}
descriptors_vec
} }
// Methods // Methods
fn request_device(&mut self, options: RequestDeviceoptions, sender: IpcSender<BluetoothObjectMsg>) { fn request_device(&mut self, options: RequestDeviceoptions, sender: IpcSender<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => send_error!(sender, ADAPTER_ERROR),
}; };
let devices = self.get_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); send_error!(sender, DEVICE_ERROR);
} }
@ -400,8 +390,8 @@ impl BluetoothManager {
send_error!(sender, DEVICE_MATCH_ERROR); send_error!(sender, DEVICE_MATCH_ERROR);
} }
pub fn gatt_server_connect(&mut self, device_id: String, sender: IpcSender<BluetoothObjectMsg>) { fn gatt_server_connect(&mut self, device_id: String, sender: IpcSender<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => send_error!(sender, ADAPTER_ERROR),
}; };
@ -411,7 +401,7 @@ impl BluetoothManager {
if d.is_connected().unwrap_or(false) { if d.is_connected().unwrap_or(false) {
true true
} else { } else {
!d.connect().is_err() d.connect().is_ok()
} }
}, },
None => send_error!(sender, DEVICE_ERROR), None => send_error!(sender, DEVICE_ERROR),
@ -423,8 +413,8 @@ impl BluetoothManager {
sender.send(message).unwrap(); sender.send(message).unwrap();
} }
pub fn gatt_server_disconnect(&mut self, device_id: String, sender: IpcSender<BluetoothObjectMsg>) { fn gatt_server_disconnect(&mut self, device_id: String, sender: IpcSender<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => send_error!(sender, ADAPTER_ERROR),
}; };
@ -432,7 +422,7 @@ impl BluetoothManager {
let connected = match self.get_device(&mut adapter, &device_id) { let connected = match self.get_device(&mut adapter, &device_id) {
Some(d) => { Some(d) => {
if d.is_connected().unwrap_or(false) { if d.is_connected().unwrap_or(false) {
d.disconnect().is_err() d.disconnect().is_ok()
} else { } else {
false false
} }
@ -446,8 +436,8 @@ impl BluetoothManager {
sender.send(message).unwrap(); sender.send(message).unwrap();
} }
pub 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<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => send_error!(sender, ADAPTER_ERROR),
}; };
@ -470,22 +460,22 @@ impl BluetoothManager {
send_error!(sender, PRIMARY_SERVICE_ERROR); send_error!(sender, PRIMARY_SERVICE_ERROR);
} }
pub 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<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => send_error!(sender, ADAPTER_ERROR),
}; };
let services: Vec<BluetoothGATTService> = 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_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); send_error!(sender, PRIMARY_SERVICE_ERROR);
} }
let mut services_vec: Vec<BluetoothObjectMsg> = 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() {
@ -504,8 +494,8 @@ impl BluetoothManager {
sender.send(message).unwrap(); sender.send(message).unwrap();
} }
pub 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<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => send_error!(sender, ADAPTER_ERROR),
}; };
@ -519,15 +509,15 @@ impl BluetoothManager {
let message = BluetoothObjectMsg::BluetoothCharacteristic { let message = BluetoothObjectMsg::BluetoothCharacteristic {
uuid: uuid, uuid: uuid,
instance_id: characteristic.get_object_path(), instance_id: characteristic.get_object_path(),
broadcast: properties[0], broadcast: properties.contains(BROADCAST),
read: properties[1], read: properties.contains(READ),
write_without_response: properties[2], write_without_response: properties.contains(WRITE_WITHOUT_RESPONSE),
write: properties[3], write: properties.contains(WRITE),
notify: properties[4], notify: properties.contains(NOTIFY),
indicate: properties[5], indicate: properties.contains(INDICATE),
authenticated_signed_writes: properties[6], authenticated_signed_writes: properties.contains(AUTHENTICATED_SIGNED_WRITES),
reliable_write: properties[7], reliable_write: properties.contains(RELIABLE_WRITE),
writable_auxiliaries: properties[8], writable_auxiliaries: properties.contains(WRITABLE_AUXILIARIES),
}; };
return sender.send(message).unwrap(); return sender.send(message).unwrap();
} }
@ -535,37 +525,37 @@ impl BluetoothManager {
send_error!(sender, CHARACTERISTIC_ERROR); send_error!(sender, CHARACTERISTIC_ERROR);
} }
pub 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<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => send_error!(sender, ADAPTER_ERROR),
}; };
let characteristics = match uuid { let characteristics = match uuid {
Some(id) => self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &id), Some(id) => self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &id),
None => self.get_gatt_characteristics(&mut adapter, &service_id), None => self.get_and_cache_gatt_characteristics(&mut adapter, &service_id),
}; };
if characteristics.is_empty() { if characteristics.is_empty() {
send_error!(sender, CHARACTERISTIC_ERROR); send_error!(sender, CHARACTERISTIC_ERROR);
} }
let mut characteristics_vec: Vec<BluetoothObjectMsg> = 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(BluetoothObjectMsg::BluetoothCharacteristic {
uuid: uuid, uuid: uuid,
instance_id: characteristic.get_object_path(), instance_id: characteristic.get_object_path(),
broadcast: properties[0], broadcast: properties.contains(BROADCAST),
read: properties[1], read: properties.contains(READ),
write_without_response: properties[2], write_without_response: properties.contains(WRITE_WITHOUT_RESPONSE),
write: properties[3], write: properties.contains(WRITE),
notify: properties[4], notify: properties.contains(NOTIFY),
indicate: properties[5], indicate: properties.contains(INDICATE),
authenticated_signed_writes: properties[6], authenticated_signed_writes: properties.contains(AUTHENTICATED_SIGNED_WRITES),
reliable_write: properties[7], reliable_write: properties.contains(RELIABLE_WRITE),
writable_auxiliaries: properties[8], writable_auxiliaries: properties.contains(WRITABLE_AUXILIARIES),
}); });
} }
} }
@ -576,8 +566,8 @@ impl BluetoothManager {
sender.send(message).unwrap(); sender.send(message).unwrap();
} }
pub 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<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => send_error!(sender, ADAPTER_ERROR),
}; };
@ -597,22 +587,22 @@ impl BluetoothManager {
send_error!(sender, DESCRIPTOR_ERROR); send_error!(sender, DESCRIPTOR_ERROR);
} }
pub 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<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => send_error!(sender, ADAPTER_ERROR),
}; };
let descriptors = match uuid { let descriptors = match uuid {
Some(id) => self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &id), Some(id) => self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &id),
None => self.get_gatt_descriptors(&mut adapter, &characteristic_id), None => self.get_and_cache_gatt_descriptors(&mut adapter, &characteristic_id),
}; };
if descriptors.is_empty() { if descriptors.is_empty() {
send_error!(sender, DESCRIPTOR_ERROR); send_error!(sender, DESCRIPTOR_ERROR);
} }
let mut descriptors_vec: Vec<BluetoothObjectMsg> = 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(BluetoothObjectMsg::BluetoothDescriptor {
@ -628,20 +618,16 @@ impl BluetoothManager {
sender.send(message).unwrap(); sender.send(message).unwrap();
} }
pub fn read_value(&mut self, id: String, sender: IpcSender<BluetoothObjectMsg>) { fn read_value(&mut self, id: String, sender: IpcSender<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => send_error!(sender, ADAPTER_ERROR),
}; };
let mut value = match self.get_gatt_characteristic(&mut adapter, &id) { let mut value = self.get_gatt_characteristic(&mut adapter, &id)
Some(c) => Some(c.read_value().unwrap_or(vec!())), .map(|c| c.read_value().unwrap_or(vec![]));
None => None,
};
if value.is_none() { if value.is_none() {
value = match self.get_gatt_descriptor(&mut adapter, &id) { value = self.get_gatt_descriptor(&mut adapter, &id)
Some(d) => Some(d.read_value().unwrap_or(vec!())), .map(|d| d.read_value().unwrap_or(vec![]));
None => None,
};
} }
let message = match value { let message = match value {
@ -652,20 +638,16 @@ impl BluetoothManager {
sender.send(message).unwrap(); sender.send(message).unwrap();
} }
pub 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<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_or_create_adapter() {
Some(a) => a, Some(a) => a,
None => send_error!(sender, ADAPTER_ERROR), None => send_error!(sender, ADAPTER_ERROR),
}; };
let mut result = match self.get_gatt_characteristic(&mut adapter, &id) { let mut result = self.get_gatt_characteristic(&mut adapter, &id)
Some(c) => Some(c.write_value(value.clone())), .map(|c| c.write_value(value.clone()));
None => None,
};
if result.is_none() { if result.is_none() {
result = match self.get_gatt_descriptor(&mut adapter, &id) { result = self.get_gatt_descriptor(&mut adapter, &id)
Some(d) => Some(d.write_value(value.clone())), .map(|d| d.write_value(value.clone()));
None => None,
};
} }
let message = match result { let message = match result {

View file

@ -11,6 +11,8 @@
#![deny(unsafe_code)] #![deny(unsafe_code)]
#[macro_use]
extern crate bitflags;
extern crate brotli; extern crate brotli;
extern crate cookie as cookie_rs; extern crate cookie as cookie_rs;
extern crate device; extern crate device;

View file

@ -162,6 +162,11 @@ name = "bitflags"
version = "0.6.0" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bitflags"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "block" name = "block"
version = "0.1.5" version = "0.1.5"
@ -1331,6 +1336,7 @@ dependencies = [
name = "net" name = "net"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"bitflags 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"brotli 0.3.20 (git+https://github.com/ende76/brotli-rs)", "brotli 0.3.20 (git+https://github.com/ende76/brotli-rs)",
"cookie 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "cookie 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"device 0.0.1 (git+https://github.com/dati91/devices?branch=device-api)", "device 0.0.1 (git+https://github.com/dati91/devices?branch=device-api)",

6
ports/cef/Cargo.lock generated
View file

@ -148,6 +148,11 @@ name = "bitflags"
version = "0.6.0" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bitflags"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "block" name = "block"
version = "0.1.5" version = "0.1.5"
@ -1219,6 +1224,7 @@ dependencies = [
name = "net" name = "net"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"bitflags 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"brotli 0.3.20 (git+https://github.com/ende76/brotli-rs)", "brotli 0.3.20 (git+https://github.com/ende76/brotli-rs)",
"cookie 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "cookie 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"devtools_traits 0.0.1", "devtools_traits 0.0.1",

6
ports/gonk/Cargo.lock generated
View file

@ -141,6 +141,11 @@ name = "bitflags"
version = "0.6.0" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bitflags"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "block" name = "block"
version = "0.1.5" version = "0.1.5"
@ -1202,6 +1207,7 @@ dependencies = [
name = "net" name = "net"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"bitflags 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"brotli 0.3.20 (git+https://github.com/ende76/brotli-rs)", "brotli 0.3.20 (git+https://github.com/ende76/brotli-rs)",
"cookie 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "cookie 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"devtools_traits 0.0.1", "devtools_traits 0.0.1",