Reorganise the BluetoothUUID utility functions

This commit is contained in:
Anthony Ramine 2016-10-01 21:45:28 +02:00
parent 4ccdbccdd7
commit ffe712a103
5 changed files with 67 additions and 59 deletions

View file

@ -88,7 +88,7 @@ impl Bluetooth {
// TODO: Step 1: Triggered by user activation. // TODO: Step 1: Triggered by user activation.
// Step 2. // Step 2.
let option = try!(convert_request_device_options(self.global().r(), filters, optional_services)); let option = try!(convert_request_device_options(filters, optional_services));
// TODO: Step 3-5: Implement the permission API. // TODO: Step 3-5: Implement the permission API.
@ -123,8 +123,7 @@ impl Bluetooth {
} }
// https://webbluetoothcg.github.io/web-bluetooth/#request-bluetooth-devices // https://webbluetoothcg.github.io/web-bluetooth/#request-bluetooth-devices
fn convert_request_device_options(global: GlobalRef, fn convert_request_device_options(filters: &Option<Vec<BluetoothRequestDeviceFilter>>,
filters: &Option<Vec<BluetoothRequestDeviceFilter>>,
optional_services: &Option<Vec<BluetoothServiceUUID>>) optional_services: &Option<Vec<BluetoothServiceUUID>>)
-> Fallible<RequestDeviceoptions> { -> Fallible<RequestDeviceoptions> {
// Step 2.2: There is no requiredServiceUUIDS, we scan for all devices. // Step 2.2: There is no requiredServiceUUIDS, we scan for all devices.
@ -141,7 +140,7 @@ fn convert_request_device_options(global: GlobalRef,
// Step 2.4. // Step 2.4.
for filter in filters { for filter in filters {
// Step 2.4.8. // Step 2.4.8.
uuid_filters.push(try!(canonicalize_filter(&filter, global))); uuid_filters.push(try!(canonicalize_filter(&filter)));
} }
} }
@ -149,7 +148,7 @@ fn convert_request_device_options(global: GlobalRef,
if let &Some(ref opt_services) = optional_services { if let &Some(ref opt_services) = optional_services {
for opt_service in opt_services { for opt_service in opt_services {
// Step 2.5 - 2.6. // Step 2.5 - 2.6.
let uuid = try!(BluetoothUUID::GetService(global, opt_service.clone())).to_string(); let uuid = try!(BluetoothUUID::service(opt_service.clone())).to_string();
// Step 2.7. // Step 2.7.
// Note: What we are doing here is adding the not blacklisted UUIDs to the result vector, // Note: What we are doing here is adding the not blacklisted UUIDs to the result vector,
@ -165,7 +164,7 @@ fn convert_request_device_options(global: GlobalRef,
} }
// https://webbluetoothcg.github.io/web-bluetooth/#request-bluetooth-devices // https://webbluetoothcg.github.io/web-bluetooth/#request-bluetooth-devices
fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter, global: GlobalRef) -> Fallible<BluetoothScanfilter> { fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter) -> Fallible<BluetoothScanfilter> {
// Step 2.4.1. // Step 2.4.1.
if filter.services.is_none() && if filter.services.is_none() &&
filter.name.is_none() && filter.name.is_none() &&
@ -190,7 +189,7 @@ fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter, global: GlobalRef)
for service in services { for service in services {
// Step 2.4.3.2 - 2.4.3.3. // Step 2.4.3.2 - 2.4.3.3.
let uuid = try!(BluetoothUUID::GetService(global, service.clone())).to_string(); let uuid = try!(BluetoothUUID::service(service.clone())).to_string();
// Step 2.4.3.4. // Step 2.4.3.4.
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
@ -251,7 +250,7 @@ fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter, global: GlobalRef)
let service_data_uuid = match filter.serviceDataUUID { let service_data_uuid = match filter.serviceDataUUID {
Some(ref service_data_uuid) => { Some(ref service_data_uuid) => {
// Step 2.4.7.1 - 2.4.7.2. // Step 2.4.7.1 - 2.4.7.2.
let uuid = try!(BluetoothUUID::GetService(global, service_data_uuid.clone())).to_string(); let uuid = try!(BluetoothUUID::service(service_data_uuid.clone())).to_string();
// Step 2.4.7.3. // Step 2.4.7.3.
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {

View file

@ -85,7 +85,7 @@ impl BluetoothRemoteGATTCharacteristic {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor
fn get_descriptor(&self, descriptor: BluetoothDescriptorUUID) -> Fallible<Root<BluetoothRemoteGATTDescriptor>> { fn get_descriptor(&self, descriptor: BluetoothDescriptorUUID) -> Fallible<Root<BluetoothRemoteGATTDescriptor>> {
let uuid = try!(BluetoothUUID::GetDescriptor(self.global().r(), descriptor)).to_string(); let uuid = try!(BluetoothUUID::descriptor(descriptor)).to_string();
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
return Err(Security) return Err(Security)
} }
@ -112,7 +112,7 @@ impl BluetoothRemoteGATTCharacteristic {
-> Fallible<Vec<Root<BluetoothRemoteGATTDescriptor>>> { -> Fallible<Vec<Root<BluetoothRemoteGATTDescriptor>>> {
let mut uuid: Option<String> = None; let mut uuid: Option<String> = None;
if let Some(d) = descriptor { if let Some(d) = descriptor {
uuid = Some(try!(BluetoothUUID::GetDescriptor(self.global().r(), d)).to_string()); uuid = Some(try!(BluetoothUUID::descriptor(d)).to_string());
if let Some(ref uuid) = uuid { if let Some(ref uuid) = uuid {
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
return Err(Security) return Err(Security)

View file

@ -70,7 +70,7 @@ impl BluetoothRemoteGATTServer {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice
fn get_primary_service(&self, service: BluetoothServiceUUID) -> Fallible<Root<BluetoothRemoteGATTService>> { fn get_primary_service(&self, service: BluetoothServiceUUID) -> Fallible<Root<BluetoothRemoteGATTService>> {
let uuid = try!(BluetoothUUID::GetService(self.global().r(), service)).to_string(); let uuid = try!(BluetoothUUID::service(service)).to_string();
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
return Err(Security) return Err(Security)
} }
@ -98,7 +98,7 @@ impl BluetoothRemoteGATTServer {
-> Fallible<Vec<Root<BluetoothRemoteGATTService>>> { -> Fallible<Vec<Root<BluetoothRemoteGATTService>>> {
let mut uuid: Option<String> = None; let mut uuid: Option<String> = None;
if let Some(s) = service { if let Some(s) = service {
uuid = Some(try!(BluetoothUUID::GetService(self.global().r(), s)).to_string()); uuid = Some(try!(BluetoothUUID::service(s)).to_string());
if let Some(ref uuid) = uuid { if let Some(ref uuid) = uuid {
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
return Err(Security) return Err(Security)

View file

@ -74,7 +74,7 @@ impl BluetoothRemoteGATTService {
fn get_characteristic(&self, fn get_characteristic(&self,
characteristic: BluetoothCharacteristicUUID) characteristic: BluetoothCharacteristicUUID)
-> Fallible<Root<BluetoothRemoteGATTCharacteristic>> { -> Fallible<Root<BluetoothRemoteGATTCharacteristic>> {
let uuid = try!(BluetoothUUID::GetCharacteristic(self.global().r(), characteristic)).to_string(); let uuid = try!(BluetoothUUID::characteristic(characteristic)).to_string();
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
return Err(Security) return Err(Security)
} }
@ -112,7 +112,7 @@ impl BluetoothRemoteGATTService {
-> Fallible<Vec<Root<BluetoothRemoteGATTCharacteristic>>> { -> Fallible<Vec<Root<BluetoothRemoteGATTCharacteristic>>> {
let mut uuid: Option<String> = None; let mut uuid: Option<String> = None;
if let Some(c) = characteristic { if let Some(c) = characteristic {
uuid = Some(try!(BluetoothUUID::GetCharacteristic(self.global().r(), c)).to_string()); uuid = Some(try!(BluetoothUUID::characteristic(c)).to_string());
if let Some(ref uuid) = uuid { if let Some(ref uuid) = uuid {
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
return Err(Security) return Err(Security)
@ -155,7 +155,7 @@ impl BluetoothRemoteGATTService {
fn get_included_service(&self, fn get_included_service(&self,
service: BluetoothServiceUUID) service: BluetoothServiceUUID)
-> Fallible<Root<BluetoothRemoteGATTService>> { -> Fallible<Root<BluetoothRemoteGATTService>> {
let uuid = try!(BluetoothUUID::GetService(self.global().r(), service)).to_string(); let uuid = try!(BluetoothUUID::service(service)).to_string();
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
return Err(Security) return Err(Security)
} }
@ -185,7 +185,7 @@ impl BluetoothRemoteGATTService {
-> Fallible<Vec<Root<BluetoothRemoteGATTService>>> { -> Fallible<Vec<Root<BluetoothRemoteGATTService>>> {
let mut uuid: Option<String> = None; let mut uuid: Option<String> = None;
if let Some(s) = service { if let Some(s) = service {
uuid = Some(try!(BluetoothUUID::GetService(self.global().r(), s)).to_string()); uuid = Some(try!(BluetoothUUID::service(s)).to_string());
if let Some(ref uuid) = uuid { if let Some(ref uuid) = uuid {
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
return Err(Security) return Err(Security)

View file

@ -272,60 +272,36 @@ const VALID_UUID_REGEX: &'static str = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-
impl BluetoothUUID { impl BluetoothUUID {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-canonicaluuid // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-canonicaluuid
pub fn CanonicalUUID(_: GlobalRef, alias: u32) -> UUID { pub fn CanonicalUUID(_: GlobalRef, alias: u32) -> UUID {
DOMString::from(format!("{:08x}", &alias) + BASE_UUID) canonical_uuid(alias)
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getservice // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getservice
pub fn GetService(globalref: GlobalRef, name: BluetoothServiceUUID) -> Fallible<UUID> { pub fn GetService(_: GlobalRef, name: BluetoothServiceUUID) -> Fallible<UUID> {
BluetoothUUID::resolve_uuid_name(globalref, Self::service(name)
name,
BLUETOOTH_ASSIGNED_SERVICES,
DOMString::from(SERVICE_PREFIX))
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getcharacteristic // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getcharacteristic
pub fn GetCharacteristic(globalref: GlobalRef, name: BluetoothCharacteristicUUID) -> Fallible<UUID> { pub fn GetCharacteristic(_: GlobalRef, name: BluetoothCharacteristicUUID) -> Fallible<UUID> {
BluetoothUUID::resolve_uuid_name(globalref, Self::characteristic(name)
name,
BLUETOOTH_ASSIGNED_CHARCTERISTICS,
DOMString::from(CHARACTERISTIC_PREFIX))
} }
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getdescriptor // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getdescriptor
pub fn GetDescriptor(globalref: GlobalRef, name: BluetoothDescriptorUUID) -> Fallible<UUID> { pub fn GetDescriptor(_: GlobalRef, name: BluetoothDescriptorUUID) -> Fallible<UUID> {
BluetoothUUID::resolve_uuid_name(globalref, Self::descriptor(name)
name, }
BLUETOOTH_ASSIGNED_DESCRIPTORS, }
DOMString::from(DESCRIPTOR_PREFIX))
impl BluetoothUUID {
pub fn service(name: BluetoothServiceUUID) -> Fallible<UUID> {
resolve_uuid_name(name, BLUETOOTH_ASSIGNED_SERVICES, SERVICE_PREFIX)
} }
// https://webbluetoothcg.github.io/web-bluetooth/#resolveuuidname pub fn characteristic(name: BluetoothServiceUUID) -> Fallible<UUID> {
pub fn resolve_uuid_name(globalref: GlobalRef, resolve_uuid_name(name, BLUETOOTH_ASSIGNED_CHARCTERISTICS, CHARACTERISTIC_PREFIX)
name: StringOrUnsignedLong, }
assigned_numbers_table: &'static [(&'static str, u32)],
prefix: DOMString) pub fn descriptor(name: BluetoothDescriptorUUID) -> Fallible<UUID> {
-> Fallible<DOMString> { resolve_uuid_name(name, BLUETOOTH_ASSIGNED_DESCRIPTORS, DESCRIPTOR_PREFIX)
match name {
// Step 1
StringOrUnsignedLong::UnsignedLong(unsigned32) => {
Ok(BluetoothUUID::CanonicalUUID(globalref, unsigned32))
},
StringOrUnsignedLong::String(dstring) => {
// Step 2
let regex = Regex::new(VALID_UUID_REGEX).unwrap();
if regex.is_match(&*dstring) {
Ok(dstring)
} else {
// Step 3
let concatenated = format!("{}.{}", prefix, dstring);
let is_in_table = assigned_numbers_table.iter().find(|p| p.0 == concatenated);
match is_in_table {
Some(&(_, alias)) => Ok(BluetoothUUID::CanonicalUUID(globalref, alias)),
None => Err(Syntax),
}
}
},
}
} }
} }
@ -337,3 +313,36 @@ impl Clone for StringOrUnsignedLong {
} }
} }
} }
fn canonical_uuid(alias: u32) -> UUID {
UUID::from(format!("{:08x}", &alias) + BASE_UUID)
}
// https://webbluetoothcg.github.io/web-bluetooth/#resolveuuidname
fn resolve_uuid_name(
name: StringOrUnsignedLong,
assigned_numbers_table: &'static [(&'static str, u32)],
prefix: &str)
-> Fallible<DOMString> {
match name {
// Step 1
StringOrUnsignedLong::UnsignedLong(unsigned32) => {
Ok(canonical_uuid(unsigned32))
},
StringOrUnsignedLong::String(dstring) => {
// Step 2
let regex = Regex::new(VALID_UUID_REGEX).unwrap();
if regex.is_match(&*dstring) {
Ok(dstring)
} else {
// Step 3
let concatenated = format!("{}.{}", prefix, dstring);
let is_in_table = assigned_numbers_table.iter().find(|p| p.0 == concatenated);
match is_in_table {
Some(&(_, alias)) => Ok(canonical_uuid(alias)),
None => Err(Syntax),
}
}
},
}
}