Skip to content

Commit

Permalink
Merge pull request #21 from frux-c/tag-id-detail
Browse files Browse the repository at this point in the history
Bug fixes mainly
  • Loading branch information
frux-c authored Aug 17, 2024
2 parents a842673 + e8633bd commit a53413b
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 64 deletions.
93 changes: 93 additions & 0 deletions scenes/uhf_scene_settings.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "../uhf_app_i.h"
#include "../uhf_module.h"

char* yes_no[] = {"No", "Yes"};

void uhf_settings_set_module_baudrate(VariableItem* item) {
M100Module* module = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
Expand Down Expand Up @@ -38,6 +40,50 @@ void uhf_settings_set_module_working_region(VariableItem* item) {
variable_item_set_current_value_text(item, WORKING_REGIONS_STR[index]);
}

void uhf_settings_set_epc_write_mask(VariableItem* item){
M100Module* uhf_module = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, yes_no[index]);
if(index){
m100_enable_write_mask(uhf_module, WRITE_EPC);
return;
}
m100_disable_write_mask(uhf_module, WRITE_EPC);
}

void uhf_settings_set_tid_write_mask(VariableItem* item){
M100Module* uhf_module = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, yes_no[index]);
if(index){
m100_enable_write_mask(uhf_module, WRITE_TID);
return;
}
m100_disable_write_mask(uhf_module, WRITE_TID);
}

void uhf_settings_set_user_write_mask(VariableItem* item){
M100Module* uhf_module = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, yes_no[index]);
if(index){
m100_enable_write_mask(uhf_module, WRITE_USER);
return;
}
m100_disable_write_mask(uhf_module, WRITE_USER);
}

void uhf_settings_set_rfu_write_mask(VariableItem* item){
M100Module* uhf_module = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, yes_no[index]);
if(index){
m100_enable_write_mask(uhf_module, WRITE_RFU);
return;
}
m100_disable_write_mask(uhf_module, WRITE_RFU);
}

