Skip to content

Commit

Permalink
possible fix for crashing on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
frux-c committed May 27, 2024
1 parent 9ac6065 commit 4e3108d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
7 changes: 4 additions & 3 deletions uhf_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down
40 changes: 19 additions & 21 deletions uhf_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion uhf_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion uhf_tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdbool.h>
#include <stddef.h>

#define MAX_BANK_SIZE 256
#define MAX_BANK_SIZE 200
// storage enum
typedef enum { ReservedBank, EPCBank, TIDBank, UserBank } BankType;

Expand Down
20 changes: 11 additions & 9 deletions uhf_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 4e3108d

Please sign in to comment.