Skip to main content

Getting started on a device

A device service has no heap. The broker's memory is a static the board declares, a delivery borrows its payload from a receive buffer of that memory, and a reply is encoded into a send buffer of it. This guide builds a valve board that applies the commands published to valves/v1/set and reports the state it reached.

A service has three parts: the handler and the app, the task of the runner that owns the MQTT connection, and the main of the board that spawns both.

The crates​

cargo add ruststream --no-default-features --features embassy,macros
cargo add ruststream-rust-mqtt --features embassy
cargo add embassy-executor embassy-net static_cell

The embassy feature of the broker crate adds EmbassyNetwork, the runner's network over an embassy-net stack. The broker crate is the device variant until its alloc feature is on, and the framework follows: a feature of ruststream that needs a heap, such as json, turns the framework's alloc on, and the broker crate then asks for its own alloc feature as well. A device that has a heap takes the heap variant the same way.

The handler and the app​

The handler takes a Command and returns the State it reaches. Command is the payload as the bytes that arrived: it borrows them from the receive buffer of the broker, so reading a command copies nothing. State is the bytes that leave, and the reply is encoded straight into a send buffer. #[ruststream::app(runtime = embassy)] turns the app function into an embassy task, app::task.

/// A command as the controller sends its bytes, borrowed from the broker's receive buffer.
#[derive(Deserialized)]
struct Command<'a>(&'a [u8]);

/// The state the valve reports, as the bytes that leave.
#[derive(Outgoing, Serialized)]
#[outgoing(name = "valves/v1/state")]
struct State(&'static [u8]);

/// Moves the valve and reports where it is.
#[subscriber(MqttTopic::new("valves/v1/set"), reply)]
async fn set(command: &Command<'_>) -> State {
match command.0 {
b"open" => State(b"opened"),
_ => State(b"closed"),
}
}

#[ruststream::app(runtime = embassy)]
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| b.include(set).out_reply(MqttPublish::default()))
}

The memory, the runner and the board​

MqttState is all the memory the broker owns: the receive buffers deliveries borrow from, the send buffers, and the table of subscriptions. The board declares it as a static and hands it to MqttBroker::new once, which returns the broker with its runner. The runner owns the connection: it opens it when the app connects the broker, and every read, write and acknowledgement goes through it. So the board spawns two tasks, the runner's over the network stack and the app's with the broker. The socket buffers live in the runner's task, and their size is the board's choice.

/// The broker's memory: four deliveries of up to 1 KiB, and eight subscriptions.
static MQTT: ConstStaticCell<MqttState> = ConstStaticCell::new(MqttState::new());

/// The runner owns the connection, and the app starts only while it runs.
#[embassy_executor::task]
async fn mqtt(runner: MqttRunner, stack: Stack<'static>) -> ! {
let mut rx = [0; 1024];
let mut tx = [0; 1024];
runner
.run(EmbassyNetwork::new(stack, &mut rx, &mut tx))
.await
}

/// What the board's `main` does once its network stack is up.
fn start(spawner: Spawner, stack: Stack<'static>) {
let config = MqttConfig::new("192.168.1.10:1883").client_id("valve-1");
let (broker, runner) = MqttBroker::new(MQTT.take(), config);
spawner.spawn(mqtt(runner, stack).expect("the pool holds the runner's task"));
spawner.spawn(app::task(broker).expect("the pool holds the app's task"));
}

The defaults of MqttState are four deliveries of up to 1 KiB and eight subscriptions. The address, the client id, the credentials and the session are the settings of MqttConfig. Before the firmware reaches a board, test the service on the host: the test declares its own memory and needs no runner.