本页译自英文,由模型翻译。若与英文原文存在差异,以英文原文为准。
基于本地内存 Broker 的设备服务
一块温度计板在没有堆的 embassy 运行时上运行 RustStream 服务,并在主机上用一个分配器测试它:框架一旦发生任何分配,测试就会失败。
前面各章的商店运行在 MemoryBroker 和 tokio 运行时上。
固件有自己的一对组合:嵌入式运行时和本地内存 Broker,后者是同一块板子各部分之间的队列。
订阅者的写法不变,挂载也是同样的链式形式。
本章构建一块小板子:传感器发布读数,板子把读数换算成摄氏度,读数过高时发出警报。
依赖
[dependencies]
ruststream = { version = "0.8", default-features = false, features = ["embassy", "memory", "macros", "postcard"] }
serde = { version = "1", default-features = false, features = ["derive"] }
default-features = false 去掉 tokio 和 json,embassy 引入面向固件的运行器。
postcard 是本构建的默认编解码器:它向切片编码、从切片解码,所以板子不需要堆来存放缓冲区。
挂载时不指定编解码器,就使用它。
这份列表里没有任何内容会启用框架的 alloc feature,因此每个大小本该由堆决定的部分都采用固定形态。
总线
板子把总线声明为 static,应用函数收到的是基于它构建的 Broker。
每个订阅容纳四条消息,处理器手中的那条也计入。
总线所依赖的临界区由二进制 crate 提供 critical-section 实现。
在没有比较并交换指令的内核上(例如 Cortex-M0+),固件还要为 portable-atomic 开启 critical-section 特性。
/// 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();
处理器与回复
读数和报告是普通的 serde 类型,它们自己声明要发往哪里。
- 宏
- 手写
/// 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,
}
/// A sensor's reading, as the other end of the bus encodes it with postcard, the build's default codec.
#[derive(Serialize, Deserialize)]
struct Reading {
sensor: u8,
millivolts: u16,
}
// What `#[derive(Outgoing)]` with `#[outgoing(name = "sensors/temp")]` writes.
impl OutgoingDestination for Reading {
type Form = FixedName;
const DESTINATION: &'static str = "sensors/temp";
}
impl MessageHeaders for Reading {
type Contract = NoHeaders;
}
/// What the board reports for a reading.
#[derive(Serialize, Deserialize)]
struct Report {
sensor: u8,
celsius: i16,
}
impl OutgoingDestination for Report {
type Form = FixedName;
const DESTINATION: &'static str = "reports";
}
impl MessageHeaders for Report {
type Contract = NoHeaders;
}
处理器接收一条读数,返回它的报告。 回复发往报告类型所声明的目的地,所以处理器自己不发布任何东西。
- 宏
- 手写
/// 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),
}
}
/// Reports every reading in degrees.
struct OnReading;
impl Handle<Reading, Report, (), (), ()> for OnReading {
fn handle(
&self,
reading: &Reading,
_outs: &(),
_ctx: &mut Context<'_, (), ()>,
) -> impl Future<Output = Result<Report, HandlerOutcome>> {
let millivolts = i32::from(reading.millivolts);
ready(Ok(Report {
sensor: reading.sensor,
celsius: i16::try_from((millivolts - 500) / 10).unwrap_or(i16::MAX),
}))
}
}
应用把处理器挂载到板子的总线上。
out_reply 以本构建的默认编解码器通过总线发布报告,encode_capacity 为每个工作者指定编码回复用的字节数,默认 256。
放不下的回复会以 CodecFailure::BufferFull 被拒绝,绝不会被截断:这次投递会被确认,失败则交给处理器的 .error 延续。
- 宏
- 手写
fn reply_app(bus: Bus) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(bus, |b| {
b.include(on_reading)
.out_reply(LocalMemoryPublish)
.encode_capacity::<64>()
})
}
fn reply_app(bus: Bus) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(bus, |b| {
b.include(
subscriber(LocalMemorySource::new("sensors/temp"), OnReading)
.reply()
.build(),
)
.out_reply(LocalMemoryPublish)
.encode_capacity::<64>()
})
}
发布到另一个主题
第二个处理器读取报告,温度超过五十度时发出警报。
警报和报告一样是 serde 类型,经由 Out 槽位发出,与商店的通知相同。
槽位用本构建的默认编解码器对它编码。
- 宏
- 手写
/// The alert the board raises for a sensor.
#[derive(Debug, PartialEq, Serialize, Deserialize, Outgoing)]
#[outgoing(name = "alerts")]
struct Alert {
sensor: u8,
}
/// The slot alerts leave through.
#[derive(OutSlot)]
#[publishes(Alert)]
struct Alarms;
/// Raises an alert for a report past fifty degrees.
#[subscriber(LocalMemorySource::new("reports"))]
async fn on_hot(report: &Report, Out(alarms): Out<impl Publisher, Alarms>) -> HandlerOutcome {
if report.celsius <= 50 {
return HandlerOutcome::ack();
}
let alert = Alert {
sensor: report.sensor,
};
match alarms.message(&alert).publish().await {
Ok(()) => HandlerOutcome::ack(),
Err(_) => HandlerOutcome::retry(),
}
}
/// The alert the board raises for a sensor.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Alert {
sensor: u8,
}
// What `#[derive(Outgoing)]` with `#[outgoing(name = "alerts")]` writes.
impl OutgoingDestination for Alert {
type Form = FixedName;
const DESTINATION: &'static str = "alerts";
}
impl MessageHeaders for Alert {
type Contract = NoHeaders;
}
/// The slot alerts leave through.
struct Alarms;
impl OutSlot for Alarms {
const NAME: &'static str = "Alarms";
type Destination = Reads;
}
impl PublishedThrough<Alarms> for Alert {}
/// Raises an alert for a report past fifty degrees.
struct OnHot;
impl<Entry> Handle<Report, (), Outs<(Entry,)>, (), ()> for OnHot
where
Entry: OutEntry<Alarms, Wire: Publisher>,
{
async fn handle(
&self,
report: &Report,
outs: &Outs<(Entry,)>,
_ctx: &mut Context<'_, (), ()>,
) -> Result<(), HandlerOutcome> {
if report.celsius <= 50 {
return Ok(());
}
let alert = Alert {
sensor: report.sensor,
};
outs.get(Alarms)
.message(&alert)
.publish()
.await
.map_err(|_| HandlerOutcome::retry())
}
}
挂载加入这个处理器,并指明槽位背后的发布者。
- 宏
- 手写
fn device_app(bus: Bus) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(bus, |b| {
b.include(on_reading)
.out_reply(LocalMemoryPublish)
.encode_capacity::<64>()
.include(on_hot)
.out(Alarms, LocalMemoryPublish)
})
}
fn device_app(bus: Bus) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(bus, |b| {
b.include(
subscriber(LocalMemorySource::new("sensors/temp"), OnReading)
.reply()
.build(),
)
.out_reply(LocalMemoryPublish)
.encode_capacity::<64>()
.include(subscriber(LocalMemorySource::new("reports"), OnHot).build())
.out(Alarms, LocalMemoryPublish)
})
}
在设备上,#[ruststream::app(runtime = embassy)] 把这个函数交给板子的执行器。
设备方面的其余能力见嵌入式运行时页面:按板子时钟的重试、延续和批。
在主机上测试
测试在主机上运行,使用 std,通过嵌入式 TestApp 驱动。
它原样启动板子运行的那个应用,总线本身就是进程内模式,不需要替身 Broker。
测试框架自己的记录(每个订阅收到了什么、每个槽位发布了什么)放在主机的堆上。
它之下的框架保持设备的形态:测试构建不启用 alloc,应用以固定形态被轮询。
测试可以证明服务不做任何分配。
测试二进制把 assert_no_alloc::AllocDisabler 安装为全局分配器,应用被轮询期间框架的任何分配都会使测试失败。
测试框架自己的记录由分配器放行。
这项保护在调试构建中生效,而 cargo test 构建的正是调试版本。
[dev-dependencies]
ruststream = { version = "0.8", default-features = false, features = ["testing"] }
assert_no_alloc = "1.1"
futures = "0.3"
- 宏
- 手写
/// Fails an allocation made while the harness polls the app.
#[global_allocator]
static ALLOCATOR: AllocDisabler = AllocDisabler;
#[test]
fn a_hot_reading_raises_an_alert() -> Result<(), Box<dyn Error>> {
block_on(async {
let tb = TestApp::start(device_app(Bus::new(&BUS))).await?;
let reading = Reading {
sensor: 2,
millivolts: 1200,
};
tb.broker::<Bus>().message(&reading).publish().await?;
tb.broker::<Bus>()
.subscriber("reports")
.assert_called_once();
tb.out::<Alarms>()
.assert_called_once()
.decoded_as::<Alert>()
.with(&Alert { sensor: 2 });
tb.shutdown().await?;
Ok(())
})
}
/// Fails an allocation made while the harness polls the app.
#[global_allocator]
static ALLOCATOR: AllocDisabler = AllocDisabler;
#[test]
fn a_hot_reading_raises_an_alert() -> Result<(), Box<dyn Error>> {
block_on(async {
let tb = TestApp::start(device_app(Bus::new(&BUS))).await?;
let reading = Reading {
sensor: 2,
millivolts: 1200,
};
tb.broker::<Bus>().message(&reading).publish().await?;
tb.broker::<Bus>()
.subscriber("reports")
.assert_called_once();
tb.out::<Alarms>()
.assert_called_once()
.decoded_as::<Alert>()
.with(&Alert { sensor: 2 });
tb.shutdown().await?;
Ok(())
})
}
测试发布一条 1200 毫伏的读数,处理器把它换算为 70 度。
它检查报告已到达 reports,并且传感器 2 的警报经由它的槽位发出。
测试中的发布会等到它引发的一切都结算之后才返回,所以检查可以紧随其后。
板子的时钟只在测试调用 advance 时前进,与商店测试中暂停的时钟一样。
让测试保持设备的形态
Cargo 对一次构建中的每个 crate 只构建一次,使用这次构建里所有 crate 请求的 feature 的并集。
如果某个开发依赖,或测试构建中的任何其他 crate,启用了框架的 alloc feature,被测服务也会随之启用它。
这时测试检查的是带堆的框架形态,分配器对设备什么也没证明。
常见原因是依赖行保留了默认 feature,例如 ruststream = { version = "0.8", features = ["testing"] }:它引入 json 和 tokio,而这两者都隐含 alloc。
alloc、tokio、compio、tracing、json、msgpack、cbor 和 conformance 同样如此。
因此服务中每一行 ruststream(包括开发依赖)都带 default-features = false,测试构建中也没有其他 crate 请求这些 feature。
核心仓库把自己的无 alloc 构建单独放在 checks/noalloc,因为 crate 自身单元测试所需的开发依赖会启用 alloc。
它的 just test-no-alloc 配方用 cargo tree -e features 列出每个构建的 feature 树,一旦其中出现 alloc 就失败。
同样的检查也适用于服务:
cargo tree -e features,normal,build,dev -i ruststream-core | grep 'feature "alloc"'
只要服务保持设备的形态,这条命令就不输出任何内容。