-//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)
-{
- if (*size==0) return -1;
- int start = DetectASKClock(BinStream, *size, clk, maxErr); //clock default
- if (*clk==0 || start < 0) return -1;
- if (*invert != 1) *invert = 0;
- if (amp==1) askAmp(BinStream, *size);
-
- uint8_t initLoopMax = 255;
- if (initLoopMax > *size) initLoopMax = *size;
- // Detect high and lows
- //25% clip in case highs and lows aren't clipped [marshmellow]
- int high, low;
- if (getHiLo(BinStream, initLoopMax, &high, &low, 75, 75) < 1)
- return -1; //just noise
-
- // if clean clipped waves detected run alternate demod
- if (DetectCleanAskWave(BinStream, *size, high, low))
- return cleanAskRawDemod(BinStream, size, *clk, *invert, high, low);
-
- int lastBit; //set first clock check - can go negative
- size_t i, errCnt = 0, bitnum = 0; //output counter
- uint8_t midBit = 0;
- size_t MaxBits = 1024;
- lastBit = start - *clk;
-
- for (i = start; i < *size; ++i) {
- if (i - lastBit > *clk){
- if (BinStream[i] >= high) {
- BinStream[bitnum++] = *invert;
- } else if (BinStream[i] <= low) {
- BinStream[bitnum++] = *invert ^ 1;
- } else {
- if (bitnum > 0) {
- BinStream[bitnum++]=7;
- errCnt++;
- }
- }
- midBit = 0;
- lastBit += *clk;
- } else if (i-lastBit > (*clk/2) && midBit == 0){
- if (BinStream[i] >= high) {
- BinStream[bitnum++] = *invert;
- } else if (BinStream[i] <= low) {
- BinStream[bitnum++] = *invert ^ 1;
- } else {
-
- BinStream[bitnum] = BinStream[bitnum-1];
- bitnum++;
- }
- midBit = 1;
- }
- if (bitnum >= MaxBits) break;
- }
- *size = bitnum;
- return errCnt;
-}
-
-// 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)
-{
- size_t last_transition = 0;
- size_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;
- }
- }
- 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 invert, uint8_t fchigh, uint8_t fclow)
-{
- uint8_t lastval=dest[0];
- size_t idx=0;
- size_t numBits=0;
- uint32_t n=1;
- for( idx=1; idx < size; idx++) {
- n++;
- if (dest[idx]==lastval) continue;
-
- //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;
- }
- if (n == 0) n = 1;
-
- memset(dest+numBits, dest[idx-1]^invert , 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/fchigh) {
- if (dest[idx-2]==1) {
- n = (n * fclow + rfLen/2) / rfLen;
- } else {
- n = (n * fchigh + rfLen/2) / rfLen;
- }
- memset(dest+numBits, 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, 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*2) 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;
-}
-
-// by marshmellow
-// to help detect clocks on heavily clipped samples
-// based on counts between zero crossings
-int DetectStrongAskClock(uint8_t dest[], size_t size)
-{
- int clk[]={0,8,16,32,40,50,64,100,128};
- 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++;
- }
- }
- }
- uint8_t tol;
- for (idx=8; idx>0; idx--){
- tol = clk[idx]/8;
- if (clk[idx] >= highCnt - tol && clk[idx] <= highCnt + tol)
- return clk[idx];
- if (clk[idx] >= highCnt2 - tol && clk[idx] <= highCnt2 + tol)
- return clk[idx];
- }
- return -1;
-}
-
-// by marshmellow
-// not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
-// maybe somehow adjust peak trimming value based on samples to fix?
-// return start index of best starting position for that clock and return clock (by reference)
-int DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr)
-{
- size_t i=1;
- uint8_t clk[]={255,8,16,32,40,50,64,100,128,255};
- uint8_t loopCnt = 255; //don't need to loop through entire array...
- if (size==0) return -1;
- if (size <= loopCnt) loopCnt = size-1; //not enough samples
-
- //if we already have a valid clock
- uint8_t clockFnd=0;
- for (;i<9;++i)
- if (clk[i] == *clock) clockFnd=i;
- //clock found but continue to find best startpos
-
- //get high and low peak
- int peak, low;
- if (getHiLo(dest, loopCnt, &peak, &low, 75, 75) < 1) return -1;
-
- //test for large clean peaks
- if (DetectCleanAskWave(dest, size, peak, low)==1){
- int ans = DetectStrongAskClock(dest, size);
- for (i=8; i>1; i--){
- if (clk[i] == ans) {
- *clock = ans;
- //clockFnd = i;
- return 0; // for strong waves i don't use the 'best start position' yet...
- //break; //clock found but continue to find best startpos [not yet]
- }
- }
- }
- uint8_t ii;
- uint8_t clkCnt, tol = 0;
- uint16_t bestErr[]={1000,1000,1000,1000,1000,1000,1000,1000,1000};
- uint8_t bestStart[]={0,0,0,0,0,0,0,0,0};
- size_t errCnt = 0;
- size_t arrLoc, loopEnd;