Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial std feature to allow using std::Box rather than heapless::Box #44

Merged
merged 3 commits into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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: |
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"rust-analyzer.diagnostics.disabled": [
"unresolved-import"
],
"rust-analyzer.cargo.features": ["log", "x86"]
"rust-analyzer.cargo.features": ["log"]
}
17 changes: 5 additions & 12 deletions mqttrust_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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"] }
Expand All @@ -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"]
14 changes: 11 additions & 3 deletions mqttrust_core/src/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions mqttrust_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![cfg_attr(not(test), no_std)]

#[cfg(feature = "std")]
extern crate std;

mod client;
mod eventloop;
mod logging;
Expand All @@ -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))]
Expand All @@ -37,7 +39,10 @@ pub enum Notification {
/// Incoming connection acknowledge
ConnAck,
/// Incoming publish from the broker
Publish(Box<BoxedPublish, Init>),
#[cfg(not(feature = "std"))]
Publish(heapless::pool::singleton::Box<state::BoxedPublish, heapless::pool::Init>),
#[cfg(feature = "std")]
Publish(std::boxed::Box<PublishNotification>),
/// Incoming puback from the broker
Puback(Pid),
/// Incoming pubrec from the broker
Expand Down
26 changes: 19 additions & 7 deletions mqttrust_core/src/state.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -40,6 +42,7 @@ pub enum StateError {
InvalidHeader,
}

#[cfg(not(feature = "std"))]
pool!(
#[allow(non_upper_case_globals)]
BoxedPublish: PublishNotification
Expand Down Expand Up @@ -78,13 +81,16 @@ impl<const TIMER_HZ: u32> MqttState<TIMER_HZ> {
/// connection for persistent sessions while new state should
/// instantiated for clean sessions
pub fn new() -> Self {
const LEN: usize = core::mem::size_of::<PublishNotification>()
+ core::mem::align_of::<PublishNotification>()
- core::mem::size_of::<PublishNotification>()
% core::mem::align_of::<PublishNotification>();

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::<PublishNotification>()
+ core::mem::align_of::<PublishNotification>()
- core::mem::size_of::<PublishNotification>()
% core::mem::align_of::<PublishNotification>();

static mut PUBLISH_MEM: [u8; LEN] = [0u8; LEN];
BoxedPublish::grow(unsafe { &mut PUBLISH_MEM });
}

MqttState {
connection_status: MqttConnectionStatus::Disconnected,
Expand Down Expand Up @@ -269,9 +275,15 @@ impl<const TIMER_HZ: u32> MqttState<TIMER_HZ> {
publish: Publish<'b>,
) -> Result<(Option<Notification>, Option<Packet<'static>>), 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)),
Expand Down