371 lines
11 KiB
Rust
371 lines
11 KiB
Rust
mod body;
|
|
mod error;
|
|
mod header;
|
|
|
|
use body::*;
|
|
use error::*;
|
|
use header::*;
|
|
|
|
use std::collections::VecDeque;
|
|
|
|
const FLAGDELIMITER: u8 = 0x7E;
|
|
|
|
pub fn parse_inbound_msg(
|
|
rawdata: &mut InboundDataWrapper,
|
|
) -> std::result::Result<Message, MessageError> {
|
|
let mut msg: Message = Message::default();
|
|
|
|
match msg.parse_header(rawdata) {
|
|
Ok(_) => {
|
|
msg.inbound_check(rawdata);
|
|
}
|
|
Err(e) => {
|
|
println!("error parsing header {e}");
|
|
return Err(MessageError);
|
|
}
|
|
};
|
|
|
|
Ok(msg)
|
|
}
|
|
|
|
#[derive(Default, Debug)]
|
|
pub struct InboundDataWrapper {
|
|
pub data: VecDeque<u8>,
|
|
pub count: usize,
|
|
pub start: bool,
|
|
pub end: bool,
|
|
}
|
|
|
|
impl InboundDataWrapper {
|
|
pub fn new(data: Vec<u8>) -> InboundDataWrapper {
|
|
InboundDataWrapper {
|
|
data: data.into(),
|
|
count: 0,
|
|
start: false,
|
|
end: false,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Iterator for InboundDataWrapper {
|
|
type Item = u8;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
let res: Option<u8>;
|
|
|
|
loop {
|
|
match self.data.pop_front() {
|
|
Some(o) => {
|
|
if o == FLAGDELIMITER {
|
|
if self.start {
|
|
self.end = true;
|
|
res = None;
|
|
break;
|
|
}
|
|
self.start = true;
|
|
continue;
|
|
}
|
|
res = Some(o);
|
|
}
|
|
None => {
|
|
res = None;
|
|
}
|
|
};
|
|
break;
|
|
}
|
|
self.count += 1;
|
|
|
|
res
|
|
}
|
|
}
|
|
|
|
#[derive(Default, Debug)]
|
|
pub struct Message {
|
|
pub header: MessageHeader,
|
|
pub content: MessageType,
|
|
pub body: Vec<u8>,
|
|
pub checksum: u8,
|
|
valid: bool,
|
|
}
|
|
|
|
impl Message {
|
|
pub fn new() -> Self {
|
|
let msg = Self::default();
|
|
return msg;
|
|
}
|
|
fn parse_header(
|
|
&mut self,
|
|
rawdata: &mut InboundDataWrapper,
|
|
) -> std::result::Result<(), NotOurProtocolError> {
|
|
let data = rawdata.into_iter();
|
|
self.header.set_id(u16::from_be_bytes(
|
|
vec![data.next().unwrap(), data.next().unwrap()]
|
|
.try_into()
|
|
.unwrap(),
|
|
));
|
|
|
|
self.header.set_properties(u16::from_be_bytes(
|
|
vec![data.next().unwrap(), data.next().unwrap()]
|
|
.try_into()
|
|
.unwrap(),
|
|
));
|
|
self.header.parse_properties();
|
|
|
|
let mut rtid: [u8; 6] = [0; 6];
|
|
for i in 0..rtid.len() {
|
|
rtid[i] = data.next().unwrap();
|
|
}
|
|
self.header.set_raw_terminal_id(rtid);
|
|
|
|
self.header.parse_terminal_id();
|
|
|
|
self.header.serial_number = u16::from_be_bytes(
|
|
vec![data.next().unwrap(), data.next().unwrap()]
|
|
.try_into()
|
|
.unwrap(),
|
|
);
|
|
|
|
for _ in 0..self.header.bodylength as usize {
|
|
self.body.push(data.next().unwrap());
|
|
}
|
|
|
|
self.checksum = data.next().unwrap();
|
|
|
|
self.parse_body();
|
|
Ok(())
|
|
}
|
|
|
|
pub fn inbound_check(&mut self, rawdata: &mut InboundDataWrapper) {
|
|
let data = rawdata.into_iter();
|
|
let mut check: u8 = 0;
|
|
|
|
let _ = data.map(|b| check ^= b);
|
|
|
|
self.inbound_validate(check);
|
|
}
|
|
|
|
fn inbound_validate(&mut self, check: u8) {
|
|
self.valid = check == 0;
|
|
}
|
|
|
|
fn _is_valid(&self) -> bool {
|
|
self.valid
|
|
}
|
|
|
|
fn parse_body(&mut self) {
|
|
match self.header.get_id() {
|
|
TerminalUniversalResponse::ID => {
|
|
self.content = MessageType::TerminalUniversalResponse(
|
|
TerminalUniversalResponse::new(&self.body),
|
|
)
|
|
}
|
|
PlatformUniversalResponse::ID => {
|
|
self.content = MessageType::PlatformUniversalResponse(
|
|
PlatformUniversalResponse::new(&self.body),
|
|
)
|
|
}
|
|
TerminalHeartbeat::ID => {
|
|
self.content = MessageType::TerminalHeartbeat(TerminalHeartbeat::new(&self.body))
|
|
}
|
|
TerminalRegistration::ID => {
|
|
self.content =
|
|
MessageType::TerminalRegistration(TerminalRegistration::new(&self.body))
|
|
}
|
|
TerminalRegistrationReply::ID => {
|
|
self.content = MessageType::TerminalRegistrationReply(
|
|
TerminalRegistrationReply::new(&self.body),
|
|
)
|
|
}
|
|
TerminalLogout::ID => {
|
|
self.content = MessageType::TerminalLogout(TerminalLogout::new(&self.body))
|
|
}
|
|
TerminalAuthentication::ID => {
|
|
self.content =
|
|
MessageType::TerminalAuthentication(TerminalAuthentication::new(&self.body))
|
|
}
|
|
TerminalParameterSetting::ID => {
|
|
self.content =
|
|
MessageType::TerminalParameterSetting(TerminalParameterSetting::new(&self.body))
|
|
}
|
|
QueryTerminalParameter::ID => {
|
|
self.content =
|
|
MessageType::QueryTerminalParameter(QueryTerminalParameter::new(&self.body))
|
|
}
|
|
QueryTerminalParameterResponse::ID => {
|
|
self.content = MessageType::QueryTerminalParameterResponse(
|
|
QueryTerminalParameterResponse::new(&self.body),
|
|
)
|
|
}
|
|
TerminalControl::ID => {
|
|
self.content = MessageType::TerminalControl(TerminalControl::new(&self.body))
|
|
}
|
|
LocationInformationReport::ID => {
|
|
self.content = MessageType::LocationInformationReport(
|
|
LocationInformationReport::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::TerminalUniversalResponse(
|
|
TerminalUniversalResponse::new(&self.body),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn set_reply(inmsg: Message) -> Option<Message> {
|
|
let mut reply: Message = Message::default();
|
|
let terminal_id = inmsg.header.get_raw_terminal_id().clone();
|
|
match inmsg.content {
|
|
MessageType::TerminalRegistration(t) => {
|
|
let cnt = t.generate_reply(terminal_id.into(), inmsg.header.serial_number);
|
|
reply.header.build(
|
|
TerminalRegistrationReply::ID,
|
|
cnt.to_raw().len(),
|
|
terminal_id,
|
|
);
|
|
reply.content = MessageType::TerminalRegistrationReply(cnt);
|
|
}
|
|
MessageType::TerminalAuthentication(t) => {
|
|
let cnt = t.generate_reply(TerminalAuthentication::ID, inmsg.header.serial_number);
|
|
reply.header.build(
|
|
PlatformUniversalResponse::ID,
|
|
cnt.to_raw().len(),
|
|
terminal_id,
|
|
);
|
|
reply.content = MessageType::PlatformUniversalResponse(cnt);
|
|
}
|
|
MessageType::LocationInformationReport(t) => {
|
|
let cnt =
|
|
t.generate_reply(LocationInformationReport::ID, inmsg.header.serial_number);
|
|
reply.header.build(
|
|
PlatformUniversalResponse::ID,
|
|
cnt.to_raw().len(),
|
|
terminal_id,
|
|
);
|
|
reply.content = MessageType::PlatformUniversalResponse(cnt);
|
|
}
|
|
MessageType::TerminalHeartbeat(t) => {
|
|
let cnt = t.generate_reply(TerminalHeartbeat::ID, inmsg.header.serial_number);
|
|
reply.header.build(
|
|
PlatformUniversalResponse::ID,
|
|
cnt.to_raw().len(),
|
|
terminal_id,
|
|
);
|
|
reply.content = MessageType::PlatformUniversalResponse(cnt);
|
|
}
|
|
MessageType::TerminalLogout(t) => {
|
|
let cnt = t.generate_reply(TerminalHeartbeat::ID, inmsg.header.serial_number);
|
|
reply.header.build(
|
|
PlatformUniversalResponse::ID,
|
|
cnt.to_raw().len(),
|
|
terminal_id,
|
|
);
|
|
reply.content = MessageType::PlatformUniversalResponse(cnt);
|
|
}
|
|
_ => {
|
|
println!("no type");
|
|
return None;
|
|
}
|
|
}
|
|
reply.outbound_finalize();
|
|
Some(reply)
|
|
}
|
|
|
|
pub fn store(inmsg: &Message) {
|
|
match inmsg.content {
|
|
MessageType::LocationInformationReport(ref t) => {
|
|
println!("{:?}", t);
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
pub fn outbound_finalize(&mut self) {
|
|
let mut checksum: u8 = 0;
|
|
|
|
for v in vec![self.header.to_raw(), self.content.to_raw()] {
|
|
for b in v {
|
|
checksum = checksum ^ b;
|
|
}
|
|
}
|
|
|
|
self.checksum = checksum;
|
|
self.valid = true;
|
|
}
|
|
|
|
pub fn to_raw(&mut self) -> VecDeque<u8> {
|
|
let mut resp: VecDeque<u8> = vec![].into();
|
|
|
|
for b in self.header.to_raw() {
|
|
resp.push_back(b);
|
|
}
|
|
|
|
for b in self.content.to_raw() {
|
|
resp.push_back(b);
|
|
}
|
|
|
|
resp.push_back(self.checksum);
|
|
|
|
resp.push_front(FLAGDELIMITER);
|
|
resp.push_back(FLAGDELIMITER);
|
|
resp
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for Message {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
write!(
|
|
f,
|
|
"Message: (header: {}), content: {}, checksum: {}, valid: {}",
|
|
self.header, self.content, self.checksum, self.valid
|
|
)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum MessageType {
|
|
None,
|
|
TerminalUniversalResponse(TerminalUniversalResponse),
|
|
PlatformUniversalResponse(PlatformUniversalResponse),
|
|
TerminalHeartbeat(TerminalHeartbeat),
|
|
TerminalRegistration(TerminalRegistration),
|
|
TerminalRegistrationReply(TerminalRegistrationReply),
|
|
TerminalLogout(TerminalLogout),
|
|
TerminalAuthentication(TerminalAuthentication),
|
|
TerminalParameterSetting(TerminalParameterSetting),
|
|
QueryTerminalParameter(QueryTerminalParameter),
|
|
QueryTerminalParameterResponse(QueryTerminalParameterResponse),
|
|
TerminalControl(TerminalControl),
|
|
LocationInformationReport(LocationInformationReport),
|
|
StartOfTrip(StartOfTrip),
|
|
EndOfTrip(EndOfTrip),
|
|
}
|
|
|
|
impl Default for MessageType {
|
|
fn default() -> Self {
|
|
Self::None
|
|
}
|
|
}
|
|
|
|
impl BodyMessage for MessageType {
|
|
fn to_raw(&self) -> Vec<u8> {
|
|
let res = match self {
|
|
MessageType::TerminalRegistrationReply(o) => o.to_raw(),
|
|
MessageType::PlatformUniversalResponse(o) => o.to_raw(),
|
|
//MessageType::TerminalHeartbeat(o) => o.to_raw(),
|
|
_ => vec![],
|
|
};
|
|
res
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for MessageType {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
write!(f, "{:?}", self)
|
|
}
|
|
}
|