emulation.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "nfc_playlist.h"
  2. #include "scences/emulation.h"
  3. NfcPlaylistEmulationState EmulationState = NfcPlaylistEmulationState_Stopped;
  4. void nfc_playlist_emulation_scene_on_enter(void* context) {
  5. NfcPlaylist* nfc_playlist = context;
  6. nfc_playlist_emulation_setup(nfc_playlist);
  7. nfc_playlist_emulation_start(nfc_playlist);
  8. }
  9. bool nfc_playlist_emulation_scene_on_event(void* context, SceneManagerEvent event) {
  10. UNUSED(context);
  11. if (event.event == 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
  12. EmulationState = NfcPlaylistEmulationState_Canceled;
  13. return true;
  14. }
  15. return false;
  16. }
  17. void nfc_playlist_emulation_scene_on_exit(void* context) {
  18. NfcPlaylist* nfc_playlist = context;
  19. EmulationState = NfcPlaylistEmulationState_Stopped;
  20. nfc_playlist_emulation_stop(nfc_playlist);
  21. nfc_playlist_emulation_free(nfc_playlist);
  22. popup_reset(nfc_playlist->popup);
  23. }
  24. void nfc_playlist_emulation_setup(void* context) {
  25. NfcPlaylist* nfc_playlist = context;
  26. nfc_playlist->thread = furi_thread_alloc_ex("NfcPlaylistEmulationWorker", 8192, nfc_playlist_emulation_task, nfc_playlist);
  27. nfc_playlist->nfc_playlist_worker = nfc_playlist_worker_alloc();
  28. }
  29. void nfc_playlist_emulation_free(NfcPlaylist* nfc_playlist) {
  30. furi_assert(nfc_playlist);
  31. furi_thread_free(nfc_playlist->thread);
  32. nfc_playlist_worker_free(nfc_playlist->nfc_playlist_worker);
  33. nfc_playlist->thread = NULL;
  34. nfc_playlist->nfc_playlist_worker = NULL;
  35. }
  36. void nfc_playlist_emulation_start(NfcPlaylist* nfc_playlist) {
  37. furi_assert(nfc_playlist);
  38. furi_thread_start(nfc_playlist->thread);
  39. }
  40. void nfc_playlist_emulation_stop(NfcPlaylist* nfc_playlist) {
  41. furi_assert(nfc_playlist);
  42. furi_thread_join(nfc_playlist->thread);
  43. }
  44. int32_t nfc_playlist_emulation_task(void* context) {
  45. NfcPlaylist* nfc_playlist = context;
  46. Storage* storage = furi_record_open(RECORD_STORAGE);
  47. Stream* stream = file_stream_alloc(storage);
  48. FuriString* line = furi_string_alloc();
  49. popup_reset(nfc_playlist->popup);
  50. popup_set_context(nfc_playlist->popup, nfc_playlist);
  51. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup);
  52. if (file_stream_open(stream, furi_string_get_cstr(nfc_playlist->settings.file_path), FSAM_READ, FSOM_OPEN_EXISTING) && nfc_playlist->settings.file_selected_check) {
  53. EmulationState = NfcPlaylistEmulationState_Emulating;
  54. int file_position = 0;
  55. while(stream_read_line(stream, line) && EmulationState == NfcPlaylistEmulationState_Emulating) {
  56. char* file_path = (char*)furi_string_get_cstr(line);
  57. if (strlen(file_path) <= 1) {continue;}
  58. if (nfc_playlist->settings.emulate_delay > 0 && file_position > 0) {
  59. popup_set_header(nfc_playlist->popup, "Delaying", 64, 10, AlignCenter, AlignTop);
  60. start_blink(nfc_playlist, NfcPlaylistLedState_Error);
  61. int time_counter_delay_ms = (options_emulate_delay[nfc_playlist->settings.emulate_delay]*1000);
  62. do {
  63. char display_text[10];
  64. snprintf(display_text, 10, "%ds", (time_counter_delay_ms/1000));
  65. popup_set_text(nfc_playlist->popup, display_text, 64, 50, AlignCenter, AlignTop);
  66. furi_delay_ms(50);
  67. time_counter_delay_ms -= 50;
  68. } while(time_counter_delay_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating);
  69. } else if (nfc_playlist->settings.emulate_delay > 0) {
  70. file_position++;
  71. }
  72. if (EmulationState != NfcPlaylistEmulationState_Emulating) {break;}
  73. char const* full_file_name = strchr(file_path, '/') != NULL ? &strrchr(file_path, '/')[1] : file_path;
  74. char file_name[sizeof(full_file_name)];
  75. strcpy(file_name, full_file_name);
  76. strtok(file_name, ".");
  77. char const* file_ext = &strrchr(file_path, '.')[1];
  78. int time_counter_ms = (options_emulate_timeout[nfc_playlist->settings.emulate_timeout]*1000);
  79. if (storage_file_exists(storage, file_path) == false) {
  80. int popup_header_text_size = strlen(file_name) + 18;
  81. char popup_header_text[popup_header_text_size];
  82. snprintf(popup_header_text, popup_header_text_size, "%s\n%s", "ERROR not found:", file_name);
  83. popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
  84. start_blink(nfc_playlist, NfcPlaylistLedState_Error);
  85. while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
  86. char popup_text[9];
  87. snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
  88. popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
  89. furi_delay_ms(50);
  90. time_counter_ms -= 50;
  91. }
  92. } else if (strcasestr(file_ext, "nfc") == NULL) {
  93. int popup_header_text_size = strlen(file_name) + 21;
  94. char popup_header_text[popup_header_text_size];
  95. snprintf(popup_header_text, popup_header_text_size, "%s\n%s", "ERROR invalid file:", file_name);
  96. popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
  97. start_blink(nfc_playlist, NfcPlaylistLedState_Error);
  98. while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
  99. char popup_text[9];
  100. snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
  101. popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
  102. furi_delay_ms(50);
  103. time_counter_ms -= 50;
  104. }
  105. } else {
  106. int popup_header_text_size = strlen(file_name) + 12;
  107. char popup_header_text[popup_header_text_size];
  108. snprintf(popup_header_text, popup_header_text_size, "%s\n%s", "Emulating:", file_name);
  109. popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
  110. nfc_playlist_worker_set_nfc_data(nfc_playlist->nfc_playlist_worker, file_path);
  111. nfc_playlist_worker_start(nfc_playlist->nfc_playlist_worker);
  112. start_blink(nfc_playlist, NfcPlaylistLedState_Normal);
  113. while(nfc_playlist_worker_is_emulating(nfc_playlist->nfc_playlist_worker) && time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
  114. char popup_text[9];
  115. snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
  116. popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
  117. furi_delay_ms(50);
  118. time_counter_ms -= 50;
  119. }
  120. nfc_playlist_worker_stop(nfc_playlist->nfc_playlist_worker);
  121. }
  122. }
  123. popup_reset(nfc_playlist->popup);
  124. popup_set_header(nfc_playlist->popup, EmulationState == NfcPlaylistEmulationState_Canceled ? "Emulation stopped" : "Emulation finished", 64, 10, AlignCenter, AlignTop);
  125. popup_set_text(nfc_playlist->popup, "Press back", 64, 50, AlignCenter, AlignTop);
  126. stop_blink(nfc_playlist);
  127. EmulationState = NfcPlaylistEmulationState_Stopped;
  128. } else {
  129. popup_set_header(nfc_playlist->popup, "Failed to open playlist", 64, 10, AlignCenter, AlignTop);
  130. popup_set_text(nfc_playlist->popup, "Press back", 64, 50, AlignCenter, AlignTop);
  131. }
  132. furi_string_free(line);
  133. file_stream_close(stream);
  134. furi_record_close(RECORD_STORAGE);
  135. stream_free(stream);
  136. return 0;
  137. }