pub mod client;
pub mod server;
use bluetooth_adv::Uuid;
use serde::{Deserialize, Serialize};
pub mod uuid {
pub const PRIMARY_SERVICE: u16 = 0x2800;
pub const SECONDARY_SERVICE: u16 = 0x2801;
pub const INCLUDE: u16 = 0x2802;
pub const CHARACTERISTIC: u16 = 0x2803;
pub const EXTENDED_PROPERTIES: u16 = 0x2900;
pub const USER_DESCRIPTION: u16 = 0x2901;
pub const CCCD: u16 = 0x2902;
pub const SCCD: u16 = 0x2903;
pub const PRESENTATION_FORMAT: u16 = 0x2904;
pub const AGGREGATE_FORMAT: u16 = 0x2905;
pub const GAP: u16 = 0x1800;
pub const GATT: u16 = 0x1801;
pub const DEVICE_NAME: u16 = 0x2a00;
pub const APPEARANCE: u16 = 0x2a01;
pub const SERVICE_CHANGED: u16 = 0x2a05;
pub const CENTRAL_ADDRESS_RESOLUTION: u16 = 0x2aa6;
pub const CLIENT_SUPPORTED_FEATURES: u16 = 0x2b29;
pub const DATABASE_HASH: u16 = 0x2b2a;
pub const SERVER_SUPPORTED_FEATURES: u16 = 0x2b3a;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Properties(pub u8);
impl Properties {
pub const BROADCAST: u8 = 0x01;
pub const READ: u8 = 0x02;
pub const WRITE_WITHOUT_RESPONSE: u8 = 0x04;
pub const WRITE: u8 = 0x08;
pub const NOTIFY: u8 = 0x10;
pub const INDICATE: u8 = 0x20;
pub const SIGNED_WRITE: u8 = 0x40;
pub const EXTENDED: u8 = 0x80;
pub fn has(&self, bit: u8) -> bool {
self.0 & bit != 0
}
pub fn names(&self) -> Vec<&'static str> {
[
(Self::BROADCAST, "broadcast"),
(Self::READ, "read"),
(Self::WRITE_WITHOUT_RESPONSE, "write_without_response"),
(Self::WRITE, "write"),
(Self::NOTIFY, "notify"),
(Self::INDICATE, "indicate"),
(Self::SIGNED_WRITE, "authenticated_signed_writes"),
(Self::EXTENDED, "extended_properties"),
]
.iter()
.filter(|(b, _)| self.has(*b))
.map(|(_, n)| *n)
.collect()
}
pub fn from_names<S: AsRef<str>>(names: &[S]) -> Result<Properties, String> {
let mut p = 0;
for n in names {
p |= match n.as_ref() {
"broadcast" => Self::BROADCAST,
"read" => Self::READ,
"write_without_response" => Self::WRITE_WITHOUT_RESPONSE,
"write" => Self::WRITE,
"notify" => Self::NOTIFY,
"indicate" => Self::INDICATE,
"authenticated_signed_writes" => Self::SIGNED_WRITE,
other => return Err(format!("unknown characteristic property {other:?}")),
};
}
Ok(Properties(p))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Service {
pub uuid: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<&'static str>,
pub primary: bool,
pub start: u16,
pub end: u16,
pub characteristics: Vec<Characteristic>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Characteristic {
pub uuid: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<&'static str>,
pub declaration: u16,
pub value_handle: u16,
pub properties: Properties,
pub end: u16,
pub descriptors: Vec<Descriptor>,
}
impl Characteristic {
pub fn cccd(&self) -> Option<u16> {
self.descriptors
.iter()
.find(|d| d.uuid == Uuid::Uuid16(uuid::CCCD))
.map(|d| d.handle)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct Descriptor {
pub uuid: Uuid,
pub handle: u16,
}
pub(crate) fn uuid_bytes(u: &Uuid) -> Vec<u8> {
match u.shortened() {
Uuid::Uuid16(v) => v.to_le_bytes().to_vec(),
other => {
let mut a = other.to_u128_bytes();
a.reverse();
a.to_vec()
}
}
}