nfc_playlist_scene_emulation.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "../nfc_playlist.h"
  2. typedef enum NfcPlaylistEmulationState {
  3. NfcPlaylistEmulationState_Emulating,
  4. NfcPlaylistEmulationState_Stopped,
  5. NfcPlaylistEmulationState_Canceled
  6. } NfcPlaylistEmulationState;
  7. NfcPlaylistEmulationState EmulationState = NfcPlaylistEmulationState_Stopped;
  8. int32_t nfc_playlist_emulation_task(void* context) {
  9. NfcPlaylist* nfc_playlist = context;
  10. Storage* storage = furi_record_open(RECORD_STORAGE);
  11. Stream* stream = file_stream_alloc(storage);
  12. bool skip_delay = false;
  13. popup_reset(nfc_playlist->popup);
  14. popup_set_context(nfc_playlist->popup, nfc_playlist);
  15. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup);
  16. if(file_stream_open(
  17. stream,
  18. furi_string_get_cstr(nfc_playlist->settings.playlist_path),
  19. FSAM_READ,
  20. FSOM_OPEN_EXISTING)) {
  21. EmulationState = NfcPlaylistEmulationState_Emulating;
  22. int file_position = 0;
  23. FuriString* line = furi_string_alloc();
  24. FuriString* tmp_header_str = furi_string_alloc();
  25. FuriString* tmp_counter_str = furi_string_alloc();
  26. FuriString* tmp_file_name = furi_string_alloc();
  27. //
  28. char tmp_file_ext[6] = {0};
  29. //FuriString* tmp_file_ext = furi_string_alloc();
  30. while(stream_read_line(stream, line) &&
  31. EmulationState == NfcPlaylistEmulationState_Emulating) {
  32. char* file_path = (char*)furi_string_get_cstr(line);
  33. if(strspn(file_path, " \t\n\r") == strlen(file_path)) {
  34. continue;
  35. }
  36. if(nfc_playlist->settings.emulate_delay > 0 && file_position != 0 && !skip_delay) {
  37. popup_set_header(nfc_playlist->popup, "Delaying", 64, 10, AlignCenter, AlignTop);
  38. start_blink(nfc_playlist, NfcPlaylistLedState_Error);
  39. int time_counter_delay_ms =
  40. (options_emulate_delay[nfc_playlist->settings.emulate_delay] * 1000);
  41. while(time_counter_delay_ms > 0 &&
  42. EmulationState == NfcPlaylistEmulationState_Emulating) {
  43. furi_string_printf(tmp_counter_str, "%ds", (time_counter_delay_ms / 1000));
  44. popup_set_text(
  45. nfc_playlist->popup,
  46. furi_string_get_cstr(tmp_counter_str),
  47. 64,
  48. 50,
  49. AlignCenter,
  50. AlignTop);
  51. furi_delay_ms(50);
  52. time_counter_delay_ms -= 50;
  53. furi_string_reset(tmp_counter_str);
  54. };
  55. } else if(nfc_playlist->settings.emulate_delay > 0) {
  56. skip_delay = false;
  57. file_position++;
  58. }
  59. if(EmulationState != NfcPlaylistEmulationState_Emulating) {
  60. break;
  61. }
  62. path_extract_filename(line, tmp_file_name, false);
  63. //path_extract_ext_str(line, tmp_file_ext);
  64. // why?
  65. path_extract_extension(line, tmp_file_ext, 6);
  66. int time_counter_ms =
  67. (options_emulate_timeout[nfc_playlist->settings.emulate_timeout] * 1000);
  68. // use strcmp instead furi string to compare file extension
  69. if(strcmp(tmp_file_ext, ".nfc") != 0) {
  70. if(nfc_playlist->settings.skip_error) {
  71. skip_delay = true;
  72. continue;
  73. }
  74. furi_string_printf(
  75. tmp_header_str,
  76. "ERROR invalid file:\n%s",
  77. furi_string_get_cstr(tmp_file_name));
  78. popup_set_header(
  79. nfc_playlist->popup,
  80. furi_string_get_cstr(tmp_header_str),
  81. 64,
  82. 10,
  83. AlignCenter,
  84. AlignTop);
  85. start_blink(nfc_playlist, NfcPlaylistLedState_Error);
  86. while(time_counter_ms > 0 &&
  87. EmulationState == NfcPlaylistEmulationState_Emulating) {
  88. furi_string_printf(tmp_counter_str, "%ds", (time_counter_ms / 1000));
  89. popup_set_text(
  90. nfc_playlist->popup,
  91. furi_string_get_cstr(tmp_counter_str),
  92. 64,
  93. 50,
  94. AlignCenter,
  95. AlignTop);
  96. furi_delay_ms(50);
  97. time_counter_ms -= 50;
  98. };
  99. } else if(!storage_file_exists(storage, file_path)) {
  100. if(nfc_playlist->settings.skip_error) {
  101. skip_delay = true;
  102. continue;
  103. }
  104. furi_string_printf(
  105. tmp_header_str, "ERROR not found:\n%s", furi_string_get_cstr(tmp_file_name));
  106. popup_set_header(
  107. nfc_playlist->popup,
  108. furi_string_get_cstr(tmp_header_str),
  109. 64,
  110. 10,
  111. AlignCenter,
  112. AlignTop);
  113. start_blink(nfc_playlist, NfcPlaylistLedState_Error);
  114. while(time_counter_ms > 0 &&
  115. EmulationState == NfcPlaylistEmulationState_Emulating) {
  116. furi_string_printf(tmp_counter_str, "%ds", (time_counter_ms / 1000));
  117. popup_set_text(
  118. nfc_playlist->popup,
  119. furi_string_get_cstr(tmp_counter_str),
  120. 64,
  121. 50,
  122. AlignCenter,
  123. AlignTop);
  124. furi_delay_ms(50);
  125. time_counter_ms -= 50;
  126. };
  127. } else {
  128. furi_string_printf(
  129. tmp_header_str, "Emulating:\n%s", furi_string_get_cstr(tmp_file_name));
  130. popup_set_header(
  131. nfc_playlist->popup,
  132. furi_string_get_cstr(tmp_header_str),
  133. 64,
  134. 10,
  135. AlignCenter,
  136. AlignTop);
  137. nfc_playlist_emulation_worker_set_nfc_data(
  138. nfc_playlist->nfc_playlist_emulation_worker, file_path);
  139. nfc_playlist_emulation_worker_start(nfc_playlist->nfc_playlist_emulation_worker);
  140. start_blink(nfc_playlist, NfcPlaylistLedState_Normal);
  141. while(nfc_playlist_emulation_worker_is_emulating(
  142. nfc_playlist->nfc_playlist_emulation_worker) &&
  143. time_counter_ms > 0 &&
  144. EmulationState == NfcPlaylistEmulationState_Emulating) {
  145. furi_string_printf(tmp_counter_str, "%ds", (time_counter_ms / 1000));
  146. popup_set_text(
  147. nfc_playlist->popup,
  148. furi_string_get_cstr(tmp_counter_str),
  149. 64,
  150. 50,
  151. AlignCenter,
  152. AlignTop);
  153. furi_delay_ms(50);
  154. time_counter_ms -= 50;
  155. };
  156. nfc_playlist_emulation_worker_stop(nfc_playlist->nfc_playlist_emulation_worker);
  157. nfc_playlist_emulation_worker_clear_nfc_data(
  158. nfc_playlist->nfc_playlist_emulation_worker);
  159. }
  160. }
  161. popup_reset(nfc_playlist->popup);
  162. if(nfc_playlist->settings.playlist_length == 0) {
  163. popup_set_header(nfc_playlist->popup, "Empty playlist", 64, 10, AlignCenter, AlignTop);
  164. } else {
  165. popup_set_header(
  166. nfc_playlist->popup,
  167. EmulationState == NfcPlaylistEmulationState_Canceled ? "Emulation stopped" :
  168. "Emulation finished",
  169. 64,
  170. 10,
  171. AlignCenter,
  172. AlignTop);
  173. }
  174. popup_set_text(nfc_playlist->popup, "Press back", 64, 50, AlignCenter, AlignTop);
  175. stop_blink(nfc_playlist);
  176. EmulationState = NfcPlaylistEmulationState_Stopped;
  177. furi_string_free(line);
  178. furi_string_free(tmp_header_str);
  179. furi_string_free(tmp_counter_str);
  180. furi_string_free(tmp_file_name);
  181. //furi_string_free(tmp_file_ext);
  182. } else {
  183. popup_set_header(
  184. nfc_playlist->popup, "Failed to open playlist", 64, 10, AlignCenter, AlignTop);
  185. popup_set_text(nfc_playlist->popup, "Press back", 64, 50, AlignCenter, AlignTop);
  186. }
  187. file_stream_close(stream);
  188. furi_record_close(RECORD_STORAGE);
  189. stream_free(stream);
  190. return 0;
  191. }
  192. void nfc_playlist_emulation_setup(void* context) {
  193. NfcPlaylist* nfc_playlist = context;
  194. nfc_playlist->thread = furi_thread_alloc_ex(
  195. "NfcPlaylistEmulationWorker", 4096, nfc_playlist_emulation_task, nfc_playlist);
  196. nfc_playlist->nfc_playlist_emulation_worker = nfc_playlist_emulation_worker_alloc();
  197. }
  198. void nfc_playlist_emulation_free(NfcPlaylist* nfc_playlist) {
  199. furi_assert(nfc_playlist);
  200. furi_thread_free(nfc_playlist->thread);
  201. nfc_playlist_emulation_worker_free(nfc_playlist->nfc_playlist_emulation_worker);
  202. nfc_playlist->thread = NULL;
  203. nfc_playlist->nfc_playlist_emulation_worker = NULL;
  204. }
  205. void nfc_playlist_emulation_start(NfcPlaylist* nfc_playlist) {
  206. furi_assert(nfc_playlist);
  207. furi_thread_start(nfc_playlist->thread);
  208. }
  209. void nfc_playlist_emulation_stop(NfcPlaylist* nfc_playlist) {
  210. furi_assert(nfc_playlist);
  211. furi_thread_join(nfc_playlist->thread);
  212. }
  213. void nfc_playlist_emulation_scene_on_enter(void* context) {
  214. NfcPlaylist* nfc_playlist = context;
  215. nfc_playlist_emulation_setup(nfc_playlist);
  216. nfc_playlist_emulation_start(nfc_playlist);
  217. }
  218. bool nfc_playlist_emulation_scene_on_event(void* context, SceneManagerEvent event) {
  219. UNUSED(context);
  220. bool consumed = false;
  221. if(event.event == 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
  222. EmulationState = NfcPlaylistEmulationState_Canceled;
  223. consumed = true;
  224. }
  225. return consumed;
  226. }
  227. void nfc_playlist_emulation_scene_on_exit(void* context) {
  228. NfcPlaylist* nfc_playlist = context;
  229. EmulationState = NfcPlaylistEmulationState_Stopped;
  230. nfc_playlist_emulation_stop(nfc_playlist);
  231. nfc_playlist_emulation_free(nfc_playlist);
  232. popup_reset(nfc_playlist->popup);
  233. }