]> git.zerfleddert.de Git - proxmark3-svn/commitdiff
FIX: @marshmellow42 's fixes for enhanced STT and AskDemod. Now the "lf presco...
authoriceman1001 <iceman@iuse.se>
Fri, 20 Jan 2017 09:03:53 +0000 (10:03 +0100)
committericeman1001 <iceman@iuse.se>
Fri, 20 Jan 2017 09:03:53 +0000 (10:03 +0100)
client/cmddata.c
client/cmdlfpresco.c
common/lfdemod.c
common/lfdemod.h

index 1d491f5fad77e0fdf5cf21261e59d731b1d3878b..ece0810d3b2c5bcf4f6bc2d8bbfcbf226e6ce191 100644 (file)
@@ -524,7 +524,6 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType,
        int clk = 0;
        int maxErr = 100;
        int maxLen = 0;
-       uint8_t askAmp = 0;
        char amp = param_getchar(Cmd, 0);
        uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0};
        sscanf(Cmd, "%i %i %i %i %c", &clk, &invert, &maxErr, &maxLen, &amp);
@@ -537,12 +536,15 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType,
                invert=1;
                clk=0;
        }
-       if (amp == 'a' || amp == 'A') askAmp=1; 
        size_t BitLen = getFromGraphBuf(BitStream);
        if (g_debugMode) PrintAndLog("DEBUG: Bitlen from grphbuff: %d", BitLen);
        if (BitLen<255) return 0;
        if (maxLen<BitLen && maxLen != 0) BitLen = maxLen;
        int foundclk = 0;
+       //amp before ST check
+       if (amp == 'a' || amp == 'A') {
+               askAmp(BitStream, BitLen); 
+       }
        bool st = false;
        if (*stCheck) st = DetectST(BitStream, &BitLen, &foundclk);
        if (st) {
@@ -550,7 +552,7 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType,
                clk = (clk == 0) ? foundclk : clk;
                if (verbose || g_debugMode) PrintAndLog("\nFound Sequence Terminator");
        }
