Complete and working reimplementation of COTAG support
- "lf cotag reader": now behaves mostly the same as "lf read",
except it is dedicated for COTAG.
- It makes sure to send COTAG start sequence and to use
real time sampling mode to get enough samples.
- "lf cotag demod": Working client-side COTAG demodulation implementation
(high-low raw demod + Manchester demodulation from high/low demod).
- Now uses samples directly from g_GraphBuffer
- Supports 3 types of COTAG preamble (1 active, 2 passive), which
can start anywhere in the samples.
- Prints full 128-bits of COTAG tag data in 3 formats: raw,
groupped by nibbles, and in hex, and identifies and
prints out the card number.
Signed-off-by: andrej@towelbyte.net
This commit is contained in:
1 parent
f6d5a1fc70
commit
c1cefd6e38
10 files changed
+576
-20
No files matched your search
+9
-4
@@ -748,12 +748,13 @@ int CmdLFConfig(const char *Cmd) {
|
||||
return lf_setconfig(&config);
|
||||
}
|
||||
|
||||
static int lf_read_internal(bool realtime, bool verbose, uint64_t samples) {
|
||||
static int lf_read_internal(bool realtime, bool verbose, uint64_t samples, bool cotag) {
|
||||
if (!g_session.pm3_present) return PM3_ENOTTY;
|
||||
|
||||
lf_sample_payload_t payload = {0};
|
||||
payload.realtime = realtime;
|
||||
payload.verbose = verbose;
|
||||
payload.cotag = cotag;
|
||||
|
||||
sample_config current_config;
|
||||
int retval = lf_getconfig(¤t_config);
|
||||
@@ -824,7 +825,11 @@ static int lf_read_internal(bool realtime, bool verbose, uint64_t samples) {
|
||||
}
|
||||
|
||||
int lf_read(bool verbose, uint64_t samples) {
|
||||
return lf_read_internal(false, verbose, samples);
|
||||
return lf_read_internal(false, verbose, samples, false);
|
||||
}
|
||||
|
||||
int lf_read_cotag(bool realtime, bool verbose, uint64_t samples) {
|
||||
return lf_read_internal(realtime, verbose, samples, true);
|
||||
}
|
||||
|
||||
int CmdLFRead(const char *Cmd) {
|
||||
@@ -864,7 +869,7 @@ int CmdLFRead(const char *Cmd) {
|
||||
}
|
||||
int ret = PM3_SUCCESS;
|
||||
do {
|
||||
ret = lf_read_internal(realtime, verbose, samples);
|
||||
ret = lf_read_internal(realtime, verbose, samples, false);
|
||||
} while (cm && (kbd_enter_pressed() == false));
|
||||
|
||||
if (ret == PM3_SUCCESS) {
|
||||
@@ -1727,7 +1732,7 @@ static int lf_relay_tag(uint64_t samples, uint16_t port) {
|
||||
break;
|
||||
}
|
||||
|
||||
lf_read_internal(false, false, samples);
|
||||
lf_read_internal(false, false, samples, false);
|
||||
|
||||
if ((g_GraphTraceLen > 1000) && (getSignalProperties()->isnoise == false)) {
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ int CmdLFfind(const char *Cmd);
|
||||
int CmdLFRelay(const char *Cmd);
|
||||
|
||||
int lf_read(bool verbose, uint64_t samples);
|
||||
int lf_read_cotag(bool realtime, bool verbose, uint64_t samples);
|
||||
int lf_sniff(bool realtime, bool verbose, uint64_t samples);
|
||||
int lf_setconfig(sample_config *config);
|
||||
int lf_getconfig(sample_config *config);
|
||||
|
||||
+527
-5
@@ -18,6 +18,10 @@
|
||||
#include "cmdlfcotag.h" // COTAG function declarations
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <inttypes.h>
|
||||
#include <math.h>
|
||||
#include "cmdparser.h" // command_t
|
||||
#include "comms.h"
|
||||
#include "lfdemod.h"
|
||||
@@ -26,9 +30,30 @@
|
||||
#include "ctype.h" // tolower
|
||||
#include "cliparser.h"
|
||||
#include "commonutil.h" // reflect32
|
||||
#include "cmdlf.h" // lf_getconfig, lf_setconfig, lf_read_cotag
|
||||
#include "graph.h" // g_GraphTraceLen
|
||||
|
||||
#define XSTR(x) #x
|
||||
#define STR(x) XSTR(x)
|
||||
|
||||
#define LF_COTAG_DIVISOR 90 // 132 kHz
|
||||
#define LF_COTAG_CLOCK 768
|
||||
#define LF_COTAG_DATA_LEN 128
|
||||
|
||||
#define LF_COTAG_DEF_SAMPLES_READ 500000
|
||||
|
||||
static int CmdHelp(const char *Cmd);
|
||||
|
||||
static bool demod_cotag(int32_t *samples, int num_samples, int clock, int clock_start, int32_t threshold, bool verbose);
|
||||
static int detect_edge(const int32_t *samples, int num_samples,
|
||||
int index_start, int32_t threshold);
|
||||
static void find_avg_high_low(const int32_t *samples, int num_samples,
|
||||
int clock,
|
||||
double *out_avg_high, double *out_avg_low);
|
||||
|
||||
#if 0
|
||||
// TODO: With the new cotag implementation shall we remove this old version
|
||||
// or is there anything we could use from it?
|
||||
// COTAG demod should be able to use g_GraphBuffer,
|
||||
// when data load samples
|
||||
int demodCOTAG(bool verbose) {
|
||||
@@ -93,30 +118,436 @@ int demodCOTAG(bool verbose) {
|
||||
}
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Demodulate COTAG samples.
|
||||
*
|
||||
* @param clock Number of samples per clock cycle.
|
||||
* @param clock_start Sample index where the first clock cycle starts.
|
||||
* Use -1 for auto clock-start detection.
|
||||
* @param threshold Amplitude threshold that defines a "high" sample. Use -1 for auto-threshold detection.
|
||||
*/
|
||||
static bool demod_cotag(int32_t *samples, int num_samples, int clock, int clock_start, int32_t threshold, bool verbose) {
|
||||
int clock_half = clock / 2;
|
||||
int32_t min, max;
|
||||
double avg;
|
||||
bool rv = false;
|
||||
uint8_t *high_low_demod_01 = NULL;
|
||||
|
||||
uint8_t *manchester_demod = NULL;
|
||||
uint8_t *manchester_demod_reversed = NULL;
|
||||
|
||||
/* Calculate min, max, average */
|
||||
int64_t sum = 0;
|
||||
min = max = samples[0];
|
||||
for (int i = 0; i < num_samples; i++) {
|
||||
sum += samples[i];
|
||||
if (samples[i] < min) min = samples[i];
|
||||
if (samples[i] > max) max = samples[i];
|
||||
}
|
||||
avg = (double)sum / (double)num_samples;
|
||||
|
||||
if (verbose) {
|
||||
PrintAndLogEx(INFO, " Clock: %d", clock);
|
||||
if (threshold == -1)
|
||||
PrintAndLogEx(INFO, " Threshold: auto");
|
||||
else
|
||||
PrintAndLogEx(INFO, " Threshold: %d", threshold);
|
||||
PrintAndLogEx(INFO, " Min : %" PRId32, min);
|
||||
PrintAndLogEx(INFO, " Max : %" PRId32, max);
|
||||
PrintAndLogEx(INFO, " Avg : %.2f\n", avg);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/* DC offset removal: subtract average from every sample */
|
||||
for (int i = 0; i < num_samples; i++) {
|
||||
double v = round((double)samples[i] - avg);
|
||||
samples[i] = (int32_t)v;
|
||||
}
|
||||
|
||||
/* Auto threshold detection */
|
||||
if (threshold == -1) {
|
||||
double avg_high, avg_low;
|
||||
find_avg_high_low(samples, num_samples, clock, &avg_high, &avg_low);
|
||||
double thr = floor(0.9 * avg_high);
|
||||
threshold = (int32_t)thr;
|
||||
if (verbose)
|
||||
PrintAndLogEx(INFO, " Auto threshold: avg_low=%.2f, avg_high=%.2f --> threshold=%d", avg_low, avg_high, (int)threshold);
|
||||
}
|
||||
|
||||
/* Auto clock-start detection (first edge detection) */
|
||||
if (clock_start == -1) {
|
||||
clock_start = detect_edge(samples, num_samples, 0, threshold);
|
||||
if (verbose)
|
||||
PrintAndLogEx(INFO, " Detected clock start candidate: sample #%d", clock_start);
|
||||
}
|
||||
|
||||
/* High/low raw demodulation of clock-half cycles */
|
||||
int high_low_demod_01_len = (num_samples - clock_start) / clock_half + 16;
|
||||
high_low_demod_01 = calloc(high_low_demod_01_len, sizeof(uint8_t));
|
||||
if (!high_low_demod_01) {
|
||||
PrintAndLogEx(ERR, "Error: out of memory");
|
||||
goto end;
|
||||
}
|
||||
int high_low_demod_01_count = 0;
|
||||
|
||||
for (int idx = clock_start; idx + clock <= num_samples; idx += clock) {
|
||||
/* Average absolute values of first half */
|
||||
int32_t clock_half1_val_avg = 0;
|
||||
for (int k = 0; k < clock_half; k++)
|
||||
clock_half1_val_avg += abs(samples[idx + k]);
|
||||
double h1v = round((double)clock_half1_val_avg / clock_half);
|
||||
clock_half1_val_avg = (int32_t)h1v;
|
||||
|
||||
/* Average absolute values of second half */
|
||||
int32_t clock_half2_val_avg = 0;
|
||||
for (int k = 0; k < clock_half; k++)
|
||||
clock_half2_val_avg += abs(samples[idx + clock_half + k]);
|
||||
double h2v = round((double)clock_half2_val_avg / clock_half);
|
||||
clock_half2_val_avg = (int32_t)h2v;
|
||||
|
||||
uint8_t half1 = (clock_half1_val_avg >= threshold) ? 1 : 0;
|
||||
uint8_t half2 = (clock_half2_val_avg >= threshold) ? 1 : 0;
|
||||
|
||||
high_low_demod_01[high_low_demod_01_count] = half1;
|
||||
high_low_demod_01[high_low_demod_01_count + 1] = half2;
|
||||
high_low_demod_01_count += 2;
|
||||
}
|
||||
|
||||
/* Manchester demodulation buffer: */
|
||||
int manchester_demod_len = high_low_demod_01_count / 2;
|
||||
manchester_demod = calloc(manchester_demod_len, sizeof(uint8_t));
|
||||
if (!manchester_demod) {
|
||||
PrintAndLogEx(ERR, "Error: out of memory");
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* Manchester demodulation (Thomas) from raw high/low demod (high_low_demod_01) */
|
||||
const int MAX_MANDEMOD_ERRORS = 64;
|
||||
bool demod_success = true;
|
||||
int manchester_count = 0;
|
||||
int mandemod_err_count = 0;
|
||||
for (int i = 0; i + 1 < high_low_demod_01_count;) {
|
||||
uint8_t half1 = high_low_demod_01[i];
|
||||
uint8_t half2 = high_low_demod_01[i + 1];
|
||||
|
||||
if (half1 == 0 && half2 == 1) {
|
||||
manchester_demod[manchester_count++] = 0;
|
||||
i += 2;
|
||||
} else if (half1 == 1 && half2 == 0) {
|
||||
manchester_demod[manchester_count++] = 1;
|
||||
i += 2;
|
||||
} else {
|
||||
if (verbose)
|
||||
PrintAndLogEx(INFO, " Manchester demod error: index %d (sample #%" PRId32 "): half1=%u, half2=%u --> clock align by half cycle",
|
||||
i, (int32_t)(clock_start + i * clock_half), (unsigned)half1, (unsigned)half2);
|
||||
i += 1; /* re-align by one half-clock forward */
|
||||
mandemod_err_count++;
|
||||
if (mandemod_err_count >= MAX_MANDEMOD_ERRORS) {
|
||||
PrintAndLogEx(ERR, " Manchester demod: too many errors (%d), giving up", mandemod_err_count);
|
||||
demod_success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (demod_success) {
|
||||
if (verbose)
|
||||
PrintAndLogEx(INFO, " Manchester demod: %d bits", manchester_count);
|
||||
} else
|
||||
goto end;
|
||||
printf("\n");
|
||||
|
||||
/* Reverse order of bits */
|
||||
manchester_demod_reversed = calloc(manchester_demod_len, sizeof(uint8_t));
|
||||
if (!manchester_demod_reversed) {
|
||||
PrintAndLogEx(ERR, "Error: out of memory");
|
||||
goto end;
|
||||
}
|
||||
for (int i = 0; i < manchester_count; i++)
|
||||
manchester_demod_reversed[i] = manchester_demod[manchester_count - 1 - i];
|
||||
|
||||
{
|
||||
char manchester_demod_reversed_str[manchester_count + 1];
|
||||
for (int i = 0; i < manchester_count; i++)
|
||||
manchester_demod_reversed_str[i] = '0' + manchester_demod_reversed[i];
|
||||
manchester_demod_reversed_str[manchester_count] = '\0';
|
||||
PrintAndLogEx(SUCCESS, " Manchester demod reversed:");
|
||||
PrintAndLogEx(SUCCESS, " %s", manchester_demod_reversed_str);
|
||||
printf("\n");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* Example cotag card dump (manchester demod reversed):
|
||||
*
|
||||
* 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 0000 0101 1000 0010 0100 1110 0000 0000 0000 0000 1000 0000 0010 0100 1010 1000 1000 1111
|
||||
* 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 5 8 2 4 E 0 0 0 0 8 0 2 4 A 8 8 F
|
||||
*
|
||||
* Card number: 0x24A88F
|
||||
*/
|
||||
|
||||
/* Find preamble. */
|
||||
static const uint8_t preamble_a[] = {
|
||||
/* type A: 62 zeros followed by 1,0,1,0,0,0 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
|
||||
1, 0, 0, 0
|
||||
};
|
||||
static const uint8_t preamble_p[] = {
|
||||
/* type P: 55 zeros followed by 1,0,0,0,0,0,1 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
1, 0, 0, 0, 0, 0, 1
|
||||
};
|
||||
static const uint8_t preamble_p0[] = {
|
||||
/* type P-0: 61 zeros followed by 1 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
1
|
||||
};
|
||||
|
||||
struct {
|
||||
const uint8_t *pat;
|
||||
int len;
|
||||
const char *name;
|
||||
} preamble_patterns[] = {
|
||||
{ preamble_a, (int)(sizeof(preamble_a) / sizeof(preamble_a[0])), "A" },
|
||||
{ preamble_p, (int)(sizeof(preamble_p) / sizeof(preamble_p[0])), "P" },
|
||||
{ preamble_p0, (int)(sizeof(preamble_p0) / sizeof(preamble_p0[0])), "P-0" },
|
||||
{ NULL, 0, "" },
|
||||
};
|
||||
|
||||
int preamble_index = -1;
|
||||
const char *preamble_type = NULL;
|
||||
|
||||
for (int i = 0; i < manchester_count && preamble_index < 0; i++) {
|
||||
for (int p = 0; preamble_patterns[p].pat != NULL; p++) {
|
||||
if (i + preamble_patterns[p].len <= manchester_count &&
|
||||
memcmp(&manchester_demod_reversed[i], preamble_patterns[p].pat, preamble_patterns[p].len) == 0) {
|
||||
preamble_index = i;
|
||||
preamble_type = preamble_patterns[p].name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (preamble_index < 0) {
|
||||
PrintAndLogEx(INFO, " Preamble not found in manchester_demod_reversed");
|
||||
goto end;
|
||||
}
|
||||
|
||||
PrintAndLogEx(SUCCESS, " Preamble found (type %s) in manchester_demod_reversed at index %d",
|
||||
preamble_type, preamble_index);
|
||||
|
||||
/* data_bits: 128 bits starting at preamble */
|
||||
if (preamble_index + LF_COTAG_DATA_LEN > manchester_count) {
|
||||
PrintAndLogEx(INFO, " Not enough bits after preamble for full 128-bit data block");
|
||||
goto end;
|
||||
}
|
||||
|
||||
const uint8_t *data_bits = &manchester_demod_reversed[preamble_index];
|
||||
|
||||
/* Print raw 128 bits */
|
||||
{
|
||||
char str[LF_COTAG_DATA_LEN + 1];
|
||||
for (int i = 0; i < LF_COTAG_DATA_LEN; i++)
|
||||
str[i] = '0' + data_bits[i];
|
||||
str[LF_COTAG_DATA_LEN] = '\0';
|
||||
PrintAndLogEx(SUCCESS, " data bits: %s", str);
|
||||
}
|
||||
/* Print bits grouped by 4, space-separated */
|
||||
{
|
||||
char str[LF_COTAG_DATA_LEN + LF_COTAG_DATA_LEN / 4];
|
||||
int p = 0;
|
||||
for (int i = 0; i < LF_COTAG_DATA_LEN; i += 4) {
|
||||
if (i > 0) str[p++] = ' ';
|
||||
for (int j = 0; j < 4; j++)
|
||||
str[p++] = '0' + data_bits[i + j];
|
||||
}
|
||||
str[p] = '\0';
|
||||
PrintAndLogEx(SUCCESS, " data bits: %s", str);
|
||||
}
|
||||
/* Print bits as hex nibbles */
|
||||
{
|
||||
char str[LF_COTAG_DATA_LEN / 4 * 5 + 1];
|
||||
int p = 0;
|
||||
for (int i = 0; i < LF_COTAG_DATA_LEN; i += 4) {
|
||||
int nibble = (data_bits[i] << 3)
|
||||
| (data_bits[i + 1] << 2)
|
||||
| (data_bits[i + 2] << 1)
|
||||
| data_bits[i + 3];
|
||||
str[p++] = ' ';
|
||||
str[p++] = ' ';
|
||||
str[p++] = ' ';
|
||||
str[p++] = ' ';
|
||||
str[p++] = "0123456789ABCDEF"[nibble];
|
||||
}
|
||||
str[p] = '\0';
|
||||
PrintAndLogEx(SUCCESS, " data hex: %s", str);
|
||||
}
|
||||
|
||||
/* Card number: last 24 bits of data_bits as an integer */
|
||||
uint32_t c_num = 0;
|
||||
for (int i = LF_COTAG_DATA_LEN - 24; i < LF_COTAG_DATA_LEN; i++)
|
||||
c_num = (c_num << 1) | data_bits[i];
|
||||
PrintAndLogEx(SUCCESS, " card number: 0x%X == %u", c_num, c_num);
|
||||
|
||||
/* Count how many subsequent 128-bit blocks equal data_bits */
|
||||
int repeat_count = 0;
|
||||
bool fully_repeats = true;
|
||||
int pos = preamble_index + LF_COTAG_DATA_LEN;
|
||||
while (pos + LF_COTAG_DATA_LEN <= manchester_count) {
|
||||
if (memcmp(&manchester_demod_reversed[pos], data_bits, LF_COTAG_DATA_LEN) == 0) {
|
||||
repeat_count++;
|
||||
} else {
|
||||
fully_repeats = false;
|
||||
break;
|
||||
}
|
||||
pos += LF_COTAG_DATA_LEN;
|
||||
}
|
||||
|
||||
if (fully_repeats && repeat_count > 0)
|
||||
PrintAndLogEx(INFO, " Sequence fully repeats until the end %d time(s)", repeat_count);
|
||||
else
|
||||
PrintAndLogEx(INFO, " Sequence does NOT match at index %d (repeat count = %d)", pos, repeat_count);
|
||||
printf("\n");
|
||||
|
||||
rv = true;
|
||||
end:
|
||||
free(manchester_demod_reversed);
|
||||
free(manchester_demod);
|
||||
free(high_low_demod_01);
|
||||
return rv;
|
||||
}
|
||||
|
||||
int demodCOTAG(bool verbose, int clock, int threshold) {
|
||||
int clk = (clock > 0) ? clock : LF_COTAG_CLOCK;
|
||||
demod_cotag(g_GraphBuffer, (int)g_GraphTraceLen, clk, -1, threshold, verbose);
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
static int CmdCOTAGDemod(const char *Cmd) {
|
||||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "lf cotag demod",
|
||||
"Try to find COTAG preamble, if found decode / descramble data",
|
||||
"Demodulate COTAG samples from g_GraphBuffer. Try to find COTAG preamble, if found decode / descramble data.",
|
||||
"lf cotag demod"
|
||||
);
|
||||
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_lit0("v", "verbose", "verbose output"),
|
||||
arg_int0("c", "clk", "<dec>", "set clock manually (def " STR(LF_COTAG_CLOCK) ")"),
|
||||
arg_int0("t", "threshold", "<dec>", "set high value threshold manually (def: auto-detected)"),
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
||||
bool verbose = arg_get_lit(ctx, 1);
|
||||
int clock = arg_get_int_def(ctx, 2, -1);
|
||||
int thresh = arg_get_int_def(ctx, 3, -1);
|
||||
CLIParserFree(ctx);
|
||||
return demodCOTAG(verbose);
|
||||
return demodCOTAG(verbose, clock, thresh);
|
||||
}
|
||||
|
||||
static int CmdCOTAGReader(const char *Cmd) {
|
||||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "lf cotag reader",
|
||||
"Read a COTAG tag.\n"
|
||||
" - use " _YELLOW_("`lf config`") _CYAN_(" to set parameters except divisor "
|
||||
"(which is assumed " STR(LF_COTAG_DIVISOR) " for cotag. Use --divisor to override).\n")
|
||||
_CYAN_(" - use ") _YELLOW_("`data plot`") _CYAN_(" to look at it.\n")
|
||||
_CYAN_(" - use ") _YELLOW_("`lf cotag demod`") _CYAN_(" to try to demodulate it.\n")
|
||||
_CYAN_("If the number of samples is more than the device memory limit (40000 now), ")
|
||||
_CYAN_("it will try to use the real-time sampling mode.\n\n")
|
||||
_CYAN_("Note: Cotag has an extremely slow data rate - RF/" STR(LF_COTAG_CLOCK) " ")
|
||||
_CYAN_("-- capturing a full " STR(LF_COTAG_DATA_LEN) " bit card read requires a minimum of ")
|
||||
_CYAN_(STR(LF_COTAG_DATA_LEN) " x " STR(LF_COTAG_CLOCK) " samples, or a multiple thereof."),
|
||||
"lf cotag reader -v -s 700000 --> collect 700000 samples\n"
|
||||
"lf cotag reader -v --divisor 89 -s 700000 --> use divisor 89, collect 700000 samples\n"
|
||||
);
|
||||
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_u64_0("s", "samples", "<dec>", "number of samples to collect (def " STR(LF_COTAG_DEF_SAMPLES_READ) ")"),
|
||||
arg_lit0("v", "verbose", "verbose output"),
|
||||
arg_lit0("@", NULL, "continuous reading mode"),
|
||||
arg_int0(NULL, "divisor", "<19-255>", "Manually set freq divisor"),
|
||||
arg_param_end
|
||||
};
|
||||
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
||||
uint64_t samples = arg_get_u64_def(ctx, 1, LF_COTAG_DEF_SAMPLES_READ);
|
||||
bool verbose = arg_get_lit(ctx, 2);
|
||||
bool cm = arg_get_lit(ctx, 3);
|
||||
int16_t divisor = arg_get_int_def(ctx, 4, -1);
|
||||
CLIParserFree(ctx);
|
||||
|
||||
bool realtime = (samples > 40000);
|
||||
|
||||
if (divisor > -1 && (divisor < 19 || divisor > 255)) {
|
||||
PrintAndLogEx(ERR, "divisor must be between 19 and 255");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
if (g_session.pm3_present == false)
|
||||
return PM3_ENOTTY;
|
||||
|
||||
uint8_t effective_divisor = (divisor > -1) ? (uint8_t)divisor : LF_COTAG_DIVISOR;
|
||||
|
||||
/* Set lf config divisor for cotag, restore lfconfig at the end. */
|
||||
sample_config orig_config;
|
||||
int res = lf_getconfig(&orig_config);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "could not read current LF config");
|
||||
return res;
|
||||
}
|
||||
|
||||
sample_config tmp_config = orig_config;
|
||||
tmp_config.divisor = effective_divisor;
|
||||
tmp_config.verbose = verbose;
|
||||
res = lf_setconfig(&tmp_config);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "could not set LF config");
|
||||
return res;
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
PrintAndLogEx(INFO, "\nUsing divisor " _YELLOW_("%u") " (%.2f kHz)\n", effective_divisor, LF_DIV2FREQ(effective_divisor));
|
||||
}
|
||||
|
||||
if (cm || realtime) {
|
||||
PrintAndLogEx(INFO, "Press " _GREEN_("<Enter>") " to exit");
|
||||
}
|
||||
|
||||
int ret = PM3_SUCCESS;
|
||||
do {
|
||||
ret = lf_read_cotag(realtime, verbose, samples);
|
||||
} while (cm && (kbd_enter_pressed() == false));
|
||||
|
||||
orig_config.verbose = false;
|
||||
lf_setconfig(&orig_config);
|
||||
|
||||
if (verbose) {
|
||||
PrintAndLogEx(INFO, "\nRestored divisor " _YELLOW_("%u") " (%.2f kHz)\n", orig_config.divisor, LF_DIV2FREQ(orig_config.divisor));
|
||||
}
|
||||
|
||||
if (ret == PM3_SUCCESS) {
|
||||
PrintAndLogEx(SUCCESS, "Got " _YELLOW_("%zu") " samples", g_GraphTraceLen);
|
||||
|
||||
if (getSignalProperties()->isnoise) {
|
||||
PrintAndLogEx(INFO, "signal looks like noise");
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if 0 // TODO: Remove this implementation?
|
||||
// When reading a COTAG.
|
||||
// 0 = HIGH/LOW signal - maxlength bigbuff
|
||||
// 1 = translation for HI/LO into bytes with manchester 0,1 - length 300
|
||||
// 2 = raw signal - maxlength bigbuff
|
||||
static int CmdCOTAGReader(const char *Cmd) {
|
||||
int CmdCOTAGReader_old(const char *Cmd) {
|
||||
|
||||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "lf cotag reader",
|
||||
@@ -194,10 +625,11 @@ static int CmdCOTAGReader(const char *Cmd) {
|
||||
}
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
||||
static command_t CommandTable[] = {
|
||||
{"help", CmdHelp, AlwaysAvailable, "This help"},
|
||||
{"demod", CmdCOTAGDemod, AlwaysAvailable, "demodulate an COTAG tag"},
|
||||
{"demod", CmdCOTAGDemod, AlwaysAvailable, "demodulate a COTAG tag"},
|
||||
{"reader", CmdCOTAGReader, IfPm3Lf, "attempt to read and extract tag data"},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
};
|
||||
@@ -213,5 +645,95 @@ int CmdLFCOTAG(const char *Cmd) {
|
||||
}
|
||||
|
||||
int readCOTAGUid(void) {
|
||||
return (CmdCOTAGReader("-2") == PM3_SUCCESS);
|
||||
return (CmdCOTAGReader("") == PM3_SUCCESS && CmdCOTAGDemod("") == PM3_SUCCESS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find average high and average low amplitude by sliding a 256-sample window
|
||||
* over the first 8*clock samples (DC-removed values).
|
||||
*
|
||||
* @param samples DC-removed samples array.
|
||||
* @param num_samples Total number of samples.
|
||||
* @param clock Samples per clock cycle.
|
||||
* @param out_avg_high the highest window average seen
|
||||
* @param out_avg_low the lowest window average seen
|
||||
*/
|
||||
static void find_avg_high_low(const int32_t *samples, int num_samples,
|
||||
int clock,
|
||||
double *out_avg_high, double *out_avg_low) {
|
||||
const int WINDOW = 256;
|
||||
double avg_high = -127.0;
|
||||
double avg_low = 127.0;
|
||||
|
||||
int scan_end = 8 * clock;
|
||||
if (scan_end > num_samples)
|
||||
scan_end = num_samples;
|
||||
|
||||
for (int i = 0; i + WINDOW <= scan_end; i++) {
|
||||
double window_avg = 0.0;
|
||||
for (int k = 0; k < WINDOW; k++)
|
||||
window_avg += abs(samples[i + k]);
|
||||
window_avg /= WINDOW;
|
||||
|
||||
if (window_avg < avg_low)
|
||||
avg_low = window_avg;
|
||||
if (window_avg > avg_high)
|
||||
avg_high = window_avg;
|
||||
}
|
||||
|
||||
*out_avg_high = avg_high;
|
||||
*out_avg_low = avg_low;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the first amplitude edge (threshold crossing) in samples[],
|
||||
* starting at index_start, with glitch rejection.
|
||||
*
|
||||
* @param samples Array of (DC-removed) samples.
|
||||
* @param num_samples Number of samples in the array.
|
||||
* @param index_start Index to start scanning from.
|
||||
* @param threshold Amplitude threshold defining "high".
|
||||
*
|
||||
* @return Index of the detected edge, or 0 if none found.
|
||||
*/
|
||||
static int detect_edge(const int32_t *samples, int num_samples,
|
||||
int index_start, int32_t threshold) {
|
||||
const int GLITCH_WINDOW = 10;
|
||||
|
||||
if (num_samples <= 0 || index_start < 0 || index_start >= num_samples)
|
||||
return 0;
|
||||
|
||||
bool prev_high = abs(samples[index_start]) >= threshold;
|
||||
|
||||
for (int i = index_start + 1; i < num_samples; i++) {
|
||||
bool curr_high = abs(samples[i]) >= threshold;
|
||||
|
||||
if (curr_high != prev_high) {
|
||||
/* Need enough samples on both sides for glitch check */
|
||||
if ((int)i < GLITCH_WINDOW || i + GLITCH_WINDOW > num_samples) {
|
||||
prev_high = curr_high;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Sum of GLITCH_WINDOW absolute values before the crossing */
|
||||
int64_t before_sum = 0;
|
||||
for (int k = 0; k < GLITCH_WINDOW; k++)
|
||||
before_sum += abs(samples[i - GLITCH_WINDOW + k]);
|
||||
|
||||
/* Sum of GLITCH_WINDOW absolute values after the crossing */
|
||||
int64_t after_sum = 0;
|
||||
for (int k = 0; k < GLITCH_WINDOW; k++)
|
||||
after_sum += abs(samples[i + k]);
|
||||
|
||||
bool before_high = before_sum >= (int64_t)threshold * GLITCH_WINDOW;
|
||||
bool after_high = after_sum >= (int64_t)threshold * GLITCH_WINDOW;
|
||||
|
||||
if (before_high != after_high)
|
||||
return (int)i;
|
||||
}
|
||||
|
||||
prev_high = curr_high;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -27,6 +27,6 @@
|
||||
#endif
|
||||
|
||||
int CmdLFCOTAG(const char *Cmd);
|
||||
int demodCOTAG(bool verbose);
|
||||
int demodCOTAG(bool verbose, int clock, int threshold);
|
||||
int readCOTAGUid(void);
|
||||
#endif
|
||||
Reference in new issue
Block a user