From e9c4e5b25f43b6a8342aa0221f6d2aca44e0abba Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 24 Dec 2024 09:26:03 +0100 Subject: [PATCH] prepare iterator --- src/main.rs | 2 +- src/parser/body.rs | 20 +++++++++---------- src/parser/mod.rs | 50 ++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0d9268e..0dcaa63 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,7 +59,7 @@ pub async fn apiserver() -> io::Result<()> { } pub fn handle(buf: &Vec) { - let mut rawdata = buf.to_vec(); + let mut rawdata = DataWrapper::new(buf.to_vec()); match parse_inbound_msg(&mut rawdata) { Ok(o) => { println!("msg: {}", o); diff --git a/src/parser/body.rs b/src/parser/body.rs index 2a25d27..9afbcb2 100644 --- a/src/parser/body.rs +++ b/src/parser/body.rs @@ -1,13 +1,3 @@ -pub trait BodyMessage { - fn build() {} - fn body_to_iter<'a>(&'a self, rawbody: &'a Vec) -> std::slice::Iter<'a, u8> { - rawbody.into_iter() - } - fn print(&mut self, rawbody: &Vec) { - println!("{:?}", rawbody); - } -} - macro_rules! generate_impl { ($($t:ty),*) => { $( @@ -24,6 +14,16 @@ macro_rules! generate_impl { }; } +pub trait BodyMessage { + fn build() {} + fn body_to_iter<'a>(&'a self, rawbody: &'a Vec) -> std::slice::Iter<'a, u8> { + rawbody.into_iter() + } + fn print(&mut self, rawbody: &Vec) { + println!("{:?}", rawbody); + } +} + #[derive(Default, Debug)] pub struct TerminalUniversalResponse { pub answer_serial_no: u16, diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 1914b6a..31084f9 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -17,7 +17,7 @@ const PROPS_RESERVED_MASK: u16 = 0xC000; #[allow(dead_code)] const PROPS_DATAENCRYPTION_MASK: u16 = 0x0E00; -pub fn parse_inbound_msg(rawdata: &mut Vec) -> std::result::Result { +pub fn parse_inbound_msg(rawdata: &mut DataWrapper) -> std::result::Result { let mut msg: Message = Message::default(); match msg.parse_header(rawdata) { @@ -33,6 +33,35 @@ pub fn parse_inbound_msg(rawdata: &mut Vec) -> std::result::Result, + pub count: usize, +} + +impl DataWrapper { + pub fn new(data: Vec) -> DataWrapper { + DataWrapper { data, count: 0 } + } +} + +impl Iterator for DataWrapper { + type Item = u8; + + fn next(&mut self) -> Option { + let res: Option; + + if self.count < 6 { + res = self.data.pop() + } else { + res = None + } + self.count += 1; + + res + } +} + #[derive(Default, Debug)] pub struct Message { pub header: MessageHeader, @@ -55,9 +84,9 @@ impl std::fmt::Display for Message { impl Message { pub fn parse_header( &mut self, - rawdata: &mut Vec, + rawdata: &mut DataWrapper, ) -> std::result::Result<(), NotOurProtocolError> { - let mut data = rawdata.clone().into_iter(); + let mut data = rawdata.data.clone().into_iter(); let first_byte = data.next().unwrap(); if first_byte != FLAGDELIMITER { return Err(NotOurProtocolError); @@ -94,6 +123,12 @@ impl Message { pub fn parse_properties(&mut self) { self.header.bodylength = (self.header.properties & PROPS_SIZE_MASK) as u8; + self.header.encryption = + ((self.header.properties & PROPS_DATAENCRYPTION_MASK) as u16 >> 10) as u8; + self.header.encrypted = self.header.encryption != 0; + self.header.subcontract = + ((self.header.properties & PROPS_SUBCONTRACT_MASK) as u16 >> 13) == 1; + self.header.reserved = ((self.header.properties & PROPS_RESERVED_MASK) as u16 >> 14) as u16; } pub fn parse_terminal_id(&mut self) { @@ -101,8 +136,8 @@ impl Message { self.header.terminal_id = code.to_u64().unwrap(); } - pub fn check(&mut self, rawdata: &mut Vec) { - let mut data = rawdata.into_iter(); + pub fn check(&mut self, rawdata: &mut DataWrapper) { + let mut data = rawdata.data.iter(); data.next().unwrap(); let mut check: u8 = 0; @@ -204,7 +239,10 @@ pub struct MessageHeader { id: u16, properties: u16, pub bodylength: u8, - pub encryption: bool, + encryption: u8, + pub encrypted: bool, + pub subcontract: bool, + reserved: u16, raw_terminal_id: [u8; 6], pub terminal_id: u64, pub serial_number: u16,