nfc_scene_mf_desfire_app.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. FuriString* label = furi_string_alloc();
  46. int idx = SubmenuIndexDynamic;
  47. for(MifareDesfireFile* file = app->file_head; file; file = file->next) {
  48. furi_string_printf(label, "File %d", file->id);
  49. submenu_add_item(
  50. nfc->submenu,
  51. furi_string_get_cstr(label),
  52. idx++,
  53. nfc_scene_mf_desfire_app_submenu_callback,
  54. nfc);
  55. }
  56. furi_string_free(label);
  57. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewMenu);
  58. }
  59. }
  60. bool nfc_scene_mf_desfire_app_on_event(void* context, SceneManagerEvent event) {
  61. Nfc* nfc = context;
  62. bool consumed = false;
  63. uint32_t state = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneMfDesfireApp);
  64. if(event.type == SceneManagerEventTypeCustom) {
  65. if(event.event == NfcCustomEventViewExit) {
  66. consumed = scene_manager_previous_scene(nfc->scene_manager);
  67. } else {
  68. MifareDesfireApplication* app = nfc_scene_mf_desfire_app_get_app(nfc);
  69. TextBox* text_box = nfc->text_box;
  70. furi_string_reset(nfc->text_box_store);
  71. if(event.event == SubmenuIndexAppInfo) {
  72. mf_df_cat_application_info(app, nfc->text_box_store);
  73. } else {
  74. uint16_t index = event.event - SubmenuIndexDynamic;
  75. MifareDesfireFile* file = app->file_head;
  76. for(int i = 0; file && i < index; i++) {
  77. file = file->next;
  78. }
  79. if(!file) {
  80. return false;
  81. }
  82. mf_df_cat_file(file, nfc->text_box_store);
  83. }
  84. text_box_set_text(text_box, furi_string_get_cstr(nfc->text_box_store));
  85. scene_manager_set_scene_state(nfc->scene_manager, NfcSceneMfDesfireApp, state | 1);
  86. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewTextBox);
  87. consumed = true;
  88. }
  89. } else if(event.type == SceneManagerEventTypeBack) {
  90. if(state & 1) {
  91. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewMenu);
  92. scene_manager_set_scene_state(nfc->scene_manager, NfcSceneMfDesfireApp, state & ~1);
  93. consumed = true;
  94. }
  95. }
  96. return consumed;
  97. }
  98. void nfc_scene_mf_desfire_app_on_exit(void* context) {
  99. Nfc* nfc = context;
  100. // Clear views
  101. popup_reset(nfc->popup);
  102. text_box_reset(nfc->text_box);
  103. furi_string_reset(nfc->text_box_store);
  104. submenu_reset(nfc->submenu);
  105. }