Grizzly.ZWave.ParamSpec (grizzly v9.1.0)

Copy Markdown View Source

Data structure describing a parameter for a Z-Wave command.

Summary

Functions

Decodes an Elixir term from a bitstring according to the parameter specification.

Encodes an Elixir term into a bitstring according to the parameter specification.

Whether the parameter should be included in the resulting list of parameters when decoding a command.

Returns the size of the parameter in bits (or :variable) for variable-length params.

Takes the number of bits specified by the param spec from the front of the bitstring.

Validate a command spec.

Validate a command spec, raising on error.

Types

t()

@type t() :: %Grizzly.ZWave.ParamSpec{
  default: any(),
  name: atom(),
  opts: keyword(),
  required: boolean(),
  size: non_neg_integer() | :variable,
  type: type()
}

type()

@type type() ::
  :int
  | :uint
  | :boolean
  | :binary
  | :enum
  | :constant
  | :reserved
  | :bitmask
  | {:length, atom()}
  | :any

Functions

decode_value(param_spec, bitstring, other_params \\ [])

Decodes an Elixir term from a bitstring according to the parameter specification.

encode_value(param_spec, value, other_params \\ [])

@spec encode_value(t(), term(), keyword()) :: bitstring()

Encodes an Elixir term into a bitstring according to the parameter specification.

include_when_decoding?(param_spec)

@spec include_when_decoding?(t()) :: boolean()

Whether the parameter should be included in the resulting list of parameters when decoding a command.

num_bits(param_spec)

@spec num_bits(t()) :: non_neg_integer() | :variable

Returns the size of the parameter in bits (or :variable) for variable-length params.

Examples

iex> spec = %Grizzly.ZWave.ParamSpec{type: :uint, size: 16}
iex> Grizzly.ZWave.ParamSpec.num_bits(spec)
16

iex> spec = %Grizzly.ZWave.ParamSpec{type: :binary, size: 32}
iex> Grizzly.ZWave.ParamSpec.num_bits(spec)
32

iex> spec = %Grizzly.ZWave.ParamSpec{type: :int, size: :variable}
iex> Grizzly.ZWave.ParamSpec.num_bits(spec)
:variable

iex> spec = %Grizzly.ZWave.ParamSpec{type: :int, size: {:variable, :another_param}}
iex> Grizzly.ZWave.ParamSpec.num_bits(spec)
:variable

take_bits(param_spec, bitstring, other_params)

@spec take_bits(t(), bitstring(), keyword()) ::
  {:ok,
   {bits_taken :: non_neg_integer(), value :: bitstring(), rest :: bitstring()}}
  | {:error, Grizzly.ZWave.DecodeError.t()}

Takes the number of bits specified by the param spec from the front of the bitstring.

Returns {:ok, {value, rest}} where value is the taken bits and rest is the remaining bits. If there are not enough bits to take, an error is returned.

Examples

iex> spec = %Grizzly.ZWave.ParamSpec{type: :uint, size: 8}
iex> Grizzly.ZWave.ParamSpec.take_bits(spec, <<0x01, 0x02, 0x03>>, [])
{:ok, {8, <<0x01>>, <<0x02, 0x03>>}}

iex> spec = %Grizzly.ZWave.ParamSpec{type: :int, size: 16}
iex> Grizzly.ZWave.ParamSpec.take_bits(spec, <<0x01, 0x02, 0x03>>, [])
{:ok, {16, <<0x01, 0x02>>, <<0x03>>}}

iex> spec = %Grizzly.ZWave.ParamSpec{type: :int, size: :variable}
iex> Grizzly.ZWave.ParamSpec.take_bits(spec, <<0x01, 0x02, 0x03>>, [])
{:ok, {24, <<0x01, 0x02, 0x03>>, <<>>}}

iex> spec = %Grizzly.ZWave.ParamSpec{type: :int, size: {:variable, :length_param}}
iex> Grizzly.ZWave.ParamSpec.take_bits(spec, <<0x01, 0x02, 0x03>>, length_param: 2)
{:ok, {16, <<0x01, 0x02>>, <<0x03>>}}

validate(spec)

Validate a command spec.

validate!(spec)

Validate a command spec, raising on error.