Просмотр исходного кода

possible fix for crashing on exit

frux-c 1 год назад
Родитель
Сommit
4e3108d319
5 измененных файлов с 38 добавлено и 35 удалено
  1. 4 3
      uhf_app.c
  2. 19 21
      uhf_buffer.c
  3. 3 1
      uhf_buffer.h
  4. 1 1
      uhf_tag.h
  5. 11 9
      uhf_worker.c

+ 4 - 3
uhf_app.c

@@ -130,6 +130,10 @@ void uhf_free(UHFApp* uhf_app) {
     view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewWidget);
     view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewWidget);
     widget_free(uhf_app->widget);
     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
     // Tag
     uhf_tag_wrapper_free(uhf_app->worker->uhf_tag_wrapper);
     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);
     furi_record_close(RECORD_GUI);
     uhf_app->gui = NULL;
     uhf_app->gui = NULL;
 
 
-    // Variable Item List
-    variable_item_list_free(uhf_app->variable_item_list);
-
     // Notifications
     // Notifications
     furi_record_close(RECORD_NOTIFICATION);
     furi_record_close(RECORD_NOTIFICATION);
     uhf_app->notifications = NULL;
     uhf_app->notifications = NULL;

+ 19 - 21
uhf_buffer.c

@@ -11,40 +11,39 @@ Buffer* uhf_buffer_alloc(size_t initial_capacity) {
     }
     }
     buf->size = 0;
     buf->size = 0;
     buf->capacity = initial_capacity;
     buf->capacity = initial_capacity;
+    buf->head = 0;
+    buf->tail = 0;
     return buf;
     return buf;
 }
 }
 
 
 bool uhf_buffer_append_single(Buffer* buf, uint8_t data) {
 bool uhf_buffer_append_single(Buffer* buf, uint8_t data) {
     if(buf->closed) return false;
     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;
     return true;
 }
 }
 
 
 bool uhf_buffer_append(Buffer* buf, uint8_t* data, size_t data_size) {
 bool uhf_buffer_append(Buffer* buf, uint8_t* data, size_t data_size) {
     if(buf->closed) return false;
     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;
     return true;
 }
 }
 
 
 uint8_t* uhf_buffer_get_data(Buffer* buf) {
 uint8_t* uhf_buffer_get_data(Buffer* buf) {
-    return buf->data;
+    return &buf->data[buf->head];
 }
 }
 
 
 size_t uhf_buffer_get_size(Buffer* buf) {
 size_t uhf_buffer_get_size(Buffer* buf) {
@@ -60,9 +59,8 @@ void uhf_buffer_close(Buffer* buf) {
 }
 }
 
 
 void uhf_buffer_reset(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->size = 0;
     buf->closed = false;
     buf->closed = false;
 }
 }

+ 3 - 1
uhf_buffer.h

@@ -5,10 +5,12 @@
 
 
 #define MAX_BUFFER_SIZE 200
 #define MAX_BUFFER_SIZE 200
 
 
-typedef struct Buffer {
+typedef struct {
     uint8_t* data;
     uint8_t* data;
     size_t size;
     size_t size;
     size_t capacity;
     size_t capacity;
+    size_t head;
+    size_t tail;
     bool closed;
     bool closed;
 } Buffer;
 } Buffer;
 
 

+ 1 - 1
uhf_tag.h

@@ -4,7 +4,7 @@
 #include <stdbool.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <stddef.h>
 
 
-#define MAX_BANK_SIZE 256
+#define MAX_BANK_SIZE 200
 // storage enum
 // storage enum
 typedef enum { ReservedBank, EPCBank, TIDBank, UserBank } BankType;
 typedef enum { ReservedBank, EPCBank, TIDBank, UserBank } BankType;
 
 

+ 11 - 9
uhf_worker.c

