nfc.c 8.8 KB

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