-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprovision.go
235 lines (209 loc) · 6.11 KB
/
provision.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
// 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 (
"encoding/hex"
"fmt"
"path/filepath"
"strconv"
"time"
"github.com/arduino/arduino-cloud-cli/arduino"
"github.com/arduino/arduino-cloud-cli/internal/binary"
"github.com/arduino/arduino-cloud-cli/internal/iot"
"github.com/arduino/arduino-cloud-cli/internal/serial"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
)
// downloadProvisioningFile downloads and returns the absolute path
// of the provisioning binary corresponding to the passed fqbn.
func downloadProvisioningFile(fqbn string) (string, error) {
index, err := binary.LoadIndex()
if err != nil {
return "", err
}
bin := index.FindProvisionBin(fqbn)
if bin == nil {
return "", fmt.Errorf("provisioning binary for board %s not found", fqbn)
}
bytes, err := binary.Download(bin)
if err != nil {
return "", fmt.Errorf("downloading provisioning binary: %w", err)
}
// Save provision binary always in the same temporary folder to
// avoid wasting user's storage.
filename := filepath.Base(bin.URL)
path := paths.TempDir().Join("cloud-cli").Join(filename)
path.Parent().MkdirAll()
if err = path.WriteFile(bytes); err != nil {
return "", fmt.Errorf("writing provisioning binary: %w", err)
}
p, err := path.Abs()
if err != nil {
return "", fmt.Errorf("cannot retrieve absolute path of downloaded binary: %w", err)
}
return p.String(), nil
}
// provision is responsible for running the provisioning
// procedures for boards with crypto-chip.
type provision struct {
arduino.Commander
iot.Client
ser *serial.Serial
board *board
id string
}
// run provisioning procedure for boards with crypto-chip.
func (p provision) run() error {
bin, err := downloadProvisioningFile(p.board.fqbn)
if err != nil {
return err
}
logrus.Infof("%s\n", "Uploading provisioning sketch on the board")
time.Sleep(500 * time.Millisecond)
// Try to upload the provisioning sketch
errMsg := "Error while uploading the provisioning sketch"
err = retry(5, time.Millisecond*1000, errMsg, func() error {
//serialutils.Reset(dev.port, true, nil)
return p.UploadBin(p.board.fqbn, bin, p.board.port)
})
if err != nil {
return err
}
logrus.Infof("%s\n", "Connecting to the board through serial port")
// Try to connect to board through the serial port
time.Sleep(1500 * time.Millisecond)
p.ser = serial.NewSerial()
errMsg = "Error while connecting to the board"
err = retry(5, time.Millisecond*1000, errMsg, func() error {
return p.ser.Connect(p.board.port)
})
if err != nil {
return err
}
defer p.ser.Close()
// Wait some time before using the serial port
time.Sleep(2000 * time.Millisecond)
logrus.Infof("%s\n\n", "Connected to the board")
// Send configuration commands to the board
err = p.configBoard()
if err != nil {
return err
}
logrus.Infof("%s\n\n", "Device provisioning successful")
return nil
}
func (p provision) configBoard() error {
logrus.Info("Receiving the certificate")
csr, err := p.ser.SendReceive(serial.CSR, []byte(p.id))
if err != nil {
return err
}
cert, err := p.CertificateCreate(p.id, string(csr))
if err != nil {
return err
}
logrus.Info("Requesting begin storage")
err = p.ser.Send(serial.BeginStorage, nil)
if err != nil {
return err
}
s := strconv.Itoa(cert.NotBefore.Year())
logrus.Info("Sending year: ", s)
err = p.ser.Send(serial.SetYear, []byte(s))
if err != nil {
return err
}
s = fmt.Sprintf("%02d", int(cert.NotBefore.Month()))
logrus.Info("Sending month: ", s)
err = p.ser.Send(serial.SetMonth, []byte(s))
if err != nil {
return err
}
s = fmt.Sprintf("%02d", cert.NotBefore.Day())
logrus.Info("Sending day: ", s)
err = p.ser.Send(serial.SetDay, []byte(s))
if err != nil {
return err
}
s = fmt.Sprintf("%02d", cert.NotBefore.Hour())
logrus.Info("Sending hour: ", s)
err = p.ser.Send(serial.SetHour, []byte(s))
if err != nil {
return err
}
s = strconv.Itoa(31)
logrus.Info("Sending validity: ", s)
err = p.ser.Send(serial.SetValidity, []byte(s))
if err != nil {
return err
}
logrus.Info("Sending certificate serial")
b, err := hex.DecodeString(cert.Serial)
if err != nil {
err = fmt.Errorf("%s: %w", "decoding certificate serial", err)
return err
}
err = p.ser.Send(serial.SetCertSerial, b)
if err != nil {
return err
}
logrus.Info("Sending certificate authority key")
b, err = hex.DecodeString(cert.AuthorityKeyIdentifier)
if err != nil {
err = fmt.Errorf("%s: %w", "decoding certificate authority key id", err)
return err
}
err = p.ser.Send(serial.SetAuthKey, b)
if err != nil {
return err
}
logrus.Info("Sending certificate signature")
b, err = hex.DecodeString(cert.SignatureAsn1X + cert.SignatureAsn1Y)
if err != nil {
err = fmt.Errorf("%s: %w", "decoding certificate signature", err)
return err
}
err = p.ser.Send(serial.SetSignature, b)
if err != nil {
return err
}
time.Sleep(time.Second)
logrus.Info("Requesting end storage")
err = p.ser.Send(serial.EndStorage, nil)
if err != nil {
return err
}
time.Sleep(2 * time.Second)
logrus.Info("Requesting certificate reconstruction")
err = p.ser.Send(serial.ReconstructCert, nil)
if err != nil {
return err
}
return nil
}
func retry(tries int, sleep time.Duration, errMsg string, fun func() error) error {
var err error
for n := 0; n < tries; n++ {
err = fun()
if err == nil {
break
}
logrus.Warningf("%s: %s: %s", errMsg, err.Error(), "\nTrying again...")
time.Sleep(sleep)
}
return err
}