下面显示的是英文原文。
Local memory broker
LocalMemoryBroker is the in-process bus of the embassy runtime, in ruststream::memory::local: a complete broker for a queue between the parts of one firmware, and the reference implementation of the contract for brokers whose types are not Send.
Its types are not Send on embassy alone; next to compio the bus shares its state by Arc behind a critical section, and its types are Send.
It comes with the memory and embassy features, runs on any executor and reads no clock.
Its constructor does no I/O, like every broker constructor, and a subscription receives what is published after it opened.
LocalMemorySource is its subscription descriptor and LocalMemoryPublish its publish policy.
A publish to a subscription that holds its capacity is refused with LocalMemoryError::Full, and any use of a bus that was shut down returns an error.
The first const parameter of the broker is how many messages one subscription holds, waiting or in its handler's hands; a message in hand keeps its place, so a requeue always finds room.
The bus has a form for each build of the framework.
With the alloc feature the bus lives on the heap and the broker is built with LocalMemoryBroker::new().
Without it the bus is a LocalMemoryBus of fixed size, which the board declares as a static, and the broker is built over it with LocalMemoryBroker::new(&BUS).
LocalMemoryBus<MESSAGES, PAYLOAD, SUBSCRIPTIONS> fixes the messages each subscription holds, the largest payload in bytes (64 unless named), and how many subscriptions are open at once (8 unless named).
A bus that holds no message, no byte or no subscription does not build.
An empty bus is all zero bytes, so the board's static lies in .bss: it takes RAM that the startup code zeroes, and no flash.
The bus sits behind a critical section, so the binary supplies the critical-section implementation.
A delivery lies in a place of the bus from its publish to its settlement, and the message a handler reads holds that place, so it moves a reference rather than the payload and headers.
Settling the message frees the place; a delivery whose subscriber left keeps its place until it is settled.
On a core without compare-and-swap, such as a Cortex-M0+, the firmware also gives portable-atomic its critical-section feature.
Example
The bus of a device with no heap, and a service mounted on it:
/// The board's bus: each subscription holds four messages.
type Bus = LocalMemoryBroker<4>;
/// The bus is a `static` the board declares; the app function receives a broker built over it,
/// `Bus::new(&BUS)`.
static BUS: LocalMemoryBus<4> = LocalMemoryBus::new();
/// A sensor's reading, as the other end of the bus encodes it with postcard, the build's default codec.
#[derive(Serialize, Deserialize, Outgoing)]
#[outgoing(name = "sensors/temp")]
struct Reading {
sensor: u8,
millivolts: u16,
}
/// What the board reports for a reading.
#[derive(Serialize, Deserialize, Outgoing)]
#[outgoing(name = "reports")]
struct Report {
sensor: u8,
celsius: i16,
}
/// Reports every reading in degrees.
#[subscriber(LocalMemorySource::new("sensors/temp"), reply)]
async fn on_reading(reading: &Reading) -> Report {
let millivolts = i32::from(reading.millivolts);
Report {
sensor: reading.sensor,
celsius: i16::try_from((millivolts - 500) / 10).unwrap_or(i16::MAX),
}
}
/// Takes the reports and updates the display once each is acknowledged.
#[subscriber(LocalMemorySource::new("reports"))]
async fn on_report(report: &Report) -> HandlerOutcome {
let celsius = report.celsius;
// The continuation lives in a room of 128 bytes; a capture past it does not compile.
HandlerOutcome::ack().and_after(async move { show(celsius).await })
}
/// Draws a temperature on the board's display.
async fn show(celsius: i16) {
Timer::after_millis(10).await;
let _ = celsius;
}
fn app(bus: Bus) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(bus, |b| {
b.include(on_report)
.include(on_reading)
.out_reply(LocalMemoryPublish)
.encode_capacity::<64>()
})
}
The service itself is taught on the Embedded runtime page, and a test drives it with the embedded TestApp, whose broker is the same LocalMemoryBroker.