use self::constants::{
CHARACTERISTIC_NUMBERS, CUSTOM_SERVICE_UUIDS, DESCRIPTOR_NUMBERS, SERVICE_UUIDS,
};
use crate::types::Uuid;
pub(crate) mod constants;
pub mod ltv;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AssignedNumber {
pub number: u16,
pub abbreviation: Option<&'static str>,
pub name: &'static str,
}
impl AssignedNumber {
pub fn matches(&self, identifier: &str) -> bool {
let identifier = &identifier.to_uppercase();
self.matches_abbreviation(identifier)
|| self.matches_name(identifier)
|| self.matches_number(identifier)
}
fn matches_abbreviation(&self, identifier: &str) -> bool {
self.abbreviation.map(|abbr| abbr == identifier).unwrap_or(false)
}
fn matches_name(&self, identifier: &str) -> bool {
self.name.to_uppercase() == identifier
}
fn matches_number(&self, identifier: &str) -> bool {
let identifier = if identifier.starts_with("0X") { &identifier[2..] } else { identifier };
let string = Uuid::new16(self.number).to_string().to_uppercase();
if identifier.len() == 32 + 4 {
identifier == string
} else {
let short_form =
string.split("-").next().expect("split iter always has at least 1 item");
short_form == format!("{:0>8}", identifier)
}
}
}
pub fn find_service_uuid(identifier: &str) -> Option<AssignedNumber> {
SERVICE_UUIDS
.iter()
.chain(CUSTOM_SERVICE_UUIDS.iter())
.find(|sn| sn.matches(identifier))
.copied()
}
pub fn find_characteristic_number(identifier: &str) -> Option<AssignedNumber> {
CHARACTERISTIC_NUMBERS.iter().find(|cn| cn.matches(identifier)).map(|&an| an)
}
pub fn find_descriptor_number(identifier: &str) -> Option<AssignedNumber> {
DESCRIPTOR_NUMBERS.iter().find(|dn| dn.matches(identifier)).map(|&an| an)
}
#[macro_export]
macro_rules! assigned_number {
($num:expr, $abbr:expr, $name:expr) => {
AssignedNumber { number: $num, abbreviation: Some($abbr), name: $name }
};
($num:expr, $name:expr) => {
AssignedNumber { number: $num, abbreviation: None, name: $name }
};
}
#[cfg(test)]
mod tests {
use super::{
find_characteristic_number, find_descriptor_number, find_service_uuid,
CHARACTERISTIC_NUMBERS, CUSTOM_SERVICE_UUIDS, DESCRIPTOR_NUMBERS, SERVICE_UUIDS,
};
#[test]
fn test_find_characteristic_number() {
assert_eq!(find_characteristic_number("Device Name"), Some(CHARACTERISTIC_NUMBERS[0]));
assert_eq!(find_characteristic_number("aPPEARANCE"), Some(CHARACTERISTIC_NUMBERS[1]));
assert_eq!(find_characteristic_number("fake characteristic name"), None);
assert_eq!(find_characteristic_number("2a00"), Some(CHARACTERISTIC_NUMBERS[0]));
assert_eq!(find_characteristic_number("zzzz"), None);
}
#[test]
fn test_find_descriptor_number() {
assert_eq!(find_descriptor_number("Report reference"), Some(DESCRIPTOR_NUMBERS[8]));
assert_eq!(find_descriptor_number("fake descriptor name"), None);
assert_eq!(
find_descriptor_number("00002908-0000-1000-8000-00805F9B34FB"),
Some(DESCRIPTOR_NUMBERS[8])
);
assert_eq!(find_descriptor_number("2908"), Some(DESCRIPTOR_NUMBERS[8]));
assert_eq!(find_descriptor_number("zzzz"), None);
}
#[test]
fn test_find_service_uuid() {
assert_eq!(find_service_uuid("GAP"), Some(SERVICE_UUIDS[0]));
assert_eq!(find_service_uuid("Gap"), Some(SERVICE_UUIDS[0]));
assert_eq!(find_service_uuid("gap"), Some(SERVICE_UUIDS[0]));
assert_eq!(find_service_uuid("hIdS"), Some(SERVICE_UUIDS[16]));
assert_eq!(find_service_uuid("XYZ"), None);
assert_eq!(find_service_uuid("Human Interface Device Service"), Some(SERVICE_UUIDS[16]));
assert_eq!(find_service_uuid("Fake Service Name"), None);
assert_eq!(find_service_uuid("183A"), Some(SERVICE_UUIDS[39]));
assert_eq!(find_service_uuid("0x183a"), Some(SERVICE_UUIDS[39]));
assert_eq!(find_service_uuid("0000183a"), Some(SERVICE_UUIDS[39]));
assert_eq!(
find_service_uuid("0000183A-0000-1000-8000-00805F9B34FB"),
Some(SERVICE_UUIDS[39])
);
assert_eq!(
find_service_uuid("0000183a-0000-1000-8000-00805f9b34fb"),
Some(SERVICE_UUIDS[39])
);
assert_eq!(find_service_uuid("0000183A-0000-1000-8000-000000000000"), None);
assert_eq!(find_service_uuid("ZZZZZZZZ"), None);
assert_eq!(find_service_uuid("ZZZZZZZZ-0000-1000-8000-00805F9B34FB"), None);
assert_eq!(find_service_uuid("fdcf"), Some(CUSTOM_SERVICE_UUIDS[0]));
assert_eq!(find_service_uuid("FDE2"), Some(CUSTOM_SERVICE_UUIDS[19]));
}
}