]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - common/lfdemod.c
1 //-----------------------------------------------------------------------------
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Low frequency demod/decode commands
9 //-----------------------------------------------------------------------------
16 uint8_t justNoise(uint8_t *BitStream
, size_t size
)
18 static const uint8_t THRESHOLD
= 123;
19 //test samples are not just noise
20 uint8_t justNoise1
= 1;
21 for(size_t idx
=0; idx
< size
&& justNoise1
;idx
++){
22 justNoise1
= BitStream
[idx
] < THRESHOLD
;
28 //get high and low values of a wave with passed in fuzz factor. also return noise test = 1 for passed or 0 for only noise
29 int getHiLo(uint8_t *BitStream
, size_t size
, int *high
, int *low
, uint8_t fuzzHi
, uint8_t fuzzLo
)
33 // get high and low thresholds
34 for (int i
=0; i
< size
; i
++){
35 if (BitStream
[i
] > *high
) *high
= BitStream
[i
];
36 if (BitStream
[i
] < *low
) *low
= BitStream
[i
];
38 if (*high
< 123) return -1; // just noise
39 *high
= (int)(((*high
-128)*(((float)fuzzHi
)/100))+128);
40 *low
= (int)(((*low
-128)*(((float)fuzzLo
)/100))+128);
45 // pass bits to be tested in bits, length bits passed in bitLen, and parity type (even=0 | odd=1) in pType
46 // returns 1 if passed
47 uint8_t parityTest(uint32_t bits
, uint8_t bitLen
, uint8_t pType
)
50 for (uint8_t i
= 0; i
< bitLen
; i
++){
51 ans
^= ((bits
>> i
) & 1);
53 //PrintAndLog("DEBUG: ans: %d, ptype: %d",ans,pType);
54 return (ans
== pType
);
58 //search for given preamble in given BitStream and return success=1 or fail=0 and startIndex and length
59 uint8_t preambleSearch(uint8_t *BitStream
, uint8_t *preamble
, size_t pLen
, size_t *size
, size_t *startIdx
)
62 for (int idx
=0; idx
< *size
- pLen
; idx
++){
63 if (memcmp(BitStream
+idx
, preamble
, pLen
) == 0){
70 *size
= idx
- *startIdx
;
80 //takes 1s and 0s and searches for EM410x format - output EM ID
81 uint64_t Em410xDecodeOld(uint8_t *BitStream
, size_t *size
, size_t *startIdx
)
83 //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
84 // otherwise could be a void with no arguments
88 if (BitStream
[1]>1){ //allow only 1s and 0s
89 // PrintAndLog("no data found");
92 // 111111111 bit pattern represent start of frame
93 uint8_t preamble
[] = {1,1,1,1,1,1,1,1,1};
95 uint32_t parityBits
= 0;
98 for (uint8_t extraBitChk
=0; extraBitChk
<5; extraBitChk
++){
99 errChk
= preambleSearch(BitStream
+extraBitChk
+*startIdx
, preamble
, sizeof(preamble
), size
, startIdx
);
100 if (errChk
== 0) return 0;
102 for (i
=0; i
<10;i
++){ //loop through 10 sets of 5 bits (50-10p = 40 bits)
103 parityBits
= bytebits_to_byte(BitStream
+(i
*5)+idx
,5);
105 if (parityTest(parityBits
, 5, 0) == 0){
106 //parity failed try next bit (in the case of 1111111111) but last 9 = preamble
111 //set uint64 with ID from BitStream
112 for (uint8_t ii
=0; ii
<4; ii
++){
113 lo
= (lo
<< 1LL) | (BitStream
[(i
*5)+ii
+idx
]);
116 if (errChk
!= 0) return lo
;
117 //skip last 5 bit parity test for simplicity.
124 //takes 1s and 0s and searches for EM410x format - output EM ID
125 uint8_t Em410xDecode(uint8_t *BitStream
, size_t *size
, size_t *startIdx
, uint32_t *hi
, uint64_t *lo
)
127 //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
128 // otherwise could be a void with no arguments
131 if (BitStream
[1]>1){ //allow only 1s and 0s
132 // PrintAndLog("no data found");
135 // 111111111 bit pattern represent start of frame
136 uint8_t preamble
[] = {1,1,1,1,1,1,1,1,1};
138 uint32_t parityBits
= 0;
142 for (uint8_t extraBitChk
=0; extraBitChk
<5; extraBitChk
++){
143 errChk
= preambleSearch(BitStream
+extraBitChk
+*startIdx
, preamble
, sizeof(preamble
), size
, startIdx
);
144 if (errChk
== 0) return 0;
145 if (*size
>64) FmtLen
= 22;
147 for (i
=0; i
<FmtLen
; i
++){ //loop through 10 or 22 sets of 5 bits (50-10p = 40 bits or 88 bits)
148 parityBits
= bytebits_to_byte(BitStream
+(i
*5)+idx
,5);
150 if (parityTest(parityBits
, 5, 0) == 0){
151 //parity failed try next bit (in the case of 1111111111) but last 9 = preamble
156 //set uint64 with ID from BitStream
157 for (uint8_t ii
=0; ii
<4; ii
++){
158 *hi
= (*hi
<< 1) | (*lo
>> 63);
159 *lo
= (*lo
<< 1) | (BitStream
[(i
*5)+ii
+idx
]);
162 if (errChk
!= 0) return 1;
163 //skip last 5 bit parity test for simplicity.
170 //takes 3 arguments - clock, invert, maxErr as integers
171 //attempts to demodulate ask while decoding manchester
172 //prints binary found and saves in graphbuffer for further commands
173 int askmandemod(uint8_t *BinStream
, size_t *size
, int *clk
, int *invert
, int maxErr
)
177 int start
= DetectASKClock(BinStream
, *size
, clk
, 20); //clock default
178 if (*clk
==0) return -3;
179 if (start
< 0) return -3;
180 // if autodetected too low then adjust //MAY NEED ADJUSTMENT
181 //if (clk2==0 && *clk<8) *clk =64;
182 //if (clk2==0 && *clk<32) *clk=32;
183 if (*invert
!= 0 && *invert
!= 1) *invert
=0;
184 uint32_t initLoopMax
= 200;
185 if (initLoopMax
> *size
) initLoopMax
=*size
;
186 // Detect high and lows
187 // 25% fuzz in case highs and lows aren't clipped [marshmellow]
189 ans
= getHiLo(BinStream
, initLoopMax
, &high
, &low
, 75, 75);
190 if (ans
<1) return -2; //just noise
192 // PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
193 int lastBit
= 0; //set first clock check
194 uint32_t bitnum
= 0; //output counter
195 int tol
= 0; //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
196 if (*clk
<=32) tol
=1; //clock tolerance may not be needed anymore currently set to + or - 1 but could be increased for poor waves or removed entirely
198 uint32_t gLen
= *size
;
199 if (gLen
> 3000) gLen
=3000;
200 //if 0 errors allowed then only try first 2 clock cycles as we want a low tolerance
201 if (!maxErr
) gLen
=*clk
*2;
203 uint16_t MaxBits
= 500;
204 uint32_t bestStart
= *size
;
205 int bestErrCnt
= maxErr
+1;
206 // PrintAndLog("DEBUG - lastbit - %d",lastBit);
207 // loop to find first wave that works
208 for (iii
=0; iii
< gLen
; ++iii
){
209 if ((BinStream
[iii
] >= high
) || (BinStream
[iii
] <= low
)){
212 // loop through to see if this start location works
213 for (i
= iii
; i
< *size
; ++i
) {
214 if ((BinStream
[i
] >= high
) && ((i
-lastBit
) > (*clk
-tol
))){
216 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
) > (*clk
-tol
))){
217 //low found and we are expecting a bar
220 //mid value found or no bar supposed to be here
221 if ((i
-lastBit
)>(*clk
+tol
)){
222 //should have hit a high or low based on clock!!
225 //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);
228 lastBit
+=*clk
;//skip over until hit too many errors
229 if (errCnt
>(maxErr
)) break; //allow 1 error for every 1000 samples else start over
232 if ((i
-iii
) >(MaxBits
* *clk
)) break; //got plenty of bits
234 //we got more than 64 good bits and not all errors
235 if ((((i
-iii
)/ *clk
) > (64)) && (errCnt
<=maxErr
)) {
240 break; //great read - finish
242 if (errCnt
<bestErrCnt
){ //set this as new best run
249 if (bestErrCnt
<=maxErr
){
250 //best run is good enough set to best run and set overwrite BinStream
252 lastBit
= bestStart
- *clk
;
254 for (i
= iii
; i
< *size
; ++i
) {
255 if ((BinStream
[i
] >= high
) && ((i
-lastBit
) > (*clk
-tol
))){
257 BinStream
[bitnum
] = *invert
;
259 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
) > (*clk
-tol
))){
260 //low found and we are expecting a bar
262 BinStream
[bitnum
] = 1-*invert
;
265 //mid value found or no bar supposed to be here
266 if ((i
-lastBit
)>(*clk
+tol
)){
267 //should have hit a high or low based on clock!!
270 //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);
272 BinStream
[bitnum
]=77;
276 lastBit
+=*clk
;//skip over error
279 if (bitnum
>=MaxBits
) break;
291 //encode binary data into binary manchester
292 int ManchesterEncode(uint8_t *BitStream
, size_t size
)
294 size_t modIdx
=20000, i
=0;
295 if (size
>modIdx
) return -1;
296 for (size_t idx
=0; idx
< size
; idx
++){
297 BitStream
[idx
+modIdx
++] = BitStream
[idx
];
298 BitStream
[idx
+modIdx
++] = BitStream
[idx
]^1;
300 for (; i
<(size
*2); i
++){
301 BitStream
[i
] = BitStream
[i
+20000];
307 //take 10 and 01 and manchester decode
308 //run through 2 times and take least errCnt
309 int manrawdecode(uint8_t * BitStream
, size_t *size
)
312 uint16_t MaxBits
= 500;
315 uint16_t bestErr
= 1000;
316 uint16_t bestRun
= 0;
318 if (size
== 0) return -1;
319 for (ii
=1;ii
<3;++ii
){
321 for (i
=i
+ii
;i
<*size
-2;i
+=2){
322 if(BitStream
[i
]==1 && (BitStream
[i
+1]==0)){
323 } else if((BitStream
[i
]==0)&& BitStream
[i
+1]==1){
327 if(bitnum
>MaxBits
) break;
339 for (i
=i
+ii
; i
< *size
-2; i
+=2){
340 if(BitStream
[i
] == 1 && (BitStream
[i
+1] == 0)){
341 BitStream
[bitnum
++]=0;
342 } else if((BitStream
[i
] == 0) && BitStream
[i
+1] == 1){
343 BitStream
[bitnum
++]=1;
345 BitStream
[bitnum
++]=77;
348 if(bitnum
>MaxBits
) break;
356 //take 01 or 10 = 1 and 11 or 00 = 0
357 //check for phase errors - should never have 111 or 000 should be 01001011 or 10110100 for 1010
358 int BiphaseRawDecode(uint8_t *BitStream
, size_t *size
, int offset
, int invert
)
363 uint16_t MaxBits
=512;
364 //if not enough samples - error
365 if (*size
< 51) return -1;
366 //check for phase change faults - skip one sample if faulty
367 uint8_t offsetA
= 1, offsetB
= 1;
369 if (BitStream
[i
+1]==BitStream
[i
+2]) offsetA
=0;
370 if (BitStream
[i
+2]==BitStream
[i
+3]) offsetB
=0;
372 if (!offsetA
&& offsetB
) offset
++;
373 for (i
=offset
; i
<*size
-3; i
+=2){
374 //check for phase error
375 if (i
<*size
-3 && BitStream
[i
+1]==BitStream
[i
+2]) {
376 BitStream
[bitnum
++]=77;
379 if((BitStream
[i
]==1 && BitStream
[i
+1]==0) || (BitStream
[i
]==0 && BitStream
[i
+1]==1)){
380 BitStream
[bitnum
++]=1^invert
;
381 } else if((BitStream
[i
]==0 && BitStream
[i
+1]==0) || (BitStream
[i
]==1 && BitStream
[i
+1]==1)){
382 BitStream
[bitnum
++]=invert
;
384 BitStream
[bitnum
++]=77;
387 if(bitnum
>MaxBits
) break;
394 void askAmp(uint8_t *BitStream
, size_t size
)
398 for(int i
= 1; i
<size
; i
++){
399 if (BitStream
[i
]-BitStream
[i
-1]>=30) //large jump up
401 else if(BitStream
[i
]-BitStream
[i
-1]<=-20) //large jump down
404 shiftedVal
=BitStream
[i
]+shift
;
408 else if (shiftedVal
<0)
410 BitStream
[i
-1] = shiftedVal
;
416 //takes 3 arguments - clock, invert and maxErr as integers
417 //attempts to demodulate ask only
418 int askrawdemod(uint8_t *BinStream
, size_t *size
, int *clk
, int *invert
, int maxErr
, uint8_t amp
)
421 if (*size
==0) return -1;
422 int start
= DetectASKClock(BinStream
, *size
, clk
, 20); //clock default
423 if (*clk
==0) return -1;
424 if (start
<0) return -1;
425 if (*invert
!= 0 && *invert
!= 1) *invert
=0;
426 uint32_t initLoopMax
= 200;
427 if (initLoopMax
> *size
) initLoopMax
=*size
;
428 // Detect high and lows
429 //25% fuzz in case highs and lows aren't clipped [marshmellow]
431 if (amp
==1) askAmp(BinStream
, *size
);
432 ans
= getHiLo(BinStream
, initLoopMax
, &high
, &low
, 75, 75);
433 if (ans
<1) return -1; //just noise
435 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
436 int lastBit
= 0; //set first clock check
437 uint32_t bitnum
= 0; //output counter
438 uint8_t tol
= 0; //clock tolerance adjust - waves will be accepted as within the clock
439 // if they fall + or - this value + clock from last valid wave
440 if (*clk
== 32) tol
=0; //clock tolerance may not be needed anymore currently set to
441 // + or - 1 but could be increased for poor waves or removed entirely
443 uint32_t gLen
= *size
;
444 if (gLen
> 500) gLen
=500;
445 //if 0 errors allowed then only try first 2 clock cycles as we want a low tolerance
446 if (!maxErr
) gLen
=*clk
*2;
448 uint32_t bestStart
= *size
;
449 uint32_t bestErrCnt
= maxErr
; //(*size/1000);
451 uint16_t MaxBits
=1000;
452 //PrintAndLog("DEBUG - lastbit - %d",lastBit);
453 //loop to find first wave that works
454 for (iii
=start
; iii
< gLen
; ++iii
){
455 if ((BinStream
[iii
]>=high
) || (BinStream
[iii
]<=low
)){
458 //loop through to see if this start location works
459 for (i
= iii
; i
< *size
; ++i
) {
460 if ((BinStream
[i
] >= high
) && ((i
-lastBit
)>(*clk
-tol
))){
463 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
)>(*clk
-tol
))){
464 //low found and we are expecting a bar
467 } else if ((BinStream
[i
]<=low
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
470 } else if ((BinStream
[i
]>=high
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
473 } else if ((i
-lastBit
)>((*clk
/2)+tol
) && (midBit
==0)){
477 //mid value found or no bar supposed to be here
479 if ((i
-lastBit
)>(*clk
+tol
)){
480 //should have hit a high or low based on clock!!
482 //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);
485 lastBit
+=*clk
;//skip over until hit too many errors
486 if (errCnt
> maxErr
){
492 if ((i
-iii
)>(MaxBits
* *clk
)) break; //got enough bits
494 //we got more than 64 good bits and not all errors
495 if ((((i
-iii
)/ *clk
) > (64)) && (errCnt
<=maxErr
)) {
500 break; //great read - finish
502 if (errCnt
<bestErrCnt
){ //set this as new best run
509 if (bestErrCnt
<=maxErr
){
510 //best run is good enough - set to best run and overwrite BinStream
512 lastBit
= bestStart
- *clk
;
514 for (i
= iii
; i
< *size
; ++i
) {
515 if ((BinStream
[i
] >= high
) && ((i
-lastBit
) > (*clk
-tol
))){
517 BinStream
[bitnum
] = *invert
;
520 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
) > (*clk
-tol
))){
521 //low found and we are expecting a bar
523 BinStream
[bitnum
] = 1 - *invert
;
526 } else if ((BinStream
[i
]<=low
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
529 BinStream
[bitnum
] = 1 - *invert
;
531 } else if ((BinStream
[i
]>=high
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
534 BinStream
[bitnum
] = *invert
;
536 } else if ((i
-lastBit
)>((*clk
/2)+tol
) && (midBit
==0)){
539 if (bitnum
!=0) BinStream
[bitnum
] = BinStream
[bitnum
-1];
543 //mid value found or no bar supposed to be here
544 if ((i
-lastBit
)>(*clk
+tol
)){
545 //should have hit a high or low based on clock!!
548 //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);
550 BinStream
[bitnum
]=77;
553 lastBit
+=*clk
;//skip over error
556 if (bitnum
>= MaxBits
) break;
567 // demod gProxIIDemod
568 // error returns as -x
569 // success returns start position in BitStream
570 // BitStream must contain previously askrawdemod and biphasedemoded data
571 int gProxII_Demod(uint8_t BitStream
[], size_t *size
)
574 uint8_t preamble
[] = {1,1,1,1,1,0};
576 uint8_t errChk
= preambleSearch(BitStream
, preamble
, sizeof(preamble
), size
, &startIdx
);
577 if (errChk
== 0) return -3; //preamble not found
578 if (*size
!= 96) return -2; //should have found 96 bits
579 //check first 6 spacer bits to verify format
580 if (!BitStream
[startIdx
+5] && !BitStream
[startIdx
+10] && !BitStream
[startIdx
+15] && !BitStream
[startIdx
+20] && !BitStream
[startIdx
+25] && !BitStream
[startIdx
+30]){
581 //confirmed proper separator bits found
582 //return start position
583 return (int) startIdx
;
588 //translate wave to 11111100000 (1 for each short wave 0 for each long wave)
589 size_t fsk_wave_demod(uint8_t * dest
, size_t size
, uint8_t fchigh
, uint8_t fclow
)
591 uint32_t last_transition
= 0;
594 if (fchigh
==0) fchigh
=10;
595 if (fclow
==0) fclow
=8;
596 //set the threshold close to 0 (graph) or 128 std to avoid static
597 uint8_t threshold_value
= 123;
599 // sync to first lo-hi transition, and threshold
601 // Need to threshold first sample
603 if(dest
[0] < threshold_value
) dest
[0] = 0;
607 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
608 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
609 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
610 for(idx
= 1; idx
< size
; idx
++) {
611 // threshold current value
613 if (dest
[idx
] < threshold_value
) dest
[idx
] = 0;
616 // Check for 0->1 transition
617 if (dest
[idx
-1] < dest
[idx
]) { // 0 -> 1 transition
618 if ((idx
-last_transition
)<(fclow
-2)){ //0-5 = garbage noise
619 //do nothing with extra garbage
620 } else if ((idx
-last_transition
) < (fchigh
-1)) { //6-8 = 8 waves
622 } else { //9+ = 10 waves
625 last_transition
= idx
;
629 return numBits
; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
632 uint32_t myround2(float f
)
634 if (f
>= 2000) return 2000;//something bad happened
635 return (uint32_t) (f
+ (float)0.5);
638 //translate 11111100000 to 10
639 size_t aggregate_bits(uint8_t *dest
, size_t size
, uint8_t rfLen
, uint8_t maxConsequtiveBits
,
640 uint8_t invert
, uint8_t fchigh
, uint8_t fclow
)
642 uint8_t lastval
=dest
[0];
647 for( idx
=1; idx
< size
; idx
++) {
649 if (dest
[idx
]==lastval
) {
653 //if lastval was 1, we have a 1->0 crossing
654 if ( dest
[idx
-1]==1 ) {
655 n
=myround2((float)(n
+1)/((float)(rfLen
)/(float)fclow
));
656 } else {// 0->1 crossing
657 n
=myround2((float)(n
+1)/((float)(rfLen
-1)/(float)fchigh
)); //-1 for fudge factor
661 if(n
< maxConsequtiveBits
) //Consecutive
663 if(invert
==0){ //invert bits
664 memset(dest
+numBits
, dest
[idx
-1] , n
);
666 memset(dest
+numBits
, dest
[idx
-1]^1 , n
);
675 //by marshmellow (from holiman's base)
676 // full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod)
677 int fskdemod(uint8_t *dest
, size_t size
, uint8_t rfLen
, uint8_t invert
, uint8_t fchigh
, uint8_t fclow
)
680 size
= fsk_wave_demod(dest
, size
, fchigh
, fclow
);
681 size
= aggregate_bits(dest
, size
, rfLen
, 192, invert
, fchigh
, fclow
);
685 // loop to get raw HID waveform then FSK demodulate the TAG ID from it
686 int HIDdemodFSK(uint8_t *dest
, size_t *size
, uint32_t *hi2
, uint32_t *hi
, uint32_t *lo
)
688 if (justNoise(dest
, *size
)) return -1;
690 size_t numStart
=0, size2
=*size
, startIdx
=0;
692 *size
= fskdemod(dest
, size2
,50,1,10,8); //fsk2a
693 if (*size
< 96) return -2;
694 // 00011101 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1
695 uint8_t preamble
[] = {0,0,0,1,1,1,0,1};
696 // find bitstring in array
697 uint8_t errChk
= preambleSearch(dest
, preamble
, sizeof(preamble
), size
, &startIdx
);
698 if (errChk
== 0) return -3; //preamble not found
700 numStart
= startIdx
+ sizeof(preamble
);
701 // final loop, go over previously decoded FSK data and manchester decode into usable tag ID
702 for (size_t idx
= numStart
; (idx
-numStart
) < *size
- sizeof(preamble
); idx
+=2){
703 if (dest
[idx
] == dest
[idx
+1]){
704 return -4; //not manchester data
706 *hi2
= (*hi2
<<1)|(*hi
>>31);
707 *hi
= (*hi
<<1)|(*lo
>>31);
708 //Then, shift in a 0 or one into low
709 if (dest
[idx
] && !dest
[idx
+1]) // 1 0
714 return (int)startIdx
;
717 // loop to get raw paradox waveform then FSK demodulate the TAG ID from it
718 int ParadoxdemodFSK(uint8_t *dest
, size_t *size
, uint32_t *hi2
, uint32_t *hi
, uint32_t *lo
)
720 if (justNoise(dest
, *size
)) return -1;
722 size_t numStart
=0, size2
=*size
, startIdx
=0;
724 *size
= fskdemod(dest
, size2
,50,1,10,8); //fsk2a
725 if (*size
< 96) return -2;
727 // 00001111 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1
728 uint8_t preamble
[] = {0,0,0,0,1,1,1,1};
730 uint8_t errChk
= preambleSearch(dest
, preamble
, sizeof(preamble
), size
, &startIdx
);
731 if (errChk
== 0) return -3; //preamble not found
733 numStart
= startIdx
+ sizeof(preamble
);
734 // final loop, go over previously decoded FSK data and manchester decode into usable tag ID
735 for (size_t idx
= numStart
; (idx
-numStart
) < *size
- sizeof(preamble
); idx
+=2){
736 if (dest
[idx
] == dest
[idx
+1])
737 return -4; //not manchester data
738 *hi2
= (*hi2
<<1)|(*hi
>>31);
739 *hi
= (*hi
<<1)|(*lo
>>31);
740 //Then, shift in a 0 or one into low
741 if (dest
[idx
] && !dest
[idx
+1]) // 1 0
746 return (int)startIdx
;
749 uint32_t bytebits_to_byte(uint8_t* src
, size_t numbits
)
752 for(int i
= 0 ; i
< numbits
; i
++)
754 num
= (num
<< 1) | (*src
);
760 int IOdemodFSK(uint8_t *dest
, size_t size
)
762 if (justNoise(dest
, size
)) return -1;
763 //make sure buffer has data
764 if (size
< 66*64) return -2;
766 size
= fskdemod(dest
, size
, 64, 1, 10, 8); // FSK2a RF/64
767 if (size
< 65) return -3; //did we get a good demod?
769 //0 10 20 30 40 50 60
771 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
772 //-----------------------------------------------------------------------------
773 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
775 //XSF(version)facility:codeone+codetwo
778 uint8_t preamble
[] = {0,0,0,0,0,0,0,0,0,1};
779 uint8_t errChk
= preambleSearch(dest
, preamble
, sizeof(preamble
), &size
, &startIdx
);
780 if (errChk
== 0) return -4; //preamble not found
782 if (!dest
[startIdx
+8] && dest
[startIdx
+17]==1 && dest
[startIdx
+26]==1 && dest
[startIdx
+35]==1 && dest
[startIdx
+44]==1 && dest
[startIdx
+53]==1){
783 //confirmed proper separator bits found
784 //return start position
785 return (int) startIdx
;
791 // takes a array of binary values, start position, length of bits per parity (includes parity bit),
792 // Parity Type (1 for odd 0 for even), and binary Length (length to run)
793 size_t removeParity(uint8_t *BitStream
, size_t startIdx
, uint8_t pLen
, uint8_t pType
, size_t bLen
)
795 uint32_t parityWd
= 0;
796 size_t j
= 0, bitCnt
= 0;
797 for (int word
= 0; word
< (bLen
); word
+=pLen
){
798 for (int bit
=0; bit
< pLen
; bit
++){
799 parityWd
= (parityWd
<< 1) | BitStream
[startIdx
+word
+bit
];
800 BitStream
[j
++] = (BitStream
[startIdx
+word
+bit
]);
803 // if parity fails then return 0
804 if (parityTest(parityWd
, pLen
, pType
) == 0) return -1;
808 // if we got here then all the parities passed
809 //return ID start index and size
814 // FSK Demod then try to locate an AWID ID
815 int AWIDdemodFSK(uint8_t *dest
, size_t *size
)
817 //make sure buffer has enough data
818 if (*size
< 96*50) return -1;
820 if (justNoise(dest
, *size
)) return -2;
823 *size
= fskdemod(dest
, *size
, 50, 1, 10, 8); // fsk2a RF/50
824 if (*size
< 96) return -3; //did we get a good demod?
826 uint8_t preamble
[] = {0,0,0,0,0,0,0,1};
828 uint8_t errChk
= preambleSearch(dest
, preamble
, sizeof(preamble
), size
, &startIdx
);
829 if (errChk
== 0) return -4; //preamble not found
830 if (*size
!= 96) return -5;
831 return (int)startIdx
;
835 // FSK Demod then try to locate an Farpointe Data (pyramid) ID
836 int PyramiddemodFSK(uint8_t *dest
, size_t *size
)
838 //make sure buffer has data
839 if (*size
< 128*50) return -5;
841 //test samples are not just noise
842 if (justNoise(dest
, *size
)) return -1;
845 *size
= fskdemod(dest
, *size
, 50, 1, 10, 8); // fsk2a RF/50
846 if (*size
< 128) return -2; //did we get a good demod?
848 uint8_t preamble
[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
850 uint8_t errChk
= preambleSearch(dest
, preamble
, sizeof(preamble
), size
, &startIdx
);
851 if (errChk
== 0) return -4; //preamble not found
852 if (*size
!= 128) return -3;
853 return (int)startIdx
;
857 uint8_t DetectCleanAskWave(uint8_t dest
[], size_t size
, int high
, int low
)
861 for (size_t i
=20; i
<255; i
++){
862 if (dest
[i
]>low
&& dest
[i
]<high
)
868 if (cntPeaks
>190) return 1;
874 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
875 // maybe somehow adjust peak trimming value based on samples to fix?
876 // return start index of best starting position for that clock and return clock (by reference)
877 int DetectASKClock(uint8_t dest
[], size_t size
, int *clock
, int maxErr
)
880 int clk
[]={8,16,32,40,50,64,100,128,256};
881 int loopCnt
= 256; //don't need to loop through entire array...
882 if (size
== 0) return -1;
883 if (size
<loopCnt
) loopCnt
= size
;
884 //if we already have a valid clock quit
887 if (clk
[i
] == *clock
) return 0;
889 //get high and low peak
891 getHiLo(dest
, loopCnt
, &peak
, &low
, 75, 75);
893 //test for large clean peaks
894 if (DetectCleanAskWave(dest
, size
, peak
, low
)==1){
897 fcTest
=countFC(dest
, size
, &mostFC
);
898 uint8_t fc1
= fcTest
>> 8;
899 uint8_t fc2
= fcTest
& 0xFF;
916 int bestErr
[]={1000,1000,1000,1000,1000,1000,1000,1000,1000};
917 int bestStart
[]={0,0,0,0,0,0,0,0,0};
919 //test each valid clock from smallest to greatest to see which lines up
920 for(clkCnt
=0; clkCnt
< 8; clkCnt
++){
921 if (clk
[clkCnt
] == 32){
926 bestErr
[clkCnt
]=1000;
927 //try lining up the peaks by moving starting point (try first 256)
928 for (ii
=0; ii
< loopCnt
; ii
++){
929 if ((dest
[ii
] >= peak
) || (dest
[ii
] <= low
)){
931 // now that we have the first one lined up test rest of wave array
932 for (i
=0; i
<((int)((size
-ii
-tol
)/clk
[clkCnt
])-1); ++i
){
933 if (dest
[ii
+(i
*clk
[clkCnt
])]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])]<=low
){
934 }else if(dest
[ii
+(i
*clk
[clkCnt
])-tol
]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])-tol
]<=low
){
935 }else if(dest
[ii
+(i
*clk
[clkCnt
])+tol
]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])+tol
]<=low
){
936 }else{ //error no peak detected
940 //if we found no errors then we can stop here
941 // this is correct one - return this clock
942 //PrintAndLog("DEBUG: clk %d, err %d, ii %d, i %d",clk[clkCnt],errCnt,ii,i);
943 if(errCnt
==0 && clkCnt
<6) {
944 *clock
= clk
[clkCnt
];
947 //if we found errors see if it is lowest so far and save it as best run
948 if(errCnt
<bestErr
[clkCnt
]){
949 bestErr
[clkCnt
]=errCnt
;
950 bestStart
[clkCnt
]=ii
;
957 for (iii
=0; iii
<8; ++iii
){
958 if (bestErr
[iii
]<bestErr
[best
]){
959 if (bestErr
[iii
]==0) bestErr
[iii
]=1;
960 // current best bit to error ratio vs new bit to error ratio
961 if (((size
/clk
[best
])/bestErr
[best
] < (size
/clk
[iii
])/bestErr
[iii
]) ){
966 if (bestErr
[best
]>maxErr
) return -1;
968 return bestStart
[best
];
972 //detect psk clock by reading each phase shift
973 // a phase shift is determined by measuring the sample length of each wave
974 int DetectPSKClock(uint8_t dest
[], size_t size
, int clock
)
976 uint8_t clk
[]={255,16,32,40,50,64,100,128,255}; //255 is not a valid clock
977 uint16_t loopCnt
= 4096; //don't need to loop through entire array...
978 if (size
== 0) return 0;
979 if (size
<loopCnt
) loopCnt
= size
;
981 //if we already have a valid clock quit
984 if (clk
[i
] == clock
) return clock
;
986 size_t waveStart
=0, waveEnd
=0, firstFullWave
=0, lastClkBit
=0;
987 uint8_t clkCnt
, fc
=0, fullWaveLen
=0, tol
=1;
988 uint16_t peakcnt
=0, errCnt
=0, waveLenCnt
=0;
989 uint16_t bestErr
[]={1000,1000,1000,1000,1000,1000,1000,1000,1000};
990 uint16_t peaksdet
[]={0,0,0,0,0,0,0,0,0};
991 countFC(dest
, size
, &fc
);
992 //PrintAndLog("DEBUG: FC: %d",fc);
994 //find first full wave
995 for (i
=0; i
<loopCnt
; i
++){
996 if (dest
[i
] < dest
[i
+1] && dest
[i
+1] >= dest
[i
+2]){
997 if (waveStart
== 0) {
999 //PrintAndLog("DEBUG: waveStart: %d",waveStart);
1002 //PrintAndLog("DEBUG: waveEnd: %d",waveEnd);
1003 waveLenCnt
= waveEnd
-waveStart
;
1004 if (waveLenCnt
> fc
){
1005 firstFullWave
= waveStart
;
1006 fullWaveLen
=waveLenCnt
;
1013 //PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);
1015 //test each valid clock from greatest to smallest to see which lines up
1016 for(clkCnt
=7; clkCnt
>= 1 ; clkCnt
--){
1017 lastClkBit
= firstFullWave
; //set end of wave as clock align
1021 //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d",clk[clkCnt],lastClkBit);
1023 for (i
= firstFullWave
+fullWaveLen
-1; i
< loopCnt
-2; i
++){
1024 //top edge of wave = start of new wave
1025 if (dest
[i
] < dest
[i
+1] && dest
[i
+1] >= dest
[i
+2]){
1026 if (waveStart
== 0) {
1031 waveLenCnt
= waveEnd
-waveStart
;
1032 if (waveLenCnt
> fc
){
1033 //if this wave is a phase shift
1034 //PrintAndLog("DEBUG: phase shift at: %d, len: %d, nextClk: %d, ii: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+clk[clkCnt]-tol,ii+1,fc);
1035 if (i
+1 >= lastClkBit
+ clk
[clkCnt
] - tol
){ //should be a clock bit
1037 lastClkBit
+=clk
[clkCnt
];
1038 } else if (i
<lastClkBit
+8){
1039 //noise after a phase shift - ignore
1040 } else { //phase shift before supposed to based on clock
1043 } else if (i
+1 > lastClkBit
+ clk
[clkCnt
] + tol
+ fc
){
1044 lastClkBit
+=clk
[clkCnt
]; //no phase shift but clock bit
1053 if (errCnt
<= bestErr
[clkCnt
]) bestErr
[clkCnt
]=errCnt
;
1054 if (peakcnt
> peaksdet
[clkCnt
]) peaksdet
[clkCnt
]=peakcnt
;
1056 //all tested with errors
1057 //return the highest clk with the most peaks found
1059 for (i
=7; i
>=1; i
--){
1060 if (peaksdet
[i
] > peaksdet
[best
]) {
1063 //PrintAndLog("DEBUG: Clk: %d, peaks: %d, errs: %d, bestClk: %d",clk[iii],peaksdet[iii],bestErr[iii],clk[best]);
1069 //detect nrz clock by reading #peaks vs no peaks(or errors)
1070 int DetectNRZClock(uint8_t dest
[], size_t size
, int clock
)
1073 int clk
[]={8,16,32,40,50,64,100,128,256};
1074 int loopCnt
= 4096; //don't need to loop through entire array...
1075 if (size
== 0) return 0;
1076 if (size
<loopCnt
) loopCnt
= size
;
1078 //if we already have a valid clock quit
1080 if (clk
[i
] == clock
) return clock
;
1082 //get high and low peak
1084 getHiLo(dest
, loopCnt
, &peak
, &low
, 75, 75);
1086 //PrintAndLog("DEBUG: peak: %d, low: %d",peak,low);
1091 int peaksdet
[]={0,0,0,0,0,0,0,0};
1093 //test for large clipped waves
1094 for (i
=0; i
<loopCnt
; i
++){
1095 if (dest
[i
] >= peak
|| dest
[i
] <= low
){
1098 if (peakcnt
>0 && maxPeak
< peakcnt
){
1105 //test each valid clock from smallest to greatest to see which lines up
1106 for(clkCnt
=0; clkCnt
< 8; ++clkCnt
){
1107 //ignore clocks smaller than largest peak
1108 if (clk
[clkCnt
]<maxPeak
) continue;
1110 //try lining up the peaks by moving starting point (try first 256)
1111 for (ii
=0; ii
< loopCnt
; ++ii
){
1112 if ((dest
[ii
] >= peak
) || (dest
[ii
] <= low
)){
1114 // now that we have the first one lined up test rest of wave array
1115 for (i
=0; i
< ((int)((size
-ii
-tol
)/clk
[clkCnt
])-1); ++i
){
1116 if (dest
[ii
+(i
*clk
[clkCnt
])]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])]<=low
){
1120 if(peakcnt
>peaksdet
[clkCnt
]) {
1121 peaksdet
[clkCnt
]=peakcnt
;
1128 for (iii
=7; iii
> 0; iii
--){
1129 if (peaksdet
[iii
] > peaksdet
[best
]){
1132 //PrintAndLog("DEBUG: Clk: %d, peaks: %d, errs: %d, bestClk: %d",clk[iii],peaksdet[iii],bestErr[iii],clk[best]);
1138 // convert psk1 demod to psk2 demod
1139 // only transition waves are 1s
1140 void psk1TOpsk2(uint8_t *BitStream
, size_t size
)
1143 uint8_t lastBit
=BitStream
[0];
1144 for (; i
<size
; i
++){
1145 if (BitStream
[i
]==77){
1147 } else if (lastBit
!=BitStream
[i
]){
1148 lastBit
=BitStream
[i
];
1158 // convert psk2 demod to psk1 demod
1159 // from only transition waves are 1s to phase shifts change bit
1160 void psk2TOpsk1(uint8_t *BitStream
, size_t size
)
1163 for (size_t i
=0; i
<size
; i
++){
1164 if (BitStream
[i
]==1){
1172 // redesigned by marshmellow adjusted from existing decode functions
1173 // indala id decoding - only tested on 26 bit tags, but attempted to make it work for more
1174 int indala26decode(uint8_t *bitStream
, size_t *size
, uint8_t *invert
)
1176 //26 bit 40134 format (don't know other formats)
1178 int long_wait
=29;//29 leading zeros in format
1184 // Finding the start of a UID
1185 for (start
= 0; start
<= *size
- 250; start
++) {
1186 first
= bitStream
[start
];
1187 for (i
= start
; i
< start
+ long_wait
; i
++) {
1188 if (bitStream
[i
] != first
) {
1192 if (i
== (start
+ long_wait
)) {
1196 if (start
== *size
- 250 + 1) {
1197 // did not find start sequence
1200 // Inverting signal if needed
1202 for (i
= start
; i
< *size
; i
++) {
1203 bitStream
[i
] = !bitStream
[i
];
1209 //found start once now test length by finding next one
1210 for (ii
=start
+29; ii
<= *size
- 250; ii
++) {
1211 first2
= bitStream
[ii
];
1212 for (iii
= ii
; iii
< ii
+ long_wait
; iii
++) {
1213 if (bitStream
[iii
] != first2
) {
1217 if (iii
== (ii
+ long_wait
)) {
1221 if (ii
== *size
- 250 + 1){
1222 // did not find second start sequence
1229 for (ii
= 0; ii
< bitCnt
; ii
++) {
1230 bitStream
[ii
] = bitStream
[i
++];
1236 // by marshmellow - demodulate NRZ wave (both similar enough)
1237 // peaks invert bit (high=1 low=0) each clock cycle = 1 bit determined by last peak
1238 // there probably is a much simpler way to do this....
1239 int nrzRawDemod(uint8_t *dest
, size_t *size
, int *clk
, int *invert
, int maxErr
)
1241 if (justNoise(dest
, *size
)) return -1;
1242 *clk
= DetectNRZClock(dest
, *size
, *clk
);
1243 if (*clk
==0) return -2;
1246 ans
= getHiLo(dest
, 1260, &high
, &low
, 75, 75); //25% fuzz on high 25% fuzz on low
1247 if (ans
<1) return -2; //just noise
1248 uint32_t gLen
= 256;
1249 if (gLen
>*size
) gLen
= *size
;
1250 int lastBit
= 0; //set first clock check
1251 uint32_t bitnum
= 0; //output counter
1252 uint8_t tol
= 1; //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
1255 uint16_t MaxBits
= 1000;
1256 uint32_t bestErrCnt
= maxErr
+1;
1257 uint32_t bestPeakCnt
= 0;
1258 uint32_t bestPeakStart
=0;
1261 uint8_t errBitHigh
=0;
1263 uint8_t ignoreWindow
=4;
1264 uint8_t ignoreCnt
=ignoreWindow
; //in case of noice near peak
1265 //loop to find first wave that works - align to clock
1266 for (iii
=0; iii
< gLen
; ++iii
){
1267 if ((dest
[iii
]>=high
) || (dest
[iii
]<=low
)){
1272 //loop through to see if this start location works
1273 for (i
= iii
; i
< *size
; ++i
) {
1274 //if we found a high bar and we are at a clock bit
1275 if ((dest
[i
]>=high
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
1281 ignoreCnt
=ignoreWindow
;
1282 //else if low bar found and we are at a clock point
1283 }else if ((dest
[i
]<=low
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
1289 ignoreCnt
=ignoreWindow
;
1290 //else if no bars found
1291 }else if(dest
[i
] < high
&& dest
[i
] > low
) {
1301 //if we are past a clock point
1302 if (i
>= lastBit
+*clk
+tol
){ //clock val
1306 //else if bar found but we are not at a clock bit and we did not just have a clock bit
1307 }else if ((dest
[i
]>=high
|| dest
[i
]<=low
) && (i
<lastBit
+*clk
-tol
|| i
>lastBit
+*clk
+tol
) && (bitHigh
==0)){
1308 //error bar found no clock...
1311 if (bitnum
>=MaxBits
) break;
1313 //we got more than 64 good bits and not all errors
1314 if (bitnum
> (64) && (errCnt
<= (maxErr
))) {
1315 //possible good read
1318 bestErrCnt
= errCnt
;
1319 bestPeakCnt
= peakCnt
;
1320 bestPeakStart
= iii
;
1321 break; //great read - finish
1323 if (errCnt
< bestErrCnt
){ //set this as new best run
1324 bestErrCnt
= errCnt
;
1327 if (peakCnt
> bestPeakCnt
){
1328 bestPeakCnt
=peakCnt
;
1334 //PrintAndLog("DEBUG: bestErrCnt: %d, maxErr: %d, bestStart: %d, bestPeakCnt: %d, bestPeakStart: %d",bestErrCnt,maxErr,bestStart,bestPeakCnt,bestPeakStart);
1335 if (bestErrCnt
<= maxErr
){
1336 //best run is good enough set to best run and set overwrite BinStream
1338 lastBit
=bestPeakStart
-*clk
;
1340 for (i
= iii
; i
< *size
; ++i
) {
1341 //if we found a high bar and we are at a clock bit
1342 if ((dest
[i
] >= high
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
1346 dest
[bitnum
]=curBit
;
1349 ignoreCnt
=ignoreWindow
;
1350 //else if low bar found and we are at a clock point
1351 }else if ((dest
[i
]<=low
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
1355 dest
[bitnum
]=curBit
;
1358 ignoreCnt
=ignoreWindow
;
1359 //else if no bars found
1360 }else if(dest
[i
]<high
&& dest
[i
]>low
) {
1363 //if peak is done was it an error peak?
1373 //if we are past a clock point
1374 if (i
>=lastBit
+*clk
+tol
){ //clock val
1376 dest
[bitnum
]=curBit
;
1379 //else if bar found but we are not at a clock bit and we did not just have a clock bit
1380 }else if ((dest
[i
]>=high
|| dest
[i
]<=low
) && ((i
<lastBit
+*clk
-tol
) || (i
>lastBit
+*clk
+tol
)) && (bitHigh
==0)){
1381 //error bar found no clock...
1384 if (bitnum
>= MaxBits
) break;
1399 //detects the bit clock for FSK given the high and low Field Clocks
1400 uint8_t detectFSKClk(uint8_t *BitStream
, size_t size
, uint8_t fcHigh
, uint8_t fcLow
)
1402 uint8_t clk
[] = {8,16,32,40,50,64,100,128,0};
1403 uint16_t rfLens
[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
1404 uint8_t rfCnts
[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
1405 uint8_t rfLensFnd
= 0;
1406 uint8_t lastFCcnt
=0;
1407 uint32_t fcCounter
= 0;
1408 uint16_t rfCounter
= 0;
1409 uint8_t firstBitFnd
= 0;
1411 if (size
== 0) return 0;
1413 uint8_t fcTol
= (uint8_t)(0.5+(float)(fcHigh
-fcLow
)/2);
1418 //PrintAndLog("DEBUG: fcTol: %d",fcTol);
1419 // prime i to first up transition
1420 for (i
= 1; i
< size
-1; i
++)
1421 if (BitStream
[i
] > BitStream
[i
-1] && BitStream
[i
]>=BitStream
[i
+1])
1424 for (; i
< size
-1; i
++){
1425 if (BitStream
[i
] > BitStream
[i
-1] && BitStream
[i
]>=BitStream
[i
+1]){
1429 // if we got less than the small fc + tolerance then set it to the small fc
1430 if (fcCounter
< fcLow
+fcTol
)
1432 else //set it to the large fc
1435 //look for bit clock (rf/xx)
1436 if ((fcCounter
<lastFCcnt
|| fcCounter
>lastFCcnt
)){
1437 //not the same size as the last wave - start of new bit sequence
1439 if (firstBitFnd
>1){ //skip first wave change - probably not a complete bit
1440 for (int ii
=0; ii
<15; ii
++){
1441 if (rfLens
[ii
]==rfCounter
){
1447 if (rfCounter
>0 && rfLensFnd
<15){
1448 //PrintAndLog("DEBUG: rfCntr %d, fcCntr %d",rfCounter,fcCounter);
1449 rfCnts
[rfLensFnd
]++;
1450 rfLens
[rfLensFnd
++]=rfCounter
;
1456 lastFCcnt
=fcCounter
;
1465 uint8_t rfHighest
=15, rfHighest2
=15, rfHighest3
=15;
1467 for (i
=0; i
<15; i
++){
1468 //PrintAndLog("DEBUG: RF %d, cnts %d",rfLens[i], rfCnts[i]);
1469 //get highest 2 RF values (might need to get more values to compare or compare all?)
1470 if (rfCnts
[i
]>rfCnts
[rfHighest
]){
1471 rfHighest3
=rfHighest2
;
1472 rfHighest2
=rfHighest
;
1474 } else if(rfCnts
[i
]>rfCnts
[rfHighest2
]){
1475 rfHighest3
=rfHighest2
;
1477 } else if(rfCnts
[i
]>rfCnts
[rfHighest3
]){
1481 // set allowed clock remainder tolerance to be 1 large field clock length+1
1482 // we could have mistakenly made a 9 a 10 instead of an 8 or visa versa so rfLens could be 1 FC off
1483 uint8_t tol1
= fcHigh
+1;
1485 //PrintAndLog("DEBUG: hightest: 1 %d, 2 %d, 3 %d",rfLens[rfHighest],rfLens[rfHighest2],rfLens[rfHighest3]);
1487 // loop to find the highest clock that has a remainder less than the tolerance
1488 // compare samples counted divided by
1490 for (; ii
>=0; ii
--){
1491 if (rfLens
[rfHighest
] % clk
[ii
] < tol1
|| rfLens
[rfHighest
] % clk
[ii
] > clk
[ii
]-tol1
){
1492 if (rfLens
[rfHighest2
] % clk
[ii
] < tol1
|| rfLens
[rfHighest2
] % clk
[ii
] > clk
[ii
]-tol1
){
1493 if (rfLens
[rfHighest3
] % clk
[ii
] < tol1
|| rfLens
[rfHighest3
] % clk
[ii
] > clk
[ii
]-tol1
){
1500 if (ii
<0) return 0; // oops we went too far
1506 //countFC is to detect the field clock lengths.
1507 //counts and returns the 2 most common wave lengths
1508 //mainly used for FSK field clock detection
1509 uint16_t countFC(uint8_t *BitStream
, size_t size
, uint8_t *mostFC
)
1511 uint8_t fcLens
[] = {0,0,0,0,0,0,0,0,0,0};
1512 uint16_t fcCnts
[] = {0,0,0,0,0,0,0,0,0,0};
1513 uint8_t fcLensFnd
= 0;
1514 uint8_t lastFCcnt
=0;
1515 uint32_t fcCounter
= 0;
1517 if (size
== 0) return 0;
1519 // prime i to first up transition
1520 for (i
= 1; i
< size
-1; i
++)
1521 if (BitStream
[i
] > BitStream
[i
-1] && BitStream
[i
] >= BitStream
[i
+1])
1524 for (; i
< size
-1; i
++){
1525 if (BitStream
[i
] > BitStream
[i
-1] && BitStream
[i
] >= BitStream
[i
+1]){
1526 // new up transition
1529 //if we had 5 and now have 9 then go back to 8 (for when we get a fc 9 instead of an 8)
1530 if (lastFCcnt
==5 && fcCounter
==9) fcCounter
--;
1531 //if odd and not rc/5 add one (for when we get a fc 9 instead of 10)
1532 if ((fcCounter
==9 && fcCounter
& 1) || fcCounter
==4) fcCounter
++;
1534 // save last field clock count (fc/xx)
1535 // find which fcLens to save it to:
1536 for (int ii
=0; ii
<10; ii
++){
1537 if (fcLens
[ii
]==fcCounter
){
1543 if (fcCounter
>0 && fcLensFnd
<10){
1545 fcCnts
[fcLensFnd
]++;
1546 fcLens
[fcLensFnd
++]=fcCounter
;
1555 uint8_t best1
=9, best2
=9, best3
=9;
1557 // go through fclens and find which ones are bigest 2
1558 for (i
=0; i
<10; i
++){
1559 // PrintAndLog("DEBUG: FC %d, Cnt %d, Errs %d",fcLens[i],fcCnts[i],errCnt);
1560 // get the 3 best FC values
1561 if (fcCnts
[i
]>maxCnt1
) {
1566 } else if(fcCnts
[i
]>fcCnts
[best2
]){
1569 } else if(fcCnts
[i
]>fcCnts
[best3
]){
1573 uint8_t fcH
=0, fcL
=0;
1574 if (fcLens
[best1
]>fcLens
[best2
]){
1582 *mostFC
=fcLens
[best1
];
1583 // TODO: take top 3 answers and compare to known Field clocks to get top 2
1585 uint16_t fcs
= (((uint16_t)fcH
)<<8) | fcL
;
1586 // PrintAndLog("DEBUG: Best %d best2 %d best3 %d",fcLens[best1],fcLens[best2],fcLens[best3]);
1592 //countPSK_FC is to detect the psk carrier clock length.
1593 //counts and returns the 1 most common wave length
1594 uint8_t countPSK_FC(uint8_t *BitStream
, size_t size
)
1596 uint8_t fcLens
[] = {0,0,0,0,0,0,0,0,0,0};
1597 uint16_t fcCnts
[] = {0,0,0,0,0,0,0,0,0,0};
1598 uint8_t fcLensFnd
= 0;
1599 uint32_t fcCounter
= 0;
1601 if (size
== 0) return 0;
1603 // prime i to first up transition
1604 for (i
= 1; i
< size
-1; i
++)
1605 if (BitStream
[i
] > BitStream
[i
-1] && BitStream
[i
] >= BitStream
[i
+1])
1608 for (; i
< size
-1; i
++){
1609 if (BitStream
[i
] > BitStream
[i
-1] && BitStream
[i
] >= BitStream
[i
+1]){
1610 // new up transition
1613 // save last field clock count (fc/xx)
1614 // find which fcLens to save it to:
1615 for (int ii
=0; ii
<10; ii
++){
1616 if (fcLens
[ii
]==fcCounter
){
1622 if (fcCounter
>0 && fcLensFnd
<10){
1624 fcCnts
[fcLensFnd
]++;
1625 fcLens
[fcLensFnd
++]=fcCounter
;
1636 // go through fclens and find which ones are bigest
1637 for (i
=0; i
<10; i
++){
1638 //PrintAndLog("DEBUG: FC %d, Cnt %d",fcLens[i],fcCnts[i]);
1639 // get the best FC value
1640 if (fcCnts
[i
]>maxCnt1
) {
1645 return fcLens
[best1
];
1648 //by marshmellow - demodulate PSK1 wave
1649 //uses wave lengths (# Samples)
1650 int pskRawDemod(uint8_t dest
[], size_t *size
, int *clock
, int *invert
)
1652 uint16_t loopCnt
= 4096; //don't need to loop through entire array...
1653 if (size
== 0) return -1;
1654 if (*size
<loopCnt
) loopCnt
= *size
;
1656 uint8_t curPhase
= *invert
;
1657 size_t i
, waveStart
=1, waveEnd
=0, firstFullWave
=0, lastClkBit
=0;
1658 uint8_t fc
=0, fullWaveLen
=0, tol
=1;
1659 uint16_t errCnt
=0, waveLenCnt
=0;
1660 fc
= countPSK_FC(dest
, *size
);
1661 if (fc
!=2 && fc
!=4 && fc
!=8) return -1;
1662 //PrintAndLog("DEBUG: FC: %d",fc);
1663 *clock
= DetectPSKClock(dest
, *size
, *clock
);
1664 if (*clock
==0) return -1;
1665 int avgWaveVal
=0, lastAvgWaveVal
=0;
1666 //find first phase shift
1667 for (i
=0; i
<loopCnt
; i
++){
1668 if (dest
[i
]+fc
< dest
[i
+1] && dest
[i
+1] >= dest
[i
+2]){
1670 //PrintAndLog("DEBUG: waveEnd: %d",waveEnd);
1671 waveLenCnt
= waveEnd
-waveStart
;
1672 if (waveLenCnt
> fc
&& waveStart
> fc
){ //not first peak and is a large wave
1673 lastAvgWaveVal
= avgWaveVal
/(waveLenCnt
);
1674 firstFullWave
= waveStart
;
1675 fullWaveLen
=waveLenCnt
;
1676 //if average wave value is > graph 0 then it is an up wave or a 1
1677 if (lastAvgWaveVal
> 123) curPhase
^=1; //fudge graph 0 a little 123 vs 128
1683 avgWaveVal
+=dest
[i
+2];
1685 //PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);
1686 lastClkBit
= firstFullWave
; //set start of wave as clock align
1687 //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d", *clock, lastClkBit);
1692 memset(dest
+numBits
,curPhase
^1,firstFullWave
/ *clock
);
1693 numBits
+= (firstFullWave
/ *clock
);
1694 dest
[numBits
++] = curPhase
; //set first read bit
1695 for (i
= firstFullWave
+fullWaveLen
-1; i
< *size
-3; i
++){
1696 //top edge of wave = start of new wave
1697 if (dest
[i
]+fc
< dest
[i
+1] && dest
[i
+1] >= dest
[i
+2]){
1698 if (waveStart
== 0) {
1701 avgWaveVal
= dest
[i
+1];
1704 waveLenCnt
= waveEnd
-waveStart
;
1705 lastAvgWaveVal
= avgWaveVal
/waveLenCnt
;
1706 if (waveLenCnt
> fc
){
1707 //PrintAndLog("DEBUG: avgWaveVal: %d, waveSum: %d",lastAvgWaveVal,avgWaveVal);
1708 //if this wave is a phase shift
1709 //PrintAndLog("DEBUG: phase shift at: %d, len: %d, nextClk: %d, i: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+*clock-tol,i+1,fc);
1710 if (i
+1 >= lastClkBit
+ *clock
- tol
){ //should be a clock bit
1712 dest
[numBits
++] = curPhase
;
1713 lastClkBit
+= *clock
;
1714 } else if (i
<lastClkBit
+10+fc
){
1715 //noise after a phase shift - ignore
1716 } else { //phase shift before supposed to based on clock
1718 dest
[numBits
++] = 77;
1720 } else if (i
+1 > lastClkBit
+ *clock
+ tol
+ fc
){
1721 lastClkBit
+= *clock
; //no phase shift but clock bit
1722 dest
[numBits
++] = curPhase
;
1728 avgWaveVal
+=dest
[i
+1];