This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 279
/
activation.go
254 lines (210 loc) · 5.69 KB
/
activation.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package types
import (
"encoding/hex"
"errors"
"strings"
)
// Activation messages are used to notify application of a device activation
type Activation struct {
AppID string `json:"app_id,omitempty"`
DevID string `json:"dev_id,omitempty"`
AppEUI AppEUI `json:"app_eui,omitempty"`
DevEUI DevEUI `json:"dev_eui,omitempty"`
DevAddr DevAddr `json:"dev_addr,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
}
// DevNonce for LoRaWAN
type DevNonce [2]byte
// Bytes returns the DevNonce as a byte slice
func (n DevNonce) Bytes() []byte {
return n[:]
}
func (n DevNonce) String() string {
if n == [2]byte{0, 0} {
return ""
}
return strings.ToUpper(hex.EncodeToString(n.Bytes()))
}
// GoString implements the GoStringer interface.
func (n DevNonce) GoString() string {
return n.String()
}
// MarshalText implements the TextMarshaler interface.
func (n DevNonce) MarshalText() ([]byte, error) {
return []byte(n.String()), nil
}
// UnmarshalText implements the TextUnmarshaler interface.
func (n *DevNonce) UnmarshalText(data []byte) error {
parsed, err := ParseHEX(string(data), 2)
if err != nil {
return err
}
copy(n[:], parsed[:])
return nil
}
// MarshalBinary implements the BinaryMarshaler interface.
func (n DevNonce) MarshalBinary() ([]byte, error) {
return n.Bytes(), nil
}
// UnmarshalBinary implements the BinaryUnmarshaler interface.
func (n *DevNonce) UnmarshalBinary(data []byte) error {
if len(data) != 2 {
return errors.New("ttn/core: Invalid length for DevNonce")
}
copy(n[:], data)
return nil
}
// MarshalTo is used by Protobuf
func (n *DevNonce) MarshalTo(b []byte) (int, error) {
copy(b, n.Bytes())
return 2, nil
}
// Size is used by Protobuf
func (n *DevNonce) Size() int {
return 2
}
// Marshal implements the Marshaler interface.
func (n DevNonce) Marshal() ([]byte, error) {
return n.MarshalBinary()
}
// Unmarshal implements the Unmarshaler interface.
func (n *DevNonce) Unmarshal(data []byte) error {
*n = [2]byte{} // Reset the receiver
return n.UnmarshalBinary(data)
}
// Equal returns whether n is equal to other
func (n DevNonce) Equal(other DevNonce) bool {
return n == other
}
// AppNonce for LoRaWAN
type AppNonce [3]byte
// Bytes returns the AppNonce as a byte slice
func (n AppNonce) Bytes() []byte {
return n[:]
}
func (n AppNonce) String() string {
if n == [3]byte{0, 0, 0} {
return ""
}
return strings.ToUpper(hex.EncodeToString(n.Bytes()))
}
// GoString implements the GoStringer interface.
func (n AppNonce) GoString() string {
return n.String()
}
// MarshalText implements the TextMarshaler interface.
func (n AppNonce) MarshalText() ([]byte, error) {
return []byte(n.String()), nil
}
// UnmarshalText implements the TextUnmarshaler interface.
func (n *AppNonce) UnmarshalText(data []byte) error {
parsed, err := ParseHEX(string(data), 3)
if err != nil {
return err
}
copy(n[:], parsed[:])
return nil
}
// MarshalBinary implements the BinaryMarshaler interface.
func (n AppNonce) MarshalBinary() ([]byte, error) {
return n.Bytes(), nil
}
// UnmarshalBinary implements the BinaryUnmarshaler interface.
func (n *AppNonce) UnmarshalBinary(data []byte) error {
if len(data) != 3 {
return errors.New("ttn/core: Invalid length for AppNonce")
}
copy(n[:], data)
return nil
}
// MarshalTo is used by Protobuf
func (n *AppNonce) MarshalTo(b []byte) (int, error) {
copy(b, n.Bytes())
return 3, nil
}
// Size is used by Protobuf
func (n *AppNonce) Size() int {
return 3
}
// Marshal implements the Marshaler interface.
func (n AppNonce) Marshal() ([]byte, error) {
return n.MarshalBinary()
}
// Unmarshal implements the Unmarshaler interface.
func (n *AppNonce) Unmarshal(data []byte) error {
*n = [3]byte{} // Reset the receiver
return n.UnmarshalBinary(data)
}
// Equal returns whether n is equal to other
func (n AppNonce) Equal(other AppNonce) bool {
return n == other
}
// NetID for LoRaWAN
type NetID [3]byte
var emptyNetID NetID
// Bytes returns the NetID as a byte slice
func (n NetID) Bytes() []byte {
return n[:]
}
func (n NetID) String() string {
if n == [3]byte{0, 0, 0} {
return ""
}
return strings.ToUpper(hex.EncodeToString(n.Bytes()))
}
func (n NetID) IsEmpty() bool {
return n == emptyNetID
}
// GoString implements the GoStringer interface.
func (n NetID) GoString() string {
return n.String()
}
// MarshalText implements the TextMarshaler interface.
func (n NetID) MarshalText() ([]byte, error) {
return []byte(n.String()), nil
}
// UnmarshalText implements the TextUnmarshaler interface.
func (n *NetID) UnmarshalText(data []byte) error {
parsed, err := ParseHEX(string(data), 3)
if err != nil {
return err
}
copy(n[:], parsed[:])
return nil
}
// MarshalBinary implements the BinaryMarshaler interface.
func (n NetID) MarshalBinary() ([]byte, error) {
return n.Bytes(), nil
}
// UnmarshalBinary implements the BinaryUnmarshaler interface.
func (n *NetID) UnmarshalBinary(data []byte) error {
if len(data) != 3 {
return errors.New("ttn/core: Invalid length for NetID")
}
copy(n[:], data)
return nil
}
// MarshalTo is used by Protobuf
func (n *NetID) MarshalTo(b []byte) (int, error) {
copy(b, n.Bytes())
return 3, nil
}
// Size is used by Protobuf
func (n *NetID) Size() int {
return 3
}
// Marshal implements the Marshaler interface.
func (n NetID) Marshal() ([]byte, error) {
return n.MarshalBinary()
}
// Unmarshal implements the Unmarshaler interface.
func (n *NetID) Unmarshal(data []byte) error {
*n = [3]byte{} // Reset the receiver
return n.UnmarshalBinary(data)
}
// Equal returns whether n is equal to other
func (n NetID) Equal(other NetID) bool {
return n == other
}