D-Bus message decoder that unmarshals data according to D-Bus wire format.
Implements the D-Bus unmarshaling format with proper alignment and byte ordering. All structs and arrays are represented as Elixir lists for consistency.
Summary
Functions
Decodes binary data based on the provided D-Bus signature.
Decode data with a specific starting position for alignment calculations.
Types
@type decoding_state() :: %{ endianness: endianness(), position: non_neg_integer(), data: binary() }
@type endianness() :: :little | :big
Functions
@spec decode(binary(), binary(), endianness()) :: [any()]
Decodes binary data based on the provided D-Bus signature.
This function takes a D-Bus type signature string and binary data, then unmarshals it from the D-Bus wire format back into Elixir data structures. Both structs and arrays are represented as Elixir lists.
Parameters
signature- A D-Bus type signature string (e.g., "i", "s", "a(is)", etc.)data- Binary data in D-Bus wire formatendianness- Byte order for decoding (:littleor:big). Defaults to:little
Returns
Returns the decoded Elixir data structure. Multiple values are returned as a list.
Examples
# Decode a simple integer
iex> Bluez.Rebus.Decoder.decode("i", <<42, 0, 0, 0>>)
[42]
# Decode a string
iex> Bluez.Rebus.Decoder.decode("s", <<5, 0, 0, 0, "hello", 0>>)
["hello"]
# Decode an array of integers
iex> Bluez.Rebus.Decoder.decode("ai", <<12, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0>>)
[[1, 2, 3]]
# Decode a struct (returned as list)
iex> Bluez.Rebus.Decoder.decode("(si)", <<5, 0, 0, 0, "hello", 0, 0, 0, 42, 0, 0, 0>>)
[["hello", 42]]
@spec decode_at_position(binary(), binary(), endianness(), non_neg_integer()) :: list()
Decode data with a specific starting position for alignment calculations.
This is useful when the data being decoded was encoded at a specific position in a larger message, and alignment must be calculated relative to that position.