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