picopass_scene_elite_dict_attack.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "../picopass_i.h"
  2. #include <dolphin/dolphin.h>
  3. #define TAG "IclassEliteDictAttack"
  4. typedef enum {
  5. DictAttackStateIdle,
  6. DictAttackStateUserDictInProgress,
  7. DictAttackStateFlipperDictInProgress,
  8. DictAttackStateStandardDictInProgress,
  9. } DictAttackState;
  10. void picopass_dict_attack_worker_callback(PicopassWorkerEvent event, void* context) {
  11. furi_assert(context);
  12. Picopass* picopass = context;
  13. view_dispatcher_send_custom_event(picopass->view_dispatcher, event);
  14. }
  15. void picopass_dict_attack_result_callback(void* context) {
  16. furi_assert(context);
  17. Picopass* picopass = context;
  18. view_dispatcher_send_custom_event(
  19. picopass->view_dispatcher, PicopassCustomEventDictAttackSkip);
  20. }
  21. static void
  22. picopass_scene_elite_dict_attack_prepare_view(Picopass* picopass, DictAttackState state) {
  23. IclassEliteDictAttackData* dict_attack_data =
  24. &picopass->dev->dev_data.iclass_elite_dict_attack_data;
  25. PicopassWorkerState worker_state = PicopassWorkerStateReady;
  26. IclassEliteDict* dict = NULL;
  27. // Identify scene state
  28. if(state == DictAttackStateIdle) {
  29. if(iclass_elite_dict_check_presence(IclassEliteDictTypeUser)) {
  30. FURI_LOG_D(TAG, "Starting with user dictionary");
  31. state = DictAttackStateUserDictInProgress;
  32. } else {
  33. FURI_LOG_D(TAG, "Starting with standard dictionary");
  34. state = DictAttackStateStandardDictInProgress;
  35. }
  36. } else if(state == DictAttackStateUserDictInProgress) {
  37. FURI_LOG_D(TAG, "Moving from user dictionary to standard dictionary");
  38. state = DictAttackStateStandardDictInProgress;
  39. } else if(state == DictAttackStateStandardDictInProgress) {
  40. FURI_LOG_D(TAG, "Moving from standard dictionary to elite dictionary");
  41. state = DictAttackStateFlipperDictInProgress;
  42. }
  43. // Setup view
  44. if(state == DictAttackStateUserDictInProgress) {
  45. worker_state = PicopassWorkerStateEliteDictAttack;
  46. dict_attack_set_header(picopass->dict_attack, "Elite User Dictionary");
  47. dict_attack_data->type = IclassEliteDictTypeUser;
  48. dict = iclass_elite_dict_alloc(IclassEliteDictTypeUser);
  49. // If failed to load user dictionary - try the system dictionary
  50. if(!dict) {
  51. FURI_LOG_E(TAG, "User dictionary not found");
  52. state = DictAttackStateStandardDictInProgress;
  53. }
  54. }
  55. if(state == DictAttackStateStandardDictInProgress) {
  56. worker_state = PicopassWorkerStateEliteDictAttack;
  57. dict_attack_set_header(picopass->dict_attack, "Standard System Dictionary");
  58. dict_attack_data->type = IclassStandardDictTypeFlipper;
  59. dict = iclass_elite_dict_alloc(IclassStandardDictTypeFlipper);
  60. if(!dict) {
  61. FURI_LOG_E(TAG, "Flipper standard dictionary not found");
  62. state = DictAttackStateFlipperDictInProgress;
  63. }
  64. }
  65. if(state == DictAttackStateFlipperDictInProgress) {
  66. worker_state = PicopassWorkerStateEliteDictAttack;
  67. dict_attack_set_header(picopass->dict_attack, "Elite System Dictionary");
  68. dict_attack_data->type = IclassEliteDictTypeFlipper;
  69. dict = iclass_elite_dict_alloc(IclassEliteDictTypeFlipper);
  70. if(!dict) {
  71. FURI_LOG_E(TAG, "Flipper Elite dictionary not found");
  72. // Pass through to let the worker handle the failure
  73. }
  74. }
  75. // Free previous dictionary
  76. if(dict_attack_data->dict) {
  77. iclass_elite_dict_free(dict_attack_data->dict);
  78. }
  79. dict_attack_data->dict = dict;
  80. scene_manager_set_scene_state(picopass->scene_manager, PicopassSceneEliteDictAttack, state);
  81. dict_attack_set_callback(
  82. picopass->dict_attack, picopass_dict_attack_result_callback, picopass);
  83. dict_attack_set_current_sector(picopass->dict_attack, 0);
  84. dict_attack_set_card_detected(picopass->dict_attack);
  85. dict_attack_set_total_dict_keys(
  86. picopass->dict_attack, dict ? iclass_elite_dict_get_total_keys(dict) : 0);
  87. picopass_worker_start(
  88. picopass->worker,
  89. worker_state,
  90. &picopass->dev->dev_data,
  91. picopass_dict_attack_worker_callback,
  92. picopass);
  93. }
  94. void picopass_scene_elite_dict_attack_on_enter(void* context) {
  95. Picopass* picopass = context;
  96. picopass_scene_elite_dict_attack_prepare_view(picopass, DictAttackStateIdle);
  97. view_dispatcher_switch_to_view(picopass->view_dispatcher, PicopassViewDictAttack);
  98. picopass_blink_start(picopass);
  99. notification_message(picopass->notifications, &sequence_display_backlight_enforce_on);
  100. }
  101. bool picopass_scene_elite_dict_attack_on_event(void* context, SceneManagerEvent event) {
  102. Picopass* picopass = context;
  103. bool consumed = false;
  104. uint32_t state =
  105. scene_manager_get_scene_state(picopass->scene_manager, PicopassSceneEliteDictAttack);
  106. if(event.type == SceneManagerEventTypeCustom) {
  107. if(event.event == PicopassWorkerEventSuccess) {
  108. if(state == DictAttackStateUserDictInProgress ||
  109. state == DictAttackStateStandardDictInProgress) {
  110. picopass_worker_stop(picopass->worker);
  111. picopass_scene_elite_dict_attack_prepare_view(picopass, state);
  112. consumed = true;
  113. } else {
  114. scene_manager_next_scene(picopass->scene_manager, PicopassSceneReadCardSuccess);
  115. consumed = true;
  116. }
  117. } else if(event.event == PicopassWorkerEventAborted) {
  118. scene_manager_next_scene(picopass->scene_manager, PicopassSceneReadCardSuccess);
  119. consumed = true;
  120. } else if(event.event == PicopassWorkerEventCardDetected) {
  121. dict_attack_set_card_detected(picopass->dict_attack);
  122. consumed = true;
  123. } else if(event.event == PicopassWorkerEventNoCardDetected) {
  124. dict_attack_set_card_removed(picopass->dict_attack);
  125. consumed = true;
  126. } else if(event.event == PicopassWorkerEventNewDictKeyBatch) {
  127. dict_attack_inc_current_dict_key(picopass->dict_attack, PICOPASS_DICT_KEY_BATCH_SIZE);
  128. consumed = true;
  129. } else if(event.event == PicopassCustomEventDictAttackSkip) {
  130. if(state == DictAttackStateUserDictInProgress) {
  131. picopass_worker_stop(picopass->worker);
  132. consumed = true;
  133. } else if(state == DictAttackStateFlipperDictInProgress) {
  134. picopass_worker_stop(picopass->worker);
  135. consumed = true;
  136. } else if(state == DictAttackStateStandardDictInProgress) {
  137. picopass_worker_stop(picopass->worker);
  138. consumed = true;
  139. }
  140. }
  141. } else if(event.type == SceneManagerEventTypeBack) {
  142. consumed = scene_manager_previous_scene(picopass->scene_manager);
  143. }
  144. return consumed;
  145. }
  146. void picopass_scene_elite_dict_attack_on_exit(void* context) {
  147. Picopass* picopass = context;
  148. IclassEliteDictAttackData* dict_attack_data =
  149. &picopass->dev->dev_data.iclass_elite_dict_attack_data;
  150. // Stop worker
  151. picopass_worker_stop(picopass->worker);
  152. if(dict_attack_data->dict) {
  153. iclass_elite_dict_free(dict_attack_data->dict);
  154. dict_attack_data->dict = NULL;
  155. }
  156. dict_attack_reset(picopass->dict_attack);
  157. picopass_blink_stop(picopass);
  158. notification_message(picopass->notifications, &sequence_display_backlight_enforce_auto);
  159. }