-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdfu_ble.c
362 lines (318 loc) · 8.42 KB
/
dfu_ble.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* nrfdfu - Nordic DFU Upgrade Utility
*
* Copyright (C) 2020 Bruno Randolf (br1@einfach.org)
*
* 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 <stdio.h>
#include <string.h>
#include <unistd.h>
#include "conf.h"
#include "dfu_ble.h"
#include "log.h"
#include "util.h"
#ifndef BLE_SUPPORT
int ble_enter_dfu(const char* interface, const char* address,
enum BLE_ATYPE atype)
{
return false;
}
bool ble_connect_dfu_targ(const char* interface, const char* address,
enum BLE_ATYPE atype)
{
return false;
}
bool ble_write_ctrl(uint8_t* req, size_t len)
{
return false;
}
bool ble_write_data(uint8_t* req, size_t len)
{
return false;
}
const uint8_t* ble_read(void)
{
return NULL;
}
void ble_disconnect(void)
{
}
void ble_fini(void)
{
}
void ble_wait_disconnect(int ms)
{
}
#else
#include <blzlib.h>
#include <blzlib_util.h>
#define DFU_SERVICE_UUID "0000fe59-0000-1000-8000-00805f9b34fb"
#define DFU_CONTROL_UUID "8EC90001-F315-4F60-9FB8-838830DAEA50"
#define DFU_DATA_UUID "8EC90002-F315-4F60-9FB8-838830DAEA50"
#define DFU_BUTTONLESS_UUID "8EC90003-F315-4F60-9FB8-838830DAEA50"
#define SERVICE_CHANGED_UUID "2A05"
#define CONNECT_NORMAL_TRY 3
#define CONNECT_DFUTARG_TRY 10
static bool terminate = false;
static bool buttonless_noti = false;
static bool control_noti = false;
static bool disconnect_noti = false;
static blz_ctx* ctx = NULL;
static blz_dev* dev = NULL;
static blz_serv* srv = NULL;
static blz_char* cp = NULL;
static blz_char* dp = NULL;
static uint8_t recv_buf[200];
void buttonless_notify_handler(const uint8_t* data, size_t len, blz_char* ch,
void* user)
{
if (data[2] != 0x01) {
LOG_ERR("Unexpected response (%zd) %x %x %x", len, data[0], data[1],
data[2]);
}
buttonless_noti = true;
}
void control_notify_handler(const uint8_t* data, size_t len, blz_char* ch,
void* user)
{
memcpy(recv_buf, data, len);
control_noti = true;
if (conf.loglevel >= LL_DEBUG) {
dump_data("RX: ", data, len);
}
}
static void connect_handler(bool conn, uint16_t conn_hdl, bool periph,
void* user)
{
if (!conn) {
// LOG_NOTI("*disconnected*");
disconnect_noti = true;
}
}
static blz_dev* retry_connect(const char* address, enum BLE_ATYPE atype,
int tries)
{
blz_dev* dev = NULL;
int trynum = 0;
do {
if (trynum > 0) {
LOG_ERR("Retry connecting to %s", address);
sleep(5);
}
dev = blz_connect(ctx, address, atype);
} while (dev == NULL && ++trynum < tries && !terminate);
if (trynum >= tries) {
LOG_ERR("Gave up connecting to %s after %d tries", address, trynum);
}
return dev;
}
static bool start_cp_notify()
{
blz_ret r = blz_char_notify_start(cp, control_notify_handler, NULL);
if (r != BLZ_OK) {
LOG_ERR("Could not start CP notification %s", blz_errstr(r));
return false;
}
return true;
}
/** returns 0 on error, 1 on success and 2 when already in bootloader */
int ble_enter_dfu(const char* interface, const char* address,
enum BLE_ATYPE atype)
{
ctx = blz_init(interface);
if (ctx == NULL) {
LOG_ERR("Could not initialize BLE interface '%s'", interface);
return false;
}
blz_set_connect_handler(ctx, connect_handler, NULL);
#ifdef BLSLIB_EXTRAS
if (conf.ble_passkey != NULL) {
bls_set_security_parameters(
ctx, BLSLIB_SEC_FLAG_ALL | BLSLIB_SEC_FLAG_REPA, conf.ble_passkey);
}
#endif
LOG_NOTI("Connecting to %s (%s)...", address, blz_addr_type_str(atype));
dev = retry_connect(address, atype, CONNECT_NORMAL_TRY);
if (dev == NULL) {
return false;
}
#ifdef BLSLIB_EXTRAS
if (conf.ble_passkey != NULL) {
uint8_t flags = bls_start_get_security_status(dev);
LOG_ERR(
"Connection %s secure (encrypted: %d bond: %d mitm: %d lesc: %d "
"repair: %d)",
(flags & BLSLIB_SEC_FLAG_MITM) == BLSLIB_SEC_FLAG_MITM ? "is"
: "is not",
!!(flags & BLSLIB_SEC_FLAG_ENCR), !!(flags & BLSLIB_SEC_FLAG_BOND),
!!(flags & BLSLIB_SEC_FLAG_MITM), !!(flags & BLSLIB_SEC_FLAG_LESC),
!!(flags & BLSLIB_SEC_FLAG_REPA));
}
#endif
srv = blz_get_serv_from_uuid(dev, DFU_SERVICE_UUID);
if (srv == NULL) {
LOG_ERR("DFU Service not found");
return false;
}
blz_char* bch = blz_get_char_from_uuid(srv, DFU_BUTTONLESS_UUID);
if (bch == NULL) {
LOG_ERR("Could not find buttonless DFU UUID");
/* try to find characteristics of DfuTarg */
dp = blz_get_char_from_uuid(srv, DFU_DATA_UUID);
cp = blz_get_char_from_uuid(srv, DFU_CONTROL_UUID);
if (dp != NULL && cp != NULL) {
LOG_NOTI("Device already is in Bootloader");
if (!start_cp_notify()) {
return false;
} else {
return 2; /* already in bootloader */
}
} else {
return false;
}
}
blz_ret r = blz_char_indicate_start(bch, buttonless_notify_handler, NULL);
if (r != BLZ_OK) {
LOG_ERR("Could not start buttonless notification %s", blz_errstr(r));
return false;
}
LOG_NOTI("Enter DFU Bootloader");
uint8_t buf = 0x01;
r = blz_char_write(bch, &buf, 1);
if (r != BLZ_OK) {
LOG_ERR("Could not write buttonless %s", blz_errstr(r));
return false;
}
/* wait until notification is received with confirmation */
blz_loop_wait(ctx, &buttonless_noti, 10000);
if (!buttonless_noti) {
LOG_ERR("Timed out waiting for confirmation");
return false;
}
/* we don't disconnect here, because the device will reset and enter
* bootloader and appear under a new MAC and the connection times out */
/* wait until disconnected */
blz_loop_wait(ctx, &disconnect_noti, 10000);
if (!disconnect_noti) {
LOG_ERR("Timed out waiting for disconnection");
return false;
}
/* free device and service structures (also frees char and
* unsubscribes notifications of bch) */
blz_disconnect(dev);
blz_serv_free(srv);
srv = NULL;
return true;
}
bool ble_connect_dfu_targ(const char* interface, const char* address,
enum BLE_ATYPE atype)
{
/* connect to DfuTarg: increase MAC address by one */
uint8_t* mac = blz_string_to_mac_s(address);
mac[0]++;
const char* macs = blz_mac_to_string_s(mac);
LOG_NOTI("Connecting to DfuTarg (%s)...", macs);
dev = retry_connect(macs, atype, CONNECT_DFUTARG_TRY);
if (dev == NULL) {
return false;
}
srv = blz_get_serv_from_uuid(dev, DFU_SERVICE_UUID);
if (srv == NULL) {
LOG_ERR("DFU Service not found");
return false;
}
dp = blz_get_char_from_uuid(srv, DFU_DATA_UUID);
cp = blz_get_char_from_uuid(srv, DFU_CONTROL_UUID);
if (dp == NULL || cp == NULL) {
LOG_ERR("Could not find DFU UUIDs");
dp = cp = NULL;
return false;
}
LOG_NOTI("DFU characteristics found");
return start_cp_notify();
}
bool ble_write_ctrl(uint8_t* req, size_t len)
{
if (conf.loglevel >= LL_DEBUG) {
dump_data("CP: ", req, len);
}
blz_ret r = blz_char_write(cp, req, len);
if (r != BLZ_OK) {
LOG_ERR("Failed to write CP: %s", blz_errstr(r));
return false;
}
return true;
}
bool ble_write_data(uint8_t* req, size_t len)
{
if (conf.loglevel >= LL_DEBUG) {
dump_data("TX: ", req, len);
}
blz_ret r = blz_char_write_cmd(dp, req, len);
if (r != BLZ_OK) {
LOG_ERR("Failed to write data: %s", blz_errstr(r));
return false;
}
return true;
}
const uint8_t* ble_read(void)
{
/* wait until notification is received */
control_noti = false;
blz_loop_wait(ctx, &control_noti, 10000);
if (!control_noti) {
LOG_ERR("BLE waiting for notification failed");
return NULL;
}
return recv_buf;
}
void ble_disconnect(void)
{
if (cp) {
blz_char_notify_stop(cp);
}
if (dev) {
blz_disconnect(dev);
}
}
void ble_fini(void)
{
if (dev) {
blz_disconnect(dev);
dev = NULL;
}
if (srv) {
blz_serv_free(srv); // also frees chars
srv = NULL;
}
blz_fini(ctx);
ctx = NULL;
buttonless_noti = true;
disconnect_noti = true;
control_noti = true;
terminate = true;
}
void ble_wait_disconnect(int ms)
{
LOG_NOTI("Waiting for Bootloader to disconnect...");
disconnect_noti = false;
blz_loop_wait(ctx, &disconnect_noti, ms);
if (!disconnect_noti) {
LOG_ERR("Timed out waiting for disconnection");
}
/* necessary for blzlib (Bluez) */
ble_disconnect();
}
#endif