|
|
@@ -2,19 +2,33 @@
|
|
|
#include "uhf_module_cmd.h"
|
|
|
|
|
|
#define DELAY_MS 100
|
|
|
+#define WAIT_TICK 8000 // max wait time in between each byte
|
|
|
+
|
|
|
+volatile uint16_t tick = 0;
|
|
|
|
|
|
void rx_callback(UartIrqEvent event, uint8_t data, void* ctx) {
|
|
|
UNUSED(event);
|
|
|
- Buffer* buf = ctx;
|
|
|
- buffer_append_single(buf, data);
|
|
|
- if(data == FRAME_END) buffer_close(buf);
|
|
|
+ Buffer* buffer = ctx;
|
|
|
+ if(buffer->closed) return; // buffer closed
|
|
|
+ buffer_append_single(buffer, data); // append data
|
|
|
+ if(data == FRAME_END) buffer_close(buffer); // end of frame
|
|
|
+ tick = WAIT_TICK; // reset tick
|
|
|
+}
|
|
|
+
|
|
|
+static void setup_and_send_rx(M100Module* module, uint8_t* cmd, size_t cmd_length) {
|
|
|
+ buffer_reset(module->buf);
|
|
|
+ // furi_hal_uart_set_br(FuriHalUartIdUSART1, module->baudrate);
|
|
|
+ // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, rx_callback, module->buf);
|
|
|
+ tick = WAIT_TICK;
|
|
|
+ furi_hal_uart_tx(FuriHalUartIdUSART1, cmd, cmd_length);
|
|
|
+ while(--tick) {
|
|
|
+ furi_delay_us(5);
|
|
|
+ }
|
|
|
+ buffer_close(module->buf);
|
|
|
}
|
|
|
|
|
|
M100ModuleInfo* m100_module_info_alloc() {
|
|
|
M100ModuleInfo* module_info = (M100ModuleInfo*)malloc(sizeof(M100ModuleInfo));
|
|
|
- module_info->hw_version = NULL;
|
|
|
- module_info->sw_version = NULL;
|
|
|
- module_info->manufacturer = NULL;
|
|
|
return module_info;
|
|
|
}
|
|
|
|
|
|
@@ -27,9 +41,10 @@ void m100_module_info_free(M100ModuleInfo* module_info) {
|
|
|
M100Module* m100_module_alloc() {
|
|
|
M100Module* module = (M100Module*)malloc(sizeof(M100Module));
|
|
|
module->info = m100_module_info_alloc();
|
|
|
- module->buf = buffer_alloc(128);
|
|
|
- furi_hal_uart_set_br(FuriHalUartIdUSART1, DEFAULT_BAUDRATE);
|
|
|
- module->baudrate = (uint16_t)(DEFAULT_BAUDRATE);
|
|
|
+ module->buf = buffer_alloc(MAX_BUFFER_SIZE);
|
|
|
+ module->baudrate = DEFAULT_BAUDRATE;
|
|
|
+ // module->area = DEFAULT_AREA;
|
|
|
+ furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, rx_callback, module->buf);
|
|
|
return module;
|
|
|
}
|
|
|
|
|
|
@@ -67,15 +82,7 @@ uint16_t crc16_genibus(const uint8_t* data, size_t length) {
|
|
|
return crc ^ 0xFFFF; // Post-inversion
|
|
|
}
|
|
|
|
|
|
-char* m100_get_hardware_version(M100Module* module) {
|
|
|
- if(module->info->hw_version != NULL) {
|
|
|
- free(module->info->hw_version);
|
|
|
- module->info->hw_version = NULL;
|
|
|
- }
|
|
|
- buffer_reset(module->buf);
|
|
|
- furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, rx_callback, module->buf);
|
|
|
- furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)&CMD_HW_VERSION.cmd[0], CMD_HW_VERSION.length);
|
|
|
- furi_delay_ms(DELAY_MS);
|
|
|
+char* _m100_info_helper(M100Module* module, char** info) {
|
|
|
if(!buffer_get_size(module->buf)) return NULL;
|
|
|
uint8_t* data = buffer_get_data(module->buf);
|
|
|
uint16_t payload_len = data[3];
|
|
|
@@ -84,68 +91,38 @@ char* m100_get_hardware_version(M100Module* module) {
|
|
|
for(int i = 0; i < payload_len; i++) {
|
|
|
furi_string_cat_printf(temp_str, "%c", data[6 + i]);
|
|
|
}
|
|
|
- char* hw_version = (char*)malloc(sizeof(char) * payload_len);
|
|
|
- memcpy(hw_version, furi_string_get_cstr(temp_str), payload_len);
|
|
|
- module->info->hw_version = hw_version;
|
|
|
+ if(*info == NULL) {
|
|
|
+ *info = (char*)malloc(sizeof(char) * payload_len);
|
|
|
+ } else {
|
|
|
+ for(size_t i = 0; i < strlen(*info); i++) {
|
|
|
+ (*info)[i] = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ memcpy(*info, furi_string_get_cstr(temp_str), payload_len);
|
|
|
furi_string_free(temp_str);
|
|
|
- return module->info->hw_version;
|
|
|
+ return *info;
|
|
|
+}
|
|
|
+
|
|
|
+char* m100_get_hardware_version(M100Module* module) {
|
|
|
+ setup_and_send_rx(module, (uint8_t*)&CMD_HW_VERSION.cmd[0], CMD_HW_VERSION.length);
|
|
|
+ return _m100_info_helper(module, &module->info->hw_version);
|
|
|
}
|
|
|
char* m100_get_software_version(M100Module* module) {
|
|
|
- if(module->info->sw_version != NULL) {
|
|
|
- free(module->info->sw_version);
|
|
|
- module->info->sw_version = NULL;
|
|
|
- }
|
|
|
- buffer_reset(module->buf);
|
|
|
- furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, rx_callback, module->buf);
|
|
|
- furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)&CMD_SW_VERSION.cmd[0], CMD_SW_VERSION.length);
|
|
|
- furi_delay_ms(DELAY_MS);
|
|
|
- if(!buffer_get_size(module->buf)) return NULL;
|
|
|
- uint8_t* data = buffer_get_data(module->buf);
|
|
|
- uint16_t payload_len = data[3];
|
|
|
- payload_len = (payload_len << 8) + data[4];
|
|
|
- FuriString* temp_str = furi_string_alloc();
|
|
|
- for(int i = 0; i < payload_len; i++) {
|
|
|
- furi_string_cat_printf(temp_str, "%c", data[6 + i]);
|
|
|
- }
|
|
|
- char* sw_version = (char*)malloc(sizeof(char) * payload_len);
|
|
|
- memcpy(sw_version, furi_string_get_cstr(temp_str), payload_len);
|
|
|
- module->info->sw_version = sw_version;
|
|
|
- furi_string_free(temp_str);
|
|
|
- return module->info->sw_version;
|
|
|
+ setup_and_send_rx(module, (uint8_t*)&CMD_SW_VERSION.cmd[0], CMD_SW_VERSION.length);
|
|
|
+ return _m100_info_helper(module, &module->info->sw_version);
|
|
|
}
|
|
|
char* m100_get_manufacturers(M100Module* module) {
|
|
|
- if(module->info->manufacturer != NULL) {
|
|
|
- free(module->info->manufacturer);
|
|
|
- module->info->manufacturer = NULL;
|
|
|
- }
|
|
|
- buffer_reset(module->buf);
|
|
|
- furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, rx_callback, module->buf);
|
|
|
- furi_hal_uart_tx(
|
|
|
- FuriHalUartIdUSART1, (uint8_t*)&CMD_MANUFACTURERS.cmd[0], CMD_MANUFACTURERS.length);
|
|
|
- furi_delay_ms(DELAY_MS);
|
|
|
- if(!buffer_get_size(module->buf)) return NULL;
|
|
|
- uint8_t* data = buffer_get_data(module->buf);
|
|
|
- uint16_t payload_len = data[3];
|
|
|
- payload_len = (payload_len << 8) + data[4];
|
|
|
- FuriString* temp_str = furi_string_alloc();
|
|
|
- for(int i = 0; i < payload_len; i++) {
|
|
|
- furi_string_cat_printf(temp_str, "%c", data[6 + i]);
|
|
|
- }
|
|
|
- char* manufacturer = (char*)malloc(sizeof(char) * payload_len);
|
|
|
- memcpy(manufacturer, furi_string_get_cstr(temp_str), payload_len);
|
|
|
- module->info->manufacturer = manufacturer;
|
|
|
- furi_string_free(temp_str);
|
|
|
- return module->info->manufacturer;
|
|
|
+ setup_and_send_rx(module, (uint8_t*)&CMD_MANUFACTURERS.cmd[0], CMD_MANUFACTURERS.length);
|
|
|
+ return _m100_info_helper(module, &module->info->manufacturer);
|
|
|
}
|
|
|
|
|
|
-M100ResponseType m100_send_single_poll(M100Module* module, UHFTag* uhf_tag) {
|
|
|
- buffer_reset(module->buf);
|
|
|
- furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, rx_callback, module->buf);
|
|
|
- furi_hal_uart_tx(
|
|
|
- FuriHalUartIdUSART1, (uint8_t*)&CMD_SINGLE_POLLING.cmd[0], CMD_SINGLE_POLLING.length);
|
|
|
- furi_delay_ms(DELAY_MS);
|
|
|
+M100ResponseType m100_single_poll(M100Module* module, UHFTag* uhf_tag) {
|
|
|
+ setup_and_send_rx(module, (uint8_t*)&CMD_SINGLE_POLLING.cmd[0], CMD_SINGLE_POLLING.length);
|
|
|
uint8_t* data = buffer_get_data(module->buf);
|
|
|
size_t length = buffer_get_size(module->buf);
|
|
|
+ for(size_t i = 0; i < length; i++) {
|
|
|
+ FURI_LOG_E("TAG", "data[%d] = %02X", i, data[i]);
|
|
|
+ }
|
|
|
if(length <= 8 && data[2] == 0xFF) return M100NoTagResponse;
|
|
|
uint16_t pc = data[6];
|
|
|
uint16_t crc = 0;
|
|
|
@@ -171,13 +148,12 @@ M100ResponseType m100_send_single_poll(M100Module* module, UHFTag* uhf_tag) {
|
|
|
}
|
|
|
|
|
|
M100ResponseType m100_set_select(M100Module* module, UHFTag* uhf_tag) {
|
|
|
- buffer_reset(module->buf);
|
|
|
// Set select
|
|
|
uint8_t cmd[MAX_BUFFER_SIZE];
|
|
|
size_t cmd_length = CMD_SET_SELECT_PARAMETER.length;
|
|
|
size_t mask_length_bytes = uhf_tag->epc->size;
|
|
|
size_t mask_length_bits = mask_length_bytes * 8;
|
|
|
- // payload len = sel param len + ptr len + mask len + epc len
|
|
|
+ // payload len == sel param len + ptr len + mask len + epc len
|
|
|
size_t payload_len = 7 + mask_length_bytes;
|
|
|
memcpy(cmd, CMD_SET_SELECT_PARAMETER.cmd, cmd_length);
|
|
|
// set new length
|
|
|
@@ -200,9 +176,8 @@ M100ResponseType m100_set_select(M100Module* module, UHFTag* uhf_tag) {
|
|
|
cmd[cmd_length - 2] = checksum(cmd + 1, 11 + mask_length_bytes);
|
|
|
// end frame
|
|
|
cmd[cmd_length - 1] = FRAME_END;
|
|
|
- furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, rx_callback, module->buf);
|
|
|
- furi_hal_uart_tx(FuriHalUartIdUSART1, cmd, 12 + mask_length_bytes + 3);
|
|
|
- furi_delay_ms(DELAY_MS);
|
|
|
+
|
|
|
+ setup_and_send_rx(module, cmd, 12 + mask_length_bytes + 3);
|
|
|
|
|
|
uint8_t* data = buffer_get_data(module->buf);
|
|
|
if(checksum(data + 1, 5) != data[6]) return M100ValidationFail; // error in rx
|
|
|
@@ -236,7 +211,6 @@ M100ResponseType m100_read_label_data_storage(
|
|
|
Will probably remove UHFTag as param and get it from get selected tag
|
|
|
*/
|
|
|
if(bank == EPCBank) return M100Success;
|
|
|
- buffer_reset(module->buf);
|
|
|
uint8_t cmd[MAX_BUFFER_SIZE];
|
|
|
size_t cmd_length = CMD_READ_LABEL_DATA_STORAGE_AREA.length;
|
|
|
memcpy(cmd, CMD_READ_LABEL_DATA_STORAGE_AREA.cmd, cmd_length);
|
|
|
@@ -252,21 +226,21 @@ M100ResponseType m100_read_label_data_storage(
|
|
|
cmd[13] = word_count & 0xFF;
|
|
|
// calc checksum
|
|
|
cmd[cmd_length - 2] = checksum(cmd + 1, cmd_length - 3);
|
|
|
- furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, rx_callback, module->buf);
|
|
|
- furi_hal_uart_tx(FuriHalUartIdUSART1, cmd, cmd_length);
|
|
|
- furi_delay_ms(DELAY_MS);
|
|
|
+
|
|
|
+ setup_and_send_rx(module, cmd, cmd_length);
|
|
|
+
|
|
|
uint8_t* data = buffer_get_data(module->buf);
|
|
|
uint16_t payload_len = data[3];
|
|
|
payload_len = (payload_len << 8) + data[4];
|
|
|
size_t ptr_offset = 5 /*<-ptr offset*/ + uhf_tag->epc->size + 3 /*<-pc + ul*/;
|
|
|
size_t bank_data_length = payload_len - (ptr_offset - 5 /*dont include the offset*/);
|
|
|
// print paylod length ptr offset and bank data length
|
|
|
- FURI_LOG_E(
|
|
|
- "TAG",
|
|
|
- "payload_len: %d, ptr_offset: %d, bank_data_length: %d",
|
|
|
- payload_len,
|
|
|
- ptr_offset,
|
|
|
- bank_data_length);
|
|
|
+ // FURI_LOG_E(
|
|
|
+ // "TAG",
|
|
|
+ // "payload_len: %d, ptr_offset: %d, bank_data_length: %d",
|
|
|
+ // payload_len,
|
|
|
+ // ptr_offset,
|
|
|
+ // bank_data_length);
|
|
|
if(data[2] == 0xFF) {
|
|
|
if(payload_len == 0x0001) return M100NoTagResponse;
|
|
|
return M100MemoryOverrun;
|
|
|
@@ -286,7 +260,6 @@ M100ResponseType m100_write_label_data_storage(
|
|
|
BankType bank,
|
|
|
uint16_t source_address,
|
|
|
uint32_t access_pwd) {
|
|
|
- buffer_reset(module->buf);
|
|
|
uint8_t cmd[MAX_BUFFER_SIZE];
|
|
|
size_t cmd_length = CMD_WRITE_LABEL_DATA_STORE.length;
|
|
|
memcpy(cmd, CMD_WRITE_LABEL_DATA_STORE.cmd, cmd_length);
|
|
|
@@ -337,14 +310,15 @@ M100ResponseType m100_write_label_data_storage(
|
|
|
cmd[cmd_length - 2] = checksum(cmd + 1, cmd_length - 3);
|
|
|
cmd[cmd_length - 1] = FRAME_END;
|
|
|
// send cmd
|
|
|
- furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, rx_callback, module->buf);
|
|
|
- furi_hal_uart_tx(FuriHalUartIdUSART1, cmd, cmd_length);
|
|
|
- unsigned int delay = DELAY_MS / 2;
|
|
|
- unsigned int timeout = 15;
|
|
|
- while(!buffer_get_size(module->buf)) {
|
|
|
- furi_delay_ms(delay);
|
|
|
- if(!timeout--) break;
|
|
|
- }
|
|
|
+ // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, rx_callback, module->buf);
|
|
|
+ // furi_hal_uart_tx(FuriHalUartIdUSART1, cmd, cmd_length);
|
|
|
+ // unsigned int delay = DELAY_MS / 2;
|
|
|
+ // unsigned int timeout = 15;
|
|
|
+ // while(!buffer_get_size(module->buf)) {
|
|
|
+ // furi_delay_ms(delay);
|
|
|
+ // if(!timeout--) break;
|
|
|
+ // }
|
|
|
+ setup_and_send_rx(module, cmd, cmd_length);
|
|
|
uint8_t* buff_data = buffer_get_data(module->buf);
|
|
|
size_t buff_length = buffer_get_size(module->buf);
|
|
|
if(buff_data[2] == 0xFF && buff_length == 8)
|
|
|
@@ -359,11 +333,13 @@ void m100_set_baudrate(M100Module* module, uint32_t baudrate) {
|
|
|
memcpy(cmd, CMD_SET_COMMUNICATION_BAUD_RATE.cmd, length);
|
|
|
uint16_t br_mod = baudrate / 100; // module format
|
|
|
cmd[6] = 0xFF & br_mod; // pow LSB
|
|
|
- cmd[5] = 0xFF & (br_mod >> 4); // pow MSB
|
|
|
+ cmd[5] = 0xFF & (br_mod >> 8); // pow MSB
|
|
|
+ cmd[length - 2] = checksum(cmd + 1, length - 3);
|
|
|
furi_hal_uart_tx(FuriHalUartIdUSART1, cmd, length);
|
|
|
furi_hal_uart_set_br(FuriHalUartIdUSART1, baudrate);
|
|
|
module->baudrate = baudrate;
|
|
|
}
|
|
|
+
|
|
|
bool m100_set_working_area(M100Module* module, WorkingArea area) {
|
|
|
size_t length = CMD_SET_WORK_AREA.length;
|
|
|
uint8_t cmd[length];
|
|
|
@@ -376,16 +352,19 @@ bool m100_set_working_area(M100Module* module, WorkingArea area) {
|
|
|
module->area = area;
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
bool m100_set_working_channel(M100Module* module, WorkingChannel channel) {
|
|
|
UNUSED(module);
|
|
|
UNUSED(channel);
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
bool m100_set_transmitting_power(M100Module* module, uint16_t power) {
|
|
|
UNUSED(module);
|
|
|
UNUSED(power);
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
bool m100_set_freq_hopping(M100Module* module, bool hopping) {
|
|
|
UNUSED(module);
|
|
|
UNUSED(hopping);
|