nfc.c 9.9 KB

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