nfc.c 10 KB

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