View Source Grizzly.ZWave.Encoding (grizzly v6.8.3)
Utility functions for encoding/decoding common data types.
Summary
Functions
Converts a bit into a boolean.
Converts a boolean into a bit.
Decodes an indexed bitmask.
Decodes a 128-bit binary into an IPv6 address tuple.
Encodes an indexed bitmask.
Encodes an IPv6 address tuple into a 128-bit binary.
Types
@type bit() :: 0 | 1
@type bitmask_index_to_value_fun() :: bitmask_index_to_value_fun(term())
@type bitmask_index_to_value_fun(v) :: (index :: non_neg_integer() -> v | nil)
@type bitmask_value_to_index_fun() :: bitmask_value_to_index_fun(term())
@type bitmask_value_to_index_fun(v) :: (v -> non_neg_integer())
Functions
@spec bit_to_bool(0 | 1) :: boolean()
Converts a bit into a boolean.
Examples
iex> bit_to_bool(1)
true
iex> bit_to_bool(0)
false
@spec bool_to_bit(boolean()) :: 0 | 1
Converts a boolean into a bit.
Examples
iex> bool_to_bit(true)
1
iex> bool_to_bit(false)
0
Link to this function
decode_indexed_bitmask(bitmask, value_fun \\ &Function.identity/1)
View Source@spec decode_indexed_bitmask(binary(), bitmask_index_to_value_fun(value_type)) :: [ {value_type, boolean()} ] when value_type: var
Decodes an indexed bitmask.
Examples
iex> decode_indexed_bitmask(<<>>)
[]
iex> decode_indexed_bitmask(<<0b10110001::8>>)
[{0, true}, {1, false}, {2, false}, {3, false}, {4, true}, {5, true}, {6, false}, {7, true}]
@spec decode_ipv6_address(binary()) :: :inet.ip6_address()
Decodes a 128-bit binary into an IPv6 address tuple.
Examples
iex> decode_ipv6_address(<<0xfd00::16, 0xaaaa::16, 0::16, 0::16, 0::16, 0::16, 0::16, 2::16>>)
{0xfd00, 0xaaaa, 0, 0, 0, 0, 0, 2}
Link to this function
encode_indexed_bitmask(values, index_fun \\ &Function.identity/1, opts \\ [])
View SourceEncodes an indexed bitmask.
Examples
iex> encode_indexed_bitmask([])
<<>>
iex> encode_indexed_bitmask(
...> [{0, true}, {4, true}, {5, true}, {6, false}, {7, true}, {8, true}]
...> )
<<0b10110001::8, 0b00000001::8>>
iex> encode_indexed_bitmask(
...> [{0, true}, {4, true}, {5, true}, {6, false}, {7, true}, {8, true}, {31, true}]
...> )
<<0b10110001, 0b00000001, 0b00000000, 0b10000000>>
@spec encode_ipv6_address(:inet.ip6_address()) :: binary()
Encodes an IPv6 address tuple into a 128-bit binary.
Examples
iex> encode_ipv6_address({0xfd00, 0xaaaa, 0, 0, 0, 0, 0, 2})
<<0xfd00::16, 0xaaaa::16, 0::16, 0::16, 0::16, 0::16, 0::16, 2::16>>