weebo_scene_emulate.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "../weebo_i.h"
  2. #include <nfc/protocols/mf_ultralight/mf_ultralight_listener.h>
  3. #define TAG "SceneEmulate"
  4. void weebo_scene_emulate_calculate_pwd(uint8_t* uid, uint8_t* pwd) {
  5. pwd[0] = uid[1] ^ uid[3] ^ 0xAA;
  6. pwd[1] = uid[2] ^ uid[4] ^ 0x55;
  7. pwd[2] = uid[3] ^ uid[5] ^ 0xAA;
  8. pwd[3] = uid[4] ^ uid[6] ^ 0x55;
  9. }
  10. void weebo_scene_emulate_remix(Weebo* weebo) {
  11. uint8_t PWD[4];
  12. uint8_t UID[8];
  13. uint8_t modified[NTAG215_SIZE];
  14. MfUltralightData* data = mf_ultralight_alloc();
  15. nfc_device_copy_data(weebo->nfc_device, NfcProtocolMfUltralight, data);
  16. //random uid
  17. FURI_LOG_D(TAG, "Generating random UID");
  18. UID[0] = 0x04;
  19. furi_hal_random_fill_buf(UID + 1, 6);
  20. UID[7] = UID[3] ^ UID[4] ^ UID[5] ^ UID[6];
  21. memcpy(weebo->figure + NFC3D_UID_OFFSET, UID, 8);
  22. memcpy(data->iso14443_3a_data->uid, UID, 7);
  23. //pack
  24. nfc3d_amiibo_pack(&weebo->amiiboKeys, weebo->figure, modified);
  25. //copy data in
  26. for(size_t i = 0; i < 130; i++) {
  27. memcpy(
  28. data->page[i].data, modified + i * MF_ULTRALIGHT_PAGE_SIZE, MF_ULTRALIGHT_PAGE_SIZE);
  29. }
  30. //new pwd
  31. weebo_scene_emulate_calculate_pwd(data->iso14443_3a_data->uid, PWD);
  32. memcpy(data->page[133].data, PWD, sizeof(PWD));
  33. //set data
  34. nfc_device_set_data(weebo->nfc_device, NfcProtocolMfUltralight, data);
  35. mf_ultralight_free(data);
  36. }
  37. void weebo_scene_emulate_widget_callback(GuiButtonType result, InputType type, void* context) {
  38. furi_assert(context);
  39. Weebo* weebo = context;
  40. if(type == InputTypeShort) {
  41. view_dispatcher_send_custom_event(weebo->view_dispatcher, result);
  42. }
  43. }
  44. void weebo_scene_emulate_on_enter(void* context) {
  45. Weebo* weebo = context;
  46. Widget* widget = weebo->widget;
  47. nfc_device_load(weebo->nfc_device, furi_string_get_cstr(weebo->load_path));
  48. const MfUltralightData* data = nfc_device_get_data(weebo->nfc_device, NfcProtocolMfUltralight);
  49. weebo->listener = nfc_listener_alloc(weebo->nfc, NfcProtocolMfUltralight, data);
  50. nfc_listener_start(weebo->listener, NULL, NULL);
  51. FuriString* info_str = furi_string_alloc();
  52. furi_string_cat_printf(info_str, "Emulating");
  53. widget_add_string_element(
  54. widget, 64, 5, AlignCenter, AlignCenter, FontSecondary, furi_string_get_cstr(info_str));
  55. widget_add_button_element(
  56. widget, GuiButtonTypeCenter, "Remix", weebo_scene_emulate_widget_callback, weebo);
  57. furi_string_free(info_str);
  58. weebo_blink_start(weebo);
  59. view_dispatcher_switch_to_view(weebo->view_dispatcher, WeeboViewWidget);
  60. }
  61. bool weebo_scene_emulate_on_event(void* context, SceneManagerEvent event) {
  62. Weebo* weebo = context;
  63. bool consumed = false;
  64. if(event.type == SceneManagerEventTypeCustom) {
  65. scene_manager_set_scene_state(weebo->scene_manager, WeeboSceneEmulate, event.event);
  66. if(event.event == GuiButtonTypeCenter) {
  67. //stop listener
  68. FURI_LOG_D(TAG, "Stopping listener");
  69. nfc_listener_stop(weebo->listener);
  70. nfc_listener_free(weebo->listener);
  71. weebo->listener = NULL;
  72. weebo_scene_emulate_remix(weebo);
  73. //start listener
  74. FURI_LOG_D(TAG, "Starting listener");
  75. const MfUltralightData* data =
  76. nfc_device_get_data(weebo->nfc_device, NfcProtocolMfUltralight);
  77. weebo->listener = nfc_listener_alloc(weebo->nfc, NfcProtocolMfUltralight, data);
  78. nfc_listener_start(weebo->listener, NULL, NULL);
  79. consumed = true;
  80. }
  81. }
  82. return consumed;
  83. }
  84. void weebo_scene_emulate_on_exit(void* context) {
  85. Weebo* weebo = context;
  86. if(weebo->listener) {
  87. nfc_listener_stop(weebo->listener);
  88. nfc_listener_free(weebo->listener);
  89. weebo->listener = NULL;
  90. }
  91. widget_reset(weebo->widget);
  92. weebo_blink_stop(weebo);
  93. }