nfc.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. nfc->text_box_store = furi_string_alloc();
  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. // Detect Reader
  78. nfc->detect_reader = detect_reader_alloc();
  79. view_dispatcher_add_view(
  80. nfc->view_dispatcher, NfcViewDetectReader, detect_reader_get_view(nfc->detect_reader));
  81. // Generator
  82. nfc->generator = NULL;
  83. return nfc;
  84. }
  85. void nfc_free(Nfc* nfc) {
  86. furi_assert(nfc);
  87. if(nfc->rpc_state == NfcRpcStateEmulating) {
  88. // Stop worker
  89. nfc_worker_stop(nfc->worker);
  90. } else if(nfc->rpc_state == NfcRpcStateEmulated) {
  91. // Stop worker
  92. nfc_worker_stop(nfc->worker);
  93. // Save data in shadow file
  94. nfc_device_save_shadow(nfc->dev, nfc->dev->dev_name);
  95. }
  96. if(nfc->rpc_ctx) {
  97. rpc_system_app_send_exited(nfc->rpc_ctx);
  98. rpc_system_app_set_callback(nfc->rpc_ctx, NULL, NULL);
  99. nfc->rpc_ctx = NULL;
  100. }
  101. // Nfc device
  102. nfc_device_free(nfc->dev);
  103. // Submenu
  104. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewMenu);
  105. submenu_free(nfc->submenu);
  106. // DialogEx
  107. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewDialogEx);
  108. dialog_ex_free(nfc->dialog_ex);
  109. // Popup
  110. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewPopup);
  111. popup_free(nfc->popup);
  112. // Loading
  113. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewLoading);
  114. loading_free(nfc->loading);
  115. // TextInput
  116. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewTextInput);
  117. text_input_free(nfc->text_input);
  118. // ByteInput
  119. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewByteInput);
  120. byte_input_free(nfc->byte_input);
  121. // TextBox
  122. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewTextBox);
  123. text_box_free(nfc->text_box);
  124. furi_string_free(nfc->text_box_store);
  125. // Custom Widget
  126. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewWidget);
  127. widget_free(nfc->widget);
  128. // Mifare Classic Dict Attack
  129. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewDictAttack);
  130. dict_attack_free(nfc->dict_attack);
  131. // Detect Reader
  132. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewDetectReader);
  133. detect_reader_free(nfc->detect_reader);
  134. // Worker
  135. nfc_worker_stop(nfc->worker);
  136. nfc_worker_free(nfc->worker);
  137. // View Dispatcher
  138. view_dispatcher_free(nfc->view_dispatcher);
  139. // Scene Manager
  140. scene_manager_free(nfc->scene_manager);
  141. // GUI
  142. furi_record_close(RECORD_GUI);
  143. nfc->gui = NULL;
  144. // Notifications
  145. furi_record_close(RECORD_NOTIFICATION);
  146. nfc->notifications = NULL;
  147. free(nfc);
  148. }
  149. void nfc_text_store_set(Nfc* nfc, const char* text, ...) {
  150. va_list args;
  151. va_start(args, text);
  152. vsnprintf(nfc->text_store, sizeof(nfc->text_store), text, args);
  153. va_end(args);
  154. }
  155. void nfc_text_store_clear(Nfc* nfc) {
  156. memset(nfc->text_store, 0, sizeof(nfc->text_store));
  157. }
  158. void nfc_blink_read_start(Nfc* nfc) {
  159. notification_message(nfc->notifications, &sequence_blink_start_cyan);
  160. }
  161. void nfc_blink_emulate_start(Nfc* nfc) {
  162. notification_message(nfc->notifications, &sequence_blink_start_magenta);
  163. }
  164. void nfc_blink_detect_start(Nfc* nfc) {
  165. notification_message(nfc->notifications, &sequence_blink_start_yellow);
  166. }
  167. void nfc_blink_stop(Nfc* nfc) {
  168. notification_message(nfc->notifications, &sequence_blink_stop);
  169. }
  170. void nfc_show_loading_popup(void* context, bool show) {
  171. Nfc* nfc = context;
  172. TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
  173. if(show) {
  174. // Raise timer priority so that animations can play
  175. vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
  176. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewLoading);
  177. } else {
  178. // Restore default timer priority
  179. vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
  180. }
  181. }
  182. static bool nfc_is_hal_ready() {
  183. if(!furi_hal_nfc_is_init()) {
  184. // No connection to the chip, show an error screen
  185. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  186. DialogMessage* message = dialog_message_alloc();
  187. dialog_message_set_text(
  188. message,
  189. "Error!\nNFC chip failed to start\n\n\nSend a photo of this to:\nsupport@flipperzero.one",
  190. 0,
  191. 0,
  192. AlignLeft,
  193. AlignTop);
  194. dialog_message_show(dialogs, message);
  195. dialog_message_free(message);
  196. furi_record_close(RECORD_DIALOGS);
  197. return false;
  198. } else {
  199. return true;
  200. }
  201. }
  202. int32_t nfc_app(void* p) {
  203. if(!nfc_is_hal_ready()) return 0;
  204. Nfc* nfc = nfc_alloc();
  205. char* args = p;
  206. // Check argument and run corresponding scene
  207. if(args && strlen(args)) {
  208. nfc_device_set_loading_callback(nfc->dev, nfc_show_loading_popup, nfc);
  209. uint32_t rpc_ctx = 0;
  210. if(sscanf(p, "RPC %lX", &rpc_ctx) == 1) {
  211. nfc->rpc_ctx = (void*)rpc_ctx;
  212. rpc_system_app_set_callback(nfc->rpc_ctx, nfc_rpc_command_callback, nfc);
  213. rpc_system_app_send_started(nfc->rpc_ctx);
  214. view_dispatcher_attach_to_gui(
  215. nfc->view_dispatcher, nfc->gui, ViewDispatcherTypeDesktop);
  216. scene_manager_next_scene(nfc->scene_manager, NfcSceneRpc);
  217. } else {
  218. view_dispatcher_attach_to_gui(
  219. nfc->view_dispatcher, nfc->gui, ViewDispatcherTypeFullscreen);
  220. if(nfc_device_load(nfc->dev, p, true)) {
  221. if(nfc->dev->format == NfcDeviceSaveFormatMifareUl) {
  222. scene_manager_next_scene(nfc->scene_manager, NfcSceneMfUltralightEmulate);
  223. } else if(nfc->dev->format == NfcDeviceSaveFormatMifareClassic) {
  224. scene_manager_next_scene(nfc->scene_manager, NfcSceneMfClassicEmulate);
  225. } else if(nfc->dev->format == NfcDeviceSaveFormatBankCard) {
  226. scene_manager_next_scene(nfc->scene_manager, NfcSceneDeviceInfo);
  227. } else {
  228. scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateUid);
  229. }
  230. } else {
  231. // Exit app
  232. view_dispatcher_stop(nfc->view_dispatcher);
  233. }
  234. }
  235. nfc_device_set_loading_callback(nfc->dev, NULL, nfc);
  236. } else {
  237. view_dispatcher_attach_to_gui(
  238. nfc->view_dispatcher, nfc->gui, ViewDispatcherTypeFullscreen);
  239. scene_manager_next_scene(nfc->scene_manager, NfcSceneStart);
  240. }
  241. view_dispatcher_run(nfc->view_dispatcher);
  242. nfc_free(nfc);
  243. return 0;
  244. }