Skip to content

Commit 72c44ba

Browse files
committed
Remove TRACE_IF and add reset state to disconnect handler
1 parent c1f1853 commit 72c44ba

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

services/src/DFUService/DFUService.cpp

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@
2727
#define MBED_CONF_BLE_DFU_SERVICE_TRACE_ENABLE 0
2828
#endif
2929

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-
3630
#if BLE_FEATURE_GATT_SERVER
3731

3832
#include "DFUService.h"
@@ -156,59 +150,73 @@ void DFUService::onDataWritten(const GattWriteCallbackParams &params) {
156150

157151
void DFUService::onUpdatesEnabled(const GattUpdatesEnabledCallbackParams &params) {
158152
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");
160154
} 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");
162156
}
163157
}
164158

165159
void DFUService::onUpdatesDisabled(const GattUpdatesDisabledCallbackParams &params) {
166160
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");
168162
} 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");
170164
}
171165
}
172166

173167

174168
void DFUService::set_status(uint8_t status) {
175-
TRACE_IF(tr_debug("notifying status: %d", status));
169+
tr_debug("notifying status: %d", status);
176170
_server->write(_status_char.getValueHandle(), &status, 1, false);
177171
}
178172

179173
void DFUService::set_dfu_ctrl(uint8_t bits) {
180-
TRACE_IF(tr_debug("notifying ctrl: %d", bits));
174+
tr_debug("notifying ctrl: %d", bits);
181175
_server->write(_dfu_ctrl_char.getValueHandle(), &bits, 1, false);
182176
}
183177

184178
void DFUService::onDisconnectionComplete(
185179
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;
187195
}
188196

189197
void DFUService::on_slot_write_request(GattWriteAuthCallbackParams *params) {
190198
/* Verify if desired slot is valid (within bounds and has valid BlockDevice */
191199
uint8_t desired_slot = *params->data;
192200
if(!(desired_slot < MBED_CONF_BLE_DFU_SERVICE_MAX_SLOTS) ||
193201
(_slot_bds[desired_slot] == nullptr)) {
194-
TRACE_IF(tr_debug("slot write request: rejected (invalid)"));
202+
tr_debug("slot write request: rejected (invalid)");
195203
params->authorizationReply = (GattAuthCallbackReply_t) AUTH_CALLBACK_REPLY_ATTERR_APP_INVALID_SLOT_NUM;
196204
} else
197205
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)");
199207
/* Reject slot write request and initiate a flush of the binary stream buffer */
200208
params->authorizationReply = (GattAuthCallbackReply_t) AUTH_CALLBACK_REPLY_ATTERR_APP_BUSY;
201209
initiate_flush();
202210
} else {
203-
TRACE_IF(tr_debug("slot write request: accepted"));
211+
tr_debug("slot write request: accepted");
204212
params->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS;
205213
}
206214
}
207215

208216
void DFUService::on_slot_written(uint8_t new_slot) {
209217
/* Ignore if selecting the same slot */
210218
if(_selected_slot != new_slot) {
211-
TRACE_IF(tr_debug("slot written: %d", new_slot));
219+
tr_debug("slot written: %d", new_slot);
212220
if(_slot_bds[new_slot] != nullptr) {
213221
mbed::ScopedLock<PlatformMutex> lock(_mutex);
214222
_slot_bds[_selected_slot]->deinit();
@@ -221,30 +229,30 @@ void DFUService::on_slot_written(uint8_t new_slot) {
221229

222230
void DFUService::on_offset_write_request(GattWriteAuthCallbackParams *params) {
223231
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)");
225233
/* Reject offset write request and initiate a flush of the binary stream buffer */
226234
params->authorizationReply = (GattAuthCallbackReply_t) AUTH_CALLBACK_REPLY_ATTERR_APP_BUSY;
227235
initiate_flush();
228236
} else {
229-
TRACE_IF(tr_debug("offset write request: accepted"));
237+
tr_debug("offset write request: accepted");
230238
params->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS;
231239
}
232240
}
233241

234242
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);
236244
mbed::ScopedLock<PlatformMutex> lock(_mutex);
237245
_current_offset = new_offset;
238246
}
239247

