lfrfid_scene_write.c 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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(!furi_string_empty(app->file_name)) {
  21. popup_set_text(popup, furi_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. 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->notifications, &sequence_blink_start_magenta);
  39. }
  40. bool lfrfid_scene_write_on_event(void* context, SceneManagerEvent event) {
  41. LfRfid* app = context;
  42. Popup* popup = app->popup;
  43. bool consumed = false;
  44. if(event.type == SceneManagerEventTypeCustom) {
  45. if(event.event == LfRfidEventWriteOK) {
  46. notification_message(app->notifications, &sequence_success);
  47. scene_manager_next_scene(app->scene_manager, LfRfidSceneWriteSuccess);
  48. consumed = true;
  49. } else if(event.event == LfRfidEventWriteProtocolCannotBeWritten) {
  50. popup_set_icon(popup, 72, 17, &I_DolphinCommon_56x48);
  51. popup_set_header(popup, "Error", 64, 3, AlignCenter, AlignTop);
  52. popup_set_text(popup, "This protocol\ncannot be written", 3, 17, AlignLeft, AlignTop);
  53. notification_message(app->notifications, &sequence_blink_start_red);
  54. consumed = true;
  55. } else if(
  56. (event.event == LfRfidEventWriteFobCannotBeWritten) ||
  57. (event.event == LfRfidEventWriteTooLongToWrite)) {
  58. popup_set_icon(popup, 72, 17, &I_DolphinCommon_56x48);
  59. popup_set_header(popup, "Still trying to write...", 64, 3, AlignCenter, AlignTop);
  60. popup_set_text(
  61. popup,
  62. "Make sure this\ncard is writable\nand not\nprotected.",
  63. 3,
  64. 17,
  65. AlignLeft,
  66. AlignTop);
  67. notification_message(app->notifications, &sequence_blink_start_yellow);
  68. consumed = true;
  69. }
  70. }
  71. return consumed;
  72. }
  73. void lfrfid_scene_write_on_exit(void* context) {
  74. LfRfid* app = context;
  75. notification_message(app->notifications, &sequence_blink_stop);
  76. popup_reset(app->popup);
  77. lfrfid_worker_stop(app->lfworker);
  78. lfrfid_worker_stop_thread(app->lfworker);
  79. size_t size = protocol_dict_get_data_size(app->dict, app->protocol_id);
  80. protocol_dict_set_data(app->dict, app->protocol_id, app->old_key_data, size);
  81. }