Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format components bluetooth and bluetooth_traits #21442

Merged
merged 2 commits into from Aug 17, 2018
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Prev

Format component bluetooth_traits

  • Loading branch information
kingdido999
kingdido999 committed Aug 17, 2018
commit 0e3131a54cab9c3025616a68803bee952c19a931
@@ -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,17 +45,19 @@ 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,
}
}

// 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
_ => {
@@ -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,15 +84,25 @@ 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>),
WatchAdvertisements(String, IpcSender<BluetoothResponseResult>),
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,
}
@@ -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,
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.