Error handling

This commit is contained in:
Ondrej Babec
2022-02-27 15:48:09 +01:00
parent 3cb9de0371
commit 4eaa83bf1c
14 changed files with 421 additions and 41 deletions

View File

@@ -0,0 +1,42 @@
use crate::packet::publish_packet::QualityOfService;
use crate::utils::buffer_reader::{BinaryData, EncodedString};
pub struct ClientConfig<'a> {
pub qos: QualityOfService,
pub username_flag: bool,
pub username: EncodedString<'a>,
pub password_flag: bool,
pub password: BinaryData<'a>
}
impl ClientConfig<'a> {
pub fn new() -> Self {
Self {
qos: QualityOfService::QoS0,
username_flag: false,
username: EncodedString::new(),
password_flag: false,
password: BinaryData::new(),
}
}
pub fn add_qos(& mut self, qos: QualityOfService) {
self.qos = qos;
}
pub fn add_username(& mut self, username: &'a str) {
let mut username_s: EncodedString = EncodedString::new();
username_s.string = username;
username_s.len = username.len() as u16;
self.username_flag = true;
self.username = username_s;
}
pub fn add_password(& mut self, password: &'a str) {
let mut password_s: BinaryData = BinaryData::new();
password_s.bin = password.as_bytes();
password_s.len = password_s.bin.len() as u16;
self.password = password_s;
self.password_flag = true;
}
}

View File

@@ -1,35 +1,52 @@
use core::future::Future;
use embassy::traits::rng;
use rand_core::RngCore;
use crate::client::client_config::ClientConfig;
use crate::network::network_trait::{Network, NetworkError};
use crate::packet::connack_packet::ConnackPacket;
use crate::packet::connect_packet::ConnectPacket;
use crate::packet::disconnect_packet::DisconnectPacket;
use crate::packet::mqtt_packet::Packet;
use crate::packet::puback_packet::PubackPacket;
use crate::packet::publish_packet::QualityOfService::QoS1;
use crate::packet::publish_packet::{PublishPacket, QualityOfService};
use crate::packet::suback_packet::SubackPacket;
use crate::packet::subscription_packet::SubscriptionPacket;
use crate::utils::buffer_reader::BuffReader;
use crate::utils::rng_generator::CountingRng;
pub struct MqttClientV5<'a, T, const MAX_PROPERTIES: usize> {
network_driver: &'a mut T,
buffer: &'a mut [u8],
recv_buffer: &'a mut [u8],
rng: CountingRng,
config: ClientConfig<'a>,
}
impl<'a, T, const MAX_PROPERTIES: usize> MqttClientV5<'a, T, MAX_PROPERTIES>
where
T: Network,
{
pub fn new(network_driver: &'a mut T, buffer: &'a mut [u8]) -> Self {
pub fn new(network_driver: &'a mut T, buffer: &'a mut [u8], recv_buffer: &'a mut [u8], config: ClientConfig<'a>) -> Self {
Self {
network_driver,
buffer,
recv_buffer,
rng: CountingRng(50),
config
}
}
pub async fn connect_to_broker<'b>(&'b mut self) -> Result<(), NetworkError> {
let mut len = {
let mut connect = ConnectPacket::<'b, 3, 0>::clean();
if self.config.username_flag {
connect.add_username(& self.config.username);
}
if self.config.password_flag {
connect.add_password(& self.config.password)
}
connect.encode(self.buffer)
};
@@ -58,35 +75,55 @@ where
Ok(())
}
// connect -> connack -> publish -> QoS ? -> disconn
pub async fn send_message<'b>(
&'b mut self,
topic_name: &'b str,
message: &'b str,
qos: QualityOfService,
) -> Result<(), NetworkError> {
// publish
let identifier: u16 = self.rng.next_u32() as u16;
let len = {
let mut packet = PublishPacket::<'b, 5>::new();
packet.add_topic_name(topic_name);
packet.add_qos(self.config.qos);
packet.add_identifier(identifier);
packet.add_message(message.as_bytes());
packet.encode(self.buffer)
};
self.network_driver.send(self.buffer, len).await?;
let x = self.network_driver.send(self.buffer, len).await;
if let Err(e) = x {
log::error!("Chyba pri prenosu!");
return Err(e);
}
//QoS1
if <QualityOfService as Into<u8>>::into(qos) == <QualityOfService as Into<u8>>::into(QoS1) {
todo!();
if <QualityOfService as Into<u8>>::into(self.config.qos ) == <QualityOfService as Into<u8>>::into(QoS1) {
let reason = {
self.network_driver.receive(self.buffer).await?;
let mut packet = PubackPacket::<'b, 5>::new();
packet.decode(&mut BuffReader::new(self.buffer));
[packet.packet_identifier, packet.reason_code as u16]
};
if identifier != reason[0] {
return Err(NetworkError::IDNotMatchedOnAck);
}
if reason[1] != 0 {
return Err(NetworkError::QoSAck);
}
}
Ok(())
}
// TODO - multiple topic subscribe func
pub async fn subscribe_to_topic<'b>(&'b mut self, topic_name: &'b str) -> Result<(), NetworkError> {
let len = {
let mut subs = SubscriptionPacket::<'b, 1, 1>::new();
subs.add_new_filter(topic_name);
subs.add_new_filter(topic_name, self.config.qos);
subs.encode(self.buffer)
};
let xx: [u8; 14] = (self.buffer[0..14]).try_into().unwrap();
@@ -100,7 +137,7 @@ where
*packet.reason_codes.get(0).unwrap()
};
if reason > 1 {
if reason == (<QualityOfService as Into<u8>>::into(self.config.qos) >> 1) {
Err(NetworkError::Unknown)
} else {
Ok(())
@@ -109,9 +146,20 @@ where
}
pub async fn receive_message<'b>(&'b mut self) -> Result<&'b [u8], NetworkError> {
self.network_driver.receive(self.buffer).await?;
self.network_driver.receive(self.recv_buffer).await?;
let mut packet = PublishPacket::<'b, 5>::new();
packet.decode(&mut BuffReader::new(self.buffer));
packet.decode(&mut BuffReader::new(self.recv_buffer));
if (packet.fixed_header & 0x06) == <QualityOfService as Into<u8>>::into(QualityOfService::QoS1) {
let mut puback = PubackPacket::<'b, 5>::new();
puback.packet_identifier = packet.packet_identifier;
puback.reason_code = 0x00;
{
let len = puback.encode(self.buffer);
self.network_driver.send(self.buffer, len).await ?;
}
}
return Ok(packet.message.unwrap());
}
}

View File

@@ -1 +1,2 @@
pub mod client_v5;
pub mod client_config;