-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsara.go
230 lines (198 loc) · 5.44 KB
/
sara.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
/*
arduino-fwuploader
Copyright (c) 2021 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package flasher
import (
"fmt"
"io"
"strconv"
"strings"
"time"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
"go.bug.st/serial"
)
func NewSaraFlasher(portAddress string) (*SaraFlasher, error) {
port, err := openSerial(portAddress)
if err != nil {
logrus.Error(err)
return nil, err
}
// Magic numbers ¯\_(ツ)_/¯
return &SaraFlasher{port: port, payloadSize: 128}, nil
}
type SaraFlasher struct {
port serial.Port
payloadSize int
}
func (f *SaraFlasher) FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error {
logrus.Infof("Flashing firmware %s", firmwareFile)
flasherOut.Write([]byte(fmt.Sprintf("Flashing firmware %s\n", firmwareFile)))
data, err := firmwareFile.ReadFile()
if err != nil {
logrus.Error(err)
return err
}
_, err = f.expectMinBytes("AT+ULSTFILE", "+ULSTFILE:", 1000, 0)
if err != nil {
logrus.Error(err)
return err
}
_, err = f.expectMinBytes("AT+UDWNFILE=\"UPDATE.BIN\","+strconv.Itoa(len(data))+",\"FOAT\"", ">", 20000, 0)
if err != nil {
logrus.Error(err)
return err
}
firmwareOffset := 0x0000
err = f.flashChunk(firmwareOffset, data)
if err != nil {
logrus.Error(err)
return err
}
time.Sleep(1 * time.Second)
_, err = f.expectMinBytes("", "OK", 1000, 0)
if err != nil {
logrus.Error(err)
return err
}
_, err = f.expectMinBytes("AT+UFWINSTALL", "OK", 60000, 0)
if err != nil {
logrus.Error(err)
return err
}
time.Sleep(10 * time.Second)
// wait up to 20 minutes trying to ping the module. After 20 minutes signal the error
start := time.Now()
for time.Since(start) < time.Minute*20 {
err = f.hello()
if err == nil {
return nil
}
time.Sleep(1 * time.Second)
}
if err != nil {
logrus.Error(err)
}
logrus.Infof("Flashed all the things")
flasherOut.Write([]byte("Flashed all the things\n"))
return nil
}
func (f *SaraFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string, _ io.Writer) error {
return fmt.Errorf("not supported by SaraFlasher")
}
func (f *SaraFlasher) Close() error {
return f.port.Close()
}
func (f *SaraFlasher) hello() error {
f.expectMinBytes("ATE0", "OK", 100, 0)
f.expectMinBytes("ATE0", "OK", 100, 0)
f.expectMinBytes("ATE0", "OK", 100, 0)
_, err := f.expectMinBytes("AT", "OK", 100, 0)
if err != nil {
logrus.Error(err)
}
return err
}
func (f *SaraFlasher) write(address uint32, buffer []byte) error {
return f.sendCommand(CommandData{
Payload: buffer,
})
}
func (f *SaraFlasher) flashChunk(offset int, buffer []byte) error {
bufferLength := len(buffer)
for i := 0; i < bufferLength; i += f.payloadSize {
logrus.Debugf("Flashing chunk: %s%%", strconv.Itoa((i*100)/bufferLength))
start := i
end := i + f.payloadSize
if end > bufferLength {
end = bufferLength
}
if err := f.write(uint32(offset+i), buffer[start:end]); err != nil {
logrus.Error(err)
return err
}
//time.Sleep(1 * time.Millisecond)
}
return nil
}
func (f *SaraFlasher) getMaximumPayloadSize() (uint16, error) {
return 0, fmt.Errorf("not supported by SaraFlasher")
}
func (f *SaraFlasher) serialFillBuffer(buffer []byte) error {
read := 0
for read < len(buffer) {
n, err := f.port.Read(buffer[read:])
if err != nil {
logrus.Error(err)
return err
}
if n == 0 {
err = &FlasherError{err: "Serial port closed unexpectedly"}
logrus.Error(err)
return err
}
read += n
}
return nil
}
func (f *SaraFlasher) sendCommand(data CommandData) error {
logrus.Debugf("sending command data %s", data)
if data.Payload != nil {
for {
if sent, err := f.port.Write(data.Payload); err != nil {
logrus.Error(err)
return err
} else if sent < len(data.Payload) {
data.Payload = data.Payload[sent:]
} else {
break
}
}
}
return nil
}
func (f *SaraFlasher) expectMinBytes(buffer string, response string, timeout int, min_bytes int) (string, error) {
err := f.sendCommand(CommandData{
Payload: []byte(buffer + "\r\n"),
})
if err != nil {
logrus.Error(err)
return "", err
}
// Receive response
var res []byte
n := 0
start := time.Now()
for (time.Since(start) < time.Duration(timeout)*time.Millisecond && !strings.Contains(string(res), response)) || (len(res) < min_bytes) {
data := 0
partial := make([]byte, 65535)
data, err = f.port.Read(partial)
res = append(res, partial[:data]...)
n += data
if err != nil {
logrus.Error(err)
return "", err
}
}
if !strings.Contains(string(res), response) {
err = FlasherError{err: fmt.Sprintf("Expected %s, got %s", response, res)}
logrus.Error(err)
return string(res), err
}
return string(res), nil
}
func (f *SaraFlasher) getFirmwareVersion() (string, error) {
return f.expectMinBytes("ATI9", "05.06,A.02.", 100, 25)
}