From 8791b08c4f3ba31d0dd1e9f83f8f01c1a8647b61 Mon Sep 17 00:00:00 2001 From: jane400 Date: Wed, 28 Aug 2024 15:21:29 +0200 Subject: [PATCH] feat: ble data parsing: version --- libpaket/src/locker/types.rs | 94 ++++++++++++++++++++++++++++++++++-- 1 file changed, 89 insertions(+), 5 deletions(-) diff --git a/libpaket/src/locker/types.rs b/libpaket/src/locker/types.rs index 7667cfc..4a95ba4 100644 --- a/libpaket/src/locker/types.rs +++ b/libpaket/src/locker/types.rs @@ -1,23 +1,107 @@ -use crate::LibraryError; +use btleplug::platform::PeripheralId; + +use super::utils::PrimitiveReader; +use crate::{LibraryError, LibraryResult}; // 601e7028-0565- pub static LOCKER_SERVICE_UUID_PREFIX: (u32, u16) = (0x601e7028, 0x0565); +pub enum LockerVendor { + Keba, + Snbc, + Unknown, +} + +impl From for LockerVendor { + fn from(value: u8) -> Self { + if value == 1 { + Self::Keba + } else if value == 2 { + Self::Snbc + } else { + Self::Unknown + } + } +} + +#[derive(Debug)] +pub struct LockerVersion { + pub tlv_version: u8, + pub vendor: u8, + pub major: u8, + pub minor: u8, +} + +#[derive(Debug)] +pub struct LockerDevice { + pub id: PeripheralId, + pub service_uuid: LockerServiceUUID, + pub version: LockerVersion, +} + +impl LockerDevice { + pub(crate) fn new( + id: PeripheralId, + service_uuid: LockerServiceUUID, + service_data: &Vec, + ) -> LibraryResult { + mini_assert_inval!(service_data.len() == 14); + + let mut reader = PrimitiveReader { + offset: 0, + vec: &service_data, + }; + + let version = LockerVersion { + tlv_version: reader.read_u8(), + vendor: reader.read_u8(), + major: reader.read_u8(), + minor: reader.read_u8(), + }; + let part1: u16 = reader.read_u16(); + let part2: u16 = reader.read_u16(); + let part_last = reader.read_u32(); + assert_eq!(reader.left_to_process(), 0); + + println!( + "decoded: {:0>8x}-{:0>4x}-{:0>4x}-{:0>4x}-{:0>8x}", + LOCKER_SERVICE_UUID_PREFIX.0, LOCKER_SERVICE_UUID_PREFIX.1, part1, part2, part_last + ); + println!("expected: {:?}", service_uuid); + + Ok(LockerDevice { + id, + service_uuid, + version, + }) + } +} + +#[derive(Debug)] pub struct LockerServiceUUID { service_uuid: uuid::Uuid, } +impl LockerServiceUUID { + pub fn get(&self) -> &uuid::Uuid { + &self.service_uuid + } +} + impl TryFrom for LockerServiceUUID { type Error = crate::LibraryError; - fn try_from(value: uuid::Uuid) -> Result { + fn try_from(value: uuid::Uuid) -> LibraryResult { let fields = value.as_fields(); if fields.0 != LOCKER_SERVICE_UUID_PREFIX.0 || fields.1 != LOCKER_SERVICE_UUID_PREFIX.1 { - return Err(LibraryError::InvalidArgument("TryFrom for LockerServiceUUID: prefix mismatch (expected 601e7028-0565-)".to_string())) + return Err(LibraryError::InvalidArgument( + "TryFrom for LockerServiceUUID: prefix mismatch (expected 601e7028-0565-)" + .to_string(), + )); } Ok(LockerServiceUUID { - service_uuid: value + service_uuid: value, }) } } @@ -26,4 +110,4 @@ impl Into for LockerServiceUUID { fn into(self) -> uuid::Uuid { self.service_uuid } -} \ No newline at end of file +}