nfc_playlist.c 15 KB

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