emulation.c 4.1 KB

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