lfrfid_app_scene_write.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "lfrfid_app_scene_write.h"
  2. static void lfrfid_write_callback(LFRFIDWorkerWriteResult result, void* ctx) {
  3. LfRfidApp* app = static_cast<LfRfidApp*>(ctx);
  4. LfRfidApp::Event event;
  5. switch(result) {
  6. case LFRFIDWorkerWriteOK:
  7. event.type = LfRfidApp::EventType::WriteEventOK;
  8. break;
  9. case LFRFIDWorkerWriteProtocolCannotBeWritten:
  10. event.type = LfRfidApp::EventType::WriteEventProtocolCannotBeWritten;
  11. break;
  12. case LFRFIDWorkerWriteFobCannotBeWritten:
  13. event.type = LfRfidApp::EventType::WriteEventFobCannotBeWritten;
  14. break;
  15. case LFRFIDWorkerWriteTooLongToWrite:
  16. event.type = LfRfidApp::EventType::WriteEventTooLongToWrite;
  17. break;
  18. }
  19. app->view_controller.send_event(&event);
  20. }
  21. void LfRfidAppSceneWrite::on_enter(LfRfidApp* app, bool /* need_restore */) {
  22. auto popup = app->view_controller.get<PopupVM>();
  23. popup->set_header("Writing", 89, 30, AlignCenter, AlignTop);
  24. if(string_size(app->file_name)) {
  25. popup->set_text(string_get_cstr(app->file_name), 89, 43, AlignCenter, AlignTop);
  26. } else {
  27. popup->set_text(
  28. protocol_dict_get_name(app->dict, app->protocol_id), 89, 43, AlignCenter, AlignTop);
  29. }
  30. popup->set_icon(0, 3, &I_RFIDDolphinSend_97x61);
  31. app->view_controller.switch_to<PopupVM>();
  32. size_t size = protocol_dict_get_data_size(app->dict, app->protocol_id);
  33. app->old_key_data = (uint8_t*)malloc(size);
  34. protocol_dict_get_data(app->dict, app->protocol_id, app->old_key_data, size);
  35. lfrfid_worker_start_thread(app->lfworker);
  36. lfrfid_worker_write_start(
  37. app->lfworker, (LFRFIDProtocol)app->protocol_id, lfrfid_write_callback, app);
  38. notification_message(app->notification, &sequence_blink_start_magenta);
  39. }
  40. bool LfRfidAppSceneWrite::on_event(LfRfidApp* app, LfRfidApp::Event* event) {
  41. bool consumed = true;
  42. auto popup = app->view_controller.get<PopupVM>();
  43. switch(event->type) {
  44. case LfRfidApp::EventType::WriteEventOK:
  45. notification_message(app->notification, &sequence_success);
  46. app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::WriteSuccess);
  47. break;
  48. case LfRfidApp::EventType::WriteEventProtocolCannotBeWritten:
  49. popup->set_icon(72, 17, &I_DolphinCommon_56x48);
  50. popup->set_header("Error", 64, 3, AlignCenter, AlignTop);
  51. popup->set_text("This protocol\ncannot be written", 3, 17, AlignLeft, AlignTop);
  52. notification_message(app->notification, &sequence_blink_start_red);
  53. break;
  54. case LfRfidApp::EventType::WriteEventFobCannotBeWritten:
  55. case LfRfidApp::EventType::WriteEventTooLongToWrite:
  56. popup->set_icon(72, 17, &I_DolphinCommon_56x48);
  57. popup->set_header("Still trying to write...", 64, 3, AlignCenter, AlignTop);
  58. popup->set_text(
  59. "Make sure this\ncard is writable\nand not\nprotected.", 3, 17, AlignLeft, AlignTop);
  60. notification_message(app->notification, &sequence_blink_start_yellow);
  61. break;
  62. default:
  63. consumed = false;
  64. }
  65. return consumed;
  66. }
  67. void LfRfidAppSceneWrite::on_exit(LfRfidApp* app) {
  68. notification_message(app->notification, &sequence_blink_stop);
  69. app->view_controller.get<PopupVM>()->clean();
  70. lfrfid_worker_stop(app->lfworker);
  71. lfrfid_worker_stop_thread(app->lfworker);
  72. size_t size = protocol_dict_get_data_size(app->dict, app->protocol_id);
  73. protocol_dict_set_data(app->dict, app->protocol_id, app->old_key_data, size);
  74. free(app->old_key_data);
  75. }