rust_mqtt/packet/v5/
publish_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::publish_packet::QualityOfService::{QoS0, QoS1, QoS2, INVALID};
30use crate::utils::buffer_reader::BuffReader;
31use crate::utils::buffer_writer::BuffWriter;
32use crate::utils::types::{BufferError, EncodedString};
33
34use super::packet_type::PacketType;
35use super::property::Property;
36
37#[derive(Clone, Copy, PartialEq, Debug)]
38pub enum QualityOfService {
39    QoS0,
40    QoS1,
41    QoS2,
42    INVALID,
43}
44
45impl From<u8> for QualityOfService {
46    fn from(orig: u8) -> Self {
47        match orig {
48            0 => QoS0,
49            2 => QoS1,
50            4 => QoS2,
51            _ => INVALID,
52        }
53    }
54}
55
56impl From<QualityOfService> for u8 {
57    fn from(value: QualityOfService) -> Self {
58        match value {
59            QoS0 => 0,
60            QoS1 => 2,
61            QoS2 => 4,
62            INVALID => 3,
63        }
64    }
65}
66
67pub struct PublishPacket<'a, const MAX_PROPERTIES: usize> {
68    pub fixed_header: u8,
69    pub remain_len: u32,
70    pub topic_name: EncodedString<'a>,
71    pub packet_identifier: u16,
72    pub property_len: u32,
73    pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
74    pub message: Option<&'a [u8]>,
75}
76
77impl<'a, const MAX_PROPERTIES: usize> PublishPacket<'a, MAX_PROPERTIES> {
78    pub fn add_topic_name(&mut self, topic_name: &'a str) {
79        self.topic_name.string = topic_name;
80        self.topic_name.len = topic_name.len() as u16;
81    }
82
83    pub fn add_message(&mut self, message: &'a [u8]) {
84        self.message = Some(message);
85    }
86
87    pub fn add_qos(&mut self, qos: QualityOfService) {
88        self.fixed_header |= <QualityOfService as Into<u8>>::into(qos);
89    }
90
91    pub fn add_retain(&mut self, retain: bool) {
92        self.fixed_header |= retain as u8
93    }
94
95    pub fn add_identifier(&mut self, identifier: u16) {
96        self.packet_identifier = identifier;
97    }
98}
99
100impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for PublishPacket<'a, MAX_PROPERTIES> {
101    fn new() -> Self {
102        Self {
103            fixed_header: PacketType::Publish.into(),
104            remain_len: 0,
105            topic_name: EncodedString::new(),
106            packet_identifier: 1,
107            property_len: 0,
108            properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
109            message: None,
110        }
111    }
112
113    fn encode(&mut self, buffer: &mut [u8], buffer_len: usize) -> Result<usize, BufferError> {
114        let mut buff_writer = BuffWriter::new(buffer, buffer_len);
115
116        let mut rm_ln = self.property_len;
117        let property_len_enc: [u8; 4] = VariableByteIntegerEncoder::encode(self.property_len)?;
118        let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
119        let msg_len = self.message.unwrap().len() as u32;
120        rm_ln = rm_ln + property_len_len as u32 + msg_len + self.topic_name.len as u32 + 2;
121
122        buff_writer.write_u8(self.fixed_header)?;
123        let qos = self.fixed_header & 0x06;
124        if qos != 0 {
125            rm_ln += 2;
126        }
127
128        buff_writer.write_variable_byte_int(rm_ln)?;
129        buff_writer.write_string_ref(&self.topic_name)?;
130
131        if qos != 0 {
132            buff_writer.write_u16(self.packet_identifier)?;
133        }
134
135        buff_writer.write_variable_byte_int(self.property_len)?;
136        buff_writer.write_properties::<MAX_PROPERTIES>(&self.properties)?;
137        buff_writer.insert_ref(msg_len as usize, self.message.unwrap())?;
138        Ok(buff_writer.position)
139    }
140
141    fn decode(&mut self, buff_reader: &mut BuffReader<'a>) -> Result<(), BufferError> {
142        if self.decode_fixed_header(buff_reader)? != PacketType::Publish {
143            error!("Packet you are trying to decode is not PUBLISH packet!");
144            return Err(BufferError::PacketTypeMismatch);
145        }
146        self.topic_name = buff_reader.read_string()?;
147        let qos = self.fixed_header & 0x06;
148        if qos != 0 {
149            // Decode only for QoS 1 / 2
150            self.packet_identifier = buff_reader.read_u16()?;
151        }
152        self.decode_properties(buff_reader)?;
153        let mut total_len =
154            VariableByteIntegerEncoder::len(VariableByteIntegerEncoder::encode(self.remain_len)?);
155        total_len = total_len + 1 + self.remain_len as usize;
156        self.message = Some(buff_reader.read_message(total_len));
157        Ok(())
158    }
159
160    fn set_property_len(&mut self, value: u32) {
161        self.property_len = value;
162    }
163
164    fn get_property_len(&mut self) -> u32 {
165        self.property_len
166    }
167
168    fn push_to_properties(&mut self, property: Property<'a>) {
169        self.properties.push(property);
170    }
171
172    fn property_allowed(&mut self, property: &Property<'a>) -> bool {
173        property.publish_property()
174    }
175
176    fn set_fixed_header(&mut self, header: u8) {
177        self.fixed_header = header;
178    }
179
180    fn set_remaining_len(&mut self, remaining_len: u32) {
181        self.remain_len = remaining_len;
182    }
183}