Fix incorrect behaviours

This commit is contained in:
Attila Dusnoki 2016-04-15 16:46:05 +02:00
parent 66fbc4ac46
commit b851045415

View file

@ -174,26 +174,6 @@ impl BluetoothManager {
None None
} }
fn get_gatt_service_by_uuid(&mut self,
adapter: &mut BluetoothAdapter,
device_id: &str,
service_uuid: &str)
-> Option<BluetoothGATTService> {
for service in self.cached_services.values() {
if service.get_uuid().unwrap_or("".to_owned()) == service_uuid {
return Some(service.clone());
}
}
// Update cache
let services = self.get_gatt_services(adapter, device_id);
for service in services {
if service.get_uuid().unwrap_or("".to_owned()) == service_uuid {
return Some(service.clone());
}
}
None
}
fn get_gatt_services_by_uuid(&mut self, fn get_gatt_services_by_uuid(&mut self,
adapter: &mut BluetoothAdapter, adapter: &mut BluetoothAdapter,
device_id: &str, device_id: &str,
@ -242,26 +222,6 @@ impl BluetoothManager {
None None
} }
fn get_gatt_characteristic_by_uuid(&mut self,
adapter: &mut BluetoothAdapter,
service_id: &str,
characteristic_uuid: &str)
-> Option<BluetoothGATTCharacteristic> {
for characteristic in self.cached_characteristics.values() {
if characteristic.get_uuid().unwrap_or("".to_owned()) == characteristic_uuid {
return Some(characteristic.clone());
}
}
// Update cache
let characteristics = self.get_gatt_characteristics(adapter, service_id);
for characteristic in characteristics {
if characteristic.get_uuid().unwrap_or("".to_owned()) == characteristic_uuid {
return Some(characteristic.clone());
}
}
None
}
fn get_gatt_characteristics_by_uuid(&mut self, fn get_gatt_characteristics_by_uuid(&mut self,
adapter: &mut BluetoothAdapter, adapter: &mut BluetoothAdapter,
service_id: &str, service_id: &str,
@ -330,26 +290,6 @@ impl BluetoothManager {
None None
} }
fn get_gatt_descriptor_by_uuid(&mut self,
adapter: &mut BluetoothAdapter,
characteristic_id: &str,
descriptor_uuid: &str)
-> Option<BluetoothGATTDescriptor> {
for descriptor in self.cached_descriptors.values() {
if descriptor.get_uuid().unwrap_or("".to_owned()) == descriptor_uuid {
return Some(descriptor.clone());
}
}
// Update cache
let descriptors = self.get_gatt_descriptors(adapter, characteristic_id);
for descriptor in descriptors {
if descriptor.get_uuid().unwrap_or("".to_owned()) == descriptor_uuid {
return Some(descriptor.clone());
}
}
None
}
fn get_gatt_descriptors_by_uuid(&mut self, fn get_gatt_descriptors_by_uuid(&mut self,
adapter: &mut BluetoothAdapter, adapter: &mut BluetoothAdapter,
characteristic_id: &str, characteristic_id: &str,
@ -379,10 +319,13 @@ impl BluetoothManager {
send_error!(sender, "No device found"); send_error!(sender, "No device found");
} }
match devices.into_iter().find(|ref d| matches_filters(d, options.get_filters())) { let matched_devices: Vec<BluetoothDevice> = devices.into_iter()
Some(device) => { .filter(|d| matches_filters(d, options.get_filters()))
.collect();
for device in matched_devices {
if let Ok(address) = device.get_address() {
let message = BluetoothObjectMsg::BluetoothDevice { let message = BluetoothObjectMsg::BluetoothDevice {
id: device.get_address().unwrap_or("".to_owned()), 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(),
@ -399,11 +342,11 @@ impl BluetoothManager {
Err(_) => None, Err(_) => None,
}, },
}; };
sender.send(message).unwrap(); return sender.send(message).unwrap();
},
None => send_error!(sender, "No device found, that matches the given options"),
} }
} }
send_error!(sender, "No device found, that matches the given options");
}
pub fn gatt_server_connect(&mut self, device_id: String, sender: IpcSender<BluetoothObjectMsg>) { pub fn gatt_server_connect(&mut self, device_id: String, sender: IpcSender<BluetoothObjectMsg>) {
let mut adapter = match self.get_adapter() { let mut adapter = match self.get_adapter() {
@ -456,19 +399,23 @@ impl BluetoothManager {
Some(a) => a, Some(a) => a,
None => send_error!(sender, "No adapter found"), None => send_error!(sender, "No adapter found"),
}; };
let service = match self.get_gatt_service_by_uuid(&mut adapter, &device_id, &uuid) { let services = self.get_gatt_services_by_uuid(&mut adapter, &device_id, &uuid);
Some(s) => s, if services.is_empty() {
None => send_error!(sender, "No primary service found")
};
if !service.is_primary().unwrap_or(false) {
send_error!(sender, "No primary service found"); send_error!(sender, "No primary service found");
} }
for service in services {
if service.is_primary().unwrap_or(false) {
if let Ok(uuid) = service.get_uuid() {
let message = BluetoothObjectMsg::BluetoothService { let message = BluetoothObjectMsg::BluetoothService {
uuid: service.get_uuid().unwrap_or("".to_owned()), uuid: uuid,
is_primary: true, is_primary: true,
instance_id: service.get_object_path(), instance_id: service.get_object_path(),
}; };
sender.send(message).unwrap(); return sender.send(message).unwrap();
}
}
}
send_error!(sender, "No primary service found");
} }
pub fn get_primary_services(&mut self, pub fn get_primary_services(&mut self,
@ -508,13 +455,15 @@ impl BluetoothManager {
Some(a) => a, Some(a) => a,
None => send_error!(sender, "No adapter found"), None => send_error!(sender, "No adapter found"),
}; };
let characteristic = match self.get_gatt_characteristic_by_uuid(&mut adapter, &service_id, &uuid) { let characteristics = self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &uuid);
Some(c) => c, if characteristics.is_empty() {
None => send_error!(sender, "No characteristic found"), send_error!(sender, "No characteristic found");
}; }
for characteristic in characteristics {
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 = BluetoothObjectMsg::BluetoothCharacteristic {
uuid: characteristic.get_uuid().unwrap_or("".to_owned()), uuid: uuid,
instance_id: characteristic.get_object_path(), instance_id: characteristic.get_object_path(),
broadcast: properties[0], broadcast: properties[0],
read: properties[1], read: properties[1],
@ -526,7 +475,10 @@ impl BluetoothManager {
reliable_write: properties[7], reliable_write: properties[7],
writable_auxiliaries: properties[8], writable_auxiliaries: properties[8],
}; };
sender.send(message).unwrap(); return sender.send(message).unwrap();
}
}
send_error!(sender, "No characteristic found");
} }
pub fn get_characteristics(&mut self, pub fn get_characteristics(&mut self,
@ -576,16 +528,20 @@ impl BluetoothManager {
Some(a) => a, Some(a) => a,
None => send_error!(sender, "No adapter found"), None => send_error!(sender, "No adapter found"),
}; };
let descriptors = self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &uuid);
let descriptor = match self.get_gatt_descriptor_by_uuid(&mut adapter, &characteristic_id, &uuid) { if descriptors.is_empty() {
Some(d) => d, send_error!(sender, "No descriptor found");
None => send_error!(sender, "No descriptor found"), }
}; for descriptor in descriptors {
if let Ok(uuid) = descriptor.get_uuid() {
let message = BluetoothObjectMsg::BluetoothDescriptor { let message = BluetoothObjectMsg::BluetoothDescriptor {
uuid: descriptor.get_uuid().unwrap_or("".to_owned()), uuid: uuid,
instance_id: descriptor.get_object_path(), instance_id: descriptor.get_object_path(),
}; };
sender.send(message).unwrap(); return sender.send(message).unwrap();
}
}
send_error!(sender, "No descriptor found");
} }
pub fn get_descriptors(&mut self, pub fn get_descriptors(&mut self,