forked from tato123/nrf8001_arm_support
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfu.cpp
376 lines (303 loc) · 11.5 KB
/
dfu.cpp
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <lib_aci.h>
#include <ble_assert.h>
#include <dfu.h>
#include <SPI.h>
static uint8_t state = ST_ANY;
static uint32_t firmware_len;
static uint16_t notify_every;
static uint32_t bytes_received;
static uint16_t packets_received;
static void dfu_notify (aci_state_t *aci_state);
static bool dfu_send (aci_state_t *aci_state, uint8_t *buffer, uint8_t buffer_len);
static uint8_t dfu_data_pkt_handle (aci_state_t *aci_state, aci_evt_t *aci_evt);
static uint8_t dfu_init_pkt_handle (aci_state_t *aci_state, aci_evt_t *aci_evt);
static uint8_t dfu_image_activate (aci_state_t *aci_state, aci_evt_t *aci_evt);
static uint8_t dfu_image_size_set (aci_state_t *aci_state, aci_evt_t *aci_evt);
static uint8_t dfu_image_validate (aci_state_t *aci_state, aci_evt_t *aci_evt);
#define DEBUG
enum
{
OP_CODE_START_DFU = 1, /**< Value of the Op code field for 'Start DFU' command.*/
OP_CODE_RECEIVE_INIT = 2, /**< Value of the Op code field for 'Initialize DFU parameters' command.*/
OP_CODE_RECEIVE_FW = 3, /**< Value of the Op code field for 'Receive firmware image' command.*/
OP_CODE_VALIDATE = 4, /**< Value of the Op code field for 'Validate firmware' command.*/
OP_CODE_ACTIVATE_N_RESET = 5, /**< Value of the Op code field for 'Activate & Reset' command.*/
OP_CODE_SYS_RESET = 6, /**< Value of the Op code field for 'Reset System' command.*/
OP_CODE_IMAGE_SIZE_REQ = 7, /**< Value of the Op code field for 'Report received image size' command.*/
OP_CODE_PKT_RCPT_NOTIF_REQ = 8, /**< Value of the Op code field for 'Request packet receipt notification.*/
OP_CODE_RESPONSE = 16, /**< Value of the Op code field for 'Response.*/
OP_CODE_PKT_RCPT_NOTIF = 17 /**< Value of the Op code field for 'Packets Receipt Notification'.*/
};
/* State transitions */
static uint8_t dfu_begin_init (aci_state_t *aci_state, aci_evt_t *aci_evt)
{
ble_assert (aci_state != NULL);
ble_assert (aci_evt != NULL);
ble_assert (state == ST_RDY);
return ST_RX_INIT_PKT;
}
static uint8_t dfu_begin_transfer (aci_state_t *aci_state, aci_evt_t *aci_evt)
{
ble_assert (aci_state != NULL);
ble_assert (aci_evt != NULL);
ble_assert ((state == ST_RDY) | (state == ST_RX_INIT_PKT));
return ST_RX_DATA_PKT;
}
static uint8_t dfu_data_pkt_handle (aci_state_t *aci_state, aci_evt_t *aci_evt)
{
static uint8_t response[3] = {OP_CODE_RESPONSE, BLE_DFU_RECEIVE_APP_PROCEDURE, BLE_DFU_RESP_VAL_SUCCESS};
ble_assert (aci_state != NULL);
ble_assert (aci_evt != NULL);
ble_assert (state == ST_RX_DATA_PKT);
bytes_received += aci_evt->len-2;
packets_received++;
/* Send notification for every N packets */
if (0 == (packets_received % notify_every))
{
#ifdef DEBUG
Serial.println(F(" Writing notification"));
#endif DEBUG
dfu_notify (aci_state);
}
if (firmware_len == bytes_received)
{
#ifdef DEBUG
Serial.print(F(" All bytes received ("));
Serial.print(bytes_received, DEC);
Serial.println(F(")"));
#endif
dfu_send (aci_state, response, 3);
}
return ST_RX_DATA_PKT;
}
static uint8_t dfu_image_size_set(aci_state_t *aci_state, aci_evt_t *aci_evt)
{
static uint8_t response[3] = {OP_CODE_RESPONSE, BLE_DFU_START_PROCEDURE, BLE_DFU_RESP_VAL_SUCCESS};
ble_assert (aci_state != NULL);
ble_assert (aci_evt != NULL);
ble_assert (state == ST_IDLE);
firmware_len = (uint32_t)aci_evt->params.data_received.rx_data.aci_data[3] << 24 |
(uint32_t)aci_evt->params.data_received.rx_data.aci_data[2] << 16 |
(uint32_t)aci_evt->params.data_received.rx_data.aci_data[1] << 8 |
(uint32_t)aci_evt->params.data_received.rx_data.aci_data[0];
#ifdef DEBUG
Serial.print(F("Received length: "));
Serial.println(firmware_len, DEC);
#endif
/* Write response */
dfu_send (aci_state, response, 3);
return ST_RDY;
}
static uint8_t dfu_image_activate(aci_state_t *aci_state, aci_evt_t *aci_evt)
{
ble_assert (aci_state != NULL);
ble_assert (aci_evt != NULL);
ble_assert (state == ST_FW_VALID);
lib_aci_disconnect(aci_state, ACI_REASON_TERMINATE);
return ST_FW_VALID;
}
static uint8_t dfu_init_pkt_handle (aci_state_t *aci_state, aci_evt_t *aci_evt)
{
ble_assert (aci_state != NULL);
ble_assert (aci_evt != NULL);
ble_assert (state == ST_RX_INIT_PKT);
return ST_RX_INIT_PKT;
}
static uint8_t dfu_image_validate (aci_state_t *aci_state, aci_evt_t *aci_evt)
{
uint8_t new_state;
uint8_t response[3] = {OP_CODE_RESPONSE, BLE_DFU_VALIDATE_PROCEDURE};
ble_assert (aci_state != NULL);
ble_assert (aci_evt != NULL);
ble_assert (state == ST_RX_DATA_PKT);
/* TODO: Implement CRC validation */
if (bytes_received == firmware_len)
{
#ifdef DEBUG
Serial.println(F(" Validation successful"));
#endif
/* Completed successfully */
response[3] = BLE_DFU_RESP_VAL_SUCCESS;
dfu_send(aci_state, response, 3);
state = ST_FW_VALID;
}
else
{
#ifdef DEBUG
Serial.println(F(" Validation failed"));
#endif
/* CRC error */
response[3] = BLE_DFU_RESP_VAL_CRC_ERROR;
dfu_send(aci_state, response, 3);
state = ST_FW_VALID;
}
return state;
}
static uint8_t dfu_notification_set (aci_state_t *aci_state, aci_evt_t *aci_evt)
{
ble_assert (aci_state != NULL);
ble_assert (aci_evt != NULL);
notify_every = (uint16_t)aci_evt->params.data_received.rx_data.aci_data[2] << 8 |
(uint16_t)aci_evt->params.data_received.rx_data.aci_data[1];
#ifdef DEBUG
Serial.print(F(" Remote device requested notification every "));
Serial.print(notify_every, DEC);
Serial.println(F(" packets"));
#endif
return state;
}
dfu_transition_t trans[] = {
{ ST_IDLE, BLE_DFU_PACKET_WRITE, &dfu_image_size_set },
{ ST_RDY, BLE_DFU_RECEIVE_INIT_DATA, &dfu_begin_init },
{ ST_RX_INIT_PKT, BLE_DFU_PACKET_WRITE, &dfu_init_pkt_handle },
{ ST_RDY, BLE_DFU_RECEIVE_APP_DATA, &dfu_begin_transfer },
{ ST_RX_INIT_PKT, BLE_DFU_RECEIVE_APP_DATA, &dfu_begin_transfer },
{ ST_RX_DATA_PKT, BLE_DFU_PACKET_WRITE, &dfu_data_pkt_handle },
{ ST_RX_DATA_PKT, BLE_DFU_VALIDATE, &dfu_image_validate },
{ ST_FW_VALID, BLE_DFU_SYS_RESET, &dfu_image_activate },
{ ST_FW_VALID, BLE_DFU_ACTIVATE_N_RESET, &dfu_image_activate },
{ ST_ANY, BLE_DFU_PKT_RCPT_NOTIF_ENABLED, &dfu_notification_set },
{ ST_ANY, BLE_DFU_PKT_RCPT_NOTIF_DISABLED, &dfu_notification_set }
};
#define TRANS_COUNT (sizeof(trans)/sizeof(*trans))
/* Write receive notification */
static void dfu_notify (aci_state_t *aci_state)
{
uint8_t response[6] = {OP_CODE_PKT_RCPT_NOTIF,
0,
(uint8_t) bytes_received,
(uint8_t) (bytes_received >> 8),
(uint8_t) (bytes_received >> 16),
(uint8_t) (bytes_received >> 24)
};
ble_assert (aci_state != NULL);
dfu_send (aci_state, response, 6);
}
static bool dfu_send (aci_state_t *aci_state, uint8_t *buffer, uint8_t buffer_len)
{
bool status = false;
ble_assert (aci_state != NULL);
ble_assert (buffer != NULL);
if (lib_aci_is_pipe_available(aci_state,
PIPE_DEVICE_FIRMWARE_UPDATE_BLE_SERVICE_DFU_CONTROL_POINT_TX) &&
(aci_state->data_credit_available >= 1))
{
status = lib_aci_send_data(PIPE_DEVICE_FIRMWARE_UPDATE_BLE_SERVICE_DFU_CONTROL_POINT_TX,
buffer,
buffer_len);
if (status)
{
aci_state->data_credit_available--;
}
}
return status;
}
static uint8_t dfu_get_event (aci_state_t *aci_state, aci_evt_t *aci_evt)
{
uint8_t event;
ble_assert (aci_state != NULL);
ble_assert (aci_evt != NULL);
switch (aci_evt->params.data_received.rx_data.pipe_number)
{
case PIPE_DEVICE_FIRMWARE_UPDATE_BLE_SERVICE_DFU_PACKET_RX:
event = BLE_DFU_PACKET_WRITE;
break;
case PIPE_DEVICE_FIRMWARE_UPDATE_BLE_SERVICE_DFU_CONTROL_POINT_RX_ACK_AUTO:
#ifdef DEBUG
Serial.print(F("PIPE_DEVICE_FIRMWARE_UPDATE_BLE_SERVICE_DFU_CONTROL_POINT_RX_ACK_AUTO. Opcode: "));
Serial.println(aci_evt->params.data_received.rx_data.aci_data[0], DEC);
#endif
switch (aci_evt->params.data_received.rx_data.aci_data[0])
{
/* Start DFU */
case OP_CODE_START_DFU:
event = BLE_DFU_START;
break;
/* Initialize DFU parameters */
case OP_CODE_RECEIVE_INIT:
event = BLE_DFU_RECEIVE_INIT_DATA;
break;
/* Receive firmware image */
case OP_CODE_RECEIVE_FW:
event = BLE_DFU_RECEIVE_APP_DATA;
break;
/* Validate firmware image */
case OP_CODE_VALIDATE:
event = BLE_DFU_VALIDATE;
break;
/* Activate image and reset */
case OP_CODE_ACTIVATE_N_RESET:
event = BLE_DFU_ACTIVATE_N_RESET;
break;
/* Reset system */
case OP_CODE_SYS_RESET:
event = BLE_DFU_SYS_RESET;
break;
/* Report received image size */
case OP_CODE_IMAGE_SIZE_REQ:
event = BLE_DFU_BYTES_RECEIVED_SEND;
break;
/* Packet receipt notification request */
case OP_CODE_PKT_RCPT_NOTIF_REQ:
if (aci_evt->params.data_received.rx_data.aci_data[1] == 0)
{
event = BLE_DFU_PKT_RCPT_NOTIF_DISABLED;
}
else
{
event = BLE_DFU_PKT_RCPT_NOTIF_ENABLED;
}
break;
default:
event = EV_ANY;
break;
}
break;
default:
event = EV_ANY;
}
return event;
}
void dfu_initialize (void)
{
#ifdef DEBUG
Serial.println(F(" Initializing DFU state machine"));
#endif
if (true)
{
state = ST_IDLE;
}
else
{
state = ST_INIT_ERROR;
}
}
void dfu_update (aci_state_t *aci_state, aci_evt_t *aci_evt)
{
hal_aci_evt_t aci_data;
uint8_t event;
uint8_t i;
ble_assert (aci_state != NULL);
ble_assert (aci_evt != NULL);
event = dfu_get_event(aci_state, aci_evt);
for (i = 0; i < TRANS_COUNT; i++)
{
if ((state == trans[i].st) || (ST_ANY == trans[i].st))
{
if ((event == trans[i].ev) || (EV_ANY == trans[i].ev))
{
#ifdef DEBUG
Serial.print(F("Transition (state = "));
Serial.print(trans[i].st);
Serial.print(F(", event = "));
Serial.print(trans[i].ev);
Serial.println(F(")"));
#endif
state = (trans[i].fn)(aci_state, aci_evt);
return;
}
}
}
/* Unhandled event */
ble_assert(false);
}