nfc_playlist.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include <furi.h>
  2. #include <string.h>
  3. #include <storage/storage.h>
  4. #include <toolbox/stream/stream.h>
  5. #include <toolbox/stream/file_stream.h>
  6. #include <nfc_playlist_worker.h>
  7. #include <gui/gui.h>
  8. #include <gui/view_dispatcher.h>
  9. #include <gui/scene_manager.h>
  10. #include <gui/modules/submenu.h>
  11. #include <gui/modules/popup.h>
  12. // Define log tag
  13. #define TAG "NfcPlaylist"
  14. // IDs for all scenes used by the app
  15. typedef enum {
  16. NfcPlaylistScene_MainMenu,
  17. NfcPlaylistScene_EmulatingPopup,
  18. NfcPlaylistScene_count
  19. } NfcPlaylistScene;
  20. // IDs for the view used by the app
  21. typedef enum { NfcPlaylistView_Menu, NfcPlaylistView_Popup } NfcPlaylistView;
  22. // The app context struct
  23. typedef struct {
  24. SceneManager* scene_manager;
  25. ViewDispatcher* view_dispatcher;
  26. Submenu* submenu;
  27. Popup* popup;
  28. NfcPlaylistWorker* nfc_worker;
  29. int emulate_timeout;
  30. int emulate_delay;
  31. } NfcPlaylist;
  32. // All custom events
  33. typedef enum { NfcPlaylistEvent_ShowEmulatingPopup } NfcPlaylistEvent;
  34. /* main menu scene */
  35. // Indices for menu items
  36. typedef enum { NfcPlaylistMenuSelection_Start, NfcPlaylistMenuSelection_Settings } NfcPlaylistMenuSelection;
  37. // Main menu callback - sends a custom event to the scene manager based on the menu selection
  38. void nfc_playlist_menu_callback_main_menu(void* context, uint32_t index) {
  39. FURI_LOG_T(TAG, "nfc_playlist_menu_callback_main_menu");
  40. NfcPlaylist* app = context;
  41. switch(index) {
  42. case NfcPlaylistMenuSelection_Start:
  43. scene_manager_handle_custom_event(app->scene_manager, NfcPlaylistEvent_ShowEmulatingPopup);
  44. break;
  45. }
  46. }
  47. // Resets the menu, gives it content, callbacks and selection enums
  48. void nfc_playlist_scene_on_enter_main_menu(void* context) {
  49. FURI_LOG_T(TAG, "nfc_playlist_scene_on_enter_main_menu");
  50. NfcPlaylist* app = context;
  51. submenu_reset(app->submenu);
  52. submenu_set_header(app->submenu, "NFC Playlist");
  53. submenu_add_item(
  54. app->submenu,
  55. "Start",
  56. NfcPlaylistMenuSelection_Start,
  57. nfc_playlist_menu_callback_main_menu,
  58. app);
  59. view_dispatcher_switch_to_view(app->view_dispatcher, NfcPlaylistView_Menu);
  60. }
  61. // Main menu event handler - switches scene based on the event
  62. bool nfc_playlist_scene_on_event_main_menu(void* context, SceneManagerEvent event) {
  63. FURI_LOG_T(TAG, "nfc_playlist_scene_on_event_main_menu");
  64. NfcPlaylist* app = context;
  65. bool consumed = false;
  66. switch(event.type) {
  67. case SceneManagerEventTypeCustom:
  68. switch(event.event) {
  69. case NfcPlaylistEvent_ShowEmulatingPopup:
  70. scene_manager_next_scene(app->scene_manager, NfcPlaylistScene_EmulatingPopup);
  71. consumed = true;
  72. break;
  73. }
  74. break;
  75. default: // eg. SceneManagerEventTypeBack, SceneManagerEventTypeTick
  76. consumed = false;
  77. break;
  78. }
  79. return consumed;
  80. }
  81. void nfc_playlist_scene_on_exit_main_menu(void* context) {
  82. FURI_LOG_T(TAG, "nfc_playlist_scene_on_exit_main_menu");
  83. NfcPlaylist* app = context;
  84. submenu_reset(app->submenu);
  85. }
  86. // Emulating scene
  87. void nfc_playlist_scene_on_enter_popup_emulating(void* context) {
  88. FURI_LOG_T(TAG, "nfc_playlist_scene_on_enter_popup_emulating");
  89. NfcPlaylist* app = context;
  90. // open/alloc resources
  91. Storage* storage = furi_record_open(RECORD_STORAGE);
  92. Stream* stream = file_stream_alloc(storage);
  93. FuriString* line = furi_string_alloc();
  94. app->nfc_worker = nfc_playlist_worker_alloc();
  95. // Read file
  96. if(file_stream_open(stream, APP_DATA_PATH("playlist.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) {
  97. popup_reset(app->popup);
  98. popup_set_context(app->popup, app);
  99. popup_set_header(app->popup, "Emulating:", 64, 10, AlignCenter, AlignTop);
  100. view_dispatcher_switch_to_view(app->view_dispatcher, NfcPlaylistView_Popup);
  101. int file_position = 0;
  102. // read the file line by line and print the text
  103. while(stream_read_line(stream, line)) {
  104. if (app->emulate_delay > 0) {
  105. if (file_position > 0) {
  106. int time_counter_delay_ms = app->emulate_delay;
  107. do {
  108. char display_text[30];
  109. snprintf(display_text, 30, "%s\n\n%ds", "Delaying...", (time_counter_delay_ms/1000));
  110. popup_set_text(app->popup, display_text, 64, 25, AlignCenter, AlignTop);
  111. furi_delay_ms(500);
  112. time_counter_delay_ms -= 500;
  113. } while(time_counter_delay_ms > 0);
  114. } else {
  115. file_position++;
  116. }
  117. }
  118. char* file_path = (char*)furi_string_get_cstr(line);
  119. char* file_name = &strrchr(file_path, '/')[1];
  120. int time_counter_ms = app->emulate_timeout;
  121. if (storage_file_exists(storage, file_path) == false) {
  122. char* text = strcat(file_name, "\nnot found");
  123. int size = (strlen(text) + 4);
  124. char display_text[size];
  125. do {
  126. snprintf(display_text, size, "%s\n%ds", file_name, (time_counter_ms/1000));
  127. popup_set_text(app->popup, display_text, 64, 25, AlignCenter, AlignTop);
  128. furi_delay_ms(500);
  129. time_counter_ms -= 500;
  130. } while(time_counter_ms > 0);
  131. } else {
  132. nfc_playlist_worker_set_nfc_data(app->nfc_worker, file_path);
  133. nfc_playlist_worker_start(app->nfc_worker);
  134. int size = (strlen(file_name) + 4);
  135. char display_text[size];
  136. do {
  137. snprintf(display_text, size, "%s\n%ds", file_name, (time_counter_ms/1000));
  138. popup_set_text(app->popup, display_text, 64, 25, AlignCenter, AlignTop);
  139. furi_delay_ms(500);
  140. time_counter_ms -= 500;
  141. if (time_counter_ms <= 0) {
  142. break;
  143. }
  144. } while(nfc_playlist_worker_is_emulating(app->nfc_worker));
  145. if (nfc_playlist_worker_is_emulating(app->nfc_worker)) {
  146. nfc_playlist_worker_stop(app->nfc_worker);
  147. }
  148. }
  149. }
  150. } else {
  151. FURI_LOG_E(TAG, "Failed to open file");
  152. }
  153. // Free/close resources
  154. furi_string_free(line);
  155. file_stream_close(stream);
  156. stream_free(stream);
  157. nfc_playlist_worker_free(app->nfc_worker);
  158. app->nfc_worker = NULL;
  159. // Close storage
  160. furi_record_close(RECORD_STORAGE);
  161. popup_reset(app->popup);
  162. scene_manager_previous_scene(app->scene_manager);
  163. }
  164. bool nfc_playlist_scene_on_event_popup_emulating(void* context, SceneManagerEvent event) {
  165. FURI_LOG_T(TAG, "nfc_playlist_scene_on_event_popup_emulating");
  166. UNUSED(context);
  167. UNUSED(event);
  168. return false;
  169. }
  170. void nfc_playlist_scene_on_exit_popup_emulating(void* context) {
  171. FURI_LOG_T(TAG, "nfc_playlist_scene_on_exit_popup_emulating");
  172. NfcPlaylist* app = context;
  173. popup_reset(app->popup);
  174. }
  175. // Collection of all scene on_enter handlers - in the same order as their enum
  176. void (*const nfc_playlist_scene_on_enter_handlers[])(void*) = {
  177. nfc_playlist_scene_on_enter_main_menu,
  178. nfc_playlist_scene_on_enter_popup_emulating};
  179. // Collection of all scene on event handlers - in the same order as their enum
  180. bool (*const nfc_playlist_scene_on_event_handlers[])(void*, SceneManagerEvent) = {
  181. nfc_playlist_scene_on_event_main_menu,
  182. nfc_playlist_scene_on_event_popup_emulating};
  183. // Collection of all scene on exit handlers - in the same order as their enum
  184. void (*const nfc_playlist_scene_on_exit_handlers[])(void*) = {
  185. nfc_playlist_scene_on_exit_main_menu,
  186. nfc_playlist_scene_on_exit_popup_emulating};
  187. // Collection of all on_enter, on_event, on_exit handlers */
  188. const SceneManagerHandlers nfc_playlist_scene_event_handlers = {
  189. .on_enter_handlers = nfc_playlist_scene_on_enter_handlers,
  190. .on_event_handlers = nfc_playlist_scene_on_event_handlers,
  191. .on_exit_handlers = nfc_playlist_scene_on_exit_handlers,
  192. .scene_num = NfcPlaylistScene_count};
  193. // Custom event handler - passes the event to the scene manager
  194. bool nfc_playlist_scene_manager_custom_event_callback(void* context, uint32_t custom_event) {
  195. FURI_LOG_T(TAG, "nfc_playlist_scene_manager_custom_event_callback");
  196. furi_assert(context);
  197. NfcPlaylist* app = context;
  198. return scene_manager_handle_custom_event(app->scene_manager, custom_event);
  199. }
  200. // Navigation event handler - passes the event to the scene manager
  201. bool nfc_playlist_scene_manager_navigation_event_callback(void* context) {
  202. FURI_LOG_T(TAG, "nfc_playlist_scene_manager_navigation_event_callback");
  203. furi_assert(context);
  204. NfcPlaylist* app = context;
  205. return scene_manager_handle_back_event(app->scene_manager);
  206. }
  207. // Initialise the scene manager with all handlers
  208. void nfc_playlist_scene_manager_init(NfcPlaylist* app) {
  209. FURI_LOG_T(TAG, "nfc_playlist_scene_manager_init");
  210. app->scene_manager = scene_manager_alloc(&nfc_playlist_scene_event_handlers, app);
  211. }
  212. // Initialise the views, and initialise the view dispatcher with all views
  213. void nfc_playlist_view_dispatcher_init(NfcPlaylist* app) {
  214. FURI_LOG_T(TAG, "nfc_playlist_view_dispatcher_init");
  215. app->view_dispatcher = view_dispatcher_alloc();
  216. view_dispatcher_enable_queue(app->view_dispatcher);
  217. // allocate each view
  218. FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init allocating views");
  219. app->submenu = submenu_alloc();
  220. app->popup = popup_alloc();
  221. app->emulate_timeout = 5000;
  222. app->emulate_delay = 2000;
  223. // assign callback that pass events from views to the scene manager
  224. FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init setting callbacks");
  225. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  226. view_dispatcher_set_custom_event_callback( app->view_dispatcher, nfc_playlist_scene_manager_custom_event_callback);
  227. view_dispatcher_set_navigation_event_callback(app->view_dispatcher, nfc_playlist_scene_manager_navigation_event_callback);
  228. // add views to the dispatcher, indexed by their enum value
  229. FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init adding view menu");
  230. view_dispatcher_add_view(app->view_dispatcher, NfcPlaylistView_Menu, submenu_get_view(app->submenu));
  231. FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init adding view popup");
  232. view_dispatcher_add_view(app->view_dispatcher, NfcPlaylistView_Popup, popup_get_view(app->popup));
  233. }
  234. // Initialise app data, scene manager, and view dispatcher
  235. NfcPlaylist* nfc_playlist_init() {
  236. FURI_LOG_T(TAG, "nfc_playlist_init");
  237. NfcPlaylist* app = malloc(sizeof(NfcPlaylist));
  238. nfc_playlist_scene_manager_init(app);
  239. nfc_playlist_view_dispatcher_init(app);
  240. return app;
  241. }
  242. // Free all app data, scene manager, and view dispatcher
  243. void nfc_playlist_free(NfcPlaylist* app) {
  244. FURI_LOG_T(TAG, "nfc_playlist_free");
  245. scene_manager_free(app->scene_manager);
  246. view_dispatcher_remove_view(app->view_dispatcher, NfcPlaylistView_Menu);
  247. view_dispatcher_remove_view(app->view_dispatcher, NfcPlaylistView_Popup);
  248. view_dispatcher_free(app->view_dispatcher);
  249. submenu_free(app->submenu);
  250. popup_free(app->popup);
  251. free(app);
  252. }
  253. // Go to trace log level in the dev environment
  254. void nfc_playlist_set_log_level() {
  255. #ifdef FURI_DEBUG
  256. furi_log_set_level(FuriLogLevelTrace);
  257. #else
  258. furi_log_set_level(FuriLogLevelInfo);
  259. #endif
  260. }
  261. // Application entry point
  262. int32_t nfc_playlist_main(void* p) {
  263. // Mark argument as unused
  264. UNUSED(p);
  265. nfc_playlist_set_log_level();
  266. // create the app context struct, scene manager, and view dispatcher
  267. FURI_LOG_I(TAG, "NFC PLaylist starting...");
  268. NfcPlaylist* app = nfc_playlist_init();
  269. // set the scene and launch the main loop
  270. Gui* gui = furi_record_open(RECORD_GUI);
  271. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  272. scene_manager_next_scene(app->scene_manager, NfcPlaylistScene_MainMenu);
  273. FURI_LOG_D(TAG, "Starting dispatcher...");
  274. view_dispatcher_run(app->view_dispatcher);
  275. // free all memory
  276. FURI_LOG_I(TAG, "NFC PLaylist finishing...");
  277. furi_record_close(RECORD_GUI);
  278. nfc_playlist_free(app);
  279. return 0;
  280. }