nfc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include "nfc_i.h"
  2. #include "furi_hal_nfc.h"
  3. bool nfc_custom_event_callback(void* context, uint32_t event) {
  4. furi_assert(context);
  5. Nfc* nfc = context;
  6. return scene_manager_handle_custom_event(nfc->scene_manager, event);
  7. }
  8. bool nfc_back_event_callback(void* context) {
  9. furi_assert(context);
  10. Nfc* nfc = context;
  11. return scene_manager_handle_back_event(nfc->scene_manager);
  12. }
  13. static void nfc_rpc_command_callback(RpcAppSystemEvent event, void* context) {
  14. furi_assert(context);
  15. Nfc* nfc = context;
  16. furi_assert(nfc->rpc_ctx);
  17. if(event == RpcAppEventSessionClose) {
  18. view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventRpcSessionClose);
  19. rpc_system_app_set_callback(nfc->rpc_ctx, NULL, NULL);
  20. nfc->rpc_ctx = NULL;
  21. } else if(event == RpcAppEventAppExit) {
  22. view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
  23. } else if(event == RpcAppEventLoadFile) {
  24. view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventRpcLoad);
  25. } else {
  26. rpc_system_app_confirm(nfc->rpc_ctx, event, false);
  27. }
  28. }
  29. Nfc* nfc_alloc() {
  30. Nfc* nfc = malloc(sizeof(Nfc));
  31. nfc->worker = nfc_worker_alloc();
  32. nfc->view_dispatcher = view_dispatcher_alloc();
  33. nfc->scene_manager = scene_manager_alloc(&nfc_scene_handlers, nfc);
  34. view_dispatcher_enable_queue(nfc->view_dispatcher);
  35. view_dispatcher_set_event_callback_context(nfc->view_dispatcher, nfc);
  36. view_dispatcher_set_custom_event_callback(nfc->view_dispatcher, nfc_custom_event_callback);
  37. view_dispatcher_set_navigation_event_callback(nfc->view_dispatcher, nfc_back_event_callback);
  38. // Nfc device
  39. nfc->dev = nfc_device_alloc();
  40. // Open GUI record
  41. nfc->gui = furi_record_open(RECORD_GUI);
  42. // Open Notification record
  43. nfc->notifications = furi_record_open(RECORD_NOTIFICATION);
  44. // Submenu
  45. nfc->submenu = submenu_alloc();
  46. view_dispatcher_add_view(nfc->view_dispatcher, NfcViewMenu, submenu_get_view(nfc->submenu));
  47. // Dialog
  48. nfc->dialog_ex = dialog_ex_alloc();
  49. view_dispatcher_add_view(
  50. nfc->view_dispatcher, NfcViewDialogEx, dialog_ex_get_view(nfc->dialog_ex));
  51. // Popup
  52. nfc->popup = popup_alloc();
  53. view_dispatcher_add_view(nfc->view_dispatcher, NfcViewPopup, popup_get_view(nfc->popup));
  54. // Loading
  55. nfc->loading = loading_alloc();
  56. view_dispatcher_add_view(nfc->view_dispatcher, NfcViewLoading, loading_get_view(nfc->loading));
  57. // Text Input
  58. nfc->text_input = text_input_alloc();
  59. view_dispatcher_add_view(
  60. nfc->view_dispatcher, NfcViewTextInput, text_input_get_view(nfc->text_input));
  61. // Byte Input
  62. nfc->byte_input = byte_input_alloc();
  63. view_dispatcher_add_view(
  64. nfc->view_dispatcher, NfcViewByteInput, byte_input_get_view(nfc->byte_input));
  65. // TextBox
  66. nfc->text_box = text_box_alloc();
  67. view_dispatcher_add_view(
  68. nfc->view_dispatcher, NfcViewTextBox, text_box_get_view(nfc->text_box));
  69. string_init(nfc->text_box_store);
  70. // Custom Widget
  71. nfc->widget = widget_alloc();
  72. view_dispatcher_add_view(nfc->view_dispatcher, NfcViewWidget, widget_get_view(nfc->widget));
  73. // Mifare Classic Dict Attack
  74. nfc->dict_attack = dict_attack_alloc();
  75. view_dispatcher_add_view(
  76. nfc->view_dispatcher, NfcViewDictAttack, dict_attack_get_view(nfc->dict_attack));
  77. // Generator
  78. nfc->generator = NULL;
  79. return nfc;
  80. }
  81. void nfc_free(Nfc* nfc) {
  82. furi_assert(nfc);
  83. if(nfc->rpc_state == NfcRpcStateEmulating) {
  84. // Stop worker
  85. nfc_worker_stop(nfc->worker);
  86. } else if(nfc->rpc_state == NfcRpcStateEmulated) {
  87. // Stop worker
  88. nfc_worker_stop(nfc->worker);
  89. // Save data in shadow file
  90. nfc_device_save_shadow(nfc->dev, nfc->dev->dev_name);
  91. }
  92. if(nfc->rpc_ctx) {
  93. rpc_system_app_send_exited(nfc->rpc_ctx);
  94. rpc_system_app_set_callback(nfc->rpc_ctx, NULL, NULL);
  95. nfc->rpc_ctx = NULL;
  96. }
  97. // Nfc device
  98. nfc_device_free(nfc->dev);
  99. // Submenu
  100. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewMenu);
  101. submenu_free(nfc->submenu);
  102. // DialogEx
  103. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewDialogEx);
  104. dialog_ex_free(nfc->dialog_ex);
  105. // Popup
  106. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewPopup);
  107. popup_free(nfc->popup);
  108. // Loading
  109. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewLoading);
  110. loading_free(nfc->loading);
  111. // TextInput
  112. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewTextInput);
  113. text_input_free(nfc->text_input);
  114. // ByteInput
  115. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewByteInput);
  116. byte_input_free(nfc->byte_input);
  117. // TextBox
  118. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewTextBox);
  119. text_box_free(nfc->text_box);
  120. string_clear(nfc->text_box_store);
  121. // Custom Widget
  122. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewWidget);
  123. widget_free(nfc->widget);
  124. // Mifare Classic Dict Attack
  125. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewDictAttack);
  126. dict_attack_free(nfc->dict_attack);
  127. // Worker
  128. nfc_worker_stop(nfc->worker);
  129. nfc_worker_free(nfc->worker);
  130. // View Dispatcher
  131. view_dispatcher_free(nfc->view_dispatcher);
  132. // Scene Manager
  133. scene_manager_free(nfc->scene_manager);
  134. // GUI
  135. furi_record_close(RECORD_GUI);
  136. nfc->gui = NULL;
  137. // Notifications
  138. furi_record_close(RECORD_NOTIFICATION);
  139. nfc->notifications = NULL;
  140. free(nfc);
  141. }
  142. void nfc_text_store_set(Nfc* nfc, const char* text, ...) {
  143. va_list args;
  144. va_start(args, text);
  145. vsnprintf(nfc->text_store, sizeof(nfc->text_store), text, args);
  146. va_end(args);
  147. }
  148. void nfc_text_store_clear(Nfc* nfc) {
  149. memset(nfc->text_store, 0, sizeof(nfc->text_store));
  150. }
  151. void nfc_blink_start(Nfc* nfc) {
  152. notification_message(nfc->notifications, &sequence_blink_start_blue);
  153. }
  154. void nfc_blink_stop(Nfc* nfc) {
  155. notification_message(nfc->notifications, &sequence_blink_stop);
  156. }
  157. void nfc_show_loading_popup(void* context, bool show) {
  158. Nfc* nfc = context;
  159. TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
  160. if(show) {
  161. // Raise timer priority so that animations can play
  162. vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
  163. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewLoading);
  164. } else {
  165. // Restore default timer priority
  166. vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
  167. }
  168. }
  169. int32_t nfc_app(void* p) {
  170. Nfc* nfc = nfc_alloc();
  171. char* args = p;
  172. // Check argument and run corresponding scene
  173. if(args && strlen(args)) {
  174. nfc_device_set_loading_callback(nfc->dev, nfc_show_loading_popup, nfc);
  175. uint32_t rpc_ctx = 0;
  176. if(sscanf(p, "RPC %lX", &rpc_ctx) == 1) {
  177. nfc->rpc_ctx = (void*)rpc_ctx;
  178. rpc_system_app_set_callback(nfc->rpc_ctx, nfc_rpc_command_callback, nfc);
  179. rpc_system_app_send_started(nfc->rpc_ctx);
  180. view_dispatcher_attach_to_gui(
  181. nfc->view_dispatcher, nfc->gui, ViewDispatcherTypeDesktop);
  182. scene_manager_next_scene(nfc->scene_manager, NfcSceneRpc);
  183. } else {
  184. view_dispatcher_attach_to_gui(
  185. nfc->view_dispatcher, nfc->gui, ViewDispatcherTypeFullscreen);
  186. if(nfc_device_load(nfc->dev, p, true)) {
  187. if(nfc->dev->format == NfcDeviceSaveFormatMifareUl) {
  188. scene_manager_next_scene(nfc->scene_manager, NfcSceneMfUltralightEmulate);
  189. } else if(nfc->dev->format == NfcDeviceSaveFormatMifareClassic) {
  190. scene_manager_next_scene(nfc->scene_manager, NfcSceneMfClassicEmulate);
  191. } else {
  192. scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateUid);
  193. }
  194. } else {
  195. // Exit app
  196. view_dispatcher_stop(nfc->view_dispatcher);
  197. }
  198. }
  199. nfc_device_set_loading_callback(nfc->dev, NULL, nfc);
  200. } else {
  201. view_dispatcher_attach_to_gui(
  202. nfc->view_dispatcher, nfc->gui, ViewDispatcherTypeFullscreen);
  203. scene_manager_next_scene(nfc->scene_manager, NfcSceneStart);
  204. }
  205. view_dispatcher_run(nfc->view_dispatcher);
  206. nfc_free(nfc);
  207. return 0;
  208. }