weebo_scene_emulate.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "../weebo_i.h"
  2. #include <nfc/protocols/mf_ultralight/mf_ultralight_listener.h>
  3. #define TAG "SceneEmulate"
  4. void weebo_scene_emulate_widget_callback(GuiButtonType result, InputType type, void* context) {
  5. furi_assert(context);
  6. Weebo* weebo = context;
  7. if(type == InputTypeShort) {
  8. view_dispatcher_send_custom_event(weebo->view_dispatcher, result);
  9. }
  10. }
  11. void weebo_scene_emulate_draw_screen(Weebo* weebo) {
  12. Widget* widget = weebo->widget;
  13. FuriString* info_str = furi_string_alloc();
  14. FuriString* uid_str = furi_string_alloc();
  15. const MfUltralightData* data = nfc_device_get_data(weebo->nfc_device, NfcProtocolMfUltralight);
  16. furi_string_cat_printf(info_str, "Emulating");
  17. furi_string_cat_printf(
  18. uid_str,
  19. "%02X%02X%02X%02X%02X%02X%02X",
  20. data->iso14443_3a_data->uid[0],
  21. data->iso14443_3a_data->uid[1],
  22. data->iso14443_3a_data->uid[2],
  23. data->iso14443_3a_data->uid[3],
  24. data->iso14443_3a_data->uid[4],
  25. data->iso14443_3a_data->uid[5],
  26. data->iso14443_3a_data->uid[6]);
  27. widget_reset(widget);
  28. widget_add_string_element(
  29. widget, 64, 5, AlignCenter, AlignCenter, FontSecondary, furi_string_get_cstr(info_str));
  30. widget_add_string_element(
  31. widget, 64, 25, AlignCenter, AlignCenter, FontSecondary, furi_string_get_cstr(uid_str));
  32. widget_add_button_element(
  33. widget, GuiButtonTypeCenter, "Remix", weebo_scene_emulate_widget_callback, weebo);
  34. furi_string_free(info_str);
  35. furi_string_free(uid_str);
  36. view_dispatcher_switch_to_view(weebo->view_dispatcher, WeeboViewWidget);
  37. }
  38. void weebo_scene_emulate_on_enter(void* context) {
  39. Weebo* weebo = context;
  40. nfc_device_load(weebo->nfc_device, furi_string_get_cstr(weebo->load_path));
  41. const MfUltralightData* data = nfc_device_get_data(weebo->nfc_device, NfcProtocolMfUltralight);
  42. weebo->listener = nfc_listener_alloc(weebo->nfc, NfcProtocolMfUltralight, data);
  43. nfc_listener_start(weebo->listener, NULL, NULL);
  44. weebo_scene_emulate_draw_screen(weebo);
  45. weebo_blink_start(weebo);
  46. }
  47. bool weebo_scene_emulate_on_event(void* context, SceneManagerEvent event) {
  48. Weebo* weebo = context;
  49. bool consumed = false;
  50. if(event.type == SceneManagerEventTypeCustom) {
  51. scene_manager_set_scene_state(weebo->scene_manager, WeeboSceneEmulate, event.event);
  52. if(event.event == GuiButtonTypeCenter) {
  53. //stop listener
  54. FURI_LOG_D(TAG, "Stopping listener");
  55. nfc_listener_stop(weebo->listener);
  56. nfc_listener_free(weebo->listener);
  57. weebo->listener = NULL;
  58. weebo_remix(weebo);
  59. //start listener
  60. FURI_LOG_D(TAG, "Starting listener");
  61. const MfUltralightData* data =
  62. nfc_device_get_data(weebo->nfc_device, NfcProtocolMfUltralight);
  63. weebo->listener = nfc_listener_alloc(weebo->nfc, NfcProtocolMfUltralight, data);
  64. nfc_listener_start(weebo->listener, NULL, NULL);
  65. weebo_scene_emulate_draw_screen(weebo);
  66. consumed = true;
  67. }
  68. }
  69. return consumed;
  70. }
  71. void weebo_scene_emulate_on_exit(void* context) {
  72. Weebo* weebo = context;
  73. if(weebo->listener) {
  74. nfc_listener_stop(weebo->listener);
  75. nfc_listener_free(weebo->listener);
  76. weebo->listener = NULL;
  77. }
  78. widget_reset(weebo->widget);
  79. weebo_blink_stop(weebo);
  80. }