nfc_playlist.c 11 KB

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