|
@@ -15,18 +15,25 @@
|
|
|
const uint8_t bitlen[] = {7, 5, 5};
|
|
const uint8_t bitlen[] = {7, 5, 5};
|
|
|
// char offset by track
|
|
// char offset by track
|
|
|
const int sublen[] = {32, 48, 48};
|
|
const int sublen[] = {32, 48, 48};
|
|
|
-uint8_t bit_dir = 0;
|
|
|
|
|
|
|
+
|
|
|
uint8_t last_value = 2;
|
|
uint8_t last_value = 2;
|
|
|
|
|
|
|
|
-void bitbang_raw(bool value, MagSetting* setting) {
|
|
|
|
|
|
|
+void play_halfbit(bool value, MagSetting* setting) {
|
|
|
switch(setting->tx) {
|
|
switch(setting->tx) {
|
|
|
case MagTxStateRFID:
|
|
case MagTxStateRFID:
|
|
|
furi_hal_gpio_write(RFID_PIN_OUT, value);
|
|
furi_hal_gpio_write(RFID_PIN_OUT, value);
|
|
|
break;
|
|
break;
|
|
|
- case MagTxStateGPIOA6A7:
|
|
|
|
|
|
|
+ case MagTxStateGPIO:
|
|
|
furi_hal_gpio_write(GPIO_PIN_A, value);
|
|
furi_hal_gpio_write(GPIO_PIN_A, value);
|
|
|
furi_hal_gpio_write(GPIO_PIN_B, !value);
|
|
furi_hal_gpio_write(GPIO_PIN_B, !value);
|
|
|
break;
|
|
break;
|
|
|
|
|
+ case MagTxStatePiezo:
|
|
|
|
|
+ furi_hal_gpio_write(&gpio_speaker, value);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case MagTxStateLF_P:
|
|
|
|
|
+ furi_hal_gpio_write(RFID_PIN_OUT, value);
|
|
|
|
|
+ furi_hal_gpio_write(&gpio_speaker, value);
|
|
|
|
|
+ break;
|
|
|
case MagTxCC1101_434:
|
|
case MagTxCC1101_434:
|
|
|
case MagTxCC1101_868:
|
|
case MagTxCC1101_868:
|
|
|
if(last_value == 2 || value != (bool)last_value) {
|
|
if(last_value == 2 || value != (bool)last_value) {
|
|
@@ -42,76 +49,45 @@ void bitbang_raw(bool value, MagSetting* setting) {
|
|
|
last_value = value;
|
|
last_value = value;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void play_bit_rf(bool bit, MagSetting* setting) {
|
|
|
|
|
- bit_dir ^= 1;
|
|
|
|
|
- furi_hal_gpio_write(&gpio_cc1101_g0, true);
|
|
|
|
|
- furi_delay_us(64);
|
|
|
|
|
- furi_hal_gpio_write(&gpio_cc1101_g0, false);
|
|
|
|
|
- furi_delay_us(setting->us_clock);
|
|
|
|
|
-
|
|
|
|
|
- if(bit) {
|
|
|
|
|
- furi_hal_gpio_write(&gpio_cc1101_g0, true);
|
|
|
|
|
- furi_delay_us(64);
|
|
|
|
|
- furi_hal_gpio_write(&gpio_cc1101_g0, false);
|
|
|
|
|
- }
|
|
|
|
|
- furi_delay_us(setting->us_clock);
|
|
|
|
|
- furi_delay_us(setting->us_interpacket);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void play_bit_rfid(uint8_t send_bit, MagSetting* setting) {
|
|
|
|
|
- // internal TX over RFID coil
|
|
|
|
|
- bit_dir ^= 1;
|
|
|
|
|
- furi_hal_gpio_write(RFID_PIN_OUT, bit_dir);
|
|
|
|
|
- furi_delay_us(setting->us_clock);
|
|
|
|
|
-
|
|
|
|
|
- if(send_bit) {
|
|
|
|
|
- bit_dir ^= 1;
|
|
|
|
|
- furi_hal_gpio_write(RFID_PIN_OUT, bit_dir);
|
|
|
|
|
- }
|
|
|
|
|
- furi_delay_us(setting->us_clock);
|
|
|
|
|
-
|
|
|
|
|
- furi_delay_us(setting->us_interpacket);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void play_bit_gpio(uint8_t send_bit, MagSetting* setting) {
|
|
|
|
|
- // external TX over motor driver wired to PIN_A and PIN_B
|
|
|
|
|
- bit_dir ^= 1;
|
|
|
|
|
- furi_hal_gpio_write(GPIO_PIN_A, bit_dir);
|
|
|
|
|
- furi_hal_gpio_write(GPIO_PIN_B, !bit_dir);
|
|
|
|
|
- furi_delay_us(setting->us_clock);
|
|
|
|
|
-
|
|
|
|
|
- if(send_bit) {
|
|
|
|
|
- bit_dir ^= 1;
|
|
|
|
|
- furi_hal_gpio_write(GPIO_PIN_A, bit_dir);
|
|
|
|
|
- furi_hal_gpio_write(GPIO_PIN_B, !bit_dir);
|
|
|
|
|
- }
|
|
|
|
|
- furi_delay_us(setting->us_clock);
|
|
|
|
|
-
|
|
|
|
|
- furi_delay_us(setting->us_interpacket);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-bool play_bit(uint8_t send_bit, MagSetting* setting) {
|
|
|
|
|
- // Initialize configured TX method
|
|
|
|
|
- switch(setting->tx) {
|
|
|
|
|
- case MagTxStateRFID:
|
|
|
|
|
- play_bit_rfid(send_bit, setting);
|
|
|
|
|
- break;
|
|
|
|
|
- case MagTxStateGPIOA6A7:
|
|
|
|
|
- play_bit_gpio(send_bit, setting);
|
|
|
|
|
- break;
|
|
|
|
|
- case MagTxCC1101_434:
|
|
|
|
|
- case MagTxCC1101_868:
|
|
|
|
|
- play_bit_rf(send_bit & 0x01, setting);
|
|
|
|
|
- break;
|
|
|
|
|
- default:
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+void play_track(uint8_t* bits_manchester, uint16_t n_bits, MagSetting* setting, bool reverse) {
|
|
|
|
|
+ for(uint16_t i = 0; i < n_bits; i++) {
|
|
|
|
|
+ uint16_t j = (reverse) ? (n_bits - i - 1) : i;
|
|
|
|
|
+ uint8_t byte = j / 8;
|
|
|
|
|
+ uint8_t bitmask = 1 << (7 - (j % 8));
|
|
|
|
|
+ /* Bits are stored in their arrays like on a card (LSB first). This is not how usually bits are stored in a
|
|
|
|
|
+ * byte, with the MSB first. the var bitmask creates the pattern to iterate through each bit, LSB first, like so
|
|
|
|
|
+ * 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x80... masking bits one by one from the current byte
|
|
|
|
|
+ *
|
|
|
|
|
+ * I've chosen this LSB approach since bits and bytes are hard enough to visualize with the 5/8 and 7/8 encoding
|
|
|
|
|
+ * MSR uses. It's a biiit more complicated to process, but visualizing it with printf or a debugger is
|
|
|
|
|
+ * infinitely easier
|
|
|
|
|
+ *
|
|
|
|
|
+ * Encoding the following pairs of 5 bits as 5/8: A1234 B1234 C1234 D1234
|
|
|
|
|
+ * using this LSB format looks like: A1234B12 34C1234D 12340000
|
|
|
|
|
+ * using the MSB format, looks like: 21B4321A D4321C43 00004321
|
|
|
|
|
+ * this means reading each byte backwards when printing/debugging, and the jumping 16 bits ahead, reading 8 more
|
|
|
|
|
+ * bits backward, jumping 16 more bits ahead.
|
|
|
|
|
+ *
|
|
|
|
|
+ * I find this much more convenient for debugging, with the tiny incovenience of reading the bits in reverse
|
|
|
|
|
+ * order. Thus, the reason for the bitmask above
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+ bool bit = !!(bits_manchester[byte] & bitmask);
|
|
|
|
|
+
|
|
|
|
|
+ // TODO: reimplement timing delays. Replace fixed furi_hal_cortex_delay_us to wait instead to a specific value
|
|
|
|
|
+ // for DWT->CYCCNT. Note timer is aliased to 64us as per
|
|
|
|
|
+ // #define FURI_HAL_CORTEX_INSTRUCTIONS_PER_MICROSECOND (SystemCoreClock / 1000000) | furi_hal_cortex.c
|
|
|
|
|
+
|
|
|
|
|
+ play_halfbit(bit, setting);
|
|
|
|
|
+ furi_delay_us(setting->us_clock);
|
|
|
|
|
+ // if (i % 2 == 1) furi_delay_us(setting->us_interpacket);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return true;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void tx_init_rfid() {
|
|
void tx_init_rfid() {
|
|
|
// initialize RFID system for TX
|
|
// initialize RFID system for TX
|
|
|
|
|
+
|
|
|
|
|
+ // OTG needed for RFID? Or just legacy from GPIO?
|
|
|
furi_hal_power_enable_otg();
|
|
furi_hal_power_enable_otg();
|
|
|
|
|
|
|
|
furi_hal_ibutton_start_drive();
|
|
furi_hal_ibutton_start_drive();
|
|
@@ -142,29 +118,6 @@ void tx_deinit_rfid() {
|
|
|
furi_hal_power_disable_otg();
|
|
furi_hal_power_disable_otg();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void tx_init_gpio() {
|
|
|
|
|
- furi_hal_power_enable_otg();
|
|
|
|
|
- // gpio_item_configure_all_pins(GpioModeOutputPushPull);
|
|
|
|
|
- furi_hal_gpio_init(GPIO_PIN_A, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
|
|
|
|
|
- furi_hal_gpio_init(GPIO_PIN_B, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
|
|
|
|
|
- furi_hal_gpio_init(GPIO_PIN_ENABLE, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
|
|
|
|
|
-
|
|
|
|
|
- furi_hal_gpio_write(GPIO_PIN_ENABLE, 1);
|
|
|
|
|
-
|
|
|
|
|
- // had some issues with ~300; bumped higher temporarily
|
|
|
|
|
- furi_delay_ms(500);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void tx_deinit_gpio() {
|
|
|
|
|
- furi_hal_gpio_write(GPIO_PIN_A, 0);
|
|
|
|
|
- furi_hal_gpio_write(GPIO_PIN_B, 0);
|
|
|
|
|
- furi_hal_gpio_write(GPIO_PIN_ENABLE, 0);
|
|
|
|
|
-
|
|
|
|
|
- // set back to analog output mode?
|
|
|
|
|
- //gpio_item_configure_all_pins(GpioModeAnalog);
|
|
|
|
|
- furi_hal_power_disable_otg();
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
void tx_init_rf(int hz) {
|
|
void tx_init_rf(int hz) {
|
|
|
// presets and frequency will need some experimenting
|
|
// presets and frequency will need some experimenting
|
|
|
furi_hal_subghz_reset();
|
|
furi_hal_subghz_reset();
|
|
@@ -179,10 +132,14 @@ void tx_init_rf(int hz) {
|
|
|
furi_hal_gpio_write(&gpio_cc1101_g0, false);
|
|
furi_hal_gpio_write(&gpio_cc1101_g0, false);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void tx_deinit_rf() {
|
|
|
|
|
- furi_hal_gpio_write(&gpio_cc1101_g0, false);
|
|
|
|
|
- furi_hal_subghz_reset();
|
|
|
|
|
- furi_hal_subghz_idle();
|
|
|
|
|
|
|
+void tx_init_piezo() {
|
|
|
|
|
+ // TODO: some special mutex acquire procedure? c.f. furi_hal_speaker.c
|
|
|
|
|
+ furi_hal_gpio_init(&gpio_speaker, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void tx_deinit_piezo() {
|
|
|
|
|
+ // TODO: some special mutex release procedure?
|
|
|
|
|
+ furi_hal_gpio_init(&gpio_speaker, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool tx_init(MagSetting* setting) {
|
|
bool tx_init(MagSetting* setting) {
|
|
@@ -191,8 +148,24 @@ bool tx_init(MagSetting* setting) {
|
|
|
case MagTxStateRFID:
|
|
case MagTxStateRFID:
|
|
|
tx_init_rfid();
|
|
tx_init_rfid();
|
|
|
break;
|
|
break;
|
|
|
- case MagTxStateGPIOA6A7:
|
|
|
|
|
- tx_init_gpio();
|
|
|
|
|
|
|
+ case MagTxStateGPIO:
|
|
|
|
|
+ furi_hal_power_enable_otg();
|
|
|
|
|
+ // gpio_item_configure_all_pins(GpioModeOutputPushPull);
|
|
|
|
|
+ furi_hal_gpio_init(GPIO_PIN_A, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
|
|
|
|
|
+ furi_hal_gpio_init(GPIO_PIN_B, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
|
|
|
|
|
+ furi_hal_gpio_init(GPIO_PIN_ENABLE, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
|
|
|
|
|
+
|
|
|
|
|
+ furi_hal_gpio_write(GPIO_PIN_ENABLE, 1);
|
|
|
|
|
+
|
|
|
|
|
+ // had some issues with ~300; bumped higher temporarily
|
|
|
|
|
+ furi_delay_ms(500);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case MagTxStatePiezo:
|
|
|
|
|
+ tx_init_piezo();
|
|
|
|
|
+ break;
|
|
|
|
|
+ case MagTxStateLF_P:
|
|
|
|
|
+ tx_init_piezo();
|
|
|
|
|
+ tx_init_rfid();
|
|
|
break;
|
|
break;
|
|
|
case MagTxCC1101_434:
|
|
case MagTxCC1101_434:
|
|
|
tx_init_rf(434000000);
|
|
tx_init_rf(434000000);
|
|
@@ -213,12 +186,27 @@ bool tx_deinit(MagSetting* setting) {
|
|
|
case MagTxStateRFID:
|
|
case MagTxStateRFID:
|
|
|
tx_deinit_rfid();
|
|
tx_deinit_rfid();
|
|
|
break;
|
|
break;
|
|
|
- case MagTxStateGPIOA6A7:
|
|
|
|
|
- tx_deinit_gpio();
|
|
|
|
|
|
|
+ case MagTxStateGPIO:
|
|
|
|
|
+ furi_hal_gpio_write(GPIO_PIN_A, 0);
|
|
|
|
|
+ furi_hal_gpio_write(GPIO_PIN_B, 0);
|
|
|
|
|
+ furi_hal_gpio_write(GPIO_PIN_ENABLE, 0);
|
|
|
|
|
+
|
|
|
|
|
+ // set back to analog output mode?
|
|
|
|
|
+ //gpio_item_configure_all_pins(GpioModeAnalog);
|
|
|
|
|
+ furi_hal_power_disable_otg();
|
|
|
|
|
+ break;
|
|
|
|
|
+ case MagTxStatePiezo:
|
|
|
|
|
+ tx_deinit_piezo();
|
|
|
|
|
+ break;
|
|
|
|
|
+ case MagTxStateLF_P:
|
|
|
|
|
+ tx_deinit_piezo();
|
|
|
|
|
+ tx_deinit_rfid();
|
|
|
break;
|
|
break;
|
|
|
case MagTxCC1101_434:
|
|
case MagTxCC1101_434:
|
|
|
case MagTxCC1101_868:
|
|
case MagTxCC1101_868:
|
|
|
- tx_deinit_rf();
|
|
|
|
|
|
|
+ furi_hal_gpio_write(&gpio_cc1101_g0, false);
|
|
|
|
|
+ furi_hal_subghz_reset();
|
|
|
|
|
+ furi_hal_subghz_idle();
|
|
|
break;
|
|
break;
|
|
|
default:
|
|
default:
|
|
|
return false;
|
|
return false;
|
|
@@ -227,250 +215,104 @@ bool tx_deinit(MagSetting* setting) {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// due for deprecation
|
|
|
|
|
-void track_to_bits(uint8_t* bit_array, const char* track_data, uint8_t track_index) {
|
|
|
|
|
- // convert individual track to bits
|
|
|
|
|
-
|
|
|
|
|
- int tmp, crc, lrc = 0;
|
|
|
|
|
- int i = 0;
|
|
|
|
|
-
|
|
|
|
|
- // convert track data to bits
|
|
|
|
|
- for(uint8_t j = 0; track_data[j] != '\0'; j++) {
|
|
|
|
|
- crc = 1;
|
|
|
|
|
- tmp = track_data[j] - sublen[track_index];
|
|
|
|
|
-
|
|
|
|
|
- for(uint8_t k = 0; k < bitlen[track_index] - 1; k++) {
|
|
|
|
|
- crc ^= tmp & 1;
|
|
|
|
|
- lrc ^= (tmp & 1) << k;
|
|
|
|
|
- bit_array[i] = tmp & 1;
|
|
|
|
|
- i++;
|
|
|
|
|
- tmp >>= 1;
|
|
|
|
|
- }
|
|
|
|
|
- bit_array[i] = crc;
|
|
|
|
|
- i++;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- FURI_LOG_D(TAG, "LRC");
|
|
|
|
|
- // finish calculating final "byte" (LRC)
|
|
|
|
|
- tmp = lrc;
|
|
|
|
|
- crc = 1;
|
|
|
|
|
- for(uint8_t j = 0; j < bitlen[track_index] - 1; j++) {
|
|
|
|
|
- crc ^= tmp & 1;
|
|
|
|
|
- bit_array[i] = tmp & 1;
|
|
|
|
|
- i++;
|
|
|
|
|
- tmp >>= 1;
|
|
|
|
|
- }
|
|
|
|
|
- bit_array[i] = crc;
|
|
|
|
|
- i++;
|
|
|
|
|
-
|
|
|
|
|
- // My makeshift end sentinel. All other values 0/1
|
|
|
|
|
- bit_array[i] = 2;
|
|
|
|
|
- i++;
|
|
|
|
|
-
|
|
|
|
|
- // Log the output (messy but works)
|
|
|
|
|
- //char output[500] = {0x0};
|
|
|
|
|
- /*FuriString* tmp_str;
|
|
|
|
|
- tmp_str = furi_string_alloc();
|
|
|
|
|
- for(uint8_t j = 0; bit_array[j] != 2; j++) {
|
|
|
|
|
- furi_string_cat_printf(tmp_str, "%d", (bit_array[j] & 1));
|
|
|
|
|
- //strcat(output, furi_string_get_cstr(tmp_str));
|
|
|
|
|
- }
|
|
|
|
|
- FURI_LOG_D(TAG, "Track %d: %s", (track_index + 1), track_data);
|
|
|
|
|
- FURI_LOG_D(TAG, "Track %d: %s", (track_index + 1), furi_string_get_cstr(tmp_str));*/
|
|
|
|
|
- //furi_string_free(tmp_str);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void mag_spoof_bitwise(Mag* mag) {
|
|
|
|
|
|
|
+void mag_spoof(Mag* mag) {
|
|
|
MagSetting* setting = mag->setting;
|
|
MagSetting* setting = mag->setting;
|
|
|
|
|
|
|
|
|
|
+ // TODO: cleanup this section. Possibly move precompute + tx_init to emulate_on_enter?
|
|
|
FuriString* ft1 = mag->mag_dev->dev_data.track[0].str;
|
|
FuriString* ft1 = mag->mag_dev->dev_data.track[0].str;
|
|
|
FuriString* ft2 = mag->mag_dev->dev_data.track[1].str;
|
|
FuriString* ft2 = mag->mag_dev->dev_data.track[1].str;
|
|
|
|
|
+ FuriString* ft3 = mag->mag_dev->dev_data.track[2].str;
|
|
|
|
|
|
|
|
- char* data1;
|
|
|
|
|
- char* data2;
|
|
|
|
|
|
|
+ char *data1, *data2, *data3;
|
|
|
data1 = malloc(furi_string_size(ft1) + 1);
|
|
data1 = malloc(furi_string_size(ft1) + 1);
|
|
|
data2 = malloc(furi_string_size(ft2) + 1);
|
|
data2 = malloc(furi_string_size(ft2) + 1);
|
|
|
|
|
+ data3 = malloc(furi_string_size(ft3) + 1);
|
|
|
strncpy(data1, furi_string_get_cstr(ft1), furi_string_size(ft1));
|
|
strncpy(data1, furi_string_get_cstr(ft1), furi_string_size(ft1));
|
|
|
strncpy(data2, furi_string_get_cstr(ft2), furi_string_size(ft2));
|
|
strncpy(data2, furi_string_get_cstr(ft2), furi_string_size(ft2));
|
|
|
|
|
+ strncpy(data3, furi_string_get_cstr(ft3), furi_string_size(ft3));
|
|
|
|
|
|
|
|
if(furi_log_get_level() >= FuriLogLevelDebug) {
|
|
if(furi_log_get_level() >= FuriLogLevelDebug) {
|
|
|
- debug_msr_string(data1, BITS_TRACK1, OFFSET_TRACK1);
|
|
|
|
|
- debug_msr_string(data2, BITS_TRACK2, OFFSET_TRACK2);
|
|
|
|
|
|
|
+ debug_mag_string(data1, bitlen[0], sublen[0]);
|
|
|
|
|
+ debug_mag_string(data2, bitlen[1], sublen[1]);
|
|
|
|
|
+ debug_mag_string(data3, bitlen[2], sublen[2]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
uint8_t bits_t1_raw[64] = {0x00}; // 68 chars max track 1 + 1 char crc * 7 approx =~ 483 bits
|
|
uint8_t bits_t1_raw[64] = {0x00}; // 68 chars max track 1 + 1 char crc * 7 approx =~ 483 bits
|
|
|
uint8_t bits_t1_manchester[128] = {0x00}; // twice the above
|
|
uint8_t bits_t1_manchester[128] = {0x00}; // twice the above
|
|
|
- uint16_t bits_t1_count = msr_encode(
|
|
|
|
|
- data1, (uint8_t*)bits_t1_manchester, (uint8_t*)bits_t1_raw, BITS_TRACK1, OFFSET_TRACK1);
|
|
|
|
|
|
|
+ uint16_t bits_t1_count = mag_encode(
|
|
|
|
|
+ data1, (uint8_t*)bits_t1_manchester, (uint8_t*)bits_t1_raw, bitlen[0], sublen[0]);
|
|
|
uint8_t bits_t2_raw[64] = {0x00}; // 68 chars max track 1 + 1 char crc * 7 approx =~ 483 bits
|
|
uint8_t bits_t2_raw[64] = {0x00}; // 68 chars max track 1 + 1 char crc * 7 approx =~ 483 bits
|
|
|
uint8_t bits_t2_manchester[128] = {0x00}; // twice the above
|
|
uint8_t bits_t2_manchester[128] = {0x00}; // twice the above
|
|
|
- uint16_t bits_t2_count = msr_encode(
|
|
|
|
|
- data2, (uint8_t*)bits_t2_manchester, (uint8_t*)bits_t2_raw, BITS_TRACK2, OFFSET_TRACK2);
|
|
|
|
|
|
|
+ uint16_t bits_t2_count = mag_encode(
|
|
|
|
|
+ data2, (uint8_t*)bits_t2_manchester, (uint8_t*)bits_t2_raw, bitlen[1], sublen[1]);
|
|
|
|
|
+ uint8_t bits_t3_raw[64] = {0x00};
|
|
|
|
|
+ uint8_t bits_t3_manchester[128] = {0x00};
|
|
|
|
|
+ uint16_t bits_t3_count = mag_encode(
|
|
|
|
|
+ data3, (uint8_t*)bits_t3_manchester, (uint8_t*)bits_t3_raw, bitlen[2], sublen[2]);
|
|
|
|
|
|
|
|
if(furi_log_get_level() >= FuriLogLevelDebug) {
|
|
if(furi_log_get_level() >= FuriLogLevelDebug) {
|
|
|
printf("Manchester bitcount: T1: %d, T2: %d\r\n", bits_t1_count, bits_t2_count);
|
|
printf("Manchester bitcount: T1: %d, T2: %d\r\n", bits_t1_count, bits_t2_count);
|
|
|
-
|
|
|
|
|
printf("T1 raw: ");
|
|
printf("T1 raw: ");
|
|
|
for(int i = 0; i < bits_t1_count / 16; i++) printf("%02x ", bits_t1_raw[i]);
|
|
for(int i = 0; i < bits_t1_count / 16; i++) printf("%02x ", bits_t1_raw[i]);
|
|
|
- printf("\r\n");
|
|
|
|
|
-
|
|
|
|
|
- printf("T1 manchester: ");
|
|
|
|
|
|
|
+ printf("\r\nT1 manchester: ");
|
|
|
for(int i = 0; i < bits_t1_count / 8; i++) printf("%02x ", bits_t1_manchester[i]);
|
|
for(int i = 0; i < bits_t1_count / 8; i++) printf("%02x ", bits_t1_manchester[i]);
|
|
|
- printf("\r\n");
|
|
|
|
|
-
|
|
|
|
|
- printf("T2 raw: ");
|
|
|
|
|
|
|
+ printf("\r\nT2 raw: ");
|
|
|
for(int i = 0; i < bits_t2_count / 16; i++) printf("%02x ", bits_t2_raw[i]);
|
|
for(int i = 0; i < bits_t2_count / 16; i++) printf("%02x ", bits_t2_raw[i]);
|
|
|
- printf("\r\n");
|
|
|
|
|
-
|
|
|
|
|
- printf("T2 manchester: ");
|
|
|
|
|
|
|
+ printf("\r\nT2 manchester: ");
|
|
|
for(int i = 0; i < bits_t2_count / 8; i++) printf("%02x ", bits_t2_manchester[i]);
|
|
for(int i = 0; i < bits_t2_count / 8; i++) printf("%02x ", bits_t2_manchester[i]);
|
|
|
- printf("\r\n");
|
|
|
|
|
-
|
|
|
|
|
- printf("Bitwise emulation done\r\n\r\n");
|
|
|
|
|
|
|
+ printf("\r\nT3 raw: ");
|
|
|
|
|
+ for(int i = 0; i < bits_t3_count / 16; i++) printf("%02x ", bits_t3_raw[i]);
|
|
|
|
|
+ printf("\r\nT3 manchester: ");
|
|
|
|
|
+ for(int i = 0; i < bits_t3_count / 8; i++) printf("%02x ", bits_t3_manchester[i]);
|
|
|
|
|
+ printf("\r\nBitwise emulation done\r\n\r\n");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if(!tx_init(setting)) return;
|
|
|
|
|
last_value = 2;
|
|
last_value = 2;
|
|
|
- FURI_CRITICAL_ENTER();
|
|
|
|
|
bool bit = false;
|
|
bool bit = false;
|
|
|
|
|
|
|
|
- if((setting->track == MagTrackStateAll))
|
|
|
|
|
- for(uint16_t i = 0; i < ZERO_PREFIX; i++) {
|
|
|
|
|
- bit ^= 0xFF;
|
|
|
|
|
- bitbang_raw(bit, setting);
|
|
|
|
|
- furi_delay_us(setting->us_clock * 2);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if(!tx_init(setting)) return;
|
|
|
|
|
|
|
|
- if((setting->track == MagTrackStateAll) || (setting->track == MagTrackStateOne))
|
|
|
|
|
- for(uint16_t i = 0; i < bits_t1_count; i++) {
|
|
|
|
|
- uint8_t byte = i / 8;
|
|
|
|
|
- uint8_t bitmask = 1 << (7 - (i % 8));
|
|
|
|
|
- /* this comment is mostly for zw's convenience:
|
|
|
|
|
- *
|
|
|
|
|
- * bits are stored in their arrays like on a card (LSB first). This is not how usually bits are stored in a
|
|
|
|
|
- * byte, with the MSB first. the var bitmask creates the pattern to iterate through each bit, LSB first, like so
|
|
|
|
|
- * 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x80... masking bits one by one from the current byte
|
|
|
|
|
- *
|
|
|
|
|
- * i've chosen this LSB approach since bits and bytes are hard enough to visualize with the 5/8 and 7/8 encoding
|
|
|
|
|
- * MSR uses. It's a biiit more complicated to process, but visualizing it with printf or a debugger is
|
|
|
|
|
- * infinitely easier
|
|
|
|
|
- *
|
|
|
|
|
- * Encoding the following pairs of 5 bits as 5/8: A1234 B1234 C1234 D1234
|
|
|
|
|
- * using this LSB format looks like: A1234B12 34C1234D 12340000
|
|
|
|
|
- * using the MSB format, looks like: 21B4321A D4321C43 00004321
|
|
|
|
|
- * this means reading each byte backwards when printing/debugging, and the jumping 16 bits ahead, reading 8 more
|
|
|
|
|
- * bits backward, jumping 16 more bits ahead.
|
|
|
|
|
- *
|
|
|
|
|
- * I find this much more convenient for debugging, with the tiny incovenience of reading the bits in reverse
|
|
|
|
|
- * order. THus, the reason for the bitmask above
|
|
|
|
|
- */
|
|
|
|
|
-
|
|
|
|
|
- bit = !!(bits_t1_manchester[byte] & bitmask);
|
|
|
|
|
-
|
|
|
|
|
- // TODO: reimplement timing delays. Replace fixed furi_hal_cortex_delay_us to wait instead to a specific value
|
|
|
|
|
- // for DWT->CYCCNT. Note timer is aliased to 64us as per
|
|
|
|
|
- // #define FURI_HAL_CORTEX_INSTRUCTIONS_PER_MICROSECOND (SystemCoreClock / 1000000) | furi_hal_cortex.c
|
|
|
|
|
-
|
|
|
|
|
- bitbang_raw(bit, setting);
|
|
|
|
|
- furi_delay_us(setting->us_clock);
|
|
|
|
|
- // if (i % 2 == 1) furi_delay_us(setting->us_interpacket);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ FURI_CRITICAL_ENTER();
|
|
|
|
|
+ for(uint16_t i = 0; i < (ZERO_PREFIX * 2); i++) {
|
|
|
|
|
+ // is this right?
|
|
|
|
|
+ bit ^= 0xFF;
|
|
|
|
|
+ play_halfbit(bit, setting);
|
|
|
|
|
+ furi_delay_us(setting->us_clock);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if((setting->track == MagTrackStateAll))
|
|
|
|
|
- for(uint16_t i = 0; i < ZERO_BETWEEN; i++) {
|
|
|
|
|
- bit ^= 0xFF;
|
|
|
|
|
- bitbang_raw(bit, setting);
|
|
|
|
|
- furi_delay_us(setting->us_clock * 2);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if((setting->track == MagTrackStateOneAndTwo) || (setting->track == MagTrackStateOne))
|
|
|
|
|
+ play_track((uint8_t*)bits_t1_manchester, bits_t1_count, setting, false);
|
|
|
|
|
|
|
|
- if((setting->track == MagTrackStateAll) || (setting->track == MagTrackStateTwo))
|
|
|
|
|
- for(uint16_t i = 0; i < bits_t2_count; i++) {
|
|
|
|
|
- uint16_t j = bits_t2_count - i - 1;
|
|
|
|
|
- uint8_t byte = j / 8;
|
|
|
|
|
- uint8_t bitmask = 1 << (7 - (j % 8));
|
|
|
|
|
- bool bit = !!(bits_t2_manchester[byte] & bitmask);
|
|
|
|
|
- bitbang_raw(bit, setting);
|
|
|
|
|
|
|
+ if((setting->track == MagTrackStateOneAndTwo))
|
|
|
|
|
+ for(uint16_t i = 0; i < (ZERO_BETWEEN * 2); i++) {
|
|
|
|
|
+ bit ^= 0xFF;
|
|
|
|
|
+ play_halfbit(bit, setting);
|
|
|
furi_delay_us(setting->us_clock);
|
|
furi_delay_us(setting->us_clock);
|
|
|
- // if (i % 2 == 1) furi_delay_us(setting->us_interpacket);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if((setting->track == MagTrackStateAll))
|
|
|
|
|
- for(uint16_t i = 0; i < ZERO_SUFFIX; i++) {
|
|
|
|
|
- bit ^= 0xFF;
|
|
|
|
|
- bitbang_raw(bit, setting);
|
|
|
|
|
- furi_delay_us(setting->us_clock * 2);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if((setting->track == MagTrackStateOneAndTwo) || (setting->track == MagTrackStateTwo))
|
|
|
|
|
+ play_track(
|
|
|
|
|
+ (uint8_t*)bits_t2_manchester,
|
|
|
|
|
+ bits_t2_count,
|
|
|
|
|
+ setting,
|
|
|
|
|
+ (setting->reverse == MagReverseStateOn));
|
|
|
|
|
|
|
|
|
|
+ if((setting->track == MagTrackStateThree))
|
|
|
|
|
+ play_track((uint8_t*)bits_t3_manchester, bits_t3_count, setting, false);
|
|
|
|
|
+
|
|
|
|
|
+ for(uint16_t i = 0; i < (ZERO_SUFFIX * 2); i++) {
|
|
|
|
|
+ bit ^= 0xFF;
|
|
|
|
|
+ play_halfbit(bit, setting);
|
|
|
|
|
+ furi_delay_us(setting->us_clock);
|
|
|
|
|
+ }
|
|
|
FURI_CRITICAL_EXIT();
|
|
FURI_CRITICAL_EXIT();
|
|
|
|
|
+
|
|
|
free(data1);
|
|
free(data1);
|
|
|
free(data2);
|
|
free(data2);
|
|
|
|
|
+ free(data3);
|
|
|
tx_deinit(setting);
|
|
tx_deinit(setting);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// due for deprecation
|
|
|
|
|
-void mag_spoof(Mag* mag) {
|
|
|
|
|
- MagSetting* setting = mag->setting;
|
|
|
|
|
-
|
|
|
|
|
- // precompute tracks (WIP; ignores reverse and 3rd track)
|
|
|
|
|
- // likely will be reworked to antirez's bitmap method anyway...
|
|
|
|
|
- const char* data1 = furi_string_get_cstr(mag->mag_dev->dev_data.track[0].str);
|
|
|
|
|
- const char* data2 = furi_string_get_cstr(mag->mag_dev->dev_data.track[1].str);
|
|
|
|
|
- uint8_t bit_array1[2 * (strlen(data1) * bitlen[0]) + 1];
|
|
|
|
|
- uint8_t bit_array2[2 * (strlen(data2) * bitlen[1]) + 1];
|
|
|
|
|
- track_to_bits(bit_array1, data1, 0);
|
|
|
|
|
- track_to_bits(bit_array2, data2, 1);
|
|
|
|
|
-
|
|
|
|
|
- bool spoofed = false;
|
|
|
|
|
- do {
|
|
|
|
|
- // Initialize configured TX method
|
|
|
|
|
- if(!tx_init(setting)) break;
|
|
|
|
|
-
|
|
|
|
|
- // Critical timing section (need to eliminate ifs? does this impact timing?)
|
|
|
|
|
- FURI_CRITICAL_ENTER();
|
|
|
|
|
- // Prefix of zeros
|
|
|
|
|
- for(uint16_t i = 0; i < ZERO_PREFIX; i++) {
|
|
|
|
|
- if(!play_bit(0, setting)) break;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Track 1
|
|
|
|
|
- if((setting->track == MagTrackStateAll) || (setting->track == MagTrackStateOne)) {
|
|
|
|
|
- for(uint16_t i = 0; bit_array1[i] != 2; i++) {
|
|
|
|
|
- if(!play_bit((bit_array1[i] & 1), setting)) break;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Zeros between tracks
|
|
|
|
|
- if(setting->track == MagTrackStateAll) {
|
|
|
|
|
- for(uint16_t i = 0; i < ZERO_BETWEEN; i++) {
|
|
|
|
|
- if(!play_bit(0, setting)) break;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Track 2 (TODO: Reverse track)
|
|
|
|
|
- if((setting->track == MagTrackStateAll) || (setting->track == MagTrackStateTwo)) {
|
|
|
|
|
- for(uint16_t i = 0; bit_array2[i] != 2; i++) {
|
|
|
|
|
- if(!play_bit((bit_array2[i] & 1), setting)) break;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Suffix of zeros
|
|
|
|
|
- for(uint16_t i = 0; i < ZERO_SUFFIX; i++) {
|
|
|
|
|
- if(!play_bit(0, setting)) break;
|
|
|
|
|
- }
|
|
|
|
|
- FURI_CRITICAL_EXIT();
|
|
|
|
|
-
|
|
|
|
|
- // Reset configured TX method
|
|
|
|
|
- if(!tx_deinit(setting)) break;
|
|
|
|
|
- spoofed = true;
|
|
|
|
|
- } while(0);
|
|
|
|
|
-
|
|
|
|
|
- UNUSED(spoofed);
|
|
|
|
|
- /*if(!spoofed) {
|
|
|
|
|
- // error handling?
|
|
|
|
|
- // cleanup?
|
|
|
|
|
- }*/
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
uint16_t add_bit(bool value, uint8_t* out, uint16_t count) {
|
|
uint16_t add_bit(bool value, uint8_t* out, uint16_t count) {
|
|
|
uint8_t bit = count % 8;
|
|
uint8_t bit = count % 8;
|
|
|
uint8_t byte = count / 8;
|
|
uint8_t byte = count / 8;
|
|
@@ -490,7 +332,7 @@ uint16_t add_bit_manchester(bool value, uint8_t* out, uint16_t count) {
|
|
|
return count;
|
|
return count;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-uint16_t msr_encode(
|
|
|
|
|
|
|
+uint16_t mag_encode(
|
|
|
char* data,
|
|
char* data,
|
|
|
uint8_t* out_manchester,
|
|
uint8_t* out_manchester,
|
|
|
uint8_t* out_raw,
|
|
uint8_t* out_raw,
|
|
@@ -508,10 +350,11 @@ uint16_t msr_encode(
|
|
|
uint16_t output_count = 0;
|
|
uint16_t output_count = 0;
|
|
|
int tmp, crc, lrc = 0;
|
|
int tmp, crc, lrc = 0;
|
|
|
|
|
|
|
|
- for(int i = 0; i < PREFIX_NUM_ZEROES; i++) {
|
|
|
|
|
|
|
+ /* // why are we adding zeros to the encoded string if we're also doing it while playing?
|
|
|
|
|
+ for(int i = 0; i < ZERO_PREFIX; i++) {
|
|
|
output_count = add_bit_manchester(0, out_manchester, output_count);
|
|
output_count = add_bit_manchester(0, out_manchester, output_count);
|
|
|
raw_bits_count = add_bit(0, out_raw, raw_bits_count);
|
|
raw_bits_count = add_bit(0, out_raw, raw_bits_count);
|
|
|
- }
|
|
|
|
|
|
|
+ }*/
|
|
|
|
|
|
|
|
for(int i = 0; *(data + i) != 0; i++) {
|
|
for(int i = 0; *(data + i) != 0; i++) {
|
|
|
crc = 1;
|
|
crc = 1;
|
|
@@ -543,13 +386,13 @@ uint16_t msr_encode(
|
|
|
return output_count;
|
|
return output_count;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void debug_msr_string(char* data, uint8_t track_bits, uint8_t track_ascii_offset) {
|
|
|
|
|
|
|
+void debug_mag_string(char* data, uint8_t track_bits, uint8_t track_ascii_offset) {
|
|
|
uint8_t bits_raw[64] = {0}; // 68 chars max track 1 + 1 char crc * 7 approx =~ 483 bits
|
|
uint8_t bits_raw[64] = {0}; // 68 chars max track 1 + 1 char crc * 7 approx =~ 483 bits
|
|
|
uint8_t bits_manchester[128] = {0}; // twice the above
|
|
uint8_t bits_manchester[128] = {0}; // twice the above
|
|
|
int numbits = 0;
|
|
int numbits = 0;
|
|
|
|
|
|
|
|
printf("Encoding [%s] with %d bits\r\n", data, track_bits);
|
|
printf("Encoding [%s] with %d bits\r\n", data, track_bits);
|
|
|
- numbits = msr_encode(
|
|
|
|
|
|
|
+ numbits = mag_encode(
|
|
|
data, (uint8_t*)bits_manchester, (uint8_t*)bits_raw, track_bits, track_ascii_offset);
|
|
data, (uint8_t*)bits_manchester, (uint8_t*)bits_raw, track_bits, track_ascii_offset);
|
|
|
printf("Got %d bits\r\n", numbits);
|
|
printf("Got %d bits\r\n", numbits);
|
|
|
printf("Raw byte stream: ");
|
|
printf("Raw byte stream: ");
|
|
@@ -563,13 +406,13 @@ void debug_msr_string(char* data, uint8_t track_bits, uint8_t track_ascii_offset
|
|
|
printf("Bits ");
|
|
printf("Bits ");
|
|
|
int space_counter = 0;
|
|
int space_counter = 0;
|
|
|
for(int i = 0; i < numbits / 2; i++) {
|
|
for(int i = 0; i < numbits / 2; i++) {
|
|
|
- if(i < PREFIX_NUM_ZEROES) {
|
|
|
|
|
|
|
+ /*if(i < ZERO_PREFIX) {
|
|
|
printf("X");
|
|
printf("X");
|
|
|
continue;
|
|
continue;
|
|
|
- } else if(i == PREFIX_NUM_ZEROES) {
|
|
|
|
|
|
|
+ } else if(i == ZERO_PREFIX) {
|
|
|
printf(" ");
|
|
printf(" ");
|
|
|
space_counter = 0;
|
|
space_counter = 0;
|
|
|
- }
|
|
|
|
|
|
|
+ }*/
|
|
|
printf("%01x", (bits_raw[i / 8] & (1 << (7 - (i % 8)))) != 0);
|
|
printf("%01x", (bits_raw[i / 8] & (1 << (7 - (i % 8)))) != 0);
|
|
|
if((space_counter) % track_bits == track_bits - 1) printf(" ");
|
|
if((space_counter) % track_bits == track_bits - 1) printf(" ");
|
|
|
space_counter++;
|
|
space_counter++;
|
|
@@ -583,4 +426,4 @@ void debug_msr_string(char* data, uint8_t track_bits, uint8_t track_ascii_offset
|
|
|
if(i % 4 == 3) printf(" ");
|
|
if(i % 4 == 3) printf(" ");
|
|
|
}
|
|
}
|
|
|
printf("\r\n\r\n");
|
|
printf("\r\n\r\n");
|
|
|
-}
|
|
|
|
|
|
|
+}
|