Skip to content

Commit

Permalink
Move allowedService and blocklist checks into caching functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Zakor Gyula authored and dati91 committed Dec 5, 2016
1 parent 6c3e948 commit e19f39a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions components/bluetooth/lib.rs
Expand Up @@ -424,10 +424,15 @@ impl BluetoothManager {
adapter: &mut BluetoothAdapter,
device_id: &str)
-> Vec<BluetoothGATTService> {
let services = match self.get_device(adapter, device_id) {
let mut services = match self.get_device(adapter, device_id) {
Some(d) => d.get_gatt_services().unwrap_or(vec!()),
None => vec!(),
};

services.retain(|s| !uuid_is_blocklisted(&s.get_uuid().unwrap_or(String::new()), Blocklist::All) &&
self.allowed_services
.get(device_id)
.map_or(false, |uuids| uuids.contains(&s.get_uuid().unwrap_or(String::new()))));
for service in &services {
self.cached_services.insert(service.get_id(), service.clone());
self.service_to_device.insert(service.get_id(), device_id.to_owned());
Expand Down Expand Up @@ -461,11 +466,12 @@ impl BluetoothManager {
adapter: &mut BluetoothAdapter,
service_id: &str)
-> Vec<BluetoothGATTCharacteristic> {
let characteristics = match self.get_gatt_service(adapter, service_id) {
let mut characteristics = match self.get_gatt_service(adapter, service_id) {
Some(s) => s.get_gatt_characteristics().unwrap_or(vec!()),
None => vec!(),
};

characteristics.retain(|c| !uuid_is_blocklisted(&c.get_uuid().unwrap_or(String::new()), Blocklist::All));
for characteristic in &characteristics {
self.cached_characteristics.insert(characteristic.get_id(), characteristic.clone());
self.characteristic_to_service.insert(characteristic.get_id(), service_id.to_owned());
Expand Down Expand Up @@ -524,11 +530,12 @@ impl BluetoothManager {
adapter: &mut BluetoothAdapter,
characteristic_id: &str)
-> Vec<BluetoothGATTDescriptor> {
let descriptors = match self.get_gatt_characteristic(adapter, characteristic_id) {
let mut descriptors = match self.get_gatt_characteristic(adapter, characteristic_id) {
Some(c) => c.get_gatt_descriptors().unwrap_or(vec!()),
None => vec!(),
};

descriptors.retain(|d| !uuid_is_blocklisted(&d.get_uuid().unwrap_or(String::new()), Blocklist::All));
for descriptor in &descriptors {
self.cached_descriptors.insert(descriptor.get_id(), descriptor.clone());
self.descriptor_to_characteristic.insert(descriptor.get_id(), characteristic_id.to_owned());
Expand Down Expand Up @@ -743,10 +750,6 @@ impl BluetoothManager {
}
}
}
services_vec.retain(|s| !uuid_is_blocklisted(&s.uuid, Blocklist::All) &&
self.allowed_services
.get(&device_id)
.map_or(false, |uuids| uuids.contains(&s.uuid)));

// Step 7.
if services_vec.is_empty() {
Expand Down Expand Up @@ -919,7 +922,6 @@ impl BluetoothManager {
);
}
}
characteristics_vec.retain(|c| !uuid_is_blocklisted(&c.uuid, Blocklist::All));

// Step 7.
if characteristics_vec.is_empty() {
Expand Down Expand Up @@ -987,7 +989,6 @@ impl BluetoothManager {
);
}
}
descriptors_vec.retain(|d| !uuid_is_blocklisted(&d.uuid, Blocklist::All));

// Step 7.
if descriptors_vec.is_empty() {
Expand Down

0 comments on commit e19f39a

Please sign in to comment.