Module codec
Expand description
TLV encoding and decoding.
A QMI message body is a list of TLVs: a u8 type, a u16 length and the
value. Values are packed without padding, and all integers are
little-endian.
Strings and variable-length arrays nested in a struct start with a length
prefix, see Prefixed. The IDL sets the width of each prefix (u8,
u16 or u32), independent of the maximum length. A string that makes
up the whole TLV has no prefix. A variable-length array always has one.
Strings are sent without the NUL.
Decoding shares the message buffer and doesnβt copy bytes, strings or
arrays. Use Bytes for opaque bytes, Utf8Bytes for UTF-8 text and
Items for arrays. Encoding borrows, so requests can hold &str and
&[T].
Β§Example
use bytes::Bytes;
use ql_qmi::codec::{Decode, Items, Prefixed, Reader, TlvWriter, ValueError};
// A request with a `u16` and a string with a `u8` length prefix.
let mut tlvs = TlvWriter::new();
tlvs.tlv(0x01, &0x0102u16)
.tlv(0x10, &Prefixed::<u8, _>::new("ab").unwrap());
assert_eq!(
tlvs.body(),
[
0x01, 0x02, 0x00, 0x02, 0x01, 0x10, 0x03, 0x00, 0x02, b'a', b'b'
],
);
// A struct with an array of `u16`, as received in a response.
struct Ranges {
kind: u8,
values: Items<u16>,
}
impl Decode for Ranges {
fn decode(reader: &mut Reader) -> Result<Self, ValueError> {
Ok(Self {
kind: reader.read()?,
values: reader.read::<Prefixed<u8, _>>()?.into_inner(),
})
}
}
let mut reader = Reader::new(Bytes::from_static(&[7, 2, 0x10, 0x00, 0x20, 0x00]));
let ranges: Ranges = reader.read().unwrap();
assert_eq!(ranges.kind, 7);
assert!(ranges.values.iter().eq([0x10, 0x20]));ModulesΒ§
- flags π
- items π
- open_
enum π - prefixed π
- primitive π
- reader π
- tlvs π
- utf8 π
- writer π
MacrosΒ§
- flags
- Implements
EncodeandDecodefor a bit mask type. - open_
enum - Declares an open QMI enum: a transparent newtype with associated constants.
StructsΒ§
- Bytes
- A cheaply cloneable and sliceable chunk of contiguous memory.
- Items
- A decoded array that shares the message buffer.
- Items
Iter - Iterator over
Items. - Iter
- Iterator over
Tlvs. - Prefixed
- A string or array with a length prefix of type
P. - Reader
- Reads values from the start of a shared buffer.
- Tlv
- One TLV.
- TlvWriter
- Writes the TLVs of a request.
- Tlvs
- The TLVs of a message.
- Utf8
Bytes - UTF-8 text in a shared buffer.
EnumsΒ§
- Decode
Error - Error from decoding a message.
- Value
Error - Error from decoding a value.
TraitsΒ§
- BufMut
- A trait for values that provide sequential write access to bytes.
- Decode
- A value that can be read from a TLV.
- Encode
- A value that can be written into a TLV.
- Indication
- A message that a service sends as an indication.
- Length
Prefix - Integer type of a length prefix.
- Request
- A request to a QMI service.
- Sequence
- A value that a length prefix counts.