nfc_scene_mf_desfire_app.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "../nfc_i.h"
  2. #define TAG "NfcSceneMfDesfireApp"
  3. enum SubmenuIndex {
  4. SubmenuIndexAppInfo,
  5. SubmenuIndexDynamic, // dynamic indexes start here
  6. };
  7. void nfc_scene_mf_desfire_popup_callback(void* context) {
  8. furi_assert(context);
  9. Nfc* nfc = context;
  10. view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
  11. }
  12. MifareDesfireApplication* nfc_scene_mf_desfire_app_get_app(Nfc* nfc) {
  13. uint32_t app_idx = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneMfDesfireApp) >>
  14. 1;
  15. MifareDesfireApplication* app = nfc->dev->dev_data.mf_df_data.app_head;
  16. for(uint32_t i = 0; i < app_idx && app; i++) {
  17. app = app->next;
  18. }
  19. return app;
  20. }
  21. void nfc_scene_mf_desfire_app_submenu_callback(void* context, uint32_t index) {
  22. Nfc* nfc = context;
  23. view_dispatcher_send_custom_event(nfc->view_dispatcher, index);
  24. }
  25. void nfc_scene_mf_desfire_app_on_enter(void* context) {
  26. Nfc* nfc = context;
  27. MifareDesfireApplication* app = nfc_scene_mf_desfire_app_get_app(nfc);
  28. if(!app) {
  29. popup_set_icon(nfc->popup, 5, 5, &I_WarningDolphin_45x42);
  30. popup_set_header(nfc->popup, "Empty card!", 55, 12, AlignLeft, AlignBottom);
  31. popup_set_callback(nfc->popup, nfc_scene_mf_desfire_popup_callback);
  32. popup_set_context(nfc->popup, nfc);
  33. popup_set_timeout(nfc->popup, 3000);
  34. popup_enable_timeout(nfc->popup);
  35. popup_set_text(nfc->popup, "No application\nfound.", 55, 15, AlignLeft, AlignTop);
  36. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewPopup);
  37. } else {
  38. text_box_set_font(nfc->text_box, TextBoxFontHex);
  39. submenu_add_item(
  40. nfc->submenu,
  41. "App info",
  42. SubmenuIndexAppInfo,
  43. nfc_scene_mf_desfire_app_submenu_callback,
  44. nfc);
  45. uint16_t cap = NFC_TEXT_STORE_SIZE;
  46. char* buf = nfc->text_store;
  47. int idx = SubmenuIndexDynamic;
  48. for(MifareDesfireFile* file = app->file_head; file; file = file->next) {
  49. int size = snprintf(buf, cap, "File %d", file->id);
  50. if(size < 0 || size >= cap) {
  51. FURI_LOG_W(
  52. TAG,
  53. "Exceeded NFC_TEXT_STORE_SIZE when preparing file id strings; menu truncated");
  54. break;
  55. }
  56. char* label = buf;
  57. cap -= size + 1;
  58. buf += size + 1;
  59. submenu_add_item(
  60. nfc->submenu, label, idx++, nfc_scene_mf_desfire_app_submenu_callback, nfc);
  61. }
  62. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewMenu);
  63. }
  64. }
  65. bool nfc_scene_mf_desfire_app_on_event(void* context, SceneManagerEvent event) {
  66. Nfc* nfc = context;
  67. bool consumed = false;
  68. uint32_t state = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneMfDesfireApp);
  69. if(event.type == SceneManagerEventTypeCustom) {
  70. if(event.event == NfcCustomEventViewExit) {
  71. consumed = scene_manager_previous_scene(nfc->scene_manager);
  72. } else {
  73. MifareDesfireApplication* app = nfc_scene_mf_desfire_app_get_app(nfc);
  74. TextBox* text_box = nfc->text_box;
  75. furi_string_reset(nfc->text_box_store);
  76. if(event.event == SubmenuIndexAppInfo) {
  77. mf_df_cat_application_info(app, nfc->text_box_store);
  78. } else {
  79. uint16_t index = event.event - SubmenuIndexDynamic;
  80. MifareDesfireFile* file = app->file_head;
  81. for(int i = 0; file && i < index; i++) {
  82. file = file->next;
  83. }
  84. if(!file) {
  85. return false;
  86. }
  87. mf_df_cat_file(file, nfc->text_box_store);
  88. }
  89. text_box_set_text(text_box, furi_string_get_cstr(nfc->text_box_store));
  90. scene_manager_set_scene_state(nfc->scene_manager, NfcSceneMfDesfireApp, state | 1);
  91. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewTextBox);
  92. consumed = true;
  93. }
  94. } else if(event.type == SceneManagerEventTypeBack) {
  95. if(state & 1) {
  96. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewMenu);
  97. scene_manager_set_scene_state(nfc->scene_manager, NfcSceneMfDesfireApp, state & ~1);
  98. consumed = true;
  99. }
  100. }
  101. return consumed;
  102. }
  103. void nfc_scene_mf_desfire_app_on_exit(void* context) {
  104. Nfc* nfc = context;
  105. // Clear views
  106. popup_reset(nfc->popup);
  107. text_box_reset(nfc->text_box);
  108. furi_string_reset(nfc->text_box_store);
  109. submenu_reset(nfc->submenu);
  110. }