From 1156fa865dd21e1f4eeee1fc8d440e7049958ad4 Mon Sep 17 00:00:00 2001 From: Paul Date: Sun, 22 Dec 2024 23:06:40 +0100 Subject: [PATCH] updated micodus_server --- src/parser/body.rs | 122 +++++++++++++++++++++++++++++++++++++-------- src/parser/mod.rs | 37 ++++++++------ 2 files changed, 122 insertions(+), 37 deletions(-) diff --git a/src/parser/body.rs b/src/parser/body.rs index 3d3b9f5..2a25d27 100644 --- a/src/parser/body.rs +++ b/src/parser/body.rs @@ -1,7 +1,10 @@ pub trait BodyMessage { fn build() {} - fn print(&mut self, body: &Vec) { - println!("{:?}", body); + 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); } } @@ -12,9 +15,9 @@ macro_rules! generate_impl { fn build() {} } impl $t { - pub fn new(body: &Vec) -> Self { + pub fn new(rawbody: &Vec) -> Self { let mut res = Self::default(); - res.parse(body); + res.parse(rawbody); res } })* @@ -29,8 +32,9 @@ pub struct TerminalUniversalResponse { } impl TerminalUniversalResponse { - const ID: u16 = 0x100; - pub fn parse(&mut self, body: &Vec) {} + pub const ID: u16 = 0x0001; + + pub fn parse(&mut self, rawbody: &Vec) {} pub fn debug() {} } @@ -42,14 +46,17 @@ pub struct PlatformUniversalResponse { } impl PlatformUniversalResponse { - pub fn parse(&mut self, body: &Vec) {} + pub const ID: u16 = 0x8001; + + pub fn parse(&mut self, rawbody: &Vec) {} } #[derive(Default, Debug)] pub struct TerminalHeartbeat {} impl TerminalHeartbeat { - pub fn parse(&mut self, body: &Vec) {} + pub const ID: u16 = 0x0002; + pub fn parse(&mut self, rawbody: &Vec) {} } #[derive(Default, Debug)] @@ -57,22 +64,68 @@ pub struct TerminalRegistration { pub provincial_id: u16, pub city_and_prefecture_id: u16, pub manufacturer_id: [u8; 5], - pub terminal_models: [u8; 8], + pub terminal_models: [u8; 20], //8 or 20 bytes pub terminal_id: [u8; 7], pub license_plate_color: u8, pub license_plate: String, } impl TerminalRegistration { - pub fn parse(&mut self, body: &Vec) {} - pub fn generate_reply(&self, serial: u16) -> TerminalRegistrationReply { + pub const ID: u16 = 0x0100; + + pub fn parse(&mut self, rawbody: &Vec) { + let mut bd = rawbody.into_iter(); //self.body_to_iter(rawbody); + self.provincial_id = u16::from_be_bytes( + vec![*bd.next().unwrap(), *bd.next().unwrap()] + .try_into() + .unwrap(), + ); + self.city_and_prefecture_id = u16::from_be_bytes( + vec![*bd.next().unwrap(), *bd.next().unwrap()] + .try_into() + .unwrap(), + ); + for i in 0..self.manufacturer_id.len() { + self.manufacturer_id[i] = *bd.next().unwrap(); + } + for i in 0..self.terminal_models.len() { + self.terminal_models[i] = *bd.next().unwrap(); + } + for i in 0..self.terminal_id.len() { + self.terminal_id[i] = *bd.next().unwrap(); + } + self.license_plate_color = *bd.next().unwrap(); + + while let Some(i) = bd.next() { + let c = char::from(*i); + self.license_plate.push(c); + } + } + pub fn generate_reply(&self, terminal_id: Vec, serial: u16) -> TerminalRegistrationReply { let mut res = TerminalRegistrationReply::default(); res.answer_serial_no = serial; res.result = TerminalRegistrationResult::Success as u8; + res.authentication_code = String::from_utf8(terminal_id).unwrap(); res } } +impl std::fmt::Display for TerminalRegistration { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!( + f, + "provincial: {}, city/pref: {}, manufacturer: {:?}, model: {:?}, terminal id: {}, license_plate_color: {}, license_plate: {}", + self.provincial_id, + self.city_and_prefecture_id, + self.manufacturer_id, + self.terminal_models, + String::from_utf8(self.terminal_id.try_into().unwrap()).unwrap(), + self.license_plate_color, + self.license_plate, + ) + } +} + enum TerminalRegistrationResult { Success = 0x00, VehicleRegistered, @@ -89,7 +142,9 @@ pub struct TerminalRegistrationReply { } impl TerminalRegistrationReply { - pub fn parse(&mut self, body: &Vec) {} + pub const ID: u16 = 0x8100; + + pub fn parse(&mut self, rawbody: &Vec) {} pub fn body_to_vec(&self) -> Vec { let mut res: Vec = vec![]; for b in self.answer_serial_no.to_be_bytes() { @@ -107,7 +162,15 @@ impl TerminalRegistrationReply { pub struct TerminalLogout {} impl TerminalLogout { - pub fn parse(&mut self, body: &Vec) {} + pub const ID: u16 = 0x0003; + + pub fn parse(&mut self, rawbody: &Vec) {} +} + +impl std::fmt::Display for TerminalLogout { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{}", self) + } } #[derive(Default, Debug)] @@ -116,7 +179,9 @@ pub struct TerminalAuthentication { } impl TerminalAuthentication { - pub fn parse(&mut self, body: &Vec) {} + pub const ID: u16 = 0x0102; + + pub fn parse(&mut self, rawbody: &Vec) {} } #[derive(Default, Debug)] @@ -126,7 +191,9 @@ pub struct TerminalParameterSetting { } impl TerminalParameterSetting { - pub fn parse(&mut self, body: &Vec) {} + pub const ID: u16 = 0x8103; + + pub fn parse(&mut self, rawbody: &Vec) {} } #[derive(Default, Debug)] @@ -137,13 +204,15 @@ pub struct TerminalParameterData { } impl TerminalParameterData { - pub fn parse(&mut self, body: &Vec) {} + pub fn parse(&mut self, rawbody: &Vec) {} } #[derive(Default, Debug)] pub struct QueryTerminalParameter {} impl QueryTerminalParameter { - pub fn parse(&mut self, body: &Vec) {} + pub const ID: u16 = 0x8104; + + pub fn parse(&mut self, rawbody: &Vec) {} } #[derive(Default, Debug)] @@ -154,7 +223,9 @@ pub struct QueryTerminalParameterResponse { } impl QueryTerminalParameterResponse { - pub fn parse(&mut self, body: &Vec) {} + pub const ID: u16 = 0x0104; + + pub fn parse(&mut self, rawbody: &Vec) {} } #[derive(Default, Debug)] @@ -164,7 +235,9 @@ pub struct TerminalControl { } impl TerminalControl { - pub fn parse(&mut self, body: &Vec) {} + pub const ID: u16 = 0x8105; + + pub fn parse(&mut self, rawbody: &Vec) {} } #[derive(Default, Debug)] @@ -180,19 +253,24 @@ pub struct LocationInformationReport { } impl LocationInformationReport { - pub fn parse(&mut self, body: &Vec) {} + pub const ID: u16 = 0x0200; + pub fn parse(&mut self, rawbody: &Vec) {} } #[derive(Default, Debug)] pub struct StartOfTrip {} impl StartOfTrip { - pub fn parse(&mut self, body: &Vec) {} + pub const ID: u16 = 0x0202; + + pub fn parse(&mut self, rawbody: &Vec) {} } #[derive(Default, Debug)] pub struct EndOfTrip {} impl EndOfTrip { - pub fn parse(&mut self, body: &Vec) {} + pub const ID: u16 = 0x0203; + + pub fn parse(&mut self, rawbody: &Vec) {} } generate_impl!( diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 1e18ba6..4e19dc2 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -117,54 +117,60 @@ impl Message { pub fn parse_body(&mut self) { match self.header.id { - 0x0001 => { + TerminalUniversalResponse::ID => { self.content = MessageType::TerminalUniversalResponse( TerminalUniversalResponse::new(&self.body), ) } - 0x8001 => { + PlatformUniversalResponse::ID => { self.content = MessageType::PlatformUniversalResponse( PlatformUniversalResponse::new(&self.body), ) } - 0x0002 => { + TerminalHeartbeat::ID => { self.content = MessageType::TerminalHeartbeat(TerminalHeartbeat::new(&self.body)) } - 0x0100 => { + TerminalRegistration::ID => { self.content = MessageType::TerminalRegistration(TerminalRegistration::new(&self.body)) } - 0x8100 => { + TerminalRegistrationReply::ID => { self.content = MessageType::TerminalRegistrationReply( TerminalRegistrationReply::new(&self.body), ) } - 0x0003 => self.content = MessageType::TerminalLogout(TerminalLogout::new(&self.body)), - 0x0102 => { + TerminalLogout::ID => { + self.content = MessageType::TerminalLogout(TerminalLogout::new(&self.body)) + } + TerminalAuthentication::ID => { self.content = MessageType::TerminalAuthentication(TerminalAuthentication::new(&self.body)) } - 0x8103 => { + TerminalParameterSetting::ID => { self.content = MessageType::TerminalParameterSetting(TerminalParameterSetting::new(&self.body)) } - 0x8104 => { + QueryTerminalParameter::ID => { self.content = MessageType::QueryTerminalParameter(QueryTerminalParameter::new(&self.body)) } - 0x0104 => { + QueryTerminalParameterResponse::ID => { self.content = MessageType::QueryTerminalParameterResponse( QueryTerminalParameterResponse::new(&self.body), ) } - 0x8105 => self.content = MessageType::TerminalControl(TerminalControl::new(&self.body)), - 0x0200 => { + TerminalControl::ID => { + self.content = MessageType::TerminalControl(TerminalControl::new(&self.body)) + } + LocationInformationReport::ID => { self.content = MessageType::LocationInformationReport( LocationInformationReport::new(&self.body), ) } - 0x0202 => self.content = MessageType::StartOfTrip(StartOfTrip::new(&self.body)), - 0x0203 => self.content = MessageType::EndOfTrip(EndOfTrip::new(&self.body)), + StartOfTrip::ID => { + self.content = MessageType::StartOfTrip(StartOfTrip::new(&self.body)) + } + EndOfTrip::ID => self.content = MessageType::EndOfTrip(EndOfTrip::new(&self.body)), _ => self.content = MessageType::None, } } @@ -174,7 +180,7 @@ impl Message { match msg.content { MessageType::TerminalRegistration(t) => { reply.content = MessageType::TerminalRegistrationReply( - t.generate_reply(msg.header.serial_number), + t.generate_reply(msg.header.raw_terminal_id.into(), msg.header.serial_number), ); } _ => {} @@ -228,6 +234,7 @@ impl Default for MessageType { Self::None } } + impl std::fmt::Display for MessageType { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{:?}", self)