updated micodus_server

This commit is contained in:
Paul 2024-12-22 23:06:40 +01:00
parent c52556eb2e
commit 1156fa865d
2 changed files with 122 additions and 37 deletions

View File

@ -1,7 +1,10 @@
pub trait BodyMessage { pub trait BodyMessage {
fn build() {} fn build() {}
fn print(&mut self, body: &Vec<u8>) { fn body_to_iter<'a>(&'a self, rawbody: &'a Vec<u8>) -> std::slice::Iter<'a, u8> {
println!("{:?}", body); rawbody.into_iter()
}
fn print(&mut self, rawbody: &Vec<u8>) {
println!("{:?}", rawbody);
} }
} }
@ -12,9 +15,9 @@ macro_rules! generate_impl {
fn build() {} fn build() {}
} }
impl $t { impl $t {
pub fn new(body: &Vec<u8>) -> Self { pub fn new(rawbody: &Vec<u8>) -> Self {
let mut res = Self::default(); let mut res = Self::default();
res.parse(body); res.parse(rawbody);
res res
} }
})* })*
@ -29,8 +32,9 @@ pub struct TerminalUniversalResponse {
} }
impl TerminalUniversalResponse { impl TerminalUniversalResponse {
const ID: u16 = 0x100; pub const ID: u16 = 0x0001;
pub fn parse(&mut self, body: &Vec<u8>) {}
pub fn parse(&mut self, rawbody: &Vec<u8>) {}
pub fn debug() {} pub fn debug() {}
} }
@ -42,14 +46,17 @@ pub struct PlatformUniversalResponse {
} }
impl PlatformUniversalResponse { impl PlatformUniversalResponse {
pub fn parse(&mut self, body: &Vec<u8>) {} pub const ID: u16 = 0x8001;
pub fn parse(&mut self, rawbody: &Vec<u8>) {}
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct TerminalHeartbeat {} pub struct TerminalHeartbeat {}
impl TerminalHeartbeat { impl TerminalHeartbeat {
pub fn parse(&mut self, body: &Vec<u8>) {} pub const ID: u16 = 0x0002;
pub fn parse(&mut self, rawbody: &Vec<u8>) {}
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
@ -57,22 +64,68 @@ pub struct TerminalRegistration {
pub provincial_id: u16, pub provincial_id: u16,
pub city_and_prefecture_id: u16, pub city_and_prefecture_id: u16,
pub manufacturer_id: [u8; 5], 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 terminal_id: [u8; 7],
pub license_plate_color: u8, pub license_plate_color: u8,
pub license_plate: String, pub license_plate: String,
} }
impl TerminalRegistration { impl TerminalRegistration {
pub fn parse(&mut self, body: &Vec<u8>) {} pub const ID: u16 = 0x0100;
pub fn generate_reply(&self, serial: u16) -> TerminalRegistrationReply {
pub fn parse(&mut self, rawbody: &Vec<u8>) {
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<u8>, serial: u16) -> TerminalRegistrationReply {
let mut res = TerminalRegistrationReply::default(); let mut res = TerminalRegistrationReply::default();
res.answer_serial_no = serial; res.answer_serial_no = serial;
res.result = TerminalRegistrationResult::Success as u8; res.result = TerminalRegistrationResult::Success as u8;
res.authentication_code = String::from_utf8(terminal_id).unwrap();
res 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 { enum TerminalRegistrationResult {
Success = 0x00, Success = 0x00,
VehicleRegistered, VehicleRegistered,
@ -89,7 +142,9 @@ pub struct TerminalRegistrationReply {
} }
impl TerminalRegistrationReply { impl TerminalRegistrationReply {
pub fn parse(&mut self, body: &Vec<u8>) {} pub const ID: u16 = 0x8100;
pub fn parse(&mut self, rawbody: &Vec<u8>) {}
pub fn body_to_vec(&self) -> Vec<u8> { pub fn body_to_vec(&self) -> Vec<u8> {
let mut res: Vec<u8> = vec![]; let mut res: Vec<u8> = vec![];
for b in self.answer_serial_no.to_be_bytes() { for b in self.answer_serial_no.to_be_bytes() {
@ -107,7 +162,15 @@ impl TerminalRegistrationReply {
pub struct TerminalLogout {} pub struct TerminalLogout {}
impl TerminalLogout { impl TerminalLogout {
pub fn parse(&mut self, body: &Vec<u8>) {} pub const ID: u16 = 0x0003;
pub fn parse(&mut self, rawbody: &Vec<u8>) {}
}
impl std::fmt::Display for TerminalLogout {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self)
}
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
@ -116,7 +179,9 @@ pub struct TerminalAuthentication {
} }
impl TerminalAuthentication { impl TerminalAuthentication {
pub fn parse(&mut self, body: &Vec<u8>) {} pub const ID: u16 = 0x0102;
pub fn parse(&mut self, rawbody: &Vec<u8>) {}
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
@ -126,7 +191,9 @@ pub struct TerminalParameterSetting {
} }
impl TerminalParameterSetting { impl TerminalParameterSetting {
pub fn parse(&mut self, body: &Vec<u8>) {} pub const ID: u16 = 0x8103;
pub fn parse(&mut self, rawbody: &Vec<u8>) {}
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
@ -137,13 +204,15 @@ pub struct TerminalParameterData {
} }
impl TerminalParameterData { impl TerminalParameterData {
pub fn parse(&mut self, body: &Vec<u8>) {} pub fn parse(&mut self, rawbody: &Vec<u8>) {}
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct QueryTerminalParameter {} pub struct QueryTerminalParameter {}
impl QueryTerminalParameter { impl QueryTerminalParameter {
pub fn parse(&mut self, body: &Vec<u8>) {} pub const ID: u16 = 0x8104;
pub fn parse(&mut self, rawbody: &Vec<u8>) {}
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
@ -154,7 +223,9 @@ pub struct QueryTerminalParameterResponse {
} }
impl QueryTerminalParameterResponse { impl QueryTerminalParameterResponse {
pub fn parse(&mut self, body: &Vec<u8>) {} pub const ID: u16 = 0x0104;
pub fn parse(&mut self, rawbody: &Vec<u8>) {}
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
@ -164,7 +235,9 @@ pub struct TerminalControl {
} }
impl TerminalControl { impl TerminalControl {
pub fn parse(&mut self, body: &Vec<u8>) {} pub const ID: u16 = 0x8105;
pub fn parse(&mut self, rawbody: &Vec<u8>) {}
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
@ -180,19 +253,24 @@ pub struct LocationInformationReport {
} }
impl LocationInformationReport { impl LocationInformationReport {
pub fn parse(&mut self, body: &Vec<u8>) {} pub const ID: u16 = 0x0200;
pub fn parse(&mut self, rawbody: &Vec<u8>) {}
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct StartOfTrip {} pub struct StartOfTrip {}
impl StartOfTrip { impl StartOfTrip {
pub fn parse(&mut self, body: &Vec<u8>) {} pub const ID: u16 = 0x0202;
pub fn parse(&mut self, rawbody: &Vec<u8>) {}
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct EndOfTrip {} pub struct EndOfTrip {}
impl EndOfTrip { impl EndOfTrip {
pub fn parse(&mut self, body: &Vec<u8>) {} pub const ID: u16 = 0x0203;
pub fn parse(&mut self, rawbody: &Vec<u8>) {}
} }
generate_impl!( generate_impl!(

View File

@ -117,54 +117,60 @@ impl Message {
pub fn parse_body(&mut self) { pub fn parse_body(&mut self) {
match self.header.id { match self.header.id {
0x0001 => { TerminalUniversalResponse::ID => {
self.content = MessageType::TerminalUniversalResponse( self.content = MessageType::TerminalUniversalResponse(
TerminalUniversalResponse::new(&self.body), TerminalUniversalResponse::new(&self.body),
) )
} }
0x8001 => { PlatformUniversalResponse::ID => {
self.content = MessageType::PlatformUniversalResponse( self.content = MessageType::PlatformUniversalResponse(
PlatformUniversalResponse::new(&self.body), PlatformUniversalResponse::new(&self.body),
) )
} }
0x0002 => { TerminalHeartbeat::ID => {
self.content = MessageType::TerminalHeartbeat(TerminalHeartbeat::new(&self.body)) self.content = MessageType::TerminalHeartbeat(TerminalHeartbeat::new(&self.body))
} }
0x0100 => { TerminalRegistration::ID => {
self.content = self.content =
MessageType::TerminalRegistration(TerminalRegistration::new(&self.body)) MessageType::TerminalRegistration(TerminalRegistration::new(&self.body))
} }
0x8100 => { TerminalRegistrationReply::ID => {
self.content = MessageType::TerminalRegistrationReply( self.content = MessageType::TerminalRegistrationReply(
TerminalRegistrationReply::new(&self.body), TerminalRegistrationReply::new(&self.body),
) )
} }
0x0003 => self.content = MessageType::TerminalLogout(TerminalLogout::new(&self.body)), TerminalLogout::ID => {
0x0102 => { self.content = MessageType::TerminalLogout(TerminalLogout::new(&self.body))
}
TerminalAuthentication::ID => {
self.content = self.content =
MessageType::TerminalAuthentication(TerminalAuthentication::new(&self.body)) MessageType::TerminalAuthentication(TerminalAuthentication::new(&self.body))
} }
0x8103 => { TerminalParameterSetting::ID => {
self.content = self.content =
MessageType::TerminalParameterSetting(TerminalParameterSetting::new(&self.body)) MessageType::TerminalParameterSetting(TerminalParameterSetting::new(&self.body))
} }
0x8104 => { QueryTerminalParameter::ID => {
self.content = self.content =
MessageType::QueryTerminalParameter(QueryTerminalParameter::new(&self.body)) MessageType::QueryTerminalParameter(QueryTerminalParameter::new(&self.body))
} }
0x0104 => { QueryTerminalParameterResponse::ID => {
self.content = MessageType::QueryTerminalParameterResponse( self.content = MessageType::QueryTerminalParameterResponse(
QueryTerminalParameterResponse::new(&self.body), QueryTerminalParameterResponse::new(&self.body),
) )
} }
0x8105 => self.content = MessageType::TerminalControl(TerminalControl::new(&self.body)), TerminalControl::ID => {
0x0200 => { self.content = MessageType::TerminalControl(TerminalControl::new(&self.body))
}
LocationInformationReport::ID => {
self.content = MessageType::LocationInformationReport( self.content = MessageType::LocationInformationReport(
LocationInformationReport::new(&self.body), LocationInformationReport::new(&self.body),
) )
} }
0x0202 => self.content = MessageType::StartOfTrip(StartOfTrip::new(&self.body)), StartOfTrip::ID => {
0x0203 => self.content = MessageType::EndOfTrip(EndOfTrip::new(&self.body)), self.content = MessageType::StartOfTrip(StartOfTrip::new(&self.body))
}
EndOfTrip::ID => self.content = MessageType::EndOfTrip(EndOfTrip::new(&self.body)),
_ => self.content = MessageType::None, _ => self.content = MessageType::None,
} }
} }
@ -174,7 +180,7 @@ impl Message {
match msg.content { match msg.content {
MessageType::TerminalRegistration(t) => { MessageType::TerminalRegistration(t) => {
reply.content = MessageType::TerminalRegistrationReply( 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 Self::None
} }
} }
impl std::fmt::Display for MessageType { impl std::fmt::Display for MessageType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self) write!(f, "{:?}", self)