-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac_command.go
263 lines (216 loc) · 6.52 KB
/
mac_command.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
255
256
257
258
259
260
261
262
263
package storage
import (
"bytes"
"encoding/gob"
"fmt"
"time"
"github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/brocaar/loraserver/internal/config"
"github.com/brocaar/lorawan"
)
const (
macCommandQueueTempl = "lora:ns:device:%s:mac:queue"
macCommandPendingTempl = "lora:ns:device:%s:mac:pending:%d"
)
// MACCommandBlock defines a block of MAC commands that must be handled
// together.
type MACCommandBlock struct {
CID lorawan.CID
External bool // command was enqueued by an external service
MACCommands MACCommands
}
// Size returns the size (in bytes) of the mac-commands within this block.
func (m *MACCommandBlock) Size() (int, error) {
var count int
for _, mc := range m.MACCommands {
b, err := mc.MarshalBinary()
if err != nil {
return 0, errors.Wrap(err, "marshal binary error")
}
count += len(b)
}
return count, nil
}
// MACCommands holds a slice of MACCommand items.
type MACCommands []lorawan.MACCommand
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (m MACCommands) MarshalBinary() ([]byte, error) {
var out []byte
for _, mac := range m {
b, err := mac.MarshalBinary()
if err != nil {
return nil, err
}
out = append(out, b...)
}
return out, nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (m *MACCommands) UnmarshalBinary(data []byte) error {
var pLen int
for i := 0; i < len(data); i++ {
if _, s, err := lorawan.GetMACPayloadAndSize(false, lorawan.CID(data[i])); err != nil {
pLen = 0
} else {
pLen = s
}
// check if the remaining bytes are >= CID byte + payload size
if len(data[i:]) < pLen+1 {
return errors.New("not enough remaining bytes")
}
var mc lorawan.MACCommand
if err := mc.UnmarshalBinary(false, data[i:i+1+pLen]); err != nil {
return err
}
*m = append(*m, mc)
i += pLen
}
return nil
}
// FlushMACCommandQueue flushes the mac-command queue for the given DevEUI.
func FlushMACCommandQueue(p *redis.Pool, devEUI lorawan.EUI64) error {
c := p.Get()
defer c.Close()
key := fmt.Sprintf(macCommandQueueTempl, devEUI)
_, err := redis.Int(c.Do("DEL", key))
if err != nil {
return errors.Wrap(err, "flush mac-command queue error")
}
return nil
}
// CreateMACCommandQueueItem creates a new mac-command queue item.
func CreateMACCommandQueueItem(p *redis.Pool, devEUI lorawan.EUI64, block MACCommandBlock) error {
var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(block)
if err != nil {
return errors.Wrap(err, "gob encode error")
}
c := p.Get()
defer c.Close()
exp := int64(config.C.NetworkServer.DeviceSessionTTL) / int64(time.Millisecond)
key := fmt.Sprintf(macCommandQueueTempl, devEUI)
c.Send("MULTI")
c.Send("RPUSH", key, buf.Bytes())
c.Send("PEXPIRE", key, exp)
_, err = c.Do("EXEC")
if err != nil {
return errors.Wrap(err, "create mac-command queue item error")
}
log.WithFields(log.Fields{
"dev_eui": devEUI,
"cid": block.CID,
}).Info("mac-command queue item created")
return nil
}
// GetMACCommandQueueItems returns the mac-command queue items for the
// given DevEUI.
func GetMACCommandQueueItems(p *redis.Pool, devEUI lorawan.EUI64) ([]MACCommandBlock, error) {
var out []MACCommandBlock
c := p.Get()
defer c.Close()
key := fmt.Sprintf(macCommandQueueTempl, devEUI)
values, err := redis.Values(c.Do("LRANGE", key, 0, -1))
if err != nil {
return nil, errors.Wrap(err, "get mac-command queue items error")
}
for _, value := range values {
b, ok := value.([]byte)
if !ok {
return nil, fmt.Errorf("expecte []byte type, got %T", value)
}
var block MACCommandBlock
err = gob.NewDecoder(bytes.NewReader(b)).Decode(&block)
if err != nil {
return nil, errors.Wrap(err, "gob decode error")
}
out = append(out, block)
}
return out, nil
}
// DeleteMACCommandQueueItem deletes the given mac-command from the queue.
func DeleteMACCommandQueueItem(p *redis.Pool, devEUI lorawan.EUI64, block MACCommandBlock) error {
var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(block)
if err != nil {
return errors.Wrap(err, "gob encode error")
}
c := p.Get()
defer c.Close()
key := fmt.Sprintf(macCommandQueueTempl, devEUI)
val, err := redis.Int(c.Do("LREM", key, 0, buf.Bytes()))
if err != nil {
return errors.Wrap(err, "delete mac-command queue item error")
}
if val == 0 {
return ErrDoesNotExist
}
log.WithFields(log.Fields{
"dev_eui": devEUI,
"cid": block.CID,
}).Info("mac-command deleted from queue")
return nil
}
// SetPendingMACCommand sets a mac-command to the pending buffer.
// In case an other mac-command with the same CID has been set to pending,
// it will be overwritten.
func SetPendingMACCommand(p *redis.Pool, devEUI lorawan.EUI64, block MACCommandBlock) error {
var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(block)
if err != nil {
return errors.Wrap(err, "gob encode error")
}
c := p.Get()
defer c.Close()
key := fmt.Sprintf(macCommandPendingTempl, devEUI, block.CID)
exp := int64(config.C.NetworkServer.DeviceSessionTTL) / int64(time.Millisecond)
_, err = c.Do("PSETEX", key, exp, buf.Bytes())
if err != nil {
return errors.Wrap(err, "set mac-command pending error")
}
log.WithFields(log.Fields{
"dev_eui": devEUI,
"cid": block.CID,
"commands": len(block.MACCommands),
}).Info("pending mac-command block set")
return nil
}
// GetPendingMACCommand returns the pending mac-command for the given CID.
// In case no items are pending, nil is returned.
func GetPendingMACCommand(p *redis.Pool, devEUI lorawan.EUI64, cid lorawan.CID) (*MACCommandBlock, error) {
var block MACCommandBlock
c := p.Get()
defer c.Close()
key := fmt.Sprintf(macCommandPendingTempl, devEUI, cid)
val, err := redis.Bytes(c.Do("GET", key))
if err != nil {
if err == redis.ErrNil {
return nil, nil
}
return nil, errors.Wrap(err, "get pending mac-command error")
}
err = gob.NewDecoder(bytes.NewReader(val)).Decode(&block)
if err != nil {
return nil, errors.Wrap(err, "gob decode error")
}
return &block, nil
}
// DeletePendingMACCommand removes the pending mac-command for the given CID.
func DeletePendingMACCommand(p *redis.Pool, devEUI lorawan.EUI64, cid lorawan.CID) error {
c := p.Get()
defer c.Close()
key := fmt.Sprintf(macCommandPendingTempl, devEUI, cid)
val, err := redis.Int(c.Do("DEL", key))
if err != nil {
return errors.Wrap(err, "delete pending mac-command error")
}
if val == 0 {
return ErrDoesNotExist
}
log.WithFields(log.Fields{
"dev_eui": devEUI,
"cid": cid,
}).Info("pending mac-command deleted")
return nil
}