emulation.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "nfc_playlist.h"
  2. #include "scences/emulation.h"
  3. void nfc_playlist_emulation_scene_on_enter(void* context) {
  4. NfcPlaylist* nfc_playlist = context;
  5. // open/alloc resources
  6. Storage* storage = furi_record_open(RECORD_STORAGE);
  7. Stream* stream = file_stream_alloc(storage);
  8. FuriString* line = furi_string_alloc();
  9. NfcPlaylistWorker* nfc_worker = nfc_playlist_worker_alloc();
  10. // Read file
  11. if(file_stream_open(stream, APP_DATA_PATH("playlist.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) {
  12. popup_reset(nfc_playlist->popup);
  13. popup_set_context(nfc_playlist->popup, nfc_playlist);
  14. popup_set_header(nfc_playlist->popup, "Emulating:", 64, 10, AlignCenter, AlignTop);
  15. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup);
  16. int file_position = 0;
  17. // read the file line by line and print the text
  18. while(stream_read_line(stream, line)) {
  19. if (options_emulate_delay[nfc_playlist->emulate_delay] > 0) {
  20. if (file_position > 0) {
  21. start_error_blink(nfc_playlist);
  22. int time_counter_delay_ms = (options_emulate_delay[nfc_playlist->emulate_delay] * 1000);
  23. do {
  24. char display_text[30];
  25. snprintf(display_text, 30, "%s\n\n%ds", "Delaying...", (time_counter_delay_ms/1000));
  26. popup_set_text(nfc_playlist->popup, display_text, 64, 25, AlignCenter, AlignTop);
  27. furi_delay_ms(500);
  28. time_counter_delay_ms -= 500;
  29. } while(time_counter_delay_ms > 0);
  30. } else {
  31. file_position++;
  32. }
  33. }
  34. char* file_path = (char*)furi_string_get_cstr(line);
  35. char* file_name = &strrchr(file_path, '/')[1];
  36. int time_counter_ms = (options_emulate_timeout[nfc_playlist->emulate_timeout] * 1000);
  37. if (storage_file_exists(storage, file_path) == false) {
  38. start_error_blink(nfc_playlist);
  39. char const* popup_text_unformatted = strcat(file_name, "\nnot found");
  40. int popup_text_size = (strlen(popup_text_unformatted) + 4);
  41. char popup_text[popup_text_size];
  42. do {
  43. snprintf(popup_text, popup_text_size, "%s\n%ds", file_name, (time_counter_ms/1000));
  44. popup_set_text(nfc_playlist->popup, popup_text, 64, 25, AlignCenter, AlignTop);
  45. furi_delay_ms(500);
  46. time_counter_ms -= 500;
  47. } while(time_counter_ms > 0);
  48. } else {
  49. start_normal_blink(nfc_playlist);
  50. nfc_playlist_worker_set_nfc_data(nfc_worker, file_path);
  51. nfc_playlist_worker_start(nfc_worker);
  52. int popup_text_size = (strlen(file_name) + 4);
  53. char popup_text[popup_text_size];
  54. do {
  55. snprintf(popup_text, popup_text_size, "%s\n%ds", file_name, (time_counter_ms/1000));
  56. popup_set_text(nfc_playlist->popup, popup_text, 64, 25, AlignCenter, AlignTop);
  57. furi_delay_ms(500);
  58. time_counter_ms -= 500;
  59. } while(nfc_playlist_worker_is_emulating(nfc_worker) && time_counter_ms > 0);
  60. nfc_playlist_worker_stop(nfc_worker);
  61. }
  62. }
  63. popup_reset(nfc_playlist->popup);
  64. scene_manager_previous_scene(nfc_playlist->scene_manager);
  65. stop_blink(nfc_playlist);
  66. } else {
  67. popup_reset(nfc_playlist->popup);
  68. popup_set_context(nfc_playlist->popup, nfc_playlist);
  69. popup_set_header(nfc_playlist->popup, "Error:", 64, 10, AlignCenter, AlignTop);
  70. popup_set_text(nfc_playlist->popup, "Failed to open file\n/ext/apps_data/nfc_playlist/playlist.txt", 64, 25, AlignCenter, AlignTop);
  71. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup);
  72. }
  73. // Free/close resources
  74. furi_string_free(line);
  75. file_stream_close(stream);
  76. stream_free(stream);
  77. nfc_playlist_worker_free(nfc_worker);
  78. // Close storage
  79. furi_record_close(RECORD_STORAGE);
  80. }
  81. bool nfc_playlist_emulation_scene_on_event(void* context, SceneManagerEvent event) {
  82. UNUSED(context);
  83. UNUSED(event);
  84. return false;
  85. }
  86. void nfc_playlist_emulation_scene_on_exit(void* context) {
  87. NfcPlaylist* nfc_playlist = context;
  88. popup_reset(nfc_playlist->popup);
  89. }