-
Notifications
You must be signed in to change notification settings - Fork 11
/
dfu_serial.c
216 lines (187 loc) · 5.01 KB
/
dfu_serial.c
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
/*
* nrfdfu - Nordic DFU Upgrade Utility
*
* Copyright (C) 2019 Bruno Randolf ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#include <unistd.h>
#include "conf.h"
#include "dfu.h"
#include "dfu_serial.h"
#include "log.h"
#include "serialtty.h"
#include "slip.h"
#include "util.h"
#define DFU_SERIAL_BAUDRATE 115200
#define MAX_READ_TRIES SLIP_BUF_SIZE
static uint8_t buf[SLIP_BUF_SIZE];
static int ser_fd = -1;
static bool terminate;
bool ser_encode_write(uint8_t* req, size_t len, int timeout_sec)
{
uint32_t slip_len;
slip_encode(buf, (uint8_t*)req, len, &slip_len);
bool b = serial_write(ser_fd, (const char*)buf, slip_len, timeout_sec);
if (b && conf.loglevel >= LL_DEBUG) {
dump_data("TX: ", req, len);
}
return b;
}
const uint8_t* ser_read_decode(int timeout_sec)
{
ssize_t ret;
int end = 0;
char read_buf;
int read_tries = 0;
bool timeout;
slip_t slip = {.p_buffer = buf,
.current_index = 0,
.buffer_len = BUF_SIZE,
.state = SLIP_STATE_DECODING};
do {
read_tries++;
timeout = serial_wait_read_ready(ser_fd, timeout_sec);
if (timeout) {
LOG_INF("Timeout on Serial RX");
break;
}
ret = read(ser_fd, &read_buf, 1);
if (ret < 0 && errno != EAGAIN) {
LOG_ERR("Read error: %d %s", errno, strerror(errno));
break;
} else if (ret > 0) {
end = slip_decode_add_byte(&slip, read_buf);
}
} while (end != 1 && read_tries < MAX_READ_TRIES && !terminate);
if (conf.loglevel >= LL_DEBUG) {
dump_data("RX: ", slip.p_buffer, slip.current_index);
}
return (end == 1 ? buf : NULL);
}
static bool ser_enter_dfu_cmd(void)
{
char b[200];
serial_set_baudrate(ser_fd, conf.serspeed);
/* first read and discard anything that came before */
read(ser_fd, b, 200);
LOG_INF("Sending command to enter DFU mode: '%s'", conf.dfucmd);
if (conf.dfucmd_hex) {
hex_to_bin(conf.dfucmd, (uint8_t*)b, strlen(conf.dfucmd));
size_t len = strlen(conf.dfucmd) / 2;
serial_write(ser_fd, b, len, 1);
} else {
/* it looks like the first two characters written are lost...
* and we need \r to enter CLI */
serial_write(ser_fd, "\r\r\r", 3, 1);
serial_write(ser_fd, conf.dfucmd, strlen(conf.dfucmd), 1);
serial_write(ser_fd, "\r", 1, 1);
}
if (conf.ser_acm) {
/* device sends reply but it's easy to miss, since the serial port
* disappears inmediately afterwards, so we ignore it and just reopen
* the port */
ser_reopen(2);
return true;
}
sleep(1);
int ret = read(ser_fd, b, 200);
if (ret > 0) {
if (!conf.dfucmd_hex) {
/* debug output reply */
b[ret--] = '\0';
/* remove trailing \r \n */
while (b[ret] == '\r' || b[ret] == '\n') {
b[ret--] = '\0';
}
/* remove \r \n and zero from the beginning */
ret = 0;
while ((b[ret] == '\r' || b[ret] == '\n' || b[ret] == '\0')
&& ret < sizeof(b)) {
ret++;
}
LOG_INF("Device replied: '%s' (%d)", b + ret, ret);
} else {
LOG_INF("Device replied with %d bytes", ret);
}
serial_set_baudrate(ser_fd, DFU_SERIAL_BAUDRATE);
return true;
} else {
LOG_INF("Device didn't repy (%d)", ret);
serial_set_baudrate(ser_fd, DFU_SERIAL_BAUDRATE);
return false;
}
}
bool ser_enter_dfu(void)
{
ser_fd = serial_init(conf.serport, DFU_SERIAL_BAUDRATE);
if (ser_fd <= 0) {
return false;
}
/* first check if Bootloader responds to Ping */
LOG_NOTI_("Waiting for device to be ready: ");
int ntry = 0;
bool ret = false;
do {
if (conf.dfucmd) {
ret = ser_enter_dfu_cmd();
if (terminate) {
ret = false;
break;
}
sleep(1);
if (!ret) {
/* if dfu command failed, try ping, it will
* usually fail with "Opcode not supported"
* because of the text we sent before, but then
* the next one below can succeed */
ret = dfu_ping();
}
} else {
sleep(1);
}
if (conf.loglevel < LL_INFO) {
printf(".");
fflush(stdout);
}
if (!terminate) {
ret = dfu_ping();
}
} while (!ret && ++ntry < conf.timeout && !terminate);
LOG_NL(LL_NOTICE);
if (ntry >= conf.timeout) {
LOG_NOTI("Device didn't respond after %d tries", conf.timeout);
return false;
}
return ret;
}
void ser_fini(void)
{
terminate = true;
if (ser_fd > 0) {
serial_fini(ser_fd);
ser_fd = -1;
}
}
void ser_reopen(int sleep_time)
{
LOG_NOTI("Reopen %s in %d seconds...", conf.serport, sleep_time);
serial_fini(ser_fd);
sleep(sleep_time);
ser_fd = serial_init(conf.serport, DFU_SERIAL_BAUDRATE);
}