Skip to content

Commit 12acdb7

Browse files
committed
Streamlined error handling in flash_log_write()
Now fully resets all state on error: incoming ring buffer, encoder, block buffer.
1 parent c786848 commit 12acdb7

File tree

1 file changed

+30
-36
lines changed
  • firmware/nRF51/tag-proximity/src

1 file changed

+30
-36
lines changed

firmware/nRF51/tag-proximity/src/log.c

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@ static int flash_log_write(uint8_t flush_buf)
221221

222222
/* feed it to the encoder */
223223
sres = heatshrink_encoder_sink(&hse, buf_tail, chunk_size, &sink_sz);
224+
225+
/* handle encoder error */
224226
if (sres < 0)
225-
break;
227+
goto cleanup;
226228

227229
/* pull out the compressed stream */
228230
do {
@@ -235,41 +237,26 @@ static int flash_log_write(uint8_t flush_buf)
235237
LogBlock.env.len += poll_sz;
236238
} while (pres == HSER_POLL_MORE);
237239

240+
/* handle encoder error */
238241
if (pres < 0)
239-
break;
242+
goto cleanup;
240243

241244
/* advance tail of ring buffer */
242245
buf_tail += sink_sz;
243246
if (buf_tail >= buffer+BUF_SIZE)
244247
buf_tail -= BUF_SIZE;
245248
}
246249

247-
/* on error, clear the ring buffer, reset encoder and exit */
248-
if (pres < 0 || sres < 0)
249-
{
250-
log_compression_error++;
251-
status_flags |= ERROR_LOG_COMPRESS;
252-
heatshrink_encoder_reset(&hse);
253-
buf_tail = my_head;
254-
return 1;
255-
}
256-
257250
/* if block buffer is almost full, or if we are flushing the ring buffer,
258251
flush the encoder and commit compressed data to flash memory */
259252
if ( (LOG_BLOCK_DATA_SIZE - LogBlock.env.len <= COMPRESS_CHUNK_SIZE ) || flush_buf )
260253
{
261254
/* signal encoder that we are done */
262255
fres = heatshrink_encoder_finish(&hse);
263256

264-
/* on error, clear the ring buffer, reset encoder and exit */
257+
/* handle encoder error */
265258
if (fres < 0)
266-
{
267-
log_compression_error++;
268-
status_flags |= ERROR_LOG_COMPRESS;
269-
heatshrink_encoder_reset(&hse);
270-
buf_tail = my_head;
271-
return 1;
272-
}
259+
goto cleanup;
273260

274261
/* if necessary, pull out remaining compressed data */
275262
if (fres == HSER_FINISH_MORE)
@@ -284,27 +271,15 @@ static int flash_log_write(uint8_t flush_buf)
284271
/* update block length */
285272
LogBlock.env.len += poll_sz;
286273

287-
/* on block buffer overflow, clear the ring buffer, reset encoder and exit */
274+
/* handle block buffer overflow */
288275
if (LogBlock.env.len >= LOG_BLOCK_DATA_SIZE)
289-
{
290-
log_compression_error++;
291-
status_flags |= ERROR_LOG_COMPRESS;
292-
heatshrink_encoder_reset(&hse);
293-
buf_tail = my_head;
294-
return 1;
295-
}
276+
goto cleanup;
296277
} while (pres == HSER_POLL_MORE);
297278
}
298279

299-
/* on error, clear the ring buffer, reset encoder and exit */
280+
/* handle encoder error */
300281
if (pres < 0)
301-
{
302-
log_compression_error++;
303-
status_flags |= ERROR_LOG_COMPRESS;
304-
heatshrink_encoder_reset(&hse);
305-
buf_tail = my_head;
306-
return 1;
307-
}
282+
goto cleanup;
308283

309284
/* reset encoder */
310285
heatshrink_encoder_reset(&hse);
@@ -314,6 +289,25 @@ static int flash_log_write(uint8_t flush_buf)
314289
}
315290

316291
return 0;
292+
293+
294+
/* on error, reset all state */
295+
cleanup:
296+
297+
/* report error */
298+
log_compression_error++;
299+
status_flags |= ERROR_LOG_COMPRESS;
300+
301+
/* clear block buffer */
302+
block_init();
303+
304+
/* reset encoder */
305+
heatshrink_encoder_reset(&hse);
306+
307+
/* clear ring buffer */
308+
buf_tail = my_head;
309+
310+
return 1;
317311
}
318312

319313
#else

0 commit comments

Comments
 (0)