From 980ee5336af0e5d2eb524043a7fe2514bb669f06 Mon Sep 17 00:00:00 2001 From: Sruthi Kannan <35319907+Sruthi-Kannan@users.noreply.github.com> Date: Mon, 1 Apr 2019 21:46:23 -0400 Subject: [PATCH] Update adapter.rs Fixed minor errors Fixed compiler errors for trait BluetoothAdapter Fixed errors in Mac struct of BluetoothAdapter Added bluetooth-test functionality Added bluetooth test as you suggested. Could you check if it is fine? Removed enum type BluetoothAdapter Removed create_session and create_device functions Fixed issues in bluetooth-test Also made changes to APIs that take adapter as a parameter Implemented trait for EmptyAdapter Renamed struct from BluetoothAdapter to EmptyAdapter and implemented trait BluetoothAdapter for the same. This was done to make fn new() work in adapter.rs(for Empty) Fixed errors in few methods Made changes to fn new(), fn new_mock() and methods that use self.0.clone() Moved create_mock_device to adapter.rs Reverted changes in empty.rs Modified fn new() for Empty Modified fn new() for Mac, Android and Bluez Squashed 2 commits Squashed 2 more commits Squashed 7 commits Squashed 14 commits Squashed 16 commits --- src/adapter.rs | 450 +++++++++++++++++++++++++++++++++++++++-------- src/bluetooth.rs | 307 +------------------------------- src/empty.rs | 14 +- 3 files changed, 381 insertions(+), 390 deletions(-) diff --git a/src/adapter.rs b/src/adapter.rs index 5fcf4cf..90aed9d 100644 --- a/src/adapter.rs +++ b/src/adapter.rs @@ -7,10 +7,36 @@ use blurmac::BluetoothAdapter as BluetoothAdapterMac; #[cfg(not(any(all(target_os = "linux", feature = "bluetooth"), all(target_os = "android", feature = "bluetooth"), all(target_os = "macos", feature = "bluetooth"))))] -use empty::BluetoothAdapter as BluetoothAdapterEmpty; +use empty::EmptyAdapter as BluetoothAdapterEmpty; #[cfg(feature = "bluetooth-test")] use blurmock::fake_adapter::FakeBluetoothAdapter; +#[cfg(all(target_os = "linux", feature = "bluetooth"))] +use blurz::bluetooth_discovery_session::BluetoothDiscoverySession as BluetoothDiscoverySessionBluez; +#[cfg(all(target_os = "android", feature = "bluetooth"))] +use blurdroid::bluetooth_discovery_session::DiscoverySession as BluetoothDiscoverySessionAndroid; +#[cfg(all(target_os = "macos", feature = "bluetooth"))] +use blurmac::BluetoothDiscoverySession as BluetoothDiscoverySessionMac; +#[cfg(not(any(all(target_os = "linux", feature = "bluetooth"), + all(target_os = "android", feature = "bluetooth"), + all(target_os = "macos", feature = "bluetooth"))))] +use empty::BluetoothDiscoverySession as BluetoothDiscoverySessionEmpty; +#[cfg(feature = "bluetooth-test")] +use blurmock::fake_discovery_session::FakeBluetoothDiscoverySession; + +#[cfg(all(target_os = "linux", feature = "bluetooth"))] +use blurz::bluetooth_device::BluetoothDevice as BluetoothDeviceBluez; +#[cfg(all(target_os = "android", feature = "bluetooth"))] +use blurdroid::bluetooth_device::Device as BluetoothDeviceAndroid; +#[cfg(all(target_os = "macos", feature = "bluetooth"))] +use blurmac::BluetoothDevice as BluetoothDeviceMac; +#[cfg(not(any(all(target_os = "linux", feature = "bluetooth"), + all(target_os = "android", feature = "bluetooth"), + all(target_os = "macos", feature = "bluetooth"))))] +use empty::BluetoothDevice as BluetoothDeviceEmpty; +#[cfg(feature = "bluetooth-test")] +use blurmock::fake_device::FakeBluetoothDevice; + #[cfg(feature = "bluetooth-test")] const NOT_SUPPORTED_ON_REAL_ERROR: &'static str = "Error! Test functions are not supported on real devices!"; #[cfg(feature = "bluetooth-test")] @@ -19,48 +45,142 @@ const NOT_SUPPORTED_ON_MOCK_ERROR: &'static str = "Error! The first parameter mu use std::collections::HashMap; use std::sync::Arc; use std::error::Error; -use bluetooth::BluetoothDiscoverySession; +const NOT_SUPPORTED_ERROR: &'static str = "Error! Not supported platform!"; + use bluetooth::BluetoothDevice; +use bluetooth::BluetoothDiscoverySession; pub trait BluetoothAdapter { - fn new()-> Result, Box>; fn get_id(&self)-> String; + fn set_id(&self, id: String)-> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } fn get_devices(&self)-> Result, Box>; fn get_device(&self, address: String) -> Result, Box>; + fn create_mock_device(&self, device: String) -> Result> { + Err(Box::from(NOT_SUPPORTED_ERROR)) + } fn get_address(&self) -> Result>; + fn set_address(&self, address: String) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } fn get_name(&self)-> Result>; + fn set_name(&self, name: String) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } fn get_alias(&self) -> Result>; + fn set_alias(&self, alias: String) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } fn get_class(&self)-> Result>; + fn set_class(&self, class: u32) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } fn is_powered(&self)-> Result>; + fn set_powered(&self, powered: bool) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } + fn is_present(&self) -> Result>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } + fn set_present(&self, present: bool) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } fn is_discoverable(&self) -> Result>; + fn set_discoverable(&self, discoverable: bool) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } fn is_pairable(&self)-> Result>; + fn set_pairable(&self, pairable: bool) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } fn get_pairable_timeout(&self) -> Result>; + fn set_pairable_timeout(&self, timeout: u32) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } fn get_discoverable_timeout(&self)-> Result>; + fn set_discoverable_timeout(&self, timeout: u32) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } fn is_discovering(&self)-> Result>; - fn create_discovery_session(&self) -> Result> ; + fn set_discovering(&self, discovering: bool) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } + fn set_can_start_discovery(&self, can_start_discovery: bool) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } + fn set_can_stop_discovery(&self, can_stop_discovery: bool) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } + fn create_discovery_session(&self)-> Result>; fn get_uuids(&self)-> Result, Box>; + fn set_uuids(&self, uuids: Vec) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } fn get_vendor_id_source(&self)-> Result>; fn get_vendor_id(&self)-> Result>; fn get_product_id(&self) -> Result> ; fn get_device_id(&self) -> Result>; fn get_modalias(&self) -> Result<(String, u32, u32, u32), Box>; + fn set_modalias(&self, modalias: String) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } + fn get_ad_datas(&self) -> Result, Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } + fn set_ad_datas(&self, ad_datas: Vec) -> Result<(), Box>{ + Err(Box::from(NOT_SUPPORTED_ERROR)) + } } -pub struct Bluez(Arc); -impl BluetoothAdapter for Bluez{ +impl BluetoothAdapter{ + #[cfg(all(target_os = "linux", feature = "bluetooth"))] + pub fn new() -> Result, Box> { + let bluez_adapter = try!(BluetoothAdapterBluez::init()); + Ok(Box::new(Bluez(Arc::new(bluez_adapter)))) + } + + #[cfg(all(target_os = "android", feature = "bluetooth"))] + pub fn new() -> Result, Box> { + let blurdroid_adapter = try!(BluetoothAdapterAndroid::get_adapter()); + Ok(Box::new(Android(Arc::new(blurdroid_adapter)))) + } - fn new() -> Result, Box> { - let bluez_adapter = try!(BluetoothAdapter::new()); - Ok(BluetoothAdapter::Bluez(Arc::new(bluez_adapter))) + #[cfg(all(target_os = "macos", feature = "bluetooth"))] + pub fn new() -> Result, Box> { + let mac_adapter = try!(BluetoothAdapterMac::init()); + Ok(Box::new(Mac(Arc::new(mac_adapter)))) } + #[cfg(not(any(all(target_os = "linux", feature = "bluetooth"), + all(target_os = "android", feature = "bluetooth"), + all(target_os = "macos", feature = "bluetooth"))))] + pub fn new() -> Result, Box> { + let adapter = try!(BluetoothAdapterEmpty::init()); + Ok(Box::new(Empty(Arc::new(adapter)))) + } + + #[cfg(feature = "bluetooth-test")] + pub fn new_mock() -> Result, Box> { + let fake_adapter = Mock(FakeBluetoothAdapter::new_empty()); + Ok(Box::new(fake_adapter)) + } +} + +#[derive(Clone, Debug)] +#[cfg(all(target_os = "linux", feature = "bluetooth"))] +struct Bluez(Arc); + +#[cfg(all(target_os = "linux", feature = "bluetooth"))] +impl BluetoothAdapter for Bluez{ + fn get_id(&self) -> String{ self.0.get_id() - } + } fn get_devices(&self) -> Result, Box>{ let device_list = try!(self.0.get_device_list()); - Ok(device_list.into_iter().map(|device| BluetoothDevice::create_device(self.clone(), device)).collect()) + Ok(device_list.into_iter().map(|device| BluetoothDevice::Bluez(Arc::new(BluetoothDeviceBluez::new(device)))).collect()) } fn get_device(&self, address: String) -> Result, Box> { @@ -81,12 +201,10 @@ impl BluetoothAdapter for Bluez{ self.0.get_name() } - fn get_alias(&self) -> Result> { self.0.get_alias() } - fn get_class(&self) -> Result> { self.0.get_class() } @@ -95,39 +213,34 @@ impl BluetoothAdapter for Bluez{ self.0.is_powered() } - fn is_discoverable(&self) -> Result> { self.0.is_discoverable() } - fn is_pairable(&self) -> Result> { self.0.is_pairable() } - fn get_pairable_timeout(&self) -> Result> { self.0.get_pairable_timeout() } fn get_discoverable_timeout(&self) -> Result> { self.0.get_discoverable_timeout() - } - + } fn is_discovering(&self) -> Result> { self.0.is_discovering() } - fn create_discovery_session(&self) -> Result> { - BluetoothDiscoverySession::create_session(self.clone()) + let bluez_session = try!(BluetoothDiscoverySessionBluez::create_session(self.get_id())); + Ok(BluetoothDiscoverySession::Bluez(Arc::new(bluez_session))) } fn get_uuids(&self) -> Result, Box> { self.0.get_uuids() } - fn get_vendor_id_source(&self) -> Result> { self.0.get_vendor_id_source() } @@ -147,24 +260,24 @@ impl BluetoothAdapter for Bluez{ fn get_modalias(&self) -> Result<(String, u32, u32, u32), Box> { self.0.get_modalias() } -} + fn set_modalias(&self, modalias: String) -> Result<(), Box>{ + self.0.set_modalias(modalias) + } -pub struct Android(Arc); +} +#[cfg(all(target_os = "android", feature = "bluetooth"))] +struct Android(Arc); +#[cfg(all(target_os = "android", feature = "bluetooth"))] impl BluetoothAdapter for Android{ - fn new() -> Result, Box> { - let blurdroid_adapter = try!(BluetoothAdapter::get_adapter()); - Ok(BluetoothAdapter::Android(Arc::new(blurdroid_adapter))) - } - fn get_id(&self) -> String{ self.0.get_id() } - + fn get_devices(&self) -> Result, Box>{ let device_list = try!(self.0.get_device_list()); - Ok(device_list.into_iter().map(|device| BluetoothDevice::create_device(self.clone(), device)).collect()) + Ok(device_list.into_iter().map(|device| BluetoothDevice::Android(Arc::new(BluetoothDeviceAndroid::new(self.0.clone(), device)))).collect()) } fn get_device(&self, address: String) -> Result, Box> { @@ -177,20 +290,18 @@ impl BluetoothAdapter for Android{ Ok(None) } - fn get_address(&self) -> Result> { + fn get_address(&self) -> Result> { self.0.get_address() } fn get_name(&self) -> Result> { self.0.get_name() } - + fn get_alias(&self) -> Result> { self.0.get_alias() } - - fn get_class(&self) -> Result> { self.0.get_class() } @@ -199,17 +310,14 @@ impl BluetoothAdapter for Android{ self.0.is_powered() } - fn is_discoverable(&self) -> Result> { self.0.is_discoverable() } - fn is_pairable(&self) -> Result> { self.0.is_pairable() } - fn get_pairable_timeout(&self) -> Result> { self.0.get_pairable_timeout() } @@ -217,21 +325,20 @@ impl BluetoothAdapter for Android{ fn get_discoverable_timeout(&self) -> Result> { self.0.get_discoverable_timeout() } - fn is_discovering(&self) -> Result> { self.0.is_discovering() } - fn create_discovery_session(&self) -> Result> { - BluetoothDiscoverySession::create_session(self.clone()) + let blurdroid_session = try!(BluetoothDiscoverySessionAndroid::create_session(self.0.clone())); + Ok(BluetoothDiscoverySession::Android(Arc::new(blurdroid_session))) } + fn get_uuids(&self) -> Result, Box> { self.0.get_uuids() } - - + fn get_vendor_id_source(&self) -> Result> { self.0.get_vendor_id_source() } @@ -251,27 +358,129 @@ impl BluetoothAdapter for Android{ fn get_modalias(&self) -> Result<(String, u32, u32, u32), Box> { self.0.get_modalias() } -} +} -pub struct Mac(Arc); +#[cfg(all(target_os = "macos", feature = "bluetooth"))] +struct Mac(Arc); +#[cfg(all(target_os = "macos", feature = "bluetooth"))] impl BluetoothAdapter for Mac{ - fn new() -> Result, Box> { - let mac_adapter = try!(BluetoothAdapter::new()); - Ok(BluetoothAdapter::Mac(Arc::new(mac_adapter))) + fn get_id(&self) -> String{ + self.0.get_id() + + } + fn get_devices(&self) -> Result, Box>{ + let device_list = try!(self.0.get_device_list()); + Ok(device_list.into_iter().map(|device| BluetoothDevice::Mac(Arc::new(BluetoothDeviceMac::new(self.0.clone(),device)))).collect()) + } + + + fn get_device(&self, address: String) -> Result, Box> { + let devices = try!(self.get_devices()); + for device in devices { + if try!(device.get_address()) == address { + return Ok(Some(device)); + } + } + Ok(None) + } + + fn get_address(&self) -> Result> { + self.0.get_address() + } + + fn get_name(&self) -> Result> { + self.0.get_name() + } + fn get_alias(&self) -> Result> { + self.0.get_alias() + } + fn get_class(&self) -> Result> { + self.0.get_class() + } + + fn is_powered(&self) -> Result> { + self.0.is_powered() + } + + fn is_discoverable(&self) -> Result> { + self.0.is_discoverable() + } + + fn is_pairable(&self) -> Result> { + self.0.is_pairable() + } + + fn get_pairable_timeout(&self) -> Result> { + self.0.get_pairable_timeout() + } + fn get_discoverable_timeout(&self) -> Result> { + self.0.get_discoverable_timeout() + } + + fn is_discovering(&self) -> Result> { + self.0.is_discovering() + } + + fn create_discovery_session(&self) -> Result> { + let mac_session = BluetoothDiscoverySessionMac{}; + Ok(BluetoothDiscoverySession::Mac(Arc::new(mac_session))) + } + fn get_uuids(&self) -> Result, Box> { + self.0.get_uuids() + } + + fn get_vendor_id_source(&self) -> Result> { + self.0.get_vendor_id_source() + } + + fn get_vendor_id(&self) -> Result> { + self.0.get_vendor_id() + } + + fn get_product_id(&self) -> Result> { + self.0.get_product_id() } + fn get_device_id(&self) -> Result> { + self.0.get_device_id() + } + + fn get_modalias(&self) -> Result<(String, u32, u32, u32), Box> { + self.0.get_modalias() + } +} +#[cfg(not(any(all(target_os = "linux", feature = "bluetooth"), + all(target_os = "android", feature = "bluetooth"), + all(target_os = "macos", feature = "bluetooth"))))] +struct Empty(Arc); + +#[cfg(not(any(all(target_os = "linux", feature = "bluetooth"), + all(target_os = "android", feature = "bluetooth"), + all(target_os = "macos", feature = "bluetooth"))))] +impl BluetoothAdapter for Empty{ + fn get_id(&self) -> String{ self.0.get_id() } + fn get_devices(&self) -> Result, Box>{ let device_list = try!(self.0.get_device_list()); - Ok(device_list.into_iter().map(|device| BluetoothDevice::create_device(self.clone(), device)).collect()) + Ok(device_list.into_iter().map(|device| BluetoothDevice::Empty(Arc::new(BluetoothDeviceEmpty::new(device)))).collect()) } fn get_device(&self, address: String) -> Result, Box> { + let devices = try!(self.get_devices()); + for device in devices { + if try!(device.get_address()) == address { + return Ok(Some(device)); + } + } + Ok(None) + } + fn get_address(&self) -> Result> { self.0.get_address() } @@ -280,7 +489,6 @@ impl BluetoothAdapter for Mac{ self.0.get_name() } - fn get_alias(&self) -> Result> { self.0.get_alias() } @@ -289,44 +497,53 @@ impl BluetoothAdapter for Mac{ fn get_class(&self) -> Result> { self.0.get_class() } + fn is_powered(&self) -> Result> { self.0.is_powered() } - + fn is_discoverable(&self) -> Result> { self.0.is_discoverable() } + fn set_discoverable(&self, discoverable: bool) -> Result<(), Box>{ + self.0.set_discoverable(discoverable) + } fn is_pairable(&self) -> Result> { self.0.is_pairable() } + fn get_pairable_timeout(&self) -> Result> { self.0.get_pairable_timeout() } + fn get_discoverable_timeout(&self) -> Result> { self.0.get_discoverable_timeout() } + fn set_discoverable_timeout(&self, timeout: u32) -> Result<(), Box>{ + self.0.set_discoverable_timeout(timeout) + } fn is_discovering(&self) -> Result> { self.0.is_discovering() } - - + fn create_discovery_session(&self) -> Result> { - BluetoothDiscoverySession::create_session(self.clone()) + let empty_session = BluetoothDiscoverySessionEmpty{}; + Ok(BluetoothDiscoverySession::Empty(Arc::new(empty_session))) + } fn get_uuids(&self) -> Result, Box> { self.0.get_uuids() } - - + fn get_vendor_id_source(&self) -> Result> { self.0.get_vendor_id_source() } @@ -346,23 +563,27 @@ impl BluetoothAdapter for Mac{ fn get_modalias(&self) -> Result<(String, u32, u32, u32), Box> { self.0.get_modalias() } -} -pub struct Empty(Arc); +} -impl BluetoothAdapter for Empty{ - fn new(&self) -> Result, Box> { - let adapter = try!(BluetoothAdapter::init()); - Ok(BluetoothAdapter::Empty(Arc::new(adapter))) - } - fn get_id(&self) -> String{ +#[cfg(feature = "bluetooth-test")] +struct Mock(Arc); + +#[cfg(feature = "bluetooth-test")] +impl BluetoothAdapter for Mock{ + + fn get_id(&self) -> String { self.0.get_id() } - fn get_devices(&self) -> Result, Box>{ + fn set_id(&self, id: String) -> Result<(), Box> { + Ok(self.0.set_id(id)) + } + + fn get_devices(&self) -> Result, Box> { let device_list = try!(self.0.get_device_list()); - Ok(device_list.into_iter().map(|device| BluetoothDevice::create_device(self.clone(), device)).collect()) + Ok(device_list.into_iter().map(|device| BluetoothDevice::Mock(FakeBluetoothDevice::new_empty(self.0.clone(), device))).collect()) } fn get_device(&self, address: String) -> Result, Box> { @@ -375,60 +596,121 @@ impl BluetoothAdapter for Empty{ Ok(None) } + fn create_mock_device(&self, device: String) -> Result> { + Ok(BluetoothDevice::Mock(FakeBluetoothDevice::new_empty(self.0.clone(), device))) + + } + fn get_address(&self) -> Result> { - self.0.get_address() + self.0.get_address() + } + + fn set_address(&self, address: String) -> Result<(), Box> { + self.0.set_address(address) } fn get_name(&self) -> Result> { - self.0.get_name() + self.0.get_name() } + fn set_name(&self, name: String) -> Result<(), Box> { + self.0.set_name(name) + } fn get_alias(&self) -> Result> { - self.0.get_alias() + self.0.get_alias() } + fn set_alias(&self, alias: String) -> Result<(), Box> { + self.0.set_alias(alias) + } fn get_class(&self) -> Result> { self.0.get_class() } + fn set_class(&self, class: u32) -> Result<(), Box> { + self.0.set_class(class) + } + fn is_powered(&self) -> Result> { - self.0.is_powered() + self.0.is_powered() + } + + fn set_powered(&self, powered: bool) -> Result<(), Box> { + self.0.set_powered(powered) + } + + fn is_present(&self) -> Result> { + self.0.is_present() } + fn set_present(&self, present: bool) -> Result<(), Box> { + self.0.set_present(present) + } fn is_discoverable(&self) -> Result> { - self.0.is_discoverable() + self.0.is_discoverable() } + + fn set_discoverable(&self, discoverable: bool) -> Result<(), Box> { + self.0.set_discoverable(discoverable) + } fn is_pairable(&self) -> Result> { - self.0.is_pairable() + self.0.is_pairable() } + fn set_pairable(&self, pairable: bool) -> Result<(), Box> { + self.0.set_pairable(pairable) + } fn get_pairable_timeout(&self) -> Result> { - self.0.get_pairable_timeout() + self.0.get_pairable_timeout() + } + + fn set_pairable_timeout(&self, timeout: u32) -> Result<(), Box> { + self.0.set_pairable_timeout(timeout) } fn get_discoverable_timeout(&self) -> Result> { - self.0.get_discoverable_timeout() + self.0.get_discoverable_timeout() + } + + fn set_discoverable_timeout(&self, timeout: u32) -> Result<(), Box> { + self.0.set_discoverable_timeout(timeout) } fn is_discovering(&self) -> Result> { - self.0.is_discovering() + self.0.is_discovering() + } + + fn set_discovering(&self, discovering: bool) -> Result<(), Box> { + self.0.set_discovering(discovering) } + fn set_can_start_discovery(&self, can_start_discovery: bool) -> Result<(), Box> { + self.0.set_can_start_discovery(can_start_discovery) + } + + fn set_can_stop_discovery(&self, can_stop_discovery: bool) -> Result<(), Box> { + self.0.set_can_stop_discovery(can_stop_discovery) + } fn create_discovery_session(&self) -> Result> { - BluetoothDiscoverySession::create_session(self.clone()) + let test_session = try!(FakeBluetoothDiscoverySession::create_session(self.0.clone())); + Ok(BluetoothDiscoverySession::Mock(Arc::new(test_session))) + } fn get_uuids(&self) -> Result, Box> { - self.0.get_uuids() + self.0.get_uuids() } + fn set_uuids(&self, uuids: Vec) -> Result<(), Box> { + self.0.set_uuids(uuids) + } fn get_vendor_id_source(&self) -> Result> { self.0.get_vendor_id_source() @@ -448,7 +730,19 @@ impl BluetoothAdapter for Empty{ fn get_modalias(&self) -> Result<(String, u32, u32, u32), Box> { self.0.get_modalias() - } -} + } + fn set_modalias(&self, modalias: String) -> Result<(), Box> { + self.0.set_modalias(modalias) + } + + fn get_ad_datas(&self) -> Result, Box> { + self.0.get_ad_datas() + } + + fn set_ad_datas(&self, ad_datas: Vec) -> Result<(), Box> { + self.0.set_ad_datas(ad_datas) + } + +} diff --git a/src/bluetooth.rs b/src/bluetooth.rs index 07c1420..9c2f568 100644 --- a/src/bluetooth.rs +++ b/src/bluetooth.rs @@ -11,7 +11,7 @@ use blurmac::BluetoothAdapter as BluetoothAdapterMac; #[cfg(not(any(all(target_os = "linux", feature = "bluetooth"), all(target_os = "android", feature = "bluetooth"), all(target_os = "macos", feature = "bluetooth"))))] -use empty::BluetoothAdapter as BluetoothAdapterEmpty; +use empty::EmptyAdapter as BluetoothAdapterEmpty; #[cfg(feature = "bluetooth-test")] use blurmock::fake_adapter::FakeBluetoothAdapter; @@ -89,21 +89,6 @@ const NOT_SUPPORTED_ON_REAL_ERROR: &'static str = "Error! Test functions are not #[cfg(feature = "bluetooth-test")] const NOT_SUPPORTED_ON_MOCK_ERROR: &'static str = "Error! The first parameter must be a mock structure!"; -#[derive(Clone, Debug)] -pub enum BluetoothAdapter { - #[cfg(all(target_os = "linux", feature = "bluetooth"))] - Bluez(Arc), - #[cfg(all(target_os = "android", feature = "bluetooth"))] - Android(Arc), - #[cfg(all(target_os = "macos", feature = "bluetooth"))] - Mac(Arc), - #[cfg(not(any(all(target_os = "linux", feature = "bluetooth"), - all(target_os = "android", feature = "bluetooth"), - all(target_os = "macos", feature = "bluetooth"))))] - Empty(Arc), - #[cfg(feature = "bluetooth-test")] - Mock(Arc), -} #[derive(Debug)] pub enum BluetoothDiscoverySession { @@ -272,259 +257,9 @@ macro_rules! get_inner_and_call_test_func { }; } -impl BluetoothAdapter { - #[cfg(all(target_os = "linux", feature = "bluetooth"))] - pub fn init() -> Result> { - let bluez_adapter = try!(BluetoothAdapterBluez::init()); - Ok(BluetoothAdapter::Bluez(Arc::new(bluez_adapter))) - } - - #[cfg(all(target_os = "android", feature = "bluetooth"))] - pub fn init() -> Result> { - let blurdroid_adapter = try!(BluetoothAdapterAndroid::get_adapter()); - Ok(BluetoothAdapter::Android(Arc::new(blurdroid_adapter))) - } - - #[cfg(all(target_os = "macos", feature = "bluetooth"))] - pub fn init() -> Result> { - let mac_adapter = try!(BluetoothAdapterMac::init()); - Ok(BluetoothAdapter::Mac(Arc::new(mac_adapter))) - } - - #[cfg(not(any(all(target_os = "linux", feature = "bluetooth"), - all(target_os = "android", feature = "bluetooth"), - all(target_os = "macos", feature = "bluetooth"))))] - pub fn init() -> Result> { - let adapter = try!(BluetoothAdapterEmpty::init()); - Ok(BluetoothAdapter::Empty(Arc::new(adapter))) - } - - #[cfg(feature = "bluetooth-test")] - pub fn init_mock() -> Result> { - Ok(BluetoothAdapter::Mock(FakeBluetoothAdapter::new_empty())) - } - - pub fn get_id(&self) -> String { - get_inner_and_call!(self, BluetoothAdapter, get_id) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_id(&self, id: String) { - match self { - &BluetoothAdapter::Mock(ref fake_adapter) => fake_adapter.set_id(id), - _ => (), - } - } - - pub fn get_devices(&self) -> Result, Box> { - let device_list = try!(get_inner_and_call!(self, BluetoothAdapter, get_device_list)); - Ok(device_list.into_iter().map(|device| BluetoothDevice::create_device(self.clone(), device)).collect()) - } - - pub fn get_device(&self, address: String) -> Result, Box> { - let devices = try!(self.get_devices()); - for device in devices { - if try!(device.get_address()) == address { - return Ok(Some(device)); - } - } - Ok(None) - } - - pub fn get_address(&self) -> Result> { - get_inner_and_call!(self, BluetoothAdapter, get_address) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_address(&self, address: String) -> Result<(), Box> { - match self { - &BluetoothAdapter::Mock(ref fake_adapter) => fake_adapter.set_address(address), - _ => Err(Box::from(NOT_SUPPORTED_ON_REAL_ERROR)), - } - } - - pub fn get_name(&self) -> Result> { - get_inner_and_call!(self, BluetoothAdapter, get_name) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_name(&self, name: String) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_name, name) - } - - pub fn get_alias(&self) -> Result> { - get_inner_and_call!(self, BluetoothAdapter, get_alias) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_alias(&self, alias: String) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_alias, alias) - } - - pub fn get_class(&self) -> Result> { - get_inner_and_call!(self, BluetoothAdapter, get_class) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_class(&self, class: u32) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_class, class) - } - - pub fn is_powered(&self) -> Result> { - get_inner_and_call!(self, BluetoothAdapter, is_powered) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_powered(&self, powered: bool) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_powered, powered) - } - - #[cfg(feature = "bluetooth-test")] - pub fn is_present(&self) -> Result> { - get_inner_and_call_test_func!(self, BluetoothAdapter, is_present) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_present(&self, present: bool) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_present, present) - } - - pub fn is_discoverable(&self) -> Result> { - get_inner_and_call!(self, BluetoothAdapter, is_discoverable) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_discoverable(&self, discoverable: bool) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_discoverable, discoverable) - } - - pub fn is_pairable(&self) -> Result> { - get_inner_and_call!(self, BluetoothAdapter, is_pairable) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_pairable(&self, pairable: bool) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_pairable, pairable) - } - - pub fn get_pairable_timeout(&self) -> Result> { - get_inner_and_call!(self, BluetoothAdapter, get_pairable_timeout) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_pairable_timeout(&self, timeout: u32) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_pairable_timeout, timeout) - } - - pub fn get_discoverable_timeout(&self) -> Result> { - get_inner_and_call!(self, BluetoothAdapter, get_discoverable_timeout) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_discoverable_timeout(&self, timeout: u32) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_discoverable_timeout, timeout) - } - - pub fn is_discovering(&self) -> Result> { - get_inner_and_call!(self, BluetoothAdapter, is_discovering) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_discovering(&self, discovering: bool) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_discovering, discovering) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_can_start_discovery(&self, can_start_discovery: bool) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_can_start_discovery, can_start_discovery) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_can_stop_discovery(&self, can_stop_discovery: bool) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_can_stop_discovery, can_stop_discovery) - } - - pub fn create_discovery_session(&self) -> Result> { - BluetoothDiscoverySession::create_session(self.clone()) - } - - pub fn get_uuids(&self) -> Result, Box> { - get_inner_and_call!(self, BluetoothAdapter, get_uuids) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_uuids(&self, uuids: Vec) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_uuids, uuids) - } - - pub fn get_vendor_id_source(&self) -> Result> { - get_inner_and_call!(self, BluetoothAdapter, get_vendor_id_source) - } - - pub fn get_vendor_id(&self) -> Result> { - get_inner_and_call!(self, BluetoothAdapter, get_vendor_id) - } - - pub fn get_product_id(&self) -> Result> { - get_inner_and_call!(self, BluetoothAdapter, get_product_id) - } - - pub fn get_device_id(&self) -> Result> { - get_inner_and_call!(self, BluetoothAdapter, get_device_id) - } - - pub fn get_modalias(&self) -> Result<(String, u32, u32, u32), Box> { - get_inner_and_call!(self, BluetoothAdapter, get_modalias) - } - - #[cfg(feature = "bluetooth-test")] - pub fn set_modalias(&self, modalias: String) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_modalias, modalias) - } - - #[cfg(feature = "bluetooth-test")] - pub fn get_ad_datas(&self) -> Result, Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, get_ad_datas) - } - #[cfg(feature = "bluetooth-test")] - pub fn set_ad_datas(&self, ad_datas: Vec) -> Result<(), Box> { - get_inner_and_call_test_func!(self, BluetoothAdapter, set_ad_datas, ad_datas) - } -} impl BluetoothDiscoverySession { - fn create_session(adapter: BluetoothAdapter) -> Result> { - match adapter { - #[cfg(all(target_os = "linux", feature = "bluetooth"))] - BluetoothAdapter::Bluez(bluez_adapter) => { - let bluez_session = try!(BluetoothDiscoverySessionBluez::create_session(bluez_adapter.get_id())); - Ok(BluetoothDiscoverySession::Bluez(Arc::new(bluez_session))) - }, - #[cfg(all(target_os = "android", feature = "bluetooth"))] - BluetoothAdapter::Android(android_adapter) => { - let blurdroid_session = try!(BluetoothDiscoverySessionAndroid::create_session(android_adapter)); - Ok(BluetoothDiscoverySession::Android(Arc::new(blurdroid_session))) - }, - #[cfg(all(target_os = "macos", feature = "bluetooth"))] - BluetoothAdapter::Mac(mac_adapter) => { - let mac_session = try!(BluetoothDiscoverySessionMac::create_session(mac_adapter)); - Ok(BluetoothDiscoverySession::Mac(Arc::new(mac_session))) - }, - #[cfg(not(any(all(target_os = "linux", feature = "bluetooth"), - all(target_os = "android", feature = "bluetooth"), - all(target_os = "macos", feature = "bluetooth"))))] - BluetoothAdapter::Empty(adapter) => { - let empty_session = try!(BluetoothDiscoverySessionEmpty::create_session(adapter)); - Ok(BluetoothDiscoverySession::Empty(Arc::new(empty_session))) - }, - #[cfg(feature = "bluetooth-test")] - BluetoothAdapter::Mock(fake_adapter) => { - let test_session = try!(FakeBluetoothDiscoverySession::create_session(fake_adapter)); - Ok(BluetoothDiscoverySession::Mock(Arc::new(test_session))) - }, - } - } pub fn start_discovery(&self) -> Result<(), Box> { get_inner_and_call!(self, BluetoothDiscoverySession, start_discovery) @@ -537,45 +272,6 @@ impl BluetoothDiscoverySession { impl BluetoothDevice { - fn create_device(adapter: BluetoothAdapter, device: String) -> BluetoothDevice { - match adapter { - #[cfg(all(target_os = "linux", feature = "bluetooth"))] - BluetoothAdapter::Bluez(_bluez_adapter) => { - BluetoothDevice::Bluez(Arc::new(BluetoothDeviceBluez::new(device))) - }, - #[cfg(all(target_os = "android", feature = "bluetooth"))] - BluetoothAdapter::Android(android_adapter) => { - BluetoothDevice::Android(Arc::new(BluetoothDeviceAndroid::new(android_adapter, device))) - }, - #[cfg(all(target_os = "macos", feature = "bluetooth"))] - BluetoothAdapter::Mac(mac_adapter) => { - BluetoothDevice::Mac(Arc::new(BluetoothDeviceMac::new(mac_adapter, device))) - }, - #[cfg(not(any(all(target_os = "linux", feature = "bluetooth"), - all(target_os = "android", feature = "bluetooth"), - all(target_os = "macos", feature = "bluetooth"))))] - BluetoothAdapter::Empty(_adapter) => { - BluetoothDevice::Empty(Arc::new(BluetoothDeviceEmpty::new(device))) - }, - #[cfg(feature = "bluetooth-test")] - BluetoothAdapter::Mock(fake_adapter) => { - BluetoothDevice::Mock(FakeBluetoothDevice::new_empty(fake_adapter, device)) - }, - } - } - - #[cfg(feature = "bluetooth-test")] - pub fn create_mock_device(adapter: BluetoothAdapter, device: String) -> Result> { - match adapter { - BluetoothAdapter::Mock(fake_adapter) => { - Ok(BluetoothDevice::Mock(FakeBluetoothDevice::new_empty(fake_adapter, device))) - }, - _ => { - Err(Box::from(NOT_SUPPORTED_ON_MOCK_ERROR)) - }, - } - } - pub fn get_id(&self) -> String { get_inner_and_call!(self, BluetoothDevice, get_id) } @@ -1089,3 +785,4 @@ impl BluetoothGATTDescriptor { get_inner_and_call!(@with_bluez_offset, self, BluetoothGATTDescriptor, write_value, values) } } + diff --git a/src/empty.rs b/src/empty.rs index 4994cd2..010e61e 100644 --- a/src/empty.rs +++ b/src/empty.rs @@ -9,15 +9,15 @@ use std::sync::Arc; const NOT_SUPPORTED_ERROR: &'static str = "Error! Not supported platform!"; #[derive(Clone, Debug)] -pub struct BluetoothAdapter { } +pub struct EmptyAdapter { } -impl BluetoothAdapter { - pub fn init() -> Result> { - Ok(BluetoothAdapter::new()) +impl EmptyAdapter { + pub fn init() -> Result> { + Ok(EmptyAdapter::new()) } - fn new() -> BluetoothAdapter { - BluetoothAdapter{ } + fn new() -> EmptyAdapter { + EmptyAdapter{ } } pub fn get_id(&self) -> String { @@ -121,7 +121,7 @@ impl BluetoothAdapter { pub struct BluetoothDiscoverySession { } impl BluetoothDiscoverySession { - pub fn create_session(_adapter: Arc) -> Result> { + pub fn create_session(_adapter: Arc) -> Result> { Ok(BluetoothDiscoverySession{ }) }