240248
void DFUService::on_bds_written(mbed::Span<const uint8_t> data) {
241249

242250
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);
244252

245253
/* Ignore 0-length writes */
246254
if((data.size() - 1) == 0) {
247-
TRACE_IF(tr_warn("zero-length packet written, ignoring"));
255+
tr_warn("zero-length packet written, ignoring");
248256
return;
249257
}
250258

@@ -263,7 +271,7 @@ void DFUService::on_bds_written(mbed::Span<const uint8_t> data) {
263271
schedule_write();
264272
} else {
265273
/* 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);
267275
set_status(DFU_STATE_SYNC_LOSS_BIT | _seq_id);
268276
}
269277
}
@@ -276,24 +284,24 @@ void DFUService::on_dfu_ctrl_write_request(
276284

277285
if(change.get_changed_bits() & DFU_CTRL_READONLY_BITS) {
278286
/* 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)");
280288
params->authorizationReply = (GattAuthCallbackReply_t) AUTH_CALLBACK_REPLY_ATTERR_APP_READONLY;
281289
return;
282290
}
283291

284292
if(_ctrl_req_cb) {
285293
/* Forward request to the application */
286294
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)");
288296
} else {
289297
/* 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");
291299
params->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS;
292300
}
293301
}
294302

295303
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);
297305
mbed::ScopedLock<PlatformMutex> lock(_mutex);
298306
ControlChange change(*this, new_ctrl);
299307
/* Call application handler for control updates, if available */
@@ -302,7 +310,7 @@ void DFUService::on_dfu_ctrl_written(uint8_t new_ctrl) {
302310
}
303311

304312
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");
306314

307315
if(change.value() & DFU_CTRL_ENABLE_BIT) {
308316
/* 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) {
311319
}
312320

313321
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");
315323
}
316324

317325
if(change.get_changed_bits() & DFU_CTRL_COMMIT_BIT) {
318-
TRACE_IF(tr_debug("dfu commit"));
326+
tr_debug("dfu commit");
319327
}
320328

321329
_dfu_control = new_ctrl;
322330
}
323331

324332
void DFUService::init_selected_slot(void) {
325333
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);
327335
mbed::BlockDevice* slot = _slot_bds[_selected_slot];
328336
slot->init();
329337
slot->erase(0, slot->size());
@@ -344,9 +352,9 @@ void DFUService::process_buffer(void) {
344352
* have to program byte-by-byte. This would likely be a significant hit in speed.
345353
*/
346354
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",
348356
_bin_stream_buf.size(),
349-
write_size));
357+
write_size);
350358
if(write_size == 0) {
351359
/* Skip 0-length writes */
352360
_scheduled_write = 0;
@@ -356,17 +364,17 @@ void DFUService::process_buffer(void) {
356364
_bin_stream_buf.pop(temp_buf, write_size);
357365
int result = slot->program(temp_buf, _current_offset, write_size);
358366
if(result) {
359-
TRACE_IF(tr_err("programming memory error: %d", result));
367+
tr_err("programming memory error: %d", result);
360368
set_status(DFU_STATE_FLASH_ERROR);
361369
}
362370
_current_offset += write_size;
363371
delete[] temp_buf;
364372

365373
/* If the buffer isn't empty and a flush should be performed, pad the write */
366374
if(_bin_stream_buf.size() && _flush_bin_buf) {
367-
TRACE_IF(tr_debug("flushing buffer: %lu => %lu",
375+
tr_debug("flushing buffer: %lu => %lu",
368376
_bin_stream_buf.size(),
369-
write_size));
377+
write_size);
370378
write_size = slot->get_program_size();
371379
temp_buf = new uint8_t[write_size];
372380
/* Pad the write buffer with the BD's erase value */

0 commit comments

Comments
 (0)