mag_scene_emulate_test.c 6.6 KB

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