]>
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 //get high and low with passed in fuzz factor. also return noise test = 1 for passed or 0 for only noise
17 int getHiLo(uint8_t *BitStream
, size_t size
, int *high
, int *low
, uint8_t fuzzHi
, uint8_t fuzzLo
)
21 // get high and low thresholds
22 for (int i
=0; i
< size
; i
++){
23 if (BitStream
[i
] > *high
) *high
= BitStream
[i
];
24 if (BitStream
[i
] < *low
) *low
= BitStream
[i
];
26 if (*high
< 123) return -1; // just noise
27 *high
= (int)(((*high
-128)*(((float)fuzzHi
)/100))+128);
28 *low
= (int)(((*low
-128)*(((float)fuzzLo
)/100))+128);
33 //takes 1s and 0s and searches for EM410x format - output EM ID
34 uint64_t Em410xDecode(uint8_t *BitStream
, size_t size
)
36 //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
37 // otherwise could be a void with no arguments
41 if (BitStream
[10]>1){ //allow only 1s and 0s
42 // PrintAndLog("no data found");
46 // 111111111 bit pattern represent start of frame
47 uint8_t frame_marker_mask
[] = {1,1,1,1,1,1,1,1,1};
51 while( (idx
+ 64) < size
) {
53 // search for a start of frame marker
54 if ( memcmp(BitStream
+idx
, frame_marker_mask
, sizeof(frame_marker_mask
)) == 0)
55 { // frame marker found
58 for(ii
=0; ii
<5; ++ii
){
59 parityTest
^= BitStream
[(i
*5)+ii
+idx
];
63 for (ii
=0; ii
<4;++ii
){
64 lo
=(lo
<<1LL)|(BitStream
[(i
*5)+ii
+idx
]);
66 //PrintAndLog("DEBUG: EM parity passed parity val: %d, i:%d, ii:%d,idx:%d, Buffer: %d%d%d%d%d,lo: %d",parityTest,i,ii,idx,BitStream[idx+ii+(i*5)-5],BitStream[idx+ii+(i*5)-4],BitStream[idx+ii+(i*5)-3],BitStream[idx+ii+(i*5)-2],BitStream[idx+ii+(i*5)-1],lo);
67 }else {//parity failed
68 //PrintAndLog("DEBUG: EM parity failed parity val: %d, i:%d, ii:%d,idx:%d, Buffer: %d%d%d%d%d",parityTest,i,ii,idx,BitStream[idx+ii+(i*5)-5],BitStream[idx+ii+(i*5)-4],BitStream[idx+ii+(i*5)-3],BitStream[idx+ii+(i*5)-2],BitStream[idx+ii+(i*5)-1]);
71 if (resetCnt
>5)return 0; //try 5 times
73 goto restart
;//continue;
76 //skip last 5 bit parity test for simplicity.
86 //takes 2 arguments - clock and invert both as integers
87 //attempts to demodulate ask while decoding manchester
88 //prints binary found and saves in graphbuffer for further commands
89 int askmandemod(uint8_t *BinStream
, size_t *size
, int *clk
, int *invert
)
92 *clk
=DetectASKClock(BinStream
, *size
, *clk
); //clock default
96 if (*invert
!= 0 && *invert
!= 1) *invert
=0;
97 uint32_t initLoopMax
= 200;
98 if (initLoopMax
> *size
) initLoopMax
=*size
;
99 // Detect high and lows
100 // 25% fuzz in case highs and lows aren't clipped [marshmellow]
102 ans
= getHiLo(BinStream
, initLoopMax
, &high
, &low
, 75, 75);
103 if (ans
<1) return -2; //just noise
105 // PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
106 int lastBit
= 0; //set first clock check
107 uint32_t bitnum
= 0; //output counter
108 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
109 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
111 uint32_t gLen
= *size
;
112 if (gLen
> 3000) gLen
=3000;
114 uint32_t bestStart
= *size
;
115 uint32_t bestErrCnt
= (*size
/1000);
116 uint32_t maxErr
= (*size
/1000);
117 // PrintAndLog("DEBUG - lastbit - %d",lastBit);
118 // loop to find first wave that works
119 for (iii
=0; iii
< gLen
; ++iii
){
120 if ((BinStream
[iii
] >= high
) || (BinStream
[iii
] <= low
)){
123 // loop through to see if this start location works
124 for (i
= iii
; i
< *size
; ++i
) {
125 if ((BinStream
[i
] >= high
) && ((i
-lastBit
) > (*clk
-tol
))){
127 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
) > (*clk
-tol
))){
128 //low found and we are expecting a bar
131 //mid value found or no bar supposed to be here
132 if ((i
-lastBit
)>(*clk
+tol
)){
133 //should have hit a high or low based on clock!!
136 //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);
139 lastBit
+=*clk
;//skip over until hit too many errors
140 if (errCnt
>(maxErr
)) break; //allow 1 error for every 1000 samples else start over
143 if ((i
-iii
) >(400 * *clk
)) break; //got plenty of bits
145 //we got more than 64 good bits and not all errors
146 if ((((i
-iii
)/ *clk
) > (64+errCnt
)) && (errCnt
<maxErr
)) {
151 break; //great read - finish
153 if (errCnt
<bestErrCnt
){ //set this as new best run
160 if (bestErrCnt
<maxErr
){
161 //best run is good enough set to best run and set overwrite BinStream
163 lastBit
= bestStart
- *clk
;
165 for (i
= iii
; i
< *size
; ++i
) {
166 if ((BinStream
[i
] >= high
) && ((i
-lastBit
) > (*clk
-tol
))){
168 BinStream
[bitnum
] = *invert
;
170 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
) > (*clk
-tol
))){
171 //low found and we are expecting a bar
173 BinStream
[bitnum
] = 1-*invert
;
176 //mid value found or no bar supposed to be here
177 if ((i
-lastBit
)>(*clk
+tol
)){
178 //should have hit a high or low based on clock!!
181 //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);
183 BinStream
[bitnum
]=77;
187 lastBit
+=*clk
;//skip over error
190 if (bitnum
>=400) break;
202 //take 10 and 01 and manchester decode
203 //run through 2 times and take least errCnt
204 int manrawdecode(uint8_t * BitStream
, size_t *size
)
212 for (ii
=1;ii
<3;++ii
){
214 for (i
=i
+ii
;i
<*size
-2;i
+=2){
215 if(BitStream
[i
]==1 && (BitStream
[i
+1]==0)){
216 } else if((BitStream
[i
]==0)&& BitStream
[i
+1]==1){
220 if(bitnum
>300) break;
232 for (i
=i
+ii
;i
< *size
-2;i
+=2){
233 if(BitStream
[i
] == 1 && (BitStream
[i
+1] == 0)){
234 BitStream
[bitnum
++]=0;
235 } else if((BitStream
[i
] == 0) && BitStream
[i
+1] == 1){
236 BitStream
[bitnum
++]=1;
238 BitStream
[bitnum
++]=77;
241 if(bitnum
>300) break;
250 //take 01 or 10 = 0 and 11 or 00 = 1
251 int BiphaseRawDecode(uint8_t *BitStream
, size_t *size
, int offset
, int invert
)
257 for (;i
<*size
-2; i
+=2){
258 if((BitStream
[i
]==1 && BitStream
[i
+1]==0) || (BitStream
[i
]==0 && BitStream
[i
+1]==1)){
259 BitStream
[bitnum
++]=1^invert
;
260 } else if((BitStream
[i
]==0 && BitStream
[i
+1]==0) || (BitStream
[i
]==1 && BitStream
[i
+1]==1)){
261 BitStream
[bitnum
++]=invert
;
263 BitStream
[bitnum
++]=77;
266 if(bitnum
>250) break;
273 //takes 2 arguments - clock and invert both as integers
274 //attempts to demodulate ask only
275 //prints binary found and saves in graphbuffer for further commands
276 int askrawdemod(uint8_t *BinStream
, size_t *size
, int *clk
, int *invert
)
279 // int invert=0; //invert default
281 *clk
=DetectASKClock(BinStream
, *size
, *clk
); //clock default
282 //uint8_t BitStream[502] = {0};
284 //HACK: if clock not detected correctly - default
285 if (*clk
<8) *clk
=64;
286 if (*clk
<32 && clk2
==0) *clk
=32;
287 if (*invert
!= 0 && *invert
!= 1) *invert
=0;
288 uint32_t initLoopMax
= 200;
289 if (initLoopMax
> *size
) initLoopMax
=*size
;
290 // Detect high and lows
291 //25% fuzz in case highs and lows aren't clipped [marshmellow]
293 ans
= getHiLo(BinStream
, initLoopMax
, &high
, &low
, 75, 75);
294 if (ans
<1) return -2; //just noise
296 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
297 int lastBit
= 0; //set first clock check
298 uint32_t bitnum
= 0; //output counter
299 uint8_t tol
= 0; //clock tolerance adjust - waves will be accepted as within the clock
300 // if they fall + or - this value + clock from last valid wave
301 if (*clk
== 32) tol
=1; //clock tolerance may not be needed anymore currently set to
302 // + or - 1 but could be increased for poor waves or removed entirely
304 uint32_t gLen
= *size
;
305 if (gLen
> 500) gLen
=500;
307 uint32_t bestStart
= *size
;
308 uint32_t bestErrCnt
= (*size
/1000);
309 uint32_t maxErr
= bestErrCnt
;
311 //PrintAndLog("DEBUG - lastbit - %d",lastBit);
312 //loop to find first wave that works
313 for (iii
=0; iii
< gLen
; ++iii
){
314 if ((BinStream
[iii
]>=high
) || (BinStream
[iii
]<=low
)){
316 //loop through to see if this start location works
317 for (i
= iii
; i
< *size
; ++i
) {
318 if ((BinStream
[i
] >= high
) && ((i
-lastBit
)>(*clk
-tol
))){
320 //BitStream[bitnum] = *invert;
323 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
)>(*clk
-tol
))){
324 //low found and we are expecting a bar
326 //BitStream[bitnum] = 1- *invert;
329 } else if ((BinStream
[i
]<=low
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
332 //BitStream[bitnum]= 1- *invert;
334 } else if ((BinStream
[i
]>=high
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
337 //BitStream[bitnum]= *invert;
339 } else if ((i
-lastBit
)>((*clk
/2)+tol
) && (midBit
==0)){
342 //BitStream[bitnum]= BitStream[bitnum-1];
345 //mid value found or no bar supposed to be here
347 if ((i
-lastBit
)>(*clk
+tol
)){
348 //should have hit a high or low based on clock!!
350 //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);
352 // BitStream[bitnum]=77;
357 lastBit
+=*clk
;//skip over until hit too many errors
358 if (errCnt
> ((*size
/1000))){ //allow 1 error for every 1000 samples else start over
360 // bitnum=0;//start over
365 if ((i
-iii
)>(500 * *clk
)) break; //got enough bits
367 //we got more than 64 good bits and not all errors
368 if ((((i
-iii
)/ *clk
) > (64+errCnt
)) && (errCnt
<(*size
/1000))) {
373 break; //great read - finish
375 if (errCnt
<bestErrCnt
){ //set this as new best run
382 if (bestErrCnt
<maxErr
){
383 //best run is good enough - set to best run and overwrite BinStream
385 lastBit
= bestStart
- *clk
;
387 for (i
= iii
; i
< *size
; ++i
) {
388 if ((BinStream
[i
] >= high
) && ((i
-lastBit
) > (*clk
-tol
))){
390 BinStream
[bitnum
] = *invert
;
393 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
) > (*clk
-tol
))){
394 //low found and we are expecting a bar
396 BinStream
[bitnum
] = 1-*invert
;
399 } else if ((BinStream
[i
]<=low
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
402 BinStream
[bitnum
] = 1 - *invert
;
404 } else if ((BinStream
[i
]>=high
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
407 BinStream
[bitnum
] = *invert
;
409 } else if ((i
-lastBit
)>((*clk
/2)+tol
) && (midBit
==0)){
412 if (bitnum
!=0) BinStream
[bitnum
] = BinStream
[bitnum
-1];
416 //mid value found or no bar supposed to be here
417 if ((i
-lastBit
)>(*clk
+tol
)){
418 //should have hit a high or low based on clock!!
421 //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);
423 BinStream
[bitnum
]=77;
427 lastBit
+=*clk
;//skip over error
430 if (bitnum
>=400) break;
440 //translate wave to 11111100000 (1 for each short wave 0 for each long wave)
441 size_t fsk_wave_demod(uint8_t * dest
, size_t size
, uint8_t fchigh
, uint8_t fclow
)
443 uint32_t last_transition
= 0;
446 if (fchigh
==0) fchigh
=10;
447 if (fclow
==0) fclow
=8;
448 //set the threshold close to 0 (graph) or 128 std to avoid static
449 uint8_t threshold_value
= 123;
451 // sync to first lo-hi transition, and threshold
453 // Need to threshold first sample
455 if(dest
[0] < threshold_value
) dest
[0] = 0;
459 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
460 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
461 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
462 for(idx
= 1; idx
< size
; idx
++) {
463 // threshold current value
465 if (dest
[idx
] < threshold_value
) dest
[idx
] = 0;
468 // Check for 0->1 transition
469 if (dest
[idx
-1] < dest
[idx
]) { // 0 -> 1 transition
470 if ((idx
-last_transition
)<(fclow
-2)){ //0-5 = garbage noise
471 //do nothing with extra garbage
472 } else if ((idx
-last_transition
) < (fchigh
-1)) { //6-8 = 8 waves
474 } else { //9+ = 10 waves
477 last_transition
= idx
;
481 return numBits
; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
484 uint32_t myround2(float f
)
486 if (f
>= 2000) return 2000;//something bad happened
487 return (uint32_t) (f
+ (float)0.5);
490 //translate 11111100000 to 10
491 size_t aggregate_bits(uint8_t *dest
, size_t size
, uint8_t rfLen
, uint8_t maxConsequtiveBits
,
492 uint8_t invert
, uint8_t fchigh
, uint8_t fclow
)
494 uint8_t lastval
=dest
[0];
499 for( idx
=1; idx
< size
; idx
++) {
501 if (dest
[idx
]==lastval
) {
505 //if lastval was 1, we have a 1->0 crossing
506 if ( dest
[idx
-1]==1 ) {
507 n
=myround2((float)(n
+1)/((float)(rfLen
)/(float)fclow
));
508 } else {// 0->1 crossing
509 n
=myround2((float)(n
+1)/((float)(rfLen
-1)/(float)fchigh
)); //-1 for fudge factor
513 if(n
< maxConsequtiveBits
) //Consecutive
515 if(invert
==0){ //invert bits
516 memset(dest
+numBits
, dest
[idx
-1] , n
);
518 memset(dest
+numBits
, dest
[idx
-1]^1 , n
);
527 //by marshmellow (from holiman's base)
528 // full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod)
529 int fskdemod(uint8_t *dest
, size_t size
, uint8_t rfLen
, uint8_t invert
, uint8_t fchigh
, uint8_t fclow
)
532 size
= fsk_wave_demod(dest
, size
, fchigh
, fclow
);
533 size
= aggregate_bits(dest
, size
, rfLen
, 192, invert
, fchigh
, fclow
);
536 // loop to get raw HID waveform then FSK demodulate the TAG ID from it
537 int HIDdemodFSK(uint8_t *dest
, size_t size
, uint32_t *hi2
, uint32_t *hi
, uint32_t *lo
)
540 size_t idx
=0; //, found=0; //size=0,
542 size
= fskdemod(dest
, size
,50,0,10,8);
544 // final loop, go over previously decoded manchester data and decode into usable tag ID
545 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
546 uint8_t frame_marker_mask
[] = {1,1,1,0,0,0};
550 while( idx
+ sizeof(frame_marker_mask
) < size
) {
551 // search for a start of frame marker
552 if ( memcmp(dest
+idx
, frame_marker_mask
, sizeof(frame_marker_mask
)) == 0)
553 { // frame marker found
554 idx
+=sizeof(frame_marker_mask
);
555 while(dest
[idx
] != dest
[idx
+1] && idx
< size
-2)
557 // Keep going until next frame marker (or error)
558 // Shift in a bit. Start by shifting high registers
559 *hi2
= (*hi2
<<1)|(*hi
>>31);
560 *hi
= (*hi
<<1)|(*lo
>>31);
561 //Then, shift in a 0 or one into low
562 if (dest
[idx
] && !dest
[idx
+1]) // 1 0
569 // Hopefully, we read a tag and hit upon the next frame marker
570 if(idx
+ sizeof(frame_marker_mask
) < size
)
572 if ( memcmp(dest
+idx
, frame_marker_mask
, sizeof(frame_marker_mask
)) == 0)
579 *hi2
= *hi
= *lo
= 0;
588 uint32_t bytebits_to_byte(uint8_t* src
, size_t numbits
)
591 for(int i
= 0 ; i
< numbits
; i
++)
593 num
= (num
<< 1) | (*src
);
599 int IOdemodFSK(uint8_t *dest
, size_t size
)
601 static const uint8_t THRESHOLD
= 129;
603 //make sure buffer has data
604 if (size
< 66) return -1;
605 //test samples are not just noise
606 uint8_t justNoise
= 1;
607 for(idx
=0;idx
< size
&& justNoise
;idx
++){
608 justNoise
= dest
[idx
] < THRESHOLD
;
610 if(justNoise
) return 0;
613 size
= fskdemod(dest
, size
, 64, 1, 10, 8); // RF/64 and invert
614 if (size
< 65) return -1; //did we get a good demod?
616 //0 10 20 30 40 50 60
618 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
619 //-----------------------------------------------------------------------------
620 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
622 //XSF(version)facility:codeone+codetwo
624 uint8_t mask
[] = {0,0,0,0,0,0,0,0,0,1};
625 for( idx
=0; idx
< (size
- 65); idx
++) {
626 if ( memcmp(dest
+ idx
, mask
, sizeof(mask
))==0) {
628 if (!dest
[idx
+8] && dest
[idx
+17]==1 && dest
[idx
+26]==1 && dest
[idx
+35]==1 && dest
[idx
+44]==1 && dest
[idx
+53]==1){
629 //confirmed proper separator bits found
630 //return start position
639 // pass bits to be tested in bits, length bits passed in bitLen, and parity type (even=0 | odd=1) in pType
640 // returns 1 if passed
641 int parityTest(uint32_t bits
, uint8_t bitLen
, uint8_t pType
)
644 for (int i
= 0; i
< bitLen
; i
++){
645 ans
^= ((bits
>> i
) & 1);
647 //PrintAndLog("DEBUG: ans: %d, ptype: %d",ans,pType);
648 return (ans
== pType
);
652 // takes a array of binary values, start position, length of bits per parity (includes parity bit),
653 // Parity Type (1 for odd 0 for even), and binary Length (length to run)
654 size_t removeParity(uint8_t *BitStream
, size_t startIdx
, uint8_t pLen
, uint8_t pType
, size_t bLen
)
656 uint32_t parityWd
= 0;
657 size_t j
= 0, bitCnt
= 0;
658 for (int word
= 0; word
< (bLen
); word
+=pLen
){
659 for (int bit
=0; bit
< pLen
; bit
++){
660 parityWd
= (parityWd
<< 1) | BitStream
[startIdx
+word
+bit
];
661 BitStream
[j
++] = (BitStream
[startIdx
+word
+bit
]);
664 // if parity fails then return 0
665 if (parityTest(parityWd
, pLen
, pType
) == 0) return -1;
669 // if we got here then all the parities passed
670 //return ID start index and size
675 // FSK Demod then try to locate an AWID ID
676 int AWIDdemodFSK(uint8_t *dest
, size_t size
)
678 static const uint8_t THRESHOLD
= 123;
680 //make sure buffer has data
681 if (size
< 96*50) return -1;
682 //test samples are not just noise
683 uint8_t justNoise
= 1;
684 for(idx
=0; idx
< size
&& justNoise
;idx
++){
685 justNoise
= dest
[idx
] < THRESHOLD
;
687 if(justNoise
) return -2;
690 size
= fskdemod(dest
, size
, 50, 1, 10, 8); // RF/64 and invert
691 if (size
< 96) return -3; //did we get a good demod?
693 uint8_t mask
[] = {0,0,0,0,0,0,0,1};
694 for( idx
=0; idx
< (size
- 96); idx
++) {
695 if ( memcmp(dest
+ idx
, mask
, sizeof(mask
))==0) {
696 // frame marker found
697 //return ID start index and size
699 //size should always be 96
707 // FSK Demod then try to locate an Farpointe Data (pyramid) ID
708 int PyramiddemodFSK(uint8_t *dest
, size_t size
)
710 static const uint8_t THRESHOLD
= 123;
712 // size_t size2 = size;
713 //make sure buffer has data
714 if (size
< 128*50) return -5;
715 //test samples are not just noise
716 uint8_t justNoise
= 1;
717 for(idx
=0; idx
< size
&& justNoise
;idx
++){
718 justNoise
= dest
[idx
] < THRESHOLD
;
720 if(justNoise
) return -1;
723 size
= fskdemod(dest
, size
, 50, 1, 10, 8); // RF/64 and invert
724 if (size
< 128) return -2; //did we get a good demod?
726 uint8_t mask
[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
727 for( idx
=0; idx
< (size
- 128); idx
++) {
728 if ( memcmp(dest
+ idx
, mask
, sizeof(mask
))==0) {
729 // frame marker found
738 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
739 // maybe somehow adjust peak trimming value based on samples to fix?
740 int DetectASKClock(uint8_t dest
[], size_t size
, int clock
)
743 int clk
[]={8,16,32,40,50,64,100,128,256};
744 int loopCnt
= 256; //don't need to loop through entire array...
745 if (size
<loopCnt
) loopCnt
= size
;
747 //if we already have a valid clock quit
750 if (clk
[i
] == clock
) return clock
;
752 //get high and low peak
754 getHiLo(dest
, loopCnt
, &peak
, &low
, 75, 75);
759 int bestErr
[]={1000,1000,1000,1000,1000,1000,1000,1000,1000};
761 //test each valid clock from smallest to greatest to see which lines up
762 for(clkCnt
=0; clkCnt
< 6; ++clkCnt
){
763 if (clk
[clkCnt
] == 32){
768 bestErr
[clkCnt
]=1000;
769 //try lining up the peaks by moving starting point (try first 256)
770 for (ii
=0; ii
< loopCnt
; ++ii
){
771 if ((dest
[ii
] >= peak
) || (dest
[ii
] <= low
)){
773 // now that we have the first one lined up test rest of wave array
774 for (i
=0; i
<((int)(size
/clk
[clkCnt
])-1); ++i
){
775 if (dest
[ii
+(i
*clk
[clkCnt
])]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])]<=low
){
776 }else if(dest
[ii
+(i
*clk
[clkCnt
])-tol
]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])-tol
]<=low
){
777 }else if(dest
[ii
+(i
*clk
[clkCnt
])+tol
]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])+tol
]<=low
){
778 }else{ //error no peak detected
782 //if we found no errors this is correct one - return this clock
783 if(errCnt
==0) return clk
[clkCnt
];
784 //if we found errors see if it is lowest so far and save it as best run
785 if(errCnt
<bestErr
[clkCnt
]) bestErr
[clkCnt
]=errCnt
;
791 for (iii
=0; iii
<8;++iii
){
792 if (bestErr
[iii
]<bestErr
[best
]){
793 // current best bit to error ratio vs new bit to error ratio
794 if (((size
/clk
[best
])/bestErr
[best
] < (size
/clk
[iii
])/bestErr
[iii
]) ){
803 //detect psk clock by reading #peaks vs no peaks(or errors)
804 int DetectpskNRZClock(uint8_t dest
[], size_t size
, int clock
)
807 int clk
[]={16,32,40,50,64,100,128,256};
808 int loopCnt
= 2048; //don't need to loop through entire array...
809 if (size
<loopCnt
) loopCnt
= size
;
811 //if we already have a valid clock quit
813 if (clk
[i
] == clock
) return clock
;
815 //get high and low peak
817 getHiLo(dest
, loopCnt
, &peak
, &low
, 75, 75);
819 //PrintAndLog("DEBUG: peak: %d, low: %d",peak,low);
825 int bestErr
[]={1000,1000,1000,1000,1000,1000,1000,1000,1000};
826 int peaksdet
[]={0,0,0,0,0,0,0,0,0};
827 //test each valid clock from smallest to greatest to see which lines up
828 for(clkCnt
=0; clkCnt
< 6; ++clkCnt
){
829 if (clk
[clkCnt
] >= 32){
834 //try lining up the peaks by moving starting point (try first 256)
835 for (ii
=0; ii
< loopCnt
; ++ii
){
836 if ((dest
[ii
] >= peak
) || (dest
[ii
] <= low
)){
839 // now that we have the first one lined up test rest of wave array
840 for (i
=0; i
< ((int)(size
/clk
[clkCnt
])-1); ++i
){
841 if (dest
[ii
+(i
*clk
[clkCnt
])]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])]<=low
){
843 }else if(dest
[ii
+(i
*clk
[clkCnt
])-tol
]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])-tol
]<=low
){
845 }else if(dest
[ii
+(i
*clk
[clkCnt
])+tol
]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])+tol
]<=low
){
847 }else{ //error no peak detected
851 if(peakcnt
>peaksdet
[clkCnt
]) {
852 peaksdet
[clkCnt
]=peakcnt
;
853 bestErr
[clkCnt
]=errCnt
;
860 //int ratio2; //debug
863 for (iii
=0; iii
< 7; ++iii
){
865 //ratio2=1000; //debug
866 //bits=size/clk[iii]; //debug
867 if (peaksdet
[iii
] > 0){
868 ratio
=bestErr
[iii
]/peaksdet
[iii
];
869 if (((bestErr
[best
]/peaksdet
[best
]) > (ratio
)+1)){
872 //ratio2=bits/peaksdet[iii]; //debug
874 //PrintAndLog("DEBUG: Clk: %d, peaks: %d, errs: %d, bestClk: %d, ratio: %d, bits: %d, peakbitr: %d",clk[iii],peaksdet[iii],bestErr[iii],clk[best],ratio, bits,ratio2);
879 //by marshmellow (attempt to get rid of high immediately after a low)
880 void pskCleanWave(uint8_t *BitStream
, size_t size
)
887 getHiLo(BitStream
, size
, &high
, &low
, 80, 90);
889 for (i
=0; i
< size
; ++i
){
891 if (BitStream
[i
]>low
){
899 }else if (newHigh
== 1){
900 if (BitStream
[i
]<high
){
909 if (BitStream
[i
] <= low
) newLow
=1;
910 if (BitStream
[i
] >= high
) newHigh
=1;
916 //redesigned by marshmellow adjusted from existing decode functions
917 //indala id decoding - only tested on 26 bit tags, but attempted to make it work for more
918 int indala26decode(uint8_t *bitStream
, size_t *size
, uint8_t *invert
)
920 //26 bit 40134 format (don't know other formats)
922 int long_wait
=29;//29 leading zeros in format
928 // Finding the start of a UID
929 for (start
= 0; start
<= *size
- 250; start
++) {
930 first
= bitStream
[start
];
931 for (i
= start
; i
< start
+ long_wait
; i
++) {
932 if (bitStream
[i
] != first
) {
936 if (i
== (start
+ long_wait
)) {
940 if (start
== *size
- 250 + 1) {
941 // did not find start sequence
944 // Inverting signal if needed
946 for (i
= start
; i
< *size
; i
++) {
947 bitStream
[i
] = !bitStream
[i
];
953 //found start once now test length by finding next one
954 for (ii
=start
+29; ii
<= *size
- 250; ii
++) {
955 first2
= bitStream
[ii
];
956 for (iii
= ii
; iii
< ii
+ long_wait
; iii
++) {
957 if (bitStream
[iii
] != first2
) {
961 if (iii
== (ii
+ long_wait
)) {
965 if (ii
== *size
- 250 + 1){
966 // did not find second start sequence
973 for (ii
= 0; ii
< bitCnt
; ii
++) {
974 bitStream
[ii
] = bitStream
[i
++];
981 //by marshmellow - demodulate PSK1 wave or NRZ wave (both similar enough)
982 //peaks switch bit (high=1 low=0) each clock cycle = 1 bit determined by last peak
983 int pskNRZrawDemod(uint8_t *dest
, size_t *size
, int *clk
, int *invert
)
985 pskCleanWave(dest
,*size
);
986 int clk2
= DetectpskNRZClock(dest
, *size
, *clk
);
990 ans
= getHiLo(dest
, 1260, &high
, &low
, 75, 80); //25% fuzz on high 20% fuzz on low
991 if (ans
<1) return -2; //just noise
992 uint32_t gLen
= *size
;
993 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
994 int lastBit
= 0; //set first clock check
995 uint32_t bitnum
= 0; //output counter
996 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
997 if (*clk
==32) tol
= 2; //clock tolerance may not be needed anymore currently set to + or - 1 but could be increased for poor waves or removed entirely
1000 uint32_t bestStart
= *size
;
1001 uint32_t maxErr
= (*size
/1000);
1002 uint32_t bestErrCnt
= maxErr
;
1006 uint8_t ignorewin
=*clk
/8;
1007 //PrintAndLog("DEBUG - lastbit - %d",lastBit);
1008 //loop to find first wave that works - align to clock
1009 for (iii
=0; iii
< gLen
; ++iii
){
1010 if ((dest
[iii
]>=high
) || (dest
[iii
]<=low
)){
1012 //loop through to see if this start location works
1013 for (i
= iii
; i
< *size
; ++i
) {
1014 //if we found a high bar and we are at a clock bit
1015 if ((dest
[i
]>=high
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
1020 //else if low bar found and we are at a clock point
1021 }else if ((dest
[i
]<=low
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
1026 //else if no bars found
1027 }else if(dest
[i
] < high
&& dest
[i
] > low
) {
1031 //if we are past a clock point
1032 if (i
>= lastBit
+*clk
+tol
){ //clock val
1036 //else if bar found but we are not at a clock bit and we did not just have a clock bit
1037 }else if ((dest
[i
]>=high
|| dest
[i
]<=low
) && (i
<lastBit
+*clk
-tol
|| i
>lastBit
+*clk
+tol
) && (bitHigh
==0)){
1038 //error bar found no clock...
1041 if (bitnum
>=1000) break;
1043 //we got more than 64 good bits and not all errors
1044 if ((bitnum
> (64+errCnt
)) && (errCnt
< (maxErr
))) {
1045 //possible good read
1048 bestErrCnt
= errCnt
;
1049 break; //great read - finish
1051 if (errCnt
< bestErrCnt
){ //set this as new best run
1052 bestErrCnt
= errCnt
;
1058 if (bestErrCnt
< maxErr
){
1059 //best run is good enough set to best run and set overwrite BinStream
1061 lastBit
=bestStart
-*clk
;
1063 for (i
= iii
; i
< *size
; ++i
) {
1064 //if we found a high bar and we are at a clock bit
1065 if ((dest
[i
] >= high
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
1069 dest
[bitnum
]=curBit
;
1072 //else if low bar found and we are at a clock point
1073 }else if ((dest
[i
]<=low
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
1077 dest
[bitnum
]=curBit
;
1080 //else if no bars found
1081 }else if(dest
[i
]<high
&& dest
[i
]>low
) {
1085 //if we are past a clock point
1086 if (i
>=lastBit
+*clk
+tol
){ //clock val
1088 dest
[bitnum
]=curBit
;
1091 //else if bar found but we are not at a clock bit and we did not just have a clock bit
1092 }else if ((dest
[i
]>=high
|| dest
[i
]<=low
) && ((i
<lastBit
+*clk
-tol
) || (i
>lastBit
+*clk
+tol
)) && (bitHigh
==0)){
1093 //error bar found no clock...
1099 if (bitnum
>=1000) break;
1116 //countFC is to detect the field clock and bit clock rates.
1117 //for fsk or ask not psk or nrz
1118 uint32_t countFC(uint8_t *BitStream
, size_t size
)
1120 // get high/low thresholds
1122 getHiLo(BitStream
,10, &high
, &low
, 100, 100);
1123 // get zero crossing
1124 uint8_t zeroC
= (high
-low
)/2+low
;
1125 uint8_t clk
[]={8,16,32,40,50,64,100,128};
1126 uint8_t fcLens
[] = {0,0,0,0,0,0,0,0,0,0};
1127 uint16_t fcCnts
[] = {0,0,0,0,0,0,0,0,0,0};
1128 uint8_t rfLens
[] = {0,0,0,0,0,0,0,0,0,0,0};
1129 // uint8_t rfCnts[] = {0,0,0,0,0,0,0,0,0,0};
1130 uint8_t fcLensFnd
= 0;
1131 uint8_t rfLensFnd
= 0;
1134 uint8_t lastFCcnt
=0;
1136 uint32_t fcCounter
= 0;
1137 uint32_t rfCounter
= 0;
1138 uint8_t firstBitFnd
= 0;
1141 // prime i to first up transition
1142 for (i
= 1; i
< size
; i
++)
1143 if (BitStream
[i
]>=zeroC
&& BitStream
[i
-1]<zeroC
)
1146 for (; i
< size
; i
++){
1147 curBit
= BitStream
[i
];
1148 lastBit
= BitStream
[i
-1];
1149 if (lastBit
<zeroC
&& curBit
>= zeroC
){
1150 // new up transition
1153 if (fcCounter
> 3 && fcCounter
< 256){
1154 //we've counted enough that it could be a valid field clock
1156 //if we had 5 and now have 9 then go back to 8 (for when we get a fc 9 instead of an 8)
1157 if (lastFCcnt
==5 && fcCounter
==9) fcCounter
--;
1158 //if odd and not rc/5 add one (for when we get a fc 9 instead of 10)
1159 if ((fcCounter
==9 && fcCounter
& 1) || fcCounter
==4) fcCounter
++;
1161 //look for bit clock (rf/xx)
1162 if ((fcCounter
<lastFCcnt
|| fcCounter
>lastFCcnt
)){
1163 //not the same size as the last wave - start of new bit sequence
1165 if (firstBitFnd
>1){ //skip first wave change - probably not a complete bit
1166 for (int ii
=0; ii
<10; ii
++){
1167 if (rfLens
[ii
]==rfCounter
){
1173 if (rfCounter
>0 && rfLensFnd
<10){
1174 //PrintAndLog("DEBUG: rfCntr %d, fcCntr %d",rfCounter,fcCounter);
1175 //rfCnts[rfLensFnd]++;
1176 rfLens
[rfLensFnd
++]=rfCounter
;
1179 //PrintAndLog("DEBUG i: %d",i);
1183 lastFCcnt
=fcCounter
;
1186 // save last field clock count (fc/xx)
1187 // find which fcLens to save it to:
1188 for (int ii
=0; ii
<10; ii
++){
1189 if (fcLens
[ii
]==fcCounter
){
1195 if (fcCounter
>0 && fcLensFnd
<10){
1197 //PrintAndLog("FCCntr %d",fcCounter);
1198 fcCnts
[fcLensFnd
]++;
1199 fcLens
[fcLensFnd
++]=fcCounter
;
1202 // hmmm this should not happen often - count them
1213 // if too many errors return errors as negative number (IS THIS NEEDED?)
1214 if (errCnt
>100) return -1*errCnt
;
1216 uint8_t maxCnt1
=0, best1
=9, best2
=9, best3
=9, rfHighest
=10, rfHighest2
=10, rfHighest3
=10;
1218 // go through fclens and find which ones are bigest 2
1219 for (i
=0; i
<10; i
++){
1220 // PrintAndLog("DEBUG: FC %d, Cnt %d, Errs %d, RF %d",fcLens[i],fcCnts[i],errCnt,rfLens[i]);
1222 // get the 3 best FC values
1223 if (fcCnts
[i
]>maxCnt1
) {
1228 } else if(fcCnts
[i
]>fcCnts
[best2
]){
1231 } else if(fcCnts
[i
]>fcCnts
[best3
]){
1234 //get highest 2 RF values (might need to get more values to compare or compare all?)
1235 if (rfLens
[i
]>rfLens
[rfHighest
]){
1236 rfHighest3
=rfHighest2
;
1237 rfHighest2
=rfHighest
;
1239 } else if(rfLens
[i
]>rfLens
[rfHighest2
]){
1240 rfHighest3
=rfHighest2
;
1242 } else if(rfLens
[i
]>rfLens
[rfHighest3
]){
1247 // set allowed clock remainder tolerance to be 1 large field clock length
1248 // we could have mistakenly made a 9 a 10 instead of an 8 or visa versa so rfLens could be 1 FC off
1249 int tol1
= (fcLens
[best1
]>fcLens
[best2
]) ? fcLens
[best1
] : fcLens
[best2
];
1251 // loop to find the highest clock that has a remainder less than the tolerance
1252 // compare samples counted divided by
1254 for (; ii
>=0; ii
--){
1255 if (rfLens
[rfHighest
] % clk
[ii
] < tol1
|| rfLens
[rfHighest
] % clk
[ii
] > clk
[ii
]-tol1
){
1256 if (rfLens
[rfHighest2
] % clk
[ii
] < tol1
|| rfLens
[rfHighest2
] % clk
[ii
] > clk
[ii
]-tol1
){
1257 if (rfLens
[rfHighest3
] % clk
[ii
] < tol1
|| rfLens
[rfHighest3
] % clk
[ii
] > clk
[ii
]-tol1
){
1264 if (ii
<0) ii
=7; // oops we went too far
1266 // TODO: take top 3 answers and compare to known Field clocks to get top 2
1269 // PrintAndLog("DEBUG: Best %d best2 %d best3 %d, clk %d, clk2 %d",fcLens[best1],fcLens[best2],fcLens[best3],clk[i],clk[ii]);
1272 if (fcLens
[best1
]>fcLens
[best2
]){
1273 fcs
= (((uint32_t)clk
[ii
])<<16) | (((uint32_t)fcLens
[best1
])<<8) | ((fcLens
[best2
]));
1275 fcs
= (((uint32_t)clk
[ii
])<<16) | (((uint32_t)fcLens
[best2
])<<8) | ((fcLens
[best1
]));