lf cotag sim: COTAG simulation improvements

- Implemented passive mode for COTAG sim:
- load modulation with an fc/2 subcarrier locked to the reader carrier.
    - This mode is now a default as it has several advantages:
        - phase coherent response,
        - can detect reader field drop mid frame "transmission" and restart
          immediately -- important with some readers that may drop field
          early and send reactivation shortly afterwards expecting the
          tag to respond there as well.

- "active tx mode" is now via CLI toggle --active: it may give better range,
but may not work with all readers.

- Improved COTAG activation sequence detection for a wider range of COTAG
readers that may vary slightly in activation sequence timings (thresholds
for the rise-to-rise mode).
This commit is contained in:
towelbyte committed 2026-11-08 13:10:45 +01:00
1 parent 3810eee3e0
commit 93631d06d7
3 files changed
+153 -22

No files matched your search

+137 -14
View File
@@ -2992,16 +2992,18 @@ typedef struct {
static bool cotag_activation_sequence_detect(const lf_cotagsim_t *params, cotag_detect_val_t *out) {
// Reader's tag activation sequence:
// - ON 800 us, OFF 2200 us
// - ON 800 us, OFF 2200 us
// - ON ~5800 us (the tag is expected to respond in this window)
// - OFF ~16500 us (reader's pause if no tag detected, then repeat)
//
// We detect 2 rising edges between ON(800),OFF(2200),ON(5800), i.e. approx 3000us apart.
// Another reader activation sequence identified as: ON(1500),OFF(1900),ON(Z)
//
// We detect 2 rising edges delta between ON(X),OFF(Y),ON(Z).
// Plus minus some tolerance, thus win_lo and win_hi.
// Number of samples between 2 rising edges to look for an activation sequence
const uint32_t win_lo = (3000 - 300) * 12 / (LF_COTAG_DIVISOR + 1);
const uint32_t win_hi = (3000 + 300) * 12 / (LF_COTAG_DIVISOR + 1);
// Number of samples between 2 rising edges to look for an activation sequence:
const uint32_t win_lo = 2700 * 12 / (LF_COTAG_DIVISOR + 1);
const uint32_t win_hi = 3700 * 12 / (LF_COTAG_DIVISOR + 1);
// Samples ring buffer
uint8_t ring[2 * COTAG_STEP_K] = {0};
@@ -3076,9 +3078,21 @@ static bool cotag_activation_sequence_detect(const lf_cotagsim_t *params, cotag_
}
}
#define COTAG_TX_HALFBIT_CYCLES (384 / 2) // Number of samples per half bit
#define COTAG_TX_SYNC_HALFBITS 4 // two leading sync 0 bits
#define COTAG_TX_MAX_HALFBITS (COTAG_TX_SYNC_HALFBITS + 2 * 8 * LF_COTAG_SIM_DATA_LEN)
#define COTAG_TX_HALFBIT_CARRIERS 384 // reader carrier cycles per half bit
#define COTAG_TX_HALFBIT_SAMPLES (COTAG_TX_HALFBIT_CARRIERS / 2) // samples per half bit at the 66 kHz TX divisor
#define COTAG_TX_SYNC_HALFBITS 4 // two leading sync 0 bits
#define COTAG_TX_MAX_HALFBITS (COTAG_TX_SYNC_HALFBITS + 2 * 8 * LF_COTAG_SIM_DATA_LEN)
#define COTAG_CARRIER_TIMEOUT 17 // precision counter ticks (1.5 MHz) for approx 1.5 carrier periods at 132 kHz
#define COTAG_FIELD_LOST 64 // consecutive missed carrier edges threshold for determining the reader field is gone
// a passive answer, for debug output
typedef struct {
uint32_t missed_edges; // carrier edges missed, replaced by the timeout
uint16_t nhb_sent; // num half bits sent
bool field_lost; // stopped because the reader field went away
} cotag_tx_val_t;
#define COTAG_SSC_STALL 100000
#define COTAG_SETTLE_US 10000
@@ -3117,8 +3131,10 @@ static uint16_t cotag_build_halfbits(uint8_t *half_bits, const uint8_t *data, ui
return nhb;
}
// Send COTAG frame's half bits. Return false on button press, USB data, or error.
static bool cotag_tx_frame(const uint8_t *half_bits, uint16_t nhb, uint8_t repeats, uint16_t hb_delay_us) {
// Send COTAG frame's half bits with "active tx".
// May give better range but may not work with all types of readers.
// Return false on button press, USB data, or error.
static bool cotag_tx_frame_active(const uint8_t *half_bits, uint16_t nhb, uint8_t repeats, uint16_t hb_delay_us) {
Gpio_SSC_DOUT_High();
FpgaSendCommand(FPGA_CMD_SET_DIVISOR, LF_COTAG_TX_DIVISOR);
@@ -3136,7 +3152,7 @@ static bool cotag_tx_frame(const uint8_t *half_bits, uint16_t nhb, uint8_t repea
if (hb_delay_us) {
SpinDelayUsPrecision(hb_delay_us);
} else if (cotag_skip_samples(COTAG_TX_HALFBIT_CYCLES) == false) {
} else if (cotag_skip_samples(COTAG_TX_HALFBIT_SAMPLES) == false) {
ok = false;
break;
}
@@ -3160,6 +3176,89 @@ static bool cotag_tx_frame(const uint8_t *half_bits, uint16_t nhb, uint8_t repea
return ok;
}
// Wait for the next rising edge of cross_lo, one reader carrier cycle after t_last_edge.
// Return false on timeout: reader field gone or the edge was missed (e.g. hidden by our own load).
static bool cotag_wait_carrier(uint16_t t_last_edge) {
while (Gpio_SSC_CLK_Read()) {
if (GetPrecisionCounterDelta(t_last_edge) > COTAG_CARRIER_TIMEOUT) {
return false;
}
}
while (Gpio_SSC_CLK_Read() == false) {
if (GetPrecisionCounterDelta(t_last_edge) > COTAG_CARRIER_TIMEOUT) {
return false;
}
}
return true;
}
// Send COTAG frame's half bits, load modulating with an fc/2 subcarrier clocked by the
// reader's carrier (cross_lo): subcarrier on for a high half bit, off for a low one.
//
// If reader field is gone before all half bits are sent, true is returned and tx_val->field_lost is set.
// Return false on button press or USB data.
static bool cotag_tx_frame_passive(const uint8_t *half_bits, uint16_t nhb, uint8_t repeats, cotag_tx_val_t *tx_val) {
Gpio_SSC_DOUT_Low();
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT);
gpio_fpga_mod_feedback_setup();
bool ok = true;
uint8_t lost = 0;
uint16_t t_last_edge = GetPrecisionCounterRaw();
for (uint8_t r = 0; r < repeats && ok; r++) {
// Send precomputed half bits. Leading sync bits are sent only at first repetition
for (uint16_t i = (r == 0) ? 0 : COTAG_TX_SYNC_HALFBITS; i < nhb; i++) {
// Modulate the current half bit:
for (uint16_t c = 0; c < COTAG_TX_HALFBIT_CARRIERS; c++) {
// Determine the level before the edge, so that we write it right after it:
// - half bit high: toggle the load every carrier cycle (fc/2)
// - half bit low: unloaded for the whole half bit
bool loaded = half_bits[i] && (c & 1);
bool edge = cotag_wait_carrier(t_last_edge);
if (loaded) {
Gpio_SSC_DOUT_High();
} else {
Gpio_SSC_DOUT_Low();
}
t_last_edge = GetPrecisionCounterRaw();
if (edge) {
lost = 0;
} else {
tx_val->missed_edges++;
if (++lost >= COTAG_FIELD_LOST) {
tx_val->field_lost = true;
// reader field gone: back to listening for the next activation
goto out;
}
}
}
tx_val->nhb_sent++;
if (BUTTON_PRESS()) {
ok = false;
break;
}
}
WDT_HIT();
if (data_available()) {
ok = false;
}
}
out:
Gpio_SSC_DOUT_Low();
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC);
FpgaSetupSsc(FPGA_MAJOR_MODE_LF_READER);
gpio_fpga_mod_only_setup();
Gpio_SSC_DOUT_Low(); // back to "listening"
return ok;
}
void SimulateCotag(const lf_cotagsim_t *params, bool ledcontrol) {
LFSetupFPGAForADC(LF_COTAG_DIVISOR, false);
@@ -3184,14 +3283,22 @@ void SimulateCotag(const lf_cotagsim_t *params, bool ledcontrol) {
const uint32_t settle = COTAG_SETTLE_US * 12 / (LF_COTAG_DIVISOR + 1);
if (params->active == false) {
StartPrecisionCounter();
}
cotag_detect_val_t val;
while (cotag_activation_sequence_detect(params, &val)) {
cotag_tx_val_t tx_val = {0};
if (nbits) {
if (ledcontrol) LED_C_ON();
// Transmit frame
bool ok = cotag_tx_frame(half_bits, nhb, params->repeats, params->hb_delay_us);
bool ok = params->active
? cotag_tx_frame_active(half_bits, nhb, params->repeats, params->hb_delay_us)
: cotag_tx_frame_passive(half_bits, nhb, params->repeats, &tx_val);
if (ledcontrol) LED_C_OFF();
@@ -3207,11 +3314,27 @@ void SimulateCotag(const lf_cotagsim_t *params, bool ledcontrol) {
// Debug print only after done TXing frame
if (params->verbose || nbits == 0) {
Dbprintf("COTAG reader activation sequence detected (rise-to-rise " _YELLOW_("%u") " us, threshold %d)",
val.dt * (LF_COTAG_DIVISOR + 1) / 12, val.thr);
Dbprintf("COTAG reader activation sequence detected");
if (g_dbglevel >= DBG_DEBUG) {
Dbprintf(" rise-to-rise " _YELLOW_("%u") " us, threshold %d", val.dt * (LF_COTAG_DIVISOR + 1) / 12, val.thr);
}
if (params->active == false && nbits) {
uint16_t total = nhb + (params->repeats - 1) * (nhb - COTAG_TX_SYNC_HALFBITS);
if (tx_val.field_lost) {
Dbprintf(" Answer stopped after " _YELLOW_("%u") " of %u half bits, reader field gone", tx_val.nhb_sent, total);
} else {
Dbprintf(" Sent " _YELLOW_("%u") " of %u half bits", tx_val.nhb_sent, total);
if (g_dbglevel >= DBG_DEBUG) Dbprintf(" missed carrier edges %u", tx_val.missed_edges);
}
}
}
}
if (params->active == false) {
StopPrecisionCounter();
}
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
if (ledcontrol) LEDsoff();
BigBuf_free();
+15 -8
View File
@@ -552,18 +552,18 @@ static int CmdCOTAGSim(const char *Cmd) {
_CYAN_("Note: COTAG tag full repeating sequence is 128 bits, but readers seem to care about LSB 64 bits only."),
"lf cotag sim --> only detect the reader\n"
"lf cotag sim -r A37E0D37000C1337 --> send 64 bits\n"
"lf cotag sim -r 0000000000000002A37E0D37000C1337\n"
"lf cotag sim --floor 32 --> more sensitive, for a reader further away\n"
"lf cotag sim -r A37E0D37000C1337 --hb_delay_us 2909 --> time the half bit instead of counting cycles\n"
"lf cotag sim -r 0000000000000002A37E0D37000C1337 --> send 128 bits\n"
"lf cotag sim -r A37E0D37000C1337 --active --> send 64 bits, active tx mode\n"
);
void *argtable[] = {
arg_param_begin,
arg_str0("r", "raw", "<hex>", "raw data to answer with, " STR(LF_COTAG_SIM_MIN_BYTES) " to " STR(LF_COTAG_SIM_DATA_LEN) " bytes (64-128 bits)"),
arg_int0("n", "repeat", "<1-" STR(LF_COTAG_SIM_MAX_REPEATS) ">", "repeat data frame this many times per answer (def 1)"),
arg_int0(NULL, "floor", "<16-65535>", "minimum rise threshold (def " STR(LF_COTAG_SIM_FLOOR) ")"),
arg_int0(NULL, "hb_delay_us", "<2900-2918>", "custom half bit delay [us] (should be around 2909)"),
arg_lit0("v", "verbose", "report detected reader activation sequence while answering"),
arg_lit0(NULL, "active", "simulate tag with active tx instead (may give better range but may not work with all readers)"),
arg_int0(NULL, "hb_delay_us", "<2900-2918>", "custom half bit delay [us] for active mode (should be around 2909)"),
arg_int0(NULL, "floor", "<16-65535>", "minimum rise threshold for activation detection (def " STR(LF_COTAG_SIM_FLOOR) ")"),
arg_lit0("v", "verbose", "report detected reader activation while answering"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
@@ -572,9 +572,10 @@ static int CmdCOTAGSim(const char *Cmd) {
int dlen = 0;
CLIGetHexWithReturn(ctx, 1, data, &dlen);
int repeats = arg_get_int_def(ctx, 2, 1);
int thr_floor = arg_get_int_def(ctx, 3, LF_COTAG_SIM_FLOOR);
bool active = arg_get_lit(ctx, 3);
int hb_delay = arg_get_int_def(ctx, 4, 0);
bool verbose = arg_get_lit(ctx, 5);
int thr_floor = arg_get_int_def(ctx, 5, LF_COTAG_SIM_FLOOR);
bool verbose = arg_get_lit(ctx, 6);
CLIParserFree(ctx);
if (dlen && (dlen < LF_COTAG_SIM_MIN_BYTES || dlen > LF_COTAG_SIM_DATA_LEN)) {
@@ -598,12 +599,18 @@ static int CmdCOTAGSim(const char *Cmd) {
return PM3_EINVARG;
}
if (hb_delay && active == false) {
PrintAndLogEx(ERR, "hb_delay_us can only be used with --active");
return PM3_EINVARG;
}
lf_cotagsim_t payload = {
.thr_floor = (uint16_t)thr_floor,
.hb_delay_us = (uint16_t)hb_delay,
.nbits = (uint8_t)(dlen * 8),
.repeats = (uint8_t)repeats,
.verbose = verbose,
.active = active,
};
memcpy(payload.data, data, dlen);
+1
View File
@@ -390,6 +390,7 @@ typedef struct {
uint16_t hb_delay_us; // optional custom half bit delay [us]
uint8_t repeats; // repeat data frame this many times per answer
bool verbose;
bool active; // transmit 66 kHz instead of load modulating at reader carrier / 2
uint8_t nbits; // number of data bits
uint8_t data[LF_COTAG_SIM_DATA_LEN];
} PACKED lf_cotagsim_t;