|
| 1 | +// Methods for RFID transmission |
| 2 | + |
| 3 | +// lfrid |
| 4 | +#include <lib/lfrfid/lfrfid_worker.h> |
| 5 | +#include <toolbox/protocols/protocol_dict.h> |
| 6 | +#include <lfrfid/protocols/lfrfid_protocols.h> |
| 7 | +#include <lfrfid/lfrfid_raw_file.h> |
| 8 | +#include <lib/toolbox/args.h> |
| 9 | + |
| 10 | +#include "action_i.h" |
| 11 | + |
| 12 | +// lifted from flipperzero-firmware/applications/main/lfrfid/lfrfid_cli.c |
| 13 | +void action_rfid_tx(void* context, Item* item) { |
| 14 | + App* app = context; |
| 15 | + FuriString* file_name = item->path; |
| 16 | + |
| 17 | + FlipperFormat* fff_data_file = flipper_format_file_alloc(app->storage); |
| 18 | + FuriString* temp_str; |
| 19 | + temp_str = furi_string_alloc(); |
| 20 | + uint32_t temp_data32; |
| 21 | + |
| 22 | + FuriString* protocol_name; |
| 23 | + FuriString* data_text; |
| 24 | + protocol_name = furi_string_alloc(); |
| 25 | + data_text = furi_string_alloc(); |
| 26 | + |
| 27 | + ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax); |
| 28 | + ProtocolId protocol; |
| 29 | + size_t data_size = protocol_dict_get_max_data_size(dict); |
| 30 | + uint8_t* data = malloc(data_size); |
| 31 | + |
| 32 | + FURI_LOG_I(TAG, "Max dict data size is %d", data_size); |
| 33 | + bool successful_read = false; |
| 34 | + do { |
| 35 | + if(!flipper_format_file_open_existing(fff_data_file, furi_string_get_cstr(file_name))) { |
| 36 | + FURI_LOG_E(TAG, "Error opening %s", furi_string_get_cstr(file_name)); |
| 37 | + break; |
| 38 | + } |
| 39 | + FURI_LOG_I(TAG, "Opened file"); |
| 40 | + if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) { |
| 41 | + FURI_LOG_E(TAG, "Missing or incorrect header"); |
| 42 | + break; |
| 43 | + } |
| 44 | + FURI_LOG_I(TAG, "Read file headers"); |
| 45 | + // TODO: add better header checks here... |
| 46 | + if(!strcmp(furi_string_get_cstr(temp_str), "Flipper RFID key")) { |
| 47 | + } else { |
| 48 | + FURI_LOG_E(TAG, "Type or version mismatch"); |
| 49 | + break; |
| 50 | + } |
| 51 | + |
| 52 | + // read and check the protocol field |
| 53 | + if(!flipper_format_read_string(fff_data_file, "Key type", protocol_name)) { |
| 54 | + FURI_LOG_E(TAG, "Error reading protocol"); |
| 55 | + break; |
| 56 | + } |
| 57 | + protocol = protocol_dict_get_protocol_by_name(dict, furi_string_get_cstr(protocol_name)); |
| 58 | + if(protocol == PROTOCOL_NO) { |
| 59 | + FURI_LOG_E(TAG, "Unknown protocol: %s", furi_string_get_cstr(protocol_name)); |
| 60 | + break; |
| 61 | + } |
| 62 | + FURI_LOG_I(TAG, "Protocol OK"); |
| 63 | + |
| 64 | + // read and check data field |
| 65 | + size_t required_size = protocol_dict_get_data_size(dict, protocol); |
| 66 | + FURI_LOG_I(TAG, "Protocol req data size is %d", required_size); |
| 67 | + if(!flipper_format_read_hex(fff_data_file, "Data", data, required_size)) { |
| 68 | + FURI_LOG_E(TAG, "Error reading data"); |
| 69 | + break; |
| 70 | + } |
| 71 | + // FURI_LOG_I(TAG, "Data: %s", furi_string_get_cstr(data_text)); |
| 72 | + |
| 73 | + // if(data_size != required_size) { |
| 74 | + // FURI_LOG_E( |
| 75 | + // TAG, |
| 76 | + // "%s data needs to be %zu bytes long", |
| 77 | + // protocol_dict_get_name(dict, protocol), |
| 78 | + // required_size); |
| 79 | + // break; |
| 80 | + // } |
| 81 | + |
| 82 | + protocol_dict_set_data(dict, protocol, data, data_size); |
| 83 | + successful_read = true; |
| 84 | + FURI_LOG_I(TAG, "protocol dict setup complete!"); |
| 85 | + } while(false); |
| 86 | + |
| 87 | + if(successful_read) { |
| 88 | + LFRFIDWorker* worker = lfrfid_worker_alloc(dict); |
| 89 | + |
| 90 | + lfrfid_worker_start_thread(worker); |
| 91 | + lfrfid_worker_emulate_start(worker, protocol); |
| 92 | + |
| 93 | + printf("Emulating RFID...\r\nPress Ctrl+C to abort\r\n"); |
| 94 | + int16_t time_ms = 3000; |
| 95 | + int16_t interval_ms = 200; |
| 96 | + while(time_ms > 0) { |
| 97 | + furi_delay_ms(interval_ms); |
| 98 | + time_ms -= interval_ms; |
| 99 | + } |
| 100 | + printf("Emulation stopped\r\n"); |
| 101 | + |
| 102 | + lfrfid_worker_stop(worker); |
| 103 | + lfrfid_worker_stop_thread(worker); |
| 104 | + lfrfid_worker_free(worker); |
| 105 | + } |
| 106 | + |
| 107 | + furi_string_free(temp_str); |
| 108 | + furi_string_free(protocol_name); |
| 109 | + furi_string_free(data_text); |
| 110 | + free(data); |
| 111 | + |
| 112 | + protocol_dict_free(dict); |
| 113 | + |
| 114 | + flipper_format_free(fff_data_file); |
| 115 | +} |
0 commit comments