nfc_playlist.c 14 KB

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