emulation.c 7.6 KB

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