rust_mqtt/packet/v5/
unsubscription_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::packet::v5::packet_type::PacketType;
30use crate::utils::buffer_reader::BuffReader;
31use crate::utils::buffer_writer::BuffWriter;
32use crate::utils::types::{BufferError, TopicFilter};
33
34use super::property::Property;
35
36pub struct UnsubscriptionPacket<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize> {
37    pub fixed_header: u8,
38    pub remain_len: u32,
39    pub packet_identifier: u16,
40    pub property_len: u32,
41    pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
42    pub topic_filter_len: u16,
43    pub topic_filters: Vec<TopicFilter<'a>, MAX_FILTERS>,
44}
45
46impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize>
47    UnsubscriptionPacket<'a, MAX_FILTERS, MAX_PROPERTIES>
48{
49    pub fn add_new_filter(&mut self, topic_name: &'a str) {
50        let len = topic_name.len();
51        let mut new_filter = TopicFilter::new();
52        new_filter.filter.string = topic_name;
53        new_filter.filter.len = len as u16;
54        new_filter.sub_options |= 0x01;
55        self.topic_filters.push(new_filter);
56        self.topic_filter_len += 1;
57    }
58}
59
60impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize> Packet<'a>
61    for UnsubscriptionPacket<'a, MAX_FILTERS, MAX_PROPERTIES>
62{
63    fn new() -> Self {
64        Self {
65            fixed_header: PacketType::Unsubscribe.into(),
66            remain_len: 0,
67            packet_identifier: 0,
68            property_len: 0,
69            properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
70            topic_filter_len: 0,
71            topic_filters: Vec::<TopicFilter<'a>, MAX_FILTERS>::new(),
72        }
73    }
74
75    fn encode(&mut self, buffer: &mut [u8], buffer_len: usize) -> Result<usize, BufferError> {
76        let mut buff_writer = BuffWriter::new(buffer, buffer_len);
77
78        let mut rm_ln = self.property_len;
79        let property_len_enc: [u8; 4] = VariableByteIntegerEncoder::encode(self.property_len)?;
80        let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
81
82        let mut lt = 0;
83        let mut filters_len = 0;
84        loop {
85            filters_len = filters_len + self.topic_filters.get(lt).unwrap().filter.len + 2;
86            lt += 1;
87            if lt == self.topic_filter_len as usize {
88                break;
89            }
90        }
91        rm_ln = rm_ln + property_len_len as u32 + 2 + filters_len as u32;
92
93        buff_writer.write_u8(self.fixed_header)?;
94        buff_writer.write_variable_byte_int(rm_ln)?;
95        buff_writer.write_u16(self.packet_identifier)?;
96        buff_writer.write_variable_byte_int(self.property_len)?;
97        buff_writer.write_properties::<MAX_PROPERTIES>(&self.properties)?;
98        buff_writer.write_topic_filters_ref(
99            false,
100            self.topic_filter_len as usize,
101            &self.topic_filters,
102        )?;
103        Ok(buff_writer.position)
104    }
105
106    fn decode(&mut self, _buff_reader: &mut BuffReader<'a>) -> Result<(), BufferError> {
107        error!("Unsubscribe packet does not support decode funtion on client!");
108        Err(BufferError::WrongPacketToDecode)
109    }
110
111    fn set_property_len(&mut self, value: u32) {
112        self.property_len = value;
113    }
114
115    fn get_property_len(&mut self) -> u32 {
116        self.property_len
117    }
118
119    fn push_to_properties(&mut self, property: Property<'a>) {
120        self.properties.push(property);
121    }
122
123    fn property_allowed(&mut self, property: &Property<'a>) -> bool {
124        property.unsubscribe_property()
125    }
126
127    fn set_fixed_header(&mut self, header: u8) {
128        self.fixed_header = header;
129    }
130
131    fn set_remaining_len(&mut self, remaining_len: u32) {
132        self.remain_len = remaining_len;
133    }
134}