This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
/
devices_personalize.go
114 lines (95 loc) · 3.19 KB
/
devices_personalize.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
// Copyright © 2016 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package cmd
import (
"strings"
"github.com/TheThingsNetwork/ttn/api"
"github.com/TheThingsNetwork/ttn/core/types"
"github.com/TheThingsNetwork/ttn/ttnctl/util"
"github.com/TheThingsNetwork/ttn/utils/random"
"github.com/apex/log"
"github.com/spf13/cobra"
)
var devicesPersonalizeCmd = &cobra.Command{
Use: "personalize [Device ID] [NwkSKey] [AppSKey]",
Short: "Personalize a device",
Long: `ttnctl devices personalize can be used to personalize a device (ABP).`,
Example: `$ ttnctl devices personalize test
INFO Using Application AppEUI=70B3D57EF0000024 AppID=test
INFO Generating random NwkSKey...
INFO Generating random AppSKey...
INFO Discovering Handler... Handler=ttn-handler-eu
INFO Connecting with Handler... Handler=eu.thethings.network:1904
INFO Requesting DevAddr for device...
INFO Personalized device AppID=test AppSKey=D8DD37B4B709BA76C6FEC62CAD0CCE51 DevAddr=26001ADA DevID=test NwkSKey=3382A3066850293421ED8D392B9BF4DF
`,
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) == 0 {
cmd.UsageFunc()(cmd)
return
}
devID := args[0]
if !api.ValidID(devID) {
ctx.Fatalf("Invalid Device ID") // TODO: Add link to wiki explaining device IDs
}
appID := util.GetAppID(ctx)
var nwkSKey types.NwkSKey
if len(args) > 1 {
nwkSKey, err = types.ParseNwkSKey(args[1])
if err != nil {
ctx.Fatalf("Invalid NwkSKey: %s", err)
}
} else {
ctx.Info("Generating random NwkSKey...")
copy(nwkSKey[:], random.Bytes(16))
}
var appSKey types.AppSKey
if len(args) > 2 {
appSKey, err = types.ParseAppSKey(args[2])
if err != nil {
ctx.Fatalf("Invalid AppSKey: %s", err)
}
} else {
ctx.Info("Generating random AppSKey...")
copy(appSKey[:], random.Bytes(16))
}
conn, manager := util.GetHandlerManager(ctx, appID)
defer conn.Close()
dev, err := manager.GetDevice(appID, devID)
if err != nil {
ctx.WithError(err).Fatal("Could not get existing device.")
}
ctx.Info("Requesting DevAddr for device...")
var constraints []string
if lorawan := dev.GetLorawanDevice(); lorawan != nil && lorawan.ActivationConstraints != "" {
constraints = strings.Split(lorawan.ActivationConstraints, ",")
}
constraints = append(constraints, "abp")
devAddr, err := manager.GetDevAddr(constraints...)
if err != nil {
ctx.WithError(err).Fatal("Could not request device address")
}
var emptyAppKey types.AppKey
dev.GetLorawanDevice().AppKey = &emptyAppKey
dev.GetLorawanDevice().DevAddr = &devAddr
dev.GetLorawanDevice().NwkSKey = &nwkSKey
dev.GetLorawanDevice().AppSKey = &appSKey
dev.GetLorawanDevice().FCntUp = 0
dev.GetLorawanDevice().FCntDown = 0
err = manager.SetDevice(dev)
if err != nil {
ctx.WithError(err).Fatal("Could not update Device")
}
ctx.WithFields(log.Fields{
"AppID": appID,
"DevID": devID,
"DevAddr": devAddr,
"NwkSKey": nwkSKey,
"AppSKey": appSKey,
}).Info("Personalized device")
},
}
func init() {
devicesCmd.AddCommand(devicesPersonalizeCmd)
}