Bluez.Rebus.Decoder (bluez v0.1.0)

Copy Markdown View Source

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

decoding_state()

@type decoding_state() :: %{
  endianness: endianness(),
  position: non_neg_integer(),
  data: binary()
}

endianness()

@type endianness() :: :little | :big

Functions

decode(signature, data, endianness \\ :little)

@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 format
  • endianness - Byte order for decoding (:little or :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]]

decode_at_position(signature, data, endianness, starting_position)

@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.