- // get high/low thresholds
- int high, low;
- getHiLo(BitStream,10, &high, &low, 100, 100);
- // get zero crossing
- uint8_t zeroC = (high-low)/2+low;
- uint8_t clk[]={8,16,32,40,50,64,100,128};
- uint8_t fcLens[] = {0,0,0,0,0,0,0,0,0,0};
- uint16_t fcCnts[] = {0,0,0,0,0,0,0,0,0,0};
- uint8_t rfLens[] = {0,0,0,0,0,0,0,0,0,0,0};
- // uint8_t rfCnts[] = {0,0,0,0,0,0,0,0,0,0};
- uint8_t fcLensFnd = 0;
- uint8_t rfLensFnd = 0;
- uint8_t lastBit=0;
- uint8_t curBit=0;
- uint8_t lastFCcnt=0;
- uint32_t errCnt=0;
- uint32_t fcCounter = 0;
- uint32_t rfCounter = 0;
- uint8_t firstBitFnd = 0;
- int i;
-
- // prime i to first up transition
- for (i = 1; i < size; i++)
- if (BitStream[i]>=zeroC && BitStream[i-1]<zeroC)
- break;
-
- for (; i < size; i++){
- curBit = BitStream[i];
- lastBit = BitStream[i-1];
- if (lastBit<zeroC && curBit >= zeroC){
- // new up transition
- fcCounter++;
- rfCounter++;
- if (fcCounter > 3 && fcCounter < 256){
- //we've counted enough that it could be a valid field clock
-
- //if we had 5 and now have 9 then go back to 8 (for when we get a fc 9 instead of an 8)
- if (lastFCcnt==5 && fcCounter==9) fcCounter--;
- //if odd and not rc/5 add one (for when we get a fc 9 instead of 10)
- if ((fcCounter==9 && fcCounter & 1) || fcCounter==4) fcCounter++;
-
- //look for bit clock (rf/xx)
- if ((fcCounter<lastFCcnt || fcCounter>lastFCcnt)){
- //not the same size as the last wave - start of new bit sequence
-
- if (firstBitFnd>1){ //skip first wave change - probably not a complete bit
- for (int ii=0; ii<10; ii++){
- if (rfLens[ii]==rfCounter){
- //rfCnts[ii]++;
- rfCounter=0;
- break;
- }
- }
- if (rfCounter>0 && rfLensFnd<10){
- //PrintAndLog("DEBUG: rfCntr %d, fcCntr %d",rfCounter,fcCounter);
- //rfCnts[rfLensFnd]++;
- rfLens[rfLensFnd++]=rfCounter;
- }
- } else {
- //PrintAndLog("DEBUG i: %d",i);
- firstBitFnd++;
- }
- rfCounter=0;
- lastFCcnt=fcCounter;
- }
-
- // save last field clock count (fc/xx)
- // find which fcLens to save it to:
- for (int ii=0; ii<10; ii++){
- if (fcLens[ii]==fcCounter){
- fcCnts[ii]++;
- fcCounter=0;
- break;
- }
- }
- if (fcCounter>0 && fcLensFnd<10){
- //add new fc length
- //PrintAndLog("FCCntr %d",fcCounter);
- fcCnts[fcLensFnd]++;
- fcLens[fcLensFnd++]=fcCounter;
- }
- } else{
- // hmmm this should not happen often - count them
- errCnt++;
- }
- // reset counter
- fcCounter=0;
- } else {
- // count sample
- fcCounter++;
- rfCounter++;
- }
- }
- // if too many errors return errors as negative number (IS THIS NEEDED?)
- if (errCnt>100) return -1*errCnt;
-
- uint8_t maxCnt1=0, best1=9, best2=9, best3=9, rfHighest=10, rfHighest2=10, rfHighest3=10;
-
- // go through fclens and find which ones are bigest 2
- for (i=0; i<10; i++){
- // PrintAndLog("DEBUG: FC %d, Cnt %d, Errs %d, RF %d",fcLens[i],fcCnts[i],errCnt,rfLens[i]);
-
- // get the 3 best FC values
- if (fcCnts[i]>maxCnt1) {
- best3=best2;
- best2=best1;
- maxCnt1=fcCnts[i];
- best1=i;
- } else if(fcCnts[i]>fcCnts[best2]){
- best3=best2;
- best2=i;
- } else if(fcCnts[i]>fcCnts[best3]){
- best3=i;
- }
- //get highest 2 RF values (might need to get more values to compare or compare all?)
- if (rfLens[i]>rfLens[rfHighest]){
- rfHighest3=rfHighest2;
- rfHighest2=rfHighest;
- rfHighest=i;
- } else if(rfLens[i]>rfLens[rfHighest2]){
- rfHighest3=rfHighest2;
- rfHighest2=i;
- } else if(rfLens[i]>rfLens[rfHighest3]){
- rfHighest3=i;
- }
- }
-
- // set allowed clock remainder tolerance to be 1 large field clock length
- // we could have mistakenly made a 9 a 10 instead of an 8 or visa versa so rfLens could be 1 FC off
- int tol1 = (fcLens[best1]>fcLens[best2]) ? fcLens[best1] : fcLens[best2];
-
- // loop to find the highest clock that has a remainder less than the tolerance
- // compare samples counted divided by
- int ii=7;
- for (; ii>=0; ii--){
- if (rfLens[rfHighest] % clk[ii] < tol1 || rfLens[rfHighest] % clk[ii] > clk[ii]-tol1){
- if (rfLens[rfHighest2] % clk[ii] < tol1 || rfLens[rfHighest2] % clk[ii] > clk[ii]-tol1){
- if (rfLens[rfHighest3] % clk[ii] < tol1 || rfLens[rfHighest3] % clk[ii] > clk[ii]-tol1){
- break;
- }
- }
- }
- }
-
- if (ii<0) ii=7; // oops we went too far
-
- // TODO: take top 3 answers and compare to known Field clocks to get top 2
-
- uint32_t fcs=0;
- // PrintAndLog("DEBUG: Best %d best2 %d best3 %d, clk %d, clk2 %d",fcLens[best1],fcLens[best2],fcLens[best3],clk[i],clk[ii]);
- //
-
- if (fcLens[best1]>fcLens[best2]){
- fcs = (((uint32_t)clk[ii])<<16) | (((uint32_t)fcLens[best1])<<8) | ((fcLens[best2]));
- } else {
- fcs = (((uint32_t)clk[ii])<<16) | (((uint32_t)fcLens[best2])<<8) | ((fcLens[best1]));
- }
-
- return fcs;
+ if (size == 0) return -1;
+ uint16_t loopCnt = 4096; //don't need to loop through entire array...
+ if (*size<loopCnt) loopCnt = *size;
+
+ size_t numBits=0;
+ uint8_t curPhase = *invert;
+ size_t i, waveStart=1, waveEnd=0, firstFullWave=0, lastClkBit=0;
+ uint8_t fc=0, fullWaveLen=0, tol=1;
+ 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);
+ *clock = DetectPSKClock(dest, *size, *clock);
+ if (*clock == 0) return -1;
+ int avgWaveVal=0, lastAvgWaveVal=0;
+ //find first phase shift
+ 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);
+ 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);
+ firstFullWave = waveStart;
+ fullWaveLen=waveLenCnt;
+ //if average wave value is > graph 0 then it is an up wave or a 1
+ if (lastAvgWaveVal > 123) curPhase ^= 1; //fudge graph 0 a little 123 vs 128
+ break;
+ }
+ waveStart = i+1;
+ avgWaveVal = 0;
+ }
+ avgWaveVal += dest[i+2];
+ }
+ if (firstFullWave == 0) {
+ // no phase shift detected - could be all 1's or 0's - doesn't matter where we start
+ // so skip a little to ensure we are past any Start Signal
+ firstFullWave = 160;
+ memset(dest, curPhase, firstFullWave / *clock);
+ } else {
+ memset(dest, curPhase^1, firstFullWave / *clock);
+ }
+ //advance bits
+ numBits += (firstFullWave / *clock);
+ //set start of wave as clock align
+ lastClkBit = firstFullWave;
+ //PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);
+ //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d", *clock, lastClkBit);
+ waveStart = 0;
+ dest[numBits++] = curPhase; //set first read bit
+ for (i = firstFullWave + fullWaveLen - 1; i < *size-3; i++){
+ //top edge of wave = start of new wave
+ if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){
+ if (waveStart == 0) {
+ waveStart = i+1;
+ waveLenCnt = 0;
+ avgWaveVal = dest[i+1];
+ } else { //waveEnd
+ waveEnd = i+1;
+ waveLenCnt = waveEnd-waveStart;
+ lastAvgWaveVal = avgWaveVal/waveLenCnt;
+ if (waveLenCnt > fc){
+ //PrintAndLog("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);
+ if (i+1 >= lastClkBit + *clock - tol){ //should be a clock bit
+ curPhase ^= 1;
+ dest[numBits++] = curPhase;
+ lastClkBit += *clock;
+ } else if (i < lastClkBit+10+fc){
+ //noise after a phase shift - ignore
+ } else { //phase shift before supposed to based on clock
+ errCnt++;
+ dest[numBits++] = 7;
+ }
+ } else if (i+1 > lastClkBit + *clock + tol + fc){
+ lastClkBit += *clock; //no phase shift but clock bit
+ dest[numBits++] = curPhase;
+ }
+ avgWaveVal = 0;
+ waveStart = i+1;
+ }
+ }
+ avgWaveVal += dest[i+1];
+ }
+ *size = numBits;
+ return errCnt;