- 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;
- if (g_debugMode==2) prnt("DEBUG PSK: firstFullWave: %u, waveLen: %u",firstFullWave,fullWaveLen);
- if (g_debugMode==2) prnt("DEBUG: clk: %d, lastClkBit: %u, fc: %u", *clock, lastClkBit,(unsigned int) fc);
- 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];
+ // 00001111 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1
+ uint8_t preamble[] = {0,0,0,0,1,1,1,1};
+
+ uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
+ if (errChk == 0) return -3; //preamble not found
+
+ numStart = startIdx + sizeof(preamble);
+ // final loop, go over previously decoded FSK data and manchester decode into usable tag ID
+ for (size_t idx = numStart; (idx-numStart) < *size - sizeof(preamble); idx+=2){
+ if (dest[idx] == dest[idx+1])
+ return -4; //not manchester data
+ *hi2 = (*hi2<<1)|(*hi>>31);
+ *hi = (*hi<<1)|(*lo>>31);
+ //Then, shift in a 0 or one into low
+ if (dest[idx] && !dest[idx+1]) // 1 0
+ *lo=(*lo<<1)|1;
+ else // 0 1
+ *lo=(*lo<<1)|0;