rust_mqtt/packet/v5/
suback_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::types::BufferError;
31
32use super::packet_type::PacketType;
33use super::property::Property;
34
35pub struct SubackPacket<'a, const MAX_REASONS: usize, const MAX_PROPERTIES: usize> {
36    pub fixed_header: u8,
37    pub remain_len: u32,
38    pub packet_identifier: u16,
39    pub property_len: u32,
40    pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
41    pub reason_codes: Vec<u8, MAX_REASONS>,
42}
43
44impl<'a, const MAX_REASONS: usize, const MAX_PROPERTIES: usize>
45    SubackPacket<'a, MAX_REASONS, MAX_PROPERTIES>
46{
47    pub fn read_reason_codes(
48        &mut self,
49        buff_reader: &mut BuffReader<'a>,
50    ) -> Result<(), BufferError> {
51        let rm_ln_ln = VariableByteIntegerEncoder::len(
52            VariableByteIntegerEncoder::encode(self.remain_len).unwrap(),
53        );
54        let max = self.remain_len as usize + rm_ln_ln + 1;
55        if buff_reader.position >= max {
56            return Ok(());
57        }
58        loop {
59            self.reason_codes.push(buff_reader.read_u8()?);
60            if buff_reader.position == max {
61                break;
62            }
63        }
64        Ok(())
65    }
66}
67
68impl<'a, const MAX_REASONS: usize, const MAX_PROPERTIES: usize> Packet<'a>
69    for SubackPacket<'a, MAX_REASONS, MAX_PROPERTIES>
70{
71    fn new() -> Self {
72        Self {
73            fixed_header: PacketType::Suback.into(),
74            remain_len: 0,
75            packet_identifier: 0,
76            property_len: 0,
77            properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
78            reason_codes: Vec::<u8, MAX_REASONS>::new(),
79        }
80    }
81
82    fn encode(&mut self, _buffer: &mut [u8], _buffer_len: usize) -> Result<usize, BufferError> {
83        error!("SUBACK packet does not support encoding!");
84        Err(BufferError::WrongPacketToEncode)
85    }
86
87    fn decode(&mut self, buff_reader: &mut BuffReader<'a>) -> Result<(), BufferError> {
88        if self.decode_fixed_header(buff_reader)? != PacketType::Suback {
89            error!("Packet you are trying to decode is not SUBACK packet!");
90            return Err(BufferError::PacketTypeMismatch);
91        }
92        self.packet_identifier = buff_reader.read_u16()?;
93        self.decode_properties(buff_reader)?;
94        self.read_reason_codes(buff_reader)
95    }
96
97    fn set_property_len(&mut self, value: u32) {
98        self.property_len = value;
99    }
100
101    fn get_property_len(&mut self) -> u32 {
102        self.property_len
103    }
104
105    fn push_to_properties(&mut self, property: Property<'a>) {
106        self.properties.push(property);
107    }
108
109    fn property_allowed(&mut self, property: &Property<'a>) -> bool {
110        property.suback_property()
111    }
112
113    fn set_fixed_header(&mut self, header: u8) {
114        self.fixed_header = header;
115    }
116
117    fn set_remaining_len(&mut self, remaining_len: u32) {
118        self.remain_len = remaining_len;
119    }
120}