lfrfid_app_scene_raw_read.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "lfrfid_app_scene_raw_read.h"
  2. #include <dolphin/dolphin.h>
  3. #define RAW_READ_TIME 5000
  4. static void lfrfid_read_callback(LFRFIDWorkerReadRawResult result, void* ctx) {
  5. LfRfidApp* app = static_cast<LfRfidApp*>(ctx);
  6. LfRfidApp::Event event;
  7. switch(result) {
  8. case LFRFIDWorkerReadRawFileError:
  9. event.type = LfRfidApp::EventType::ReadEventError;
  10. break;
  11. case LFRFIDWorkerReadRawOverrun:
  12. event.type = LfRfidApp::EventType::ReadEventOverrun;
  13. break;
  14. }
  15. app->view_controller.send_event(&event);
  16. }
  17. static void timer_callback(void* ctx) {
  18. LfRfidApp* app = static_cast<LfRfidApp*>(ctx);
  19. LfRfidApp::Event event;
  20. event.type = LfRfidApp::EventType::ReadEventDone;
  21. app->view_controller.send_event(&event);
  22. }
  23. void LfRfidAppSceneRawRead::on_enter(LfRfidApp* app, bool /* need_restore */) {
  24. string_init(string_file_name);
  25. auto popup = app->view_controller.get<PopupVM>();
  26. popup->set_icon(0, 3, &I_RFIDDolphinReceive_97x61);
  27. app->view_controller.switch_to<PopupVM>();
  28. lfrfid_worker_start_thread(app->lfworker);
  29. app->make_app_folder();
  30. timer = furi_timer_alloc(timer_callback, FuriTimerTypeOnce, app);
  31. furi_timer_start(timer, RAW_READ_TIME);
  32. string_printf(
  33. string_file_name, "%s/%s.ask.raw", app->app_sd_folder, string_get_cstr(app->raw_file_name));
  34. popup->set_header("Reading\nRAW RFID\nASK", 89, 30, AlignCenter, AlignTop);
  35. lfrfid_worker_read_raw_start(
  36. app->lfworker,
  37. string_get_cstr(string_file_name),
  38. LFRFIDWorkerReadTypeASKOnly,
  39. lfrfid_read_callback,
  40. app);
  41. notification_message(app->notification, &sequence_blink_start_cyan);
  42. is_psk = false;
  43. error = false;
  44. }
  45. bool LfRfidAppSceneRawRead::on_event(LfRfidApp* app, LfRfidApp::Event* event) {
  46. UNUSED(app);
  47. bool consumed = true;
  48. auto popup = app->view_controller.get<PopupVM>();
  49. switch(event->type) {
  50. case LfRfidApp::EventType::ReadEventError:
  51. error = true;
  52. popup->set_header("Reading\nRAW RFID\nFile error", 89, 30, AlignCenter, AlignTop);
  53. notification_message(app->notification, &sequence_blink_start_red);
  54. furi_timer_stop(timer);
  55. break;
  56. case LfRfidApp::EventType::ReadEventDone:
  57. if(!error) {
  58. if(is_psk) {
  59. notification_message(app->notification, &sequence_success);
  60. app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::RawSuccess);
  61. } else {
  62. popup->set_header("Reading\nRAW RFID\nPSK", 89, 30, AlignCenter, AlignTop);
  63. notification_message(app->notification, &sequence_blink_start_yellow);
  64. lfrfid_worker_stop(app->lfworker);
  65. string_printf(
  66. string_file_name,
  67. "%s/%s.psk.raw",
  68. app->app_sd_folder,
  69. string_get_cstr(app->raw_file_name));
  70. lfrfid_worker_read_raw_start(
  71. app->lfworker,
  72. string_get_cstr(string_file_name),
  73. LFRFIDWorkerReadTypePSKOnly,
  74. lfrfid_read_callback,
  75. app);
  76. furi_timer_start(timer, RAW_READ_TIME);
  77. is_psk = true;
  78. }
  79. }
  80. break;
  81. default:
  82. consumed = false;
  83. break;
  84. }
  85. return consumed;
  86. }
  87. void LfRfidAppSceneRawRead::on_exit(LfRfidApp* app) {
  88. notification_message(app->notification, &sequence_blink_stop);
  89. app->view_controller.get<PopupVM>()->clean();
  90. lfrfid_worker_stop(app->lfworker);
  91. lfrfid_worker_stop_thread(app->lfworker);
  92. furi_timer_free(timer);
  93. string_clear(string_file_name);
  94. }