pub struct Application<'a> {
pub ell: Option<Ell>,
pub afl: Option<Afl>,
pub ci: u8,
pub header: Header,
pub authenticity: Authenticity,
pub body: Body<'a>,
/* private fields */
}Expand description
A parsed telegram above the link layer.
Fields§
§ell: Option<Ell>The extended link layer, if present.
afl: Option<Afl>The authentication and fragmentation layer, if present.
ci: u8The transport layer’s CI, or the ELL’s while the ELL is still encrypted.
header: HeaderThe transport header.
authenticity: AuthenticityWhat was cryptographically verified.
body: Body<'a>The payload.
Implementations§
Source§impl<'a> Application<'a>
impl<'a> Application<'a>
Sourcepub fn parse(ci: u8, data: &'a [u8]) -> Result<Self>
pub fn parse(ci: u8, data: &'a [u8]) -> Result<Self>
Parse the bytes after a CI field: a wired telegram, or a wireless one when the
link-layer address does not matter (it is needed for a mode-5 short header and
the ELL: use Application::parse_with_link).
Examples found in repository?
14fn main() -> Result<(), mbus_kit::Error> {
15 let Some((Frame::Long { ci, data, .. }, _)) = Frame::parse(&TELEGRAM)? else {
16 return Err(mbus_kit::Error::UnexpectedEnd);
17 };
18 let app = Application::parse(ci, data)?;
19 if let Header::Long(h) = app.header {
20 println!(
21 "{} meter {:08} by {}, version {}",
22 h.address.medium,
23 h.address.id_number().unwrap_or(0),
24 h.address.manufacturer,
25 h.address.version
26 );
27 }
28 if let Body::Records(records) = app.body {
29 for record in records {
30 // e.g. "volume flow · maximum · storage 5 — 0.113 m³/h"
31 println!(" {}", record?);
32 }
33 }
34 Ok(())
35}Sourcepub fn parse_with_link(
ci: u8,
data: &'a [u8],
link: Option<Address>,
) -> Result<Self>
pub fn parse_with_link( ci: u8, data: &'a [u8], link: Option<Address>, ) -> Result<Self>
Parse the bytes after a CI field of a wireless telegram from link.
Sourcepub const fn meter(&self) -> Option<Address>
pub const fn meter(&self) -> Option<Address>
The meter: the long header’s address if there is one (a meter behind a radio adapter or gateway), the link layer’s otherwise.
Examples found in repository?
19fn main() {
20 let mut bus = Scripted::default();
21
22 // 1. Read meter 1 completely: SND_NKE, then REQ_UD2 until no more records follow.
23 // Only telegrams from meter 12345678 count (a swapped meter is caught).
24 let meter = SecondaryAddress::pattern("12345678").unwrap();
25 let mut readout = Readout::new(Target::Primary(1)).expect_meter(meter);
26 while let Some(request) = readout.next_request() {
27 let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
28 let event = if reply.is_empty() { readout.timeout(&reply) } else { readout.reply(&reply) };
29 if let Event::Telegram(app) = event {
30 println!("telegram from {}", app.meter().map_or("?".into(), |m| m.to_string()));
31 for record in app.records().into_iter().flatten().flatten() {
32 println!(" {record}");
33 }
34 }
35 }
36 match readout.outcome() {
37 Some(Outcome::Complete) => println!("complete: {} telegrams\n", readout.telegrams()),
38 Some(other) => println!("not usable: {other}\n"),
39 None => unreachable!("the readout is over"),
40 }
41
42 // 2. Find every meter on the bus by secondary address.
43 let mut search = Search::new();
44 while let Some(request) = search.next_request() {
45 let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
46 let event = if reply.is_empty() { search.timeout(&reply) } else { search.reply(&reply) };
47 match event {
48 Event::Found(meter) => println!("found {meter}"),
49 Event::Unreadable(pattern) => println!("{pattern} answers but does not name itself"),
50 _ => {}
51 }
52 }
53 println!("{} selections", search.selections());
54}Sourcepub fn expand<'o, F: Formats + ?Sized>(
&self,
formats: &F,
out: &'o mut [u8],
) -> Result<Application<'o>>
pub fn expand<'o, F: Formats + ?Sized>( &self, formats: &F, out: &'o mut [u8], ) -> Result<Application<'o>>
A compact frame expanded with a layout from formats into out, as an
application with the same meter, header and authenticity and the full records
(Compact::expand); any other body is
Error::UnsupportedCi.
Sourcepub fn records(&self) -> Option<Records<'a>>
pub fn records(&self) -> Option<Records<'a>>
The records, if the body is plain records.
Examples found in repository?
19fn main() {
20 let mut bus = Scripted::default();
21
22 // 1. Read meter 1 completely: SND_NKE, then REQ_UD2 until no more records follow.
23 // Only telegrams from meter 12345678 count (a swapped meter is caught).
24 let meter = SecondaryAddress::pattern("12345678").unwrap();
25 let mut readout = Readout::new(Target::Primary(1)).expect_meter(meter);
26 while let Some(request) = readout.next_request() {
27 let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
28 let event = if reply.is_empty() { readout.timeout(&reply) } else { readout.reply(&reply) };
29 if let Event::Telegram(app) = event {
30 println!("telegram from {}", app.meter().map_or("?".into(), |m| m.to_string()));
31 for record in app.records().into_iter().flatten().flatten() {
32 println!(" {record}");
33 }
34 }
35 }
36 match readout.outcome() {
37 Some(Outcome::Complete) => println!("complete: {} telegrams\n", readout.telegrams()),
38 Some(other) => println!("not usable: {other}\n"),
39 None => unreachable!("the readout is over"),
40 }
41
42 // 2. Find every meter on the bus by secondary address.
43 let mut search = Search::new();
44 while let Some(request) = search.next_request() {
45 let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
46 let event = if reply.is_empty() { search.timeout(&reply) } else { search.reply(&reply) };
47 match event {
48 Event::Found(meter) => println!("found {meter}"),
49 Event::Unreadable(pattern) => println!("{pattern} answers but does not name itself"),
50 _ => {}
51 }
52 }
53 println!("{} selections", search.selections());
54}