rust_mqtt/packet/v5/
connack_packet.rs

1/*
2 * MIT License
3 *
4 * Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25use heapless::Vec;
26
27use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
28use crate::packet::v5::mqtt_packet::Packet;
29use crate::utils::buffer_reader::BuffReader;
30use crate::utils::buffer_writer::BuffWriter;
31use crate::utils::types::BufferError;
32
33use super::packet_type::PacketType;
34use super::property::Property;
35
36pub struct ConnackPacket<'a, const MAX_PROPERTIES: usize> {
37    pub fixed_header: u8,
38    pub remain_len: u32,
39    pub ack_flags: u8,
40    pub connect_reason_code: u8,
41    pub property_len: u32,
42    pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
43}
44
45impl<'a, const MAX_PROPERTIES: usize> ConnackPacket<'a, MAX_PROPERTIES> {}
46
47impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for ConnackPacket<'a, MAX_PROPERTIES> {
48    fn new() -> Self {
49        Self {
50            fixed_header: PacketType::Connack.into(),
51            remain_len: 0,
52            ack_flags: 0,
53            connect_reason_code: 0,
54            property_len: 0,
55            properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
56        }
57    }
58
59    fn encode(&mut self, buffer: &mut [u8], buffer_len: usize) -> Result<usize, BufferError> {
60        let mut buff_writer = BuffWriter::new(buffer, buffer_len);
61        buff_writer.write_u8(self.fixed_header)?;
62        let property_len_enc = VariableByteIntegerEncoder::encode(self.property_len)?;
63        let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
64
65        let rm_len: u32 = 2 + self.property_len + property_len_len as u32;
66        buff_writer.write_variable_byte_int(rm_len)?;
67        buff_writer.write_u8(self.ack_flags)?;
68        buff_writer.write_u8(self.connect_reason_code)?;
69        buff_writer.write_variable_byte_int(self.property_len)?;
70        buff_writer.write_properties(&self.properties)?;
71        Ok(buff_writer.position)
72    }
73
74    fn decode(&mut self, buff_reader: &mut BuffReader<'a>) -> Result<(), BufferError> {
75        if self.decode_fixed_header(buff_reader)? != PacketType::Connack {
76            error!("Packet you are trying to decode is not CONNACK packet!");
77            return Err(BufferError::PacketTypeMismatch);
78        }
79        self.ack_flags = buff_reader.read_u8()?;
80        self.connect_reason_code = buff_reader.read_u8()?;
81        self.decode_properties(buff_reader)
82    }
83
84    fn set_property_len(&mut self, value: u32) {
85        self.property_len = value;
86    }
87
88    fn get_property_len(&mut self) -> u32 {
89        self.property_len
90    }
91
92    fn push_to_properties(&mut self, property: Property<'a>) {
93        self.properties.push(property);
94    }
95
96    fn property_allowed(&mut self, property: &Property<'a>) -> bool {
97        property.connack_property()
98    }
99
100    fn set_fixed_header(&mut self, header: u8) {
101        self.fixed_header = header;
102    }
103
104    fn set_remaining_len(&mut self, remaining_len: u32) {
105        self.remain_len = remaining_len;
106    }
107}