-
Notifications
You must be signed in to change notification settings - Fork 159
/
mac_commands.go
1335 lines (1117 loc) · 36.7 KB
/
mac_commands.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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:generate stringer -type=CID
//go:generate stringer -type=DeviceModeClass
package lorawan
import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"log"
"sync"
"time"
)
// macPayloadMutex is used when registering proprietary MAC command payloads to
// the macPayloadRegistry.
var macPayloadMutex sync.RWMutex
// CID defines the MAC command identifier.
type CID byte
// MarshalText implements encoding.TextMarshaler.
func (c CID) MarshalText() ([]byte, error) {
return []byte(c.String()), nil
}
// MAC commands as specified by the LoRaWAN R1.0 specs. Note that each *Req / *Ans
// has the same value. Based on the fact if a message is uplink or downlink
// you should use on or the other.
const (
// Class-A
ResetInd CID = 0x01
ResetConf CID = 0x01
LinkCheckReq CID = 0x02
LinkCheckAns CID = 0x02
LinkADRReq CID = 0x03
LinkADRAns CID = 0x03
DutyCycleReq CID = 0x04
DutyCycleAns CID = 0x04
RXParamSetupReq CID = 0x05
RXParamSetupAns CID = 0x05
DevStatusReq CID = 0x06
DevStatusAns CID = 0x06
NewChannelReq CID = 0x07
NewChannelAns CID = 0x07
RXTimingSetupReq CID = 0x08
RXTimingSetupAns CID = 0x08
TXParamSetupReq CID = 0x09
TXParamSetupAns CID = 0x09
DLChannelReq CID = 0x0A
DLChannelAns CID = 0x0A
RekeyInd CID = 0x0B
RekeyConf CID = 0x0B
ADRParamSetupReq CID = 0x0C
ADRParamSetupAns CID = 0x0C
DeviceTimeReq CID = 0x0D
DeviceTimeAns CID = 0x0D
ForceRejoinReq CID = 0x0E
RejoinParamSetupReq CID = 0x0F
RejoinParamSetupAns CID = 0x0F
// Class-B
PingSlotInfoReq CID = 0x10
PingSlotInfoAns CID = 0x10
PingSlotChannelReq CID = 0x11
PingSlotChannelAns CID = 0x11
// 0x12 has been deprecated in 1.1
BeaconFreqReq CID = 0x13
BeaconFreqAns CID = 0x13
// Class-C
DeviceModeInd CID = 0x20
DeviceModeConf CID = 0x20
// 0x80 to 0xFF reserved for proprietary network command extensions
)
// macPayloadInfo contains the info about a MAC payload
type macPayloadInfo struct {
size int
payload func() MACCommandPayload
}
// macPayloadRegistry contains the info for uplink and downlink MAC payloads
// in the format map[uplink]map[CID].
// Note that MAC command that do not have a payload are not included in this
// list.
var macPayloadRegistry = map[bool]map[CID]macPayloadInfo{
false: map[CID]macPayloadInfo{
ResetConf: {1, func() MACCommandPayload { return &ResetConfPayload{} }},
LinkCheckAns: {2, func() MACCommandPayload { return &LinkCheckAnsPayload{} }},
LinkADRReq: {4, func() MACCommandPayload { return &LinkADRReqPayload{} }},
DutyCycleReq: {1, func() MACCommandPayload { return &DutyCycleReqPayload{} }},
RXParamSetupReq: {4, func() MACCommandPayload { return &RXParamSetupReqPayload{} }},
NewChannelReq: {5, func() MACCommandPayload { return &NewChannelReqPayload{} }},
RXTimingSetupReq: {1, func() MACCommandPayload { return &RXTimingSetupReqPayload{} }},
TXParamSetupReq: {1, func() MACCommandPayload { return &TXParamSetupReqPayload{} }},
DLChannelReq: {4, func() MACCommandPayload { return &DLChannelReqPayload{} }},
BeaconFreqReq: {3, func() MACCommandPayload { return &BeaconFreqReqPayload{} }},
PingSlotChannelReq: {4, func() MACCommandPayload { return &PingSlotChannelReqPayload{} }},
DeviceTimeAns: {5, func() MACCommandPayload { return &DeviceTimeAnsPayload{} }},
RekeyConf: {1, func() MACCommandPayload { return &RekeyConfPayload{} }},
ADRParamSetupReq: {1, func() MACCommandPayload { return &ADRParamSetupReqPayload{} }},
ForceRejoinReq: {2, func() MACCommandPayload { return &ForceRejoinReqPayload{} }},
RejoinParamSetupReq: {1, func() MACCommandPayload { return &RejoinParamSetupReqPayload{} }},
DeviceModeConf: {1, func() MACCommandPayload { return &DeviceModeConfPayload{} }},
},
true: map[CID]macPayloadInfo{
ResetInd: {1, func() MACCommandPayload { return &ResetIndPayload{} }},
LinkADRAns: {1, func() MACCommandPayload { return &LinkADRAnsPayload{} }},
RXParamSetupAns: {1, func() MACCommandPayload { return &RXParamSetupAnsPayload{} }},
DevStatusAns: {2, func() MACCommandPayload { return &DevStatusAnsPayload{} }},
NewChannelAns: {1, func() MACCommandPayload { return &NewChannelAnsPayload{} }},
DLChannelAns: {1, func() MACCommandPayload { return &DLChannelAnsPayload{} }},
PingSlotInfoReq: {1, func() MACCommandPayload { return &PingSlotInfoReqPayload{} }},
BeaconFreqAns: {1, func() MACCommandPayload { return &BeaconFreqAnsPayload{} }},
PingSlotChannelAns: {1, func() MACCommandPayload { return &PingSlotChannelAnsPayload{} }},
RekeyInd: {1, func() MACCommandPayload { return &RekeyIndPayload{} }},
RejoinParamSetupAns: {1, func() MACCommandPayload { return &RejoinParamSetupAnsPayload{} }},
DeviceModeInd: {1, func() MACCommandPayload { return &DeviceModeIndPayload{} }},
},
}
// DwellTime defines the dwell time type.
type DwellTime int
// Possible dwell time options.
const (
DwellTimeNoLimit DwellTime = iota
DwellTime400ms
)
// GetMACPayloadAndSize returns a new MACCommandPayload instance and it's size.
func GetMACPayloadAndSize(uplink bool, c CID) (MACCommandPayload, int, error) {
macPayloadMutex.RLock()
defer macPayloadMutex.RUnlock()
v, ok := macPayloadRegistry[uplink][c]
if !ok {
return nil, 0, fmt.Errorf("lorawan: payload unknown for uplink=%v and CID=%v", uplink, c)
}
return v.payload(), v.size, nil
}
// RegisterProprietaryMACCommand registers a proprietary MAC command. Note
// that there is no need to call this when the size of the payload is > 0 bytes.
func RegisterProprietaryMACCommand(uplink bool, cid CID, payloadSize int) error {
if !(cid >= 128 && cid <= 255) {
return fmt.Errorf("lorawan: invalid CID %x", byte(cid))
}
if payloadSize == 0 {
// no need to register the payload size
return nil
}
macPayloadMutex.Lock()
defer macPayloadMutex.Unlock()
macPayloadRegistry[uplink][cid] = macPayloadInfo{
size: payloadSize,
payload: func() MACCommandPayload { return &ProprietaryMACCommandPayload{} },
}
return nil
}
// MACCommandPayload is the interface that every MACCommand payload
// must implement.
type MACCommandPayload interface {
MarshalBinary() (data []byte, err error)
UnmarshalBinary(data []byte) error
}
// MACCommand represents a MAC command with optional payload.
type MACCommand struct {
CID CID `json:"cid"`
Payload MACCommandPayload `json:"payload"`
}
// MarshalBinary marshals the object in binary form.
func (m MACCommand) MarshalBinary() ([]byte, error) {
b := []byte{byte(m.CID)}
if m.Payload != nil {
p, err := m.Payload.MarshalBinary()
if err != nil {
return nil, err
}
b = append(b, p...)
}
return b, nil
}
// UnmarshalBinary decodes the object from binary form.
func (m *MACCommand) UnmarshalBinary(uplink bool, data []byte) error {
if len(data) == 0 {
return errors.New("lorawan: at least 1 byte of data is expected")
}
m.CID = CID(data[0])
if len(data) > 1 {
p, _, err := GetMACPayloadAndSize(uplink, m.CID)
if err != nil {
return err
}
m.Payload = p
if err := m.Payload.UnmarshalBinary(data[1:]); err != nil {
return err
}
}
return nil
}
// ProprietaryMACCommandPayload represents a proprietary payload.
type ProprietaryMACCommandPayload struct {
Bytes []byte `json:"bytes"`
}
// MarshalBinary marshals the object into a slice of bytes.
func (p ProprietaryMACCommandPayload) MarshalBinary() ([]byte, error) {
return p.Bytes, nil
}
// UnmarshalBinary decodes the object from a slice of bytes.
func (p *ProprietaryMACCommandPayload) UnmarshalBinary(data []byte) error {
p.Bytes = data
return nil
}
// LinkCheckAnsPayload represents the LinkCheckAns payload.
type LinkCheckAnsPayload struct {
Margin uint8 `json:"margin"`
GwCnt uint8 `json:"gwCnt"`
}
// MarshalBinary marshals the object in binary form.
func (p LinkCheckAnsPayload) MarshalBinary() ([]byte, error) {
return []byte{byte(p.Margin), byte(p.GwCnt)}, nil
}
// UnmarshalBinary decodes the object from binary form.
func (p *LinkCheckAnsPayload) UnmarshalBinary(data []byte) error {
if len(data) != 2 {
return errors.New("lorawan: 2 bytes of data are expected")
}
p.Margin = uint8(data[0])
p.GwCnt = uint8(data[1])
return nil
}
// ChMask encodes the channels usable for uplink access. 0 = channel 1,
// 15 = channel 16.
type ChMask [16]bool
// MarshalBinary marshals the object in binary form.
func (m ChMask) MarshalBinary() ([]byte, error) {
var n uint16
for i := uint(0); i < 16; i++ {
if m[i] {
n |= 1 << i
}
}
b := make([]byte, 2)
binary.LittleEndian.PutUint16(b, n)
return b, nil
}
// UnmarshalBinary decodes the object from binary form.
func (m *ChMask) UnmarshalBinary(data []byte) error {
if len(data) != 2 {
return errors.New("lorawan: 2 bytes of data are expected")
}
n := binary.LittleEndian.Uint16(data)
for i := uint(0); i < 16; i++ {
if n&(1<<i) != 0 {
m[i] = true
}
}
return nil
}
// Redundancy represents the redundancy field.
type Redundancy struct {
ChMaskCntl uint8 `json:"chMaskCntl"`
NbRep uint8 `json:"nbRep"`
}
// MarshalBinary marshals the object in binary form.
func (r Redundancy) MarshalBinary() ([]byte, error) {
b := make([]byte, 1)
if r.NbRep > 15 {
return b, errors.New("lorawan: max value of NbRep is 15")
}
if r.ChMaskCntl > 7 {
return b, errors.New("lorawan: max value of ChMaskCntl is 7")
}
b[0] = r.NbRep ^ (r.ChMaskCntl << 4)
return b, nil
}
// UnmarshalBinary decodes the object from binary form.
func (r *Redundancy) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return errors.New("lorawan: 1 byte of data is expected")
}
r.NbRep = data[0] & ((1 << 3) ^ (1 << 2) ^ (1 << 1) ^ (1 << 0))
r.ChMaskCntl = (data[0] & ((1 << 6) ^ (1 << 5) ^ (1 << 4))) >> 4
return nil
}
// LinkADRReqPayload represents the LinkADRReq payload.
type LinkADRReqPayload struct {
DataRate uint8 `json:"dataRate"`
TXPower uint8 `json:"txPower"`
ChMask ChMask `json:"chMask"`
Redundancy Redundancy `json:"redundancy"`
}
// MarshalBinary marshals the object in binary form.
func (p LinkADRReqPayload) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, 4)
if p.DataRate > 15 {
return b, errors.New("lorawan: the max value of DataRate is 15")
}
if p.TXPower > 15 {
return b, errors.New("lorawan: the max value of TXPower is 15")
}
cm, err := p.ChMask.MarshalBinary()
if err != nil {
return b, err
}
r, err := p.Redundancy.MarshalBinary()
if err != nil {
return b, err
}
b = append(b, p.TXPower^(p.DataRate<<4))
b = append(b, cm...)
b = append(b, r...)
return b, nil
}
// UnmarshalBinary decodes the object from binary form.
func (p *LinkADRReqPayload) UnmarshalBinary(data []byte) error {
if len(data) != 4 {
return errors.New("lorawan: 4 bytes of data are expected")
}
p.DataRate = (data[0] & ((1 << 7) ^ (1 << 6) ^ (1 << 5) ^ (1 << 4))) >> 4
p.TXPower = data[0] & ((1 << 3) ^ (1 << 2) ^ (1 << 1) ^ (1 << 0))
if err := p.ChMask.UnmarshalBinary(data[1:3]); err != nil {
return err
}
return p.Redundancy.UnmarshalBinary(data[3:4])
}
// LinkADRAnsPayload represents the LinkADRAns payload.
type LinkADRAnsPayload struct {
ChannelMaskACK bool `json:"channelMaskAck"`
DataRateACK bool `json:"dataRateAck"`
PowerACK bool `json:"powerAck"`
}
// MarshalBinary marshals the object in binary form.
func (p LinkADRAnsPayload) MarshalBinary() ([]byte, error) {
var b byte
if p.ChannelMaskACK {
b = b ^ (1 << 0)
}
if p.DataRateACK {
b = b ^ (1 << 1)
}
if p.PowerACK {
b = b ^ (1 << 2)
}
return []byte{b}, nil
}
// UnmarshalBinary decodes the object from binary form.
func (p *LinkADRAnsPayload) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return errors.New("lorawan: 1 byte of data is expected")
}
if data[0]&(1<<0) > 0 {
p.ChannelMaskACK = true
}
if data[0]&(1<<1) > 0 {
p.DataRateACK = true
}
if data[0]&(1<<2) > 0 {
p.PowerACK = true
}
return nil
}
// DutyCycleReqPayload represents the DutyCycleReq payload.
type DutyCycleReqPayload struct {
MaxDCycle uint8 `json:"maxDCycle"`
}
// MarshalBinary marshals the object in binary form.
func (p DutyCycleReqPayload) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, 1)
if p.MaxDCycle > 15 && p.MaxDCycle < 255 {
return b, errors.New("lorawan: only a MaxDCycle value of 0 - 15 and 255 is allowed")
}
b = append(b, p.MaxDCycle)
return b, nil
}
// UnmarshalBinary decodes the object from binary form.
func (p *DutyCycleReqPayload) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return errors.New("lorawan: 1 byte of data is expected")
}
p.MaxDCycle = data[0]
return nil
}
// DLSettings represents the DLSettings fields (downlink settings).
type DLSettings struct {
OptNeg bool `json:"optNeg"`
RX2DataRate uint8 `json:"rx2DataRate"`
RX1DROffset uint8 `json:"rx1DROffset"`
}
// MarshalBinary marshals the object in binary form.
func (s DLSettings) MarshalBinary() ([]byte, error) {
if s.RX2DataRate > 15 {
return nil, errors.New("lorawan: max value of RX2DataRate is 15")
}
if s.RX1DROffset > 7 {
return nil, errors.New("lorawan: max value of RX1DROffset is 7")
}
b := s.RX2DataRate
b |= s.RX1DROffset << 4
if s.OptNeg {
b |= 1 << 7
}
return []byte{b}, nil
}
// MarshalText implements encoding.TextMarshaler.
func (s DLSettings) MarshalText() ([]byte, error) {
b, err := s.MarshalBinary()
if err != nil {
return nil, err
}
return []byte(hex.EncodeToString(b)), nil
}
// UnmarshalBinary decodes the object from binary form.
func (s *DLSettings) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return errors.New("lorawan: 1 byte of data is expected")
}
s.OptNeg = (data[0] & (1 << 7)) != 0
s.RX2DataRate = data[0] & ((1 << 3) | (1 << 2) | (1 << 1) | 1)
s.RX1DROffset = (data[0] & ((1 << 6) | (1 << 5) | (1 << 4))) >> 4
return nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (s *DLSettings) UnmarshalText(text []byte) error {
b, err := hex.DecodeString(string(text))
if err != nil {
return err
}
return s.UnmarshalBinary(b)
}
// RXParamSetupReqPayload represents the RXParamSetupReq payload.
type RXParamSetupReqPayload struct {
Frequency uint32 `json:"frequency"`
DLSettings DLSettings `json:"dlSettings"`
}
// MarshalBinary marshals the object in binary form.
func (p RXParamSetupReqPayload) MarshalBinary() ([]byte, error) {
b := make([]byte, 5)
if p.Frequency/100 >= 16777216 { // 2^24
return b, errors.New("lorawan: max value of Frequency is 2^24-1")
}
if p.Frequency%100 != 0 {
return b, errors.New("lorawan: Frequency must be a multiple of 100")
}
bytes, err := p.DLSettings.MarshalBinary()
if err != nil {
return b, err
}
b[0] = bytes[0]
binary.LittleEndian.PutUint32(b[1:5], p.Frequency/100)
// we don't return the last octet which is fine since we're only interested
// in the 24 LSB of Frequency
return b[0:4], nil
}
// UnmarshalBinary decodes the object from binary form.
func (p *RXParamSetupReqPayload) UnmarshalBinary(data []byte) error {
if len(data) != 4 {
return errors.New("lorawan: 4 bytes of data are expected")
}
if err := p.DLSettings.UnmarshalBinary(data[0:1]); err != nil {
return err
}
// append one block of empty bits at the end of the slice since the
// binary to uint32 expects 32 bits.
b := make([]byte, len(data))
copy(b, data)
b = append(b, byte(0))
p.Frequency = binary.LittleEndian.Uint32(b[1:5]) * 100
return nil
}
// RXParamSetupAnsPayload represents the RXParamSetupAns payload.
type RXParamSetupAnsPayload struct {
ChannelACK bool `json:"channelAck"`
RX2DataRateACK bool `json:"rx2DataRateAck"`
RX1DROffsetACK bool `json:"rx1DROffsetAck"`
}
// MarshalBinary marshals the object in binary form.
func (p RXParamSetupAnsPayload) MarshalBinary() ([]byte, error) {
var b byte
if p.ChannelACK {
b = b ^ (1 << 0)
}
if p.RX2DataRateACK {
b = b ^ (1 << 1)
}
if p.RX1DROffsetACK {
b = b ^ (1 << 2)
}
return []byte{b}, nil
}
// UnmarshalBinary decodes the object from binary form.
func (p *RXParamSetupAnsPayload) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return errors.New("lorawan: 1 byte of data is expected")
}
p.ChannelACK = data[0]&(1<<0) > 0
p.RX2DataRateACK = data[0]&(1<<1) > 0
p.RX1DROffsetACK = data[0]&(1<<2) > 0
return nil
}
// DevStatusAnsPayload represents the DevStatusAns payload.
type DevStatusAnsPayload struct {
Battery uint8 `json:"battery"`
Margin int8 `json:"margin"`
}
// MarshalBinary marshals the object in binary form.
func (p DevStatusAnsPayload) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, 2)
if p.Margin < -32 {
return b, errors.New("lorawan: min value of Margin is -32")
}
if p.Margin > 31 {
return b, errors.New("lorawan: max value of Margin is 31")
}
b = append(b, p.Battery)
if p.Margin < 0 {
b = append(b, uint8(64+p.Margin))
} else {
b = append(b, uint8(p.Margin))
}
return b, nil
}
// UnmarshalBinary decodes the object from binary form.
func (p *DevStatusAnsPayload) UnmarshalBinary(data []byte) error {
if len(data) != 2 {
return errors.New("lorawan: 2 bytes of data are expected")
}
p.Battery = data[0]
if data[1] > 31 {
p.Margin = int8(data[1]) - 64
} else {
p.Margin = int8(data[1])
}
return nil
}
// NewChannelReqPayload represents the NewChannelReq payload.
type NewChannelReqPayload struct {
ChIndex uint8 `json:"chIndex"`
Freq uint32 `json:"freq"`
MaxDR uint8 `json:"maxDR"`
MinDR uint8 `json:"minDR"`
}
// MarshalBinary marshals the object in binary form.
func (p NewChannelReqPayload) MarshalBinary() ([]byte, error) {
b := make([]byte, 5)
freq := p.Freq
// Support LoRaWAN 2.4GHz, in which case the stepping is 200Hz:
// See Frequency Encoding in MAC Commands
// https://lora-developers.semtech.com/documentation/tech-papers-and-guides/physical-layer-proposal-2.4ghz/
if freq >= 2400000000 {
freq = freq / 2
}
if freq/100 >= 16777216 { // 2^24
return b, errors.New("lorawan: max value of Freq is 2^24 - 1")
}
if p.Freq%100 != 0 {
return b, errors.New("lorawan: Freq must be a multiple of 100")
}
if p.MaxDR > 15 {
return b, errors.New("lorawan: max value of MaxDR is 15")
}
if p.MinDR > 15 {
return b, errors.New("lorawan: max value of MinDR is 15")
}
// we're borrowing the last byte b[4] because PutUint32 needs 4 bytes,
// the last byte b[4] will be set to 0 because max Freq = 2^24 - 1
binary.LittleEndian.PutUint32(b[1:5], freq/100)
b[0] = p.ChIndex
b[4] = p.MinDR ^ (p.MaxDR << 4)
return b, nil
}
// UnmarshalBinary decodes the object from binary form.
func (p *NewChannelReqPayload) UnmarshalBinary(data []byte) error {
if len(data) != 5 {
return errors.New("lorawan: 5 bytes of data are expected")
}
p.ChIndex = data[0]
p.MinDR = data[4] & ((1 << 3) ^ (1 << 2) ^ (1 << 1) ^ (1 << 0))
p.MaxDR = (data[4] & ((1 << 7) ^ (1 << 6) ^ (1 << 5) ^ (1 << 4))) >> 4
b := make([]byte, len(data))
copy(b, data)
b[4] = byte(0)
freq := binary.LittleEndian.Uint32(b[1:5])
if freq >= 12000000 {
// 2.4GHz frequency
p.Freq = freq * 200
} else {
p.Freq = freq * 100
}
return nil
}
// NewChannelAnsPayload represents the NewChannelAns payload.
type NewChannelAnsPayload struct {
ChannelFrequencyOK bool `json:"channelFrequencyOK"`
DataRateRangeOK bool `json:"dataRateRangeOK"`
}
// MarshalBinary marshals the object in binary form.
func (p NewChannelAnsPayload) MarshalBinary() ([]byte, error) {
var b byte
if p.ChannelFrequencyOK {
b = (1 << 0)
}
if p.DataRateRangeOK {
b = b ^ (1 << 1)
}
return []byte{b}, nil
}
// UnmarshalBinary decodes the object from binary form.
func (p *NewChannelAnsPayload) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return errors.New("lorawan: 1 byte of data is expected")
}
p.ChannelFrequencyOK = data[0]&(1<<0) > 0
p.DataRateRangeOK = data[0]&(1<<1) > 0
return nil
}
// RXTimingSetupReqPayload represents the RXTimingSetupReq payload.
type RXTimingSetupReqPayload struct {
Delay uint8 `json:"delay"` // 0=1s, 1=1s, 2=2s, ... 15=15s
}
// MarshalBinary marshals the object in binary form.
func (p RXTimingSetupReqPayload) MarshalBinary() ([]byte, error) {
if p.Delay > 15 {
return []byte{}, errors.New("lorawan: the max value of Delay is 15")
}
return []byte{p.Delay}, nil
}
// UnmarshalBinary decodes the object from binary form.
func (p *RXTimingSetupReqPayload) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return errors.New("lorawan: 1 byte of data is expected")
}
p.Delay = data[0]
return nil
}
// TXParamSetupReqPayload represents the TXParamSetupReq payload.
// Note that the MaxEIRP holds the coded index value, not the actual EIRP.
// To translate from the actual EIRP to the coded index value, use the
// GetTXParamSetupEIRPIndex function. To translate a coded index value back
// to the actual EIRP, use the GetTXParamSetupEIRP function.
type TXParamSetupReqPayload struct {
DownlinkDwelltime DwellTime `json:"downlinkDwellTime"`
UplinkDwellTime DwellTime `json:"uplinkDwellTime"`
MaxEIRP uint8 `json:"maxEIRPCoded"`
}
// MarshalBinary encodes the object into a bytes.
func (p TXParamSetupReqPayload) MarshalBinary() ([]byte, error) {
if p.MaxEIRP > 15 {
return nil, errors.New("lorawan: max value of MaxEIRP is 15")
}
b := p.MaxEIRP
if p.UplinkDwellTime == DwellTime400ms {
b = b ^ (1 << 4)
}
if p.DownlinkDwelltime == DwellTime400ms {
b = b ^ (1 << 5)
}
return []byte{b}, nil
}
// UnmarshalBinary decodes the object from bytes.
func (p *TXParamSetupReqPayload) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return errors.New("lorawan: 1 byte of data is expected")
}
if data[0]&(1<<4) > 0 {
p.UplinkDwellTime = DwellTime400ms
}
if data[0]&(1<<5) > 0 {
p.DownlinkDwelltime = DwellTime400ms
}
p.MaxEIRP = data[0] & 15
return nil
}
// DLChannelReqPayload represents the DLChannelReq payload.
type DLChannelReqPayload struct {
ChIndex uint8 `json:"chIndex"`
Freq uint32 `json:"freq"`
}
// MarshalBinary encodes the object into bytes.
func (p DLChannelReqPayload) MarshalBinary() ([]byte, error) {
b := make([]byte, 5) // we need one byte more for PutUint32
if p.Freq/100 >= 16777216 { // 2^24
return b, errors.New("lorawan: max value of Freq is 2^24 - 1")
}
if p.Freq%100 != 0 {
return b, errors.New("lorawan: Freq must be a multiple of 100")
}
b[0] = p.ChIndex
binary.LittleEndian.PutUint32(b[1:5], p.Freq/100)
return b[0:4], nil
}
// UnmarshalBinary decodes the object from bytes.
func (p *DLChannelReqPayload) UnmarshalBinary(data []byte) error {
if len(data) != 4 {
return errors.New("lorawan: 4 bytes of data are expected")
}
p.ChIndex = data[0]
b := make([]byte, 4)
copy(b, data[1:])
p.Freq = binary.LittleEndian.Uint32(b) * 100
return nil
}
// DLChannelAnsPayload represents the DLChannelAns payload.
type DLChannelAnsPayload struct {
UplinkFrequencyExists bool `json:"uplinkFrequencyExists"`
ChannelFrequencyOK bool `json:"channelFrequencyOK"`
}
// MarshalBinary encodes the object into bytes.
func (p DLChannelAnsPayload) MarshalBinary() ([]byte, error) {
var b byte
if p.ChannelFrequencyOK {
b = b ^ 1
}
if p.UplinkFrequencyExists {
b = b ^ (1 << 1)
}
return []byte{b}, nil
}
// UnmarshalBinary decodes the object from bytes.
func (p *DLChannelAnsPayload) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return errors.New("lorawan: 1 byte of data is expected")
}
p.ChannelFrequencyOK = data[0]&1 > 0
p.UplinkFrequencyExists = data[0]&(1<<1) > 0
return nil
}
// PingSlotInfoReqPayload represents the PingSlotInfoReq payload.
type PingSlotInfoReqPayload struct {
Periodicity uint8 `json:"periodicity"`
}
// MarshalBinary encodes the object into bytes.
func (p PingSlotInfoReqPayload) MarshalBinary() ([]byte, error) {
if p.Periodicity > 7 {
return nil, errors.New("lorawan: max value of Periodicity is 7")
}
return []byte{byte(p.Periodicity)}, nil
}
// UnmarshalBinary decodes the object from bytes.
func (p *PingSlotInfoReqPayload) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return errors.New("lorawan: 1 byte of data is expected")
}
// first 3 bits
p.Periodicity = data[0] & ((1 << 2) | (1 << 1) | (1 << 0))
return nil
}
// BeaconFreqReqPayload represents the BeaconFreqReq payload.
type BeaconFreqReqPayload struct {
Frequency uint32 `json:"frequency"`
}
// MarshalBinary encodes the object into bytes.
func (p BeaconFreqReqPayload) MarshalBinary() ([]byte, error) {
if p.Frequency/100 >= 16777216 { // 2^24
return nil, errors.New("lorawan: max value of Frequency is 2^24 - 1")
}
if p.Frequency%100 != 0 {
return nil, errors.New("lorawan: Frequency must be a multiple of 100")
}
// we need 4 bytes for PutUint32
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, p.Frequency/100)
// only return the first 3 bytes
return b[0:3], nil
}
// UnmarshalBinary decodes the object from bytes.
func (p *BeaconFreqReqPayload) UnmarshalBinary(data []byte) error {
if len(data) != 3 {
return errors.New("lorawan: 3 bytes of data are expected")
}
// we need 4 bytes for Uint32
b := make([]byte, 4)
copy(b, data)
p.Frequency = binary.LittleEndian.Uint32(b) * 100
return nil
}
// BeaconFreqAnsPayload represents the BeaconFreqAns payload.
type BeaconFreqAnsPayload struct {
BeaconFrequencyOK bool `json:"beaconFrequencyOK"`
}
// MarshalBinary encodes the object into bytes.
func (p BeaconFreqAnsPayload) MarshalBinary() ([]byte, error) {
var b byte
if p.BeaconFrequencyOK {
b = (1 << 0)
}
return []byte{b}, nil
}
// UnmarshalBinary decodes the object from bytes.
func (p *BeaconFreqAnsPayload) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return errors.New("lorawan: 1 byte of data is expected")
}
p.BeaconFrequencyOK = data[0]&(1<<0) != 0
return nil
}
// PingSlotChannelReqPayload represents the PingSlotChannelReq payload.
type PingSlotChannelReqPayload struct {
Frequency uint32 `json:"frequency"`
DR uint8 `json:"dr"`
}
// MarshalBinary encodes the object into bytes.
func (p PingSlotChannelReqPayload) MarshalBinary() ([]byte, error) {
if p.Frequency/100 >= 16777216 { // 2^24
return nil, errors.New("lorawan: max value of Frequency is 2^24 - 1")
}
if p.Frequency%100 != 0 {
return nil, errors.New("lorawan: Frequency must be a multiple of 100")
}
if p.DR >= 16 { // 2^4
return nil, errors.New("lorawan: max value of DR is 15")
}
// allocate one extra byte for PutUint32
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, p.Frequency/100)
b[3] = byte(p.DR)
return b, nil
}
// UnmarshalBinary decodes the object from bytes.
func (p *PingSlotChannelReqPayload) UnmarshalBinary(data []byte) error {
if len(data) != 4 {
return errors.New("lorawan: 4 bytes of data are expected")
}
b := make([]byte, 4)
copy(b, data)
b[3] = 0
p.Frequency = binary.LittleEndian.Uint32(b) * 100
p.DR = data[3] & ((1 << 3) | (1 << 2) | (1 << 1) | (1 << 0))
return nil
}
// PingSlotChannelAnsPayload represents the PingSlotChannelAns payload.
type PingSlotChannelAnsPayload struct {
DataRateOK bool `json:"dataRateOK"`
ChannelFrequencyOK bool `json:"channelFrequencyOK"`
}
// MarshalBinary encodes the object into bytes.
func (p PingSlotChannelAnsPayload) MarshalBinary() ([]byte, error) {
var b byte
if p.ChannelFrequencyOK {
b = (1 << 0)
}
if p.DataRateOK {
b = b | (1 << 1)
}
return []byte{b}, nil
}
// UnmarshalBinary decodes the object from bytes.
func (p *PingSlotChannelAnsPayload) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return errors.New("lorawan: 1 byte of data is expected")
}
p.ChannelFrequencyOK = data[0]&(1<<0) != 0
p.DataRateOK = data[0]&(1<<1) != 0
return nil
}
// DeviceTimeAnsPayload represents the DeviceTimeAns payload.
type DeviceTimeAnsPayload struct {
TimeSinceGPSEpoch time.Duration `json:"timeSinceGPSEpoch"`
}
// MarshalBinary encodes the object into bytes.
func (p DeviceTimeAnsPayload) MarshalBinary() ([]byte, error) {