// Methods for RFID transmission // lfrid #include #include #include #include #include #include "action_i.h" // lifted from flipperzero-firmware/applications/main/lfrfid/lfrfid_cli.c void action_rfid_tx(void* context, Item* item) { App* app = context; FuriString* file_name = item->path; FlipperFormat* fff_data_file = flipper_format_file_alloc(app->storage); FuriString* temp_str; temp_str = furi_string_alloc(); uint32_t temp_data32; FuriString* protocol_name; FuriString* data_text; protocol_name = furi_string_alloc(); data_text = furi_string_alloc(); ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax); ProtocolId protocol; size_t data_size = protocol_dict_get_max_data_size(dict); uint8_t* data = malloc(data_size); FURI_LOG_I(TAG, "Max dict data size is %d", data_size); bool successful_read = false; do { if(!flipper_format_file_open_existing(fff_data_file, furi_string_get_cstr(file_name))) { FURI_LOG_E(TAG, "Error opening %s", furi_string_get_cstr(file_name)); break; } FURI_LOG_I(TAG, "Opened file"); if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) { FURI_LOG_E(TAG, "Missing or incorrect header"); break; } FURI_LOG_I(TAG, "Read file headers"); // TODO: add better header checks here... if(!strcmp(furi_string_get_cstr(temp_str), "Flipper RFID key")) { } else { FURI_LOG_E(TAG, "Type or version mismatch"); break; } // read and check the protocol field if(!flipper_format_read_string(fff_data_file, "Key type", protocol_name)) { FURI_LOG_E(TAG, "Error reading protocol"); break; } protocol = protocol_dict_get_protocol_by_name(dict, furi_string_get_cstr(protocol_name)); if(protocol == PROTOCOL_NO) { FURI_LOG_E(TAG, "Unknown protocol: %s", furi_string_get_cstr(protocol_name)); break; } FURI_LOG_I(TAG, "Protocol OK"); // read and check data field size_t required_size = protocol_dict_get_data_size(dict, protocol); FURI_LOG_I(TAG, "Protocol req data size is %d", required_size); if(!flipper_format_read_hex(fff_data_file, "Data", data, required_size)) { FURI_LOG_E(TAG, "Error reading data"); break; } // FURI_LOG_I(TAG, "Data: %s", furi_string_get_cstr(data_text)); // if(data_size != required_size) { // FURI_LOG_E( // TAG, // "%s data needs to be %zu bytes long", // protocol_dict_get_name(dict, protocol), // required_size); // break; // } protocol_dict_set_data(dict, protocol, data, data_size); successful_read = true; FURI_LOG_I(TAG, "protocol dict setup complete!"); } while(false); if(successful_read) { LFRFIDWorker* worker = lfrfid_worker_alloc(dict); lfrfid_worker_start_thread(worker); lfrfid_worker_emulate_start(worker, protocol); printf("Emulating RFID...\r\nPress Ctrl+C to abort\r\n"); int16_t time_ms = 3000; int16_t interval_ms = 200; while(time_ms > 0) { furi_delay_ms(interval_ms); time_ms -= interval_ms; } printf("Emulation stopped\r\n"); lfrfid_worker_stop(worker); lfrfid_worker_stop_thread(worker); lfrfid_worker_free(worker); } furi_string_free(temp_str); furi_string_free(protocol_name); furi_string_free(data_text); free(data); protocol_dict_free(dict); flipper_format_free(fff_data_file); }