use uuid::Uuid;
const BLUETOOTH_BASE_UUID: u128 = 0x0000_0000_0000_1000_8000_0080_5F9B_34FB;
#[must_use]
const fn uuid_from_u16(short: u16) -> Uuid {
Uuid::from_u128(BLUETOOTH_BASE_UUID | ((short as u128) << 96))
}
pub const LOCK_SERVICE_UUID: Uuid = Uuid::from_u128(0x0A0F0001_0000_1000_8000_00805F9B34FB);
pub const BATTERY_SERVICE_UUID: Uuid = uuid_from_u16(0x180F);
pub const DEVICE_INFO_SERVICE_UUID: Uuid = uuid_from_u16(0x180A);
pub const GENERIC_ACCESS_SERVICE_UUID: Uuid = uuid_from_u16(0x1800);
pub const DIALOG_OTA_SERVICE_UUID: Uuid = uuid_from_u16(0xFEF5);
pub const LOCK_STATE_CHAR_UUID: Uuid = Uuid::from_u128(0x0A0F0001_0000_1000_8000_00805F9B34FB);
pub const STATUS_CHAR_UUID: Uuid = Uuid::from_u128(0x0A0F0011_0000_1000_8000_00805F9B34FB);
pub const LOCK_POSITION_CHAR_UUID: Uuid = Uuid::from_u128(0x0A0F0002_0000_1000_8000_00805F9B34FB);
pub const COMMAND_CHAR_UUID: Uuid = Uuid::from_u128(0x0A0F0003_0000_1000_8000_00805F9B34FB);
pub const DEVICE_INFO_CHAR_UUID: Uuid = Uuid::from_u128(0x0A0F0004_0000_1000_8000_00805F9B34FB);
pub const EXTENDED_INFO_CHAR_UUID: Uuid = Uuid::from_u128(0x0A0F1004_0000_1000_8000_00805F9B34FB);
pub const CONFIG_CHAR_UUID: Uuid = Uuid::from_u128(0x0A0F0005_0000_1000_8000_00805F9B34FB);
pub const CONTROL_CHAR_UUID: Uuid = Uuid::from_u128(0x0A0F0006_0000_1000_8000_00805F9B34FB);
pub const BATTERY_LEVEL_CHAR_UUID: Uuid = uuid_from_u16(0x2A19);
pub const FIRMWARE_REVISION_CHAR_UUID: Uuid = uuid_from_u16(0x2A26);
pub const DEVICE_NAME_CHAR_UUID: Uuid = uuid_from_u16(0x2A00);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum LockState {
Unlocked = 0x00,
Locked = 0x01,
}
impl LockState {
#[must_use]
pub const fn from_byte(byte: u8) -> Option<Self> {
match byte {
0x00 => Some(Self::Unlocked),
0x01 => Some(Self::Locked),
_ => None,
}
}
#[must_use]
pub const fn as_byte(self) -> u8 {
self as u8
}
#[must_use]
pub const fn is_locked(self) -> bool {
matches!(self, Self::Locked)
}
#[must_use]
pub const fn is_unlocked(self) -> bool {
matches!(self, Self::Unlocked)
}
}
impl TryFrom<u8> for LockState {
type Error = crate::Error;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Self::from_byte(value)
.ok_or_else(|| crate::Error::InvalidResponse(format!("invalid lock state: {value:#04x}")))
}
}
impl From<LockState> for u8 {
fn from(state: LockState) -> Self {
state.as_byte()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeviceInfo {
pub name: String,
pub firmware_version: String,
pub battery_level: u8,
}
pub const MANUFACTURER_ID: u16 = 0x0A0F;
pub const DEVICE_NAME: &str = "Ohea Lock";
#[must_use]
pub fn build_init_command() -> [u8; 5] {
[0x1A, 0x01, 0x04, 0x00, 0x31]
}
pub const ATT_ERR_INSUFFICIENT_AUTH: u8 = 0x05;
pub const ATT_ERR_ATTR_NOT_FOUND: u8 = 0x0A;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn uuid_from_u16_constructs_correct_bluetooth_uuid() {
let expected = Uuid::parse_str("00002A19-0000-1000-8000-00805F9B34FB").unwrap();
assert_eq!(uuid_from_u16(0x2A19), expected);
}
#[test]
fn standard_service_uuids_are_correct() {
assert_eq!(
BATTERY_SERVICE_UUID,
Uuid::parse_str("0000180F-0000-1000-8000-00805F9B34FB").unwrap()
);
assert_eq!(
DEVICE_INFO_SERVICE_UUID,
Uuid::parse_str("0000180A-0000-1000-8000-00805F9B34FB").unwrap()
);
assert_eq!(
GENERIC_ACCESS_SERVICE_UUID,
Uuid::parse_str("00001800-0000-1000-8000-00805F9B34FB").unwrap()
);
}
#[test]
fn lock_service_uuid_matches_manufacturer_spec() {
assert_eq!(
LOCK_SERVICE_UUID,
Uuid::parse_str("0A0F0001-0000-1000-8000-00805F9B34FB").unwrap()
);
}
#[test]
fn lock_characteristic_uuids_are_correct() {
let cases = [
(LOCK_STATE_CHAR_UUID, "0A0F0001-0000-1000-8000-00805F9B34FB"),
(STATUS_CHAR_UUID, "0A0F0011-0000-1000-8000-00805F9B34FB"),
(LOCK_POSITION_CHAR_UUID, "0A0F0002-0000-1000-8000-00805F9B34FB"),
(COMMAND_CHAR_UUID, "0A0F0003-0000-1000-8000-00805F9B34FB"),
(DEVICE_INFO_CHAR_UUID, "0A0F0004-0000-1000-8000-00805F9B34FB"),
(EXTENDED_INFO_CHAR_UUID, "0A0F1004-0000-1000-8000-00805F9B34FB"),
(CONFIG_CHAR_UUID, "0A0F0005-0000-1000-8000-00805F9B34FB"),
(CONTROL_CHAR_UUID, "0A0F0006-0000-1000-8000-00805F9B34FB"),
];
for (uuid, expected) in cases {
assert_eq!(uuid, Uuid::parse_str(expected).unwrap(), "UUID mismatch for {expected}");
}
}
#[test]
fn standard_characteristic_uuids_are_correct() {
assert_eq!(
BATTERY_LEVEL_CHAR_UUID,
Uuid::parse_str("00002A19-0000-1000-8000-00805F9B34FB").unwrap()
);
assert_eq!(
FIRMWARE_REVISION_CHAR_UUID,
Uuid::parse_str("00002A26-0000-1000-8000-00805F9B34FB").unwrap()
);
assert_eq!(
DEVICE_NAME_CHAR_UUID,
Uuid::parse_str("00002A00-0000-1000-8000-00805F9B34FB").unwrap()
);
}
#[test]
fn lock_state_from_byte_valid_values() {
assert_eq!(LockState::from_byte(0x00), Some(LockState::Locked));
assert_eq!(LockState::from_byte(0x01), Some(LockState::Unlocked));
}
#[test]
fn lock_state_from_byte_invalid_values() {
(0x02..=0xFF).for_each(|b| assert_eq!(LockState::from_byte(b), None));
}
#[test]
fn lock_state_as_byte_roundtrip() {
[LockState::Locked, LockState::Unlocked]
.into_iter()
.for_each(|s| assert_eq!(LockState::from_byte(s.as_byte()), Some(s)));
}
#[test]
fn lock_state_predicates() {
assert!(LockState::Locked.is_locked());
assert!(!LockState::Locked.is_unlocked());
assert!(!LockState::Unlocked.is_locked());
assert!(LockState::Unlocked.is_unlocked());
}
#[test]
fn lock_state_try_from_valid() {
assert_eq!(LockState::try_from(0x00).unwrap(), LockState::Locked);
assert_eq!(LockState::try_from(0x01).unwrap(), LockState::Unlocked);
}
#[test]
fn lock_state_try_from_invalid() {
assert!(LockState::try_from(0x02).is_err());
assert!(LockState::try_from(0xFF).is_err());
}
#[test]
fn lock_state_into_u8() {
assert_eq!(u8::from(LockState::Locked), 0x00);
assert_eq!(u8::from(LockState::Unlocked), 0x01);
}
#[test]
fn device_info_equality() {
let a = DeviceInfo {
name: "Ohea Lock".to_string(),
firmware_version: "1.0".to_string(),
battery_level: 100,
};
let b = a.clone();
assert_eq!(a, b);
}
#[test]
fn device_name_matches_packet_capture() {
assert_eq!(DEVICE_NAME, "Ohea Lock");
assert_eq!(DEVICE_NAME.as_bytes(), &[0x4F, 0x68, 0x65, 0x61, 0x20, 0x4C, 0x6F, 0x63, 0x6B]);
}
#[test]
fn manufacturer_id_matches_packet_capture() {
assert_eq!(MANUFACTURER_ID, 0x0A0F);
}
#[test]
fn init_command_matches_packet_capture() {
assert_eq!(build_init_command(), [0x1A, 0x01, 0x04, 0x00, 0x31]);
}
#[test]
fn init_command_length_is_five_bytes() {
assert_eq!(build_init_command().len(), 5);
}
#[test]
fn att_error_codes_match_bluetooth_spec() {
assert_eq!(ATT_ERR_INSUFFICIENT_AUTH, 0x05);
assert_eq!(ATT_ERR_ATTR_NOT_FOUND, 0x0A);
}
}