mag_scene_emulate_test.c 6.0 KB

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