Auto merge of #12260 - szeged:gattserverfunctions, r=jdm

Timeout for GATTServer's connect, disconnect methods.

<!-- Please describe your changes on the following line: -->
Added 30 seconds maximum timeout as specified in the bluetooth 4.2 https://www.bluetooth.org/DocMan/handlers/DownloadDoc.ashx?doc_id=286439 (Vol. 3, page 480).
With this the existing `bluetooth_device_disconnect.html` test will work correctly.
It will not show the `connected:true` message, before the connection established, and will not show the `connected: false` message, before not disconnected from the GATT server.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes do not require tests because, there are no Web Bluetooth test API implementation yet.

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12260)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-07-05 09:02:25 -07:00 committed by GitHub
commit 061cf054e7

View file

@ -33,6 +33,11 @@ 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";
const SECURITY_ERROR: &'static str = "The operation is insecure"; const SECURITY_ERROR: &'static str = "The operation is insecure";
const NETWORK_ERROR: &'static str = "A network error occurred";
// A transaction not completed within 30 seconds shall time out. Such a transaction shall be considered to have failed.
// https://www.bluetooth.org/DocMan/handlers/DownloadDoc.ashx?doc_id=286439 (Vol. 3, page 480)
const MAXIMUM_TRANSACTION_TIME: u8 = 30;
const CONNECTION_TIMEOUT_MS: u64 = 1000;
// The discovery session needs some time to find any nearby devices // The discovery session needs some time to find any nearby devices
const DISCOVERY_TIMEOUT_MS: u64 = 1500; const DISCOVERY_TIMEOUT_MS: u64 = 1500;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
@ -472,35 +477,43 @@ impl BluetoothManager {
fn gatt_server_connect(&mut self, device_id: String, sender: IpcSender<BluetoothResult<bool>>) { fn gatt_server_connect(&mut self, device_id: String, sender: IpcSender<BluetoothResult<bool>>) {
let mut adapter = get_adapter_or_return_error!(self, sender); let mut adapter = get_adapter_or_return_error!(self, sender);
let connected = match self.get_device(&mut adapter, &device_id) { 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) {
true return drop(sender.send(Ok(true)));
} else {
d.connect().is_ok()
} }
let _ = d.connect();
for _ in 0..MAXIMUM_TRANSACTION_TIME {
match d.is_connected().unwrap_or(false) {
true => return drop(sender.send(Ok(true))),
false => thread::sleep(Duration::from_millis(CONNECTION_TIMEOUT_MS)),
}
}
return drop(sender.send(Err(String::from(NETWORK_ERROR))));
}, },
None => return drop(sender.send(Err(String::from(DEVICE_ERROR)))), None => return drop(sender.send(Err(String::from(DEVICE_ERROR)))),
}; }
let _ = sender.send(Ok(connected));
} }
fn gatt_server_disconnect(&mut self, device_id: String, sender: IpcSender<BluetoothResult<bool>>) { fn gatt_server_disconnect(&mut self, device_id: String, sender: IpcSender<BluetoothResult<bool>>) {
let mut adapter = get_adapter_or_return_error!(self, sender); let mut adapter = get_adapter_or_return_error!(self, sender);
let connected = match self.get_device(&mut adapter, &device_id) { 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(true) {
d.disconnect().is_ok() return drop(sender.send(Ok(false)));
} else {
false
} }
let _ = d.disconnect();
for _ in 0..MAXIMUM_TRANSACTION_TIME {
match d.is_connected().unwrap_or(true) {
true => thread::sleep(Duration::from_millis(CONNECTION_TIMEOUT_MS)),
false => return drop(sender.send(Ok(false))),
}
}
return drop(sender.send(Err(String::from(NETWORK_ERROR))));
}, },
None => return drop(sender.send(Err(String::from(DEVICE_ERROR)))), None => return drop(sender.send(Err(String::from(DEVICE_ERROR)))),
}; }
let _ = sender.send(Ok(connected));
} }
fn get_primary_service(&mut self, fn get_primary_service(&mut self,