-//by marshmellow
-//take 01 or 10 = 1 and 11 or 00 = 0
-//check for phase errors - should never have 111 or 000 should be 01001011 or 10110100 for 1010
-//decodes biphase or if inverted it is AKA conditional dephase encoding AKA differential manchester encoding
-int BiphaseRawDecode(uint8_t *BitStream, size_t *size, int offset, int invert)
-{
- uint16_t bitnum = 0;
- uint16_t errCnt = 0;
- size_t i = offset;
- uint16_t MaxBits=512;
- //if not enough samples - error
- if (*size < 51) return -1;
- //check for phase change faults - skip one sample if faulty
- uint8_t offsetA = 1, offsetB = 1;
- for (; i<48; i+=2){
- if (BitStream[i+1]==BitStream[i+2]) offsetA=0;
- if (BitStream[i+2]==BitStream[i+3]) offsetB=0;
- }
- if (!offsetA && offsetB) offset++;
- for (i=offset; i<*size-3; i+=2){
- //check for phase error
- if (BitStream[i+1]==BitStream[i+2]) {
- BitStream[bitnum++]=7;
- errCnt++;
- }
- if((BitStream[i]==1 && BitStream[i+1]==0) || (BitStream[i]==0 && BitStream[i+1]==1)){
- BitStream[bitnum++]=1^invert;
- } else if((BitStream[i]==0 && BitStream[i+1]==0) || (BitStream[i]==1 && BitStream[i+1]==1)){
- BitStream[bitnum++]=invert;
- } else {
- BitStream[bitnum++]=7;
- errCnt++;
- }
- if(bitnum>MaxBits) break;
- }
- *size=bitnum;
- return errCnt;
-}
-
-// by marshmellow
-// 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;
- size_t preLastSample = 0;
- size_t LastSample = 0;
- size_t currSample = 0;
- // 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
- preLastSample = LastSample;
- LastSample = currSample;
- currSample = idx-last_transition;
- if (currSample < (fclow-2)){ //0-5 = garbage noise
- //do nothing with extra garbage
- } else if (currSample < (fchigh-1)) { //6-8 = 8 sample waves
- if (LastSample > (fchigh-2) && (preLastSample < (fchigh-1) || preLastSample == 0 )){
- dest[numBits-1]=1; //correct last 9 wave surrounded by 8 waves
- }
- dest[numBits++]=1;
-
- } else if (currSample > (fchigh) && !numBits) { //12 + and first bit = garbage
- //do nothing with beginning garbage
- } else if (currSample == (fclow+1) && LastSample == (fclow-1)) { // had a 7 then a 9 should be two 8's
- dest[numBits++]=1;
- } else { //9+ = 10 sample 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) {
- if (n < rfLen/fclow) {
- n=0;
- lastval = dest[idx];
- continue;
- }
- n = (n * fclow + rfLen/4) / rfLen;
- } else {
- 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;
-}
-
-//least significant bit first
-uint32_t bytebits_to_byteLSBF(uint8_t *src, size_t numbits)
-{
- uint32_t num = 0;
- for(int i = 0 ; i < numbits ; i++)
- {
- num = (num << 1) | *(src + (numbits-(i+1)));
- }
- 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
-// find viking preamble 0xF200 in already demoded data
-int VikingDemod_AM(uint8_t *dest, size_t *size) {
- //make sure buffer has data
- if (*size < 64*2) return -2;
-
- size_t startIdx = 0;
- uint8_t preamble[] = {1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
- uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
- if (errChk == 0) return -4; //preamble not found
- uint32_t checkCalc = bytebits_to_byte(dest+startIdx,8) ^ bytebits_to_byte(dest+startIdx+8,8) ^ bytebits_to_byte(dest+startIdx+16,8)
- ^ bytebits_to_byte(dest+startIdx+24,8) ^ bytebits_to_byte(dest+startIdx+32,8) ^ bytebits_to_byte(dest+startIdx+40,8)
- ^ bytebits_to_byte(dest+startIdx+48,8) ^ bytebits_to_byte(dest+startIdx+56,8);
- if ( checkCalc != 0xA8 ) return -5;
- if (*size != 64) return -6;
- //return start position
- return (int) startIdx;
-}
-
-// 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; 2 Always 1's), 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--; // overwrite parity with next data
- // if parity fails then return 0
- if (pType == 2) { // then marker bit which should be a 1
- if (!BitStream[j]) return 0;
- } else {
- if (parityTest(parityWd, pLen, pType) == 0) return 0;
- }
- bitCnt+=(pLen-1);
- parityWd = 0;
- }
- // if we got here then all the parities passed
- //return ID start index and size
- return bitCnt;
-}
-
-// Ask/Biphase Demod then try to locate an ISO 11784/85 ID
-// BitStream must contain previously askrawdemod and biphasedemoded data
-int FDXBdemodBI(uint8_t *dest, size_t *size)
-{
- //make sure buffer has enough data
- if (*size < 128) return -1;
-
- size_t startIdx = 0;
- uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,0,1};
-
- uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
- if (errChk == 0) return -2; //preamble not found
- return (int)startIdx;
-}
-
-// 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;
-}
-
-/*
-void dummy(char *fmt, ...){}
-
-#ifndef ON_DEVICE
-#include "ui.h"
-#define prnt PrintAndLog
-#else
-
-#define prnt dummy
-#endif
-*/