Start encoding and refactor
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use crate::encoding::variable_byte_integer::VariableByteIntegerDecoder;
|
||||
use core::str;
|
||||
use core::mem;
|
||||
use core::str;
|
||||
|
||||
use crate::encoding::variable_byte_integer::VariableByteIntegerDecoder;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct EncodedString<'a> {
|
||||
@@ -21,7 +22,7 @@ impl EncodedString<'_> {
|
||||
#[derive(Debug)]
|
||||
pub struct BinaryData<'a> {
|
||||
pub bin: &'a [u8],
|
||||
pub len: u16
|
||||
pub len: u16,
|
||||
}
|
||||
|
||||
impl BinaryData<'_> {
|
||||
@@ -37,7 +38,7 @@ impl BinaryData<'_> {
|
||||
#[derive(Debug)]
|
||||
pub struct StringPair<'a> {
|
||||
pub name: EncodedString<'a>,
|
||||
pub value: EncodedString<'a>
|
||||
pub value: EncodedString<'a>,
|
||||
}
|
||||
|
||||
impl StringPair<'_> {
|
||||
@@ -56,7 +57,11 @@ pub struct TopicFilter<'a> {
|
||||
|
||||
impl TopicFilter<'_> {
|
||||
pub fn new() -> Self {
|
||||
Self { len: 0, filter: EncodedString::new(), sub_options: 0 }
|
||||
Self {
|
||||
len: 0,
|
||||
filter: EncodedString::new(),
|
||||
sub_options: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> u16 {
|
||||
@@ -64,15 +69,14 @@ impl TopicFilter<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(core::fmt::Debug)]
|
||||
#[derive(Clone)]
|
||||
#[derive(core::fmt::Debug, Clone)]
|
||||
pub enum ParseError {
|
||||
Utf8Error,
|
||||
IndexOutOfBounce,
|
||||
VariableByteIntegerError,
|
||||
IdNotFound,
|
||||
EncodingError,
|
||||
DecodingError
|
||||
DecodingError,
|
||||
}
|
||||
|
||||
pub struct BuffReader<'a> {
|
||||
@@ -81,94 +85,112 @@ pub struct BuffReader<'a> {
|
||||
}
|
||||
|
||||
impl<'a> BuffReader<'a> {
|
||||
pub fn incrementPosition(& mut self, increment: usize) {
|
||||
pub fn increment_position(&mut self, increment: usize) {
|
||||
self.position = self.position + increment;
|
||||
}
|
||||
|
||||
pub fn new(buffer: &'a [u8]) -> Self {
|
||||
return BuffReader { buffer: buffer, position: 0 };
|
||||
return BuffReader {
|
||||
buffer: buffer,
|
||||
position: 0,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn readVariableByteInt(& mut self) -> Result<u32, ParseError> {
|
||||
let variable_byte_integer: [u8; 4] = [self.buffer[self.position], self.buffer[self.position + 1], self.buffer[self.position + 2], self.buffer[self.position + 3]];
|
||||
pub fn read_variable_byte_int(&mut self) -> Result<u32, ParseError> {
|
||||
let variable_byte_integer: [u8; 4] = [
|
||||
self.buffer[self.position],
|
||||
self.buffer[self.position + 1],
|
||||
self.buffer[self.position + 2],
|
||||
self.buffer[self.position + 3],
|
||||
];
|
||||
let mut len: usize = 1;
|
||||
if variable_byte_integer[0] & 0x80 == 1 {
|
||||
len = len + 1;
|
||||
if variable_byte_integer[1] & 0x80 == 1 {
|
||||
len = len + 1;
|
||||
if variable_byte_integer[2] & 0x80 == 1 {
|
||||
len = len + 1;
|
||||
if variable_byte_integer[0] & 0x80 == 1 {
|
||||
len = len + 1;
|
||||
if variable_byte_integer[1] & 0x80 == 1 {
|
||||
len = len + 1;
|
||||
if variable_byte_integer[2] & 0x80 == 1 {
|
||||
len = len + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
self.incrementPosition(len);
|
||||
self.increment_position(len);
|
||||
return VariableByteIntegerDecoder::decode(variable_byte_integer);
|
||||
}
|
||||
|
||||
pub fn readU32(& mut self) -> Result<u32, ParseError> {
|
||||
|
||||
pub fn read_u32(&mut self) -> Result<u32, ParseError> {
|
||||
let (int_bytes, rest) = self.buffer[self.position..].split_at(mem::size_of::<u32>());
|
||||
let ret: u32 = u32::from_be_bytes(int_bytes.try_into().unwrap());
|
||||
//let ret: u32 = (((self.buffer[self.position] as u32) << 24) | ((self.buffer[self.position + 1] as u32) << 16) | ((self.buffer[self.position + 2] as u32) << 8) | (self.buffer[self.position + 3] as u32)) as u32;
|
||||
self.incrementPosition(4);
|
||||
self.increment_position(4);
|
||||
return Ok(ret);
|
||||
}
|
||||
|
||||
pub fn readU16(& mut self) -> Result<u16, ParseError> {
|
||||
|
||||
pub fn read_u16(&mut self) -> Result<u16, ParseError> {
|
||||
let (int_bytes, rest) = self.buffer[self.position..].split_at(mem::size_of::<u16>());
|
||||
let ret: u16 = u16::from_be_bytes(int_bytes.try_into().unwrap());
|
||||
let ret: u16 = u16::from_be_bytes(int_bytes.try_into().unwrap());
|
||||
//(((self.buffer[self.position] as u16) << 8) | (self.buffer[self.position + 1] as u16)) as u16;
|
||||
self.incrementPosition(2);
|
||||
self.increment_position(2);
|
||||
return Ok(ret);
|
||||
}
|
||||
|
||||
pub fn readU8(& mut self) -> Result<u8, ParseError> {
|
||||
|
||||
pub fn read_u8(&mut self) -> Result<u8, ParseError> {
|
||||
let ret: u8 = self.buffer[self.position];
|
||||
self.incrementPosition(1);
|
||||
self.increment_position(1);
|
||||
return Ok(ret);
|
||||
}
|
||||
|
||||
pub fn readString(& mut self) -> Result<EncodedString<'a>, ParseError> {
|
||||
let len = self.readU16();
|
||||
|
||||
pub fn read_string(&mut self) -> Result<EncodedString<'a>, ParseError> {
|
||||
let len = self.read_u16();
|
||||
match len {
|
||||
Err(err) => return Err(err),
|
||||
_ => log::debug!("[parseString] let not parsed")
|
||||
_ => log::debug!("[parseString] let not parsed"),
|
||||
}
|
||||
let len_res = len.unwrap();
|
||||
let res_str = str::from_utf8(&(self.buffer[self.position..(self.position + len_res as usize)]));
|
||||
let res_str =
|
||||
str::from_utf8(&(self.buffer[self.position..(self.position + len_res as usize)]));
|
||||
if res_str.is_err() {
|
||||
log::error!("Could not parse utf-8 string");
|
||||
return Err(ParseError::Utf8Error);
|
||||
}
|
||||
return Ok(EncodedString { string: res_str.unwrap(), len: len_res });
|
||||
return Ok(EncodedString {
|
||||
string: res_str.unwrap(),
|
||||
len: len_res,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
//TODO: Index out of bounce err !!!!!
|
||||
pub fn readBinary(& mut self) -> Result<BinaryData<'a>, ParseError> {
|
||||
let len = self.readU16();
|
||||
pub fn read_binary(&mut self) -> Result<BinaryData<'a>, ParseError> {
|
||||
let len = self.read_u16();
|
||||
match len {
|
||||
Err(err) => return Err(err),
|
||||
_ => log::debug!("[parseBinary] let not parsed")
|
||||
_ => log::debug!("[parseBinary] let not parsed"),
|
||||
}
|
||||
let len_res = len.unwrap();
|
||||
let res_bin = &(self.buffer[self.position..(self.position + len_res as usize)]);
|
||||
return Ok(BinaryData { bin: res_bin, len: len_res });
|
||||
}
|
||||
|
||||
pub fn readStringPair(& mut self) -> Result<StringPair<'a>, ParseError> {
|
||||
let name = self.readString();
|
||||
match name {
|
||||
Err(err) => return Err(err),
|
||||
_ => log::debug!("[String pair] name not parsed")
|
||||
}
|
||||
let value = self.readString();
|
||||
match value {
|
||||
Err(err) => return Err(err),
|
||||
_ => log::debug!("[String pair] value not parsed")
|
||||
}
|
||||
return Ok(StringPair { name: name.unwrap(), value: value.unwrap() });
|
||||
return Ok(BinaryData {
|
||||
bin: res_bin,
|
||||
len: len_res,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn readMessage(& mut self) -> &'a [u8] {
|
||||
pub fn read_string_pair(&mut self) -> Result<StringPair<'a>, ParseError> {
|
||||
let name = self.read_string();
|
||||
match name {
|
||||
Err(err) => return Err(err),
|
||||
_ => log::debug!("[String pair] name not parsed"),
|
||||
}
|
||||
let value = self.read_string();
|
||||
match value {
|
||||
Err(err) => return Err(err),
|
||||
_ => log::debug!("[String pair] value not parsed"),
|
||||
}
|
||||
return Ok(StringPair {
|
||||
name: name.unwrap(),
|
||||
value: value.unwrap(),
|
||||
});
|
||||
}
|
||||
|
||||
pub fn read_message(&mut self) -> &'a [u8] {
|
||||
return &self.buffer[self.position..];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
119
src/utils/buffer_writer.rs
Normal file
119
src/utils/buffer_writer.rs
Normal file
@@ -0,0 +1,119 @@
|
||||
use crate::utils::buffer_reader::{BinaryData, EncodedString, StringPair};
|
||||
use core::str;
|
||||
use heapless::Vec;
|
||||
use crate::encoding::variable_byte_integer::{VariableByteInteger, VariableByteIntegerEncoder};
|
||||
use crate::packet::property::Property;
|
||||
|
||||
pub struct BuffWriter<'a> {
|
||||
buffer: &'a mut [u8],
|
||||
pub position: usize,
|
||||
}
|
||||
|
||||
impl<'a> BuffWriter<'a> {
|
||||
|
||||
pub fn insert<const LEN: usize>(& mut self, array: [u8; LEN]) {
|
||||
let mut x: usize = 0;
|
||||
loop {
|
||||
self.buffer[self.position] = array[x];
|
||||
self.increment_position(1);
|
||||
x = x + 1;
|
||||
if x == LEN {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_ref(& mut self, len: usize, array: &[u8]) {
|
||||
let mut x: usize = 0;
|
||||
loop {
|
||||
self.buffer[self.position] = array[x];
|
||||
self.increment_position(1);
|
||||
x = x + 1;
|
||||
if x == len {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(buffer: &'a mut [u8]) -> Self {
|
||||
return BuffWriter {
|
||||
buffer,
|
||||
position: 0,
|
||||
};
|
||||
}
|
||||
|
||||
fn increment_position(& mut self, increment: usize) {
|
||||
self.position = self.position + increment;
|
||||
}
|
||||
|
||||
pub fn write_u8(& mut self, byte: u8) {
|
||||
self.buffer[self.position] = byte;
|
||||
self.increment_position(1);
|
||||
}
|
||||
|
||||
pub fn write_u16(& mut self, two_bytes: u16) {
|
||||
let bytes: [u8; 2] = two_bytes.to_be_bytes();
|
||||
self.insert::<2>(bytes);
|
||||
}
|
||||
|
||||
pub fn write_u32(& mut self, four_bytes: u32) {
|
||||
let bytes: [u8; 4] = four_bytes.to_be_bytes();
|
||||
self.insert::<4>(bytes);
|
||||
}
|
||||
|
||||
pub fn write_string_ref(& mut self, str: & EncodedString<'a>) {
|
||||
self.write_u16(str.len);
|
||||
let bytes = str.string.as_bytes();
|
||||
self.insert_ref(str.len as usize, bytes);
|
||||
}
|
||||
|
||||
pub fn write_string(& mut self, str: EncodedString<'a>) {
|
||||
self.write_u16(str.len);
|
||||
let bytes = str.string.as_bytes();
|
||||
self.insert_ref(str.len as usize, bytes);
|
||||
}
|
||||
|
||||
pub fn write_binary_ref(& mut self, bin: & BinaryData<'a>) {
|
||||
self.write_u16(bin.len);
|
||||
self.insert_ref(bin.len as usize, bin.bin);
|
||||
}
|
||||
|
||||
pub fn write_binary(& mut self, bin: BinaryData<'a>) {
|
||||
self.write_u16(bin.len);
|
||||
self.insert_ref(bin.len as usize, bin.bin);
|
||||
}
|
||||
|
||||
pub fn write_string_pair_ref(& mut self, str_pair: & StringPair<'a>) {
|
||||
self.write_string_ref(&str_pair.name);
|
||||
self.write_string_ref(&str_pair.value);
|
||||
}
|
||||
|
||||
pub fn write_string_pair(& mut self, str_pair: StringPair<'a>) {
|
||||
self.write_string(str_pair.name);
|
||||
self.write_string(str_pair.value);
|
||||
}
|
||||
|
||||
pub fn write_variable_byte_int(& mut self, int: u32) {
|
||||
let x: VariableByteInteger = VariableByteIntegerEncoder::encode(int).unwrap();
|
||||
let len = VariableByteIntegerEncoder::len(x);
|
||||
self.insert_ref(len, &x);
|
||||
}
|
||||
|
||||
pub fn encode_property(& mut self, property: & Property<'a>) {
|
||||
let x: u8 = property.into();
|
||||
self.write_u8(x);
|
||||
property.encode(self);
|
||||
}
|
||||
|
||||
pub fn encode_properties<const LEN: usize>(& mut self, properties: & Vec<Property<'a>, LEN>) {
|
||||
let mut i = 0;
|
||||
loop {
|
||||
let prop: &Property = properties.get(i).unwrap();
|
||||
self.encode_property(prop);
|
||||
i = i + 1;
|
||||
if i == LEN {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod buffer_reader;
|
||||
pub mod buffer_reader;
|
||||
pub mod buffer_writer;
|
||||
|
||||
Reference in New Issue
Block a user