nfc_playlist.c 15 KB

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