|
|
@@ -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);
|
|
|
@@ -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;
|
|
|
@@ -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;
|
|
|
}
|
|
|
|