@@ -221,8 +221,10 @@ static int flash_log_write(uint8_t flush_buf)
221
221
222
222
/* feed it to the encoder */
223
223
sres = heatshrink_encoder_sink (& hse , buf_tail , chunk_size , & sink_sz );
224
+
225
+ /* handle encoder error */
224
226
if (sres < 0 )
225
- break ;
227
+ goto cleanup ;
226
228
227
229
/* pull out the compressed stream */
228
230
do {
@@ -235,41 +237,26 @@ static int flash_log_write(uint8_t flush_buf)
235
237
LogBlock .env .len += poll_sz ;
236
238
} while (pres == HSER_POLL_MORE );
237
239
240
+ /* handle encoder error */
238
241
if (pres < 0 )
239
- break ;
242
+ goto cleanup ;
240
243
241
244
/* advance tail of ring buffer */
242
245
buf_tail += sink_sz ;
243
246
if (buf_tail >= buffer + BUF_SIZE )
244
247
buf_tail -= BUF_SIZE ;
245
248
}
246
249
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
-
257
250
/* if block buffer is almost full, or if we are flushing the ring buffer,
258
251
flush the encoder and commit compressed data to flash memory */
259
252
if ( (LOG_BLOCK_DATA_SIZE - LogBlock .env .len <= COMPRESS_CHUNK_SIZE ) || flush_buf )
260
253
{
261
254
/* signal encoder that we are done */
262
255
fres = heatshrink_encoder_finish (& hse );
263
256
264
- /* on error, clear the ring buffer, reset encoder and exit */
257
+ /* handle encoder error */
265
258
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 ;
273
260
274
261
/* if necessary, pull out remaining compressed data */
275
262
if (fres == HSER_FINISH_MORE )
@@ -284,27 +271,15 @@ static int flash_log_write(uint8_t flush_buf)
284
271
/* update block length */
285
272
LogBlock .env .len += poll_sz ;
286
273
287
- /* on block buffer overflow, clear the ring buffer, reset encoder and exit */
274
+ /* handle block buffer overflow */
288
275
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 ;
296
277
} while (pres == HSER_POLL_MORE );
297
278
}
298
279
299
- /* on error, clear the ring buffer, reset encoder and exit */
280
+ /* handle encoder error */
300
281
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 ;
308
283
309
284
/* reset encoder */
310
285
heatshrink_encoder_reset (& hse );
@@ -314,6 +289,25 @@ static int flash_log_write(uint8_t flush_buf)
314
289
}
315
290
316
291
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 ;
317
311
}
318
312
319
313
#else
0 commit comments