mag_scene_emulate_test.c 6.1 KB

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