lfrfid_scene_write.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "../lfrfid_i.h"
  2. static void lfrfid_write_callback(LFRFIDWorkerWriteResult result, void* context) {
  3. LfRfid* app = context;
  4. uint32_t event = 0;
  5. if(result == LFRFIDWorkerWriteOK) {
  6. event = LfRfidEventWriteOK;
  7. } else if(result == LFRFIDWorkerWriteProtocolCannotBeWritten) {
  8. event = LfRfidEventWriteProtocolCannotBeWritten;
  9. } else if(result == LFRFIDWorkerWriteFobCannotBeWritten) {
  10. event = LfRfidEventWriteFobCannotBeWritten;
  11. } else if(result == LFRFIDWorkerWriteTooLongToWrite) {
  12. event = LfRfidEventWriteTooLongToWrite;
  13. }
  14. view_dispatcher_send_custom_event(app->view_dispatcher, event);
  15. }
  16. void lfrfid_scene_write_on_enter(void* context) {
  17. LfRfid* app = context;
  18. Popup* popup = app->popup;
  19. popup_set_header(popup, "Writing", 89, 30, AlignCenter, AlignTop);
  20. if(!string_empty_p(app->file_name)) {
  21. popup_set_text(popup, string_get_cstr(app->file_name), 89, 43, AlignCenter, AlignTop);
  22. } else {
  23. popup_set_text(
  24. popup,
  25. protocol_dict_get_name(app->dict, app->protocol_id),
  26. 89,
  27. 43,
  28. AlignCenter,
  29. AlignTop);
  30. }
  31. popup_set_icon(popup, 0, 3, &I_RFIDDolphinSend_97x61);
  32. view_dispatcher_switch_to_view(app->view_dispatcher, LfRfidViewPopup);
  33. size_t size = protocol_dict_get_data_size(app->dict, app->protocol_id);
  34. app->old_key_data = (uint8_t*)malloc(size);
  35. protocol_dict_get_data(app->dict, app->protocol_id, app->old_key_data, size);
  36. lfrfid_worker_start_thread(app->lfworker);
  37. lfrfid_worker_write_start(
  38. app->lfworker, (LFRFIDProtocol)app->protocol_id, lfrfid_write_callback, app);
  39. notification_message(app->notifications, &sequence_blink_start_magenta);
  40. }
  41. bool lfrfid_scene_write_on_event(void* context, SceneManagerEvent event) {
  42. LfRfid* app = context;
  43. Popup* popup = app->popup;
  44. bool consumed = false;
  45. if(event.type == SceneManagerEventTypeCustom) {
  46. if(event.event == LfRfidEventWriteOK) {
  47. notification_message(app->notifications, &sequence_success);
  48. scene_manager_next_scene(app->scene_manager, LfRfidSceneWriteSuccess);
  49. consumed = true;
  50. } else if(event.event == LfRfidEventWriteProtocolCannotBeWritten) {
  51. popup_set_icon(popup, 72, 17, &I_DolphinCommon_56x48);
  52. popup_set_header(popup, "Error", 64, 3, AlignCenter, AlignTop);
  53. popup_set_text(popup, "This protocol\ncannot be written", 3, 17, AlignLeft, AlignTop);
  54. notification_message(app->notifications, &sequence_blink_start_red);
  55. consumed = true;
  56. } else if(
  57. (event.event == LfRfidEventWriteFobCannotBeWritten) ||
  58. (event.event == LfRfidEventWriteTooLongToWrite)) {
  59. popup_set_icon(popup, 72, 17, &I_DolphinCommon_56x48);
  60. popup_set_header(popup, "Still trying to write...", 64, 3, AlignCenter, AlignTop);
  61. popup_set_text(
  62. popup,
  63. "Make sure this\ncard is writable\nand not\nprotected.",
  64. 3,
  65. 17,
  66. AlignLeft,
  67. AlignTop);
  68. notification_message(app->notifications, &sequence_blink_start_yellow);
  69. consumed = true;
  70. }
  71. }
  72. return consumed;
  73. }
  74. void lfrfid_scene_write_on_exit(void* context) {
  75. LfRfid* app = context;
  76. notification_message(app->notifications, &sequence_blink_stop);
  77. popup_reset(app->popup);
  78. lfrfid_worker_stop(app->lfworker);
  79. lfrfid_worker_stop_thread(app->lfworker);
  80. size_t size = protocol_dict_get_data_size(app->dict, app->protocol_id);
  81. protocol_dict_set_data(app->dict, app->protocol_id, app->old_key_data, size);
  82. free(app->old_key_data);
  83. }