@@ -15,13 +15,13 @@ UHFTag* send_polling_command(UHFWorker* uhf_worker) {
     // read epc bank
     // read epc bank
     UHFTag* uhf_tag = uhf_tag_alloc();
     UHFTag* uhf_tag = uhf_tag_alloc();
     M100ResponseType status;
     M100ResponseType status;
-    do{
+    do {
         if(uhf_worker->state == UHFWorkerStateStop) {
         if(uhf_worker->state == UHFWorkerStateStop) {
             uhf_tag_free(uhf_tag);
             uhf_tag_free(uhf_tag);
             return NULL;
             return NULL;
         }
         }
         status = m100_single_poll(uhf_worker->module, uhf_tag);
         status = m100_single_poll(uhf_worker->module, uhf_tag);
-    }while(status != M100SuccessResponse);
+    } while(status != M100SuccessResponse);
     return uhf_tag;
     return uhf_tag;
 }
 }
 
 
@@ -48,7 +48,8 @@ UHFWorkerEvent read_single_card(UHFWorker* uhf_worker) {
     if(uhf_tag == NULL) return UHFWorkerEventAborted;
     if(uhf_tag == NULL) return UHFWorkerEventAborted;
     uhf_tag_wrapper_set_tag(uhf_worker->uhf_tag_wrapper, uhf_tag);
     uhf_tag_wrapper_set_tag(uhf_worker->uhf_tag_wrapper, uhf_tag);
     // set select
     // set select
-    while(m100_set_select(uhf_worker->module, uhf_tag) != M100SuccessResponse){}
+    while(m100_set_select(uhf_worker->module, uhf_tag) != M100SuccessResponse) {
+    }
     // read tid
     // read tid
     UHFWorkerEvent event;
     UHFWorkerEvent event;
     event = read_bank_till_max_length(uhf_worker, uhf_tag, TIDBank);
     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;
     if(uhf_tag_des == NULL) return UHFWorkerEventAborted;
     UHFTag* uhf_tag_from = uhf_worker->uhf_tag_wrapper->uhf_tag;
     UHFTag* uhf_tag_from = uhf_worker->uhf_tag_wrapper->uhf_tag;
     M100ResponseType rp_type;
     M100ResponseType rp_type;
-    do{
+    do {
         rp_type = m100_set_select(uhf_worker->module, uhf_tag_des);
         rp_type = m100_set_select(uhf_worker->module, uhf_tag_des);
         if(uhf_worker->state == UHFWorkerStateStop) return UHFWorkerEventAborted;
         if(uhf_worker->state == UHFWorkerStateStop) return UHFWorkerEventAborted;
         if(rp_type == M100SuccessResponse) break;
         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(
         rp_type = m100_write_label_data_storage(
             uhf_worker->module, uhf_tag_from, uhf_tag_des, UserBank, 0, 0);
             uhf_worker->module, uhf_tag_from, uhf_tag_des, UserBank, 0, 0);
         if(uhf_worker->state == UHFWorkerStateStop) return UHFWorkerEventAborted;
         if(uhf_worker->state == UHFWorkerStateStop) return UHFWorkerEventAborted;
         if(rp_type == M100SuccessResponse) break;
         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(
         rp_type = m100_write_label_data_storage(
             uhf_worker->module, uhf_tag_from, uhf_tag_des, TIDBank, 0, 0);
             uhf_worker->module, uhf_tag_from, uhf_tag_des, TIDBank, 0, 0);
         if(uhf_worker->state == UHFWorkerStateStop) return UHFWorkerEventAborted;
         if(uhf_worker->state == UHFWorkerStateStop) return UHFWorkerEventAborted;
         if(rp_type == M100SuccessResponse) break;
         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(
         rp_type = m100_write_label_data_storage(
             uhf_worker->module, uhf_tag_from, uhf_tag_des, EPCBank, 0, 0);
             uhf_worker->module, uhf_tag_from, uhf_tag_des, EPCBank, 0, 0);
         if(uhf_worker->state == UHFWorkerStateStop) return UHFWorkerEventAborted;
         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_alloc() {
     UHFWorker* uhf_worker = (UHFWorker*)malloc(sizeof(UHFWorker));
     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->module = m100_module_alloc();
     uhf_worker->callback = NULL;
     uhf_worker->callback = NULL;
     uhf_worker->ctx = NULL;
     uhf_worker->ctx = NULL;