Skip to content

Commit

Permalink
Return with InvalidStateError if a Bluetooth id is not cached.
Browse files Browse the repository at this point in the history
  • Loading branch information
zakorgy committed Nov 4, 2016
1 parent dae007f commit 11dbb71
Show file tree
Hide file tree
Showing 19 changed files with 28 additions and 66 deletions.
28 changes: 26 additions & 2 deletions components/bluetooth/lib.rs
Expand Up @@ -628,6 +628,9 @@ impl BluetoothManager {
device_id: String,
uuid: String,
sender: IpcSender<BluetoothResult<BluetoothServiceMsg>>) {
if !self.cached_devices.contains_key(&device_id) {
return drop(sender.send(Err(BluetoothError::InvalidState)));
}
let mut adapter = get_adapter_or_return_error!(self, sender);
if !self.allowed_services.get(&device_id).map_or(false, |s| s.contains(&uuid)) {
return drop(sender.send(Err(BluetoothError::Security)));
Expand All @@ -654,6 +657,9 @@ impl BluetoothManager {
device_id: String,
uuid: Option<String>,
sender: IpcSender<BluetoothResult<BluetoothServicesMsg>>) {
if !self.cached_devices.contains_key(&device_id) {
return drop(sender.send(Err(BluetoothError::InvalidState)));
}
let mut adapter = get_adapter_or_return_error!(self, sender);
let services = match uuid {
Some(ref id) => {
Expand Down Expand Up @@ -690,6 +696,9 @@ impl BluetoothManager {
service_id: String,
uuid: String,
sender: IpcSender<BluetoothResult<BluetoothServiceMsg>>) {
if !self.cached_services.contains_key(&service_id) {
return drop(sender.send(Err(BluetoothError::InvalidState)));
}
let mut adapter = match self.get_or_create_adapter() {
Some(a) => a,
None => return drop(sender.send(Err(BluetoothError::Type(ADAPTER_ERROR.to_string())))),
Expand Down Expand Up @@ -721,6 +730,9 @@ impl BluetoothManager {
service_id: String,
uuid: Option<String>,
sender: IpcSender<BluetoothResult<BluetoothServicesMsg>>) {
if !self.cached_services.contains_key(&service_id) {
return drop(sender.send(Err(BluetoothError::InvalidState)));
}
let mut adapter = match self.get_or_create_adapter() {
Some(a) => a,
None => return drop(sender.send(Err(BluetoothError::Type(ADAPTER_ERROR.to_string())))),
Expand Down Expand Up @@ -758,6 +770,9 @@ impl BluetoothManager {
service_id: String,
uuid: String,
sender: IpcSender<BluetoothResult<BluetoothCharacteristicMsg>>) {
if !self.cached_services.contains_key(&service_id) {
return drop(sender.send(Err(BluetoothError::InvalidState)));
}
let mut adapter = get_adapter_or_return_error!(self, sender);
let characteristics = self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &uuid);
if characteristics.is_empty() {
Expand Down Expand Up @@ -789,6 +804,9 @@ impl BluetoothManager {
service_id: String,
uuid: Option<String>,
sender: IpcSender<BluetoothResult<BluetoothCharacteristicsMsg>>) {
if !self.cached_services.contains_key(&service_id) {
return drop(sender.send(Err(BluetoothError::InvalidState)));
}
let mut adapter = get_adapter_or_return_error!(self, sender);
let characteristics = match uuid {
Some(id) => self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &id),
Expand Down Expand Up @@ -828,6 +846,9 @@ impl BluetoothManager {
characteristic_id: String,
uuid: String,
sender: IpcSender<BluetoothResult<BluetoothDescriptorMsg>>) {
if !self.cached_characteristics.contains_key(&characteristic_id) {
return drop(sender.send(Err(BluetoothError::InvalidState)));
}
let mut adapter = get_adapter_or_return_error!(self, sender);
let descriptors = self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &uuid);
if descriptors.is_empty() {
Expand All @@ -848,6 +869,9 @@ impl BluetoothManager {
characteristic_id: String,
uuid: Option<String>,
sender: IpcSender<BluetoothResult<BluetoothDescriptorsMsg>>) {
if !self.cached_characteristics.contains_key(&characteristic_id) {
return drop(sender.send(Err(BluetoothError::InvalidState)));
}
let mut adapter = get_adapter_or_return_error!(self, sender);
let descriptors = match uuid {
Some(id) => self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &id),
Expand Down Expand Up @@ -879,7 +903,7 @@ impl BluetoothManager {
value = self.get_gatt_descriptor(&mut adapter, &id)
.map(|d| d.read_value().unwrap_or(vec![]));
}
let _ = sender.send(value.ok_or(BluetoothError::NotSupported));
let _ = sender.send(value.ok_or(BluetoothError::InvalidState));
}

fn write_value(&mut self, id: String, value: Vec<u8>, sender: IpcSender<BluetoothResult<bool>>) {
Expand All @@ -895,7 +919,7 @@ impl BluetoothManager {
Ok(_) => Ok(true),
Err(_) => return drop(sender.send(Err(BluetoothError::NotSupported))),
},
None => return drop(sender.send(Err(BluetoothError::NotSupported))),
None => return drop(sender.send(Err(BluetoothError::InvalidState))),
};
let _ = sender.send(message);
}
Expand Down
1 change: 1 addition & 0 deletions components/bluetooth_traits/lib.rs
Expand Up @@ -20,6 +20,7 @@ pub enum BluetoothError {
NotFound,
NotSupported,
Security,
InvalidState,
}

#[derive(Deserialize, Serialize)]
Expand Down
1 change: 1 addition & 0 deletions components/script/dom/bluetooth.rs
Expand Up @@ -289,6 +289,7 @@ impl From<BluetoothError> for Error {
BluetoothError::NotFound => Error::NotFound,
BluetoothError::NotSupported => Error::NotSupported,
BluetoothError::Security => Error::Security,
BluetoothError::InvalidState => Error::InvalidState,
}
}
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 11dbb71

Please sign in to comment.