Format component bluetooth_traits

This commit is contained in:
kingdido999 2018-08-17 11:09:38 +08:00
parent 949e50d662
commit 0e3131a54c
3 changed files with 47 additions and 37 deletions

View file

@ -10,24 +10,17 @@ use std::string::String;
const EXCLUDE_READS: &'static str = "exclude-reads";
const EXCLUDE_WRITES: &'static str = "exclude-writes";
const VALID_UUID_REGEX: &'static str = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
const VALID_UUID_REGEX: &'static str =
"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
thread_local!(pub static BLUETOOTH_BLOCKLIST: RefCell<BluetoothBlocklist> =
RefCell::new(BluetoothBlocklist(parse_blocklist())));
pub fn uuid_is_blocklisted(uuid: &str, exclude_type: Blocklist) -> bool {
BLUETOOTH_BLOCKLIST.with(|blist| {
match exclude_type {
Blocklist::All => {
blist.borrow().is_blocklisted(uuid)
},
Blocklist::Reads => {
blist.borrow().is_blocklisted_for_reads(uuid)
}
Blocklist::Writes => {
blist.borrow().is_blocklisted_for_writes(uuid)
}
}
BLUETOOTH_BLOCKLIST.with(|blist| match exclude_type {
Blocklist::All => blist.borrow().is_blocklisted(uuid),
Blocklist::Reads => blist.borrow().is_blocklisted_for_reads(uuid),
Blocklist::Writes => blist.borrow().is_blocklisted_for_writes(uuid),
})
}
@ -52,8 +45,9 @@ impl BluetoothBlocklist {
// https://webbluetoothcg.github.io/web-bluetooth/#blocklisted-for-reads
pub fn is_blocklisted_for_reads(&self, uuid: &str) -> bool {
match self.0 {
Some(ref map) => map.get(uuid).map_or(false, |et| et.eq(&Blocklist::All) ||
et.eq(&Blocklist::Reads)),
Some(ref map) => map.get(uuid).map_or(false, |et| {
et.eq(&Blocklist::All) || et.eq(&Blocklist::Reads)
}),
None => false,
}
}
@ -61,8 +55,9 @@ impl BluetoothBlocklist {
// https://webbluetoothcg.github.io/web-bluetooth/#blocklisted-for-writes
pub fn is_blocklisted_for_writes(&self, uuid: &str) -> bool {
match self.0 {
Some(ref map) => map.get(uuid).map_or(false, |et| et.eq(&Blocklist::All) ||
et.eq(&Blocklist::Writes)),
Some(ref map) => map.get(uuid).map_or(false, |et| {
et.eq(&Blocklist::All) || et.eq(&Blocklist::Writes)
}),
None => false,
}
}
@ -98,7 +93,7 @@ fn parse_blocklist() -> Option<HashMap<String, Blocklist>> {
exclude_type = Blocklist::Reads;
},
Some(EXCLUDE_WRITES) => {
exclude_type = Blocklist::Writes;
exclude_type = Blocklist::Writes;
},
// Step 4.4
_ => {

View file

@ -5,7 +5,8 @@
extern crate embedder_traits;
extern crate ipc_channel;
extern crate regex;
#[macro_use] extern crate serde;
#[macro_use]
extern crate serde;
pub mod blocklist;
pub mod scanfilter;
@ -83,7 +84,13 @@ pub enum BluetoothRequest {
RequestDevice(RequestDeviceoptions, IpcSender<BluetoothResponseResult>),
GATTServerConnect(String, IpcSender<BluetoothResponseResult>),
GATTServerDisconnect(String, IpcSender<BluetoothResult<()>>),
GetGATTChildren(String, Option<String>, bool, GATTType, IpcSender<BluetoothResponseResult>),
GetGATTChildren(
String,
Option<String>,
bool,
GATTType,
IpcSender<BluetoothResponseResult>,
),
ReadValue(String, IpcSender<BluetoothResponseResult>),
WriteValue(String, Vec<u8>, IpcSender<BluetoothResponseResult>),
EnableNotification(String, bool, IpcSender<BluetoothResponseResult>),
@ -91,7 +98,11 @@ pub enum BluetoothRequest {
SetRepresentedToNull(Vec<String>, Vec<String>, Vec<String>),
IsRepresentedDeviceNull(String, IpcSender<bool>),
GetAvailability(IpcSender<BluetoothResponseResult>),
MatchesFilter(String, BluetoothScanfilterSequence, IpcSender<BluetoothResult<bool>>),
MatchesFilter(
String,
BluetoothScanfilterSequence,
IpcSender<BluetoothResult<bool>>,
),
Test(String, IpcSender<BluetoothResult<()>>),
Exit,
}

View file

@ -36,12 +36,13 @@ pub struct BluetoothScanfilter {
}
impl BluetoothScanfilter {
pub fn new(name: Option<String>,
name_prefix: String,
services: Vec<String>,
manufacturer_data: Option<ManufacturerData>,
service_data: Option<ServiceData>)
-> BluetoothScanfilter {
pub fn new(
name: Option<String>,
name_prefix: String,
services: Vec<String>,
manufacturer_data: Option<ManufacturerData>,
service_data: Option<ServiceData>,
) -> BluetoothScanfilter {
BluetoothScanfilter {
name: name,
name_prefix: name_prefix,
@ -73,12 +74,12 @@ impl BluetoothScanfilter {
pub fn is_empty_or_invalid(&self) -> bool {
(self.name.is_none() &&
self.name_prefix.is_empty() &&
self.get_services().is_empty() &&
self.manufacturer_data.is_none() &&
self.service_data.is_none()) ||
self.get_name().unwrap_or("").len() > MAX_NAME_LENGTH ||
self.name_prefix.len() > MAX_NAME_LENGTH
self.name_prefix.is_empty() &&
self.get_services().is_empty() &&
self.manufacturer_data.is_none() &&
self.service_data.is_none()) ||
self.get_name().unwrap_or("").len() > MAX_NAME_LENGTH ||
self.name_prefix.len() > MAX_NAME_LENGTH
}
}
@ -99,7 +100,9 @@ impl BluetoothScanfilterSequence {
}
fn get_services_set(&self) -> HashSet<String> {
self.iter().flat_map(|filter| filter.services.get_services_set()).collect()
self.iter()
.flat_map(|filter| filter.services.get_services_set())
.collect()
}
fn is_empty(&self) -> bool {
@ -114,9 +117,10 @@ pub struct RequestDeviceoptions {
}
impl RequestDeviceoptions {
pub fn new(filters: BluetoothScanfilterSequence,
services: ServiceUUIDSequence)
-> RequestDeviceoptions {
pub fn new(
filters: BluetoothScanfilterSequence,
services: ServiceUUIDSequence,
) -> RequestDeviceoptions {
RequestDeviceoptions {
filters: filters,
optional_services: services,