rust_mqtt/packet/v5/
pubcomp_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 PubcompPacket<'a, const MAX_PROPERTIES: usize> {
37    pub fixed_header: u8,
38    pub remain_len: u32,
39    pub packet_identifier: u16,
40    pub reason_code: u8,
41    pub property_len: u32,
42    pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
43}
44
45impl<'a, const MAX_PROPERTIES: usize> PubcompPacket<'a, MAX_PROPERTIES> {}
46
47impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for PubcompPacket<'a, MAX_PROPERTIES> {
48    fn new() -> Self {
49        Self {
50            fixed_header: PacketType::Pubcomp.into(),
51            remain_len: 0,
52            packet_identifier: 0,
53            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
62        let mut rm_ln = self.property_len;
63        let property_len_enc: [u8; 4] = VariableByteIntegerEncoder::encode(self.property_len)?;
64        let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
65        rm_ln = rm_ln + property_len_len as u32 + 3;
66
67        buff_writer.write_u8(self.fixed_header)?;
68        buff_writer.write_variable_byte_int(rm_ln)?;
69        buff_writer.write_u16(self.packet_identifier)?;
70        buff_writer.write_u8(self.reason_code)?;
71        buff_writer.write_variable_byte_int(self.property_len)?;
72        buff_writer.write_properties::<MAX_PROPERTIES>(&self.properties)?;
73        Ok(buff_writer.position)
74    }
75
76    fn decode(&mut self, buff_reader: &mut BuffReader<'a>) -> Result<(), BufferError> {
77        if self.decode_fixed_header(buff_reader)? != PacketType::Pubcomp {
78            error!("Packet you are trying to decode is not PUBCOMP packet!");
79            return Err(BufferError::PacketTypeMismatch);
80        }
81        self.packet_identifier = buff_reader.read_u16()?;
82        self.reason_code = buff_reader.read_u8()?;
83        self.decode_properties(buff_reader)?;
84        Ok(())
85    }
86
87    fn set_property_len(&mut self, value: u32) {
88        self.property_len = value;
89    }
90
91    fn get_property_len(&mut self) -> u32 {
92        self.property_len
93    }
94
95    fn push_to_properties(&mut self, property: Property<'a>) {
96        self.properties.push(property);
97    }
98
99    fn property_allowed(&mut self, property: &Property<'a>) -> bool {
100        property.pubcomp_property()
101    }
102
103    fn set_fixed_header(&mut self, header: u8) {
104        self.fixed_header = header;
105    }
106
107    fn set_remaining_len(&mut self, remaining_len: u32) {
108        self.remain_len = remaining_len;
109    }
110}