uint8_t uhf_settings_get_module_baudrate_index(M100Module* module) {
for(uint8_t i = 0; i < BAUD_RATES_COUNT; i++) {
if(BAUD_RATES[i] == module->uart->baudrate) {
Expand Down Expand Up @@ -71,6 +117,7 @@ void uhf_scene_settings_on_enter(void* ctx) {
VariableItem* item;
VariableItemList* variable_item_list = uhf_app->variable_item_list;

// Add baudrate item
uint8_t value_index = uhf_settings_get_module_baudrate_index(uhf_module);
char text_buf[10];
snprintf(text_buf, sizeof(text_buf), "%lu", uhf_module->uart->baudrate);
Expand All @@ -84,6 +131,7 @@ void uhf_scene_settings_on_enter(void* ctx) {
variable_item_set_current_value_text(item, text_buf);
variable_item_set_current_value_index(item, value_index);

// Add power item
value_index = uhf_settings_get_module_power_index(uhf_module);
item = variable_item_list_add(
variable_item_list,
Expand All @@ -95,6 +143,7 @@ void uhf_scene_settings_on_enter(void* ctx) {
variable_item_set_current_value_text(item, text_buf);
variable_item_set_current_value_index(item, value_index);

// Add working region item
value_index = uhf_settings_get_module_working_region_index(uhf_module);
item = variable_item_list_add(
variable_item_list,
Expand All @@ -106,6 +155,50 @@ void uhf_scene_settings_on_enter(void* ctx) {
variable_item_set_current_value_index(item, value_index);

view_dispatcher_switch_to_view(uhf_app->view_dispatcher, UHFViewVariableItemList);


// Add write modes
value_index = m100_is_write_mask_enabled(uhf_module, WRITE_EPC)? 1: 0;
item = variable_item_list_add(
variable_item_list,
"Write EPC:",
2,
uhf_settings_set_epc_write_mask,
uhf_module);
variable_item_set_current_value_text(item, yes_no[value_index]);
variable_item_set_current_value_index(item, value_index);

value_index = m100_is_write_mask_enabled(uhf_module, WRITE_TID)? 1: 0;
item = variable_item_list_add(
variable_item_list,
"Write TID:",
2,
uhf_settings_set_tid_write_mask,
uhf_module);
variable_item_set_current_value_text(item, yes_no[value_index]);
variable_item_set_current_value_index(item, value_index);

value_index = m100_is_write_mask_enabled(uhf_module, WRITE_USER)? 1: 0;
item = variable_item_list_add(
variable_item_list,
"Write User:",
2,
uhf_settings_set_user_write_mask,
uhf_module);
variable_item_set_current_value_text(item, yes_no[value_index]);
variable_item_set_current_value_index(item, value_index);

value_index = m100_is_write_mask_enabled(uhf_module, WRITE_RFU)? 1: 0;
item = variable_item_list_add(
variable_item_list,
"Write RFU:",
2,
uhf_settings_set_rfu_write_mask,
uhf_module);
variable_item_set_current_value_text(item, yes_no[value_index]);
variable_item_set_current_value_index(item, value_index);


}

bool uhf_scene_settings_on_event(void* ctx, SceneManagerEvent event) {
Expand Down
26 changes: 19 additions & 7 deletions uhf_app.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "uhf_app_i.h"
#include "expansion/expansion.h"

char* convertToHexString(uint8_t* array, size_t length) {
if(array == NULL || length == 0) {
Expand Down Expand Up @@ -130,6 +131,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 +155,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 Expand Up @@ -194,15 +196,25 @@ void uhf_show_loading_popup(void* ctx, bool show) {

int32_t uhf_app_main(void* ctx) {
UNUSED(ctx);
Expansion* expansion = furi_record_open(RECORD_EXPANSION);
expansion_disable(expansion);
bool is_5v_enabled_by_app = false;
// enable 5v pin if not enabled
if(!furi_hal_power_is_otg_enabled()) {
furi_hal_power_enable_otg();
is_5v_enabled_by_app = true;
}
UHFApp* uhf_app = uhf_alloc();
// enable 5v pin
furi_hal_power_enable_otg();
// enter app
scene_manager_next_scene(uhf_app->scene_manager, UHFSceneModuleInfo);
view_dispatcher_run(uhf_app->view_dispatcher);
// disable 5v pin
furi_hal_power_disable_otg();
// disable 5v pin if enabled by app
if(is_5v_enabled_by_app) {
furi_hal_power_disable_otg();
}
// exit app
uhf_free(uhf_app);
expansion_enable(expansion);
furi_record_close(RECORD_EXPANSION);
return 0;
}
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
16 changes: 15 additions & 1 deletion uhf_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ static M100ResponseType setup_and_send_rx(M100Module* module, uint8_t* cmd, size
// send cmd
uhf_uart_send_wait(uart, cmd, cmd_length);
// wait for response by polling
while(!uhf_is_buffer_closed(buffer) && !uhf_uart_tick(uart)) {}
while(!uhf_is_buffer_closed(buffer) && !uhf_uart_tick(uart)) {
}
// reset tick
uhf_uart_tick_reset(uart);
// Validation Checks
Expand Down Expand Up @@ -45,6 +46,7 @@ M100Module* m100_module_alloc() {
module->region = DEFAULT_WORKING_REGION;
module->info = m100_module_info_alloc();
module->uart = uhf_uart_alloc();
module->write_mask = WRITE_EPC; // default to write epc only
return module;
}

Expand Down Expand Up @@ -182,6 +184,18 @@ M100ResponseType m100_set_select(M100Module* module, UHFTag* uhf_tag) {
return M100SuccessResponse;
}

void m100_enable_write_mask(M100Module* module, WriteMask mask) {
module->write_mask |= mask;
}

void m100_disable_write_mask(M100Module* module, WriteMask mask) {
module->write_mask &= ~mask;
}

bool m100_is_write_mask_enabled(M100Module* module, WriteMask mask) {
return (module->write_mask & mask) == mask;
}

UHFTag* m100_get_select_param(M100Module* module) {
uhf_buffer_reset(module->uart->buffer);
// furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, rx_callback, module->uart->buffer);
Expand Down
11 changes: 11 additions & 0 deletions uhf_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@ typedef enum {
M100ChecksumFail
} M100ResponseType;

typedef enum {
WRITE_EPC = 1 << 0,
WRITE_TID = 1 << 1,
WRITE_USER = 1 << 2,
WRITE_RFU = 1 << 3
} WriteMask;

typedef struct {
M100ModuleInfo* info;
WorkingRegion region;
uint16_t region_frequency;
uint16_t transmitting_power;
uint16_t max_transmitting_power;
uint16_t write_mask;
bool freq_hopping;
UHFUart* uart;
} M100Module;
Expand Down Expand Up @@ -79,3 +87,6 @@ M100ResponseType m100_write_label_data_storage(
uint32_t access_pwd);

uint32_t m100_get_baudrate(M100Module* module);
void m100_enable_write_mask(M100Module* module, WriteMask mask);
void m100_disable_write_mask(M100Module* module, WriteMask mask);
bool m100_is_write_mask_enabled(M100Module* module, WriteMask mask);
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
Loading

0 comments on commit a53413b

Please sign in to comment.