From 19189307613654a3967159f364cb7eb90b45b4ac Mon Sep 17 00:00:00 2001 From: Ondrej Babec Date: Fri, 25 Feb 2022 09:31:16 +0100 Subject: [PATCH] Client first --- Cargo.lock | 185 +++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + LICENSE | 16 +-- src/client/client_v5.rs | 20 ++++ src/client/mod.rs | 1 + src/lib.rs | 2 + src/main.rs | 25 ----- src/network/mod.rs | 1 + src/network/network_trait.rs | 5 + src/packet/publish_packet.rs | 14 ++- 10 files changed, 232 insertions(+), 38 deletions(-) create mode 100644 src/client/client_v5.rs create mode 100644 src/client/mod.rs create mode 100644 src/network/mod.rs create mode 100644 src/network/network_trait.rs diff --git a/Cargo.lock b/Cargo.lock index 6e711ae..77f9b8b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -89,6 +89,12 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "bytes" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" + [[package]] name = "cfg-if" version = "1.0.0" @@ -484,6 +490,28 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" +[[package]] +name = "mio" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba272f85fa0b41fc91872be579b3bbe0f56b792aa361a380eb669469f68dafb2" +dependencies = [ + "libc", + "log", + "miow", + "ntapi", + "winapi", +] + +[[package]] +name = "miow" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" +dependencies = [ + "winapi", +] + [[package]] name = "nb" version = "0.1.3" @@ -510,6 +538,15 @@ dependencies = [ "version_check", ] +[[package]] +name = "ntapi" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c28774a7fd2fbb4f0babd8237ce554b73af68021b5f695a3cebd6c59bac0980f" +dependencies = [ + "winapi", +] + [[package]] name = "num-traits" version = "0.1.43" @@ -528,6 +565,45 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "once_cell" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" + +[[package]] +name = "parking_lot" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f5ec2493a61ac0506c0f4199f99070cbe83857b0337006a30f3e6719b8ef58" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28141e0cc4143da2443301914478dc976a61ffdb3f043058310c70df2fed8954" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", +] + [[package]] name = "pin-project" version = "1.0.10" @@ -608,6 +684,15 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +[[package]] +name = "redox_syscall" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" +dependencies = [ + "bitflags", +] + [[package]] name = "regex" version = "1.5.4" @@ -661,6 +746,7 @@ dependencies = [ "env_logger", "heapless", "log", + "tokio", ] [[package]] @@ -734,12 +820,37 @@ dependencies = [ "serde 1.0.136", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" +dependencies = [ + "libc", +] + [[package]] name = "slab" version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5" +[[package]] +name = "smallvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" + +[[package]] +name = "socket2" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66d72b759436ae32898a2af0a14218dbf55efde3feeb170eb623637db85ee1e0" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "spin" version = "0.9.2" @@ -787,6 +898,37 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "tokio" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af73ac49756f3f7c01172e34a23e5d0216f6c32333757c2c61feb2bbff5a5ee" +dependencies = [ + "bytes", + "libc", + "memchr", + "mio", + "num_cpus", + "once_cell", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "winapi", +] + +[[package]] +name = "tokio-macros" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b557f72f448c511a979e2564e55d74e6c4432fc96ff4f6241bc6bded342643b7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "toml" version = "0.5.8" @@ -860,6 +1002,49 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3df6e476185f92a12c072be4a189a0210dcdcf512a1891d6dff9edb874deadc6" +dependencies = [ + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_msvc" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8e92753b1c443191654ec532f14c199742964a061be25d77d7a96f09db20bf5" + +[[package]] +name = "windows_i686_gnu" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a711c68811799e017b6038e0922cb27a5e2f43a2ddb609fe0b6f3eeda9de615" + +[[package]] +name = "windows_i686_msvc" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "146c11bb1a02615db74680b32a68e2d61f553cc24c4eb5b4ca10311740e44172" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c912b12f7454c6620635bbff3450962753834be2a594819bd5e945af18ec64bc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "504a2476202769977a040c6364301a3f65d0cc9e3fb08600b2bda150a0488316" + [[package]] name = "yaml-rust" version = "0.4.5" diff --git a/Cargo.toml b/Cargo.toml index bfc5885..7bba01c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ drogue-device = { version = "0.1.0", default-features = false, features = ["log" env_logger = "0.9.0" log = "0.4.14" heapless = "0.7.10" +tokio = { version = "1", features = ["full"] } [patch.crates-io] embassy = { git = "https://github.com/embassy-rs/embassy.git", rev = "d76cd5ceaf5140c48ef97180beae156c0c0e07c8" } diff --git a/LICENSE b/LICENSE index f288702..a824681 100644 --- a/LICENSE +++ b/LICENSE @@ -98,7 +98,7 @@ public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. +a computer Network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible @@ -264,7 +264,7 @@ in one of these ways: medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. + Corresponding Source from a Network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This @@ -277,7 +277,7 @@ in one of these ways: Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source + copy the object code is a Network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the @@ -330,9 +330,9 @@ been installed in ROM). requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. +Network may be denied when the modification itself materially and +adversely affects the operation of the Network or violates the rules and +protocols for communication across the Network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly @@ -499,7 +499,7 @@ patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, +publicly available Network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner @@ -557,7 +557,7 @@ under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the +section 13, concerning interaction through a Network will apply to the combination as such. 14. Revised Versions of this License. diff --git a/src/client/client_v5.rs b/src/client/client_v5.rs new file mode 100644 index 0000000..6f0e3c3 --- /dev/null +++ b/src/client/client_v5.rs @@ -0,0 +1,20 @@ +use crate::packet::publish_packet::PublishPacket; +use crate::network::network_trait::Network; + +struct MqttClientV5 { + network_driver: T, +} + +impl MqttClientV5 +where + T: Network, +{ + fn send_message(& mut self, topic_name: & str, message: & str, buffer: & mut [u8]) { + let packet = PublishPacket::new(topic_name, message); + self.network_driver.send() + } + + fn receive_message(& mut self) { + + } +} diff --git a/src/client/mod.rs b/src/client/mod.rs new file mode 100644 index 0000000..ab4c08b --- /dev/null +++ b/src/client/mod.rs @@ -0,0 +1 @@ +pub mod client_v5; \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 9ce0124..4b69db3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,8 @@ pub mod encoding; pub mod packet; pub mod utils; +pub mod client; +mod network; #[allow(unused_variables)] pub fn print_stack(file: &'static str, line: u32) { diff --git a/src/main.rs b/src/main.rs index d8ac3a5..8764f37 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,3 @@ -/*use rust_mqtt::packet::mqtt_packet::*; -use rust_mqtt::packet::property::*;*/ -/*use heapless::Vec; -use std::fs::File; -use std::io::Read;*/ - use rust_mqtt::packet::connect_packet::ConnectPacket; use rust_mqtt::packet::mqtt_packet::Packet; use rust_mqtt::packet::publish_packet::PublishPacket; @@ -31,23 +25,4 @@ fn main() { let lncntrl = cntrl.encode(&mut res3); println!("{:02X?}", &res3[0..lncntrl]); log::info!("xxx"); - - /*let fl = File::open("/Users/obabec/development/school/rust-mqtt/mqtt_control_example.bin"); - - let mut f = File::open("/Users/obabec/development/school/rust-mqtt/mqtt_control_example.bin").expect("no file found"); - let mut buffer: [u8; 500] = [0; 500]; - f.read(&mut buffer).expect("buffer overflow"); - - - // - let mut payld = *b"xxxxx";*/ - //let packet = Packet::clean(txt, &mut payld); - /*let mut buffer_reader = BuffReader::new(&buffer); - packet_builder.decode_packet(& mut buffer_reader); - - - let bytes: [u8; 4] = packet_builder.currentPacket.protocol_name.to_be_bytes(); - - let prot = std::str::from_utf8(&bytes).unwrap(); - log::info!("Protocol name: {}", prot)*/ } diff --git a/src/network/mod.rs b/src/network/mod.rs new file mode 100644 index 0000000..294a433 --- /dev/null +++ b/src/network/mod.rs @@ -0,0 +1 @@ +pub mod network_trait; \ No newline at end of file diff --git a/src/network/network_trait.rs b/src/network/network_trait.rs new file mode 100644 index 0000000..2564695 --- /dev/null +++ b/src/network/network_trait.rs @@ -0,0 +1,5 @@ + +pub trait Network { + fn send(buffer: & mut [u8]); + fn receive(buffer: & mut [u8]); +} \ No newline at end of file diff --git a/src/packet/publish_packet.rs b/src/packet/publish_packet.rs index cb7d05e..3bbaaff 100644 --- a/src/packet/publish_packet.rs +++ b/src/packet/publish_packet.rs @@ -27,21 +27,25 @@ pub struct PublishPacket<'a, const MAX_PROPERTIES: usize> { } impl<'a, const MAX_PROPERTIES: usize> PublishPacket<'a, MAX_PROPERTIES> { - pub fn new(message: &'a [u8]) -> Self { + pub fn new(topic_name: & str, message: &'a str) -> Self { let mut x = Self { fixed_header: PacketType::Publish.into(), remain_len: 0, topic_name: EncodedString::new(), - packet_identifier: 0, + packet_identifier: 1, property_len: 0, properties: Vec::, MAX_PROPERTIES>::new(), - message, + message: message.as_bytes(), }; - x.topic_name.string = "test/topic"; - x.topic_name.len = 10; + x.add_topic_name(topic_name); return x; } + pub fn add_topic_name(&mut self, topic_name: & str) { + self.topic_name.string = topic_name; + self.topic_name.len = topic_name.len() as u16; + } + pub fn decode_publish_packet(&mut self, buff_reader: &mut BuffReader<'a>) { if self.decode_fixed_header(buff_reader) != (PacketType::Publish).into() { log::error!("Packet you are trying to decode is not PUBLISH packet!");