27
27
#define MBED_CONF_BLE_DFU_SERVICE_TRACE_ENABLE 0
28
28
#endif
29
29
30
- #if MBED_CONF_BLE_DFU_SERVICE_TRACE_ENABLE || 1
31
- #define TRACE_IF (x ) do { x; } while (0 );
32
- #else
33
- #define TRACE_IF (x )
34
- #endif
35
-
36
30
#if BLE_FEATURE_GATT_SERVER
37
31
38
32
#include " DFUService.h"
@@ -156,59 +150,73 @@ void DFUService::onDataWritten(const GattWriteCallbackParams ¶ms) {
156
150
157
151
void DFUService::onUpdatesEnabled (const GattUpdatesEnabledCallbackParams ¶ms) {
158
152
if (params.attHandle == _dfu_ctrl_char.getValueHandle ()) {
159
- TRACE_IF ( tr_debug (" Updates enabled for control characteristic" ) );
153
+ tr_debug (" Updates enabled for control characteristic" );
160
154
} else if (params.attHandle == _status_char.getValueHandle ()) {
161
- TRACE_IF ( tr_debug (" Updates enabled for status characteristic" ))
155
+ tr_debug (" Updates enabled for status characteristic" );
162
156
}
163
157
}
164
158
165
159
void DFUService::onUpdatesDisabled (const GattUpdatesDisabledCallbackParams ¶ms) {
166
160
if (params.attHandle == _dfu_ctrl_char.getValueHandle ()) {
167
- TRACE_IF ( tr_debug (" Updates disabled for control characteristic" ) );
161
+ tr_debug (" Updates disabled for control characteristic" );
168
162
} else if (params.attHandle == _status_char.getValueHandle ()) {
169
- TRACE_IF ( tr_debug (" Updates disabled for status characteristic" ))
163
+ tr_debug (" Updates disabled for status characteristic" );
170
164
}
171
165
}
172
166
173
167
174
168
void DFUService::set_status (uint8_t status) {
175
- TRACE_IF ( tr_debug (" notifying status: %d" , status) );
169
+ tr_debug (" notifying status: %d" , status);
176
170
_server->write (_status_char.getValueHandle (), &status, 1 , false );
177
171
}
178
172
179
173
void DFUService::set_dfu_ctrl (uint8_t bits) {
180
- TRACE_IF ( tr_debug (" notifying ctrl: %d" , bits) );
174
+ tr_debug (" notifying ctrl: %d" , bits);
181
175
_server->write (_dfu_ctrl_char.getValueHandle (), &bits, 1 , false );
182
176
}
183
177
184
178
void DFUService::onDisconnectionComplete (
185
179
const ble::DisconnectionCompleteEvent &event) {
186
- // TODO determine disconnection behavior in various states
180
+
181
+ /* * Clear the binary stream buffer */
182
+ uint8_t data;
183
+ for (int i = 0 ; i < _bin_stream_buf.size (); i++) {
184
+ _bin_stream_buf.pop (data);
185
+ }
186
+
187
+ /* * Reset state */
188
+ _selected_slot = 0 ;
189
+ _current_offset = 0 ;
190
+ _dfu_control = 0 ;
191
+ _status = 0 ;
192
+ _flush_bin_buf = false ;
193
+ _scheduled_write = 0 ;
194
+ _seq_id = 0 ;
187
195
}
188
196
189
197
void DFUService::on_slot_write_request (GattWriteAuthCallbackParams *params) {
190
198
/* Verify if desired slot is valid (within bounds and has valid BlockDevice */
191
199
uint8_t desired_slot = *params->data ;
192
200
if (!(desired_slot < MBED_CONF_BLE_DFU_SERVICE_MAX_SLOTS) ||
193
201
(_slot_bds[desired_slot] == nullptr )) {
194
- TRACE_IF ( tr_debug (" slot write request: rejected (invalid)" ) );
202
+ tr_debug (" slot write request: rejected (invalid)" );
195
203
params->authorizationReply = (GattAuthCallbackReply_t) AUTH_CALLBACK_REPLY_ATTERR_APP_INVALID_SLOT_NUM;
196
204
} else
197
205
if (!_bin_stream_buf.empty () || _flush_bin_buf) {
198
- TRACE_IF ( tr_debug (" slot write request: rejected (busy)" ) );
206
+ tr_debug (" slot write request: rejected (busy)" );
199
207
/* Reject slot write request and initiate a flush of the binary stream buffer */
200
208
params->authorizationReply = (GattAuthCallbackReply_t) AUTH_CALLBACK_REPLY_ATTERR_APP_BUSY;
201
209
initiate_flush ();
202
210
} else {
203
- TRACE_IF ( tr_debug (" slot write request: accepted" ) );
211
+ tr_debug (" slot write request: accepted" );
204
212
params->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS;
205
213
}
206
214
}
207
215
208
216
void DFUService::on_slot_written (uint8_t new_slot) {
209
217
/* Ignore if selecting the same slot */
210
218
if (_selected_slot != new_slot) {
211
- TRACE_IF ( tr_debug (" slot written: %d" , new_slot) );
219
+ tr_debug (" slot written: %d" , new_slot);
212
220
if (_slot_bds[new_slot] != nullptr ) {
213
221
mbed::ScopedLock<PlatformMutex> lock (_mutex);
214
222
_slot_bds[_selected_slot]->deinit ();
@@ -221,30 +229,30 @@ void DFUService::on_slot_written(uint8_t new_slot) {
221
229
222
230
void DFUService::on_offset_write_request (GattWriteAuthCallbackParams *params) {
223
231
if (!_bin_stream_buf.empty () || _flush_bin_buf) {
224
- TRACE_IF ( tr_debug (" offset write request: rejected (busy)" ) );
232
+ tr_debug (" offset write request: rejected (busy)" );
225
233
/* Reject offset write request and initiate a flush of the binary stream buffer */
226
234
params->authorizationReply = (GattAuthCallbackReply_t) AUTH_CALLBACK_REPLY_ATTERR_APP_BUSY;
227
235
initiate_flush ();
228
236
} else {
229
- TRACE_IF ( tr_debug (" offset write request: accepted" ) );
237
+ tr_debug (" offset write request: accepted" );
230
238
params->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS;
231
239
}
232
240
}
233
241
234
242
void DFUService::on_offset_written (uint32_t new_offset) {
235
- TRACE_IF ( tr_debug (" offset written: %lu" , new_offset) );
243
+ tr_debug (" offset written: %lu" , new_offset);
236
244
mbed::ScopedLock<PlatformMutex> lock (_mutex);
237
245
_current_offset = new_offset;
238
246
}
239
247
240
248
void DFUService::on_bds_written (mbed::Span<const uint8_t > data) {
241
249
242
250
uint8_t seq_id = *data.data ();
243
- TRACE_IF ( tr_debug (" bds written, sequence num: %d, %i bytes in payload" , seq_id, data.size ()-1 ) );
251
+ tr_debug (" bds written, sequence num: %d, %i bytes in payload" , seq_id, data.size ()-1 );
244
252
245
253
/* Ignore 0-length writes */
246
254
if ((data.size () - 1 ) == 0 ) {
247
- TRACE_IF ( tr_warn (" zero-length packet written, ignoring" ) );
255
+ tr_warn (" zero-length packet written, ignoring" );
248
256
return ;
249
257
}
250
258
@@ -263,7 +271,7 @@ void DFUService::on_bds_written(mbed::Span<const uint8_t> data) {
263
271
schedule_write ();
264
272
} else {
265
273
/* Otherwise, notify the client that the expected sequence ID did not match using the status characteristic */
266
- TRACE_IF ( tr_warn (" sequence number does not match; expected: %d, actual: %d" , _seq_id, seq_id) );
274
+ tr_warn (" sequence number does not match; expected: %d, actual: %d" , _seq_id, seq_id);
267
275
set_status (DFU_STATE_SYNC_LOSS_BIT | _seq_id);
268
276
}
269
277
}
@@ -276,24 +284,24 @@ void DFUService::on_dfu_ctrl_write_request(
276
284
277
285
if (change.get_changed_bits () & DFU_CTRL_READONLY_BITS) {
278
286
/* Reject writes that modify read-only bits */
279
- TRACE_IF ( tr_debug (" dfu_ctrl write request: rejected (read-only)" ) );
287
+ tr_debug (" dfu_ctrl write request: rejected (read-only)" );
280
288
params->authorizationReply = (GattAuthCallbackReply_t) AUTH_CALLBACK_REPLY_ATTERR_APP_READONLY;
281
289
return ;
282
290
}
283
291
284
292
if (_ctrl_req_cb) {
285
293
/* Forward request to the application */
286
294
params->authorizationReply = _ctrl_req_cb (change);
287
- TRACE_IF ( tr_debug (" dfu_ctrl write request: accepted (by application)" ) );
295
+ tr_debug (" dfu_ctrl write request: accepted (by application)" );
288
296
} else {
289
297
/* If no application handler, accept by default */
290
- TRACE_IF ( tr_debug (" dfu_ctrl write request: accepted" ) );
298
+ tr_debug (" dfu_ctrl write request: accepted" );
291
299
params->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS;
292
300
}
293
301
}
294
302
295
303
void DFUService::on_dfu_ctrl_written (uint8_t new_ctrl) {
296
- TRACE_IF ( tr_debug (" dfu_ctrl written: %d" , new_ctrl) );
304
+ tr_debug (" dfu_ctrl written: %d" , new_ctrl);
297
305
mbed::ScopedLock<PlatformMutex> lock (_mutex);
298
306
ControlChange change (*this , new_ctrl);
299
307
/* Call application handler for control updates, if available */
@@ -302,7 +310,7 @@ void DFUService::on_dfu_ctrl_written(uint8_t new_ctrl) {
302
310
}
303
311
304
312
if (change.get_changed_bits () & DFU_CTRL_ENABLE_BIT) {
305
- TRACE_IF ( tr_debug (" dfu mode %s" , (change.value () & DFU_CTRL_ENABLE_BIT) ? " enabled" : " aborted" ) );
313
+ tr_debug (" dfu mode %s" , (change.value () & DFU_CTRL_ENABLE_BIT) ? " enabled" : " aborted" );
306
314
307
315
if (change.value () & DFU_CTRL_ENABLE_BIT) {
308
316
/* If DFU is being enabled, clear the currently-selected update slot */
@@ -311,19 +319,19 @@ void DFUService::on_dfu_ctrl_written(uint8_t new_ctrl) {
311
319
}
312
320
313
321
if (change.get_changed_bits () & DFU_CTRL_DELTA_MODE_EN_BIT) {
314
- TRACE_IF ( tr_debug (" delta mode %s" , (change.value () & DFU_CTRL_DELTA_MODE_EN_BIT) ? " enabled" : " disabled" ) );
322
+ tr_debug (" delta mode %s" , (change.value () & DFU_CTRL_DELTA_MODE_EN_BIT) ? " enabled" : " disabled" );
315
323
}
316
324
317
325
if (change.get_changed_bits () & DFU_CTRL_COMMIT_BIT) {
318
- TRACE_IF ( tr_debug (" dfu commit" ) );
326
+ tr_debug (" dfu commit" );
319
327
}
320
328
321
329
_dfu_control = new_ctrl;
322
330
}
323
331
324
332
void DFUService::init_selected_slot (void ) {
325
333
mbed::ScopedLock<PlatformMutex> lock (_mutex); // TODO mutex lock necessary here?
326
- TRACE_IF ( tr_debug (" initializing slot %d" , _selected_slot))
334
+ tr_debug (" initializing slot %d" , _selected_slot);
327
335
mbed::BlockDevice* slot = _slot_bds[_selected_slot];
328
336
slot->init ();
329
337
slot->erase (0 , slot->size ());
@@ -344,9 +352,9 @@ void DFUService::process_buffer(void) {
344
352
* have to program byte-by-byte. This would likely be a significant hit in speed.
345
353
*/
346
354
bd_size_t write_size = (_bin_stream_buf.size () / slot->get_program_size ()) * slot->get_program_size ();
347
- TRACE_IF ( tr_debug (" processing buffer: %lu => %lu" ,
355
+ tr_debug (" processing buffer: %lu => %lu" ,
348
356
_bin_stream_buf.size (),
349
- write_size)) ;
357
+ write_size);
350
358
if (write_size == 0 ) {
351
359
/* Skip 0-length writes */
352
360
_scheduled_write = 0 ;
@@ -356,17 +364,17 @@ void DFUService::process_buffer(void) {
356
364
_bin_stream_buf.pop (temp_buf, write_size);
357
365
int result = slot->program (temp_buf, _current_offset, write_size);
358
366
if (result) {
359
- TRACE_IF ( tr_err (" programming memory error: %d" , result) );
367
+ tr_err (" programming memory error: %d" , result);
360
368
set_status (DFU_STATE_FLASH_ERROR);
361
369
}
362
370
_current_offset += write_size;
363
371
delete[] temp_buf;
364
372
365
373
/* If the buffer isn't empty and a flush should be performed, pad the write */
366
374
if (_bin_stream_buf.size () && _flush_bin_buf) {
367
- TRACE_IF ( tr_debug (" flushing buffer: %lu => %lu" ,
375
+ tr_debug (" flushing buffer: %lu => %lu" ,
368
376
_bin_stream_buf.size (),
369
- write_size)) ;
377
+ write_size);
370
378
write_size = slot->get_program_size ();
371
379
temp_buf = new uint8_t [write_size];
372
380
/* Pad the write buffer with the BD's erase value */
0 commit comments