mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Reorganise the BluetoothUUID utility functions
This commit is contained in:
parent
4ccdbccdd7
commit
ffe712a103
5 changed files with 67 additions and 59 deletions
|
@ -88,7 +88,7 @@ impl Bluetooth {
|
|||
// TODO: Step 1: Triggered by user activation.
|
||||
|
||||
// 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.
|
||||
|
||||
|
@ -123,8 +123,7 @@ impl Bluetooth {
|
|||
}
|
||||
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#request-bluetooth-devices
|
||||
fn convert_request_device_options(global: GlobalRef,
|
||||
filters: &Option<Vec<BluetoothRequestDeviceFilter>>,
|
||||
fn convert_request_device_options(filters: &Option<Vec<BluetoothRequestDeviceFilter>>,
|
||||
optional_services: &Option<Vec<BluetoothServiceUUID>>)
|
||||
-> Fallible<RequestDeviceoptions> {
|
||||
// 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.
|
||||
for filter in filters {
|
||||
// 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 {
|
||||
for opt_service in opt_services {
|
||||
// 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.
|
||||
// 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
|
||||
fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter, global: GlobalRef) -> Fallible<BluetoothScanfilter> {
|
||||
fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter) -> Fallible<BluetoothScanfilter> {
|
||||
// Step 2.4.1.
|
||||
if filter.services.is_none() &&
|
||||
filter.name.is_none() &&
|
||||
|
@ -190,7 +189,7 @@ fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter, global: GlobalRef)
|
|||
|
||||
for service in services {
|
||||
// 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.
|
||||
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 {
|
||||
Some(ref service_data_uuid) => {
|
||||
// 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.
|
||||
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
|
||||
|
|
|
@ -85,7 +85,7 @@ impl BluetoothRemoteGATTCharacteristic {
|
|||
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor
|
||||
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) {
|
||||
return Err(Security)
|
||||
}
|
||||
|
@ -112,7 +112,7 @@ impl BluetoothRemoteGATTCharacteristic {
|
|||
-> Fallible<Vec<Root<BluetoothRemoteGATTDescriptor>>> {
|
||||
let mut uuid: Option<String> = None;
|
||||
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 uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
|
||||
return Err(Security)
|
||||
|
|
|
@ -70,7 +70,7 @@ impl BluetoothRemoteGATTServer {
|
|||
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice
|
||||
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) {
|
||||
return Err(Security)
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ impl BluetoothRemoteGATTServer {
|
|||
-> Fallible<Vec<Root<BluetoothRemoteGATTService>>> {
|
||||
let mut uuid: Option<String> = None;
|
||||
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 uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
|
||||
return Err(Security)
|
||||
|
|
|
@ -74,7 +74,7 @@ impl BluetoothRemoteGATTService {
|
|||
fn get_characteristic(&self,
|
||||
characteristic: BluetoothCharacteristicUUID)
|
||||
-> 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) {
|
||||
return Err(Security)
|
||||
}
|
||||
|
@ -112,7 +112,7 @@ impl BluetoothRemoteGATTService {
|
|||
-> Fallible<Vec<Root<BluetoothRemoteGATTCharacteristic>>> {
|
||||
let mut uuid: Option<String> = None;
|
||||
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 uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
|
||||
return Err(Security)
|
||||
|
@ -155,7 +155,7 @@ impl BluetoothRemoteGATTService {
|
|||
fn get_included_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) {
|
||||
return Err(Security)
|
||||
}
|
||||
|
@ -185,7 +185,7 @@ impl BluetoothRemoteGATTService {
|
|||
-> Fallible<Vec<Root<BluetoothRemoteGATTService>>> {
|
||||
let mut uuid: Option<String> = None;
|
||||
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 uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
|
||||
return Err(Security)
|
||||
|
|
|
@ -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 {
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-canonicaluuid
|
||||
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
|
||||
pub fn GetService(globalref: GlobalRef, name: BluetoothServiceUUID) -> Fallible<UUID> {
|
||||
BluetoothUUID::resolve_uuid_name(globalref,
|
||||
name,
|
||||
BLUETOOTH_ASSIGNED_SERVICES,
|
||||
DOMString::from(SERVICE_PREFIX))
|
||||
pub fn GetService(_: GlobalRef, name: BluetoothServiceUUID) -> Fallible<UUID> {
|
||||
Self::service(name)
|
||||
}
|
||||
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getcharacteristic
|
||||
pub fn GetCharacteristic(globalref: GlobalRef, name: BluetoothCharacteristicUUID) -> Fallible<UUID> {
|
||||
BluetoothUUID::resolve_uuid_name(globalref,
|
||||
name,
|
||||
BLUETOOTH_ASSIGNED_CHARCTERISTICS,
|
||||
DOMString::from(CHARACTERISTIC_PREFIX))
|
||||
pub fn GetCharacteristic(_: GlobalRef, name: BluetoothCharacteristicUUID) -> Fallible<UUID> {
|
||||
Self::characteristic(name)
|
||||
}
|
||||
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getdescriptor
|
||||
pub fn GetDescriptor(globalref: GlobalRef, name: BluetoothDescriptorUUID) -> Fallible<UUID> {
|
||||
BluetoothUUID::resolve_uuid_name(globalref,
|
||||
name,
|
||||
BLUETOOTH_ASSIGNED_DESCRIPTORS,
|
||||
DOMString::from(DESCRIPTOR_PREFIX))
|
||||
pub fn GetDescriptor(_: GlobalRef, name: BluetoothDescriptorUUID) -> Fallible<UUID> {
|
||||
Self::descriptor(name)
|
||||
}
|
||||
}
|
||||
|
||||
// https://webbluetoothcg.github.io/web-bluetooth/#resolveuuidname
|
||||
pub fn resolve_uuid_name(globalref: GlobalRef,
|
||||
name: StringOrUnsignedLong,
|
||||
assigned_numbers_table: &'static [(&'static str, u32)],
|
||||
prefix: DOMString)
|
||||
-> Fallible<DOMString> {
|
||||
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),
|
||||
impl BluetoothUUID {
|
||||
pub fn service(name: BluetoothServiceUUID) -> Fallible<UUID> {
|
||||
resolve_uuid_name(name, BLUETOOTH_ASSIGNED_SERVICES, SERVICE_PREFIX)
|
||||
}
|
||||
|
||||
pub fn characteristic(name: BluetoothServiceUUID) -> Fallible<UUID> {
|
||||
resolve_uuid_name(name, BLUETOOTH_ASSIGNED_CHARCTERISTICS, CHARACTERISTIC_PREFIX)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
pub fn descriptor(name: BluetoothDescriptorUUID) -> Fallible<UUID> {
|
||||
resolve_uuid_name(name, BLUETOOTH_ASSIGNED_DESCRIPTORS, DESCRIPTOR_PREFIX)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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),
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue