-
-
Notifications
You must be signed in to change notification settings - Fork 325
/
uplink.go
376 lines (314 loc) · 9.69 KB
/
uplink.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package uplink
import (
"context"
"crypto/aes"
"encoding/hex"
"fmt"
"time"
keywrap "github.com/NickBall/go-aes-key-wrap"
"github.com/golang/protobuf/ptypes"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/brocaar/chirpstack-api/go/v3/as"
pb "github.com/brocaar/chirpstack-api/go/v3/as/integration"
"github.com/brocaar/chirpstack-api/go/v3/common"
"github.com/brocaar/chirpstack-application-server/internal/codec"
"github.com/brocaar/chirpstack-application-server/internal/config"
"github.com/brocaar/chirpstack-application-server/internal/integration"
"github.com/brocaar/chirpstack-application-server/internal/logging"
"github.com/brocaar/chirpstack-application-server/internal/storage"
"github.com/brocaar/lorawan"
)
type uplinkContext struct {
uplinkDataReq as.HandleUplinkDataRequest
ctx context.Context
device storage.Device
application storage.Application
deviceProfile storage.DeviceProfile
data []byte
objectJSON string
}
var tasks = []func(*uplinkContext) error{
getDevice,
getApplication,
getDeviceProfile,
updateDeviceLastSeenAndDR,
updateDeviceActivation,
decryptPayload,
handleCodec,
handleIntegrations,
saveMetrics,
}
// Handle handles the uplink event.
func Handle(ctx context.Context, req as.HandleUplinkDataRequest) error {
uc := uplinkContext{
ctx: ctx,
uplinkDataReq: req,
}
for _, f := range tasks {
if err := f(&uc); err != nil {
if err == ErrAbort {
return nil
}
return err
}
}
return nil
}
func getDevice(ctx *uplinkContext) error {
var err error
var devEUI lorawan.EUI64
copy(devEUI[:], ctx.uplinkDataReq.DevEui)
ctx.device, err = storage.GetDevice(ctx.ctx, storage.DB(), devEUI, false, true)
if err != nil {
return errors.Wrap(err, "get device error")
}
return nil
}
func getDeviceProfile(ctx *uplinkContext) error {
var err error
ctx.deviceProfile, err = storage.GetDeviceProfile(ctx.ctx, storage.DB(), ctx.device.DeviceProfileID, false, true)
if err != nil {
return errors.Wrap(err, "get device-profile error")
}
return nil
}
func getApplication(ctx *uplinkContext) error {
var err error
ctx.application, err = storage.GetApplication(ctx.ctx, storage.DB(), ctx.device.ApplicationID)
if err != nil {
return errors.Wrap(err, "get application error")
}
return nil
}
func updateDeviceLastSeenAndDR(ctx *uplinkContext) error {
if err := storage.UpdateDeviceLastSeenAndDR(ctx.ctx, storage.DB(), ctx.device.DevEUI, time.Now(), int(ctx.uplinkDataReq.Dr)); err != nil {
return errors.Wrap(err, "update device last-seen and dr error")
}
return nil
}
func updateDeviceActivation(ctx *uplinkContext) error {
da := ctx.uplinkDataReq.DeviceActivationContext
// nothing to do when there is no new device activation context
if da == nil {
return nil
}
// key envelope must not be nil
if da.AppSKey == nil {
return errors.New("AppSKey must not be nil")
}
appSKey, err := unwrapASKey(da.AppSKey)
if err != nil {
return errors.Wrap(err, "unwrap AppSKey error")
}
var devAddr lorawan.DevAddr
copy(devAddr[:], da.DevAddr)
// if DevAddr and AppSKey are equal, there is nothing to do
if ctx.device.DevAddr == devAddr && ctx.device.AppSKey == appSKey {
return nil
}
ctx.device.DevAddr = devAddr
ctx.device.AppSKey = appSKey
if err := storage.UpdateDeviceActivation(ctx.ctx, storage.DB(), ctx.device.DevEUI, ctx.device.DevAddr, ctx.device.AppSKey); err != nil {
return errors.Wrap(err, "update device activation error")
}
pl := pb.JoinEvent{
ApplicationId: uint64(ctx.device.ApplicationID),
ApplicationName: ctx.application.Name,
DevEui: ctx.device.DevEUI[:],
DeviceName: ctx.device.Name,
DevAddr: ctx.device.DevAddr[:],
RxInfo: ctx.uplinkDataReq.RxInfo,
TxInfo: ctx.uplinkDataReq.TxInfo,
Dr: ctx.uplinkDataReq.Dr,
Tags: make(map[string]string),
PublishedAt: ptypes.TimestampNow(),
}
// set tags
for k, v := range ctx.device.Tags.Map {
if v.Valid {
pl.Tags[k] = v.String
}
}
vars := make(map[string]string)
for k, v := range ctx.device.Variables.Map {
if v.Valid {
vars[k] = v.String
}
}
err = integration.ForApplicationID(ctx.device.ApplicationID).HandleJoinEvent(ctx.ctx, vars, pl)
if err != nil {
return errors.Wrap(err, "send join notification error")
}
return nil
}
func decryptPayload(ctx *uplinkContext) error {
var err error
ctx.data, err = lorawan.EncryptFRMPayload(ctx.device.AppSKey, true, ctx.device.DevAddr, ctx.uplinkDataReq.FCnt, ctx.uplinkDataReq.Data)
if err != nil {
return errors.Wrap(err, "decrypt payload error")
}
return nil
}
func handleCodec(ctx *uplinkContext) error {
codecType := ctx.application.PayloadCodec
decoderScript := ctx.application.PayloadDecoderScript
if ctx.deviceProfile.PayloadCodec != "" {
codecType = ctx.deviceProfile.PayloadCodec
decoderScript = ctx.deviceProfile.PayloadDecoderScript
}
if codecType == codec.None {
return nil
}
start := time.Now()
b, err := codec.BinaryToJSON(codecType, uint8(ctx.uplinkDataReq.FPort), ctx.device.Variables, decoderScript, ctx.data)
if err != nil {
log.WithFields(log.Fields{
"codec": codecType,
"application_id": ctx.device.ApplicationID,
"f_port": ctx.uplinkDataReq.FPort,
"f_cnt": ctx.uplinkDataReq.FCnt,
"dev_eui": ctx.device.DevEUI,
}).WithError(err).Error("decode payload error")
errEvent := pb.ErrorEvent{
ApplicationId: uint64(ctx.device.ApplicationID),
ApplicationName: ctx.application.Name,
DeviceName: ctx.device.Name,
DevEui: ctx.device.DevEUI[:],
Type: pb.ErrorType_UPLINK_CODEC,
Error: err.Error(),
FCnt: ctx.uplinkDataReq.FCnt,
Tags: make(map[string]string),
PublishedAt: ptypes.TimestampNow(),
}
for k, v := range ctx.device.Tags.Map {
if v.Valid {
errEvent.Tags[k] = v.String
}
}
vars := make(map[string]string)
for k, v := range ctx.device.Variables.Map {
if v.Valid {
vars[k] = v.String
}
}
if err := integration.ForApplicationID(ctx.device.ApplicationID).HandleErrorEvent(ctx.ctx, vars, errEvent); err != nil {
log.WithError(err).Error("send error event to integration error")
}
}
log.WithFields(log.Fields{
"application_id": ctx.application.ID,
"codec": codecType,
"duration": time.Since(start),
}).Debug("payload codec completed Decode execution")
ctx.objectJSON = string(b)
return nil
}
func handleIntegrations(ctx *uplinkContext) error {
pl := pb.UplinkEvent{
ApplicationId: uint64(ctx.device.ApplicationID),
ApplicationName: ctx.application.Name,
DeviceProfileId: ctx.device.DeviceProfileID.String(),
DeviceProfileName: ctx.deviceProfile.Name,
DeviceName: ctx.device.Name,
DevEui: ctx.device.DevEUI[:],
RxInfo: ctx.uplinkDataReq.RxInfo,
TxInfo: ctx.uplinkDataReq.TxInfo,
Dr: ctx.uplinkDataReq.Dr,
Adr: ctx.uplinkDataReq.Adr,
FCnt: ctx.uplinkDataReq.FCnt,
FPort: ctx.uplinkDataReq.FPort,
Data: ctx.data,
ObjectJson: ctx.objectJSON,
Tags: make(map[string]string),
ConfirmedUplink: ctx.uplinkDataReq.ConfirmedUplink,
DevAddr: ctx.device.DevAddr[:],
PublishedAt: ptypes.TimestampNow(),
}
//set device profile tags
for k, v := range ctx.deviceProfile.Tags.Map {
if v.Valid {
pl.Tags[k] = v.String
}
}
// set tags
for k, v := range ctx.device.Tags.Map {
if v.Valid {
pl.Tags[k] = v.String
}
}
vars := make(map[string]string)
for k, v := range ctx.device.Variables.Map {
if v.Valid {
vars[k] = v.String
}
}
bgCtx := context.Background()
bgCtx = context.WithValue(bgCtx, logging.ContextIDKey, ctx.ctx.Value(logging.ContextIDKey))
// Handle the actual integration handling in a Go-routine so that the
// as.HandleUplinkData api can return.
go func() {
err := integration.ForApplicationID(ctx.device.ApplicationID).HandleUplinkEvent(bgCtx, vars, pl)
if err != nil {
log.WithError(err).Error("send uplink event error")
}
}()
return nil
}
func saveMetrics(ctx *uplinkContext) error {
var maxRSSI int32
var maxSNR float64
for i, rxInfo := range ctx.uplinkDataReq.GetRxInfo() {
if i == 0 {
maxRSSI = rxInfo.Rssi
maxSNR = rxInfo.LoraSnr
}
if rxInfo.Rssi > maxRSSI {
maxRSSI = rxInfo.Rssi
}
if rxInfo.LoraSnr > maxSNR {
maxSNR = rxInfo.LoraSnr
}
}
// note that the RSS and SNR needs to be divided by the rx_count
metrics := storage.MetricsRecord{
Time: time.Now(),
Metrics: map[string]float64{
"rx_count": 1.0,
"gw_rssi_sum": float64(maxRSSI),
"gw_snr_sum": maxSNR,
fmt.Sprintf("rx_freq_%d", ctx.uplinkDataReq.GetTxInfo().Frequency): 1.0,
fmt.Sprintf("rx_dr_%d", ctx.uplinkDataReq.Dr): 1.0,
},
}
if err := storage.SaveMetrics(ctx.ctx, fmt.Sprintf("device:%s", ctx.device.DevEUI), metrics); err != nil {
return errors.Wrap(err, "save metrics error")
}
return nil
}
func unwrapASKey(ke *common.KeyEnvelope) (lorawan.AES128Key, error) {
var key lorawan.AES128Key
if ke.KekLabel == "" {
copy(key[:], ke.AesKey)
return key, nil
}
for i := range config.C.JoinServer.KEK.Set {
if config.C.JoinServer.KEK.Set[i].Label == ke.KekLabel {
kek, err := hex.DecodeString(config.C.JoinServer.KEK.Set[i].KEK)
if err != nil {
return key, errors.Wrap(err, "decode kek error")
}
block, err := aes.NewCipher(kek)
if err != nil {
return key, errors.Wrap(err, "new cipher error")
}
b, err := keywrap.Unwrap(block, ke.AesKey)
if err != nil {
return key, errors.Wrap(err, "key unwrap error")
}
copy(key[:], b)
return key, nil
}
}
return key, fmt.Errorf("unknown kek label: %s", ke.KekLabel)
}