+// by marshmellow
+// pass bits to be tested in bits, length bits passed in bitLen, and parity type (even=0 | odd=1) in pType
+// returns 1 if passed
+uint8_t parityTest(uint32_t bits, uint8_t bitLen, uint8_t pType)
+{
+ uint8_t ans = 0;
+ for (uint8_t i = 0; i < bitLen; i++){
+ ans ^= ((bits >> i) & 1);
+ }
+ //PrintAndLog("DEBUG: ans: %d, ptype: %d",ans,pType);
+ return (ans == pType);
+}
+
+// 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)
+{
+ static const uint8_t THRESHOLD = 123;
+ uint32_t idx=0, idx2=0;
+ //make sure buffer has data
+ if (size < 96*50) return -1;
+ //test samples are not just noise
+ uint8_t justNoise = 1;
+ for(idx=0; idx < size && justNoise ;idx++){
+ justNoise = dest[idx] < THRESHOLD;
+ }
+ if(justNoise) return -2;
+
+ // FSK demodulator
+ size = fskdemod(dest, size, 50, 1, 10, 8); // RF/64 and invert
+ if (size < 96) return -3; //did we get a good demod?
+
+ uint8_t mask[] = {0,0,0,0,0,0,0,1};
+ for( idx=0; idx < (size - 96); idx++) {
+ if ( memcmp(dest + idx, mask, sizeof(mask))==0) {
+ // frame marker found
+ //return ID start index
+ if (idx2 == 0) idx2=idx;
+ else if(idx-idx2==96) return idx2;
+ else return -5;
+
+ // should always get 96 bits if it is awid
+ }
+ }
+ //never found mask
+ return -4;
+}
+
+// by marshmellow
+// FSK Demod then try to locate an Farpointe Data (pyramid) ID
+int PyramiddemodFSK(uint8_t *dest, size_t size)
+{
+ static const uint8_t THRESHOLD = 123;
+ uint32_t idx=0, idx2=0;
+ // size_t size2 = size;
+ //make sure buffer has data
+ if (size < 128*50) return -5;
+ //test samples are not just noise
+ uint8_t justNoise = 1;
+ for(idx=0; idx < size && justNoise ;idx++){
+ justNoise = dest[idx] < THRESHOLD;
+ }
+ if(justNoise) return -1;
+
+ // FSK demodulator
+ size = fskdemod(dest, size, 50, 1, 10, 8); // RF/64 and invert
+ if (size < 128) return -2; //did we get a good demod?
+
+ uint8_t mask[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
+ for( idx=0; idx < (size - 128); idx++) {
+ if ( memcmp(dest + idx, mask, sizeof(mask))==0) {
+ // frame marker found
+ if (idx2==0) idx2=idx;
+ else if (idx-idx2==128) return idx2;
+ else return -3;
+ }
+ }
+ //never found mask
+ return -4;
+}
+