nfc_scene_mf_desfire_data.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "../nfc_i.h"
  2. #define TAG "NfcSceneMfDesfireData"
  3. enum {
  4. MifareDesfireDataStateMenu,
  5. MifareDesfireDataStateItem, // MUST be last, states >= this correspond with submenu index
  6. };
  7. enum SubmenuIndex {
  8. SubmenuIndexCardInfo,
  9. SubmenuIndexDynamic, // dynamic indexes start here
  10. };
  11. void nfc_scene_mf_desfire_data_submenu_callback(void* context, uint32_t index) {
  12. Nfc* nfc = (Nfc*)context;
  13. view_dispatcher_send_custom_event(nfc->view_dispatcher, index);
  14. }
  15. void nfc_scene_mf_desfire_data_on_enter(void* context) {
  16. Nfc* nfc = context;
  17. Submenu* submenu = nfc->submenu;
  18. uint32_t state = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneMfDesfireData);
  19. MifareDesfireData* data = &nfc->dev->dev_data.mf_df_data;
  20. text_box_set_font(nfc->text_box, TextBoxFontHex);
  21. submenu_add_item(
  22. submenu,
  23. "Card info",
  24. SubmenuIndexCardInfo,
  25. nfc_scene_mf_desfire_data_submenu_callback,
  26. nfc);
  27. uint16_t cap = NFC_TEXT_STORE_SIZE;
  28. char* buf = nfc->text_store;
  29. int idx = SubmenuIndexDynamic;
  30. for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
  31. int size = snprintf(buf, cap, "App %02x%02x%02x", app->id[0], app->id[1], app->id[2]);
  32. if(size < 0 || size >= cap) {
  33. FURI_LOG_W(
  34. TAG, "Exceeded NFC_TEXT_STORE_SIZE when preparing app id strings; menu truncated");
  35. break;
  36. }
  37. char* label = buf;
  38. cap -= size + 1;
  39. buf += size + 1;
  40. submenu_add_item(submenu, label, idx++, nfc_scene_mf_desfire_data_submenu_callback, nfc);
  41. }
  42. if(state >= MifareDesfireDataStateItem) {
  43. submenu_set_selected_item(
  44. nfc->submenu, state - MifareDesfireDataStateItem + SubmenuIndexDynamic);
  45. scene_manager_set_scene_state(
  46. nfc->scene_manager, NfcSceneMfDesfireData, MifareDesfireDataStateMenu);
  47. }
  48. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewMenu);
  49. }
  50. bool nfc_scene_mf_desfire_data_on_event(void* context, SceneManagerEvent event) {
  51. Nfc* nfc = context;
  52. bool consumed = false;
  53. uint32_t state = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneMfDesfireData);
  54. MifareDesfireData* data = &nfc->dev->dev_data.mf_df_data;
  55. if(event.type == SceneManagerEventTypeCustom) {
  56. TextBox* text_box = nfc->text_box;
  57. string_reset(nfc->text_box_store);
  58. if(event.event == SubmenuIndexCardInfo) {
  59. mf_df_cat_card_info(data, nfc->text_box_store);
  60. text_box_set_text(text_box, string_get_cstr(nfc->text_box_store));
  61. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewTextBox);
  62. scene_manager_set_scene_state(
  63. nfc->scene_manager,
  64. NfcSceneMfDesfireData,
  65. MifareDesfireDataStateItem + SubmenuIndexCardInfo);
  66. consumed = true;
  67. } else {
  68. uint16_t index = event.event - SubmenuIndexDynamic;
  69. scene_manager_set_scene_state(
  70. nfc->scene_manager, NfcSceneMfDesfireData, MifareDesfireDataStateItem + index);
  71. scene_manager_set_scene_state(nfc->scene_manager, NfcSceneMfDesfireApp, index << 1);
  72. scene_manager_next_scene(nfc->scene_manager, NfcSceneMfDesfireApp);
  73. consumed = true;
  74. }
  75. } else if(event.type == SceneManagerEventTypeBack) {
  76. if(state >= MifareDesfireDataStateItem) {
  77. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewMenu);
  78. scene_manager_set_scene_state(
  79. nfc->scene_manager, NfcSceneMfDesfireData, MifareDesfireDataStateMenu);
  80. consumed = true;
  81. }
  82. }
  83. return consumed;
  84. }
  85. void nfc_scene_mf_desfire_data_on_exit(void* context) {
  86. Nfc* nfc = context;
  87. // Clear views
  88. text_box_reset(nfc->text_box);
  89. string_reset(nfc->text_box_store);
  90. submenu_reset(nfc->submenu);
  91. }