mifare_nested_scene_collecting.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "../mifare_nested_i.h"
  2. void mifare_nested_scene_collecting_widget_callback(
  3. GuiButtonType result,
  4. InputType type,
  5. void* context) {
  6. MifareNested* mifare_nested = context;
  7. if(type == InputTypeShort) {
  8. view_dispatcher_send_custom_event(mifare_nested->view_dispatcher, result);
  9. }
  10. }
  11. bool mifare_nested_collecting_worker_callback(MifareNestedWorkerEvent event, void* context) {
  12. MifareNested* mifare_nested = context;
  13. NestedState* plugin_state = mifare_nested->nested_state;
  14. if(event == MifareNestedWorkerEventNewNonce) {
  15. mifare_nested_blink_nonce_collection_start(mifare_nested);
  16. uint8_t collected = 0;
  17. NonceList_t* nonces = mifare_nested->nonces;
  18. for(uint8_t tries = 0; tries < nonces->tries; tries++) {
  19. for(uint8_t sector = 0; sector < nonces->sector_count; sector++) {
  20. for(uint8_t keyType = 0; keyType < 2; keyType++) {
  21. Nonces* info = nonces->nonces[sector][keyType][tries];
  22. if(info->collected) {
  23. collected++;
  24. }
  25. }
  26. }
  27. }
  28. with_view_model(
  29. plugin_state->view,
  30. NestedAttackViewModel * model,
  31. {
  32. model->calibrating = false;
  33. model->lost_tag = false;
  34. model->nonces_collected = collected;
  35. model->keys_count = nonces->sector_count * nonces->tries * 2;
  36. },
  37. true);
  38. } else if(event == MifareNestedWorkerEventNoTagDetected) {
  39. mifare_nested_blink_start(mifare_nested);
  40. with_view_model(
  41. plugin_state->view, NestedAttackViewModel * model, { model->lost_tag = true; }, true);
  42. } else if(event == MifareNestedWorkerEventCalibrating) {
  43. mifare_nested_blink_calibration_start(mifare_nested);
  44. with_view_model(
  45. plugin_state->view,
  46. NestedAttackViewModel * model,
  47. {
  48. model->calibrating = true;
  49. model->lost_tag = false;
  50. model->need_prediction = false;
  51. },
  52. true);
  53. } else if(event == MifareNestedWorkerEventNeedPrediction) {
  54. with_view_model(
  55. plugin_state->view,
  56. NestedAttackViewModel * model,
  57. { model->need_prediction = true; },
  58. true);
  59. }
  60. view_dispatcher_send_custom_event(mifare_nested->view_dispatcher, event);
  61. return true;
  62. }
  63. void mifare_nested_scene_collecting_on_enter(void* context) {
  64. MifareNested* mifare_nested = context;
  65. NestedState* nested = mifare_nested->nested_state;
  66. mifare_nested_worker_start(
  67. mifare_nested->worker,
  68. mifare_nested->collecting_type,
  69. &mifare_nested->nfc_dev->dev_data,
  70. mifare_nested_collecting_worker_callback,
  71. mifare_nested);
  72. mifare_nested_blink_start(mifare_nested);
  73. with_view_model(
  74. nested->view,
  75. NestedAttackViewModel * model,
  76. {
  77. model->lost_tag = false;
  78. model->nonces_collected = 0;
  79. },
  80. false);
  81. view_dispatcher_switch_to_view(mifare_nested->view_dispatcher, MifareNestedViewCollecting);
  82. }
  83. bool mifare_nested_scene_collecting_on_event(void* context, SceneManagerEvent event) {
  84. MifareNested* mifare_nested = context;
  85. bool consumed = false;
  86. if(event.type == SceneManagerEventTypeCustom) {
  87. if(event.event == GuiButtonTypeCenter) {
  88. scene_manager_search_and_switch_to_previous_scene(mifare_nested->scene_manager, 0);
  89. consumed = true;
  90. } else if(event.event == MifareNestedWorkerEventNoncesCollected) {
  91. scene_manager_next_scene(
  92. mifare_nested->scene_manager, MifareNestedSceneNoncesCollected);
  93. consumed = true;
  94. } else if(event.event == MifareNestedWorkerEventAttackFailed) {
  95. scene_manager_next_scene(mifare_nested->scene_manager, MifareNestedSceneFailed);
  96. consumed = true;
  97. } else if(event.event == MifareNestedWorkerEventNeedKey) {
  98. scene_manager_next_scene(mifare_nested->scene_manager, MifareNestedSceneNoKeys);
  99. consumed = true;
  100. } else if(event.event == MifareNestedWorkerEventUnpredictablePRNG) {
  101. scene_manager_next_scene(
  102. mifare_nested->scene_manager, MifareNestedSceneUnpredictablePRNG);
  103. consumed = true;
  104. } else if(
  105. event.event == MifareNestedWorkerEventNewNonce ||
  106. event.event == MifareNestedWorkerEventNoTagDetected ||
  107. event.event == MifareNestedWorkerEventCalibrating ||
  108. event.event == MifareNestedWorkerEventNeedPrediction) {
  109. consumed = true;
  110. }
  111. }
  112. return consumed;
  113. }
  114. void mifare_nested_scene_collecting_on_exit(void* context) {
  115. MifareNested* mifare_nested = context;
  116. mifare_nested_worker_stop(mifare_nested->worker);
  117. // Clear view
  118. mifare_nested_blink_stop(mifare_nested);
  119. popup_reset(mifare_nested->popup);
  120. widget_reset(mifare_nested->widget);
  121. }