-
-
Notifications
You must be signed in to change notification settings - Fork 545
/
rejoin_param_setup.go
64 lines (55 loc) · 1.94 KB
/
rejoin_param_setup.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
package maccommand
import (
"context"
"fmt"
"github.com/brocaar/chirpstack-network-server/v3/internal/logging"
"github.com/brocaar/chirpstack-network-server/v3/internal/storage"
"github.com/brocaar/lorawan"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
// RequestRejoinParamSetup modifies the rejoin-request interval parameters.
func RequestRejoinParamSetup(maxTimeN, maxCountN int) storage.MACCommandBlock {
return storage.MACCommandBlock{
CID: lorawan.RejoinParamSetupReq,
MACCommands: []lorawan.MACCommand{
{
CID: lorawan.RejoinParamSetupReq,
Payload: &lorawan.RejoinParamSetupReqPayload{
MaxTimeN: uint8(maxTimeN),
MaxCountN: uint8(maxCountN),
},
},
},
}
}
func handleRejoinParamSetupAns(ctx context.Context, ds *storage.DeviceSession, block storage.MACCommandBlock, pendingBlock *storage.MACCommandBlock) ([]storage.MACCommandBlock, error) {
if len(block.MACCommands) != 1 {
return nil, fmt.Errorf("exactly one mac-command expected, got: %d", len(block.MACCommands))
}
if pendingBlock == nil || len(pendingBlock.MACCommands) == 0 {
return nil, errors.New("expected pending mac-command")
}
req := pendingBlock.MACCommands[0].Payload.(*lorawan.RejoinParamSetupReqPayload)
pl, ok := block.MACCommands[0].Payload.(*lorawan.RejoinParamSetupAnsPayload)
if !ok {
return nil, fmt.Errorf("expected *lorawan.RejoinParamSetupAnsPayload, got %T", block.MACCommands[0].Payload)
}
ds.RejoinRequestEnabled = true
ds.RejoinRequestMaxCountN = int(req.MaxCountN)
ds.RejoinRequestMaxTimeN = int(req.MaxTimeN)
if pl.TimeOK {
log.WithFields(log.Fields{
"dev_eui": ds.DevEUI,
"time_ok": pl.TimeOK,
"ctx_id": ctx.Value(logging.ContextIDKey),
}).Info("rejoin_param_setup request acknowledged")
} else {
log.WithFields(log.Fields{
"dev_eui": ds.DevEUI,
"time_ok": pl.TimeOK,
"ctx_id": ctx.Value(logging.ContextIDKey),
}).Warning("rejoin_param_setup request acknowledged")
}
return nil, nil
}