mirror of
https://github.com/servo/servo.git
synced 2025-06-30 03:53:37 +01:00
Add macro to match the adapter
This commit is contained in:
parent
b11648903b
commit
4cac7bccbf
1 changed files with 20 additions and 44 deletions
|
@ -62,6 +62,15 @@ macro_rules! return_if_cached(
|
||||||
);
|
);
|
||||||
);
|
);
|
||||||
|
|
||||||
|
macro_rules! get_adapter_or_return_error(
|
||||||
|
($bl_manager:expr, $sender:expr) => (
|
||||||
|
match $bl_manager.get_or_create_adapter() {
|
||||||
|
Some(adapter) => adapter,
|
||||||
|
None => return drop($sender.send(Err(String::from(ADAPTER_ERROR)))),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
);
|
||||||
|
|
||||||
pub trait BluetoothThreadFactory {
|
pub trait BluetoothThreadFactory {
|
||||||
fn new() -> Self;
|
fn new() -> Self;
|
||||||
}
|
}
|
||||||
|
@ -402,10 +411,7 @@ impl BluetoothManager {
|
||||||
fn request_device(&mut self,
|
fn request_device(&mut self,
|
||||||
options: RequestDeviceoptions,
|
options: RequestDeviceoptions,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothDeviceMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothDeviceMsg>>) {
|
||||||
let mut adapter = match self.get_or_create_adapter() {
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
Some(a) => a,
|
|
||||||
None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
|
|
||||||
};
|
|
||||||
if let Some(ref session) = BluetoothDiscoverySession::create_session(adapter.get_object_path()).ok() {
|
if let Some(ref session) = BluetoothDiscoverySession::create_session(adapter.get_object_path()).ok() {
|
||||||
if session.start_discovery().is_ok() {
|
if session.start_discovery().is_ok() {
|
||||||
thread::sleep(Duration::from_millis(DISCOVERY_TIMEOUT_MS));
|
thread::sleep(Duration::from_millis(DISCOVERY_TIMEOUT_MS));
|
||||||
|
@ -432,10 +438,7 @@ 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 = match self.get_or_create_adapter() {
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
Some(a) => a,
|
|
||||||
None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
|
|
||||||
};
|
|
||||||
|
|
||||||
let connected = match self.get_device(&mut adapter, &device_id) {
|
let connected = match self.get_device(&mut adapter, &device_id) {
|
||||||
Some(d) => {
|
Some(d) => {
|
||||||
|
@ -452,10 +455,7 @@ impl BluetoothManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = match self.get_or_create_adapter() {
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
Some(a) => a,
|
|
||||||
None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
|
|
||||||
};
|
|
||||||
|
|
||||||
let connected = match self.get_device(&mut adapter, &device_id) {
|
let connected = match self.get_device(&mut adapter, &device_id) {
|
||||||
Some(d) => {
|
Some(d) => {
|
||||||
|
@ -475,10 +475,7 @@ impl BluetoothManager {
|
||||||
device_id: String,
|
device_id: String,
|
||||||
uuid: String,
|
uuid: String,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothServiceMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothServiceMsg>>) {
|
||||||
let mut adapter = match self.get_or_create_adapter() {
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
Some(a) => a,
|
|
||||||
None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
|
|
||||||
};
|
|
||||||
let services = self.get_gatt_services_by_uuid(&mut adapter, &device_id, &uuid);
|
let services = self.get_gatt_services_by_uuid(&mut adapter, &device_id, &uuid);
|
||||||
if services.is_empty() {
|
if services.is_empty() {
|
||||||
return drop(sender.send(Err(String::from(PRIMARY_SERVICE_ERROR))));
|
return drop(sender.send(Err(String::from(PRIMARY_SERVICE_ERROR))));
|
||||||
|
@ -501,10 +498,7 @@ impl BluetoothManager {
|
||||||
device_id: String,
|
device_id: String,
|
||||||
uuid: Option<String>,
|
uuid: Option<String>,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothServicesMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothServicesMsg>>) {
|
||||||
let mut adapter = match self.get_or_create_adapter() {
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
Some(a) => a,
|
|
||||||
None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
|
|
||||||
};
|
|
||||||
let services = 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_and_cache_gatt_services(&mut adapter, &device_id),
|
None => self.get_and_cache_gatt_services(&mut adapter, &device_id),
|
||||||
|
@ -595,10 +589,7 @@ impl BluetoothManager {
|
||||||
service_id: String,
|
service_id: String,
|
||||||
uuid: String,
|
uuid: String,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothCharacteristicMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothCharacteristicMsg>>) {
|
||||||
let mut adapter = match self.get_or_create_adapter() {
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
Some(a) => a,
|
|
||||||
None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
|
|
||||||
};
|
|
||||||
let characteristics = self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &uuid);
|
let characteristics = self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &uuid);
|
||||||
if characteristics.is_empty() {
|
if characteristics.is_empty() {
|
||||||
return drop(sender.send(Err(String::from(CHARACTERISTIC_ERROR))));
|
return drop(sender.send(Err(String::from(CHARACTERISTIC_ERROR))));
|
||||||
|
@ -629,10 +620,7 @@ impl BluetoothManager {
|
||||||
service_id: String,
|
service_id: String,
|
||||||
uuid: Option<String>,
|
uuid: Option<String>,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothCharacteristicsMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothCharacteristicsMsg>>) {
|
||||||
let mut adapter = match self.get_or_create_adapter() {
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
Some(a) => a,
|
|
||||||
None => return drop(sender.send(Err(String::from(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_and_cache_gatt_characteristics(&mut adapter, &service_id),
|
None => self.get_and_cache_gatt_characteristics(&mut adapter, &service_id),
|
||||||
|
@ -671,10 +659,7 @@ impl BluetoothManager {
|
||||||
characteristic_id: String,
|
characteristic_id: String,
|
||||||
uuid: String,
|
uuid: String,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothDescriptorMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothDescriptorMsg>>) {
|
||||||
let mut adapter = match self.get_or_create_adapter() {
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
Some(a) => a,
|
|
||||||
None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
|
|
||||||
};
|
|
||||||
let descriptors = self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &uuid);
|
let descriptors = self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &uuid);
|
||||||
if descriptors.is_empty() {
|
if descriptors.is_empty() {
|
||||||
return drop(sender.send(Err(String::from(DESCRIPTOR_ERROR))));
|
return drop(sender.send(Err(String::from(DESCRIPTOR_ERROR))));
|
||||||
|
@ -694,10 +679,7 @@ impl BluetoothManager {
|
||||||
characteristic_id: String,
|
characteristic_id: String,
|
||||||
uuid: Option<String>,
|
uuid: Option<String>,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothDescriptorsMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothDescriptorsMsg>>) {
|
||||||
let mut adapter = match self.get_or_create_adapter() {
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
Some(a) => a,
|
|
||||||
None => return drop(sender.send(Err(String::from(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_and_cache_gatt_descriptors(&mut adapter, &characteristic_id),
|
None => self.get_and_cache_gatt_descriptors(&mut adapter, &characteristic_id),
|
||||||
|
@ -721,10 +703,7 @@ impl BluetoothManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_value(&mut self, id: String, sender: IpcSender<BluetoothResult<Vec<u8>>>) {
|
fn read_value(&mut self, id: String, sender: IpcSender<BluetoothResult<Vec<u8>>>) {
|
||||||
let mut adapter = match self.get_or_create_adapter() {
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
Some(a) => a,
|
|
||||||
None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
|
|
||||||
};
|
|
||||||
let mut value = self.get_gatt_characteristic(&mut adapter, &id)
|
let mut value = self.get_gatt_characteristic(&mut adapter, &id)
|
||||||
.map(|c| c.read_value().unwrap_or(vec![]));
|
.map(|c| c.read_value().unwrap_or(vec![]));
|
||||||
if value.is_none() {
|
if value.is_none() {
|
||||||
|
@ -735,10 +714,7 @@ impl BluetoothManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_value(&mut self, id: String, value: Vec<u8>, sender: IpcSender<BluetoothResult<bool>>) {
|
fn write_value(&mut self, id: String, value: Vec<u8>, sender: IpcSender<BluetoothResult<bool>>) {
|
||||||
let mut adapter = match self.get_or_create_adapter() {
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
Some(a) => a,
|
|
||||||
None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
|
|
||||||
};
|
|
||||||
let mut result = self.get_gatt_characteristic(&mut adapter, &id)
|
let mut result = self.get_gatt_characteristic(&mut adapter, &id)
|
||||||
.map(|c| c.write_value(value.clone()));
|
.map(|c| c.write_value(value.clone()));
|
||||||
if result.is_none() {
|
if result.is_none() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue