emulation.c 7.5 KB

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