lfrfid_scene_raw_read.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "../lfrfid_i.h"
  2. #define RAW_READ_TIME 5000
  3. typedef struct {
  4. FuriString* string_file_name;
  5. FuriTimer* timer;
  6. bool is_psk;
  7. bool error;
  8. } LfRfidReadRawState;
  9. static void lfrfid_read_callback(LFRFIDWorkerReadRawResult result, void* context) {
  10. LfRfid* app = context;
  11. if(result == LFRFIDWorkerReadRawFileError) {
  12. view_dispatcher_send_custom_event(app->view_dispatcher, LfRfidEventReadError);
  13. } else if(result == LFRFIDWorkerReadRawOverrun) {
  14. view_dispatcher_send_custom_event(app->view_dispatcher, LfRfidEventReadOverrun);
  15. }
  16. }
  17. static void timer_callback(void* context) {
  18. LfRfid* app = context;
  19. view_dispatcher_send_custom_event(app->view_dispatcher, LfRfidEventReadDone);
  20. }
  21. void lfrfid_scene_raw_read_on_enter(void* context) {
  22. LfRfid* app = context;
  23. Popup* popup = app->popup;
  24. LfRfidReadRawState* state = malloc(sizeof(LfRfidReadRawState));
  25. scene_manager_set_scene_state(app->scene_manager, LfRfidSceneRawRead, (uint32_t)state);
  26. state->string_file_name = furi_string_alloc();
  27. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  28. view_dispatcher_switch_to_view(app->view_dispatcher, LfRfidViewPopup);
  29. lfrfid_worker_start_thread(app->lfworker);
  30. lfrfid_make_app_folder(app);
  31. state->timer = furi_timer_alloc(timer_callback, FuriTimerTypeOnce, app);
  32. furi_timer_start(state->timer, RAW_READ_TIME);
  33. furi_string_printf(
  34. state->string_file_name,
  35. "%s/%s%s",
  36. LFRFID_SD_FOLDER,
  37. furi_string_get_cstr(app->raw_file_name),
  38. LFRFID_APP_RAW_ASK_EXTENSION);
  39. popup_set_header(popup, "Reading\nRAW RFID\nASK", 89, 30, AlignCenter, AlignTop);
  40. lfrfid_worker_read_raw_start(
  41. app->lfworker,
  42. furi_string_get_cstr(state->string_file_name),
  43. LFRFIDWorkerReadTypeASKOnly,
  44. lfrfid_read_callback,
  45. app);
  46. notification_message(app->notifications, &sequence_blink_start_cyan);
  47. state->is_psk = false;
  48. state->error = false;
  49. }
  50. bool lfrfid_scene_raw_read_on_event(void* context, SceneManagerEvent event) {
  51. LfRfid* app = context;
  52. Popup* popup = app->popup;
  53. LfRfidReadRawState* state =
  54. (LfRfidReadRawState*)scene_manager_get_scene_state(app->scene_manager, LfRfidSceneRawRead);
  55. bool consumed = false;
  56. furi_assert(state);
  57. if(event.type == SceneManagerEventTypeCustom) {
  58. if(event.event == LfRfidEventReadError) {
  59. consumed = true;
  60. state->error = true;
  61. popup_set_header(
  62. popup, "Reading\nRAW RFID\nFile error", 89, 30, AlignCenter, AlignTop);
  63. notification_message(app->notifications, &sequence_blink_start_red);
  64. furi_timer_stop(state->timer);
  65. } else if(event.event == LfRfidEventReadDone) {
  66. consumed = true;
  67. if(!state->error) {
  68. if(state->is_psk) {
  69. notification_message(app->notifications, &sequence_success);
  70. scene_manager_next_scene(app->scene_manager, LfRfidSceneRawSuccess);
  71. } else {
  72. popup_set_header(
  73. popup, "Reading\nRAW RFID\nPSK", 89, 30, AlignCenter, AlignTop);
  74. notification_message(app->notifications, &sequence_blink_start_yellow);
  75. lfrfid_worker_stop(app->lfworker);
  76. furi_string_printf(
  77. state->string_file_name,
  78. "%s/%s%s",
  79. LFRFID_SD_FOLDER,
  80. furi_string_get_cstr(app->raw_file_name),
  81. LFRFID_APP_RAW_PSK_EXTENSION);
  82. lfrfid_worker_read_raw_start(
  83. app->lfworker,
  84. furi_string_get_cstr(state->string_file_name),
  85. LFRFIDWorkerReadTypePSKOnly,
  86. lfrfid_read_callback,
  87. app);
  88. furi_timer_start(state->timer, RAW_READ_TIME);
  89. state->is_psk = true;
  90. }
  91. }
  92. }
  93. }
  94. return consumed;
  95. }
  96. void lfrfid_scene_raw_read_on_exit(void* context) {
  97. LfRfid* app = context;
  98. LfRfidReadRawState* state =
  99. (LfRfidReadRawState*)scene_manager_get_scene_state(app->scene_manager, LfRfidSceneRawRead);
  100. notification_message(app->notifications, &sequence_blink_stop);
  101. popup_reset(app->popup);
  102. lfrfid_worker_stop(app->lfworker);
  103. lfrfid_worker_stop_thread(app->lfworker);
  104. furi_timer_free(state->timer);
  105. furi_string_free(state->string_file_name);
  106. free(state);
  107. }