Add licences

This commit is contained in:
Ondrej Babec
2022-02-27 19:40:00 +01:00
parent 4eaa83bf1c
commit 80c0e25eda
35 changed files with 792 additions and 777 deletions

View File

@@ -1,3 +1,28 @@
/*
* MIT License
*
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
use crate::packet::publish_packet::QualityOfService;
use crate::utils::buffer_reader::{BinaryData, EncodedString};

View File

@@ -11,6 +11,7 @@ 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::reason_codes::ReasonCode;
use crate::packet::suback_packet::SubackPacket;
use crate::packet::subscription_packet::SubscriptionPacket;
use crate::utils::buffer_reader::BuffReader;
@@ -38,7 +39,7 @@ where
}
}
pub async fn connect_to_broker<'b>(&'b mut self) -> Result<(), NetworkError> {
pub async fn connect_to_broker<'b>(&'b mut self) -> Result<(), ReasonCode> {
let mut len = {
let mut connect = ConnectPacket::<'b, 3, 0>::clean();
if self.config.username_flag {
@@ -50,7 +51,7 @@ where
connect.encode(self.buffer)
};
self.network_driver.send(self.buffer, len).await?;
self.network_driver.send(self.buffer, len).await ?;
//connack
let reason: u8 = {
@@ -61,14 +62,14 @@ where
};
if reason != 0x00 {
Err(NetworkError::Connection)
return Err(ReasonCode::from(reason));
} else {
Ok(())
}
}
pub async fn disconnect<'b>(&'b mut self) -> Result<(), NetworkError> {
pub async fn disconnect<'b>(&'b mut self) -> Result<(), ReasonCode> {
let mut disconnect = DisconnectPacket::<'b, 5>::new();
let mut len = disconnect.encode(self.buffer);
self.network_driver.send(self.buffer, len).await?;
@@ -79,7 +80,7 @@ where
&'b mut self,
topic_name: &'b str,
message: &'b str,
) -> Result<(), NetworkError> {
) -> Result<(), ReasonCode> {
let identifier: u16 = self.rng.next_u32() as u16;
let len = {
@@ -91,62 +92,58 @@ where
packet.encode(self.buffer)
};
let x = self.network_driver.send(self.buffer, len).await;
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(self.config.qos ) == <QualityOfService as Into<u8>>::into(QoS1) {
let reason = {
self.network_driver.receive(self.buffer).await?;
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);
return Err(ReasonCode::PacketIdentifierNotFound);
}
if reason[1] != 0 {
return Err(NetworkError::QoSAck);
return Err(ReasonCode::from(reason[1] as u8));
}
}
Ok(())
}
// TODO - multiple topic subscribe func
pub async fn subscribe_to_topic<'b>(&'b mut self, topic_name: &'b str) -> Result<(), NetworkError> {
pub async fn subscribe_to_topic<'b>(&'b mut self, topic_name: &'b str) -> Result<(), ReasonCode> {
let len = {
let mut subs = SubscriptionPacket::<'b, 1, 1>::new();
subs.add_new_filter(topic_name, self.config.qos);
subs.encode(self.buffer)
};
let xx: [u8; 14] = (self.buffer[0..14]).try_into().unwrap();
log::info!("{:x?}", xx);
self.network_driver.send(self.buffer, len).await?;
self.network_driver.send(self.buffer, len).await ?;
let reason = {
self.network_driver.receive(self.buffer).await?;
self.network_driver.receive(self.buffer).await ?;
let mut packet = SubackPacket::<'b, 5, 5>::new();
packet.decode(&mut BuffReader::new(self.buffer));
*packet.reason_codes.get(0).unwrap()
};
if reason == (<QualityOfService as Into<u8>>::into(self.config.qos) >> 1) {
Err(NetworkError::Unknown)
if reason != (<QualityOfService as Into<u8>>::into(self.config.qos) >> 1) {
Err(ReasonCode::from(reason))
} else {
Ok(())
}
}
pub async fn receive_message<'b>(&'b mut self) -> Result<&'b [u8], NetworkError> {
self.network_driver.receive(self.recv_buffer).await?;
pub async fn receive_message<'b>(&'b mut self) -> Result<&'b [u8], ReasonCode> {
self.network_driver.receive(self.recv_buffer).await ?;
let mut packet = PublishPacket::<'b, 5>::new();
packet.decode(&mut BuffReader::new(self.recv_buffer));

View File

@@ -1,2 +1,27 @@
/*
* MIT License
*
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
pub mod client_v5;
pub mod client_config;