action_rfid.c 3.8 KB

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