Skip to main content

Module codec

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 Encode and Decode for 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.
ItemsIter
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.
Utf8Bytes
UTF-8 text in a shared buffer.

EnumsΒ§

DecodeError
Error from decoding a message.
ValueError
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.
LengthPrefix
Integer type of a length prefix.
Request
A request to a QMI service.
Sequence
A value that a length prefix counts.