diff options
Diffstat (limited to 'inform/inform.go')
-rw-r--r-- | inform/inform.go | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/inform/inform.go b/inform/inform.go index 5c3ca1c..ac3b57d 100644 --- a/inform/inform.go +++ b/inform/inform.go | |||
@@ -27,12 +27,26 @@ type InformWrapper struct { | |||
27 | 27 | ||
28 | // Create InformWrapper with sane defaults | 28 | // Create InformWrapper with sane defaults |
29 | func NewInformWrapper() *InformWrapper { | 29 | func NewInformWrapper() *InformWrapper { |
30 | return &InformWrapper{ | 30 | w := &InformWrapper{ |
31 | Version: INFORM_VERSION, | 31 | Version: INFORM_VERSION, |
32 | MacAddr: make([]byte, 6), | 32 | MacAddr: make([]byte, 6), |
33 | Flags: 0, | 33 | Flags: 0, |
34 | DataVersion: DATA_VERSION, | 34 | DataVersion: DATA_VERSION, |
35 | } | 35 | } |
36 | |||
37 | // Almost all messages are encrypted outside of provisioning so default | ||
38 | // this and make users explicitly disable it. | ||
39 | w.SetEncrypted(true) | ||
40 | |||
41 | return w | ||
42 | } | ||
43 | |||
44 | // Create an InformWrapper that is a response to an incoming wrapper. Copies | ||
45 | // all necessary data for a response so callers can just set a payload | ||
46 | func NewInformWrapperResponse(msg *InformWrapper) *InformWrapper { | ||
47 | w := NewInformWrapper() | ||
48 | copy(w.MacAddr, msg.MacAddr) | ||
49 | return w | ||
36 | } | 50 | } |
37 | 51 | ||
38 | // Update the payload data with JSON value | 52 | // Update the payload data with JSON value |
@@ -45,6 +59,19 @@ func (i *InformWrapper) UpdatePayload(v interface{}) error { | |||
45 | } | 59 | } |
46 | } | 60 | } |
47 | 61 | ||
62 | // Unmarshal a payload body that we received from a device. Does not work for | ||
63 | // user-set messages | ||
64 | func (i *InformWrapper) UnmarshalPayload() (*DeviceMessage, error) { | ||
65 | var m DeviceMessage | ||
66 | |||
67 | err := json.Unmarshal(i.Payload, &m) | ||
68 | if err != nil { | ||
69 | return nil, err | ||
70 | } | ||
71 | |||
72 | return &m, nil | ||
73 | } | ||
74 | |||
48 | // Format Mac address bytes as lowercase string with colons | 75 | // Format Mac address bytes as lowercase string with colons |
49 | func (i *InformWrapper) FormattedMac() string { | 76 | func (i *InformWrapper) FormattedMac() string { |
50 | return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", | 77 | return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", |