1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(clippy::unwrap_used)]
//! See: `book/tutorial/04-default-codec.md` for full example
use tinyklv::prelude::*; // Klv proc-macro, traits
use tinyklv::dec::binary as decb; // binary decoders
#[derive(Klv, Debug, PartialEq)]
#[klv(
stream = &[u8],
sentinel = b"HEARTBEAT",
key(dec = decb::u8),
len(dec = decb::u8_as_usize),
default(typ = u8, dec = decb::u8),
default(typ = u16, dec = decb::be_u16),
default(typ = u32, dec = decb::be_u32),
allow_unimplemented_encode,
)]
struct Heartbeat {
#[klv(key = 0x01)] sequence: u8,
#[klv(key = 0x02)] temperature_centideg: u16,
#[klv(key = 0x03)] battery_pct: u8,
#[klv(key = 0x04)] rssi_dbm: u8,
#[klv(key = 0x05)] uptime_s: u32,
#[klv(key = 0x06)] mode_flags: u8,
}
fn main() {
// manually construct the stream
let stream = [
0xDE, 0xAD, 0xBE, 0x00, // junk preamble, no sentinel here
// "HEARTBEAT" sentinel
0x48, 0x45, 0x41, 0x52, 0x54, 0x42, 0x45, 0x41, 0x54,
0x16, // body length = 22 bytes
0x01, 0x01, 0x2A, // sequence = 42
0x02, 0x02, 0x09, 0x2E, // temperature = 2350
0x03, 0x01, 0x57, // battery_pct = 87
0x04, 0x01, 0xB8, // rssi_dbm = 0xB8
0x05, 0x04, 0x00, 0x00, 0x0E, 0x10, // uptime_s = 3600
0x06, 0x01, 0x03, // mode_flags = 0b0000_0011
];
// manually construct the expected value
let expected = Heartbeat {
sequence: 42,
temperature_centideg: 2350,
battery_pct: 87,
rssi_dbm: 0xB8,
uptime_s: 3600,
mode_flags: 0b0000_0011,
};
// seek sentinel, decode the value
let decoded = Heartbeat::decode_frame(
&mut stream.as_slice(),
).unwrap();
// they equal!
assert_eq!(decoded, expected);
}