-//by marshmellow
-//takes 3 arguments - clock, invert and maxErr as integers
-//attempts to demodulate ask only
-int askrawdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp)
-{
- uint32_t i;
- if (*size==0) return -1;
- int start = DetectASKClock(BinStream, *size, clk, 20); //clock default
- if (*clk==0) return -1;
- if (start<0) return -1;
- if (*invert != 0 && *invert != 1) *invert =0;
- if (amp==1) askAmp(BinStream, *size);
-
- uint32_t initLoopMax = 200;
- if (initLoopMax > *size) initLoopMax=*size;
- // Detect high and lows
- //25% clip in case highs and lows aren't clipped [marshmellow]
- uint8_t clip = 75;
- int high, low, ans;
- ans = getHiLo(BinStream, initLoopMax, &high, &low, clip, clip);
- if (ans<1) return -1; //just noise
-
- if (DetectCleanAskWave(BinStream, *size, high, low)) {
- //PrintAndLog("Clean");
- return cleanAskRawDemod(BinStream, size, *clk, *invert, high, low);
- }
-
- //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
- int lastBit = 0; //set first clock check
- uint32_t bitnum = 0; //output counter
- uint8_t tol = 0; //clock tolerance adjust - waves will be accepted as within the clock
- // if they fall + or - this value + clock from last valid wave
- if (*clk == 32) tol=0; //clock tolerance may not be needed anymore currently set to
- // + or - 1 but could be increased for poor waves or removed entirely
- uint32_t iii = 0;
- uint32_t gLen = *size;
- if (gLen > 500) gLen=500;
- //if 0 errors allowed then only try first 2 clock cycles as we want a low tolerance
- if (!maxErr) gLen = *clk * 2;
- uint8_t errCnt =0;
- uint32_t bestStart = *size;
- uint32_t bestErrCnt = maxErr; //(*size/1000);
- uint8_t midBit=0;
- uint16_t MaxBits=1000;
-
- //PrintAndLog("DEBUG - lastbit - %d",lastBit);
- //loop to find first wave that works
- for (iii=start; iii < gLen; ++iii){
- if ((BinStream[iii]>=high) || (BinStream[iii]<=low)){
- lastBit=iii-*clk;
- errCnt=0;
- //loop through to see if this start location works
- for (i = iii; i < *size; ++i) {
- if ((BinStream[i] >= high) && ((i-lastBit)>(*clk-tol))){
- lastBit+=*clk;
- midBit=0;
- } else if ((BinStream[i] <= low) && ((i-lastBit)>(*clk-tol))){
- //low found and we are expecting a bar
- lastBit+=*clk;
- midBit=0;
- } else if ((BinStream[i]<=low) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
- //mid bar?
- midBit=1;
- } else if ((BinStream[i]>=high) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
- //mid bar?
- midBit=1;
- } else if ((i-lastBit)>((*clk/2)+tol) && (midBit==0)){
- //no mid bar found
- midBit=1;
- } else {
- //mid value found or no bar supposed to be here
-
- if ((i-lastBit)>(*clk+tol)){
- //should have hit a high or low based on clock!!
- //debug
- //PrintAndLog("DEBUG - no wave in expected area - location: %d, expected: %d-%d, lastBit: %d - resetting search",i,(lastBit+(clk-((int)(tol)))),(lastBit+(clk+((int)(tol)))),lastBit);
-
- errCnt++;
- lastBit+=*clk;//skip over until hit too many errors
- if (errCnt > maxErr){
- //errCnt=0;
- break;
- }
- }
- }
- if ((i-iii)>(MaxBits * *clk)) break; //got enough bits
- }
- //we got more than 64 good bits and not all errors
- if ((((i-iii)/ *clk) > (64)) && (errCnt<=maxErr)) {
- //possible good read
- if (errCnt==0){
- bestStart=iii;
- bestErrCnt=errCnt;
- break; //great read - finish
- }
- if (errCnt<bestErrCnt){ //set this as new best run
- bestErrCnt=errCnt;
- bestStart = iii;
- }
- }
- }
- }
- if (bestErrCnt<=maxErr){
- //best run is good enough - set to best run and overwrite BinStream
- iii = bestStart;
- lastBit = bestStart - *clk;
- bitnum=0;
- for (i = iii; i < *size; ++i) {
- if ((BinStream[i] >= high) && ((i-lastBit) > (*clk-tol))){
- lastBit += *clk;
- BinStream[bitnum] = *invert;
- bitnum++;
- midBit=0;
- } else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){
- //low found and we are expecting a bar
- lastBit+=*clk;
- BinStream[bitnum] = 1 - *invert;
- bitnum++;
- midBit=0;
- } else if ((BinStream[i]<=low) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
- //mid bar?
- midBit=1;
- BinStream[bitnum] = 1 - *invert;
- bitnum++;
- } else if ((BinStream[i]>=high) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
- //mid bar?
- midBit=1;
- BinStream[bitnum] = *invert;
- bitnum++;
- } else if ((i-lastBit)>((*clk/2)+tol) && (midBit==0)){
- //no mid bar found
- midBit=1;
- if (bitnum!=0) BinStream[bitnum] = BinStream[bitnum-1];
- bitnum++;
-
- } else {
- //mid value found or no bar supposed to be here
- if ((i-lastBit)>(*clk+tol)){
- //should have hit a high or low based on clock!!
-
- //debug
- //PrintAndLog("DEBUG - no wave in expected area - location: %d, expected: %d-%d, lastBit: %d - resetting search",i,(lastBit+(clk-((int)(tol)))),(lastBit+(clk+((int)(tol)))),lastBit);
- if (bitnum > 0){
- BinStream[bitnum]=77;
- bitnum++;
- }
- lastBit+=*clk;//skip over error
- }
- }
- if (bitnum >= MaxBits) break;
- }
- *size=bitnum;
- } else{
- *invert=bestStart;
- *clk=iii;
- return -1;
- }
- return bestErrCnt;
-}
-
-// demod gProxIIDemod
-// error returns as -x
-// success returns start position in BitStream
-// BitStream must contain previously askrawdemod and biphasedemoded data
-int gProxII_Demod(uint8_t BitStream[], size_t *size)
-{
- size_t startIdx=0;
- uint8_t preamble[] = {1,1,1,1,1,0};
-
- uint8_t errChk = preambleSearch(BitStream, preamble, sizeof(preamble), size, &startIdx);
- if (errChk == 0) return -3; //preamble not found
- if (*size != 96) return -2; //should have found 96 bits
- //check first 6 spacer bits to verify format
- if (!BitStream[startIdx+5] && !BitStream[startIdx+10] && !BitStream[startIdx+15] && !BitStream[startIdx+20] && !BitStream[startIdx+25] && !BitStream[startIdx+30]){
- //confirmed proper separator bits found
- //return start position
- return (int) startIdx;
- }
- return -5;
-}
-
-//translate wave to 11111100000 (1 for each short wave 0 for each long wave)
-size_t fsk_wave_demod(uint8_t * dest, size_t size, uint8_t fchigh, uint8_t fclow)
-{
- uint32_t last_transition = 0;
- uint32_t idx = 1;
- //uint32_t maxVal=0;
- if (fchigh==0) fchigh=10;
- if (fclow==0) fclow=8;
- //set the threshold close to 0 (graph) or 128 std to avoid static
- uint8_t threshold_value = 123;
-
- // sync to first lo-hi transition, and threshold
-
- // Need to threshold first sample
-
- if(dest[0] < threshold_value) dest[0] = 0;
- else dest[0] = 1;
-
- size_t numBits = 0;
- // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
- // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
- // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
- for(idx = 1; idx < size; idx++) {
- // threshold current value
-
- if (dest[idx] < threshold_value) dest[idx] = 0;
- else dest[idx] = 1;
-
- // Check for 0->1 transition
- if (dest[idx-1] < dest[idx]) { // 0 -> 1 transition
- if ((idx-last_transition)<(fclow-2)){ //0-5 = garbage noise
- //do nothing with extra garbage
- } else if ((idx-last_transition) < (fchigh-1)) { //6-8 = 8 waves
- dest[numBits]=1;
- } else if ((idx-last_transition) > (fchigh+1) && !numBits) { //12 + and first bit = garbage
- //do nothing with beginning garbage
- } else { //9+ = 10 waves
- dest[numBits]=0;
- }
- last_transition = idx;
- numBits++;
- }
- }
- return numBits; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
-}
-
-//translate 11111100000 to 10
-size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t maxConsequtiveBits,
- uint8_t invert, uint8_t fchigh, uint8_t fclow)
-{
- uint8_t lastval=dest[0];
- uint32_t idx=0;
- size_t numBits=0;
- uint32_t n=1;
- for( idx=1; idx < size; idx++) {
-
- if (dest[idx]==lastval) {
- n++;
- continue;
- }
- n++;
- //if lastval was 1, we have a 1->0 crossing
- if (dest[idx-1]==1) {
- if (!numBits && n < rfLen/fclow) {
- n=0;
- lastval = dest[idx];
- continue;
- }
- n = (n * fclow + rfLen/2) / rfLen;
- } else {// 0->1 crossing
- //test first bitsample too small
- if (!numBits && n < rfLen/fchigh) {
- n=0;
- lastval = dest[idx];
- continue;
- }
- n = (n * fchigh + rfLen/2) / rfLen; //-1 for fudge factor
- }
- if (n == 0) n = 1;
-
- if(n < maxConsequtiveBits) //Consecutive
- {
- if(invert==0){ //invert bits
- memset(dest+numBits, dest[idx-1] , n);
- }else{
- memset(dest+numBits, dest[idx-1]^1 , n);
- }
- numBits += n;
- }
- n=0;
- lastval=dest[idx];
- }//end for
-
- // if valid extra bits at the end were all the same frequency - add them in
- if (n > rfLen/fclow && n > rfLen/fchigh) {
- if (dest[idx-2]==1) {
- n = ((n+1) * fclow + rfLen/2) / rfLen;
- } else {// 0->1 crossing
- n = ((n+1) * fchigh + (rfLen-1)/2) / (rfLen-1); //-1 for fudge factor
- }
- memset(dest, dest[idx-1]^invert , n);
- numBits += n;
- }
- return numBits;
-}
-//by marshmellow (from holiman's base)
-// full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod)
-int fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow)
-{
- // FSK demodulator
- size = fsk_wave_demod(dest, size, fchigh, fclow);
- size = aggregate_bits(dest, size, rfLen, 192, invert, fchigh, fclow);
- return size;
-}
-
-// loop to get raw HID waveform then FSK demodulate the TAG ID from it
-int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo)
-{
- if (justNoise(dest, *size)) return -1;
-
- size_t numStart=0, size2=*size, startIdx=0;
- // FSK demodulator
- *size = fskdemod(dest, size2,50,1,10,8); //fsk2a
- if (*size < 96) return -2;
- // 00011101 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1
- uint8_t preamble[] = {0,0,0,1,1,1,0,1};
- // find bitstring in array
- 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;
- }
- return (int)startIdx;
-}
-
-// loop to get raw paradox waveform then FSK demodulate the TAG ID from it
-int ParadoxdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo)
-{
- if (justNoise(dest, *size)) return -1;
-
- size_t numStart=0, size2=*size, startIdx=0;
- // FSK demodulator
- *size = fskdemod(dest, size2,50,1,10,8); //fsk2a
- if (*size < 96) return -2;
-
- // 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;
- }
- return (int)startIdx;
-}
-
-uint32_t bytebits_to_byte(uint8_t* src, size_t numbits)
-{
- uint32_t num = 0;
- for(int i = 0 ; i < numbits ; i++)
- {
- num = (num << 1) | (*src);
- src++;
- }
- return num;
-}
-
-int IOdemodFSK(uint8_t *dest, size_t size)
-{
- if (justNoise(dest, size)) return -1;
- //make sure buffer has data
- if (size < 66*64) return -2;
- // FSK demodulator
- size = fskdemod(dest, size, 64, 1, 10, 8); // FSK2a RF/64
- if (size < 65) return -3; //did we get a good demod?
- //Index map
- //0 10 20 30 40 50 60
- //| | | | | | |
- //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
- //-----------------------------------------------------------------------------
- //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
- //
- //XSF(version)facility:codeone+codetwo
- //Handle the data
- size_t startIdx = 0;
- uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,1};
- uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), &size, &startIdx);
- if (errChk == 0) return -4; //preamble not found
-
- if (!dest[startIdx+8] && dest[startIdx+17]==1 && dest[startIdx+26]==1 && dest[startIdx+35]==1 && dest[startIdx+44]==1 && dest[startIdx+53]==1){
- //confirmed proper separator bits found
- //return start position
- return (int) startIdx;
- }
- return -5;
-}
-
-// by marshmellow
-// takes a array of binary values, start position, length of bits per parity (includes parity bit),
-// Parity Type (1 for odd 0 for even), and binary Length (length to run)
-size_t removeParity(uint8_t *BitStream, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen)
-{
- uint32_t parityWd = 0;
- size_t j = 0, bitCnt = 0;
- for (int word = 0; word < (bLen); word+=pLen){
- for (int bit=0; bit < pLen; bit++){
- parityWd = (parityWd << 1) | BitStream[startIdx+word+bit];
- BitStream[j++] = (BitStream[startIdx+word+bit]);
- }
- j--;
- // if parity fails then return 0
- if (parityTest(parityWd, pLen, pType) == 0) return -1;
- bitCnt+=(pLen-1);
- parityWd = 0;
- }
- // if we got here then all the parities passed
- //return ID start index and size
- return bitCnt;
-}
-
-// by marshmellow
-// FSK Demod then try to locate an AWID ID
-int AWIDdemodFSK(uint8_t *dest, size_t *size)
-{
- //make sure buffer has enough data
- if (*size < 96*50) return -1;
-
- if (justNoise(dest, *size)) return -2;
-
- // FSK demodulator
- *size = fskdemod(dest, *size, 50, 1, 10, 8); // fsk2a RF/50
- if (*size < 96) return -3; //did we get a good demod?
-
- uint8_t preamble[] = {0,0,0,0,0,0,0,1};
- size_t startIdx = 0;
- uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
- if (errChk == 0) return -4; //preamble not found
- if (*size != 96) return -5;
- return (int)startIdx;
-}
-
-// by marshmellow
-// FSK Demod then try to locate an Farpointe Data (pyramid) ID
-int PyramiddemodFSK(uint8_t *dest, size_t *size)
-{
- //make sure buffer has data
- if (*size < 128*50) return -5;
-
- //test samples are not just noise
- if (justNoise(dest, *size)) return -1;
-
- // FSK demodulator
- *size = fskdemod(dest, *size, 50, 1, 10, 8); // fsk2a RF/50
- if (*size < 128) return -2; //did we get a good demod?
-
- uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
- size_t startIdx = 0;
- uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
- if (errChk == 0) return -4; //preamble not found
- if (*size != 128) return -3;
- return (int)startIdx;
-}
-
-
-uint8_t DetectCleanAskWave(uint8_t dest[], size_t size, int high, int low)
-{
- uint16_t allPeaks=1;
- uint16_t cntPeaks=0;
- size_t loopEnd = 572;
- if (loopEnd > size) loopEnd = size;
- for (size_t i=60; i<loopEnd; i++){
- if (dest[i]>low && dest[i]<high)
- allPeaks=0;
- else
- cntPeaks++;
- }
- if (allPeaks == 0){
- if (cntPeaks > 300) return 1;
- }
- return allPeaks;
-}
-
-int DetectStrongAskClock(uint8_t dest[], size_t size)
-{
- int clk[]={0,8,16,32,40,50,64,100,128,256};
- size_t idx = 40;
- uint8_t high=0;
- size_t cnt = 0;
- size_t highCnt = 0;
- size_t highCnt2 = 0;
- for (;idx < size; idx++){
- if (dest[idx]>128) {
- if (!high){
- high=1;
- if (cnt > highCnt){
- if (highCnt != 0) highCnt2 = highCnt;
- highCnt = cnt;
- } else if (cnt > highCnt2) {
- highCnt2 = cnt;
- }
- cnt=1;
- } else {
- cnt++;
- }
- } else if (dest[idx] <= 128){
- if (high) {
- high=0;
- if (cnt > highCnt) {
- if (highCnt != 0) highCnt2 = highCnt;
- highCnt = cnt;
- } else if (cnt > highCnt2) {
- highCnt2 = cnt;
- }
- cnt=1;
- } else {
- cnt++;
- }