rust_mqtt/packet/v5/
connect_packet.rs1use 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::{BinaryData, BufferError, EncodedString};
32
33use super::packet_type::PacketType;
34use super::property::Property;
35
36pub struct ConnectPacket<'a, const MAX_PROPERTIES: usize, const MAX_WILL_PROPERTIES: usize> {
37 pub fixed_header: u8,
38 pub remain_len: u32,
39 pub protocol_name_len: u16,
40 pub protocol_name: u32,
41 pub protocol_version: u8,
42 pub connect_flags: u8,
43 pub keep_alive: u16,
44 pub property_len: u32,
45 pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
46 pub client_id: EncodedString<'a>,
47 pub will_property_len: u32,
48 pub will_properties: Vec<Property<'a>, MAX_WILL_PROPERTIES>,
49 pub will_topic: EncodedString<'a>,
50 pub will_payload: BinaryData<'a>,
51 pub username: EncodedString<'a>,
52 pub password: BinaryData<'a>,
53}
54
55impl<'a, const MAX_PROPERTIES: usize, const MAX_WILL_PROPERTIES: usize>
56 ConnectPacket<'a, MAX_PROPERTIES, MAX_WILL_PROPERTIES>
57{
58 pub fn clean() -> Self {
59 let mut x = Self {
60 fixed_header: PacketType::Connect.into(),
61 remain_len: 0,
62 protocol_name_len: 4,
63 protocol_name: 0x4d515454,
64 protocol_version: 5,
65 connect_flags: 0x02,
66 keep_alive: 60,
67 property_len: 3,
68 properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
69 client_id: EncodedString::new(),
70 will_property_len: 0,
72 will_properties: Vec::<Property<'a>, MAX_WILL_PROPERTIES>::new(),
73 will_topic: EncodedString::new(),
74 will_payload: BinaryData::new(),
75 username: EncodedString::new(),
76
77 password: BinaryData::new(),
78 };
79
80 let y = Property::ReceiveMaximum(20);
81 x.properties.push(y);
82 x.client_id.len = 0;
83 x
84 }
85
86 pub fn add_packet_type(&mut self, new_packet_type: PacketType) {
87 self.fixed_header &= 0x0F;
88 self.fixed_header |= u8::from(new_packet_type);
89 }
90
91 pub fn add_username(&mut self, username: &EncodedString<'a>) {
92 self.username = (*username).clone();
93 self.connect_flags |= 0x80;
94 }
95
96 pub fn add_password(&mut self, password: &BinaryData<'a>) {
97 self.password = (*password).clone();
98 self.connect_flags |= 0x40;
99 }
100
101 pub fn add_will(&mut self, topic: &EncodedString<'a>, payload: &BinaryData<'a>, retain: bool) {
102 self.will_topic = topic.clone();
103 self.will_payload = payload.clone();
104 self.connect_flags |= 0x04;
105 if retain {
106 self.connect_flags |= 0x20;
107 }
108 }
109
110 pub fn add_client_id(&mut self, id: &EncodedString<'a>) {
111 self.client_id = (*id).clone();
112 }
113}
114
115impl<'a, const MAX_PROPERTIES: usize, const MAX_WILL_PROPERTIES: usize> Packet<'a>
116 for ConnectPacket<'a, MAX_PROPERTIES, MAX_WILL_PROPERTIES>
117{
118 fn new() -> Self {
119 Self {
120 fixed_header: PacketType::Connect.into(),
121 remain_len: 0,
122 protocol_name_len: 4,
123 protocol_name: 0x4d515454,
125 protocol_version: 5,
126 connect_flags: 0x02,
128 keep_alive: 180,
129 property_len: 0,
130 properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
131 client_id: EncodedString::new(),
132 will_property_len: 0,
133 will_properties: Vec::<Property<'a>, MAX_WILL_PROPERTIES>::new(),
134 will_topic: EncodedString::new(),
135 will_payload: BinaryData::new(),
136 username: EncodedString::new(),
137 password: BinaryData::new(),
138 }
139 }
140
141 fn encode(&mut self, buffer: &mut [u8], buffer_len: usize) -> Result<usize, BufferError> {
142 let mut buff_writer = BuffWriter::new(buffer, buffer_len);
143
144 let mut rm_ln = self.property_len;
145 let property_len_enc: [u8; 4] = VariableByteIntegerEncoder::encode(self.property_len)?;
146 let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
147 rm_ln = rm_ln + property_len_len as u32 + 10 + self.client_id.len as u32 + 2;
149
150 if self.connect_flags & 0x04 != 0 {
151 let wil_prop_len_enc = VariableByteIntegerEncoder::encode(self.will_property_len)?;
152 let wil_prop_len_len = VariableByteIntegerEncoder::len(wil_prop_len_enc);
153 rm_ln = rm_ln
154 + wil_prop_len_len as u32
155 + self.will_property_len
156 + self.will_topic.len as u32
157 + 2
158 + self.will_payload.len as u32
159 + 2;
160 }
161 if (self.connect_flags & 0x80) != 0 {
162 rm_ln = rm_ln + self.username.len as u32 + 2;
163 }
164
165 if self.connect_flags & 0x40 != 0 {
166 rm_ln = rm_ln + self.password.len as u32 + 2;
167 }
168
169 buff_writer.write_u8(self.fixed_header)?;
170 buff_writer.write_variable_byte_int(rm_ln)?;
171
172 buff_writer.write_u16(self.protocol_name_len)?;
173 buff_writer.write_u32(self.protocol_name)?;
174 buff_writer.write_u8(self.protocol_version)?;
175 buff_writer.write_u8(self.connect_flags)?;
176 buff_writer.write_u16(self.keep_alive)?;
177 buff_writer.write_variable_byte_int(self.property_len)?;
178 buff_writer.write_properties::<MAX_PROPERTIES>(&self.properties)?;
179 buff_writer.write_string_ref(&self.client_id)?;
180
181 if self.connect_flags & 0x04 != 0 {
182 buff_writer.write_variable_byte_int(self.will_property_len)?;
183 buff_writer.write_properties(&self.will_properties)?;
184 buff_writer.write_string_ref(&self.will_topic)?;
185 buff_writer.write_binary_ref(&self.will_payload)?;
186 }
187
188 if self.connect_flags & 0x80 != 0 {
189 buff_writer.write_string_ref(&self.username)?;
190 }
191
192 if self.connect_flags & 0x40 != 0 {
193 buff_writer.write_binary_ref(&self.password)?;
194 }
195
196 Ok(buff_writer.position)
197 }
198
199 fn decode(&mut self, _buff_reader: &mut BuffReader<'a>) -> Result<(), BufferError> {
200 error!("Decode function is not available for control packet!");
201 Err(BufferError::WrongPacketToDecode)
202 }
203
204 fn set_property_len(&mut self, value: u32) {
205 self.property_len = value;
206 }
207
208 fn get_property_len(&mut self) -> u32 {
209 self.property_len
210 }
211
212 fn push_to_properties(&mut self, property: Property<'a>) {
213 self.properties.push(property);
214 }
215
216 fn property_allowed(&mut self, property: &Property<'a>) -> bool {
217 property.connect_property()
218 }
219
220 fn set_fixed_header(&mut self, header: u8) {
221 self.fixed_header = header;
222 }
223
224 fn set_remaining_len(&mut self, remaining_len: u32) {
225 self.remain_len = remaining_len;
226 }
227}