Chg: step2 in the signed vis unsigned graphbuffer signal processing.

It will break some tone-based demods like ti etc.
This commit is contained in:
Chris 2018-09-09 12:34:43 +02:00
commit d774e4c84f
8 changed files with 276 additions and 217 deletions

View file

@ -456,8 +456,8 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType,
int maxErr = 100; int maxErr = 100;
int maxLen = 0; int maxLen = 0;
uint8_t askamp = 0; uint8_t askamp = 0;
char amp = param_getchar(Cmd, 0); char amp = tolower(param_getchar(Cmd, 0));
uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0}; uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0};
sscanf(Cmd, "%i %i %i %i %c", &clk, &invert, &maxErr, &maxLen, &amp); sscanf(Cmd, "%i %i %i %i %c", &clk, &invert, &maxErr, &maxLen, &amp);
@ -473,7 +473,7 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType,
clk = 0; clk = 0;
} }
size_t BitLen = getFromGraphBuf(BitStream); size_t BitLen = getFromGraphBuf(bits);
PrintAndLogEx(DEBUG, "DEBUG: (ASKDemod_ext) Bitlen from grphbuff: %d", BitLen); PrintAndLogEx(DEBUG, "DEBUG: (ASKDemod_ext) Bitlen from grphbuff: %d", BitLen);
@ -483,13 +483,13 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType,
int foundclk = 0; int foundclk = 0;
//amp before ST check //amp before ST check
if (amp == 'a' || amp == 'A') if (amp == 'a')
askAmp(BitStream, BitLen); askAmp(bits, BitLen);
bool st = false; bool st = false;
size_t ststart = 0, stend = 0; size_t ststart = 0, stend = 0;
if (*stCheck) if (*stCheck)
st = DetectST(BitStream, &BitLen, &foundclk, &ststart, &stend); st = DetectST(bits, &BitLen, &foundclk, &ststart, &stend);
if (st) { if (st) {
*stCheck = st; *stCheck = st;
@ -501,10 +501,10 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType,
} }
int startIdx = 0; int startIdx = 0;
int errCnt = askdemod_ext(BitStream, &BitLen, &clk, &invert, maxErr, askamp, askType, &startIdx); int errCnt = askdemod_ext(bits, &BitLen, &clk, &invert, maxErr, askamp, askType, &startIdx);
if (errCnt < 0 || BitLen < 16){ //if fatal error (or -1) if (errCnt < 0 || BitLen < 16){ //if fatal error (or -1)
PrintAndLogEx(DEBUG, "DEBUG: (ASKDemod_ext) No data found errors:%d, invert:%d, bitlen:%d, clock:%d", errCnt, invert, BitLen, clk); PrintAndLogEx(DEBUG, "DEBUG: (ASKDemod_ext) No data found errors:%d, invert:%c, bitlen:%d, clock:%d", errCnt, (invert)?'Y':'N', BitLen, clk);
return 0; return 0;
} }
@ -516,7 +516,7 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType,
if (verbose || g_debugMode) PrintAndLogEx(DEBUG, "DEBUG: (ASKDemod_ext) Using clock:%d, invert:%d, bits found:%d", clk, invert, BitLen); if (verbose || g_debugMode) PrintAndLogEx(DEBUG, "DEBUG: (ASKDemod_ext) Using clock:%d, invert:%d, bits found:%d", clk, invert, BitLen);
//output //output
setDemodBuf(BitStream,BitLen,0); setDemodBuf(bits, BitLen, 0);
setClockGrid(clk, startIdx); setClockGrid(clk, startIdx);
if (verbose || g_debugMode){ if (verbose || g_debugMode){
@ -1429,6 +1429,7 @@ int getSamples(int n, bool silent) {
GraphTraceLen = j; GraphTraceLen = j;
if (!silent) PrintAndLogEx(NORMAL, "Unpacked %d samples" , j ); if (!silent) PrintAndLogEx(NORMAL, "Unpacked %d samples" , j );
} else { } else {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
GraphBuffer[j] = ((int)got[j]) - 128; GraphBuffer[j] = ((int)got[j]) - 128;
@ -1436,9 +1437,11 @@ int getSamples(int n, bool silent) {
GraphTraceLen = n; GraphTraceLen = n;
} }
//ICEMAN todo //ICEMAN todo
// set signal properties low/high/mean/amplitude and is_noice detection uint8_t bits[GraphTraceLen];
justNoise(GraphBuffer, GraphTraceLen); size_t size = getFromGraphBuf(bits);
// set signal properties low/high/mean/amplitude and is_noice detection
isNoise(bits, size);
setClockGrid(0, 0); setClockGrid(0, 0);
DemodBufferLen = 0; DemodBufferLen = 0;
@ -1569,9 +1572,9 @@ int CmdLoad(const char *Cmd) {
DemodBufferLen = 0; DemodBufferLen = 0;
RepaintGraphWindow(); RepaintGraphWindow();
//ICEMAN todo //ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection // set signal properties low/high/mean/amplitude and isnoice detection
justNoise(GraphBuffer, GraphTraceLen); isNoise_int(GraphBuffer, GraphTraceLen);
return 0; return 0;
} }
@ -1637,9 +1640,9 @@ int CmdNorm(const char *Cmd) {
} }
RepaintGraphWindow(); RepaintGraphWindow();
//ICEMAN todo //ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection // set signal properties low/high/mean/amplitude and isnoice detection
justNoise(GraphBuffer, GraphTraceLen); isNoise_int(GraphBuffer, GraphTraceLen);
return 0; return 0;
} }
@ -1722,7 +1725,12 @@ int CmdDirectionalThreshold(const char *Cmd) {
PrintAndLogEx(INFO, "Applying Up Threshold: %d, Down Threshold: %d\n", up, down); PrintAndLogEx(INFO, "Applying Up Threshold: %d, Down Threshold: %d\n", up, down);
directionalThreshold(GraphBuffer, GraphBuffer,GraphTraceLen, up, down); directionalThreshold(GraphBuffer, GraphBuffer, GraphTraceLen, up, down);
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
isNoise_int(GraphBuffer, GraphTraceLen);
RepaintGraphWindow(); RepaintGraphWindow();
return 0; return 0;
} }
@ -1751,7 +1759,7 @@ int CmdZerocrossings(const char *Cmd) {
//ICEMAN todo //ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection // set signal properties low/high/mean/amplitude and isnoice detection
justNoise(GraphBuffer, GraphTraceLen); isNoise_int(GraphBuffer, GraphTraceLen);
RepaintGraphWindow(); RepaintGraphWindow();
return 0; return 0;
@ -2004,6 +2012,10 @@ int CmdDataIIR(const char *Cmd){
uint8_t k = param_get8(Cmd, 0); uint8_t k = param_get8(Cmd, 0);
//iceIIR_Butterworth(GraphBuffer, GraphTraceLen); //iceIIR_Butterworth(GraphBuffer, GraphTraceLen);
iceSimple_Filter(GraphBuffer, GraphTraceLen, k); iceSimple_Filter(GraphBuffer, GraphTraceLen, k);
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
isNoise_int(GraphBuffer, GraphTraceLen);
RepaintGraphWindow(); RepaintGraphWindow();
return 0; return 0;
} }

View file

