- //find first phase shift
- int avgWaveVal=0, lastAvgWaveVal=0;
- waveStart = i;
- for (; i<loopCnt; i++) {
- // find peak
- if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){
- waveEnd = i+1;
- if (g_debugMode == 2) prnt("DEBUG PSK: waveEnd: %u, waveStart: %u",waveEnd, waveStart);
- waveLenCnt = waveEnd-waveStart;
- if (waveLenCnt > fc && waveStart > fc && !(waveLenCnt > fc+3)){ //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 (could cause inverting)
- if (lastAvgWaveVal > threshold_value) curPhase ^= 1;
- 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 PSK: 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;
- } else if (waveLenCnt < fc - 1) { //wave is smaller than field clock (shouldn't happen often)
- errCnt2++;
- if(errCnt2 > 101) return errCnt2;
- }
- avgWaveVal = 0;
- waveStart = i+1;
- }
- }
- avgWaveVal += dest[i+1];
+ 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;