prepare iterator
This commit is contained in:
parent
b663cef8f0
commit
e9c4e5b25f
@ -59,7 +59,7 @@ pub async fn apiserver() -> io::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle(buf: &Vec<u8>) {
|
pub fn handle(buf: &Vec<u8>) {
|
||||||
let mut rawdata = buf.to_vec();
|
let mut rawdata = DataWrapper::new(buf.to_vec());
|
||||||
match parse_inbound_msg(&mut rawdata) {
|
match parse_inbound_msg(&mut rawdata) {
|
||||||
Ok(o) => {
|
Ok(o) => {
|
||||||
println!("msg: {}", o);
|
println!("msg: {}", o);
|
||||||
|
@ -1,13 +1,3 @@
|
|||||||
pub trait BodyMessage {
|
|
||||||
fn build() {}
|
|
||||||
fn body_to_iter<'a>(&'a self, rawbody: &'a Vec<u8>) -> std::slice::Iter<'a, u8> {
|
|
||||||
rawbody.into_iter()
|
|
||||||
}
|
|
||||||
fn print(&mut self, rawbody: &Vec<u8>) {
|
|
||||||
println!("{:?}", rawbody);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! generate_impl {
|
macro_rules! generate_impl {
|
||||||
($($t:ty),*) => {
|
($($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<u8>) -> std::slice::Iter<'a, u8> {
|
||||||
|
rawbody.into_iter()
|
||||||
|
}
|
||||||
|
fn print(&mut self, rawbody: &Vec<u8>) {
|
||||||
|
println!("{:?}", rawbody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
pub struct TerminalUniversalResponse {
|
pub struct TerminalUniversalResponse {
|
||||||
pub answer_serial_no: u16,
|
pub answer_serial_no: u16,
|
||||||
|
@ -17,7 +17,7 @@ const PROPS_RESERVED_MASK: u16 = 0xC000;
|
|||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
const PROPS_DATAENCRYPTION_MASK: u16 = 0x0E00;
|
const PROPS_DATAENCRYPTION_MASK: u16 = 0x0E00;
|
||||||
|
|
||||||
pub fn parse_inbound_msg(rawdata: &mut Vec<u8>) -> std::result::Result<Message, MessageError> {
|
pub fn parse_inbound_msg(rawdata: &mut DataWrapper) -> std::result::Result<Message, MessageError> {
|
||||||
let mut msg: Message = Message::default();
|
let mut msg: Message = Message::default();
|
||||||
|
|
||||||
match msg.parse_header(rawdata) {
|
match msg.parse_header(rawdata) {
|
||||||
@ -33,6 +33,35 @@ pub fn parse_inbound_msg(rawdata: &mut Vec<u8>) -> std::result::Result<Message,
|
|||||||
Ok(msg)
|
Ok(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct DataWrapper {
|
||||||
|
pub data: Vec<u8>,
|
||||||
|
pub count: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DataWrapper {
|
||||||
|
pub fn new(data: Vec<u8>) -> DataWrapper {
|
||||||
|
DataWrapper { data, count: 0 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for DataWrapper {
|
||||||
|
type Item = u8;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
let res: Option<u8>;
|
||||||
|
|
||||||
|
if self.count < 6 {
|
||||||
|
res = self.data.pop()
|
||||||
|
} else {
|
||||||
|
res = None
|
||||||
|
}
|
||||||
|
self.count += 1;
|
||||||
|
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
pub header: MessageHeader,
|
pub header: MessageHeader,
|
||||||
@ -55,9 +84,9 @@ impl std::fmt::Display for Message {
|
|||||||
impl Message {
|
impl Message {
|
||||||
pub fn parse_header(
|
pub fn parse_header(
|
||||||
&mut self,
|
&mut self,
|
||||||
rawdata: &mut Vec<u8>,
|
rawdata: &mut DataWrapper,
|
||||||
) -> std::result::Result<(), NotOurProtocolError> {
|
) -> 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();
|
let first_byte = data.next().unwrap();
|
||||||
if first_byte != FLAGDELIMITER {
|
if first_byte != FLAGDELIMITER {
|
||||||
return Err(NotOurProtocolError);
|
return Err(NotOurProtocolError);
|
||||||
@ -94,6 +123,12 @@ impl Message {
|
|||||||
|
|
||||||
pub fn parse_properties(&mut self) {
|
pub fn parse_properties(&mut self) {
|
||||||
self.header.bodylength = (self.header.properties & PROPS_SIZE_MASK) as u8;
|
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) {
|
pub fn parse_terminal_id(&mut self) {
|
||||||
@ -101,8 +136,8 @@ impl Message {
|
|||||||
self.header.terminal_id = code.to_u64().unwrap();
|
self.header.terminal_id = code.to_u64().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn check(&mut self, rawdata: &mut Vec<u8>) {
|
pub fn check(&mut self, rawdata: &mut DataWrapper) {
|
||||||
let mut data = rawdata.into_iter();
|
let mut data = rawdata.data.iter();
|
||||||
data.next().unwrap();
|
data.next().unwrap();
|
||||||
let mut check: u8 = 0;
|
let mut check: u8 = 0;
|
||||||
|
|
||||||
@ -204,7 +239,10 @@ pub struct MessageHeader {
|
|||||||
id: u16,
|
id: u16,
|
||||||
properties: u16,
|
properties: u16,
|
||||||
pub bodylength: u8,
|
pub bodylength: u8,
|
||||||
pub encryption: bool,
|
encryption: u8,
|
||||||
|
pub encrypted: bool,
|
||||||
|
pub subcontract: bool,
|
||||||
|
reserved: u16,
|
||||||
raw_terminal_id: [u8; 6],
|
raw_terminal_id: [u8; 6],
|
||||||
pub terminal_id: u64,
|
pub terminal_id: u64,
|
||||||
pub serial_number: u16,
|
pub serial_number: u16,
|
||||||
|
Loading…
Reference in New Issue
Block a user