This repository has been archived by the owner on Sep 3, 2020. It is now read-only.
forked from brocaar/lorawan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fhdr.go
210 lines (184 loc) · 4.96 KB
/
fhdr.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
package lorawan
import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"log"
)
// DevAddr represents the device address.
type DevAddr [4]byte
// NwkID returns the NwkID bits of the DevAddr.
func (a DevAddr) NwkID() byte {
return a[0] >> 1 // 7 msb
}
// MarshalBinary marshals the object in binary form.
func (a DevAddr) MarshalBinary() ([]byte, error) {
out := make([]byte, len(a))
for i, v := range a {
// little endian
out[len(a)-i-1] = v
}
return out, nil
}
// UnmarshalBinary decodes the object from binary form.
func (a *DevAddr) UnmarshalBinary(data []byte) error {
if len(data) != len(a) {
return fmt.Errorf("lorawan: %d bytes of data are expected", len(a))
}
for i, v := range data {
// little endian
a[len(a)-i-1] = v
}
return nil
}
// MarshalText implements encoding.TextMarshaler.
func (a DevAddr) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (a *DevAddr) UnmarshalText(text []byte) error {
b, err := hex.DecodeString(string(text))
if err != nil {
return err
}
if len(b) != len(a) {
return fmt.Errorf("lorawan: exactly %d bytes are expected", len(a))
}
copy(a[:], b)
return nil
}
// String implements fmt.Stringer.
func (a DevAddr) String() string {
return hex.EncodeToString(a[:])
}
// Scan implements sql.Scanner.
func (a *DevAddr) Scan(src interface{}) error {
b, ok := src.([]byte)
if !ok {
return errors.New("lorawan: []byte type expected")
}
if len(b) != len(a) {
return fmt.Errorf("lorawan []byte must have length %d", len(a))
}
copy(a[:], b)
return nil
}
// FCtrl represents the FCtrl (frame control) field.
type FCtrl struct {
ADR bool `json:"adr"`
ADRACKReq bool `json:"adrAckReq"`
ACK bool `json:"ack"`
FPending bool `json:"fPending"` // only used for downlink messages
fOptsLen uint8 // will be set automatically by the FHDR when serialized to []byte
}
// MarshalBinary marshals the object in binary form.
func (c FCtrl) MarshalBinary() ([]byte, error) {
if c.fOptsLen > 15 {
return []byte{}, errors.New("lorawan: max value of FOptsLen is 15")
}
b := byte(c.fOptsLen)
if c.FPending {
b = b ^ (1 << 4)
}
if c.ACK {
b = b ^ (1 << 5)
}
if c.ADRACKReq {
b = b ^ (1 << 6)
}
if c.ADR {
b = b ^ (1 << 7)
}
return []byte{b}, nil
}
// UnmarshalBinary decodes the object from binary form.
func (c *FCtrl) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return errors.New("lorawan: 1 byte of data is expected")
}
c.fOptsLen = data[0] & ((1 << 3) ^ (1 << 2) ^ (1 << 1) ^ (1 << 0))
c.FPending = data[0]&(1<<4) > 0
c.ACK = data[0]&(1<<5) > 0
c.ADRACKReq = data[0]&(1<<6) > 0
c.ADR = data[0]&(1<<7) > 0
return nil
}
// FHDR represents the frame header.
type FHDR struct {
DevAddr DevAddr `json:"devAddr"`
FCtrl FCtrl `json:"fCtrl"`
FCnt uint32 `json:"fCnt"` // only the least-significant 16 bits will be marshalled
FOpts []MACCommand `json:"fOpts"` // max. number of allowed bytes is 15
}
// MarshalBinary marshals the object in binary form.
func (h FHDR) MarshalBinary() ([]byte, error) {
var b []byte
var err error
var opts []byte
for _, mac := range h.FOpts {
b, err = mac.MarshalBinary()
if err != nil {
return []byte{}, err
}
opts = append(opts, b...)
}
h.FCtrl.fOptsLen = uint8(len(opts))
if h.FCtrl.fOptsLen > 15 {
return []byte{}, errors.New("lorawan: max number of FOpts bytes is 15")
}
out := make([]byte, 0, 7+h.FCtrl.fOptsLen)
b, err = h.DevAddr.MarshalBinary()
if err != nil {
return []byte{}, err
}
out = append(out, b...)
b, err = h.FCtrl.MarshalBinary()
if err != nil {
return []byte{}, err
}
out = append(out, b...)
fCntBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(fCntBytes, h.FCnt)
out = append(out, fCntBytes[0:2]...)
out = append(out, opts...)
return out, nil
}
// UnmarshalBinary decodes the object from binary form.
func (h *FHDR) UnmarshalBinary(uplink bool, data []byte) error {
if len(data) < 7 {
return errors.New("lorawan: at least 7 bytes are expected")
}
if err := h.DevAddr.UnmarshalBinary(data[0:4]); err != nil {
return err
}
if err := h.FCtrl.UnmarshalBinary(data[4:5]); err != nil {
return err
}
fCntBytes := make([]byte, 4)
copy(fCntBytes, data[5:7])
h.FCnt = binary.LittleEndian.Uint32(fCntBytes)
if len(data) > 7 {
var pLen int
for i := 0; i < len(data[7:]); i++ {
if _, s, err := GetMACPayloadAndSize(uplink, CID(data[7+i])); err != nil {
pLen = 0
} else {
pLen = s
}
// check if the remaining bytes are >= CID byte + payload size
if len(data[7+i:]) < pLen+1 {
return errors.New("lorawan: not enough remaining bytes")
}
mc := MACCommand{}
if err := mc.UnmarshalBinary(uplink, data[7+i:7+i+1+pLen]); err != nil {
log.Printf("warning: unmarshal mac-command error (skipping remaining mac-command bytes): %s", err)
break
}
h.FOpts = append(h.FOpts, mc)
// go to the next command (skip the payload bytes of the current command)
i = i + pLen
}
}
return nil
}