-
Notifications
You must be signed in to change notification settings - Fork 26
/
device.go
735 lines (638 loc) · 21.4 KB
/
device.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
package main
import (
"encoding/hex"
"fmt"
"strconv"
"time"
"github.com/brocaar/chirpstack-api/go/gw"
"github.com/brocaar/lorawan"
lwBand "github.com/brocaar/lorawan/band"
"github.com/golang/protobuf/ptypes"
log "github.com/sirupsen/logrus"
"github.com/iegomez/lds/lds"
"gioui.org/layout"
l "gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/scartill/giox"
xmat "github.com/scartill/giox/material"
)
// Marshalers, versions, and message types.
var (
marshalers = []string{"json", "protobuf", "v2_json"}
majorVersions = map[lorawan.Major]string{0: "LoRaWANRev1"}
macVersions = map[lorawan.MACVersion]string{0: "LoRaWAN 1.0", 1: "LoRaWAN 1.1"}
mTypes = map[lorawan.MType]string{lorawan.UnconfirmedDataUp: "UnconfirmedDataUp", lorawan.ConfirmedDataUp: "ConfirmedDataUp"}
)
// lds device related vars.
var (
cDevice *lds.Device
)
type device struct {
DevEUI string `toml:"eui"`
DevAddress string `toml:"address"`
NwkSEncKey string `toml:"network_session_encription_key"`
SNwkSIntKey string `toml:"serving_network_session_integrity_key"` //For Lorawan 1.0 this is the same as the NwkSEncKey
FNwkSIntKey string `toml:"forwarding_network_session_integrity_key"` //For Lorawan 1.0 this is the same as the NwkSEncKey
AppSKey string `toml:"application_session_key"`
Marshaler string `toml:"marshaler"`
NwkKey string `toml:"nwk_key"` //Network key, used to be called application key for Lorawan 1.0
AppKey string `toml:"app_key"` //Application key, for Lorawan 1.1
JoinEUI string `toml:"join_eui"` //JoinEUI for 1.1. (AppEUI on 1.0)
Major lorawan.Major `toml:"-"`
MACVersion lorawan.MACVersion `toml:"mac_version"` //Lorawan MAC version
MType lorawan.MType `toml:"-"`
Profile string `toml:"profile"`
Joined bool `toml:"joined"`
SkipFCntCheck bool `toml:"skip_fcnt_check"`
}
// Widgets
var (
deviceEUIEdit widget.Editor
deviceAddressEdit widget.Editor
nwkSEncKeyEdit widget.Editor
sNwkSIntKeyEdit widget.Editor
fNwkSIntKeyEdit widget.Editor
appSKeyEdit widget.Editor
nwkKeyEdit widget.Editor
appKeyEdit widget.Editor
joinEUIEdit widget.Editor
marshalerCombo giox.Combo
majorVersionCombo giox.Combo
macVersionCombo giox.Combo
mTypeCombo giox.Combo
profileCombo giox.Combo
disableFCWCheckbox widget.Bool
joinButton widget.Clickable
resetButton widget.Clickable
setValuesButton widget.Clickable
ulFcntEdit widget.Editor
dlFcntEdit widget.Editor
devNonceEdit widget.Editor
joinNonceEdit widget.Editor
resetDevice bool
resetCancelButton widget.Clickable
resetConfirmButton widget.Clickable
setRedisValues bool
setRedisValuesCancelButton widget.Clickable
setRedisValuesConfirmButton widget.Clickable
)
func createDeviceForm() {
marshalerItems := make([]string, len(marshalers))
for i, v := range marshalers {
marshalerItems[i] = string(v)
}
marshalerCombo = giox.MakeCombo(marshalerItems, "<select marshaler>")
ki := 0
majorVersionItems := make([]string, len(majorVersions))
for _, v := range majorVersions {
majorVersionItems[ki] = string(v)
ki++
}
majorVersionCombo = giox.MakeCombo(majorVersionItems, "<select major version>")
ki = 0
macVersionItems := make([]string, len(macVersions))
for _, v := range macVersions {
macVersionItems[ki] = string(v)
ki++
}
macVersionCombo = giox.MakeCombo(macVersionItems, "<select MAC version>")
ki = 0
mTypeItems := make([]string, len(mTypes))
for _, v := range mTypes {
mTypeItems[ki] = string(v)
ki++
}
mTypeCombo = giox.MakeCombo(mTypeItems, "<select message type>")
profileCombo = giox.MakeCombo([]string{"OTAA", "ABP"}, "<select profile>")
}
func deviceResetGuiValues() {
deviceEUIEdit.SetText(config.Device.DevEUI)
deviceAddressEdit.SetText(config.Device.DevAddress)
nwkSEncKeyEdit.SetText(config.Device.NwkSEncKey)
sNwkSIntKeyEdit.SetText(config.Device.SNwkSIntKey)
fNwkSIntKeyEdit.SetText(config.Device.FNwkSIntKey)
appSKeyEdit.SetText(config.Device.AppSKey)
nwkKeyEdit.SetText(config.Device.NwkKey)
appKeyEdit.SetText(config.Device.AppKey)
joinEUIEdit.SetText(config.Device.JoinEUI)
marshalerCombo.SelectItem(string(config.Device.Marshaler))
majorVersionCombo.SelectItem(majorVersions[config.Device.Major])
macVersionCombo.SelectItem(macVersions[config.Device.MACVersion])
mTypeCombo.SelectItem(mTypes[config.Device.MType])
profileCombo.SelectItem(config.Device.Profile)
disableFCWCheckbox.Value = config.Device.SkipFCntCheck
}
func deviceForm(th *material.Theme) l.FlexChild {
config.Device.DevEUI = deviceEUIEdit.Text()
config.Device.DevAddress = deviceAddressEdit.Text()
config.Device.NwkSEncKey = nwkSEncKeyEdit.Text()
config.Device.SNwkSIntKey = sNwkSIntKeyEdit.Text()
config.Device.FNwkSIntKey = fNwkSIntKeyEdit.Text()
config.Device.AppSKey = appSKeyEdit.Text()
config.Device.NwkKey = nwkKeyEdit.Text()
config.Device.AppKey = appKeyEdit.Text()
config.Device.JoinEUI = joinEUIEdit.Text()
config.Device.Marshaler = marshalerCombo.SelectedText()
config.Device.Major = 0
if majorVersionCombo.HasSelected() {
for k, v := range majorVersions {
if majorVersionCombo.SelectedText() == string(v) {
config.Device.Major = k
}
}
}
config.Device.MACVersion = 0
if macVersionCombo.HasSelected() {
for k, v := range macVersions {
if macVersionCombo.SelectedText() == string(v) {
config.Device.MACVersion = k
}
}
}
config.Device.MType = lorawan.UnconfirmedDataUp
if mTypeCombo.HasSelected() {
for k, v := range mTypes {
if mTypeCombo.SelectedText() == string(v) {
config.Device.MType = k
}
}
}
config.Device.Profile = "OTAA"
if profileCombo.HasSelected() {
config.Device.Profile = profileCombo.SelectedText()
}
config.Device.SkipFCntCheck = disableFCWCheckbox.Value
for joinButton.Clicked() {
join()
}
for resetButton.Clicked() {
resetDevice = true
}
for setValuesButton.Clicked() {
setRedisValues = true
}
if resetDevice {
if ok, subform := resetDeviceSubform(th); ok {
return subform
}
}
if setRedisValues {
if ok, subform := setRedisValuesSubform(th); ok {
return subform
}
}
leftWidgets := []layout.FlexChild{
xmat.RigidSection(th, "Device"),
xmat.RigidEditor(th, "DevEUI", "<device EUI>", &deviceEUIEdit),
xmat.RigidEditor(th, "DevAddress", "<device address>", &deviceAddressEdit),
xmat.RigidEditor(th, "NwkSEncKey", "<network session key>", &nwkSEncKeyEdit),
xmat.RigidEditor(th, "SNwkSIntKey", "<network session key>", &sNwkSIntKeyEdit),
xmat.RigidEditor(th, "FNwkSIntKey", "<forward session key>", &fNwkSIntKeyEdit),
xmat.RigidEditor(th, "AppSKey", "<application session key>", &appSKeyEdit),
xmat.RigidEditor(th, "NwkKey", "<network key>", &nwkKeyEdit),
xmat.RigidEditor(th, "AppKey", "<application key>", &appKeyEdit),
xmat.RigidEditor(th, "JoinEUI", "<join EUI>", &joinEUIEdit),
}
comboOpen := marshalerCombo.IsExpanded() ||
majorVersionCombo.IsExpanded() ||
macVersionCombo.IsExpanded() ||
mTypeCombo.IsExpanded() ||
profileCombo.IsExpanded()
rightWidgets := []l.FlexChild{
xmat.RigidSection(th, ""), // Placeholder
}
if !comboOpen || marshalerCombo.IsExpanded() {
rightWidgets = append(rightWidgets, labelCombo(th, "Marshaler", &marshalerCombo))
}
if !comboOpen || majorVersionCombo.IsExpanded() {
rightWidgets = append(rightWidgets, labelCombo(th, "LoRaWAN Major", &majorVersionCombo))
}
if !comboOpen || macVersionCombo.IsExpanded() {
rightWidgets = append(rightWidgets, labelCombo(th, "MAC Version", &macVersionCombo))
}
if !comboOpen || mTypeCombo.IsExpanded() {
rightWidgets = append(rightWidgets, labelCombo(th, "MType", &mTypeCombo))
}
if !comboOpen || profileCombo.IsExpanded() {
rightWidgets = append(rightWidgets, labelCombo(th, "Profile", &profileCombo))
}
if !comboOpen {
rightWidgets = append(rightWidgets,
xmat.RigidCheckBox(th, "Disable frame counter validation", &disableFCWCheckbox),
)
buttons := []l.FlexChild{
xmat.RigidButton(th, "Join", &joinButton),
}
if cDevice != nil {
buttons = append(buttons, []l.FlexChild{
xmat.RigidButton(th, "Reset device", &resetButton),
xmat.RigidButton(th, "Set values", &setValuesButton),
}...)
}
rightWidgets = append(rightWidgets, l.Rigid(func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Horizontal}.Layout(gtx, buttons...)
}))
if cDevice != nil {
rightWidgets = append(rightWidgets, []l.FlexChild{
xmat.RigidLabel(th, fmt.Sprintf("DlFCnt: %d - DevNonce: %d", cDevice.DlFcnt, cDevice.DevNonce)),
xmat.RigidLabel(th, fmt.Sprintf("UlFCnt: %d - JoinNonce: %d", cDevice.UlFcnt, cDevice.JoinNonce)),
xmat.RigidLabel(th, fmt.Sprintf("Joined: %t", cDevice.Joined)),
}...)
}
}
inset := l.Inset{Left: unit.Dp(30)}
return l.Rigid(func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Horizontal}.Layout(gtx,
l.Rigid(func(gtx l.Context) l.Dimensions {
return inset.Layout(gtx, func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Vertical}.Layout(gtx, leftWidgets...)
})
}),
l.Rigid(func(gtx l.Context) l.Dimensions {
return inset.Layout(gtx, func(gtx l.Context) l.Dimensions {
return l.Flex{Axis: l.Vertical}.Layout(gtx, rightWidgets...)
})
}),
)
})
}
func setDevice() {
//Build your node with known keys (ABP).
nwkSEncHexKey := config.Device.NwkSEncKey
sNwkSIntHexKey := config.Device.SNwkSIntKey
fNwkSIntHexKey := config.Device.FNwkSIntKey
appSHexKey := config.Device.AppSKey
devHexAddr := config.Device.DevAddress
devAddr, err := lds.HexToDevAddress(devHexAddr)
if err != nil {
log.Errorf("dev addr error: %s", err)
}
nwkSEncKey, err := lds.HexToKey(nwkSEncHexKey)
if err != nil {
log.Errorf("nwkSEncKey error: %s", err)
}
sNwkSIntKey, err := lds.HexToKey(sNwkSIntHexKey)
if err != nil {
log.Errorf("sNwkSIntKey error: %s", err)
}
fNwkSIntKey, err := lds.HexToKey(fNwkSIntHexKey)
if err != nil {
log.Errorf("fNwkSIntKey error: %s", err)
}
appSKey, err := lds.HexToKey(appSHexKey)
if err != nil {
log.Errorf("appskey error: %s", err)
}
devEUI, err := lds.HexToEUI(config.Device.DevEUI)
if err != nil {
log.Errorf("devEUI error: %s", err)
return
}
nwkHexKey := config.Device.NwkKey
appHexKey := config.Device.AppKey
appKey, err := lds.HexToKey(appHexKey)
if err != nil {
log.Errorf("appKey error: %s", err)
return
}
nwkKey, err := lds.HexToKey(nwkHexKey)
if err != nil {
log.Errorf("nwkKey error: %s", err)
return
}
joinEUI, err := lds.HexToEUI(config.Device.JoinEUI)
if err != nil {
log.Errorf("joinEUI error: %s", err)
return
}
if cDevice == nil {
cDevice = &lds.Device{
DevEUI: devEUI,
DevAddr: devAddr,
NwkSEncKey: nwkSEncKey,
SNwkSIntKey: sNwkSIntKey,
FNwkSIntKey: fNwkSIntKey,
AppSKey: appSKey,
AppKey: appKey,
NwkKey: nwkKey,
JoinEUI: joinEUI,
Profile: config.Device.Profile,
Major: lorawan.Major(config.Device.Major),
MACVersion: lorawan.MACVersion(config.Device.MACVersion),
SkipFCntCheck: config.Device.SkipFCntCheck,
}
//Get redis info.
if cDevice.GetInfo() {
config.Device.NwkSEncKey = lds.KeyToHex(cDevice.NwkSEncKey)
config.Device.FNwkSIntKey = lds.KeyToHex(cDevice.FNwkSIntKey)
config.Device.SNwkSIntKey = lds.KeyToHex(cDevice.SNwkSIntKey)
config.Device.AppSKey = lds.KeyToHex(cDevice.AppSKey)
config.Device.DevAddress = lds.DevAddressToHex(cDevice.DevAddr)
ulFcnt := int(cDevice.UlFcnt)
dlFcnt := int(cDevice.DlFcnt)
devNonce := int(cDevice.DevNonce)
joinNonce := int(cDevice.JoinNonce)
ulFcntEdit.SetText(strconv.Itoa(ulFcnt))
dlFcntEdit.SetText(strconv.Itoa(dlFcnt))
devNonceEdit.SetText(strconv.Itoa(devNonce))
joinNonceEdit.SetText(strconv.Itoa(joinNonce))
}
} else {
cDevice.DevEUI = devEUI
cDevice.DevAddr = devAddr
cDevice.NwkSEncKey = nwkSEncKey
cDevice.SNwkSIntKey = sNwkSIntKey
cDevice.FNwkSIntKey = fNwkSIntKey
cDevice.AppSKey = appSKey
cDevice.AppKey = appKey
cDevice.NwkKey = nwkKey
cDevice.JoinEUI = joinEUI
cDevice.Profile = config.Device.Profile
cDevice.Major = lorawan.Major(config.Device.Major)
cDevice.MACVersion = lorawan.MACVersion(config.Device.MACVersion)
cDevice.SkipFCntCheck = config.Device.SkipFCntCheck
}
cDevice.SetMarshaler(config.Device.Marshaler)
}
func resetDeviceSubform(th *material.Theme) (bool, l.FlexChild) {
for resetCancelButton.Clicked() {
resetDevice = false
return false, l.FlexChild{}
}
for resetConfirmButton.Clicked() {
//Reset device.
err := cDevice.Reset()
if err != nil {
log.Errorln(err)
} else {
setDevice()
log.Warningln("Device was reset")
}
resetDevice = false
return false, l.FlexChild{}
}
widgets := []l.FlexChild{
xmat.RigidSection(th, "This will delete saved devNonce, joinNonce, downlink and uplink frame counters,\ndevice address and device keys.\nAre you sure you want to proceed?"),
xmat.RigidButton(th, "Cancel", &resetCancelButton),
xmat.RigidButton(th, "Confirm", &resetConfirmButton),
}
inset := l.Inset{Top: unit.Dp(20)}
return true, l.Rigid(func(gtx l.Context) l.Dimensions {
return inset.Layout(gtx, func(gtx l.Context) l.Dimensions {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx, widgets...)
})
})
}
func setRedisValuesSubform(th *material.Theme) (bool, layout.FlexChild) {
ulFcntEdit.SetText(strconv.FormatUint(uint64(cDevice.UlFcnt), 10))
dlFcntEdit.SetText(strconv.FormatUint(uint64(cDevice.DlFcnt), 10))
devNonceEdit.SetText(strconv.FormatUint(uint64(cDevice.DevNonce), 10))
joinNonceEdit.SetText(strconv.FormatUint(uint64(cDevice.JoinNonce), 10))
for setRedisValuesCancelButton.Clicked() {
//Close popup.
setRedisValues = false
return false, layout.FlexChild{}
}
for setRedisValuesConfirmButton.Clicked() {
//Set values.
var (
ulFcnt int
dlFcnt int
devNonce int
joinNonce int
)
extractInt(&ulFcntEdit, &ulFcnt, 0)
extractInt(&dlFcntEdit, &dlFcnt, 0)
extractInt(&devNonceEdit, &devNonce, 0)
extractInt(&joinNonceEdit, &joinNonce, 0)
log.Warningln("Setting Redis values")
err := cDevice.SetValues(ulFcnt, dlFcnt, devNonce, joinNonce)
if err != nil {
log.Errorln(err)
}
//Close popup.
setRedisValues = false
return false, layout.FlexChild{}
}
widgets := []layout.FlexChild{
xmat.RigidSection(th, "Set counters and nonces"),
xmat.RigidLabel(th, "Warning: this will only work when device is activated; when not, values will be reset on program start. Modifying these values may result in failure of communication."),
xmat.RigidEditor(th, fmt.Sprintf("DlFcnt"), "<downlink>", &dlFcntEdit),
xmat.RigidEditor(th, fmt.Sprintf("UlFcnt"), "<uplink>", &ulFcntEdit),
xmat.RigidEditor(th, fmt.Sprintf("DevNonce"), "<dev nonce>", &devNonceEdit),
xmat.RigidEditor(th, fmt.Sprintf("JoinNonce"), "<join nonce>", &joinNonceEdit),
xmat.RigidButton(th, "Cancel", &setRedisValuesCancelButton),
xmat.RigidButton(th, "Confirm", &setRedisValuesConfirmButton),
}
inset := layout.Inset{Top: unit.Dp(20)}
return true, layout.Rigid(func(gtx l.Context) l.Dimensions {
return inset.Layout(gtx, func(gtx l.Context) l.Dimensions {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx, widgets...)
})
})
}
func join() {
if !cNSClient.IsConnected() {
if mqttClient == nil || !mqttClient.IsConnected() {
log.Errorln("Neither client is connected")
return
}
}
//Always set device to get any changes to the configuration.
setDevice()
gwID, err := lds.MACToGatewayID(config.GW.MAC)
if err != nil {
log.Errorf("gw mac error: %s", err)
return
}
now := time.Now()
rxTime := ptypes.TimestampNow()
tsge := ptypes.DurationProto(now.Sub(time.Time{}))
urx := gw.UplinkRXInfo{
GatewayId: gwID,
Rssi: int32(config.RXInfo.Rssi),
LoraSnr: float64(config.RXInfo.LoRaSNR),
Channel: uint32(config.RXInfo.Channel),
RfChain: uint32(config.RXInfo.RfChain),
TimeSinceGpsEpoch: tsge,
Time: rxTime,
Board: 0,
Antenna: 0,
Location: nil,
FineTimestamp: nil,
FineTimestampType: gw.FineTimestampType_NONE,
Context: make([]byte, 4),
}
lmi := &gw.LoRaModulationInfo{
Bandwidth: uint32(config.DR.Bandwidth),
SpreadingFactor: uint32(config.DR.SpreadFactor),
CodeRate: config.RXInfo.CodeRate,
}
umi := &gw.UplinkTXInfo_LoraModulationInfo{
LoraModulationInfo: lmi,
}
utx := gw.UplinkTXInfo{
Frequency: uint32(config.RXInfo.Frequency),
ModulationInfo: umi,
}
if !cNSClient.IsConnected() {
err = cDevice.Join(mqttClient, config.MQTT.UplinkTopic, config.GW.MAC, &urx, &utx)
} else {
err = cDevice.JoinUDP(cNSClient, config.GW.MAC, &urx, &utx)
}
if err != nil {
log.Errorf("join error: %s", err)
} else {
log.Println("join sent")
}
}
func run() {
if !cNSClient.IsConnected() {
if mqttClient == nil || !mqttClient.IsConnected() {
log.Errorln("Neither client is connected")
return
}
}
setDevice()
/*dataRate := &lds.DataRate{
Bandwidth: config.DR.Bandwidth,
Modulation: "LORA",
SpreadFactor: config.DR.SpreadFactor,
BitRate: config.DR.BitRate,
}*/
//Get DR index from a dr.
dataRate := lwBand.DataRate{
Modulation: lwBand.Modulation("LORA"),
SpreadFactor: config.DR.SpreadFactor,
Bandwidth: config.DR.Bandwidth,
BitRate: config.DR.BitRate,
}
running = true
for {
if stop {
stop = false
running = false
return
}
payload := []byte{}
var pErr error
if config.RawPayload.UseRaw {
payload, pErr = hex.DecodeString(config.RawPayload.Payload)
if pErr != nil {
log.Errorf("couldn't decode hex payload: %s", pErr)
running = false
return
}
} else if config.RawPayload.UseEncoder {
payload, pErr = EncodeToBytes()
if pErr != nil {
log.Errorf("couldn't encode js object: %s", pErr)
running = false
return
}
} else {
for _, v := range config.EncodedType {
if v.IsFloat {
arr := lds.GenerateFloat(float32(v.Value), float32(v.MaxValue), int32(v.NumBytes))
payload = append(payload, arr...)
} else {
arr := lds.GenerateInt(int32(v.Value), int32(v.NumBytes))
payload = append(payload, arr...)
}
}
}
gwID, err := lds.MACToGatewayID(config.GW.MAC)
if err != nil {
log.Errorf("gw mac error: %s", err)
running = false
return
}
now := time.Now()
rxTime := ptypes.TimestampNow()
tsge := ptypes.DurationProto(now.Sub(time.Time{}))
urx := gw.UplinkRXInfo{
GatewayId: gwID,
Rssi: int32(config.RXInfo.Rssi),
LoraSnr: float64(config.RXInfo.LoRaSNR),
Channel: uint32(config.RXInfo.Channel),
RfChain: uint32(config.RXInfo.RfChain),
TimeSinceGpsEpoch: tsge,
Time: rxTime,
Board: 0,
Antenna: 0,
Location: nil,
FineTimestamp: nil,
FineTimestampType: gw.FineTimestampType_NONE,
Context: make([]byte, 4),
}
lmi := &gw.LoRaModulationInfo{
Bandwidth: uint32(config.DR.Bandwidth),
SpreadingFactor: uint32(config.DR.SpreadFactor),
CodeRate: config.RXInfo.CodeRate,
}
umi := &gw.UplinkTXInfo_LoraModulationInfo{
LoraModulationInfo: lmi,
}
utx := gw.UplinkTXInfo{
Frequency: uint32(config.RXInfo.Frequency),
ModulationInfo: umi,
}
var fOpts []*lorawan.MACCommand
for i := 0; i < len(macCommands); i++ {
if macCommands[i].Use.Value {
fOpts = append(fOpts, &macCommands[i].MACCommand)
}
}
//Now send an uplink
var ulfc uint32
if !cNSClient.IsConnected() {
ulfc, err = cDevice.Uplink(mqttClient, config.MQTT.UplinkTopic, config.Device.MType, uint8(config.RawPayload.FPort), &urx, &utx, payload, config.GW.MAC, config.Band.Name, dataRate, fOpts, fCtrl)
} else {
ulfc, err = cDevice.UplinkUDP(cNSClient, config.Device.MType, uint8(config.RawPayload.FPort), &urx, &utx, payload, config.GW.MAC, config.Band.Name, dataRate, fOpts, fCtrl)
}
if err != nil {
log.Errorf("couldn't send uplink: %s", err)
} else {
log.Infof("message sent, uplink framecounter is now %d", ulfc)
}
if !repeat || !running {
stop = false
running = false
return
}
time.Sleep(time.Duration(interval) * time.Second)
}
}
func onIncomingDownlink(payload []byte) error {
log.Debugf("Incoming Downlink len=%d", len(payload))
err := error(nil)
if cDevice != nil {
mqtt := mqttClient != nil && mqttClient.IsConnected()
dlMessage, err := cDevice.ProcessDownlink(payload, cDevice.MACVersion, mqtt)
//Update keys when necessary.
config.Device.AppSKey = lds.KeyToHex(cDevice.AppSKey)
config.Device.FNwkSIntKey = lds.KeyToHex(cDevice.FNwkSIntKey)
config.Device.NwkSEncKey = lds.KeyToHex(cDevice.NwkSEncKey)
config.Device.SNwkSIntKey = lds.KeyToHex(cDevice.SNwkSIntKey)
config.Device.DevAddress = lds.DevAddressToHex(cDevice.DevAddr)
config.Device.Joined = cDevice.Joined
//Update session keys based on join-accept
if cDevice.Profile == "OTAA" && cDevice.Joined {
deviceAddressEdit.SetText(config.Device.DevAddress)
nwkSEncKeyEdit.SetText(config.Device.NwkSEncKey)
sNwkSIntKeyEdit.SetText(config.Device.SNwkSIntKey)
appSKeyEdit.SetText(config.Device.AppSKey)
fNwkSIntKeyEdit.SetText(config.Device.FNwkSIntKey)
}
if err != nil {
log.Errorf("downlink error: %s", err)
} else {
log.Infof("received message: %s", dlMessage)
}
//Get redis info.
cDevice.GetInfo()
}
return err
}