-       int errCnt = askdemod(BitStream, &BitLen, &clk, &invert, maxErr, askAmp, askType);
+       int errCnt = askdemod(BitStream, &BitLen, &clk, &invert, maxErr, 0, askType);
        if (errCnt<0 || BitLen<16){  //if fatal error (or -1)
                if (g_debugMode) PrintAndLog("DEBUG: no data found %d, errors:%d, bitlen:%d, clock:%d",errCnt,invert,BitLen,clk);
                return 0;
@@ -592,7 +594,7 @@ int ASKDemod(const char *Cmd, bool verbose, bool emSearch, uint8_t askType) {
 int Cmdaskmandemod(const char *Cmd)
 {
        char cmdp = param_getchar(Cmd, 0);
-       if (strlen(Cmd) > 25 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_am();
+       if (strlen(Cmd) > 45 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_am();
 
        bool st = TRUE;
        if (Cmd[0]=='s') 
@@ -1152,7 +1154,7 @@ int FSKrawDemod(const char *Cmd, bool verbose)
 int CmdFSKrawdemod(const char *Cmd)
 {
        char cmdp = param_getchar(Cmd, 0);
-       if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_fs();
+       if (strlen(Cmd) > 20 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_fs();
 
        return FSKrawDemod(Cmd, TRUE);
 }
@@ -2001,7 +2003,7 @@ int NRZrawDemod(const char *Cmd, bool verbose)
 int CmdNRZrawDemod(const char *Cmd)
 {
        char cmdp = param_getchar(Cmd, 0);
-       if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_nr();
+       if (strlen(Cmd) > 16 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_nr();
 
        return NRZrawDemod(Cmd, TRUE);
 }
@@ -2014,7 +2016,7 @@ int CmdPSK1rawDemod(const char *Cmd)
 {
        int ans;
        char cmdp = param_getchar(Cmd, 0);
-       if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_p1();
+       if (strlen(Cmd) > 16 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_p1();
 
        ans = PSKDemod(Cmd, TRUE);
        //output
@@ -2034,7 +2036,7 @@ int CmdPSK2rawDemod(const char *Cmd)
 {
        int ans = 0;
        char cmdp = param_getchar(Cmd, 0);
-       if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H')     return usage_data_rawdemod_p2();
+       if (strlen(Cmd) > 16 || cmdp == 'h' || cmdp == 'H')     return usage_data_rawdemod_p2();
 
        ans = PSKDemod(Cmd, TRUE);
        if (!ans){
@@ -2055,7 +2057,7 @@ int CmdRawDemod(const char *Cmd)
        char cmdp2 = Cmd[1];
        int ans = 0;
 
-       if (strlen(Cmd) > 20 || cmdp == 'h' || cmdp == 'H' || strlen(Cmd) < 2)
+       if (strlen(Cmd) > 35 || cmdp == 'h' || cmdp == 'H' || strlen(Cmd) < 2)
                return usage_data_rawdemod();
 
        if (cmdp == 'f' && cmdp2 == 's')
@@ -2316,10 +2318,9 @@ int CmdLoad(const char *Cmd)
                GraphBuffer[GraphTraceLen] = atoi(line);
                GraphTraceLen++;
        }
-       if (f) {
+       if (f)
                fclose(f);
-               f = NULL;
-       }
+
        PrintAndLog("loaded %d samples", GraphTraceLen);
        RepaintGraphWindow();
        return 0;
@@ -2392,10 +2393,9 @@ int CmdSave(const char *Cmd)
        for (int i = 0; i < GraphTraceLen; i++)
                fprintf(f, "%d\n", GraphBuffer[i]);
 
-       if (f) {
+       if (f)
                fclose(f);
-               f = NULL;
-       }
+
        PrintAndLog("saved to '%s'", Cmd);
        return 0;
 }
index 47ff7db3e4ab6e4ac0d94287cb6fed071c713ef4..accd6f22ebb624729756d4e3134d8d8550a8f8f9 100644 (file)
@@ -116,7 +116,7 @@ int GetPrescoBits(uint32_t fullcode, uint8_t *prescoBits) {
 //see ASKDemod for what args are accepted
 int CmdPrescoDemod(const char *Cmd) {
        bool st = true;
-       if (!ASKDemod_ext("32 0 0", FALSE, FALSE, 1, &st)) {
+       if (!ASKDemod_ext("32 0 0 0 0 a", FALSE, FALSE, 1, &st)) {
                if (g_debugMode) PrintAndLog("DEBUG: Error Presco ASKDemod failed");
                return 0;
        }
index 4cf22d8667359942c30980e8fa62f14699d204d1..8b951dc86abdb5464d4b8cc44535ae038bac4bec 100644 (file)
@@ -60,7 +60,7 @@ uint8_t parityTest(uint32_t bits, uint8_t bitLen, uint8_t pType)
        for (uint8_t i = 0; i < bitLen; i++){
                ans ^= ((bits >> i) & 1);
        }
-       //PrintAndLog("DEBUG: ans: %d, ptype: %d",ans,pType);
+       //prnt("DEBUG: ans: %d, ptype: %d",ans,pType);
        return (ans == pType);
 }
 
@@ -220,7 +220,7 @@ int cleanAskRawDemod(uint8_t *BinStream, size_t *size, int clk, int invert, int
                        if ((BinStream[i] >= high && !waveHigh) || (BinStream[i] <= low && waveHigh)){
 
                                if (smplCnt > clk-(clk/4)-1) { //full clock
-                                       if (smplCnt > clk + (clk/4)+2) { //too many samples
+                                       if (smplCnt > clk + (clk/4)+1) { //too many samples
                                                errCnt++;
                                                if (g_debugMode==2) prnt("DEBUG ASK: Modulation Error at: %u", i);
                                                BinStream[bitCnt++] = 7;
@@ -281,7 +281,7 @@ int askdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr
        if (*clk==0 || start < 0) return -3;
        if (*invert != 1) *invert = 0;
        if (amp==1) askAmp(BinStream, *size);
-       if (g_debugMode==2) prnt("DEBUG ASK: size %d, clk %d, beststart %d", *size, *clk, start);
+       if (g_debugMode==2) prnt("DEBUG ASK: clk %d, beststart %d, amp %d", *clk, start, amp);
 
        uint8_t initLoopMax = 255;
        if (initLoopMax > *size) initLoopMax = *size;
@@ -537,7 +537,7 @@ size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t rfLen,
        uint32_t n=1;
        for( idx=1; idx < size; idx++) {
                n++;
-               if (dest[idx]==lastval) continue; 
+               if (dest[idx]==lastval) continue; //skip until we hit a transition
                
                //find out how many bits (n) we collected
                //if lastval was 1, we have a 1->0 crossing
@@ -1355,7 +1355,7 @@ uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fc
        fcCounter=0;
        rfCounter=0;
        firstBitFnd=0;
-       //PrintAndLog("DEBUG: fcTol: %d",fcTol);
+       //prnt("DEBUG: fcTol: %d",fcTol);
        // prime i to first peak / up transition
        for (i = 160; i < size-20; i++)
                if (BitStream[i] > BitStream[i-1] && BitStream[i]>=BitStream[i+1])
@@ -1386,7 +1386,7 @@ uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fc
                                        }
                                }
                                if (rfCounter > 0 && rfLensFnd < 15){
-                                       //PrintAndLog("DEBUG: rfCntr %d, fcCntr %d",rfCounter,fcCounter);
+                                       //prnt("DEBUG: rfCntr %d, fcCntr %d",rfCounter,fcCounter);
                                        rfCnts[rfLensFnd]++;
                                        rfLens[rfLensFnd++] = rfCounter;
                                }
@@ -1544,7 +1544,7 @@ int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert)
        uint16_t errCnt=0, waveLenCnt=0;
        fc = countFC(dest, *size, 0);
        if (fc!=2 && fc!=4 && fc!=8) return -1;
-       //PrintAndLog("DEBUG: FC: %d",fc);
+       //prnt("DEBUG: FC: %d",fc);
        *clock = DetectPSKClock(dest, *size, *clock);
        if (*clock == 0) return -1;
        int avgWaveVal=0, lastAvgWaveVal=0;
@@ -1552,7 +1552,7 @@ int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert)
        for (i=0; i<loopCnt; i++){
                if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){
                        waveEnd = i+1;
-                       //PrintAndLog("DEBUG: waveEnd: %d",waveEnd);
+                       //prnt("DEBUG: waveEnd: %d",waveEnd);
                        waveLenCnt = waveEnd-waveStart;
                        if (waveLenCnt > fc && waveStart > fc && !(waveLenCnt > fc+2)){ //not first peak and is a large wave but not out of whack
                                lastAvgWaveVal = avgWaveVal/(waveLenCnt);
@@ -1595,9 +1595,9 @@ int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert)
                                waveLenCnt = waveEnd-waveStart;
                                lastAvgWaveVal = avgWaveVal/waveLenCnt;
                                if (waveLenCnt > fc){  
-                                       //PrintAndLog("DEBUG: avgWaveVal: %d, waveSum: %d",lastAvgWaveVal,avgWaveVal);
+                                       //prnt("DEBUG: avgWaveVal: %d, waveSum: %d",lastAvgWaveVal,avgWaveVal);
                                        //this wave is a phase shift
-                                       //PrintAndLog("DEBUG: phase shift at: %d, len: %d, nextClk: %d, i: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+*clock-tol,i+1,fc);
+                                       //prnt("DEBUG: phase shift at: %d, len: %d, nextClk: %d, i: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+*clock-tol,i+1,fc);
                                        if (i+1 >= lastClkBit + *clock - tol){ //should be a clock bit
                                                curPhase ^= 1;
                                                dest[numBits++] = curPhase;
@@ -1632,13 +1632,15 @@ bool DetectST(uint8_t buffer[], size_t *size, int *foundclock) {
        int tol = 0;
        int i, j, skip, start, end, low, high, minClk, waveStart;
        bool complete = false;
-       int tmpbuff[bufsize / 64];
-       int waveLen[bufsize / 64];
+       int tmpbuff[bufsize / 32]; //guess rf/32 clock, if click is smaller we will only have room for a fraction of the samples captured
+       int waveLen[bufsize / 32]; //  if clock is larger then we waste memory in array size that is not needed...
        size_t testsize = (bufsize < 512) ? bufsize : 512;
        int phaseoff = 0;
        high = low = 128;
        memset(tmpbuff, 0, sizeof(tmpbuff));
+       memset(waveLen, 0, sizeof(waveLen));
 
+       
        if ( getHiLo(buffer, testsize, &high, &low, 80, 80) == -1 ) {
                if (g_debugMode==2) prnt("DEBUG STT: just noise detected - quitting");
                return false; //just noise
@@ -1665,7 +1667,7 @@ bool DetectST(uint8_t buffer[], size_t *size, int *foundclock) {
                waveStart = i;
                while ((buffer[i] > low) && (i < bufsize))
                        ++i;
-               if (j >= (bufsize/64)) {
+               if (j >= (bufsize/32)) {
                        break;
                }
                waveLen[j] = i - waveStart; //first high to first low
@@ -1711,6 +1713,8 @@ bool DetectST(uint8_t buffer[], size_t *size, int *foundclock) {
        if (start < 0) {
                if (g_debugMode==2) prnt("DEBUG STT: first STT not found - quitting");
                return false;
+       } else {
+               if (g_debugMode==2) prnt("DEBUG STT: first STT found at: %d, j=%d",start, j);
        }
        if (waveLen[i+2] > clk*1+tol)
                phaseoff = 0;
@@ -1724,7 +1728,7 @@ bool DetectST(uint8_t buffer[], size_t *size, int *foundclock) {
        end = skip;
        for (i += 3; i < j - 4; ++i) {
                end += tmpbuff[i];
-               if (tmpbuff[i] >= clk*1-tol && tmpbuff[i] <= (clk*2)+tol) {           //1 to 2 clocks depending on 2 bits prior
+               if (tmpbuff[i] >= clk*1-tol && tmpbuff[i] <= (clk*2)+tol && waveLen[i] < clk+tol) {           //1 to 2 clocks depending on 2 bits prior
                        if (tmpbuff[i+1] >= clk*2-tol && tmpbuff[i+1] <= clk*2+tol && waveLen[i+1] > clk*3/2-tol) {       //2 clocks and wave size is 1 1/2
                                if (tmpbuff[i+2] >= (clk*3)/2-tol && tmpbuff[i+2] <= clk*2+tol && waveLen[i+2] > clk-tol) { //1 1/2 to 2 clocks and at least one full clock wave
                                        if (tmpbuff[i+3] >= clk*1-tol && tmpbuff[i+3] <= clk*2+tol) { //1 to 2 clocks for end of ST + first bit
@@ -1746,12 +1750,15 @@ bool DetectST(uint8_t buffer[], size_t *size, int *foundclock) {
        start = skip;
        size_t datalen = end - start;
        // check validity of datalen (should be even clock increments)  - use a tolerance of up to 1/8th a clock
-       if (datalen % clk > clk/8) {
+       if ( clk - (datalen % clk) <= clk/8) {
+               // padd the amount off - could be problematic...  but shouldn't happen often
+               datalen += clk - (datalen % clk);
+       } else if ( (datalen % clk) <= clk/8 ) {
+               // padd the amount off - could be problematic...  but shouldn't happen often
+               datalen -= datalen % clk;
+       } else {
                if (g_debugMode==2) prnt("DEBUG STT: datalen not divisible by clk: %u %% %d = %d - quitting", datalen, clk, datalen % clk);
                return false;
-       } else {
-               // padd the amount off - could be problematic...  but shouldn't happen often
-               datalen += datalen % clk;
        }
        // if datalen is less than one t55xx block - ERROR
        if (datalen/clk < 8*4) {
@@ -1759,8 +1766,20 @@ bool DetectST(uint8_t buffer[], size_t *size, int *foundclock) {
                return false;
        }
        size_t dataloc = start;
+       if (buffer[dataloc-(clk*4)-(clk/8)] <= low && buffer[dataloc] <= low && buffer[dataloc-(clk*4)] >= high) {
+               //we have low drift (and a low just before the ST and a low just after the ST) - compensate by backing up the start 
+               for ( i=0; i <= (clk/8); ++i ) {
+                       if ( buffer[dataloc - (clk*4) - i] <= low ) {
+                               dataloc -= i;
+                               break;
+                       }
+               }
+       }
+       
        size_t newloc = 0;
        i=0;
+       if (g_debugMode==2) prnt("DEBUG STT: Starting STT trim - start: %d, datalen: %d ",dataloc, datalen);            
+
        // warning - overwriting buffer given with raw wave data with ST removed...
        while ( dataloc < bufsize-(clk/2) ) {
                //compensate for long high at end of ST not being high due to signal loss... (and we cut out the start of wave high part)
@@ -1779,6 +1798,7 @@ bool DetectST(uint8_t buffer[], size_t *size, int *foundclock) {
                }
                newloc += i;
                //skip next ST  -  we just assume it will be there from now on...
+               if (g_debugMode==2) prnt("DEBUG STT: skipping STT at %d to %d", dataloc, dataloc+(clk*4));
                dataloc += clk*4;
        }
        *size = newloc;
index 7bd3ce405faf85e3758dd676eb21738527bb1a07..7b25a02a004f6ad445b2c6f7f5a770362ff31be7 100644 (file)
@@ -20,6 +20,7 @@
 uint8_t  justNoise(uint8_t *bits, size_t size);
 size_t   addParity(uint8_t *BitSource, uint8_t *dest, uint8_t sourceLen, uint8_t pLen, uint8_t pType);
 int      askdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType);
+void     askAmp(uint8_t *BitStream, size_t size);
 int      BiphaseRawDecode(uint8_t * BitStream, size_t *size, int offset, int invert);
 uint32_t bytebits_to_byte(uint8_t* src, size_t numbits);
 uint32_t bytebits_to_byteLSBF(uint8_t* src, size_t numbits);
Impressum, Datenschutz