passy_scene_advanced_menu.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "../passy_i.h"
  2. #define ASN_EMIT_DEBUG 0
  3. #include <lib/asn1/COM.h>
  4. #define TAG "SceneAdvancedMenu"
  5. static const char* dg_names[] = {
  6. "DG3",
  7. "DG4",
  8. "DG5",
  9. "DG6",
  10. "DG7",
  11. "DG8",
  12. "DG9",
  13. "DG10",
  14. "DG11",
  15. "DG12",
  16. "DG13",
  17. "DG14",
  18. "DG15"};
  19. static const uint8_t dg_ids[] =
  20. {0x63, 0x76, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x77, 0x42};
  21. static const uint16_t dg_file_ids[] = {
  22. 0x0103,
  23. 0x0104,
  24. 0x0105,
  25. 0x0106,
  26. 0x0107,
  27. 0x0108,
  28. 0x0109,
  29. 0x010A,
  30. 0x010B,
  31. 0x010C,
  32. 0x010D,
  33. 0x010E,
  34. 0x010F};
  35. enum SubmenuIndex {
  36. SubmenuIndexReadDG3 = 0,
  37. SubmenuIndexReadDG4,
  38. SubmenuIndexReadDG5,
  39. SubmenuIndexReadDG6,
  40. SubmenuIndexReadDG7,
  41. SubmenuIndexReadDG8,
  42. SubmenuIndexReadDG9,
  43. SubmenuIndexReadDG10,
  44. SubmenuIndexReadDG11,
  45. SubmenuIndexReadDG12,
  46. SubmenuIndexReadDG13,
  47. SubmenuIndexReadDG14,
  48. SubmenuIndexReadDG15,
  49. };
  50. void passy_scene_advanced_menu_submenu_callback(void* context, uint32_t index) {
  51. Passy* passy = context;
  52. view_dispatcher_send_custom_event(passy->view_dispatcher, index);
  53. }
  54. void passy_scene_advanced_menu_on_enter(void* context) {
  55. Passy* passy = context;
  56. Submenu* submenu = passy->submenu;
  57. submenu_reset(submenu);
  58. COM_t* com = 0;
  59. com = calloc(1, sizeof *com);
  60. assert(com);
  61. asn_dec_rval_t rval = asn_decode(
  62. 0,
  63. ATS_DER,
  64. &asn_DEF_COM,
  65. (void**)&com,
  66. bit_buffer_get_data(passy->COM),
  67. bit_buffer_get_size_bytes(passy->COM));
  68. if(rval.code == RC_OK) {
  69. FURI_LOG_I(TAG, "ASN.1 decode success");
  70. char payloadDebug[384] = {0};
  71. memset(payloadDebug, 0, sizeof(payloadDebug));
  72. (&asn_DEF_COM)->op->print_struct(&asn_DEF_COM, com, 1, print_struct_callback, payloadDebug);
  73. if(strlen(payloadDebug) > 0) {
  74. FURI_LOG_D(TAG, "COM: %s", payloadDebug);
  75. } else {
  76. FURI_LOG_D(TAG, "Received empty Payload");
  77. }
  78. } else {
  79. FURI_LOG_E(TAG, "ASN.1 decode failed: %d. %d consumed", rval.code, rval.consumed);
  80. passy_log_bitbuffer(TAG, "COM", passy->COM);
  81. }
  82. for(size_t i = 0; i < com->dataGroups.size; i++) {
  83. uint8_t value = com->dataGroups.buf[i];
  84. int8_t dg_id = -1;
  85. for(size_t j = 0; j < sizeof(dg_ids); j++) {
  86. if(value == dg_ids[j]) {
  87. dg_id = j;
  88. break;
  89. }
  90. }
  91. if(dg_id == -1) {
  92. continue;
  93. }
  94. FuriString* dg_name = furi_string_alloc();
  95. furi_string_printf(dg_name, "Read %s", dg_names[dg_id]);
  96. submenu_add_item(
  97. submenu,
  98. furi_string_get_cstr(dg_name),
  99. dg_id, // using the index requires that the enum aligns with the dg_id
  100. passy_scene_advanced_menu_submenu_callback,
  101. passy);
  102. furi_string_free(dg_name);
  103. }
  104. free(com);
  105. com = 0;
  106. view_dispatcher_switch_to_view(passy->view_dispatcher, PassyViewMenu);
  107. }
  108. bool passy_scene_advanced_menu_on_event(void* context, SceneManagerEvent event) {
  109. Passy* passy = context;
  110. bool consumed = false;
  111. if(event.type == SceneManagerEventTypeCustom) {
  112. scene_manager_set_scene_state(passy->scene_manager, PassySceneAdvancedMenu, event.event);
  113. passy->read_type = dg_file_ids[event.event];
  114. scene_manager_next_scene(passy->scene_manager, PassySceneRead);
  115. consumed = true;
  116. } else if(event.type == SceneManagerEventTypeBack) {
  117. scene_manager_search_and_switch_to_previous_scene(
  118. passy->scene_manager, PassySceneMainMenu);
  119. consumed = true;
  120. }
  121. return consumed;
  122. }
  123. void passy_scene_advanced_menu_on_exit(void* context) {
  124. Passy* passy = context;
  125. submenu_reset(passy->submenu);
  126. }