From 4e3108d319cdd62868903e1498c16e07bdd28368 Mon Sep 17 00:00:00 2001 From: frux-c Date: Mon, 27 May 2024 08:48:34 -0600 Subject: [PATCH] possible fix for crashing on exit --- uhf_app.c | 7 ++++--- uhf_buffer.c | 40 +++++++++++++++++++--------------------- uhf_buffer.h | 4 +++- uhf_tag.h | 2 +- uhf_worker.c | 20 +++++++++++--------- 5 files changed, 38 insertions(+), 35 deletions(-) diff --git a/uhf_app.c b/uhf_app.c index ea8d2135175..1181d212ae6 100644 --- a/uhf_app.c +++ b/uhf_app.c @@ -130,6 +130,10 @@ void uhf_free(UHFApp* uhf_app) { view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewWidget); widget_free(uhf_app->widget); + // Variable Item List + view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewVariableItemList); + variable_item_list_free(uhf_app->variable_item_list); + // Tag uhf_tag_wrapper_free(uhf_app->worker->uhf_tag_wrapper); @@ -150,9 +154,6 @@ void uhf_free(UHFApp* uhf_app) { furi_record_close(RECORD_GUI); uhf_app->gui = NULL; - // Variable Item List - variable_item_list_free(uhf_app->variable_item_list); - // Notifications furi_record_close(RECORD_NOTIFICATION); uhf_app->notifications = NULL; diff --git a/uhf_buffer.c b/uhf_buffer.c index 5ee32c40292..7b7ffe7269f 100644 --- a/uhf_buffer.c +++ b/uhf_buffer.c @@ -11,40 +11,39 @@ Buffer* uhf_buffer_alloc(size_t initial_capacity) { } buf->size = 0; buf->capacity = initial_capacity; + buf->head = 0; + buf->tail = 0; return buf; } bool uhf_buffer_append_single(Buffer* buf, uint8_t data) { if(buf->closed) return false; - if(buf->size + 1 > buf->capacity) { - size_t new_capacity = buf->capacity * 2; - uint8_t* new_data = (uint8_t*)realloc(buf->data, sizeof(uint8_t) * new_capacity); - if(!new_data) return false; - buf->data = new_data; - buf->capacity = new_capacity; + buf->data[buf->tail] = data; + buf->tail = (buf->tail + 1) % buf->capacity; + if(buf->size < buf->capacity) { + buf->size++; + } else { + buf->head = (buf->head + 1) % buf->capacity; } - buf->data[buf->size++] = data; return true; } bool uhf_buffer_append(Buffer* buf, uint8_t* data, size_t data_size) { if(buf->closed) return false; - if(buf->size + data_size > buf->capacity) { - size_t new_capacity = buf->capacity * 2; - uint8_t* new_data = (uint8_t*)realloc(buf->data, new_capacity); - if(!new_data) return false; - - buf->data = new_data; - buf->capacity = new_capacity; + for(size_t i = 0; i < data_size; i++) { + buf->data[buf->tail] = data[i]; + buf->tail = (buf->tail + 1) % buf->capacity; + if(buf->size < buf->capacity) { + buf->size++; + } else { + buf->head = (buf->head + 1) % buf->capacity; + } } - - memcpy((void*)&buf->data[buf->size], data, data_size); - buf->size += data_size; return true; } uint8_t* uhf_buffer_get_data(Buffer* buf) { - return buf->data; + return &buf->data[buf->head]; } size_t uhf_buffer_get_size(Buffer* buf) { @@ -60,9 +59,8 @@ void uhf_buffer_close(Buffer* buf) { } void uhf_buffer_reset(Buffer* buf) { - for(size_t i = 0; i < MAX_BUFFER_SIZE; i++) { - buf->data[i] = 0; - } + buf->head = 0; + buf->tail = 0; buf->size = 0; buf->closed = false; } diff --git a/uhf_buffer.h b/uhf_buffer.h index d1420094bc6..f3086cecadb 100644 --- a/uhf_buffer.h +++ b/uhf_buffer.h @@ -5,10 +5,12 @@ #define MAX_BUFFER_SIZE 200 -typedef struct Buffer { +typedef struct { uint8_t* data; size_t size; size_t capacity; + size_t head; + size_t tail; bool closed; } Buffer; diff --git a/uhf_tag.h b/uhf_tag.h index bfc045ceb4f..7d997872aac 100644 --- a/uhf_tag.h +++ b/uhf_tag.h @@ -4,7 +4,7 @@ #include #include -#define MAX_BANK_SIZE 256 +#define MAX_BANK_SIZE 200 // storage enum typedef enum { ReservedBank, EPCBank, TIDBank, UserBank } BankType; diff --git a/uhf_worker.c b/uhf_worker.c index e13b93d6826..7761b5cc6c4 100644 --- a/uhf_worker.c +++ b/uhf_worker.c @@ -15,13 +15,13 @@ UHFTag* send_polling_command(UHFWorker* uhf_worker) { // read epc bank UHFTag* uhf_tag = uhf_tag_alloc(); M100ResponseType status; - do{ + do { if(uhf_worker->state == UHFWorkerStateStop) { uhf_tag_free(uhf_tag); return NULL; } status = m100_single_poll(uhf_worker->module, uhf_tag); - }while(status != M100SuccessResponse); + } while(status != M100SuccessResponse); return uhf_tag; } @@ -48,7 +48,8 @@ UHFWorkerEvent read_single_card(UHFWorker* uhf_worker) { if(uhf_tag == NULL) return UHFWorkerEventAborted; uhf_tag_wrapper_set_tag(uhf_worker->uhf_tag_wrapper, uhf_tag); // set select - while(m100_set_select(uhf_worker->module, uhf_tag) != M100SuccessResponse){} + while(m100_set_select(uhf_worker->module, uhf_tag) != M100SuccessResponse) { + } // read tid UHFWorkerEvent event; event = read_bank_till_max_length(uhf_worker, uhf_tag, TIDBank); @@ -64,24 +65,24 @@ UHFWorkerEvent write_single_card(UHFWorker* uhf_worker) { if(uhf_tag_des == NULL) return UHFWorkerEventAborted; UHFTag* uhf_tag_from = uhf_worker->uhf_tag_wrapper->uhf_tag; M100ResponseType rp_type; - do{ + do { rp_type = m100_set_select(uhf_worker->module, uhf_tag_des); if(uhf_worker->state == UHFWorkerStateStop) return UHFWorkerEventAborted; if(rp_type == M100SuccessResponse) break; - }while(true); - while(m100_is_write_mask_enabled(uhf_worker->module, WRITE_USER)){ + } while(true); + while(m100_is_write_mask_enabled(uhf_worker->module, WRITE_USER)) { rp_type = m100_write_label_data_storage( uhf_worker->module, uhf_tag_from, uhf_tag_des, UserBank, 0, 0); if(uhf_worker->state == UHFWorkerStateStop) return UHFWorkerEventAborted; if(rp_type == M100SuccessResponse) break; } - while(m100_is_write_mask_enabled(uhf_worker->module, WRITE_TID)){ + while(m100_is_write_mask_enabled(uhf_worker->module, WRITE_TID)) { rp_type = m100_write_label_data_storage( uhf_worker->module, uhf_tag_from, uhf_tag_des, TIDBank, 0, 0); if(uhf_worker->state == UHFWorkerStateStop) return UHFWorkerEventAborted; if(rp_type == M100SuccessResponse) break; } - while(m100_is_write_mask_enabled(uhf_worker->module, WRITE_EPC)){ + while(m100_is_write_mask_enabled(uhf_worker->module, WRITE_EPC)) { rp_type = m100_write_label_data_storage( uhf_worker->module, uhf_tag_from, uhf_tag_des, EPCBank, 0, 0); if(uhf_worker->state == UHFWorkerStateStop) return UHFWorkerEventAborted; @@ -107,7 +108,8 @@ int32_t uhf_worker_task(void* ctx) { UHFWorker* uhf_worker_alloc() { UHFWorker* uhf_worker = (UHFWorker*)malloc(sizeof(UHFWorker)); - uhf_worker->thread = furi_thread_alloc_ex("UHFWorker", UHF_WORKER_STACK_SIZE, uhf_worker_task, uhf_worker); + uhf_worker->thread = + furi_thread_alloc_ex("UHFWorker", UHF_WORKER_STACK_SIZE, uhf_worker_task, uhf_worker); uhf_worker->module = m100_module_alloc(); uhf_worker->callback = NULL; uhf_worker->ctx = NULL;