From 65a10377c502810a7aee441cf42f74cd0f42832d Mon Sep 17 00:00:00 2001 From: Mathias Koch Date: Sun, 23 Jan 2022 20:48:52 +0100 Subject: [PATCH] Add initial std feature to allow using std::Box rather than heapless::Box (#44) * Add initial std feature to allow using std::Box rather than heapless::Box * Remove unused defmt features --- .github/workflows/ci.yml | 12 ++++++------ .vscode/settings.json | 2 +- mqttrust_core/Cargo.toml | 17 +++++------------ mqttrust_core/src/eventloop.rs | 14 +++++++++++--- mqttrust_core/src/lib.rs | 11 ++++++++--- mqttrust_core/src/state.rs | 26 +++++++++++++++++++------- 6 files changed, 50 insertions(+), 32 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7b8678e..3a6448c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,7 +40,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: test - args: --lib --features "x86,log" + args: --lib --features "log" integration: name: Integration Test runs-on: ubuntu-latest @@ -61,7 +61,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: run - args: --features=x86,log --example echo + args: --features=log --example echo device_advisor: name: AWS IoT Device Advisor @@ -100,7 +100,7 @@ jobs: AWS_HOSTNAME: ${{ steps.hostname.outputs.AWS_HOSTNAME }} with: command: build - args: --features=x86,log --example aws_device_advisor --release + args: --features=log --example aws_device_advisor --release - name: Start test suite id: test_suite @@ -206,7 +206,7 @@ jobs: uses: actions-rs/clippy-check@v1 with: token: ${{ secrets.GITHUB_TOKEN }} - args: --features "x86,log" -- ${{ env.CLIPPY_PARAMS }} + args: --features "log" -- ${{ env.CLIPPY_PARAMS }} grcov: name: Coverage runs-on: ubuntu-latest @@ -236,7 +236,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: test - args: --lib --no-fail-fast --features "x86,log" + args: --lib --no-fail-fast --features "log" env: CARGO_INCREMENTAL: "0" RUSTFLAGS: "-Zprofile -Ccodegen-units=1 -Copt-level=0 -Coverflow-checks=off -Cpanic=unwind -Zpanic_abort_tests" @@ -296,7 +296,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: doc - args: --verbose --no-deps --features "x86,log" + args: --verbose --no-deps --features "log" # - name: Finalize documentation # run: | diff --git a/.vscode/settings.json b/.vscode/settings.json index c28d5a3..ac799dc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,5 +7,5 @@ "rust-analyzer.diagnostics.disabled": [ "unresolved-import" ], - "rust-analyzer.cargo.features": ["log", "x86"] + "rust-analyzer.cargo.features": ["log"] } \ No newline at end of file diff --git a/mqttrust_core/Cargo.toml b/mqttrust_core/Cargo.toml index 4c0e10f..90b84b5 100644 --- a/mqttrust_core/Cargo.toml +++ b/mqttrust_core/Cargo.toml @@ -16,11 +16,11 @@ name = "mqttrust_core" [[example]] name = "echo" -required-features = ["x86", "log"] +required-features = ["log"] [[example]] name = "aws_device_advisor" -required-features = ["x86", "log"] +required-features = ["log"] [badges] maintenance = { status = "actively-developed" } @@ -29,7 +29,7 @@ maintenance = { status = "actively-developed" } embedded-hal = { version = "=1.0.0-alpha.6" } embedded-nal = "0.6.0" nb = "^1" -heapless = { version = "^0.7", features = ["serde"] } +heapless = { version = "^0.7", features = ["serde", "x86-sync-pool"] } mqttrust = { version = "^0.4.1-alpha.0", path = "../mqttrust" } bbqueue = "0.5" fugit = { version = "0.3", features = ["defmt"] } @@ -46,13 +46,6 @@ env_logger = "0.9.0" [features] default = [] -x86 = ["heapless/x86-sync-pool"] +std = [] -defmt-impl = ["defmt", "mqttrust/defmt-impl", "heapless/defmt-impl"] - -defmt-default = ["defmt-impl"] -defmt-trace = ["defmt-impl"] -defmt-debug = ["defmt-impl"] -defmt-info = ["defmt-impl"] -defmt-warn = ["defmt-impl"] -defmt-error = ["defmt-impl"] +defmt-impl = ["defmt", "mqttrust/defmt-impl", "heapless/defmt-impl", "fugit/defmt"] \ No newline at end of file diff --git a/mqttrust_core/src/eventloop.rs b/mqttrust_core/src/eventloop.rs index dbc6df0..c9768a0 100644 --- a/mqttrust_core/src/eventloop.rs +++ b/mqttrust_core/src/eventloop.rs @@ -54,9 +54,17 @@ where // Socket is present, but not connected. Usually this implies // that the socket is closed for writes. Disconnect to close & // recycle the socket. - mqtt_log!(warn, "Socket cleanup!"); - self.disconnect(network); - return Err(EventError::Network(NetworkError::SocketClosed).into()); + + // Fallthrough to allow reading mqtt client error codes, unless + // MQTT is actually connected + if matches!( + self.state.connection_status, + MqttConnectionStatus::Connected + ) { + mqtt_log!(warn, "Socket cleanup!"); + self.disconnect(network); + return Err(EventError::Network(NetworkError::SocketClosed).into()); + } } Err(_) => { // We have no socket present at all diff --git a/mqttrust_core/src/lib.rs b/mqttrust_core/src/lib.rs index 9fee24b..4575efc 100644 --- a/mqttrust_core/src/lib.rs +++ b/mqttrust_core/src/lib.rs @@ -1,5 +1,8 @@ #![cfg_attr(not(test), no_std)] +#[cfg(feature = "std")] +extern crate std; + mod client; mod eventloop; mod logging; @@ -12,12 +15,11 @@ pub use bbqueue; pub use client::Client; use core::convert::TryFrom; pub use eventloop::EventLoop; -use heapless::pool::{singleton::Box, Init}; use heapless::{String, Vec}; pub use mqttrust::encoding::v4::{Pid, Publish, QoS, QosPid, Suback}; pub use mqttrust::*; pub use options::{Broker, MqttOptions}; -use state::{BoxedPublish, StateError}; +use state::StateError; #[derive(Debug, PartialEq)] #[cfg_attr(feature = "defmt-impl", derive(defmt::Format))] @@ -37,7 +39,10 @@ pub enum Notification { /// Incoming connection acknowledge ConnAck, /// Incoming publish from the broker - Publish(Box), + #[cfg(not(feature = "std"))] + Publish(heapless::pool::singleton::Box), + #[cfg(feature = "std")] + Publish(std::boxed::Box), /// Incoming puback from the broker Puback(Pid), /// Incoming pubrec from the broker diff --git a/mqttrust_core/src/state.rs b/mqttrust_core/src/state.rs index 4ec00d1..f0551e5 100644 --- a/mqttrust_core/src/state.rs +++ b/mqttrust_core/src/state.rs @@ -1,10 +1,12 @@ use crate::mqtt_log; use crate::packet::SerializedPacket; use crate::Notification; +#[cfg(not(feature = "std"))] use crate::PublishNotification; use core::convert::TryInto; use fugit::TimerDurationU32; use fugit::TimerInstantU32; +#[cfg(not(feature = "std"))] use heapless::{pool, pool::singleton::Pool}; use heapless::{FnvIndexMap, FnvIndexSet, IndexMap, IndexSet}; use mqttrust::encoding::v4::*; @@ -40,6 +42,7 @@ pub enum StateError { InvalidHeader, } +#[cfg(not(feature = "std"))] pool!( #[allow(non_upper_case_globals)] BoxedPublish: PublishNotification @@ -78,13 +81,16 @@ impl MqttState { /// connection for persistent sessions while new state should /// instantiated for clean sessions pub fn new() -> Self { - const LEN: usize = core::mem::size_of::() - + core::mem::align_of::() - - core::mem::size_of::() - % core::mem::align_of::(); - - static mut PUBLISH_MEM: [u8; LEN] = [0u8; LEN]; - BoxedPublish::grow(unsafe { &mut PUBLISH_MEM }); + #[cfg(not(feature = "std"))] + { + const LEN: usize = core::mem::size_of::() + + core::mem::align_of::() + - core::mem::size_of::() + % core::mem::align_of::(); + + static mut PUBLISH_MEM: [u8; LEN] = [0u8; LEN]; + BoxedPublish::grow(unsafe { &mut PUBLISH_MEM }); + } MqttState { connection_status: MqttConnectionStatus::Disconnected, @@ -269,9 +275,15 @@ impl MqttState { publish: Publish<'b>, ) -> Result<(Option, Option>), StateError> { let qospid = (publish.qos, publish.pid); + + #[cfg(not(feature = "std"))] let boxed_publish = BoxedPublish::alloc().unwrap(); + #[cfg(not(feature = "std"))] let notification = Notification::Publish(boxed_publish.init(publish.try_into().unwrap())); + #[cfg(feature = "std")] + let notification = Notification::Publish(std::boxed::Box::new(publish.try_into().unwrap())); + let request = match qospid { (QoS::AtMostOnce, _) => None, (QoS::AtLeastOnce, Some(pid)) => Some(Packet::Puback(pid)),