Prepare client for future v3 support (#11)

* Prepare client for future v3 support
* Add hiveMQ
This commit is contained in:
obabec
2022-03-31 13:34:26 +02:00
committed by GitHub
parent 19087016a6
commit 96be69c520
7 changed files with 212 additions and 26 deletions

View File

@@ -22,7 +22,7 @@
* SOFTWARE.
*/
use crate::client::client_config::ClientConfig;
use crate::client::client_config::{ClientConfig, MqttVersion};
use crate::network::NetworkConnection;
use crate::packet::v5::connack_packet::ConnackPacket;
use crate::packet::v5::connect_packet::ConnectPacket;
@@ -44,9 +44,10 @@ use heapless::Vec;
use rand_core::RngCore;
use crate::encoding::variable_byte_integer::{VariableByteInteger, VariableByteIntegerDecoder, VariableByteIntegerEncoder};
use crate::network::NetworkError::Connection;
use crate::packet::v5::property::Property;
use crate::utils::buffer_writer::BuffWriter;
pub struct MqttClientV5<'a, T, const MAX_PROPERTIES: usize> {
pub struct MqttClient<'a, T, const MAX_PROPERTIES: usize> {
connection: Option<T>,
buffer: &'a mut [u8],
buffer_len: usize,
@@ -56,7 +57,7 @@ pub struct MqttClientV5<'a, T, const MAX_PROPERTIES: usize> {
config: ClientConfig<'a, MAX_PROPERTIES>,
}
impl<'a, T, const MAX_PROPERTIES: usize> MqttClientV5<'a, T, MAX_PROPERTIES>
impl<'a, T, const MAX_PROPERTIES: usize> MqttClient<'a, T, MAX_PROPERTIES>
where
T: NetworkConnection,
{
@@ -79,7 +80,7 @@ where
}
}
pub async fn connect_to_broker<'b>(&'b mut self) -> Result<(), ReasonCode> {
async fn connect_to_broker_v5<'b>(&'b mut self) -> Result<(), ReasonCode> {
if self.connection.is_none() {
return Err(ReasonCode::NetworkError);
}
@@ -141,7 +142,14 @@ where
}
}
pub async fn disconnect<'b>(&'b mut self) -> Result<(), ReasonCode> {
pub async fn connect_to_broker<'b>(&'b mut self) -> Result<(), ReasonCode> {
match self.config.mqtt_version {
MqttVersion::MQTTv3 => {Err(ReasonCode::UnsupportedProtocolVersion)}
MqttVersion::MQTTv5 => {self.connect_to_broker_v5().await}
}
}
async fn disconnect_v5<'b>(&'b mut self) -> Result<(), ReasonCode> {
if self.connection.is_none() {
return Err(ReasonCode::NetworkError);
}
@@ -168,7 +176,14 @@ where
Ok(())
}
pub async fn send_message<'b>(
pub async fn disconnect<'b>(&'b mut self) -> Result<(), ReasonCode> {
match self.config.mqtt_version {
MqttVersion::MQTTv3 => {Err(ReasonCode::UnsupportedProtocolVersion)}
MqttVersion::MQTTv5 => {self.disconnect_v5().await}
}
}
async fn send_message_v5<'b>(
&'b mut self,
topic_name: &'b str,
message: &'b str,
@@ -228,7 +243,18 @@ where
Ok(())
}
pub async fn subscribe_to_topics<'b, const TOPICS: usize>(
pub async fn send_message<'b>(
&'b mut self,
topic_name: &'b str,
message: &'b str,
) -> Result<(), ReasonCode> {
match self.config.mqtt_version {
MqttVersion::MQTTv3 => {Err(ReasonCode::UnsupportedProtocolVersion)}
MqttVersion::MQTTv5 => {self.send_message_v5(topic_name, message).await}
}
}
async fn subscribe_to_topics_v5<'b, const TOPICS: usize>(
&'b mut self,
topic_names: &'b Vec<&'b str, TOPICS>,
) -> Result<(), ReasonCode> {
@@ -285,7 +311,17 @@ where
Ok(())
}
pub async fn subscribe_to_topic<'b>(
pub async fn subscribe_to_topics<'b, const TOPICS: usize>(
&'b mut self,
topic_names: &'b Vec<&'b str, TOPICS>,
) -> Result<(), ReasonCode> {
match self.config.mqtt_version {
MqttVersion::MQTTv3 => {Err(ReasonCode::UnsupportedProtocolVersion)}
MqttVersion::MQTTv5 => {self.subscribe_to_topics_v5(topic_names).await}
}
}
async fn subscribe_to_topic_v5<'b>(
&'b mut self,
topic_name: &'b str,
) -> Result<(), ReasonCode> {
@@ -330,7 +366,17 @@ where
}
}
pub async fn receive_message<'b>(&'b mut self) -> Result<&'b [u8], ReasonCode> {
pub async fn subscribe_to_topic<'b>(
&'b mut self,
topic_name: &'b str,
) -> Result<(), ReasonCode> {
match self.config.mqtt_version {
MqttVersion::MQTTv3 => {Err(ReasonCode::UnsupportedProtocolVersion)}
MqttVersion::MQTTv5 => {self.subscribe_to_topic_v5(topic_name).await}
}
}
async fn receive_message_v5<'b>(&'b mut self) -> Result<&'b [u8], ReasonCode> {
if self.connection.is_none() {
return Err(ReasonCode::NetworkError);
}
@@ -374,7 +420,14 @@ where
return Ok(packet.message.unwrap());
}
pub async fn send_ping<'b>(&'b mut self) -> Result<(), ReasonCode> {
pub async fn receive_message<'b>(&'b mut self) -> Result<&'b [u8], ReasonCode> {
match self.config.mqtt_version {
MqttVersion::MQTTv3 => {Err(ReasonCode::UnsupportedProtocolVersion)}
MqttVersion::MQTTv5 => {self.receive_message_v5().await}
}
}
pub async fn send_ping_v5<'b>(&'b mut self) -> Result<(), ReasonCode> {
if self.connection.is_none() {
return Err(ReasonCode::NetworkError);
}
@@ -400,8 +453,16 @@ where
Ok(())
}
}
pub async fn send_ping<'b>(&'b mut self) -> Result<(), ReasonCode> {
match self.config.mqtt_version {
MqttVersion::MQTTv3 => {Err(ReasonCode::UnsupportedProtocolVersion)}
MqttVersion::MQTTv5 => {self.send_ping_v5().await}
}
}
}
async fn receive_packet<'c, T:NetworkConnection>(buffer: & mut [u8],buffer_len: usize, recv_buffer: & mut [u8], conn: &'c mut T) -> Result<usize, ReasonCode> {
let mut target_len = 0;
let mut rem_len: VariableByteInteger = [0; 4];

View File

@@ -28,6 +28,11 @@ use crate::utils::types::{BinaryData, EncodedString};
use heapless::Vec;
#[derive(Clone, PartialEq)]
pub enum MqttVersion {
MQTTv3,
MQTTv5
}
#[derive(Clone)]
pub struct ClientConfig<'a, const MAX_PROPERTIES: usize> {
@@ -40,10 +45,11 @@ pub struct ClientConfig<'a, const MAX_PROPERTIES: usize> {
pub password: BinaryData<'a>,
pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
pub max_packet_size: u32,
pub mqtt_version: MqttVersion,
}
impl<'a, const MAX_PROPERTIES: usize> ClientConfig<'a, MAX_PROPERTIES> {
pub fn new() -> Self {
pub fn new(version: MqttVersion) -> Self {
Self {
qos: QualityOfService::QoS0,
keep_alive: 60,
@@ -54,6 +60,7 @@ impl<'a, const MAX_PROPERTIES: usize> ClientConfig<'a, MAX_PROPERTIES> {
password: BinaryData::new(),
properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
max_packet_size: 265_000,
mqtt_version: version
}
}

View File

@@ -24,4 +24,4 @@
#[allow(unused_must_use)]
pub mod client_config;
pub mod client_v5;
pub mod client;