From c45512ec4f7796b576d833e7c478b822e4362d2e Mon Sep 17 00:00:00 2001 From: Ian Armour Date: Tue, 5 Apr 2022 22:33:13 -0700 Subject: [PATCH] Added basic protocol stuff still very much wip --- .gitignore | 5 ++ Cargo.toml | 10 ++++ src/main.rs | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index 088ba6b..22d3516 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,8 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk + + +# Added by cargo + +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..da2251e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ancs-rs" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nom = "7.1.1" +byteorder = "1.4.3" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..40ea430 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,151 @@ +use byteorder::{LittleEndian}; +use std::str; +use std::convert::{ + Into, + TryFrom, +}; +use nom::{ + number::complete::{be_u8, be_u16}, + multi::count, + IResult, +}; + +#[repr(u8)] +enum EventID { + notification_added = 0, + notification_modified = 1, + notification_removed = 2, +} + +enum EventFlag { + silent = 0b00000001, + important = 0b00000010, + pre_existing = 0b00000100, + positive_action = 0b00001000, + negative_action = 0b00010000, +} + +#[repr(u8)] +enum CategoryID { + other = 0, + incoming_call = 1, + missed_call = 2, + voicemail = 3, + social = 4, + schedule = 5, + email = 6, + news = 7, + health_and_fitness = 8, + business_and_finance = 9, + location = 10, + entertainment = 11, +} + +#[repr(u8)] +enum CommandID { + get_notification_attributes = 0, + get_app_attributes = 1, + perform_notification_action = 2, +} + +struct Attribute(AttributeID, u16, String); + +impl Attribute { + pub fn parse(i:&[u8]) -> IResult<&[u8], Self> { + let (i, id) = be_u8(i)?; + let (i, length) = be_u16(i)?; + let (i, attribute) = count(be_u8, length.into())(i)?; + + Ok((i, Attribute(AttributeID::try_from(id).unwrap(), length, String::from_utf8(attribute).unwrap()))) + } +} + +#[repr(u8)] +enum AttributeID { + app_identifier = 0, + title = 1, + subtitle = 2, + message = 3, + message_size = 4, + date = 5, + positive_action_label = 6, + negative_action_label = 7, +} + +impl TryFrom for AttributeID { + type Error = (); + + fn try_from(v: u8) -> Result { + match v { + x if x == AttributeID::app_identifier as u8 => Ok(AttributeID::app_identifier), + x if x == AttributeID::title as u8 => Ok(AttributeID::title), + x if x == AttributeID::subtitle as u8 => Ok(AttributeID::subtitle), + x if x == AttributeID::message as u8 => Ok(AttributeID::message), + x if x == AttributeID::message_size as u8 => Ok(AttributeID::message_size), + x if x == AttributeID::date as u8 => Ok(AttributeID::date), + x if x == AttributeID::positive_action_label as u8 => Ok(AttributeID::positive_action_label), + x if x == AttributeID::negative_action_label as u8 => Ok(AttributeID::negative_action_label), + _ => Err(()), + } + } +} + +struct NotificationSourceCharacteristic { + event_id: EventID, + event_flags: EventFlag, + category_id: CategoryID, + category_count: u8, + notification_uuid: u32, +} + +impl Into> for NotificationSourceCharacteristic { + fn into(self) -> Vec { + let id = self.event_id as u8; + } +} + +struct GetNotificationAttributesRequest { + command_id: CommandID, + notification_uuid: u32, + attribute_ids: Vec, +} + +impl Into> for GetNotificationAttributesRequest { + fn into(self) -> Vec { + let id = self.command_id as u8; + let notification_uuid = self.notification_uuid.to_le_bytes(); + let mut attribute_ids = self.attribute_ids.into_iter().map(|id| id as u8).collect(); + + let mut v: Vec = Vec::new(); + + v.push(id); + v.extend(notification_uuid); + v.append(&mut attribute_ids); + + return v; + } +} + +struct GetNotificationAttributesResponse { + command_id: CommandID, + notification_uuid: u32, + attribute_list: Vec, +} + +struct GetAppAttributesRequest { + command_id: CommandID, + app_identifier: String, + attribute_ids: Vec, +} + +struct GetAppAttributesResponse { + command_id: CommandID, + app_identifier: String, + attribute_list: Vec, +} + + + +fn main() { + println!("Hello, world!"); +} \ No newline at end of file