Crate gmqtt[−][src]
Expand description
This library provides a way for writing and reading MQTT V5 packets in a `no_std` and `no_alloc` environment
Details
- Write a packet to a buffer and read back from it
use gmqtt::{
control_packet::{
connect::{ConnectProperties, Login},
Connect, Packet,
},
read_packet, write_packet,
};
const MAX_MQTT_PACKET_SIZE: u32 = 1024;
pub fn main() {
// Instantiate a packet with your data
let keep_alive: u16 = 60; // 60 seconds
let login = Login {
username: "username",
password: "password".as_bytes(),
};
let properties = ConnectProperties {
topic_alias_max: Some(0),
max_packet_size: Some(MAX_MQTT_PACKET_SIZE),
..ConnectProperties::default()
};
let connect_packet = Packet::Connect(Connect {
protocol: gmqtt::Protocol::V5,
clean_start: false,
keep_alive,
client_id: "our client id",
last_will: None,
login: Some(login),
properties: Some(properties),
});
let mut buffer = [0u8; MAX_MQTT_PACKET_SIZE as usize];
// Use the `write_packet` function to write/encode the packet, returning the encoded length
let len = write_packet(&connect_packet, &mut buffer).unwrap();
// Use the `read_packet` function to read/decode the packet.
let read_packet = read_packet(&buffer[..len]).unwrap();
assert_eq!(connect_packet, read_packet);
}
Re-exports
pub use crate::control_packet::clone_packet;
pub use crate::control_packet::read_packet;
pub use crate::control_packet::write_packet;