lfrfid_app_scene_save_data.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "lfrfid_app_scene_save_data.h"
  2. #include <dolphin/dolphin.h>
  3. static void print_buffer(const uint8_t* buffer) {
  4. for(uint8_t i = 0; i < LFRFID_KEY_SIZE; i++) {
  5. printf("%02X", buffer[i]);
  6. }
  7. }
  8. void LfRfidAppSceneSaveData::on_enter(LfRfidApp* app, bool need_restore) {
  9. auto byte_input = app->view_controller.get<ByteInputVM>();
  10. RfidKey& key = app->worker.key;
  11. printf("k: ");
  12. print_buffer(key.get_data());
  13. printf(" o: ");
  14. print_buffer(old_key_data);
  15. printf(" n: ");
  16. print_buffer(new_key_data);
  17. printf("\r\n");
  18. if(need_restore) printf("restored\r\n");
  19. if(need_restore) {
  20. key.set_data(old_key_data, key.get_type_data_count());
  21. } else {
  22. memcpy(old_key_data, key.get_data(), key.get_type_data_count());
  23. }
  24. memcpy(new_key_data, key.get_data(), key.get_type_data_count());
  25. byte_input->set_header_text("Enter the data in hex");
  26. byte_input->set_result_callback(
  27. save_callback, NULL, app, new_key_data, app->worker.key.get_type_data_count());
  28. app->view_controller.switch_to<ByteInputVM>();
  29. }
  30. bool LfRfidAppSceneSaveData::on_event(LfRfidApp* app, LfRfidApp::Event* event) {
  31. bool consumed = false;
  32. RfidKey& key = app->worker.key;
  33. if(event->type == LfRfidApp::EventType::Next) {
  34. key.set_data(new_key_data, key.get_type_data_count());
  35. DOLPHIN_DEED(DolphinDeedRfidAdd);
  36. app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::SaveName);
  37. }
  38. return consumed;
  39. }
  40. void LfRfidAppSceneSaveData::on_exit(LfRfidApp* app) {
  41. app->view_controller.get<ByteInputVM>()->clean();
  42. }
  43. void LfRfidAppSceneSaveData::save_callback(void* context) {
  44. LfRfidApp* app = static_cast<LfRfidApp*>(context);
  45. LfRfidApp::Event event;
  46. event.type = LfRfidApp::EventType::Next;
  47. app->view_controller.send_event(&event);
  48. }