Get rid of constants
This commit is contained in:
parent
963040026a
commit
4ed1c7bd2b
|
@ -58,7 +58,6 @@ impl VariableByteIntegerEncoder {
|
|||
if (encoded_byte & 128) == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use heapless::Vec;
|
||||
use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
|
||||
use heapless::Vec;
|
||||
|
||||
use crate::packet::mqtt_packet::Packet;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
use crate::utils::buffer_writer::{BuffWriter};
|
||||
use crate::utils::buffer_writer::BuffWriter;
|
||||
|
||||
use super::packet_type::PacketType;
|
||||
use super::property::Property;
|
||||
|
@ -22,13 +22,13 @@ pub struct AuthPacket<'a, const MAX_PROPERTIES: usize> {
|
|||
}
|
||||
|
||||
impl<'a, const MAX_PROPERTIES: usize> AuthPacket<'a, MAX_PROPERTIES> {
|
||||
pub fn decode_auth_packet(& mut self, buff_reader: & mut BuffReader<'a>) {
|
||||
pub fn decode_auth_packet(&mut self, buff_reader: &mut BuffReader<'a>) {
|
||||
self.decode_fixed_header(buff_reader);
|
||||
self.auth_reason = buff_reader.read_u8().unwrap();
|
||||
self.decode_properties(buff_reader);
|
||||
}
|
||||
|
||||
pub fn add_reason_code(& mut self, code: u8) {
|
||||
pub fn add_reason_code(&mut self, code: u8) {
|
||||
if code != 0 && code != 24 && code != 25 {
|
||||
log::error!("Provided reason code is not supported!");
|
||||
return;
|
||||
|
@ -36,7 +36,7 @@ impl<'a, const MAX_PROPERTIES: usize> AuthPacket<'a, MAX_PROPERTIES> {
|
|||
self.auth_reason = code;
|
||||
}
|
||||
|
||||
pub fn add_property(& mut self, p: Property<'a>) {
|
||||
pub fn add_property(&mut self, p: Property<'a>) {
|
||||
if p.auth_property() {
|
||||
self.push_to_properties(p);
|
||||
} else {
|
||||
|
@ -50,11 +50,12 @@ impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for AuthPacket<'a, MAX_PROPERTI
|
|||
return AuthPacket { fixed_header: PacketType::Auth.into(), remain_len: 0, auth_reason: 0, property_len: 0, properties: Vec::<Property<'a>, MAX_PROPERTIES>::new() }
|
||||
}*/
|
||||
|
||||
fn encode(&mut self, buffer: & mut [u8]) -> usize {
|
||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||
let mut buff_writer = BuffWriter::new(buffer);
|
||||
|
||||
let mut rm_ln = self.property_len;
|
||||
let property_len_enc: [u8; 4] = VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_enc: [u8; 4] =
|
||||
VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
|
||||
rm_ln = rm_ln + property_len_len as u32;
|
||||
rm_ln = rm_ln + 1;
|
||||
|
@ -83,11 +84,11 @@ impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for AuthPacket<'a, MAX_PROPERTI
|
|||
self.properties.push(property);
|
||||
}
|
||||
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use heapless::Vec;
|
||||
use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
|
||||
use heapless::Vec;
|
||||
|
||||
use crate::packet::mqtt_packet::Packet;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
|
@ -8,9 +8,7 @@ use crate::utils::buffer_writer::BuffWriter;
|
|||
use super::packet_type::PacketType;
|
||||
use super::property::Property;
|
||||
|
||||
pub const MAX_PROPERTIES: usize = 18;
|
||||
|
||||
pub struct ConnackPacket<'a> {
|
||||
pub struct ConnackPacket<'a, const MAX_PROPERTIES: usize> {
|
||||
// 7 - 4 mqtt control packet type, 3-0 flagy
|
||||
pub fixed_header: u8,
|
||||
// 1 - 4 B lenght of variable header + len of payload
|
||||
|
@ -21,7 +19,7 @@ pub struct ConnackPacket<'a> {
|
|||
pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
|
||||
}
|
||||
|
||||
impl<'a> ConnackPacket<'a> {
|
||||
impl<'a, const MAX_PROPERTIES: usize> ConnackPacket<'a, MAX_PROPERTIES> {
|
||||
pub fn decode_connack_packet(&mut self, buff_reader: &mut BuffReader<'a>) {
|
||||
if self.decode_fixed_header(buff_reader) != (PacketType::Connack).into() {
|
||||
log::error!("Packet you are trying to decode is not CONNACK packet!");
|
||||
|
@ -33,7 +31,7 @@ impl<'a> ConnackPacket<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Packet<'a> for ConnackPacket<'a> {
|
||||
impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for ConnackPacket<'a, MAX_PROPERTIES> {
|
||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||
let mut buff_writer = BuffWriter::new(buffer);
|
||||
buff_writer.write_u8(self.fixed_header);
|
||||
|
@ -64,11 +62,11 @@ impl<'a> Packet<'a> for ConnackPacket<'a> {
|
|||
self.properties.push(property);
|
||||
}
|
||||
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use heapless::Vec;
|
||||
use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
|
||||
use heapless::Vec;
|
||||
|
||||
use crate::packet::mqtt_packet::Packet;
|
||||
use crate::utils::buffer_reader::BinaryData;
|
||||
|
@ -38,7 +38,9 @@ pub struct ConnectPacket<'a, const MAX_PROPERTIES: usize, const MAX_WILL_PROPERT
|
|||
pub password: BinaryData<'a>,
|
||||
}
|
||||
|
||||
impl<'a, const MAX_PROPERTIES: usize, const MAX_WILL_PROPERTIES: usize> ConnectPacket<'a, MAX_PROPERTIES, MAX_WILL_PROPERTIES> {
|
||||
impl<'a, const MAX_PROPERTIES: usize, const MAX_WILL_PROPERTIES: usize>
|
||||
ConnectPacket<'a, MAX_PROPERTIES, MAX_WILL_PROPERTIES>
|
||||
{
|
||||
pub fn clean() -> Self {
|
||||
let mut x = Self {
|
||||
fixed_header: PacketType::Connect.into(),
|
||||
|
@ -96,20 +98,28 @@ impl<'a, const MAX_PROPERTIES: usize, const MAX_WILL_PROPERTIES: usize> ConnectP
|
|||
self.fixed_header = cur_type | flags;
|
||||
}
|
||||
}
|
||||
impl<'a, const MAX_PROPERTIES: usize, const MAX_WILL_PROPERTIES: usize> Packet<'a> for ConnectPacket<'a, MAX_PROPERTIES, MAX_WILL_PROPERTIES> {
|
||||
impl<'a, const MAX_PROPERTIES: usize, const MAX_WILL_PROPERTIES: usize> Packet<'a>
|
||||
for ConnectPacket<'a, MAX_PROPERTIES, MAX_WILL_PROPERTIES>
|
||||
{
|
||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||
let mut buff_writer = BuffWriter::new(buffer);
|
||||
|
||||
let mut rm_ln = self.property_len;
|
||||
let property_len_enc: [u8; 4] = VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_enc: [u8; 4] =
|
||||
VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
|
||||
// 12 = protocol_name_len + protocol_name + protocol_version + connect_flags + keep_alive + client_id_len
|
||||
rm_ln = rm_ln + property_len_len as u32 + 12;
|
||||
|
||||
if self.connect_flags & 0x04 == 1 {
|
||||
let wil_prop_len_enc = VariableByteIntegerEncoder::encode(self.will_property_len).unwrap();
|
||||
let wil_prop_len_enc =
|
||||
VariableByteIntegerEncoder::encode(self.will_property_len).unwrap();
|
||||
let wil_prop_len_len = VariableByteIntegerEncoder::len(wil_prop_len_enc);
|
||||
rm_ln = rm_ln + wil_prop_len_len as u32 + self.will_property_len as u32 + self.will_topic.len as u32 + self.will_payload.len as u32;
|
||||
rm_ln = rm_ln
|
||||
+ wil_prop_len_len as u32
|
||||
+ self.will_property_len as u32
|
||||
+ self.will_topic.len as u32
|
||||
+ self.will_payload.len as u32;
|
||||
}
|
||||
|
||||
if self.connect_flags & 0x80 == 1 {
|
||||
|
@ -134,17 +144,17 @@ impl<'a, const MAX_PROPERTIES: usize, const MAX_WILL_PROPERTIES: usize> Packet<'
|
|||
|
||||
if self.connect_flags & 0x04 == 1 {
|
||||
buff_writer.write_variable_byte_int(self.will_property_len);
|
||||
buff_writer.encode_properties(& self.will_properties);
|
||||
buff_writer.write_string_ref(& self.will_topic);
|
||||
buff_writer.write_binary_ref(& self.will_payload);
|
||||
buff_writer.encode_properties(&self.will_properties);
|
||||
buff_writer.write_string_ref(&self.will_topic);
|
||||
buff_writer.write_binary_ref(&self.will_payload);
|
||||
}
|
||||
|
||||
if self.connect_flags & 0x80 == 1 {
|
||||
buff_writer.write_string_ref(& self.username);
|
||||
buff_writer.write_string_ref(&self.username);
|
||||
}
|
||||
|
||||
if self.connect_flags & 0x40 == 1 {
|
||||
buff_writer.write_binary_ref(& self.password);
|
||||
buff_writer.write_binary_ref(&self.password);
|
||||
}
|
||||
|
||||
return buff_writer.position;
|
||||
|
@ -166,11 +176,11 @@ impl<'a, const MAX_PROPERTIES: usize, const MAX_WILL_PROPERTIES: usize> Packet<'
|
|||
self.properties.push(property);
|
||||
}
|
||||
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use heapless::Vec;
|
||||
use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
|
||||
use heapless::Vec;
|
||||
|
||||
use crate::packet::mqtt_packet::Packet;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
|
@ -63,11 +63,11 @@ impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for DisconnectPacket<'a, MAX_PR
|
|||
self.properties.push(property);
|
||||
}
|
||||
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
|
||||
//pub mod control_packet;
|
||||
pub mod auth_packet;
|
||||
pub mod connack_packet;
|
||||
pub mod mqtt_packet;
|
||||
pub mod packet_type;
|
||||
pub mod property;
|
||||
pub mod auth_packet;
|
||||
pub mod connack_packet;
|
||||
pub mod puback_packet;
|
||||
pub mod pubcomp_packet;
|
||||
pub mod publish_packet;
|
||||
|
@ -13,10 +12,9 @@ pub mod pubrel_packet;
|
|||
pub mod subscription_packet;
|
||||
pub mod unsubscription_packet;
|
||||
|
||||
|
||||
pub mod connect_packet;
|
||||
pub mod disconnect_packet;
|
||||
pub mod pingreq_packet;
|
||||
pub mod pingresp_packet;
|
||||
pub mod suback_packet;
|
||||
pub mod unsuback_packet;
|
||||
pub mod connect_packet;
|
||||
|
|
|
@ -16,10 +16,10 @@ pub trait Packet<'a> {
|
|||
fn push_to_properties(&mut self, property: Property<'a>);
|
||||
|
||||
// header
|
||||
fn set_fixed_header(& mut self, header: u8);
|
||||
fn set_remaining_len(& mut self, remaining_len: u32);
|
||||
fn set_fixed_header(&mut self, header: u8);
|
||||
fn set_remaining_len(&mut self, remaining_len: u32);
|
||||
|
||||
fn decode_properties(&mut self, buff_reader: & mut BuffReader<'a>) {
|
||||
fn decode_properties(&mut self, buff_reader: &mut BuffReader<'a>) {
|
||||
self.set_property_len(buff_reader.read_variable_byte_int().unwrap());
|
||||
let mut x: u32 = 0;
|
||||
let mut prop: Result<Property, ParseError>;
|
||||
|
|
|
@ -64,6 +64,6 @@ impl Into<u8> for PacketType {
|
|||
PacketType::Disconnect => 0xE0,
|
||||
PacketType::Auth => 0xF0,
|
||||
PacketType::Reserved => 0x00,
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,8 +5,6 @@ use crate::utils::buffer_writer::BuffWriter;
|
|||
use super::packet_type::PacketType;
|
||||
use super::property::Property;
|
||||
|
||||
pub const MAX_PROPERTIES: usize = 2;
|
||||
|
||||
pub struct PingreqPacket {
|
||||
// 7 - 4 mqtt control packet type, 3-0 flagy
|
||||
pub fixed_header: u8,
|
||||
|
@ -41,11 +39,11 @@ impl<'a> Packet<'a> for PingreqPacket {
|
|||
log::error!("PINGREQ packet does not contain any properties!");
|
||||
}
|
||||
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,11 +46,11 @@ impl<'a> Packet<'a> for PingrespPacket {
|
|||
log::error!("PINGRESP packet does not contain any properties!");
|
||||
}
|
||||
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,15 +39,14 @@ pub enum Property<'a> {
|
|||
}
|
||||
|
||||
impl<'a> Property<'a> {
|
||||
|
||||
pub fn auth_property(& self) -> bool {
|
||||
pub fn auth_property(&self) -> bool {
|
||||
return match self {
|
||||
Property::AuthenticationMethod(_u) => true,
|
||||
Property::AuthenticationData(_u) => true,
|
||||
Property::ReasonString(_u) => true,
|
||||
Property::UserProperty(_u) => true,
|
||||
_ => false
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn len(&self) -> u16 {
|
||||
|
@ -80,10 +79,10 @@ impl<'a> Property<'a> {
|
|||
Property::SubscriptionIdentifierAvailable(_u) => 1,
|
||||
Property::SharedSubscriptionAvailable(_u) => 1,
|
||||
_ => 0,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn encode(&self, buff_writer: & mut BuffWriter<'a>) {
|
||||
pub fn encode(&self, buff_writer: &mut BuffWriter<'a>) {
|
||||
return match self {
|
||||
Property::PayloadFormat(u) => buff_writer.write_u8(*u),
|
||||
Property::MessageExpiryInterval(u) => buff_writer.write_u32(*u),
|
||||
|
@ -113,10 +112,10 @@ impl<'a> Property<'a> {
|
|||
Property::SubscriptionIdentifierAvailable(u) => buff_writer.write_u8(*u),
|
||||
Property::SharedSubscriptionAvailable(u) => buff_writer.write_u8(*u),
|
||||
_ => (),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn decode(buff_reader: & mut BuffReader<'a>) -> Result<Property<'a>, ParseError> {
|
||||
pub fn decode(buff_reader: &mut BuffReader<'a>) -> Result<Property<'a>, ParseError> {
|
||||
let property_identifier = buff_reader.read_u8();
|
||||
return match property_identifier {
|
||||
Ok(0x01) => Ok(Property::PayloadFormat(buff_reader.read_u8()?)),
|
||||
|
@ -124,17 +123,13 @@ impl<'a> Property<'a> {
|
|||
Ok(0x03) => Ok(Property::ContentType(buff_reader.read_string()?)),
|
||||
Ok(0x08) => Ok(Property::ResponseTopic(buff_reader.read_string()?)),
|
||||
Ok(0x09) => Ok(Property::CorrelationData(buff_reader.read_binary()?)),
|
||||
Ok(0x0B) => {
|
||||
Ok(Property::SubscriptionIdentifier(
|
||||
buff_reader.read_variable_byte_int()?,
|
||||
))
|
||||
}
|
||||
Ok(0x0B) => Ok(Property::SubscriptionIdentifier(
|
||||
buff_reader.read_variable_byte_int()?,
|
||||
)),
|
||||
Ok(0x11) => Ok(Property::SessionExpiryInterval(buff_reader.read_u32()?)),
|
||||
Ok(0x12) => {
|
||||
Ok(Property::AssignedClientIdentifier(
|
||||
buff_reader.read_string()?,
|
||||
))
|
||||
}
|
||||
Ok(0x12) => Ok(Property::AssignedClientIdentifier(
|
||||
buff_reader.read_string()?,
|
||||
)),
|
||||
Ok(0x13) => Ok(Property::ServerKeepAlive(buff_reader.read_u16()?)),
|
||||
Ok(0x15) => Ok(Property::AuthenticationMethod(buff_reader.read_string()?)),
|
||||
Ok(0x16) => Ok(Property::AuthenticationData(buff_reader.read_binary()?)),
|
||||
|
@ -150,28 +145,22 @@ impl<'a> Property<'a> {
|
|||
Ok(0x24) => Ok(Property::MaximumQoS(buff_reader.read_u8()?)),
|
||||
Ok(0x25) => Ok(Property::RetainAvailable(buff_reader.read_u8()?)),
|
||||
Ok(0x26) => Ok(Property::UserProperty(buff_reader.read_string_pair()?)),
|
||||
Ok(0x28) => {
|
||||
Ok(Property::WildcardSubscriptionAvailable(
|
||||
buff_reader.read_u8()?,
|
||||
))
|
||||
}
|
||||
Ok(0x29) => {
|
||||
Ok(Property::SubscriptionIdentifierAvailable(
|
||||
buff_reader.read_u8()?,
|
||||
))
|
||||
}
|
||||
Ok(0x2A) => {
|
||||
Ok(Property::SharedSubscriptionAvailable(
|
||||
buff_reader.read_u8()?,
|
||||
))
|
||||
}
|
||||
Ok(0x28) => Ok(Property::WildcardSubscriptionAvailable(
|
||||
buff_reader.read_u8()?,
|
||||
)),
|
||||
Ok(0x29) => Ok(Property::SubscriptionIdentifierAvailable(
|
||||
buff_reader.read_u8()?,
|
||||
)),
|
||||
Ok(0x2A) => Ok(Property::SharedSubscriptionAvailable(
|
||||
buff_reader.read_u8()?,
|
||||
)),
|
||||
Err(err) => Err(err),
|
||||
_ => Err(ParseError::IdNotFound),
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<u8> for & Property<'a> {
|
||||
impl Into<u8> for &Property<'a> {
|
||||
fn into(self) -> u8 {
|
||||
return match &*self {
|
||||
Property::PayloadFormat(_u) => 0x01,
|
||||
|
@ -201,8 +190,8 @@ impl Into<u8> for & Property<'a> {
|
|||
Property::WildcardSubscriptionAvailable(_u) => 0x28,
|
||||
Property::SubscriptionIdentifierAvailable(_u) => 0x29,
|
||||
Property::SharedSubscriptionAvailable(_u) => 0x2A,
|
||||
_ => 0x00
|
||||
}
|
||||
_ => 0x00,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use heapless::Vec;
|
||||
use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
|
||||
use heapless::Vec;
|
||||
|
||||
use crate::packet::mqtt_packet::Packet;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
|
@ -8,9 +8,7 @@ use crate::utils::buffer_writer::BuffWriter;
|
|||
use super::packet_type::PacketType;
|
||||
use super::property::Property;
|
||||
|
||||
pub const MAX_PROPERTIES: usize = 2;
|
||||
|
||||
pub struct PubackPacket<'a> {
|
||||
pub struct PubackPacket<'a, const MAX_PROPERTIES: usize> {
|
||||
// 7 - 4 mqtt control packet type, 3-0 flagy
|
||||
pub fixed_header: u8,
|
||||
// 1 - 4 B lenght of variable header + len of payload
|
||||
|
@ -25,7 +23,7 @@ pub struct PubackPacket<'a> {
|
|||
pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
|
||||
}
|
||||
|
||||
impl<'a> PubackPacket<'a> {
|
||||
impl<'a, const MAX_PROPERTIES: usize> PubackPacket<'a, MAX_PROPERTIES> {
|
||||
pub fn decode_puback_packet(&mut self, buff_reader: &mut BuffReader<'a>) {
|
||||
if self.decode_fixed_header(buff_reader) != (PacketType::Puback).into() {
|
||||
log::error!("Packet you are trying to decode is not PUBACK packet!");
|
||||
|
@ -37,12 +35,13 @@ impl<'a> PubackPacket<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Packet<'a> for PubackPacket<'a> {
|
||||
impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for PubackPacket<'a, MAX_PROPERTIES> {
|
||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||
let mut buff_writer = BuffWriter::new(buffer);
|
||||
|
||||
let mut rm_ln = self.property_len;
|
||||
let property_len_enc: [u8; 4] = VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_enc: [u8; 4] =
|
||||
VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
|
||||
rm_ln = rm_ln + property_len_len as u32 + 3;
|
||||
|
||||
|
@ -71,11 +70,11 @@ impl<'a> Packet<'a> for PubackPacket<'a> {
|
|||
self.properties.push(property);
|
||||
}
|
||||
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use heapless::Vec;
|
||||
use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
|
||||
use heapless::Vec;
|
||||
|
||||
use crate::packet::mqtt_packet::Packet;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
|
@ -8,9 +8,7 @@ use crate::utils::buffer_writer::BuffWriter;
|
|||
use super::packet_type::PacketType;
|
||||
use super::property::Property;
|
||||
|
||||
pub const MAX_PROPERTIES: usize = 2;
|
||||
|
||||
pub struct PubcompPacket<'a> {
|
||||
pub struct PubcompPacket<'a, const MAX_PROPERTIES: usize> {
|
||||
// 7 - 4 mqtt control packet type, 3-0 flagy
|
||||
pub fixed_header: u8,
|
||||
// 1 - 4 B lenght of variable header + len of payload
|
||||
|
@ -25,8 +23,7 @@ pub struct PubcompPacket<'a> {
|
|||
pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
|
||||
}
|
||||
|
||||
impl<'a> PubcompPacket<'a> {
|
||||
|
||||
impl<'a, const MAX_PROPERTIES: usize> PubcompPacket<'a, MAX_PROPERTIES> {
|
||||
pub fn decode_puback_packet(&mut self, buff_reader: &mut BuffReader<'a>) {
|
||||
if self.decode_fixed_header(buff_reader) != (PacketType::Pubcomp).into() {
|
||||
log::error!("Packet you are trying to decode is not PUBCOMP packet!");
|
||||
|
@ -38,12 +35,13 @@ impl<'a> PubcompPacket<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Packet<'a> for PubcompPacket<'a> {
|
||||
impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for PubcompPacket<'a, MAX_PROPERTIES> {
|
||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||
let mut buff_writer = BuffWriter::new(buffer);
|
||||
|
||||
let mut rm_ln = self.property_len;
|
||||
let property_len_enc: [u8; 4] = VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_enc: [u8; 4] =
|
||||
VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
|
||||
rm_ln = rm_ln + property_len_len as u32 + 3;
|
||||
|
||||
|
@ -72,11 +70,11 @@ impl<'a> Packet<'a> for PubcompPacket<'a> {
|
|||
self.properties.push(property);
|
||||
}
|
||||
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use heapless::Vec;
|
||||
use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
|
||||
use heapless::Vec;
|
||||
|
||||
use crate::packet::mqtt_packet::Packet;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
|
@ -9,9 +9,7 @@ use crate::utils::buffer_writer::BuffWriter;
|
|||
use super::packet_type::PacketType;
|
||||
use super::property::Property;
|
||||
|
||||
pub const MAX_PROPERTIES: usize = 9;
|
||||
|
||||
pub struct PublishPacket<'a> {
|
||||
pub struct PublishPacket<'a, const MAX_PROPERTIES: usize> {
|
||||
// 7 - 4 mqtt control packet type, 3-0 flagy
|
||||
pub fixed_header: u8,
|
||||
// 1 - 4 B lenght of variable header + len of payload
|
||||
|
@ -28,10 +26,17 @@ pub struct PublishPacket<'a> {
|
|||
pub message: &'a [u8],
|
||||
}
|
||||
|
||||
impl<'a> PublishPacket<'a> {
|
||||
impl<'a, const MAX_PROPERTIES: usize> PublishPacket<'a, MAX_PROPERTIES> {
|
||||
pub fn new(message: &'a [u8]) -> Self {
|
||||
let mut x = Self { fixed_header: PacketType::Publish.into(), remain_len: 0, topic_name: EncodedString::new(), packet_identifier: 0,
|
||||
property_len: 0, properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(), message };
|
||||
let mut x = Self {
|
||||
fixed_header: PacketType::Publish.into(),
|
||||
remain_len: 0,
|
||||
topic_name: EncodedString::new(),
|
||||
packet_identifier: 0,
|
||||
property_len: 0,
|
||||
properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
|
||||
message,
|
||||
};
|
||||
x.topic_name.string = "test/topic";
|
||||
x.topic_name.len = 10;
|
||||
return x;
|
||||
|
@ -53,12 +58,13 @@ impl<'a> PublishPacket<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Packet<'a> for PublishPacket<'a> {
|
||||
impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for PublishPacket<'a, MAX_PROPERTIES> {
|
||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||
let mut buff_writer = BuffWriter::new(buffer);
|
||||
|
||||
let mut rm_ln = self.property_len;
|
||||
let property_len_enc: [u8; 4] = VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_enc: [u8; 4] =
|
||||
VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
|
||||
let mut msg_len = self.message.len() as u32;
|
||||
rm_ln = rm_ln + property_len_len as u32 + msg_len + self.topic_name.len as u32 + 2;
|
||||
|
@ -70,7 +76,7 @@ impl<'a> Packet<'a> for PublishPacket<'a> {
|
|||
}
|
||||
|
||||
buff_writer.write_variable_byte_int(rm_ln);
|
||||
buff_writer.write_string_ref(& self.topic_name);
|
||||
buff_writer.write_string_ref(&self.topic_name);
|
||||
|
||||
if qos != 0 {
|
||||
buff_writer.write_u16(self.packet_identifier);
|
||||
|
@ -98,11 +104,11 @@ impl<'a> Packet<'a> for PublishPacket<'a> {
|
|||
self.properties.push(property);
|
||||
}
|
||||
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use heapless::Vec;
|
||||
use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
|
||||
use heapless::Vec;
|
||||
|
||||
use crate::packet::mqtt_packet::Packet;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
|
@ -8,9 +8,7 @@ use crate::utils::buffer_writer::BuffWriter;
|
|||
use super::packet_type::PacketType;
|
||||
use super::property::Property;
|
||||
|
||||
pub const MAX_PROPERTIES: usize = 2;
|
||||
|
||||
pub struct PubrecPacket<'a> {
|
||||
pub struct PubrecPacket<'a, const MAX_PROPERTIES: usize> {
|
||||
// 7 - 4 mqtt control packet type, 3-0 flagy
|
||||
pub fixed_header: u8,
|
||||
// 1 - 4 B lenght of variable header + len of payload
|
||||
|
@ -25,8 +23,7 @@ pub struct PubrecPacket<'a> {
|
|||
pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
|
||||
}
|
||||
|
||||
impl<'a> PubrecPacket<'a> {
|
||||
|
||||
impl<'a, const MAX_PROPERTIES: usize> PubrecPacket<'a, MAX_PROPERTIES> {
|
||||
pub fn decode_pubrec_packet(&mut self, buff_reader: &mut BuffReader<'a>) {
|
||||
if self.decode_fixed_header(buff_reader) != (PacketType::Pubrec).into() {
|
||||
log::error!("Packet you are trying to decode is not PUBREC packet!");
|
||||
|
@ -38,12 +35,13 @@ impl<'a> PubrecPacket<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Packet<'a> for PubrecPacket<'a> {
|
||||
impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for PubrecPacket<'a, MAX_PROPERTIES> {
|
||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||
let mut buff_writer = BuffWriter::new(buffer);
|
||||
|
||||
let mut rm_ln = self.property_len;
|
||||
let property_len_enc: [u8; 4] = VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_enc: [u8; 4] =
|
||||
VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
|
||||
rm_ln = rm_ln + property_len_len as u32 + 3;
|
||||
|
||||
|
@ -71,11 +69,11 @@ impl<'a> Packet<'a> for PubrecPacket<'a> {
|
|||
fn push_to_properties(&mut self, property: Property<'a>) {
|
||||
self.properties.push(property);
|
||||
}
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use heapless::Vec;
|
||||
use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
|
||||
use heapless::Vec;
|
||||
|
||||
use crate::packet::mqtt_packet::Packet;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
|
@ -8,9 +8,7 @@ use crate::utils::buffer_writer::BuffWriter;
|
|||
use super::packet_type::PacketType;
|
||||
use super::property::Property;
|
||||
|
||||
pub const MAX_PROPERTIES: usize = 2;
|
||||
|
||||
pub struct PubrelPacket<'a> {
|
||||
pub struct PubrelPacket<'a, const MAX_PROPERTIES: usize> {
|
||||
// 7 - 4 mqtt control packet type, 3-0 flagy
|
||||
pub fixed_header: u8,
|
||||
// 1 - 4 B lenght of variable header + len of payload
|
||||
|
@ -25,8 +23,7 @@ pub struct PubrelPacket<'a> {
|
|||
pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
|
||||
}
|
||||
|
||||
impl<'a> PubrelPacket<'a> {
|
||||
|
||||
impl<'a, const MAX_PROPERTIES: usize> PubrelPacket<'a, MAX_PROPERTIES> {
|
||||
pub fn decode_puback_packet(&mut self, buff_reader: &mut BuffReader<'a>) {
|
||||
if self.decode_fixed_header(buff_reader) != (PacketType::Pubrel).into() {
|
||||
log::error!("Packet you are trying to decode is not PUBREL packet!");
|
||||
|
@ -38,12 +35,13 @@ impl<'a> PubrelPacket<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Packet<'a> for PubrelPacket<'a> {
|
||||
impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for PubrelPacket<'a, MAX_PROPERTIES> {
|
||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||
let mut buff_writer = BuffWriter::new(buffer);
|
||||
|
||||
let mut rm_ln = self.property_len;
|
||||
let property_len_enc: [u8; 4] = VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_enc: [u8; 4] =
|
||||
VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
|
||||
rm_ln = rm_ln + property_len_len as u32 + 3;
|
||||
|
||||
|
@ -72,11 +70,11 @@ impl<'a> Packet<'a> for PubrelPacket<'a> {
|
|||
self.properties.push(property);
|
||||
}
|
||||
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use heapless::Vec;
|
||||
use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
|
||||
use heapless::Vec;
|
||||
|
||||
use crate::packet::mqtt_packet::Packet;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
|
@ -8,9 +8,7 @@ use crate::utils::buffer_writer::BuffWriter;
|
|||
use super::packet_type::PacketType;
|
||||
use super::property::Property;
|
||||
|
||||
pub const MAX_PROPERTIES: usize = 2;
|
||||
|
||||
pub struct SubackPacket<'a, const MAX_REASONS: usize> {
|
||||
pub struct SubackPacket<'a, const MAX_REASONS: usize, const MAX_PROPERTIES: usize> {
|
||||
// 7 - 4 mqtt control packet type, 3-0 flagy
|
||||
pub fixed_header: u8,
|
||||
// 1 - 4 B lenght of variable header + len of payload
|
||||
|
@ -26,8 +24,9 @@ pub struct SubackPacket<'a, const MAX_REASONS: usize> {
|
|||
pub reason_codes: Vec<u8, MAX_REASONS>,
|
||||
}
|
||||
|
||||
impl<'a, const MAX_REASONS: usize> SubackPacket<'a, MAX_REASONS> {
|
||||
|
||||
impl<'a, const MAX_REASONS: usize, const MAX_PROPERTIES: usize>
|
||||
SubackPacket<'a, MAX_REASONS, MAX_PROPERTIES>
|
||||
{
|
||||
pub fn read_reason_codes(&mut self, buff_reader: &mut BuffReader<'a>) {
|
||||
let mut i = 0;
|
||||
loop {
|
||||
|
@ -50,7 +49,9 @@ impl<'a, const MAX_REASONS: usize> SubackPacket<'a, MAX_REASONS> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, const MAX_REASONS: usize> Packet<'a> for SubackPacket<'a, MAX_REASONS> {
|
||||
impl<'a, const MAX_REASONS: usize, const MAX_PROPERTIES: usize> Packet<'a>
|
||||
for SubackPacket<'a, MAX_REASONS, MAX_PROPERTIES>
|
||||
{
|
||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||
log::error!("SUBACK packet does not support encoding!");
|
||||
return 0;
|
||||
|
@ -72,11 +73,11 @@ impl<'a, const MAX_REASONS: usize> Packet<'a> for SubackPacket<'a, MAX_REASONS>
|
|||
self.properties.push(property);
|
||||
}
|
||||
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use heapless::Vec;
|
||||
use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
|
||||
use heapless::Vec;
|
||||
|
||||
use crate::packet::mqtt_packet::Packet;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
|
@ -9,9 +9,7 @@ use crate::utils::buffer_writer::BuffWriter;
|
|||
use super::packet_type::PacketType;
|
||||
use super::property::Property;
|
||||
|
||||
pub const MAX_PROPERTIES: usize = 2;
|
||||
|
||||
pub struct SubscriptionPacket<'a, const MAX_FILTERS: usize> {
|
||||
pub struct SubscriptionPacket<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize> {
|
||||
// 7 - 4 mqtt control packet type, 3-0 flagy
|
||||
pub fixed_header: u8,
|
||||
// 1 - 4 B lenght of variable header + len of payload
|
||||
|
@ -31,10 +29,19 @@ pub struct SubscriptionPacket<'a, const MAX_FILTERS: usize> {
|
|||
pub topic_filters: Vec<TopicFilter<'a>, MAX_FILTERS>,
|
||||
}
|
||||
|
||||
impl<'a, const MAX_FILTERS: usize> SubscriptionPacket<'a, MAX_FILTERS> {
|
||||
impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize>
|
||||
SubscriptionPacket<'a, MAX_FILTERS, MAX_PROPERTIES>
|
||||
{
|
||||
pub fn new() -> Self {
|
||||
let mut x = Self { fixed_header: PacketType::Subscribe.into(), remain_len: 0, packet_identifier: 1,
|
||||
property_len: 0, properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(), topic_filter_len: 1, topic_filters: Vec::<TopicFilter<'a>, MAX_FILTERS>::new() };
|
||||
let mut x = Self {
|
||||
fixed_header: PacketType::Subscribe.into(),
|
||||
remain_len: 0,
|
||||
packet_identifier: 1,
|
||||
property_len: 0,
|
||||
properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
|
||||
topic_filter_len: 1,
|
||||
topic_filters: Vec::<TopicFilter<'a>, MAX_FILTERS>::new(),
|
||||
};
|
||||
let mut p = TopicFilter::new();
|
||||
p.filter.len = 6;
|
||||
p.filter.string = "test/#";
|
||||
|
@ -43,12 +50,15 @@ impl<'a, const MAX_FILTERS: usize> SubscriptionPacket<'a, MAX_FILTERS> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, const MAX_FILTERS: usize> Packet<'a> for SubscriptionPacket<'a, MAX_FILTERS> {
|
||||
impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize> Packet<'a>
|
||||
for SubscriptionPacket<'a, MAX_FILTERS, MAX_PROPERTIES>
|
||||
{
|
||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||
let mut buff_writer = BuffWriter::new(buffer);
|
||||
|
||||
let mut rm_ln = self.property_len;
|
||||
let property_len_enc: [u8; 4] = VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_enc: [u8; 4] =
|
||||
VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
|
||||
|
||||
let mut lt = 0;
|
||||
|
@ -67,7 +77,11 @@ impl<'a, const MAX_FILTERS: usize> Packet<'a> for SubscriptionPacket<'a, MAX_FIL
|
|||
buff_writer.write_u16(self.packet_identifier);
|
||||
buff_writer.write_variable_byte_int(self.property_len);
|
||||
buff_writer.encode_properties::<MAX_PROPERTIES>(&self.properties);
|
||||
buff_writer.encode_topic_filters_ref(false, self.topic_filter_len as usize, & self.topic_filters);
|
||||
buff_writer.encode_topic_filters_ref(
|
||||
false,
|
||||
self.topic_filter_len as usize,
|
||||
&self.topic_filters,
|
||||
);
|
||||
return buff_writer.position;
|
||||
}
|
||||
|
||||
|
@ -86,11 +100,11 @@ impl<'a, const MAX_FILTERS: usize> Packet<'a> for SubscriptionPacket<'a, MAX_FIL
|
|||
self.properties.push(property);
|
||||
}
|
||||
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,9 +6,7 @@ use crate::utils::buffer_reader::BuffReader;
|
|||
use super::packet_type::PacketType;
|
||||
use super::property::Property;
|
||||
|
||||
pub const MAX_PROPERTIES: usize = 20;
|
||||
|
||||
pub struct UnsubackPacket<'a, const MAX_REASONS: usize> {
|
||||
pub struct UnsubackPacket<'a, const MAX_REASONS: usize, const MAX_PROPERTIES: usize> {
|
||||
// 7 - 4 mqtt control packet type, 3-0 flagy
|
||||
pub fixed_header: u8,
|
||||
// 1 - 4 B lenght of variable header + len of payload
|
||||
|
@ -24,8 +22,9 @@ pub struct UnsubackPacket<'a, const MAX_REASONS: usize> {
|
|||
pub reason_codes: Vec<u8, MAX_REASONS>,
|
||||
}
|
||||
|
||||
impl<'a, const MAX_REASONS: usize> UnsubackPacket<'a, MAX_REASONS> {
|
||||
|
||||
impl<'a, const MAX_REASONS: usize, const MAX_PROPERTIES: usize>
|
||||
UnsubackPacket<'a, MAX_REASONS, MAX_PROPERTIES>
|
||||
{
|
||||
pub fn read_reason_codes(&mut self, buff_reader: &mut BuffReader<'a>) {
|
||||
let mut i = 0;
|
||||
loop {
|
||||
|
@ -48,7 +47,9 @@ impl<'a, const MAX_REASONS: usize> UnsubackPacket<'a, MAX_REASONS> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, const MAX_REASONS: usize> Packet<'a> for UnsubackPacket<'a, MAX_REASONS> {
|
||||
impl<'a, const MAX_REASONS: usize, const MAX_PROPERTIES: usize> Packet<'a>
|
||||
for UnsubackPacket<'a, MAX_REASONS, MAX_PROPERTIES>
|
||||
{
|
||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||
log::error!("UNSUBACK packet does not support encoding!");
|
||||
return 0;
|
||||
|
@ -70,11 +71,11 @@ impl<'a, const MAX_REASONS: usize> Packet<'a> for UnsubackPacket<'a, MAX_REASONS
|
|||
self.properties.push(property);
|
||||
}
|
||||
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use heapless::Vec;
|
||||
use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
|
||||
use heapless::Vec;
|
||||
|
||||
use crate::packet::mqtt_packet::Packet;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
|
@ -9,9 +9,7 @@ use crate::utils::buffer_writer::BuffWriter;
|
|||
use super::packet_type::PacketType;
|
||||
use super::property::Property;
|
||||
|
||||
pub const MAX_PROPERTIES: usize = 20;
|
||||
|
||||
pub struct UnsubscriptionPacket<'a, const MAX_FILTERS: usize> {
|
||||
pub struct UnsubscriptionPacket<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize> {
|
||||
// 7 - 4 mqtt control packet type, 3-0 flagy
|
||||
pub fixed_header: u8,
|
||||
// 1 - 4 B lenght of variable header + len of payload
|
||||
|
@ -31,18 +29,23 @@ pub struct UnsubscriptionPacket<'a, const MAX_FILTERS: usize> {
|
|||
pub topic_filters: Vec<TopicFilter<'a>, MAX_FILTERS>,
|
||||
}
|
||||
|
||||
impl<'a, const MAX_FILTERS: usize> UnsubscriptionPacket<'a, MAX_FILTERS> {
|
||||
impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize>
|
||||
UnsubscriptionPacket<'a, MAX_FILTERS, MAX_PROPERTIES>
|
||||
{
|
||||
/*pub fn new() -> Self {
|
||||
|
||||
}*/
|
||||
}
|
||||
|
||||
impl<'a, const MAX_FILTERS: usize> Packet<'a> for UnsubscriptionPacket<'a, MAX_FILTERS> {
|
||||
impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize> Packet<'a>
|
||||
for UnsubscriptionPacket<'a, MAX_FILTERS, MAX_PROPERTIES>
|
||||
{
|
||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||
let mut buff_writer = BuffWriter::new(buffer);
|
||||
|
||||
let mut rm_ln = self.property_len;
|
||||
let property_len_enc: [u8; 4] = VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_enc: [u8; 4] =
|
||||
VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||
let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
|
||||
rm_ln = rm_ln + property_len_len as u32 + 4 + self.topic_filter_len as u32;
|
||||
|
||||
|
@ -52,7 +55,11 @@ impl<'a, const MAX_FILTERS: usize> Packet<'a> for UnsubscriptionPacket<'a, MAX_F
|
|||
buff_writer.write_variable_byte_int(self.property_len);
|
||||
buff_writer.encode_properties::<MAX_PROPERTIES>(&self.properties);
|
||||
buff_writer.write_u16(self.topic_filter_len);
|
||||
buff_writer.encode_topic_filters_ref(false, self.topic_filter_len as usize, & self.topic_filters);
|
||||
buff_writer.encode_topic_filters_ref(
|
||||
false,
|
||||
self.topic_filter_len as usize,
|
||||
&self.topic_filters,
|
||||
);
|
||||
return buff_writer.position;
|
||||
}
|
||||
|
||||
|
@ -72,11 +79,11 @@ impl<'a, const MAX_FILTERS: usize> Packet<'a> for UnsubscriptionPacket<'a, MAX_F
|
|||
self.properties.push(property);
|
||||
}
|
||||
|
||||
fn set_fixed_header(& mut self, header: u8) {
|
||||
fn set_fixed_header(&mut self, header: u8) {
|
||||
self.fixed_header = header;
|
||||
}
|
||||
|
||||
fn set_remaining_len(& mut self, remaining_len: u32) {
|
||||
fn set_remaining_len(&mut self, remaining_len: u32) {
|
||||
self.remain_len = remaining_len;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use crate::encoding::variable_byte_integer::{VariableByteInteger, VariableByteIntegerEncoder};
|
||||
use crate::packet::property::Property;
|
||||
use crate::utils::buffer_reader::{BinaryData, EncodedString, StringPair, TopicFilter};
|
||||
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],
|
||||
|
@ -10,8 +10,7 @@ pub struct BuffWriter<'a> {
|
|||
}
|
||||
|
||||
impl<'a> BuffWriter<'a> {
|
||||
|
||||
pub fn insert<const LEN: usize>(& mut self, array: [u8; LEN]) {
|
||||
pub fn insert<const LEN: usize>(&mut self, array: [u8; LEN]) {
|
||||
let mut x: usize = 0;
|
||||
if LEN != 0 {
|
||||
loop {
|
||||
|
@ -25,7 +24,7 @@ impl<'a> BuffWriter<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn insert_ref(& mut self, len: usize, array: &[u8]) {
|
||||
pub fn insert_ref(&mut self, len: usize, array: &[u8]) {
|
||||
let mut x: usize = 0;
|
||||
if len != 0 {
|
||||
loop {
|
||||
|
@ -46,70 +45,70 @@ impl<'a> BuffWriter<'a> {
|
|||
};
|
||||
}
|
||||
|
||||
fn increment_position(& mut self, increment: usize) {
|
||||
fn increment_position(&mut self, increment: usize) {
|
||||
self.position = self.position + increment;
|
||||
}
|
||||
|
||||
pub fn write_u8(& mut self, byte: u8) {
|
||||
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) {
|
||||
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) {
|
||||
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>) {
|
||||
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>) {
|
||||
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>) {
|
||||
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>) {
|
||||
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>) {
|
||||
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>) {
|
||||
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) {
|
||||
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>) {
|
||||
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>) {
|
||||
pub fn encode_properties<const LEN: usize>(&mut self, properties: &Vec<Property<'a>, LEN>) {
|
||||
let mut i = 0;
|
||||
let len = properties.len();
|
||||
if len != 0 {
|
||||
|
@ -124,17 +123,22 @@ impl<'a> BuffWriter<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn encode_topic_filter_ref(& mut self, sub: bool, topic_filter: & TopicFilter<'a>) {
|
||||
fn encode_topic_filter_ref(&mut self, sub: bool, topic_filter: &TopicFilter<'a>) {
|
||||
self.write_string_ref(&topic_filter.filter);
|
||||
if sub {
|
||||
self.write_u8(topic_filter.sub_options)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode_topic_filters_ref<const MAX: usize>(& mut self, sub: bool, len: usize, filters: & Vec<TopicFilter<'a>, MAX>) {
|
||||
pub fn encode_topic_filters_ref<const MAX: usize>(
|
||||
&mut self,
|
||||
sub: bool,
|
||||
len: usize,
|
||||
filters: &Vec<TopicFilter<'a>, MAX>,
|
||||
) {
|
||||
let mut i = 0;
|
||||
loop {
|
||||
let topic_filter: & TopicFilter<'a> = filters.get(i).unwrap();
|
||||
let topic_filter: &TopicFilter<'a> = filters.get(i).unwrap();
|
||||
self.encode_topic_filter_ref(sub, topic_filter);
|
||||
i = i + 1;
|
||||
if i == len {
|
||||
|
|
Loading…
Reference in New Issue
Block a user