nfc_scene_mifare_desfire_app.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "../nfc_i.h"
  2. #define TAG "NfcSceneMifareDesfireApp"
  3. enum SubmenuIndex {
  4. SubmenuIndexAppInfo,
  5. SubmenuIndexDynamic, // dynamic indexes start here
  6. };
  7. MifareDesfireApplication* nfc_scene_mifare_desfire_app_get_app(Nfc* nfc) {
  8. uint32_t app_idx =
  9. scene_manager_get_scene_state(nfc->scene_manager, NfcSceneMifareDesfireApp) >> 1;
  10. MifareDesfireApplication* app = nfc->dev->dev_data.mf_df_data.app_head;
  11. for(uint32_t i = 0; i < app_idx && app; i++) {
  12. app = app->next;
  13. }
  14. return app;
  15. }
  16. void nfc_scene_mifare_desfire_app_submenu_callback(void* context, uint32_t index) {
  17. Nfc* nfc = context;
  18. view_dispatcher_send_custom_event(nfc->view_dispatcher, index);
  19. }
  20. void nfc_scene_mifare_desfire_app_on_enter(void* context) {
  21. Nfc* nfc = context;
  22. Submenu* submenu = nfc->submenu;
  23. MifareDesfireApplication* app = nfc_scene_mifare_desfire_app_get_app(nfc);
  24. if(!app) {
  25. popup_set_icon(nfc->popup, 5, 5, &I_WarningDolphin_45x42);
  26. popup_set_header(nfc->popup, "Internal Error!", 55, 12, AlignLeft, AlignBottom);
  27. popup_set_text(
  28. nfc->popup,
  29. "No app selected.\nThis should\nnever happen,\nplease file a bug.",
  30. 55,
  31. 15,
  32. AlignLeft,
  33. AlignTop);
  34. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewPopup);
  35. FURI_LOG_E(TAG, "Bad state. No app selected?");
  36. return;
  37. }
  38. text_box_set_font(nfc->text_box, TextBoxFontHex);
  39. submenu_add_item(
  40. submenu,
  41. "App info",
  42. SubmenuIndexAppInfo,
  43. nfc_scene_mifare_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. submenu, label, idx++, nfc_scene_mifare_desfire_app_submenu_callback, nfc);
  61. }
  62. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewMenu);
  63. }
  64. bool nfc_scene_mifare_desfire_app_on_event(void* context, SceneManagerEvent event) {
  65. Nfc* nfc = context;
  66. bool consumed = false;
  67. uint32_t state = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneMifareDesfireApp);
  68. if(event.type == SceneManagerEventTypeCustom) {
  69. MifareDesfireApplication* app = nfc_scene_mifare_desfire_app_get_app(nfc);
  70. TextBox* text_box = nfc->text_box;
  71. string_reset(nfc->text_box_store);
  72. if(event.event == SubmenuIndexAppInfo) {
  73. mf_df_cat_application_info(app, nfc->text_box_store);
  74. } else {
  75. uint16_t index = event.event - SubmenuIndexDynamic;
  76. MifareDesfireFile* file = app->file_head;
  77. for(int i = 0; file && i < index; i++) {
  78. file = file->next;
  79. }
  80. if(!file) {
  81. return false;
  82. }
  83. mf_df_cat_file(file, nfc->text_box_store);
  84. }
  85. text_box_set_text(text_box, string_get_cstr(nfc->text_box_store));
  86. scene_manager_set_scene_state(nfc->scene_manager, NfcSceneMifareDesfireApp, state | 1);
  87. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewTextBox);
  88. consumed = true;
  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(
  93. nfc->scene_manager, NfcSceneMifareDesfireApp, state & ~1);
  94. consumed = true;
  95. }
  96. }
  97. return consumed;
  98. }
  99. void nfc_scene_mifare_desfire_app_on_exit(void* context) {
  100. Nfc* nfc = context;
  101. // Clear views
  102. text_box_reset(nfc->text_box);
  103. string_reset(nfc->text_box_store);
  104. submenu_reset(nfc->submenu);
  105. }