@ -147,10 +147,9 @@ int CmdLFCommandRead(const char *Cmd) {
bool errors = false; bool errors = false;
uint8_t cmdp = 0; uint8_t cmdp = 0;
while(param_getchar(Cmd, cmdp) != 0x00 && !errors) { while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch(param_getchar(Cmd, cmdp)) { switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h': case 'h':
case 'H':
return usage_lf_cmdread(); return usage_lf_cmdread();
case 'c': case 'c':
param_getstr(Cmd, cmdp+1, (char *)&c.d.asBytes, sizeof(c.d.asBytes)); param_getstr(Cmd, cmdp+1, (char *)&c.d.asBytes, sizeof(c.d.asBytes));
@ -188,16 +187,20 @@ int CmdLFCommandRead(const char *Cmd) {
int CmdFlexdemod(const char *Cmd) { int CmdFlexdemod(const char *Cmd) {
#define LONG_WAIT 100 #define LONG_WAIT 100
int i, j, start, bit, sum; int i, j, start, bit, sum, phase = 0;
int phase = 0;
for (i = 0; i < GraphTraceLen; ++i) uint8_t data[MAX_GRAPH_TRACE_LEN] = {0};
GraphBuffer[i] = (GraphBuffer[i] < 0) ? -1 : 1; size_t size = getFromGraphBuf(data);
if (size == 0)
return 0;
for (i = 0; i < size; ++i)
data[i] = (data[i] < 0) ? -1 : 1;
for (start = 0; start < GraphTraceLen - LONG_WAIT; start++) { for (start = 0; start < size - LONG_WAIT; start++) {
int first = GraphBuffer[start]; int first = data[start];
for (i = start; i < start + LONG_WAIT; i++) { for (i = start; i < start + LONG_WAIT; i++) {
if (GraphBuffer[i] != first) { if (data[i] != first) {
break; break;
} }
} }
@ -205,20 +208,21 @@ int CmdFlexdemod(const char *Cmd) {
break; break;
} }
if (start == GraphTraceLen - LONG_WAIT) { if (start == size - LONG_WAIT) {
PrintAndLogEx(NORMAL, "nothing to wait for"); PrintAndLogEx(NORMAL, "nothing to wait for");
return 0; return 0;
} }
GraphBuffer[start] = 2; data[start] = 4;
GraphBuffer[start+1] = -2; data[start+1] = 0;
uint8_t bits[64] = {0x00}; uint8_t bits[64] = {0x00};
i = start; i = start;
for (bit = 0; bit < 64; bit++) { for (bit = 0; bit < 64; bit++) {
sum = 0; sum = 0;
for (int j = 0; j < 16; j++) { for (int j = 0; j < 16; j++) {
sum += GraphBuffer[i++]; sum += data[i++];
} }
bits[bit] = (sum > 0) ? 1 : 0; bits[bit] = (sum > 0) ? 1 : 0;
PrintAndLogEx(NORMAL, "bit %d sum %d", bit, sum); PrintAndLogEx(NORMAL, "bit %d sum %d", bit, sum);
@ -227,7 +231,7 @@ int CmdFlexdemod(const char *Cmd) {
for (bit = 0; bit < 64; bit++) { for (bit = 0; bit < 64; bit++) {
sum = 0; sum = 0;
for (j = 0; j < 16; j++) for (j = 0; j < 16; j++)
sum += GraphBuffer[i++]; sum += data[i++];
if (sum > 0 && bits[bit] != 1) PrintAndLogEx(NORMAL, "oops1 at %d", bit); if (sum > 0 && bits[bit] != 1) PrintAndLogEx(NORMAL, "oops1 at %d", bit);
@ -235,8 +239,9 @@ int CmdFlexdemod(const char *Cmd) {
} }
// iceman, use demod buffer? blue line?
// HACK writing back to graphbuffer. // HACK writing back to graphbuffer.
GraphTraceLen = 32*64; GraphTraceLen = 32 * 64;
i = 0; i = 0;
for (bit = 0; bit < 64; bit++) { for (bit = 0; bit < 64; bit++) {
@ -261,8 +266,8 @@ int CmdLFSetConfig(const char *Cmd) {
uint8_t unsigned_trigg = 0; uint8_t unsigned_trigg = 0;
uint8_t cmdp = 0; uint8_t cmdp = 0;
while(param_getchar(Cmd, cmdp) != 0x00 && !errors) { while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch(param_getchar(Cmd, cmdp)) { switch (param_getchar(Cmd, cmdp)) {
case 'h': case 'h':
return usage_lf_config(); return usage_lf_config();
case 'H': case 'H':
@ -348,18 +353,15 @@ int CmdLFRead(const char *Cmd) {
bool silent = false; bool silent = false;
uint32_t samples = 0; uint32_t samples = 0;
uint8_t cmdp = 0; uint8_t cmdp = 0;
while(param_getchar(Cmd, cmdp) != 0x00 && !errors) { while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch(param_getchar(Cmd, cmdp)) { switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h': case 'h':
case 'H':
return usage_lf_read(); return usage_lf_read();
case 's': case 's':
case 'S':
silent = true; silent = true;
cmdp++; cmdp++;
break; break;
case 'd': case 'd':
case 'D':
samples = param_get32ex(Cmd, cmdp, 0, 10); samples = param_get32ex(Cmd, cmdp, 0, 10);
cmdp +=2; cmdp +=2;
break; break;
@ -377,8 +379,8 @@ int CmdLFRead(const char *Cmd) {
} }
int CmdLFSnoop(const char *Cmd) { int CmdLFSnoop(const char *Cmd) {
uint8_t cmdp = param_getchar(Cmd, 0); uint8_t cmdp = tolower(param_getchar(Cmd, 0));
if(cmdp == 'h' || cmdp == 'H') return usage_lf_snoop(); if (cmdp == 'h') return usage_lf_snoop();
UsbCommand c = {CMD_LF_SNOOP_RAW_ADC_SAMPLES,{0,0,0}}; UsbCommand c = {CMD_LF_SNOOP_RAW_ADC_SAMPLES,{0,0,0}};
clearCommandBuffer(); clearCommandBuffer();
@ -444,8 +446,8 @@ int CmdLFfskSim(const char *Cmd) {
int dataLen = 0; int dataLen = 0;
uint8_t cmdp = 0; uint8_t cmdp = 0;
while(param_getchar(Cmd, cmdp) != 0x00 && !errors) { while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch(param_getchar(Cmd, cmdp)){ switch (param_getchar(Cmd, cmdp)){
case 'h': case 'h':
return usage_lf_simfsk(); return usage_lf_simfsk();
case 'c': case 'c':
@ -473,7 +475,7 @@ int CmdLFfskSim(const char *Cmd) {
if (dataLen == 0) errors = true; if (dataLen == 0) errors = true;
if (errors) PrintAndLogEx(WARNING, "Error getting hex data"); if (errors) PrintAndLogEx(WARNING, "Error getting hex data");
cmdp+=2; cmdp += 2;
break; break;
default: default:
PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
@ -533,13 +535,12 @@ int CmdLFaskSim(const char *Cmd) {
uint8_t encoding = 1, separator = 0, clk = 0, invert = 0; uint8_t encoding = 1, separator = 0, clk = 0, invert = 0;
bool errors = false; bool errors = false;
char hexData[64] = {0x00}; char hexData[64] = {0x00};
uint8_t data[255]= {0x00}; // store entered hex data uint8_t data[255] = {0x00}; // store entered hex data
int dataLen = 0; int dataLen = 0;
uint8_t cmdp = 0; uint8_t cmdp = 0;
while(param_getchar(Cmd, cmdp) != 0x00 && !errors) { while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch(param_getchar(Cmd, cmdp)) { switch (tolower(param_getchar(Cmd, cmdp))) {
case 'H':
case 'h': return usage_lf_simask(); case 'h': return usage_lf_simask();
case 'i': case 'i':
invert = 1; invert = 1;
@ -632,8 +633,8 @@ int CmdLFpskSim(const char *Cmd) {
uint8_t cmdp = 0; uint8_t cmdp = 0;
uint8_t pskType = 1; uint8_t pskType = 1;
while(param_getchar(Cmd, cmdp) != 0x00 && !errors) { while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch(param_getchar(Cmd, cmdp)) { switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h': case 'h':
return usage_lf_simpsk(); return usage_lf_simpsk();
case 'i': case 'i':
@ -735,6 +736,7 @@ int CmdLFSimBidir(const char *Cmd) {
return 0; return 0;
} }
// ICEMAN, todo, swap from Graphbuffer.
int CmdVchDemod(const char *Cmd) { int CmdVchDemod(const char *Cmd) {
// Is this the entire sync pattern, or does this also include some // Is this the entire sync pattern, or does this also include some
// data bits that happen to be the same everywhere? That would be // data bits that happen to be the same everywhere? That would be

View file

@ -411,7 +411,7 @@ int CmdEM410xRead(const char *Cmd) {
// this read loops on device side. // this read loops on device side.
// uses the demod in lfops.c // uses the demod in lfops.c
int CmdEM410xRead_device(const char *Cmd) { int CmdEM410xRead_device(const char *Cmd) {
char cmdp = param_getchar(Cmd, 0); char cmdp = tolower(param_getchar(Cmd, 0));
uint8_t findone = (cmdp == '1') ? 1 : 0; uint8_t findone = (cmdp == '1') ? 1 : 0;
UsbCommand c = {CMD_EM410X_DEMOD, {findone, 0, 0}}; UsbCommand c = {CMD_EM410X_DEMOD, {findone, 0, 0}};
SendCommand(&c); SendCommand(&c);
@ -423,13 +423,13 @@ int CmdEM410xRead_device(const char *Cmd) {
//attempts to demodulate ask while decoding manchester //attempts to demodulate ask while decoding manchester
//prints binary found and saves in graphbuffer for further commands //prints binary found and saves in graphbuffer for further commands
int CmdEM410xDemod(const char *Cmd) { int CmdEM410xDemod(const char *Cmd) {
char cmdp = param_getchar(Cmd, 0); char cmdp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') return usage_lf_em410x_demod(); if (strlen(Cmd) > 10 || cmdp == 'h') return usage_lf_em410x_demod();
uint32_t hi = 0; uint32_t hi = 0;
uint64_t lo = 0; uint64_t lo = 0;
if(AskEm410xDemod(Cmd, &hi, &lo, true) != 1) return 0; if (AskEm410xDemod(Cmd, &hi, &lo, true) != 1) return 0;
g_em410xid = lo; g_em410xid = lo;
return 1; return 1;
@ -437,8 +437,8 @@ int CmdEM410xDemod(const char *Cmd) {
// emulate an EM410X tag // emulate an EM410X tag
int CmdEM410xSim(const char *Cmd) { int CmdEM410xSim(const char *Cmd) {
char cmdp = param_getchar(Cmd, 0); char cmdp = tolower(param_getchar(Cmd, 0));
if (cmdp == 'h' || cmdp == 'H') return usage_lf_em410x_sim(); if (cmdp == 'h') return usage_lf_em410x_sim();
uint8_t uid[5] = {0x00}; uint8_t uid[5] = {0x00};
@ -594,8 +594,8 @@ int CmdEM410xWatch(const char *Cmd) {
//currently only supports manchester modulations //currently only supports manchester modulations
int CmdEM410xWatchnSpoof(const char *Cmd) { int CmdEM410xWatchnSpoof(const char *Cmd) {
char cmdp = param_getchar(Cmd, 0); char cmdp = tolower(param_getchar(Cmd, 0));
if (cmdp == 'h' || cmdp == 'H') return usage_lf_em410x_ws(); if (cmdp == 'h') return usage_lf_em410x_ws();
// loops if the captured ID was in XL-format. // loops if the captured ID was in XL-format.
CmdEM410xWatch(Cmd); CmdEM410xWatch(Cmd);
@ -784,64 +784,59 @@ uint32_t OutputEM4x50_Block(uint8_t *BitStream, size_t size, bool verbose, bool
//completed by Marshmellow //completed by Marshmellow
int EM4x50Read(const char *Cmd, bool verbose) { int EM4x50Read(const char *Cmd, bool verbose) {
uint8_t fndClk[] = {8,16,32,40,50,64,128}; uint8_t fndClk[] = {8,16,32,40,50,64,128};
int clk = 0; int clk = 0, invert = 0, tol = 0, phaseoff;
int invert = 0; int i = 0, j = 0, startblock, skip, block, start, end, low = 0, high = 0, minClk = 255;
int tol = 0;
int i, j, startblock, skip, block, start, end, low, high, minClk;
bool complete = false;
int tmpbuff[MAX_GRAPH_TRACE_LEN / 64];
uint32_t Code[6]; uint32_t Code[6];
char tmp[6]; char tmp[6];
char tmp2[20]; char tmp2[20];
int phaseoff; bool complete = false;
high = low = 0;
int tmpbuff[MAX_GRAPH_TRACE_LEN / 64];
memset(tmpbuff, 0, sizeof(tmpbuff) ); memset(tmpbuff, 0, sizeof(tmpbuff) );
// get user entry if any // get user entry if any
sscanf(Cmd, "%i %i", &clk, &invert); sscanf(Cmd, "%i %i", &clk, &invert);
// first get high and low values uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0};
for (i = 0; i < GraphTraceLen; i++) { size_t size = getFromGraphBuf(bits);
if (GraphBuffer[i] > high)
high = GraphBuffer[i]; isNoise(bits, size);
else if (GraphBuffer[i] < low)
low = GraphBuffer[i]; signal_t *sp = getSignalProperties();
} high = sp->high;
low = sp->low;
i = 0;
j = 0;
minClk = 255;
// get to first full low to prime loop and skip incomplete first pulse // get to first full low to prime loop and skip incomplete first pulse
while ((GraphBuffer[i] < high) && (i < GraphTraceLen)) while ((bits[i] < high) && (i < size))
++i; ++i;
while ((GraphBuffer[i] > low) && (i < GraphTraceLen)) while ((bits[i] > low) && (i < size))
++i; ++i;
skip = i; skip = i;
// populate tmpbuff buffer with pulse lengths // populate tmpbuff buffer with pulse lengths
while (i < GraphTraceLen) { while (i < size) {
// measure from low to low // measure from low to low
while ((GraphBuffer[i] > low) && (i < GraphTraceLen)) while ((bits[i] > low) && (i < size))
++i; ++i;
start= i; start= i;
while ((GraphBuffer[i] < high) && (i < GraphTraceLen)) while ((bits[i] < high) && (i < size))
++i; ++i;
while ((GraphBuffer[i] > low) && (i < GraphTraceLen)) while ((bits[i] > low) && (i < size))
++i; ++i;
if (j>=(MAX_GRAPH_TRACE_LEN/64)) { if (j >= (MAX_GRAPH_TRACE_LEN/64)) {
break; break;
} }
tmpbuff[j++]= i - start; tmpbuff[j++] = i - start;
if (i-start < minClk && i < GraphTraceLen) { if (i-start < minClk && i < size) {
minClk = i - start; minClk = i - start;
} }
} }
// set clock // set clock
if (!clk) { if (!clk) {
for (uint8_t clkCnt = 0; clkCnt<7; clkCnt++) { for (uint8_t clkCnt = 0; clkCnt < 7; clkCnt++) {
tol = fndClk[clkCnt]/8; tol = fndClk[clkCnt] / 8;
if (minClk >= fndClk[clkCnt]-tol && minClk <= fndClk[clkCnt]+1) { if (minClk >= fndClk[clkCnt] - tol && minClk <= fndClk[clkCnt] + 1) {
clk=fndClk[clkCnt]; clk = fndClk[clkCnt];
break; break;
} }
} }
@ -849,15 +844,15 @@ int EM4x50Read(const char *Cmd, bool verbose) {
if (verbose || g_debugMode) PrintAndLogEx(WARNING, "Error: EM4x50 - didn't find a clock"); if (verbose || g_debugMode) PrintAndLogEx(WARNING, "Error: EM4x50 - didn't find a clock");
return 0; return 0;
} }
} else tol = clk/8; } else tol = clk / 8;
// look for data start - should be 2 pairs of LW (pulses of clk*3,clk*2) // look for data start - should be 2 pairs of LW (pulses of clk*3,clk*2)
start = -1; start = -1;
for (i= 0; i < j - 4 ; ++i) { for (i= 0; i < j - 4 ; ++i) {
skip += tmpbuff[i]; skip += tmpbuff[i];
if (tmpbuff[i] >= clk*3-tol && tmpbuff[i] <= clk*3+tol) //3 clocks if (tmpbuff[i] >= clk * 3 - tol && tmpbuff[i] <= clk * 3 + tol) //3 clocks
if (tmpbuff[i+1] >= clk*2-tol && tmpbuff[i+1] <= clk*2+tol) //2 clocks if (tmpbuff[i+1] >= clk * 2 - tol && tmpbuff[i+1] <= clk * 2 + tol) //2 clocks
if (tmpbuff[i+2] >= clk*3-tol && tmpbuff[i+2] <= clk*3+tol) //3 clocks if (tmpbuff[i+2] >= clk * 3 - tol && tmpbuff[i+2] <= clk * 3 + tol) //3 clocks
if (tmpbuff[i+3] >= clk-tol) //1.5 to 2 clocks - depends on bit following if (tmpbuff[i+3] >= clk-tol) //1.5 to 2 clocks - depends on bit following
{ {
start= i + 4; start= i + 4;
@ -867,18 +862,20 @@ int EM4x50Read(const char *Cmd, bool verbose) {
startblock = i + 4; startblock = i + 4;
// skip over the remainder of LW // skip over the remainder of LW
skip += tmpbuff[i+1] + tmpbuff[i+2] + clk; skip += (tmpbuff[i+1] + tmpbuff[i+2] + clk);
if (tmpbuff[i+3]>clk)
phaseoff = tmpbuff[i+3]-clk; if (tmpbuff[i+3] > clk)
phaseoff = tmpbuff[i+3] - clk;
else else
phaseoff = 0; phaseoff = 0;
// now do it again to find the end // now do it again to find the end
end = skip; end = skip;
for (i += 3; i < j - 4 ; ++i) { for (i += 3; i < j - 4 ; ++i) {
end += tmpbuff[i]; end += tmpbuff[i];
if (tmpbuff[i] >= clk*3-tol && tmpbuff[i] <= clk*3+tol) //3 clocks if (tmpbuff[i] >= clk * 3 - tol && tmpbuff[i] <= clk * 3 + tol) //3 clocks
if (tmpbuff[i+1] >= clk*2-tol && tmpbuff[i+1] <= clk*2+tol) //2 clocks if (tmpbuff[i+1] >= clk * 2 - tol && tmpbuff[i+1] <= clk * 2 + tol) //2 clocks
if (tmpbuff[i+2] >= clk*3-tol && tmpbuff[i+2] <= clk*3+tol) //3 clocks if (tmpbuff[i+2] >= clk * 3 - tol && tmpbuff[i+2] <= clk * 3 + tol) //3 clocks
if (tmpbuff[i+3] >= clk-tol) //1.5 to 2 clocks - depends on bit following if (tmpbuff[i+3] >= clk-tol) //1.5 to 2 clocks - depends on bit following
{ {
complete= true; complete= true;
@ -897,8 +894,9 @@ int EM4x50Read(const char *Cmd, bool verbose) {
return 0; return 0;
} }
} else if (start < 0) return 0; } else if (start < 0) return 0;
start = skip; start = skip;
snprintf(tmp2, sizeof(tmp2),"%d %d 1000 %d", clk, invert, clk*47); snprintf(tmp2, sizeof(tmp2),"%d %d 1000 %d", clk, invert, clk * 47);
// save GraphBuffer - to restore it later // save GraphBuffer - to restore it later
save_restoreGB(GRAPH_SAVE); save_restoreGB(GRAPH_SAVE);
// get rid of leading crap // get rid of leading crap
@ -916,17 +914,19 @@ int EM4x50Read(const char *Cmd, bool verbose) {
// look for LW before start of next block // look for LW before start of next block
for ( ; i < j - 4 ; ++i) { for ( ; i < j - 4 ; ++i) {
skip += tmpbuff[i]; skip += tmpbuff[i];
if (tmpbuff[i] >= clk*3-tol && tmpbuff[i] <= clk*3+tol) if (tmpbuff[i] >= clk * 3 - tol && tmpbuff[i] <= clk * 3 + tol)
if (tmpbuff[i+1] >= clk-tol) if (tmpbuff[i+1] >= clk-tol)
break; break;
} }
if (i >= j-4) break; //next LW not found if (i >= j-4) break; //next LW not found
skip += clk; skip += clk;
if (tmpbuff[i+1]>clk) if (tmpbuff[i+1] > clk)
phaseoff = tmpbuff[i+1]-clk; phaseoff = tmpbuff[i+1] - clk;
else else
phaseoff = 0; phaseoff = 0;
i += 2; i += 2;
if (ASKDemod(tmp2, false, false, 1) < 1) { if (ASKDemod(tmp2, false, false, 1) < 1) {
save_restoreGB(GRAPH_RESTORE); save_restoreGB(GRAPH_RESTORE);
return 0; return 0;
@ -934,14 +934,14 @@ int EM4x50Read(const char *Cmd, bool verbose) {
//set DemodBufferLen to just one block //set DemodBufferLen to just one block
DemodBufferLen = skip/clk; DemodBufferLen = skip/clk;
//test parities //test parities
pTest = EM_ByteParityTest(DemodBuffer,DemodBufferLen,5,9,0); pTest = EM_ByteParityTest(DemodBuffer,DemodBufferLen, 5, 9, 0);
pTest &= EM_EndParityTest(DemodBuffer,DemodBufferLen,5,9,0); pTest &= EM_EndParityTest(DemodBuffer,DemodBufferLen, 5, 9, 0);
AllPTest &= pTest; AllPTest &= pTest;
//get output //get output
Code[block] = OutputEM4x50_Block(DemodBuffer,DemodBufferLen,verbose, pTest); Code[block] = OutputEM4x50_Block(DemodBuffer, DemodBufferLen, verbose, pTest);
PrintAndLogEx(DEBUG, "\nskipping %d samples, bits:%d", skip, skip/clk); PrintAndLogEx(DEBUG, "\nskipping %d samples, bits:%d", skip, skip/clk);
//skip to start of next block //skip to start of next block
snprintf(tmp,sizeof(tmp),"%i",skip); snprintf(tmp, sizeof(tmp), "%i", skip);
CmdLtrim(tmp); CmdLtrim(tmp);
block++; block++;
if (i >= end) break; //in case chip doesn't output 6 blocks if (i >= end) break; //in case chip doesn't output 6 blocks
@ -956,7 +956,7 @@ int EM4x50Read(const char *Cmd, bool verbose) {
PrintAndLogEx(NORMAL, "Found data at sample: %i - using clock: %i", start, clk); PrintAndLogEx(NORMAL, "Found data at sample: %i - using clock: %i", start, clk);
end = block; end = block;
for (block=0; block < end; block++){ for (block=0; block < end; block++){
PrintAndLogEx(NORMAL, "Block %d: %08x",block,Code[block]); PrintAndLogEx(NORMAL, "Block %d: %08x", block, Code[block]);
} }
if (AllPTest) { if (AllPTest) {
PrintAndLogEx(NORMAL, "Parities Passed"); PrintAndLogEx(NORMAL, "Parities Passed");
@ -972,19 +972,19 @@ int EM4x50Read(const char *Cmd, bool verbose) {
} }
int CmdEM4x50Read(const char *Cmd) { int CmdEM4x50Read(const char *Cmd) {
uint8_t ctmp = param_getchar(Cmd, 0); uint8_t ctmp = tolower(param_getchar(Cmd, 0));
if ( ctmp == 'H' || ctmp == 'h' ) return usage_lf_em4x50_read(); if ( ctmp == 'h' ) return usage_lf_em4x50_read();
return EM4x50Read(Cmd, true); return EM4x50Read(Cmd, true);
} }
int CmdEM4x50Write(const char *Cmd){ int CmdEM4x50Write(const char *Cmd){
uint8_t ctmp = param_getchar(Cmd, 0); uint8_t ctmp = tolower(param_getchar(Cmd, 0));
if ( ctmp == 'H' || ctmp == 'h' ) return usage_lf_em4x50_write(); if ( ctmp == 'h' ) return usage_lf_em4x50_write();
PrintAndLogEx(NORMAL, "no implemented yet"); PrintAndLogEx(NORMAL, "no implemented yet");
return 0; return 0;
} }
int CmdEM4x50Dump(const char *Cmd){ int CmdEM4x50Dump(const char *Cmd){
uint8_t ctmp = param_getchar(Cmd, 0); uint8_t ctmp = tolower(param_getchar(Cmd, 0));
if ( ctmp == 'H' || ctmp == 'h' ) return usage_lf_em4x50_dump(); if ( ctmp == 'h' ) return usage_lf_em4x50_dump();
PrintAndLogEx(NORMAL, "no implemented yet"); PrintAndLogEx(NORMAL, "no implemented yet");
return 0; return 0;
} }
@ -1000,6 +1000,11 @@ bool downloadSamplesEM(){
return false; return false;
} }
setGraphBuf(got, sizeof(got)); setGraphBuf(got, sizeof(got));
if (isNoise(got, sizeof(got))) {
PrintAndLogEx(DEBUG, "No tag found");
return false;
}
return true; return true;
} }
@ -1146,12 +1151,7 @@ int EM4x05ReadWord_ext(uint8_t addr, uint32_t pwd, bool usePwd, uint32_t *word)
if ( !downloadSamplesEM() ) { if ( !downloadSamplesEM() ) {
return -1; return -1;
} }
int testLen = (GraphTraceLen < 1000) ? GraphTraceLen : 1000;
if (justNoise(GraphBuffer, testLen)) {
PrintAndLogEx(DEBUG, "No tag found");
return -1;
}
return demodEM4x05resp(word); return demodEM4x05resp(word);
} }

View file

@ -184,9 +184,11 @@ int CmdIndalaDemodAlt(const char *Cmd) {
int count = 0; int count = 0;
int i, j; int i, j;
// worst case with GraphTraceLen=64000 is < 4096 // worst case with GraphTraceLen=40000 is < 4096
// under normal conditions it's < 2048 // under normal conditions it's < 2048
uint8_t data[MAX_GRAPH_TRACE_LEN] = {0};
size_t datasize = getFromGraphBuf(data);
uint8_t rawbits[4096]; uint8_t rawbits[4096];
int rawbit = 0; int rawbit = 0;
int worst = 0, worstPos = 0; int worst = 0, worstPos = 0;
@ -197,9 +199,9 @@ int CmdIndalaDemodAlt(const char *Cmd) {
// PrintAndLogEx(NORMAL, "Expecting a bit less than %d raw bits", GraphTraceLen / 32); // PrintAndLogEx(NORMAL, "Expecting a bit less than %d raw bits", GraphTraceLen / 32);
// loop through raw signal - since we know it is psk1 rf/32 fc/2 skip every other value (+=2) // loop through raw signal - since we know it is psk1 rf/32 fc/2 skip every other value (+=2)
for (i = 0; i < GraphTraceLen-1; i += 2) { for (i = 0; i < datasize-1; i += 2) {
count += 1; count += 1;
if ((GraphBuffer[i] > GraphBuffer[i + 1]) && (state != 1)) { if ((data[i] > data[i + 1]) && (state != 1)) {
// appears redundant - marshmellow // appears redundant - marshmellow
if (state == 0) { if (state == 0) {
for (j = 0; j < count - 8; j += 16) { for (j = 0; j < count - 8; j += 16) {
@ -212,7 +214,7 @@ int CmdIndalaDemodAlt(const char *Cmd) {
} }
state = 1; state = 1;
count = 0; count = 0;
} else if ((GraphBuffer[i] < GraphBuffer[i + 1]) && (state != 0)) { } else if ((data[i] < data[i + 1]) && (state != 0)) {
//appears redundant //appears redundant
if (state == 1) { if (state == 1) {
for (j = 0; j < count - 8; j += 16) { for (j = 0; j < count - 8; j += 16) {
@ -228,7 +230,7 @@ int CmdIndalaDemodAlt(const char *Cmd) {
} }
} }
if (rawbit>0){ if (rawbit > 0){
PrintAndLogEx(NORMAL, "Recovered %d raw bits, expected: %d", rawbit, GraphTraceLen/32); PrintAndLogEx(NORMAL, "Recovered %d raw bits, expected: %d", rawbit, GraphTraceLen/32);
PrintAndLogEx(NORMAL, "worst metric (0=best..7=worst): %d at pos %d", worst, worstPos); PrintAndLogEx(NORMAL, "worst metric (0=best..7=worst): %d at pos %d", worst, worstPos);
} else { } else {
@ -301,14 +303,14 @@ int CmdIndalaDemodAlt(const char *Cmd) {
int idx; int idx;
uid1 = uid2 = 0; uid1 = uid2 = 0;
if (uidlen==64){ if (uidlen == 64){
for( idx=0; idx<64; idx++) { for( idx=0; idx < 64; idx++) {
if (showbits[idx] == '0') { if (showbits[idx] == '0') {
uid1=(uid1<<1)|(uid2>>31); uid1 = (uid1<<1) | (uid2>>31);
uid2=(uid2<<1)|0; uid2 = (uid2<<1) | 0;
} else { } else {
uid1=(uid1<<1)|(uid2>>31); uid1 = (uid1<<1) | (uid2>>31);
uid2=(uid2<<1)|1; uid2 = (uid2<<1) | 1;
} }
} }
PrintAndLogEx(NORMAL, "UID=%s (%x%08x)", showbits, uid1, uid2); PrintAndLogEx(NORMAL, "UID=%s (%x%08x)", showbits, uid1, uid2);
@ -317,12 +319,12 @@ int CmdIndalaDemodAlt(const char *Cmd) {
uid3 = uid4 = uid5 = uid6 = uid7 = 0; uid3 = uid4 = uid5 = uid6 = uid7 = 0;
for( idx=0; idx<224; idx++) { for( idx=0; idx<224; idx++) {
uid1=(uid1<<1)|(uid2>>31); uid1 = (uid1<<1) | (uid2>>31);
uid2=(uid2<<1)|(uid3>>31); uid2 = (uid2<<1) | (uid3>>31);
uid3=(uid3<<1)|(uid4>>31); uid3 = (uid3<<1) | (uid4>>31);
uid4=(uid4<<1)|(uid5>>31); uid4 = (uid4<<1) | (uid5>>31);
uid5=(uid5<<1)|(uid6>>31); uid5 = (uid5<<1) | (uid6>>31);
uid6=(uid6<<1)|(uid7>>31); uid6 = (uid6<<1) | (uid7>>31);
if (showbits[idx] == '0') if (showbits[idx] == '0')
uid7 = (uid7<<1) | 0; uid7 = (uid7<<1) | 0;
@ -353,7 +355,7 @@ int CmdIndalaDemodAlt(const char *Cmd) {
// Remodulating for tag cloning // Remodulating for tag cloning
// HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod) // HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod)
// since this changes graphbuffer data. // since this changes graphbuffer data.
GraphTraceLen = 32*uidlen; GraphTraceLen = 32 * uidlen;
i = 0; i = 0;
int phase = 0; int phase = 0;
for (bit = 0; bit < uidlen; bit++) { for (bit = 0; bit < uidlen; bit++) {

View file

@ -83,10 +83,10 @@ int CmdIOProxRead_device(const char *Cmd) {
int CmdIOProxDemod(const char *Cmd) { int CmdIOProxDemod(const char *Cmd) {
int retval = 0; int retval = 0;
int idx = 0; int idx = 0;
uint8_t bits[MAX_GRAPH_TRACE_LEN]={0}; uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0};
size_t size = getFromGraphBuf(bits); size_t size = getFromGraphBuf(bits);
if (size < 65) { if (size < 65) {
if (g_debugMode)PrintAndLogEx(DEBUG, "DEBUG: Error - IO prox not enough samples in GraphBuffer"); if (g_debugMode) PrintAndLogEx(DEBUG, "DEBUG: Error - IO prox not enough samples in GraphBuffer");
return 0; return 0;
} }
//get binary from fsk wave //get binary from fsk wave

View file

@ -504,12 +504,13 @@ int CmdT55xxDetect(const char *Cmd){
// detect configuration? // detect configuration?
bool tryDetectModulation(){ bool tryDetectModulation(){
uint8_t hits = 0;
t55xx_conf_block_t tests[15]; t55xx_conf_block_t tests[15];
int bitRate=0; int bitRate = 0, clk = 0, firstClockEdge = 0;
uint8_t fc1 = 0, fc2 = 0, ans = 0; uint8_t hits = 0, fc1 = 0, fc2 = 0, ans = 0;
int clk = 0, firstClockEdge = 0;
ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, &firstClockEdge); ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, &firstClockEdge);
if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) { if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) {
if ( FSKrawDemod("0 0", false) && test(DEMOD_FSK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)){ if ( FSKrawDemod("0 0", false) && test(DEMOD_FSK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)){
tests[hits].modulation = DEMOD_FSK; tests[hits].modulation = DEMOD_FSK;
@ -1305,15 +1306,13 @@ bool AquireData( uint8_t page, uint8_t block, bool pwdmode, uint32_t password ){
return false; return false;
} }
//uint8_t got[12288]; uint8_t got[8000];
uint8_t got[7679]; if ( !GetFromDevice(BIG_BUF, got, sizeof(got), 0, NULL, 4000, true)) {
if ( !GetFromDevice(BIG_BUF, got, sizeof(got), 0, NULL, 6000, true)) {
PrintAndLogEx(WARNING, "command execution time out"); PrintAndLogEx(WARNING, "command execution time out");
return false; return false;
} }
setGraphBuf(got, sizeof(got)); setGraphBuf(got, sizeof(got));
return !isNoise(got, sizeof(got));
return !justNoise(GraphBuffer, sizeof(got));
} }
char * GetBitRateStr(uint32_t id, bool xmode) { char * GetBitRateStr(uint32_t id, bool xmode) {

View file

@ -105,7 +105,6 @@ int32_t compute_mean_int(int *in, size_t N) {
return mean / (int)N; return mean / (int)N;
} }
void zeromean(uint8_t* data, size_t size) { void zeromean(uint8_t* data, size_t size) {
// zero mean data // zero mean data
@ -119,13 +118,13 @@ void zeromean(uint8_t* data, size_t size) {
data[i] -= accum; data[i] -= accum;
} }
//test samples are not just noise //test samples are not just noise
// By measuring mean and look at amplitude of signal from HIGH / LOW, we can detect noise // By measuring mean and look at amplitude of signal from HIGH / LOW, we can detect noise
bool isNoise_int(int *bits, uint32_t size) { bool isNoise_int(int *bits, uint32_t size) {
resetSignal(); resetSignal();
if ( bits == NULL || size < 100 ) return true; if ( bits == NULL || size < 100 ) return true;
//zeromean(bits, size);
int32_t sum = 0; int32_t sum = 0;
for ( size_t i = 0; i < size; i++) { for ( size_t i = 0; i < size; i++) {
if ( bits[i] < signalprop.low ) signalprop.low = bits[i]; if ( bits[i] < signalprop.low ) signalprop.low = bits[i];
@ -138,7 +137,7 @@ bool isNoise_int(int *bits, uint32_t size) {
signalprop.amplitude = ABS(signalprop.high - signalprop.mean); signalprop.amplitude = ABS(signalprop.high - signalprop.mean);
signalprop.isnoise = signalprop.amplitude < NOICE_AMPLITUDE_THRESHOLD; signalprop.isnoise = signalprop.amplitude < NOICE_AMPLITUDE_THRESHOLD;
if (g_debugMode == 1) if (g_debugMode)
printSignal(); printSignal();
return signalprop.isnoise; return signalprop.isnoise;
@ -149,6 +148,7 @@ bool isNoise_int(int *bits, uint32_t size) {
bool isNoise(uint8_t *bits, uint32_t size) { bool isNoise(uint8_t *bits, uint32_t size) {
resetSignal(); resetSignal();
if ( bits == NULL || size < 100 ) return true; if ( bits == NULL || size < 100 ) return true;
zeromean(bits, size);
uint32_t sum = 0; uint32_t sum = 0;
for ( uint32_t i = 0; i < size; i++) { for ( uint32_t i = 0; i < size; i++) {
@ -162,7 +162,7 @@ bool isNoise(uint8_t *bits, uint32_t size) {
signalprop.amplitude = signalprop.high - signalprop.mean; signalprop.amplitude = signalprop.high - signalprop.mean;
signalprop.isnoise = signalprop.amplitude < NOICE_AMPLITUDE_THRESHOLD; signalprop.isnoise = signalprop.amplitude < NOICE_AMPLITUDE_THRESHOLD;
if (g_debugMode == 1) if (g_debugMode)
printSignal(); printSignal();
return signalprop.isnoise; return signalprop.isnoise;
@ -170,22 +170,20 @@ bool isNoise(uint8_t *bits, uint32_t size) {
//by marshmellow //by marshmellow
//get high and low values of a wave with passed in fuzz factor. also return noise test = 1 for passed or 0 for only noise //get high and low values of a wave with passed in fuzz factor. also return noise test = 1 for passed or 0 for only noise
int getHiLo(uint8_t *bits, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo) { //void getHiLo(uint8_t *bits, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo) {
void getHiLo(int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo) {
// just noise - no super good detection. good enough
if (signalprop.isnoise) return -1;
// add fuzz. // add fuzz.
*high = (signalprop.high * fuzzHi) / 100; *high = (signalprop.high * fuzzHi) / 100;
if ( signalprop.low < 0 ) { if ( signalprop.low < 0 ) {
*low = (signalprop.low * fuzzLo) / 100; *low = (signalprop.low * fuzzLo) / 100;
} else { } else {
*low = signalprop.low * (100 + (100 - fuzzLo))/100; uint8_t range = signalprop.high - signalprop.low;
*low = signalprop.low + ((range * (100-fuzzLo))/100);
} }
if (g_debugMode > 0) if (g_debugMode)
prnt("getHiLo fuzzed: High %d | Low %d", *high, *low); prnt("getHiLo fuzzed: High %d | Low %d", *high, *low);
return 1;
} }
// by marshmellow // by marshmellow
@ -361,13 +359,17 @@ void getNextHigh(uint8_t *samples, size_t size, int high, size_t *i) {
// load wave counters // load wave counters
bool loadWaveCounters(uint8_t *samples, size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low) { bool loadWaveCounters(uint8_t *samples, size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low) {
size_t i=0, firstLow, firstHigh; size_t i = 0, firstLow, firstHigh;
size_t testsize = (size < 512) ? size : 512; //size_t testsize = (size < 512) ? size : 512;
if ( getHiLo(samples, testsize, high, low, 80, 80) == -1 ) { // just noise - no super good detection. good enough
if (signalprop.isnoise) {
if (g_debugMode == 2) prnt("DEBUG STT: just noise detected - quitting"); if (g_debugMode == 2) prnt("DEBUG STT: just noise detected - quitting");
return false; //just noise return false;
} }
//getHiLo(samples, testsize, high, low, 80, 80);
getHiLo(high, low, 80, 80);
// get to first full low to prime loop and skip incomplete first pulse // get to first full low to prime loop and skip incomplete first pulse
getNextHigh(samples, size, *high, &i); getNextHigh(samples, size, *high, &i);
@ -469,7 +471,8 @@ int ManchesterEncode(uint8_t *bits, size_t size) {
// by marshmellow // by marshmellow
// to detect a wave that has heavily clipped (clean) samples // to detect a wave that has heavily clipped (clean) samples
uint8_t DetectCleanAskWave(uint8_t *dest, size_t size, uint8_t high, uint8_t low) { // loop 512 samples, if 300 of them is deemed maxed out, we assume the wave is clipped.
bool DetectCleanAskWave(uint8_t *dest, size_t size, uint8_t high, uint8_t low) {
bool allArePeaks = true; bool allArePeaks = true;
uint16_t cntPeaks = 0; uint16_t cntPeaks = 0;
size_t loopEnd = 512 + 160; size_t loopEnd = 512 + 160;
@ -484,8 +487,9 @@ uint8_t DetectCleanAskWave(uint8_t *dest, size_t size, uint8_t high, uint8_t low
else else
cntPeaks++; cntPeaks++;
} }
if (!allArePeaks){ if (!allArePeaks){
if (cntPeaks > 300) return true; if (cntPeaks > 250) return true;
} }
return allArePeaks; return allArePeaks;
} }
@ -504,11 +508,13 @@ int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clo
size_t i = 100; size_t i = 100;
size_t minClk = 512; size_t minClk = 512;
int shortestWaveIdx = 0; int shortestWaveIdx = 0;
if (g_debugMode == 1) prnt("DEBUG ASK: DetectStrongAskClock: hi %d | low %d ", high, low);
// get to first full low to prime loop and skip incomplete first pulse // get to first full low to prime loop and skip incomplete first pulse
getNextHigh(dest, size, high, &i); getNextHigh(dest, size, high, &i);
getNextLow(dest, size, low, &i); getNextLow(dest, size, low, &i);
// loop through all samples // loop through all samples
while (i < size) { while (i < size) {
// measure from low to low // measure from low to low
@ -516,7 +522,7 @@ int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clo
getNextHigh(dest, size, high, &i); getNextHigh(dest, size, high, &i);
getNextLow(dest, size, low, &i); getNextLow(dest, size, low, &i);
//get minimum measured distance //get minimum measured distance
if (i-startwave < minClk && i < size) { if (i-startwave < minClk && i < size) {
minClk = i - startwave; minClk = i - startwave;
@ -546,18 +552,29 @@ int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr) {
size -= 60; //sometimes there is a strange end wave - filter out this.... size -= 60; //sometimes there is a strange end wave - filter out this....
//if we already have a valid clock //if we already have a valid clock
uint8_t clockFnd = 0; uint8_t clockFnd = 0;
for (; i < clkEnd; ++i) for (; i < clkEnd; ++i) {
if (clk[i] == *clock) clockFnd = i; if (clk[i] == *clock) {
//clock found but continue to find best startpos //clock found but continue to find best startpos
clockFnd = i;
}
}
// just noise - no super good detection. good enough
if (signalprop.isnoise) {
if (g_debugMode == 2) prnt("DEBUG DetectASKClock: just noise detected - quitting");
return -1;
}
//get high and low peak //get high and low peak
int peak, low; int peak_hi, peak_low;
if (getHiLo(dest, loopCnt, &peak, &low, 75, 75) < 1) return -1; //getHiLo(dest, loopCnt, &peak_hi, &peak_low, 75, 75);
getHiLo(&peak_hi, &peak_low, 75, 75);
//test for large clean peaks //test for large clean peaks
if (!clockFnd){ if (!clockFnd){
if (DetectCleanAskWave(dest, size, peak, low)==1){ if (DetectCleanAskWave(dest, size, peak_hi, peak_low)){
int ans = DetectStrongAskClock(dest, size, peak, low, clock);
int ans = DetectStrongAskClock(dest, size, peak_hi, peak_low, clock);
if (g_debugMode == 2) prnt("DEBUG ASK: detectaskclk Clean Ask Wave Detected: clk %i, ShortestWave: %i", *clock ,ans); if (g_debugMode == 2) prnt("DEBUG ASK: detectaskclk Clean Ask Wave Detected: clk %i, ShortestWave: %i", *clock ,ans);
if (ans > 0){ if (ans > 0){
return ans; //return shortest wave start position return ans; //return shortest wave start position
@ -570,8 +587,8 @@ int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr) {
uint16_t ii; uint16_t ii;
uint8_t clkCnt, tol = 0; uint8_t clkCnt, tol = 0;
uint16_t bestErr[]={1000,1000,1000,1000,1000,1000,1000,1000,1000}; uint16_t bestErr[] = {1000,1000,1000,1000,1000,1000,1000,1000,1000};
uint8_t bestStart[]={0,0,0,0,0,0,0,0,0}; uint8_t bestStart[] = {0,0,0,0,0,0,0,0,0};
size_t errCnt = 0; size_t errCnt = 0;
size_t arrLoc, loopEnd; size_t arrLoc, loopEnd;
@ -585,9 +602,9 @@ int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr) {
//test each valid clock from smallest to greatest to see which lines up //test each valid clock from smallest to greatest to see which lines up
for (; clkCnt < clkEnd; clkCnt++) { for (; clkCnt < clkEnd; clkCnt++) {
if (clk[clkCnt] <= 32) { if (clk[clkCnt] <= 32) {
tol=1; tol = 1;
} else { } else {
tol=0; tol = 0;
} }
//if no errors allowed - keep start within the first clock //if no errors allowed - keep start within the first clock
if (!maxErr && size > clk[clkCnt]*2 + tol && clk[clkCnt] < 128) if (!maxErr && size > clk[clkCnt]*2 + tol && clk[clkCnt] < 128)
@ -597,17 +614,17 @@ int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr) {
//try lining up the peaks by moving starting point (try first few clocks) //try lining up the peaks by moving starting point (try first few clocks)
for (ii=0; ii < loopCnt; ii++){ for (ii=0; ii < loopCnt; ii++){
if (dest[ii] < peak && dest[ii] > low) continue; if (dest[ii] < peak_hi && dest[ii] > peak_low) continue;
errCnt = 0; errCnt = 0;
// now that we have the first one lined up test rest of wave array // now that we have the first one lined up test rest of wave array
loopEnd = ((size-ii-tol) / clk[clkCnt]) - 1; loopEnd = ((size-ii-tol) / clk[clkCnt]) - 1;
for (i=0; i < loopEnd; ++i){ for (i=0; i < loopEnd; ++i){
arrLoc = ii + (i * clk[clkCnt]); arrLoc = ii + (i * clk[clkCnt]);
if (dest[arrLoc] >= peak || dest[arrLoc] <= low){ if (dest[arrLoc] >= peak_hi || dest[arrLoc] <= peak_low){
}else if (dest[arrLoc-tol] >= peak || dest[arrLoc-tol] <= low){ } else if (dest[arrLoc-tol] >= peak_hi || dest[arrLoc-tol] <= peak_low){
}else if (dest[arrLoc+tol] >= peak || dest[arrLoc+tol] <= low){ } else if (dest[arrLoc+tol] >= peak_hi || dest[arrLoc+tol] <= peak_low){
}else{ //error no peak detected } else { //error no peak detected
errCnt++; errCnt++;
} }
} }
@ -618,7 +635,7 @@ int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr) {
if (!clockFnd) if (!clockFnd)
*clock = clk[clkCnt]; *clock = clk[clkCnt];
return ii; return ii;
} }
//if we found errors see if it is lowest so far and save it as best run //if we found errors see if it is lowest so far and save it as best run
if (errCnt < bestErr[clkCnt]) { if (errCnt < bestErr[clkCnt]) {
bestErr[clkCnt] = errCnt; bestErr[clkCnt] = errCnt;
@ -701,10 +718,18 @@ int DetectNRZClock(uint8_t *dest, size_t size, int clock, size_t *clockStartIdx)
// size must be larger than 20 here // size must be larger than 20 here
if (size < loopCnt) loopCnt = size-20; if (size < loopCnt) loopCnt = size-20;
// just noise - no super good detection. good enough
if (signalprop.isnoise) {
if (g_debugMode == 2) prnt("DEBUG DetectNZRClock: just noise detected - quitting");
return 0;
}
//get high and low peak //get high and low peak
int peak, low; int peak, low;
if (getHiLo(dest, loopCnt, &peak, &low, 90, 90) < 1) return 0; //getHiLo(dest, loopCnt, &peak, &low, 90, 90);
getHiLo(&peak, &low, 90, 90);
bool strong = false; bool strong = false;
int lowestTransition = DetectStrongNRZClk(dest, size-20, peak, low, &strong); int lowestTransition = DetectStrongNRZClk(dest, size-20, peak, low, &strong);
if (strong) return lowestTransition; if (strong) return lowestTransition;
@ -1431,11 +1456,18 @@ int cleanAskRawDemod(uint8_t *bits, size_t *size, int clk, int invert, int high,
//by marshmellow //by marshmellow
//attempts to demodulate ask modulations, askType == 0 for ask/raw, askType==1 for ask/manchester //attempts to demodulate ask modulations, askType == 0 for ask/raw, askType==1 for ask/manchester
int askdemod_ext(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType, int *startIdx) { int askdemod_ext(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType, int *startIdx) {
if (*size==0) return -1;
int start = DetectASKClock(bits, *size, clk, maxErr); //clock default if (*size == 0) return -1;
if (*clk==0 || start < 0) return -3;
int start = DetectASKClock(bits, *size, clk, maxErr);
if (*clk == 0 || start < 0) return -3;
if (*invert != 1) *invert = 0; if (*invert != 1) *invert = 0;
if (amp==1) askAmp(bits, *size);
// amplify signal data.
// ICEMAN todo,
if (amp == 1) askAmp(bits, *size);
if (g_debugMode == 2) prnt("DEBUG ASK: clk %d, beststart %d, amp %d", *clk, start, amp); if (g_debugMode == 2) prnt("DEBUG ASK: clk %d, beststart %d, amp %d", *clk, start, amp);
//start pos from detect ask clock is 1/2 clock offset //start pos from detect ask clock is 1/2 clock offset
@ -1443,12 +1475,19 @@ int askdemod_ext(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr,
*startIdx = start - (*clk/2); *startIdx = start - (*clk/2);
uint16_t initLoopMax = 1024; uint16_t initLoopMax = 1024;
if (initLoopMax > *size) initLoopMax = *size; if (initLoopMax > *size) initLoopMax = *size;
// just noise - no super good detection. good enough
if (signalprop.isnoise) {
if (g_debugMode == 2) prnt("DEBUG askdemod_ext: just noise detected - quitting");
return -2;
}
// Detect high and lows // Detect high and lows
//25% clip in case highs and lows aren't clipped [marshmellow] //25% clip in case highs and lows aren't clipped [marshmellow]
int high, low; int high, low;
if (getHiLo(bits, initLoopMax, &high, &low, 75, 75) < 1) //getHiLo(bits, initLoopMax, &high, &low, 75, 75);
return -2; //just noise getHiLo(&high, &low, 75, 75);
size_t errCnt = 0; size_t errCnt = 0;
// if clean clipped waves detected run alternate demod // if clean clipped waves detected run alternate demod
if (DetectCleanAskWave(bits, *size, high, low)) { if (DetectCleanAskWave(bits, *size, high, low)) {
@ -1461,11 +1500,13 @@ int askdemod_ext(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr,
uint8_t alignPos = 0; uint8_t alignPos = 0;
errCnt = manrawdecode(bits, size, 0, &alignPos); errCnt = manrawdecode(bits, size, 0, &alignPos);
*startIdx += *clk/2 * alignPos; *startIdx += *clk/2 * alignPos;
if (g_debugMode) if (g_debugMode)
prnt("DEBUG: (askdemod_ext) CLEAN: startIdx %i, alignPos %u", *startIdx, alignPos); prnt("DEBUG: (askdemod_ext) CLEAN: startIdx %i, alignPos %u", *startIdx, alignPos);
} }
return errCnt; return errCnt;
} }
if (g_debugMode) prnt("DEBUG: (askdemod_ext) Weak wave detected: startIdx %i", *startIdx); if (g_debugMode) prnt("DEBUG: (askdemod_ext) Weak wave detected: startIdx %i", *startIdx);
int lastBit; //set first clock check - can go negative int lastBit; //set first clock check - can go negative
@ -1477,28 +1518,28 @@ int askdemod_ext(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr,
lastBit = start - *clk; lastBit = start - *clk;
for (i = start; i < *size; ++i) { for (i = start; i < *size; ++i) {
if (i-lastBit >= *clk-tol){ if (i - lastBit >= *clk - tol){
if (bits[i] >= high) { if (bits[i] >= high) {
bits[bitnum++] = *invert; bits[bitnum++] = *invert;
} else if (bits[i] <= low) { } else if (bits[i] <= low) {
bits[bitnum++] = *invert ^ 1; bits[bitnum++] = *invert ^ 1;
} else if (i-lastBit >= *clk+tol) { } else if (i-lastBit >= *clk + tol) {
if (bitnum > 0) { if (bitnum > 0) {
if (g_debugMode == 2) prnt("DEBUG: (askdemod_ext) Modulation Error at: %u", i); if (g_debugMode == 2) prnt("DEBUG: (askdemod_ext) Modulation Error at: %u", i);
bits[bitnum++]=7; bits[bitnum++] = 7;
errCnt++; errCnt++;
} }
} else { //in tolerance - looking for peak } else { //in tolerance - looking for peak
continue; continue;
} }
midBit = 0; midBit = 0;
lastBit += *clk; lastBit += *clk;
} else if (i-lastBit >= (*clk/2-tol) && !midBit && !askType){ } else if (i-lastBit >= (*clk/2 - tol) && !midBit && !askType){
if (bits[i] >= high) { if (bits[i] >= high) {
bits[bitnum++] = *invert; bits[bitnum++] = *invert;
} else if (bits[i] <= low) { } else if (bits[i] <= low) {
bits[bitnum++] = *invert ^ 1; bits[bitnum++] = *invert ^ 1;
} else if (i-lastBit >= *clk/2+tol) { } else if (i-lastBit >= *clk/2 + tol) {
bits[bitnum] = bits[bitnum-1]; bits[bitnum] = bits[bitnum-1];
bitnum++; bitnum++;
} else { //in tolerance - looking for peak } else { //in tolerance - looking for peak
@ -1530,8 +1571,17 @@ int nrzRawDemod(uint8_t *dest, size_t *size, int *clk, int *invert, int *startId
if (gLen > *size) if (gLen > *size)
gLen = *size-20; gLen = *size-20;
// just noise - no super good detection. good enough
if (signalprop.isnoise) {
if (g_debugMode == 2) prnt("DEBUG nrzRawDemod: just noise detected - quitting");
return -3;
}
int high, low; int high, low;
if (getHiLo(dest, gLen, &high, &low, 75, 75) < 1) return -3; //25% fuzz on high 25% fuzz on low //getHiLo(dest, gLen, &high, &low, 75, 75);
getHiLo(&high, &low, 75, 75);
getHiLo(&high, &low, 75, 75);
uint8_t bit=0; uint8_t bit=0;
//convert wave samples to 1's and 0's //convert wave samples to 1's and 0's

View file

@ -35,14 +35,7 @@ extern int32_t compute_mean_int(int *in, size_t N);
bool isNoise_int(int *bits, uint32_t size); bool isNoise_int(int *bits, uint32_t size);
bool isNoise(uint8_t *bits, uint32_t size); bool isNoise(uint8_t *bits, uint32_t size);
extern void zeromean(uint8_t* data, size_t size); extern void zeromean(uint8_t* data, size_t size);
// buffer is unsigned on DEVIE
#ifdef ON_DEVICE
#define justNoise(a, b) isNoise((a), (b))
#else
#define justNoise(a, b) isNoise_int((a), (b))
#endif
void getNextLow(uint8_t *samples, size_t size, int low, size_t *i); void getNextLow(uint8_t *samples, size_t size, int low, size_t *i);
void getNextHigh(uint8_t *samples, size_t size, int high, size_t *i); void getNextHigh(uint8_t *samples, size_t size, int high, size_t *i);
bool loadWaveCounters(uint8_t *samples, size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low); bool loadWaveCounters(uint8_t *samples, size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low);
@ -58,14 +51,15 @@ extern uint32_t bytebits_to_byte(uint8_t *src, size_t numbits);
extern uint32_t bytebits_to_byteLSBF(uint8_t *src, size_t numbits); extern uint32_t bytebits_to_byteLSBF(uint8_t *src, size_t numbits);
extern uint16_t countFC(uint8_t *bits, size_t size, uint8_t fskAdj); extern uint16_t countFC(uint8_t *bits, size_t size, uint8_t fskAdj);
extern int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr); extern int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr);
extern uint8_t DetectCleanAskWave(uint8_t *dest, size_t size, uint8_t high, uint8_t low); extern bool DetectCleanAskWave(uint8_t *dest, size_t size, uint8_t high, uint8_t low);
extern uint8_t detectFSKClk(uint8_t *bits, size_t size, uint8_t fcHigh, uint8_t fcLow, int *firstClockEdge); extern uint8_t detectFSKClk(uint8_t *bits, size_t size, uint8_t fcHigh, uint8_t fcLow, int *firstClockEdge);
extern int DetectNRZClock(uint8_t *dest, size_t size, int clock, size_t *clockStartIdx); extern int DetectNRZClock(uint8_t *dest, size_t size, int clock, size_t *clockStartIdx);
extern int DetectPSKClock(uint8_t *dest, size_t size, int clock, size_t *firstPhaseShift, uint8_t *curPhase, uint8_t *fc); extern int DetectPSKClock(uint8_t *dest, size_t size, int clock, size_t *firstPhaseShift, uint8_t *curPhase, uint8_t *fc);
extern int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clock); extern int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clock);
extern bool DetectST(uint8_t *buffer, size_t *size, int *foundclock, size_t *ststart, size_t *stend); extern bool DetectST(uint8_t *buffer, size_t *size, int *foundclock, size_t *ststart, size_t *stend);
extern size_t fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow, int *startIdx); extern size_t fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow, int *startIdx);
extern int getHiLo(uint8_t *bits, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo); //extern void getHiLo(uint8_t *bits, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo);
extern void getHiLo(int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo);
extern uint32_t manchesterEncode2Bytes(uint16_t datain); extern uint32_t manchesterEncode2Bytes(uint16_t datain);
extern int ManchesterEncode(uint8_t *bits, size_t size); extern int ManchesterEncode(uint8_t *bits, size_t size);
extern int manrawdecode(uint8_t *bits, size_t *size, uint8_t invert, uint8_t *alignPos); extern int manrawdecode(uint8_t *bits, size_t *size, uint8_t invert, uint8_t *alignPos);