diff --git a/client/src/cmddata.c b/client/src/cmddata.c index 67fbafafd..0ac3c0f4e 100644 --- a/client/src/cmddata.c +++ b/client/src/cmddata.c @@ -527,7 +527,7 @@ static int Cmdaskmandemod(const char *Cmd) { CLIExecWithReturn(ctx, Cmd, argtable, true); bool amplify = arg_get_lit(ctx, 1); - uint8_t clk = (uint8_t)arg_get_int_def(ctx, 2, 0) & 0xFF; + uint16_t clk = (uint16_t)arg_get_int_def(ctx, 2, 0); bool invert = arg_get_lit(ctx, 3); bool st = arg_get_lit(ctx, 4); uint8_t max_err = (uint8_t)arg_get_int_def(ctx, 5, 100) & 0xFF; @@ -773,7 +773,7 @@ static int Cmdaskrawdemod(const char *Cmd) { CLIExecWithReturn(ctx, Cmd, argtable, true); bool amplify = arg_get_lit(ctx, 1); - uint8_t clk = (uint8_t)arg_get_int_def(ctx, 2, 0) & 0xFF; + uint16_t clk = (uint16_t)arg_get_int_def(ctx, 2, 0); bool invert = arg_get_lit(ctx, 3); bool st = arg_get_lit(ctx, 4); uint8_t max_err = (uint8_t)arg_get_int_def(ctx, 5, 100) & 0xFF; @@ -3313,6 +3313,9 @@ cleanup: } int centerThreshold(const int *in, int *out, size_t len, int8_t up, int8_t down) { + if (len < 5) { + return PM3_EINVARG; + } for (size_t i = 0; i < len; ++i) { if ((in[i] <= up) && (in[i] >= down)) { @@ -3362,6 +3365,46 @@ static int CmdCenterThreshold(const char *Cmd) { return PM3_SUCCESS; } +static int envelope_square(const int *in, int *out, size_t len) { + if (len < 10) { + return PM3_EINVARG; + } + + for (size_t i = 0; i < len - 5; i++) { + + if (in[i] == 0 && in[i+1] == 0 && in[i+2] == 0 && in[i+3] == 0 && in[i+4] == 0) { + i += 4; + continue; + } + + out[i] = 255; + } + return PM3_SUCCESS; +} + +static int CmdEnvelope(const char *Cmd) { + CLIParserContext *ctx; + CLIParserInit(&ctx, "data envelop", + "Create an square envelop of the samples", + "data envelop" + ); + void *argtable[] = { + arg_param_begin, + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + CLIParserFree(ctx); + + envelope_square(g_GraphBuffer, g_GraphBuffer, g_GraphTraceLen); + + uint8_t bits[g_GraphTraceLen]; + size_t size = getFromGraphBuf(bits); + // set signal properties low/high/mean/amplitude and is_noice detection + computeSignalProperties(bits, size); + RepaintGraphWindow(); + return PM3_SUCCESS; +} + static command_t CommandTable[] = { {"help", CmdHelp, AlwaysAvailable, "This help"}, @@ -3378,6 +3421,7 @@ static command_t CommandTable[] = { {"autocorr", CmdAutoCorr, AlwaysAvailable, "Autocorrelation over window"}, {"dirthreshold", CmdDirectionalThreshold, AlwaysAvailable, "Max rising higher up-thres/ Min falling lower down-thres"}, {"decimate", CmdDecimate, AlwaysAvailable, "Decimate samples"}, + {"envelope", CmdEnvelope, AlwaysAvailable, "Generate square envelope of samples"}, {"undecimate", CmdUndecimate, AlwaysAvailable, "Un-decimate samples"}, {"hide", CmdHide, AlwaysAvailable, "Hide graph window"}, {"hpf", CmdHpf, AlwaysAvailable, "Remove DC offset from trace"}, diff --git a/common/lfdemod.c b/common/lfdemod.c index 73b580a6b..42ac2bcc0 100644 --- a/common/lfdemod.c +++ b/common/lfdemod.c @@ -436,10 +436,10 @@ static size_t findModStart(const uint8_t *src, size_t size, uint8_t expWaveSize) } static int getClosestClock(int testclk) { - const uint16_t clocks[] = {8, 16, 32, 40, 50, 64, 100, 128, 256, 384}; - const uint8_t limit[] = {1, 2, 4, 4, 5, 8, 8, 8, 8, 8}; + const uint16_t clocks[] = {8, 16, 32, 40, 50, 64, 100, 128, 256, 272, 384}; + const uint8_t limit[] = {1, 2, 4, 4, 5, 8, 8, 8, 8, 24, 24}; - for (uint8_t i = 0; i < 10; i++) { + for (uint8_t i = 0; i < ARRAYLEN(clocks); i++) { if (testclk >= clocks[i] - limit[i] && testclk <= clocks[i] + limit[i]) return clocks[i]; } @@ -613,7 +613,7 @@ bool DetectCleanAskWave(const uint8_t *dest, size_t size, uint8_t high, uint8_t // based on count of low to low int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clock) { size_t i = 100; - size_t minClk = 512; + size_t minClk = 768; uint16_t shortestWaveIdx = 0; // get to first full low to prime loop and skip incomplete first pulse @@ -622,11 +622,11 @@ int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clo if (i == size) return -1; - if (size < 512) + if (size < 768) return -2; // clock, numoftimes, first idx - uint16_t tmpclk[10][3] = { + uint16_t tmpclk[11][3] = { {8, 0, 0}, {16, 0, 0}, {32, 0, 0}, @@ -636,11 +636,12 @@ int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clo {100, 0, 0}, {128, 0, 0}, {256, 0, 0}, + {272, 0, 0}, {384, 0, 0}, }; // loop through all samples (well, we don't want to go out-of-bounds) - while (i < (size - 512)) { + while (i < (size - 768)) { // measure from low to low size_t startwave = i; @@ -655,7 +656,7 @@ int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clo int foo = getClosestClock(minClk); if (foo > 0) { - for (uint8_t j = 0; j < 10; j++) { + for (uint8_t j = 0; j < 11; j++) { if (tmpclk[j][0] == foo) { tmpclk[j][1]++; @@ -669,8 +670,17 @@ int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clo } // find the clock with most hits and it the first index it was encountered. + int possible_clks = 0; + for (uint8_t j = 0; j < 11; j++) { + if (tmpclk[j][1] > 0) { + possible_clks++; + } + } + + uint16_t second_shortest = 0; + int second = 0; int max = 0; - for (uint8_t j = 0; j < 10; j++) { + for (int j = 10; j > -1; j--) { if (g_debugMode == 2) { prnt("DEBUG, ASK, clocks %u | hits %u | idx %u" , tmpclk[j][0] @@ -678,13 +688,23 @@ int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clo , tmpclk[j][2] ); } + if (max < tmpclk[j][1]) { + second = *clock; + second_shortest = shortestWaveIdx; + *clock = tmpclk[j][0]; shortestWaveIdx = tmpclk[j][2]; max = tmpclk[j][1]; } } + // ASK clock 8 is very rare and usually gives us false positives + if (possible_clks > 1 && *clock == 8) { + *clock = second; + shortestWaveIdx = second_shortest; + } + if (*clock == 0) return -1; @@ -712,9 +732,9 @@ int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr) { } size_t i = 1; - uint8_t num_clks = 9; + uint8_t num_clks = 10; // first 255 value pos0 is placeholder for user inputed clock. - uint16_t clk[] = {255, 8, 16, 32, 40, 50, 64, 100, 128, 255}; + uint16_t clk[] = {255, 8, 16, 32, 40, 50, 64, 100, 128, 255, 272}; // sometimes there is a strange end wave - filter out this size -= 60; @@ -755,8 +775,8 @@ int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr) { uint8_t clkCnt, tol; size_t j = 0; - uint16_t bestErr[] = {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000}; - uint8_t bestStart[] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; + uint16_t bestErr[] = {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000}; + uint8_t bestStart[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; size_t errCnt, arrLoc, loopEnd; if (found_clk) {