- uint16_t loopCnt = 4096; //don't need to loop through entire array...
- if (size == 0) return -1;
- if (*size<loopCnt) loopCnt = *size;
-
- uint8_t curPhase = *invert;
- size_t i, waveStart=0, waveEnd=0, firstFullWave=0, lastClkBit=0;
- uint8_t fc=0, fullWaveLen=0, tol=1;
- uint16_t errCnt=0, waveLenCnt=0;
- fc = countPSK_FC(dest, *size);
- 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 full wave
- for (i=0; i<loopCnt; i++){
- if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){
- if (waveStart == 0) {
- waveStart = i+1;
- avgWaveVal=dest[i+1];
- //PrintAndLog("DEBUG: waveStart: %d",waveStart);
- } else {
- waveEnd = i+1;
- //PrintAndLog("DEBUG: waveEnd: %d",waveEnd);
- waveLenCnt = waveEnd-waveStart;
- lastAvgWaveVal = avgWaveVal/waveLenCnt;
- if (waveLenCnt > fc){
- firstFullWave = waveStart;
- fullWaveLen=waveLenCnt;
- //if average wave value is > graph 0 then it is an up wave or a 1
- if (lastAvgWaveVal > 128) curPhase^=1;
- break;
- }
- waveStart=0;
- avgWaveVal=0;
- }
- }
- avgWaveVal+=dest[i+1];
- }
- //PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);
- lastClkBit = firstFullWave; //set start of wave as clock align
- waveStart = 0;
- errCnt=0;
- size_t numBits=0;
- //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d", *clock, lastClkBit);
-
- 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);
- //if 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;
- numBits++;
- lastClkBit += *clock;
- } else if (i<lastClkBit+10){
- //noise after a phase shift - ignore
- } else { //phase shift before supposed to based on clock
- errCnt++;
- dest[numBits] = 77;
- numBits++;
- }
- } else if (i+1 > lastClkBit + *clock + tol + fc){
- lastClkBit += *clock; //no phase shift but clock bit
- dest[numBits] = curPhase;
- numBits++;
+ if (size == 0) return -1;
+ uint16_t loopCnt = 4096; //don't need to loop through entire array...
+ if (*size<loopCnt) loopCnt = *size;
+
+ 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){ //not first peak and is a large wave
+ 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];
+ }
+ //PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);
+ lastClkBit = firstFullWave; //set start of wave as clock align
+ //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d", *clock, lastClkBit);
+ waveStart = 0;
+ size_t numBits=0;
+ //set skipped bits
+ memset(dest, curPhase^1, firstFullWave / *clock);
+ numBits += (firstFullWave / *clock);
+ 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;
+}
+// on successful return 1 otherwise return 0
+int VikingDecode(uint8_t *BitStream,
+ size_t size,
+ size_t *startIdx,
+ uint8_t *id_bits,
+ size_t id_bits_size)
+{
+ //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
+ // otherwise could be a void with no arguments
+ //set defaults
+ uint32_t i = 0;
+ uint32_t lastcheckindex = size - (id_bits_size * 2);
+ int found = 0;
+ while (i < lastcheckindex)
+ {
+ if (memcmp(BitStream + i,id_bits,id_bits_size) == 0)
+ {
+ *startIdx = i;
+ found = 1;
+ break;