-
Notifications
You must be signed in to change notification settings - Fork 9
/
mailbox_queue.ex
79 lines (66 loc) · 2.36 KB
/
mailbox_queue.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
defmodule Grizzly.ZWave.Commands.MailboxQueue do
@moduledoc """
The Mailbox Queue Command is a container for various operations between a
mailbox proxy and a Mailbox Service.
Params:
* `last` - Indicates if this is the last command in the queue. Only applies when
the operation is `:pop`.
* `operation` - The operation to perform.
* `handle` - Used along with the message's source IP address to identify the
queue the message belongs to.
* `entry` - The message to be operated upon.
"""
@behaviour Grizzly.ZWave.Command
import Grizzly.ZWave.Encoding
alias Grizzly.ZWave.Command
alias Grizzly.ZWave.CommandClasses.Mailbox
@type operation :: :push | :pop | :waiting | :ping | :ack | :nack | :queue_full
@type param ::
{:last, boolean()} | {:operation, operation()} | {:handle, byte()} | {:entry, binary()}
@impl Grizzly.ZWave.Command
@spec new([param()]) :: {:ok, Command.t()}
def new(params) do
command = %Command{
name: :mailbox_queue,
command_byte: 0x04,
command_class: Mailbox,
params: params,
impl: __MODULE__
}
{:ok, command}
end
@impl Grizzly.ZWave.Command
@spec encode_params(Command.t()) :: binary()
def encode_params(command) do
last = Command.param(command, :last, false)
operation = Command.param!(command, :operation)
handle = Command.param!(command, :handle)
entry = Command.param!(command, :entry)
<<0::4, bool_to_bit(last)::1, encode_operation(operation)::3, handle::8>> <> entry
end
@impl Grizzly.ZWave.Command
@spec decode_params(binary()) :: {:ok, [param()]}
def decode_params(<<_reserved::4, last::1, operation::3, handle::8, entry::binary>>) do
{:ok,
[
last: bit_to_bool(last),
operation: decode_operation(operation),
handle: handle,
entry: entry
]}
end
defp encode_operation(:push), do: 0
defp encode_operation(:pop), do: 1
defp encode_operation(:waiting), do: 2
defp encode_operation(:ping), do: 3
defp encode_operation(:ack), do: 4
defp encode_operation(:nack), do: 5
defp encode_operation(:queue_full), do: 6
defp decode_operation(0), do: :push
defp decode_operation(1), do: :pop
defp decode_operation(2), do: :waiting
defp decode_operation(3), do: :ping
defp decode_operation(4), do: :ack
defp decode_operation(5), do: :nack
defp decode_operation(6), do: :queue_full
end