-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreatelora.go
189 lines (165 loc) · 5.3 KB
/
createlora.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
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package device
import (
"errors"
"fmt"
"time"
"github.com/arduino/arduino-cloud-cli/arduino/cli"
"github.com/arduino/arduino-cloud-cli/internal/config"
"github.com/arduino/arduino-cloud-cli/internal/iot"
iotclient "github.com/arduino/iot-client-go"
"github.com/sirupsen/logrus"
"go.bug.st/serial"
)
const (
deveuiUploadAttempts = 3
deveuiUploadWait = 1000
serialEUIAttempts = 4
serialEUIWait = 2000
serialEUITimeout = 3500
serialEUIBaudrate = 9600
// dev-eui is an IEEE EUI64 address, so it must have length of 8 bytes.
// It's retrieved as hexadecimal string, thus 16 chars are expected.
deveuiLength = 16
)
// DeviceLoraInfo contains the most interesting
// parameters of an Arduino IoT Cloud LoRa device.
type DeviceLoraInfo struct {
DeviceInfo
AppEUI string `json:"app_eui"`
AppKey string `json:"app_key"`
EUI string `json:"eui"`
}
// CreateLoRaParams contains the parameters needed
// to provision a LoRa device.
type CreateLoraParams struct {
CreateParams
FrequencyPlan string
}
// CreateLora command is used to provision a new LoRa arduino device
// and to add it to Arduino IoT Cloud.
func CreateLora(params *CreateLoraParams, cred *config.Credentials) (*DeviceLoraInfo, error) {
comm, err := cli.NewCommander()
if err != nil {
return nil, err
}
ports, err := comm.BoardList()
if err != nil {
return nil, err
}
board := boardFromPorts(ports, ¶ms.CreateParams)
if board == nil {
err = errors.New("no board found")
return nil, err
}
if !board.isLora() {
return nil, fmt.Errorf(
"board with fqbn %s found at port %s is not a LoRa device."+
" Try the 'create' command instead if it's a device with a supported crypto-chip"+
" or 'create-generic' otherwise",
board.fqbn,
board.address,
)
}
bin, err := downloadProvisioningFile(board.fqbn)
if err != nil {
return nil, err
}
logrus.Infof("%s", "Uploading deveui sketch on the LoRa board")
errMsg := "Error while uploading the LoRa provisioning binary"
err = retry(deveuiUploadAttempts, deveuiUploadWait*time.Millisecond, errMsg, func() error {
return comm.UploadBin(board.fqbn, bin, board.address, board.protocol)
})
if err != nil {
return nil, fmt.Errorf("failed to upload LoRa provisioning binary: %w", err)
}
eui, err := extractEUI(board.address)
if err != nil {
return nil, err
}
iotClient, err := iot.NewClient(cred)
if err != nil {
return nil, err
}
logrus.Info("Creating a new device on the cloud")
dev, err := iotClient.DeviceLoraCreate(params.Name, board.serial, board.dType, eui, params.FrequencyPlan)
if err != nil {
return nil, err
}
devInfo, err := getDeviceLoraInfo(iotClient, dev)
if err != nil {
errDel := iotClient.DeviceDelete(dev.DeviceId)
if errDel != nil { // Oh no
return nil, fmt.Errorf(
"device was successfully provisioned and configured on IoT-API but " +
"now we can't fetch its information nor delete it - please check " +
"it on the web application.\n\nFetch error: " + err.Error() +
"\nDeletion error: " + errDel.Error(),
)
}
return nil, fmt.Errorf("%s: %w", "cannot provision LoRa device", err)
}
return devInfo, nil
}
// extractEUI extracts the EUI from the provisioned lora board.
func extractEUI(port string) (string, error) {
var ser serial.Port
logrus.Infof("%s\n", "Connecting to the board through serial port")
errMsg := "Error while connecting to the board"
err := retry(serialEUIAttempts, serialEUIWait*time.Millisecond, errMsg, func() error {
var err error
ser, err = serial.Open(port, &serial.Mode{BaudRate: serialEUIBaudrate})
return err
})
if err != nil {
return "", fmt.Errorf("failed to extract deveui from the board: %w", err)
}
err = ser.SetReadTimeout(serialEUITimeout * time.Millisecond)
if err != nil {
return "", fmt.Errorf("setting serial read timeout: %w", err)
}
buff := make([]byte, deveuiLength)
n, err := ser.Read(buff)
if err != nil {
return "", fmt.Errorf("reading from serial: %w", err)
}
if n < deveuiLength {
return "", errors.New("cannot read eui from the device")
}
eui := string(buff)
return eui, nil
}
func getDeviceLoraInfo(iotClient *iot.Client, loraDev *iotclient.ArduinoLoradevicev1) (*DeviceLoraInfo, error) {
dev, err := iotClient.DeviceShow(loraDev.DeviceId)
if err != nil {
return nil, fmt.Errorf("cannot retrieve device from the cloud: %w", err)
}
devInfo := &DeviceLoraInfo{
DeviceInfo: DeviceInfo{
Name: dev.Name,
ID: dev.Id,
Board: dev.Type,
Serial: dev.Serial,
FQBN: dev.Fqbn,
},
AppEUI: loraDev.AppEui,
AppKey: loraDev.AppKey,
EUI: loraDev.Eui,
}
return devInfo, nil
}