mag_scene_emulate_test.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "../mag_i.h"
  2. #define PIN_A 0
  3. #define PIN_B 1 // currently unused
  4. #define CLOCK_US 240 // typically set between 200-500us
  5. #define TEST_STR "%B123456781234567^LASTNAME/FIRST^YYMMSSSDDDDDDDDDDDDDDDDDDDDDDDDD?;1234567812?"
  6. // TODO: better way of setting temp test str,
  7. // text wrapping on screen? (Will be relevant for any loaded data too)
  8. uint8_t magspoof_bit_dir = 0;
  9. const char *test_str = TEST_STR;
  10. void gpio_item_set_rfid_pin(uint8_t index, bool level) {
  11. if (index == 0) {
  12. furi_hal_gpio_write(&gpio_rfid_carrier_out, level);
  13. // A7 GPIO pin for debugging purposes
  14. // furi_hal_gpio_write(&gpio_ext_pa7, level);
  15. }
  16. }
  17. static void play_bit(uint8_t send_bit) {
  18. magspoof_bit_dir ^= 1;
  19. gpio_item_set_rfid_pin(PIN_A, magspoof_bit_dir);
  20. // PIN_B goes unused in current LF modulation.
  21. // Leaving legacy here in event we attempt downstream modulation,
  22. // rather than just modulating RFID_OUT upstream for signal forming
  23. gpio_item_set_rfid_pin(PIN_B, !magspoof_bit_dir);
  24. furi_delay_us(CLOCK_US);
  25. if (send_bit) {
  26. magspoof_bit_dir ^= 1;
  27. gpio_item_set_rfid_pin(PIN_A, magspoof_bit_dir);
  28. gpio_item_set_rfid_pin(PIN_B, !magspoof_bit_dir);
  29. }
  30. furi_delay_us(CLOCK_US);
  31. }
  32. static void mag_spoof(FuriString *track_str, uint8_t track) {
  33. furi_hal_power_enable_otg();
  34. size_t from;
  35. size_t to;
  36. // TODO ';' in first track case
  37. if (track == 0) {
  38. from = furi_string_search_char(track_str, '%');
  39. to = furi_string_search_char(track_str, '?', from);
  40. } else if (track == 1) {
  41. from = furi_string_search_char(track_str, ';');
  42. to = furi_string_search_char(track_str, '?', from);
  43. } else {
  44. from = 0;
  45. to = furi_string_size(track_str);
  46. }
  47. if (from >= to) {
  48. return;
  49. }
  50. furi_string_mid(track_str, from, to - from + 1);
  51. const char *data = furi_string_get_cstr(track_str);
  52. printf("%s", data);
  53. furi_hal_ibutton_start_drive();
  54. furi_hal_ibutton_pin_low();
  55. // Initializing at GpioSpeedLow seems sufficient for our needs; no improvements seen by increasing speed setting
  56. // this doesn't seem to make a difference, leaving it in
  57. furi_hal_gpio_init(&gpio_rfid_data_in, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  58. furi_hal_gpio_write(&gpio_rfid_data_in, false);
  59. // false->ground RFID antenna; true->don't ground
  60. // skotopes (RFID dev) say normally you'd want RFID_PULL in high for signal forming, while modulating RFID_OUT
  61. // dunaevai135 had it low in their old code. Leaving low, as it doesn't seem to make a difference on my janky antenna
  62. furi_hal_gpio_init(&gpio_nfc_irq_rfid_pull, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  63. furi_hal_gpio_write(&gpio_nfc_irq_rfid_pull, false);
  64. furi_hal_gpio_init(&gpio_rfid_carrier_out, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  65. // A7 GPIO pin for debugging purposes
  66. // furi_hal_gpio_init(&gpio_ext_pa7, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  67. // TODO: initialize pins on scene enter, perhaps, so as to avoid this delay each time the button is pressed?
  68. // Also, why is such a long delay needed?
  69. furi_delay_ms(300);
  70. // prevents interrupts &c. from impacting critical timings
  71. FURI_CRITICAL_ENTER();
  72. const uint8_t bitlen[] = {7, 5, 5};
  73. const int sublen[] = {32, 48, 48};
  74. int tmp, crc, lrc = 0;
  75. magspoof_bit_dir = 0;
  76. // First put out a bunch of leading zeros.
  77. for (uint8_t i = 0; i < 25; i++) {
  78. play_bit(0);
  79. }
  80. for (uint8_t i = 0; data[i] != '\0'; i++) {
  81. crc = 1;
  82. tmp = data[i] - sublen[track];
  83. for (uint8_t j = 0; j < bitlen[track] - 1; j++) {
  84. crc ^= tmp & 1;
  85. lrc ^= (tmp & 1) << j;
  86. play_bit(tmp & 1);
  87. tmp >>= 1;
  88. }
  89. play_bit(crc);
  90. }
  91. // finish calculating and send last "byte" (LRC)
  92. tmp = lrc;
  93. crc = 1;
  94. for (uint8_t j = 0; j < bitlen[track] - 1; j++) {
  95. crc ^= tmp & 1;
  96. play_bit(tmp & 1);
  97. tmp >>= 1;
  98. }
  99. play_bit(crc);
  100. // finish with 0's
  101. for (uint8_t i = 0; i < 5 * 5; i++) {
  102. play_bit(0);
  103. }
  104. gpio_item_set_rfid_pin(PIN_A, 0);
  105. gpio_item_set_rfid_pin(PIN_B, 0);
  106. // end critical timing section
  107. FURI_CRITICAL_EXIT();
  108. furi_hal_rfid_pins_reset();
  109. furi_hal_power_disable_otg();
  110. }
  111. void mag_scene_emulate_test_on_enter(void *context) {
  112. Mag *mag = context;
  113. Widget *widget = mag->widget;
  114. //FuriString *tmp_string;
  115. //tmp_string = furi_string_alloc();
  116. widget_add_button_element(widget, GuiButtonTypeLeft, "Back", mag_widget_callback, mag);
  117. widget_add_button_element(widget, GuiButtonTypeRight, "Emulate", mag_widget_callback, mag);
  118. //furi_string_printf(tmp_string, test_str);
  119. //widget_add_string_element(
  120. // widget, 64, 0, AlignLeft, AlignTop, FontSecondary, furi_string_get_cstr(tmp_string));
  121. //furi_string_reset(tmp_string);
  122. view_dispatcher_switch_to_view(mag->view_dispatcher, MagViewWidget);
  123. //furi_string_free(tmp_string);
  124. }
  125. bool mag_scene_emulate_test_on_event(void *context, SceneManagerEvent event) {
  126. Mag *mag = context;
  127. SceneManager *scene_manager = mag->scene_manager;
  128. bool consumed = false;
  129. if (event.type == SceneManagerEventTypeCustom) {
  130. if (event.event == GuiButtonTypeRight) {
  131. consumed = true;
  132. // Hardcoding a test string for the time being, while we debug/improve LF RFID TX
  133. FuriString *v = furi_string_alloc();
  134. furi_string_set_str(v, test_str);
  135. // blink led while spoofing
  136. notification_message(mag->notifications, &sequence_blink_start_magenta);
  137. mag_spoof(v, 0);
  138. notification_message(mag->notifications, &sequence_blink_stop);
  139. furi_string_free(v);
  140. } else if (event.event == GuiButtonTypeLeft) {
  141. consumed = true;
  142. scene_manager_previous_scene(scene_manager);
  143. }
  144. }
  145. return consumed;
  146. }
  147. void mag_scene_emulate_test_on_exit(void *context) {
  148. Mag *mag = context;
  149. widget_reset(mag->widget);
  150. }