nfc_scene_mf_desfire_app.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "../nfc_i.h"
  2. #define TAG "NfcSceneMfDesfireApp"
  3. enum SubmenuIndex {
  4. SubmenuIndexAppInfo,
  5. SubmenuIndexDynamic, // dynamic indexes start here
  6. };
  7. MifareDesfireApplication* nfc_scene_mf_desfire_app_get_app(Nfc* nfc) {
  8. uint32_t app_idx = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneMfDesfireApp) >>
  9. 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_mf_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_mf_desfire_app_on_enter(void* context) {
  21. Nfc* nfc = context;
  22. Submenu* submenu = nfc->submenu;
  23. MifareDesfireApplication* app = nfc_scene_mf_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, "App info", SubmenuIndexAppInfo, nfc_scene_mf_desfire_app_submenu_callback, nfc);
  41. uint16_t cap = NFC_TEXT_STORE_SIZE;
  42. char* buf = nfc->text_store;
  43. int idx = SubmenuIndexDynamic;
  44. for(MifareDesfireFile* file = app->file_head; file; file = file->next) {
  45. int size = snprintf(buf, cap, "File %d", file->id);
  46. if(size < 0 || size >= cap) {
  47. FURI_LOG_W(
  48. TAG,
  49. "Exceeded NFC_TEXT_STORE_SIZE when preparing file id strings; menu truncated");
  50. break;
  51. }
  52. char* label = buf;
  53. cap -= size + 1;
  54. buf += size + 1;
  55. submenu_add_item(submenu, label, idx++, nfc_scene_mf_desfire_app_submenu_callback, nfc);
  56. }
  57. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewMenu);
  58. }
  59. bool nfc_scene_mf_desfire_app_on_event(void* context, SceneManagerEvent event) {
  60. Nfc* nfc = context;
  61. bool consumed = false;
  62. uint32_t state = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneMfDesfireApp);
  63. if(event.type == SceneManagerEventTypeCustom) {
  64. MifareDesfireApplication* app = nfc_scene_mf_desfire_app_get_app(nfc);
  65. TextBox* text_box = nfc->text_box;
  66. string_reset(nfc->text_box_store);
  67. if(event.event == SubmenuIndexAppInfo) {
  68. mf_df_cat_application_info(app, nfc->text_box_store);
  69. } else {
  70. uint16_t index = event.event - SubmenuIndexDynamic;
  71. MifareDesfireFile* file = app->file_head;
  72. for(int i = 0; file && i < index; i++) {
  73. file = file->next;
  74. }
  75. if(!file) {
  76. return false;
  77. }
  78. mf_df_cat_file(file, nfc->text_box_store);
  79. }
  80. text_box_set_text(text_box, string_get_cstr(nfc->text_box_store));
  81. scene_manager_set_scene_state(nfc->scene_manager, NfcSceneMfDesfireApp, state | 1);
  82. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewTextBox);
  83. consumed = true;
  84. } else if(event.type == SceneManagerEventTypeBack) {
  85. if(state & 1) {
  86. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewMenu);
  87. scene_manager_set_scene_state(nfc->scene_manager, NfcSceneMfDesfireApp, state & ~1);
  88. consumed = true;
  89. }
  90. }
  91. return consumed;
  92. }
  93. void nfc_scene_mf_desfire_app_on_exit(void* context) {
  94. Nfc* nfc = context;
  95. // Clear views
  96. text_box_reset(nfc->text_box);
  97. string_reset(nfc->text_box_store);
  98. submenu_reset(nfc->submenu);
  99. }