nfc.c 10 KB

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