emulation.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. switch (event.event) {
  12. case 0:
  13. if (EmulationState == NfcPlaylistEmulationState_Emulating) {
  14. EmulationState = NfcPlaylistEmulationState_Canceled;
  15. return true;
  16. }
  17. break;
  18. default:
  19. break;
  20. }
  21. return false;
  22. }
  23. void nfc_playlist_emulation_scene_on_exit(void* context) {
  24. NfcPlaylist* nfc_playlist = context;
  25. EmulationState = NfcPlaylistEmulationState_Stopped;
  26. nfc_playlist_emulation_stop(nfc_playlist);
  27. nfc_playlist_emulation_free(nfc_playlist);
  28. popup_reset(nfc_playlist->popup);
  29. }
  30. void nfc_playlist_emulation_setup(void* context) {
  31. NfcPlaylist* nfc_playlist = context;
  32. nfc_playlist->thread = furi_thread_alloc_ex(
  33. "NfcPlaylistEmulationWorker", 8192, nfc_playlist_emulation_task, nfc_playlist);
  34. nfc_playlist->nfc_playlist_worker = nfc_playlist_worker_alloc();
  35. }
  36. void nfc_playlist_emulation_free(NfcPlaylist* nfc_playlist) {
  37. furi_assert(nfc_playlist);
  38. furi_thread_free(nfc_playlist->thread);
  39. nfc_playlist_worker_free(nfc_playlist->nfc_playlist_worker);
  40. nfc_playlist->thread = NULL;
  41. nfc_playlist->nfc_playlist_worker = NULL;
  42. }
  43. void nfc_playlist_emulation_start(NfcPlaylist* nfc_playlist) {
  44. furi_assert(nfc_playlist);
  45. furi_thread_start(nfc_playlist->thread);
  46. }
  47. void nfc_playlist_emulation_stop(NfcPlaylist* nfc_playlist) {
  48. furi_assert(nfc_playlist);
  49. furi_thread_join(nfc_playlist->thread);
  50. }
  51. int32_t nfc_playlist_emulation_task(void* context) {
  52. NfcPlaylist* nfc_playlist = context;
  53. // open/alloc resources
  54. Storage* storage = furi_record_open(RECORD_STORAGE);
  55. Stream* stream = file_stream_alloc(storage);
  56. FuriString* line = furi_string_alloc();
  57. popup_reset(nfc_playlist->popup);
  58. popup_set_context(nfc_playlist->popup, nfc_playlist);
  59. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup);
  60. // Read file
  61. if(file_stream_open(stream, furi_string_get_cstr(nfc_playlist->file_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  62. EmulationState = NfcPlaylistEmulationState_Emulating;
  63. int file_position = 0;
  64. // read the file line by line and print the text
  65. while(stream_read_line(stream, line) && EmulationState == NfcPlaylistEmulationState_Emulating) {
  66. if (options_emulate_delay[nfc_playlist->emulate_delay] > 0) {
  67. if (file_position > 0) {
  68. popup_set_header(nfc_playlist->popup, "Delaying", 64, 10, AlignCenter, AlignTop);
  69. start_error_blink(nfc_playlist);
  70. int time_counter_delay_ms = (options_emulate_delay[nfc_playlist->emulate_delay] * 1000);
  71. do {
  72. char display_text[10];
  73. snprintf(display_text, 10, "%ds", (time_counter_delay_ms/1000));
  74. popup_set_text(nfc_playlist->popup, display_text, 64, 50, AlignCenter, AlignTop);
  75. furi_delay_ms(500);
  76. time_counter_delay_ms -= 500;
  77. } while(time_counter_delay_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating);
  78. } else {
  79. file_position++;
  80. }
  81. }
  82. if (EmulationState != NfcPlaylistEmulationState_Emulating) {
  83. break;
  84. }
  85. char* file_path = (char*)furi_string_get_cstr(line);
  86. char* file_name;
  87. if (strchr(file_path, '/') != NULL) {
  88. file_name = &strrchr(file_path, '/')[1];
  89. } else {
  90. file_name = file_path;
  91. }
  92. char const* file_ext = &strrchr(file_path, '.')[1];
  93. int time_counter_ms = (options_emulate_timeout[nfc_playlist->emulate_timeout] * 1000);
  94. if (storage_file_exists(storage, file_path) == false) {
  95. char popup_header_text[(18 + strlen(file_name))];
  96. snprintf(popup_header_text, (18 + strlen(file_name)), "%s\n%s", "ERROR not found:", file_name);
  97. popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
  98. start_error_blink(nfc_playlist);
  99. while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
  100. char popup_text[9];
  101. snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
  102. popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
  103. furi_delay_ms(500);
  104. time_counter_ms -= 500;
  105. }
  106. }
  107. else if (strcasestr(file_ext, "nfc") == NULL) {
  108. char popup_header_text[(21 + strlen(file_name))];
  109. snprintf(popup_header_text, (21 + strlen(file_name)), "%s\n%s", "ERROR invalid file:", file_name);
  110. popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
  111. start_error_blink(nfc_playlist);
  112. while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
  113. char popup_text[9];
  114. snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
  115. popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
  116. furi_delay_ms(500);
  117. time_counter_ms -= 500;
  118. }
  119. }
  120. else {
  121. char popup_header_text[(12 + strlen(file_name))];
  122. snprintf(popup_header_text, (12 + strlen(file_name)), "%s\n%s", "Emulating:", file_name);
  123. popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
  124. nfc_playlist_worker_set_nfc_data(nfc_playlist->nfc_playlist_worker, file_path);
  125. nfc_playlist_worker_start(nfc_playlist->nfc_playlist_worker);
  126. start_normal_blink(nfc_playlist);
  127. while(nfc_playlist_worker_is_emulating(nfc_playlist->nfc_playlist_worker) && time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
  128. char popup_text[9];
  129. snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
  130. popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
  131. furi_delay_ms(500);
  132. time_counter_ms -= 500;
  133. }
  134. nfc_playlist_worker_stop(nfc_playlist->nfc_playlist_worker);
  135. }
  136. }
  137. popup_reset(nfc_playlist->popup);
  138. popup_set_header(nfc_playlist->popup, EmulationState == NfcPlaylistEmulationState_Canceled ? "Emulation stopped" : "Emulation finished", 64, 10, AlignCenter, AlignTop);
  139. popup_set_text(nfc_playlist->popup, "Press back", 64, 50, AlignCenter, AlignTop);
  140. stop_blink(nfc_playlist);
  141. EmulationState = NfcPlaylistEmulationState_Stopped;
  142. } else {
  143. popup_set_header(nfc_playlist->popup, "Error:", 64, 10, AlignCenter, AlignTop);
  144. popup_set_text(nfc_playlist->popup, "Failed to open file\n/ext/apps_data/nfc_playlist/playlist.txt", 64, 25, AlignCenter, AlignTop);
  145. }
  146. // Free/close resources
  147. furi_string_free(line);
  148. file_stream_close(stream);
  149. stream_free(stream);
  150. // Close storage
  151. return 0;
  152. }