Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
added read for all banks, stability seems reasonable
Browse files Browse the repository at this point in the history
  • Loading branch information
frux-c committed Aug 5, 2023
1 parent 28b375c commit 51f9d90
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 60 deletions.
4 changes: 2 additions & 2 deletions scenes/uhf_scene_read_tag_success.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ void uhf_scene_read_card_success_widget_callback(GuiButtonType result, InputType
void uhf_scene_read_tag_success_on_enter(void* ctx) {
UHFApp* uhf_app = ctx;

const uint8_t* read_data = uhf_response_data_get_uhf_data(uhf_app->worker->response_data, 0)->data;
const uint8_t* read_data =
uhf_response_data_get_uhf_data(uhf_app->worker->response_data, 0)->data;

widget_add_string_element(
uhf_app->widget, 32, 5, AlignLeft, AlignCenter, FontPrimary, "Read Success");
Expand All @@ -38,7 +39,6 @@ void uhf_scene_read_tag_success_on_enter(void* ctx) {
AlignCenter,
FontKeyboard,
convertToHexString(read_data + 6, 2));

widget_add_string_element(
uhf_app->widget,
96,
Expand Down
6 changes: 3 additions & 3 deletions scenes/uhf_scene_verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ bool uhf_scene_verify_on_event(void* ctx, SceneManagerEvent event) {
1,
15,
AlignLeft,
AlignCenter,
AlignTop,
FontSecondary,
furi_string_get_cstr(temp_str));
furi_string_reset(temp_str);
Expand All @@ -83,7 +83,7 @@ bool uhf_scene_verify_on_event(void* ctx, SceneManagerEvent event) {
1,
27,
AlignLeft,
AlignCenter,
AlignTop,
FontSecondary,
furi_string_get_cstr(temp_str));
furi_string_reset(temp_str);
Expand All @@ -97,7 +97,7 @@ bool uhf_scene_verify_on_event(void* ctx, SceneManagerEvent event) {
1,
39,
AlignLeft,
AlignCenter,
AlignTop,
FontSecondary,
furi_string_get_cstr(temp_str));

Expand Down
45 changes: 40 additions & 5 deletions uhf_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

static const char* uhf_file_header = "Flipper UHF device";
static const uint32_t uhf_file_version = 1;
static const uint8_t bank_data_start = 20;
static const uint8_t bank_data_length = 16;

// empty callback
void empty_rx_callback(UartIrqEvent event, uint8_t data, void* ctx) {
Expand Down Expand Up @@ -50,10 +52,44 @@ bool uhf_save_read_data(UHFResponseData* uhf_response_data, Storage* storage, co
if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) return false;
// write header
if(!flipper_format_write_header_cstr(file, uhf_file_header, uhf_file_version)) return false;
// write epc bank
if(!flipper_format_write_hex(
file, "EPC", uhf_response_data->head->data, uhf_response_data->head->length))
return false;
// write rfu data to file
UHFData* rfu_data = uhf_response_data_get_uhf_data(uhf_response_data, 1);
if(rfu_data->length) {
if(!flipper_format_write_hex(
file, "RFU", rfu_data->data + bank_data_start, bank_data_length))
return false;
} else {
if(!flipper_format_write_hex(file, "RFU", UHF_BANK_DOES_NOT_EXIST, 1)) return false;
}

// write epc data to file
UHFData* epc_data = uhf_response_data_get_uhf_data(uhf_response_data, 2);
if(epc_data->length) {
if(!flipper_format_write_hex(
file, "EPC", epc_data->data + bank_data_start, bank_data_length))
return false;
} else {
if(!flipper_format_write_hex(file, "EPC", UHF_BANK_DOES_NOT_EXIST, 1)) return false;
}

// write tid data to file
UHFData* tid_data = uhf_response_data_get_uhf_data(uhf_response_data, 3);
if(tid_data->length) {
if(!flipper_format_write_hex(
file, "TID", tid_data->data + bank_data_start, bank_data_length))
return false;
} else {
if(!flipper_format_write_hex(file, "TID", UHF_BANK_DOES_NOT_EXIST, 1)) return false;
}
// write user data to file
UHFData* user_data = uhf_response_data_get_uhf_data(uhf_response_data, 4);
if(user_data->length) {
if(!flipper_format_write_hex(
file, "USER", user_data->data + bank_data_start, bank_data_length))
return false;
} else {
if(!flipper_format_write_hex(file, "USER", UHF_BANK_DOES_NOT_EXIST, 1)) return false;
}
furi_string_free(temp_str);
flipper_format_free(file);
return true;
Expand Down Expand Up @@ -217,7 +253,6 @@ int32_t uhf_app_main(void* ctx) {

// enable 5v pin
furi_hal_power_enable_otg();

scene_manager_next_scene(uhf_app->scene_manager, UHFSceneVerify);
view_dispatcher_run(uhf_app->view_dispatcher);

Expand Down
1 change: 1 addition & 0 deletions uhf_app_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "uhf_worker.h"
#include <uhf_rfid_icons.h>

#define UHF_BANK_DOES_NOT_EXIST (uint8_t[]){0xFF}
#define UHF_TEXT_STORE_SIZE 128
#define UHF_APPS_DATA_FOLDER EXT_PATH("apps_data")
#define UHF_APPS_STORAGE_FOLDER \
Expand Down
2 changes: 1 addition & 1 deletion uhf_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stdint.h>
#include <stdlib.h>

#define MAX_DATA_SIZE 32
#define MAX_DATA_SIZE 64

typedef struct UHFData {
uint8_t data[MAX_DATA_SIZE];
Expand Down
118 changes: 70 additions & 48 deletions uhf_worker.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#include "uhf_worker.h"
#include "uhf_cmd.h"

#define CB_DELAY 50
// inner functions
uint8_t calculate_checksum(uint8_t* arr, size_t length) {
uint8_t calculate_checksum(UHFData* uhf_data) {
// CheckSum8 Modulo 256
// Sum of Bytes % 256
uint8_t sum_val = 0x00;
for(size_t i = 0; i < length; i++) {
sum_val += arr[i];
size_t length = uhf_data->length - 2;
for(size_t i = 1; i < length; i++) {
sum_val += uhf_data->data[i];
}
return sum_val % 256;
}

bool validate_checksum(UHFData* uhf_data) {
uint8_t data_checksum = uhf_data->data[uhf_data->length - 2];
uint8_t actual_checksum = calculate_checksum(uhf_data);
return data_checksum == actual_checksum;
}
// uart callback functions
void module_rx_callback(UartIrqEvent event, uint8_t data, void* ctx) {
UNUSED(event);
Expand All @@ -33,15 +39,15 @@ UHFWorkerEvent verify_module_connected(UHFWorker* uhf_worker) {
// read hardware version
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, module_rx_callback, hardware_version);
furi_hal_uart_tx(FuriHalUartIdUSART1, CMD_HARDWARE_VERSION.cmd, CMD_HARDWARE_VERSION.length);
furi_delay_ms(50);
furi_delay_ms(CB_DELAY);
// read software version
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, module_rx_callback, software_version);
furi_hal_uart_tx(FuriHalUartIdUSART1, CMD_SOFTWARE_VERSION.cmd, CMD_SOFTWARE_VERSION.length);
furi_delay_ms(50);
furi_delay_ms(CB_DELAY);
// read manufacturer
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, module_rx_callback, manufacturer);
furi_hal_uart_tx(FuriHalUartIdUSART1, CMD_MANUFACTURERS.cmd, CMD_MANUFACTURERS.length);
furi_delay_ms(50);
furi_delay_ms(CB_DELAY);
// FURI_LOG_E("log", "done sending tx");
if(!hardware_version->end || !software_version->end || !manufacturer->end) {
return UHFWorkerEventFail;
Expand Down Expand Up @@ -72,81 +78,97 @@ UHFWorkerEvent read_single_card(UHFWorker* uhf_worker) {
}
}
// Set select
uint8_t* select_cmd = (uint8_t*)malloc(sizeof(uint8_t) * CMD_SET_SELECT_PARAMETER.length);
memcpy(select_cmd, (void*)&CMD_SET_SELECT_PARAMETER.cmd[0], CMD_SET_SELECT_PARAMETER.length);
UHFData* select_cmd = uhf_data_alloc();
select_cmd->length = CMD_SET_SELECT_PARAMETER.length;
memcpy((void*)&select_cmd->data, (void*)&CMD_SET_SELECT_PARAMETER.cmd[0], select_cmd->length);
// set select param
select_cmd[5] = 0x01; // 0x00=rfu, 0x01=epc, 0x10=tid, 0x11=user
select_cmd->data[5] = 0x01; // 0x00=rfu, 0x01=epc, 0x10=tid, 0x11=user
// set ptr
select_cmd[9] = 0x20;
select_cmd->data[9] = 0x20;
// set mask
for(int i = 0; i < 12; i++) // 96 bits, 12 bytes
{
select_cmd[12 + i] = raw_read_data->data[8 + i];
select_cmd->data[12 + i] = raw_read_data->data[8 + i];
}
// checksum
select_cmd[CMD_SET_SELECT_PARAMETER.length - 2] =
calculate_checksum(select_cmd + 1, CMD_SET_SELECT_PARAMETER.length - 3);
select_cmd->data[select_cmd->length - 2] = calculate_checksum(select_cmd);
UHFData* select_response = uhf_response_data_add_new_uhf_data(uhf_response_data);
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, module_rx_callback, select_response);
furi_hal_uart_tx(FuriHalUartIdUSART1, select_cmd, CMD_SET_SELECT_PARAMETER.length);
furi_delay_ms(50);
furi_hal_uart_tx(FuriHalUartIdUSART1, select_cmd->data, select_cmd->length);
furi_delay_ms(CB_DELAY);

if(select_response->data[5] != 0x00) return UHFWorkerEventFail;

uint8_t* read_bank_cmd =
(uint8_t*)malloc(sizeof(uint8_t) * CMD_READ_LABEL_DATA_STORAGE.length);
UHFData* read_bank_cmd = uhf_data_alloc();
read_bank_cmd->length = CMD_READ_LABEL_DATA_STORAGE.length;
memcpy(
read_bank_cmd,
(void*)&read_bank_cmd->data[0],
(void*)&CMD_READ_LABEL_DATA_STORAGE.cmd[0],
CMD_READ_LABEL_DATA_STORAGE.length);
read_bank_cmd->length);

// int retry = 10;
// read rfu bank
UHFData* rfu_bank = select_response;
uhf_data_reset(rfu_bank);
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, module_rx_callback, rfu_bank);
read_bank_cmd[9] = 0x00;
furi_hal_uart_tx(FuriHalUartIdUSART1, read_bank_cmd, CMD_READ_LABEL_DATA_STORAGE.length);
furi_delay_ms(50);

// for(int i = 0; i < (int)rfu_bank->length; i++) {
// FURI_LOG_E("rfu_bank", "data[%d]=%02x", i, rfu_bank->data[i]);
// }
read_bank_cmd->data[9] = 0x00;
read_bank_cmd->data[read_bank_cmd->length - 2] = calculate_checksum(read_bank_cmd);
uhf_data_reset(rfu_bank);
furi_hal_uart_tx(FuriHalUartIdUSART1, read_bank_cmd->data, read_bank_cmd->length);
furi_delay_ms(CB_DELAY);
if(rfu_bank->data[2] != read_bank_cmd->data[2]) {
uhf_data_reset(rfu_bank);
}

// read epc bank
UHFData* epc_bank = uhf_response_data_add_new_uhf_data(uhf_response_data);
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, module_rx_callback, epc_bank);
read_bank_cmd[9] = 0x01;
furi_hal_uart_tx(FuriHalUartIdUSART1, read_bank_cmd, CMD_READ_LABEL_DATA_STORAGE.length);
furi_delay_ms(50);

// for(int i = 0; i < (int)epc_bank->length; i++) {
// FURI_LOG_E("epc_bank", "data[%d]=%02x", i, epc_bank->data[i]);
// }
read_bank_cmd->data[9] = 0x01;
read_bank_cmd->data[read_bank_cmd->length - 2] = calculate_checksum(read_bank_cmd);
uhf_data_reset(epc_bank);
furi_hal_uart_tx(FuriHalUartIdUSART1, read_bank_cmd->data, read_bank_cmd->length);
furi_delay_ms(CB_DELAY);
if(epc_bank->data[2] != read_bank_cmd->data[2]) {
uhf_data_reset(epc_bank);
}

// read tid bank
UHFData* tid_bank = uhf_response_data_add_new_uhf_data(uhf_response_data);
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, module_rx_callback, tid_bank);
read_bank_cmd[9] = 0x02;
furi_hal_uart_tx(FuriHalUartIdUSART1, read_bank_cmd, CMD_READ_LABEL_DATA_STORAGE.length);
furi_delay_ms(50);

// for(int i = 0; i < (int)tid_bank->length; i++) {
// FURI_LOG_E("tid_bank", "data[%d]=%02x", i, tid_bank->data[i]);
// }
read_bank_cmd->data[9] = 0x02;
read_bank_cmd->data[read_bank_cmd->length - 2] = calculate_checksum(read_bank_cmd);
uhf_data_reset(tid_bank);
furi_hal_uart_tx(FuriHalUartIdUSART1, read_bank_cmd->data, read_bank_cmd->length);
furi_delay_ms(CB_DELAY);
if(tid_bank->data[2] != read_bank_cmd->data[2]) {
uhf_data_reset(tid_bank);
}

// read user bank
UHFData* user_bank = uhf_response_data_add_new_uhf_data(uhf_response_data);
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, module_rx_callback, user_bank);
read_bank_cmd[9] = 0x03;
furi_hal_uart_tx(FuriHalUartIdUSART1, read_bank_cmd, CMD_READ_LABEL_DATA_STORAGE.length);
furi_delay_ms(50);
read_bank_cmd->data[9] = 0x03;
read_bank_cmd->data[read_bank_cmd->length - 2] = calculate_checksum(read_bank_cmd);
uhf_data_reset(user_bank);
furi_hal_uart_tx(FuriHalUartIdUSART1, read_bank_cmd->data, read_bank_cmd->length);
furi_delay_ms(CB_DELAY);
if(user_bank->data[2] != read_bank_cmd->data[2]) {
uhf_data_reset(user_bank);
}

// for(int i = 0; i < (int)user_bank->length; i++) {
// FURI_LOG_E("user_bank", "data[%d]=%02x", i, user_bank->data[i]);
// }

free(select_cmd);
free(read_bank_cmd);
// for(int i = 0; i < (int)rfu_bank->length; i++) {
// FURI_LOG_E("rfu_bank", "data[%d]=%02x", i, rfu_bank->data[i]);
// }
// for(int i = 0; i < (int)epc_bank->length; i++) {
// FURI_LOG_E("epc_bank", "data[%d]=%02x", i, epc_bank->data[i]);
// }
// for(int i = 0; i < (int)tid_bank->length; i++) {
// FURI_LOG_E("tid_bank", "data[%d]=%02x", i, tid_bank->data[i]);
// }
uhf_data_free(select_cmd);
uhf_data_free(read_bank_cmd);
return UHFWorkerEventSuccess;
}

Expand Down
4 changes: 3 additions & 1 deletion uhf_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ void uhf_worker_start(
UHFWorkerCallback callback,
void* ctx);
void uhf_worker_stop(UHFWorker* uhf_worker);
void uhf_worker_free(UHFWorker* uhf_worker);
void uhf_worker_free(UHFWorker* uhf_worker);
uint8_t calculate_checksum(UHFData* uhf_data);
bool validate_checksum(UHFData* uhf_data);

0 comments on commit 51f9d90

Please sign in to comment.