nfc.c 10 KB

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