]>
git.zerfleddert.de Git - proxmark3-svn/blob - common/lfdemod.c
2352caadbac2d7b6c048650f0237a14c50d3f489
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 commands
9 //-----------------------------------------------------------------------------
16 //takes 1s and 0s and searches for EM410x format - output EM ID
17 uint64_t Em410xDecode(uint8_t *BitStream
, size_t size
)
19 //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
20 // otherwise could be a void with no arguments
26 uint32_t initLoopMax
= 65;
27 if (initLoopMax
>size
) initLoopMax
=size
;
29 for (;i
< initLoopMax
; ++i
) //65 samples should be plenty to find high and low values
31 if (BitStream
[i
] > high
)
33 else if (BitStream
[i
] < low
)
36 if (((high
!=1)||(low
!=0))){ //allow only 1s and 0s
37 // PrintAndLog("no data found");
41 // 111111111 bit pattern represent start of frame
42 uint8_t frame_marker_mask
[] = {1,1,1,1,1,1,1,1,1};
46 while( (idx
+ 64) < size
) {
48 // search for a start of frame marker
49 if ( memcmp(BitStream
+idx
, frame_marker_mask
, sizeof(frame_marker_mask
)) == 0)
50 { // frame marker found
53 for(ii
=0; ii
<5; ++ii
){
54 parityTest
+= BitStream
[(i
*5)+ii
+idx
];
56 if (parityTest
== ((parityTest
>>1)<<1)){
58 for (ii
=0; ii
<4;++ii
){
59 lo
=(lo
<<1LL)|(BitStream
[(i
*5)+ii
+idx
]);
61 //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);
62 }else {//parity failed
63 //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]);
66 if (resetCnt
>5)return 0;
68 goto restart
;//continue;
71 //skip last 5 bit parity test for simplicity.
81 //takes 2 arguments - clock and invert both as integers
82 //attempts to demodulate ask while decoding manchester
83 //prints binary found and saves in graphbuffer for further commands
84 int askmandemod(uint8_t *BinStream
, size_t *size
, int *clk
, int *invert
)
87 int high
= 0, low
= 255;
88 *clk
=DetectASKClock(BinStream
, *size
, *clk
); //clock default
92 if (*invert
!= 0 && *invert
!= 1) *invert
=0;
93 uint32_t initLoopMax
= 200;
94 if (initLoopMax
> *size
) initLoopMax
=*size
;
95 // Detect high and lows
96 for (i
= 0; i
< initLoopMax
; ++i
) //200 samples should be enough to find high and low values
98 if (BinStream
[i
] > high
)
100 else if (BinStream
[i
] < low
)
103 if ((high
< 129) ){ //throw away static (anything < 1 graph)
104 //PrintAndLog("no data found");
107 //25% fuzz in case highs and lows aren't clipped [marshmellow]
108 high
=(int)(((high
-128)*.75)+128);
109 low
= (int)(((low
-128)*.75)+128);
111 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
112 int lastBit
= 0; //set first clock check
113 uint32_t bitnum
= 0; //output counter
114 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
115 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
117 uint32_t gLen
= *size
;
118 if (gLen
> 3000) gLen
=3000;
120 uint32_t bestStart
= *size
;
121 uint32_t bestErrCnt
= (*size
/1000);
122 uint32_t maxErr
= (*size
/1000);
123 //PrintAndLog("DEBUG - lastbit - %d",lastBit);
124 //loop to find first wave that works
125 for (iii
=0; iii
< gLen
; ++iii
){
126 if ((BinStream
[iii
] >= high
) || (BinStream
[iii
] <= low
)){
129 //loop through to see if this start location works
130 for (i
= iii
; i
< *size
; ++i
) {
131 if ((BinStream
[i
] >= high
) && ((i
-lastBit
) > (*clk
-tol
))){
133 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
) > (*clk
-tol
))){
134 //low found and we are expecting a bar
137 //mid value found or no bar supposed to be here
138 if ((i
-lastBit
)>(*clk
+tol
)){
139 //should have hit a high or low based on clock!!
142 //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);
145 lastBit
+=*clk
;//skip over until hit too many errors
146 if (errCnt
>(maxErr
)) break; //allow 1 error for every 1000 samples else start over
149 if ((i
-iii
) >(400 * *clk
)) break; //got plenty of bits
151 //we got more than 64 good bits and not all errors
152 if ((((i
-iii
)/ *clk
) > (64+errCnt
)) && (errCnt
<maxErr
)) {
157 break; //great read - finish
159 if (errCnt
<bestErrCnt
){ //set this as new best run
166 if (bestErrCnt
<maxErr
){
167 //best run is good enough set to best run and set overwrite BinStream
169 lastBit
= bestStart
- *clk
;
171 for (i
= iii
; i
< *size
; ++i
) {
172 if ((BinStream
[i
] >= high
) && ((i
-lastBit
) > (*clk
-tol
))){
174 BinStream
[bitnum
] = *invert
;
176 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
) > (*clk
-tol
))){
177 //low found and we are expecting a bar
179 BinStream
[bitnum
] = 1-*invert
;
182 //mid value found or no bar supposed to be here
183 if ((i
-lastBit
)>(*clk
+tol
)){
184 //should have hit a high or low based on clock!!
187 //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);
189 BinStream
[bitnum
]=77;
193 lastBit
+=*clk
;//skip over error
196 if (bitnum
>=400) break;
208 //take 10 and 01 and manchester decode
209 //run through 2 times and take least errCnt
210 int manrawdecode(uint8_t * BitStream
, size_t *size
)
218 for (ii
=1;ii
<3;++ii
){
220 for (i
=i
+ii
;i
<*size
-2;i
+=2){
221 if(BitStream
[i
]==1 && (BitStream
[i
+1]==0)){
222 } else if((BitStream
[i
]==0)&& BitStream
[i
+1]==1){
226 if(bitnum
>300) break;
238 for (i
=i
+ii
;i
< *size
-2;i
+=2){
239 if(BitStream
[i
] == 1 && (BitStream
[i
+1] == 0)){
240 BitStream
[bitnum
++]=0;
241 } else if((BitStream
[i
] == 0) && BitStream
[i
+1] == 1){
242 BitStream
[bitnum
++]=1;
244 BitStream
[bitnum
++]=77;
247 if(bitnum
>300) break;
256 //take 01 or 10 = 0 and 11 or 00 = 1
257 int BiphaseRawDecode(uint8_t *BitStream
, size_t *size
, int offset
)
263 for (;i
<*size
-2;i
+=2){
264 if((BitStream
[i
]==1 && BitStream
[i
+1]==0) || (BitStream
[i
]==0 && BitStream
[i
+1]==1)){
265 BitStream
[bitnum
++]=1;
266 } else if((BitStream
[i
]==0 && BitStream
[i
+1]==0) || (BitStream
[i
]==1 && BitStream
[i
+1]==1)){
267 BitStream
[bitnum
++]=0;
269 BitStream
[bitnum
++]=77;
272 if(bitnum
>250) break;
279 //takes 2 arguments - clock and invert both as integers
280 //attempts to demodulate ask only
281 //prints binary found and saves in graphbuffer for further commands
282 int askrawdemod(uint8_t *BinStream
, size_t *size
, int *clk
, int *invert
)
285 // int invert=0; //invert default
286 int high
= 0, low
= 255;
287 *clk
=DetectASKClock(BinStream
, *size
, *clk
); //clock default
288 uint8_t BitStream
[502] = {0};
290 if (*clk
<8) *clk
=64;
291 if (*clk
<32) *clk
=32;
292 if (*invert
!= 0 && *invert
!= 1) *invert
=0;
293 uint32_t initLoopMax
= 200;
294 if (initLoopMax
> *size
) initLoopMax
=*size
;
295 // Detect high and lows
296 for (i
= 0; i
< initLoopMax
; ++i
) //200 samples should be plenty to find high and low values
298 if (BinStream
[i
] > high
)
300 else if (BinStream
[i
] < low
)
303 if ((high
< 129)){ //throw away static high has to be more than 0 on graph.
305 // PrintAndLog("no data found");
308 //25% fuzz in case highs and lows aren't clipped [marshmellow]
309 high
=(int)(((high
-128)*.75)+128);
310 low
= (int)(((low
-128)*.75)+128);
312 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
313 int lastBit
= 0; //set first clock check
314 uint32_t bitnum
= 0; //output counter
315 uint8_t tol
= 0; //clock tolerance adjust - waves will be accepted as within the clock
316 // if they fall + or - this value + clock from last valid wave
317 if (*clk
== 32) tol
=1; //clock tolerance may not be needed anymore currently set to
318 // + or - 1 but could be increased for poor waves or removed entirely
320 uint32_t gLen
= *size
;
321 if (gLen
> 500) gLen
=500;
323 uint32_t bestStart
= *size
;
324 uint32_t bestErrCnt
= (*size
/1000);
326 //PrintAndLog("DEBUG - lastbit - %d",lastBit);
327 //loop to find first wave that works
328 for (iii
=0; iii
< gLen
; ++iii
){
329 if ((BinStream
[iii
]>=high
) || (BinStream
[iii
]<=low
)){
331 //loop through to see if this start location works
332 for (i
= iii
; i
< *size
; ++i
) {
333 if ((BinStream
[i
] >= high
) && ((i
-lastBit
)>(*clk
-tol
))){
335 BitStream
[bitnum
] = *invert
;
338 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
)>(*clk
-tol
))){
339 //low found and we are expecting a bar
341 BitStream
[bitnum
] = 1- *invert
;
344 } else if ((BinStream
[i
]<=low
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
347 BitStream
[bitnum
]= 1- *invert
;
349 } else if ((BinStream
[i
]>=high
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
352 BitStream
[bitnum
]= *invert
;
354 } else if ((i
-lastBit
)>((*clk
/2)+tol
) && (midBit
==0)){
357 BitStream
[bitnum
]= BitStream
[bitnum
-1];
360 //mid value found or no bar supposed to be here
362 if ((i
-lastBit
)>(*clk
+tol
)){
363 //should have hit a high or low based on clock!!
365 //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);
367 BitStream
[bitnum
]=77;
372 lastBit
+=*clk
;//skip over until hit too many errors
373 if (errCnt
> ((*size
/1000))){ //allow 1 error for every 1000 samples else start over
375 bitnum
=0;//start over
380 if (bitnum
>500) break;
382 //we got more than 64 good bits and not all errors
383 if ((bitnum
> (64+errCnt
)) && (errCnt
<(*size
/1000))) {
385 if (errCnt
==0) break; //great read - finish
386 if (bestStart
== iii
) break; //if current run == bestErrCnt run (after exhausted testing) then finish
387 if (errCnt
<bestErrCnt
){ //set this as new best run
393 if (iii
>=gLen
){ //exhausted test
394 //if there was a ok test go back to that one and re-run the best run (then dump after that run)
395 if (bestErrCnt
< (*size
/1000)) iii
=bestStart
;
399 for (i
=0; i
< bitnum
; ++i
){
400 BinStream
[i
]=BitStream
[i
];
406 //translate wave to 11111100000 (1 for each short wave 0 for each long wave)
407 size_t fsk_wave_demod(uint8_t * dest
, size_t size
, uint8_t fchigh
, uint8_t fclow
)
409 uint32_t last_transition
= 0;
412 if (fchigh
==0) fchigh
=10;
413 if (fclow
==0) fclow
=8;
414 //set the threshold close to 0 (graph) or 128 std to avoid static
415 uint8_t threshold_value
= 123;
417 // sync to first lo-hi transition, and threshold
419 // Need to threshold first sample
421 if(dest
[0] < threshold_value
) dest
[0] = 0;
425 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
426 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
427 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
428 for(idx
= 1; idx
< size
; idx
++) {
429 // threshold current value
431 if (dest
[idx
] < threshold_value
) dest
[idx
] = 0;
434 // Check for 0->1 transition
435 if (dest
[idx
-1] < dest
[idx
]) { // 0 -> 1 transition
436 if ((idx
-last_transition
)<(fclow
-2)){ //0-5 = garbage noise
437 //do nothing with extra garbage
438 } else if ((idx
-last_transition
) < (fchigh
-1)) { //6-8 = 8 waves
440 } else { //9+ = 10 waves
443 last_transition
= idx
;
447 return numBits
; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
450 uint32_t myround2(float f
)
452 if (f
>= 2000) return 2000;//something bad happened
453 return (uint32_t) (f
+ (float)0.5);
456 //translate 11111100000 to 10
457 size_t aggregate_bits(uint8_t *dest
, size_t size
, uint8_t rfLen
, uint8_t maxConsequtiveBits
,
458 uint8_t invert
, uint8_t fchigh
, uint8_t fclow
)
460 uint8_t lastval
=dest
[0];
465 for( idx
=1; idx
< size
; idx
++) {
467 if (dest
[idx
]==lastval
) {
471 //if lastval was 1, we have a 1->0 crossing
472 if ( dest
[idx
-1]==1 ) {
473 n
=myround2((float)(n
+1)/((float)(rfLen
)/(float)fclow
));
474 } else {// 0->1 crossing
475 n
=myround2((float)(n
+1)/((float)(rfLen
-1)/(float)fchigh
)); //-1 for fudge factor
479 if(n
< maxConsequtiveBits
) //Consecutive
481 if(invert
==0){ //invert bits
482 memset(dest
+numBits
, dest
[idx
-1] , n
);
484 memset(dest
+numBits
, dest
[idx
-1]^1 , n
);
493 //by marshmellow (from holiman's base)
494 // full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod)
495 int fskdemod(uint8_t *dest
, size_t size
, uint8_t rfLen
, uint8_t invert
, uint8_t fchigh
, uint8_t fclow
)
498 size
= fsk_wave_demod(dest
, size
, fchigh
, fclow
);
499 size
= aggregate_bits(dest
, size
, rfLen
, 192, invert
, fchigh
, fclow
);
502 // loop to get raw HID waveform then FSK demodulate the TAG ID from it
503 int HIDdemodFSK(uint8_t *dest
, size_t size
, uint32_t *hi2
, uint32_t *hi
, uint32_t *lo
)
506 size_t idx
=0; //, found=0; //size=0,
508 size
= fskdemod(dest
, size
,50,0,10,8);
510 // final loop, go over previously decoded manchester data and decode into usable tag ID
511 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
512 uint8_t frame_marker_mask
[] = {1,1,1,0,0,0};
516 while( idx
+ sizeof(frame_marker_mask
) < size
) {
517 // search for a start of frame marker
518 if ( memcmp(dest
+idx
, frame_marker_mask
, sizeof(frame_marker_mask
)) == 0)
519 { // frame marker found
520 idx
+=sizeof(frame_marker_mask
);
521 while(dest
[idx
] != dest
[idx
+1] && idx
< size
-2)
523 // Keep going until next frame marker (or error)
524 // Shift in a bit. Start by shifting high registers
525 *hi2
= (*hi2
<<1)|(*hi
>>31);
526 *hi
= (*hi
<<1)|(*lo
>>31);
527 //Then, shift in a 0 or one into low
528 if (dest
[idx
] && !dest
[idx
+1]) // 1 0
535 // Hopefully, we read a tag and hit upon the next frame marker
536 if(idx
+ sizeof(frame_marker_mask
) < size
)
538 if ( memcmp(dest
+idx
, frame_marker_mask
, sizeof(frame_marker_mask
)) == 0)
545 *hi2
= *hi
= *lo
= 0;
554 uint32_t bytebits_to_byte(uint8_t* src
, size_t numbits
)
557 for(int i
= 0 ; i
< numbits
; i
++)
559 num
= (num
<< 1) | (*src
);
565 int IOdemodFSK(uint8_t *dest
, size_t size
)
567 static const uint8_t THRESHOLD
= 129;
569 //make sure buffer has data
570 if (size
< 66) return -1;
571 //test samples are not just noise
572 uint8_t justNoise
= 1;
573 for(idx
=0;idx
< size
&& justNoise
;idx
++){
574 justNoise
= dest
[idx
] < THRESHOLD
;
576 if(justNoise
) return 0;
579 size
= fskdemod(dest
, size
, 64, 1, 10, 8); // RF/64 and invert
580 if (size
< 65) return -1; //did we get a good demod?
582 //0 10 20 30 40 50 60
584 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
585 //-----------------------------------------------------------------------------
586 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
588 //XSF(version)facility:codeone+codetwo
590 uint8_t mask
[] = {0,0,0,0,0,0,0,0,0,1};
591 for( idx
=0; idx
< (size
- 65); idx
++) {
592 if ( memcmp(dest
+ idx
, mask
, sizeof(mask
))==0) {
594 if (!dest
[idx
+8] && dest
[idx
+17]==1 && dest
[idx
+26]==1 && dest
[idx
+35]==1 && dest
[idx
+44]==1 && dest
[idx
+53]==1){
595 //confirmed proper separator bits found
596 //return start position
605 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
606 // maybe somehow adjust peak trimming value based on samples to fix?
607 int DetectASKClock(uint8_t dest
[], size_t size
, int clock
)
612 int clk
[]={16,32,40,50,64,100,128,256};
613 int loopCnt
= 256; //don't need to loop through entire array...
614 if (size
<loopCnt
) loopCnt
= size
;
616 //if we already have a valid clock quit
618 if (clk
[i
] == clock
) return clock
;
620 //get high and low peak
621 for (i
=0; i
< loopCnt
; ++i
){
629 peak
=(int)(((peak
-128)*.75)+128);
630 low
= (int)(((low
-128)*.75)+128);
634 int bestErr
[]={1000,1000,1000,1000,1000,1000,1000,1000};
636 //test each valid clock from smallest to greatest to see which lines up
637 for(clkCnt
=0; clkCnt
< 6; ++clkCnt
){
638 if (clk
[clkCnt
] == 32){
643 bestErr
[clkCnt
]=1000;
644 //try lining up the peaks by moving starting point (try first 256)
645 for (ii
=0; ii
< loopCnt
; ++ii
){
646 if ((dest
[ii
] >= peak
) || (dest
[ii
] <= low
)){
648 // now that we have the first one lined up test rest of wave array
649 for (i
=0; i
<((int)(size
/clk
[clkCnt
])-1); ++i
){
650 if (dest
[ii
+(i
*clk
[clkCnt
])]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])]<=low
){
651 }else if(dest
[ii
+(i
*clk
[clkCnt
])-tol
]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])-tol
]<=low
){
652 }else if(dest
[ii
+(i
*clk
[clkCnt
])+tol
]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])+tol
]<=low
){
653 }else{ //error no peak detected
657 //if we found no errors this is correct one - return this clock
658 if(errCnt
==0) return clk
[clkCnt
];
659 //if we found errors see if it is lowest so far and save it as best run
660 if(errCnt
<bestErr
[clkCnt
]) bestErr
[clkCnt
]=errCnt
;
666 for (iii
=0; iii
<7;++iii
){
667 if (bestErr
[iii
]<bestErr
[best
]){
668 // current best bit to error ratio vs new bit to error ratio
669 if (((size
/clk
[best
])/bestErr
[best
] < (size
/clk
[iii
])/bestErr
[iii
]) ){
678 //detect psk clock by reading #peaks vs no peaks(or errors)
679 int DetectpskNRZClock(uint8_t dest
[], size_t size
, int clock
)
684 int clk
[]={16,32,40,50,64,100,128,256};
685 int loopCnt
= 2048; //don't need to loop through entire array...
686 if (size
<loopCnt
) loopCnt
= size
;
688 //if we already have a valid clock quit
690 if (clk
[i
] == clock
) return clock
;
692 //get high and low peak
693 for (i
=0; i
< loopCnt
; ++i
){
701 peak
=(int)(((peak
-128)*.75)+128);
702 low
= (int)(((low
-128)*.75)+128);
703 //PrintAndLog("DEBUG: peak: %d, low: %d",peak,low);
709 int bestErr
[]={1000,1000,1000,1000,1000,1000,1000,1000,1000};
710 int peaksdet
[]={0,0,0,0,0,0,0,0,0};
711 //test each valid clock from smallest to greatest to see which lines up
712 for(clkCnt
=0; clkCnt
< 6; ++clkCnt
){
713 if (clk
[clkCnt
] == 32){
718 //try lining up the peaks by moving starting point (try first 256)
719 for (ii
=0; ii
< loopCnt
; ++ii
){
720 if ((dest
[ii
] >= peak
) || (dest
[ii
] <= low
)){
723 // now that we have the first one lined up test rest of wave array
724 for (i
=0; i
< ((int)(size
/clk
[clkCnt
])-1); ++i
){
725 if (dest
[ii
+(i
*clk
[clkCnt
])]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])]<=low
){
727 }else if(dest
[ii
+(i
*clk
[clkCnt
])-tol
]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])-tol
]<=low
){
729 }else if(dest
[ii
+(i
*clk
[clkCnt
])+tol
]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])+tol
]<=low
){
731 }else{ //error no peak detected
735 if(peakcnt
>peaksdet
[clkCnt
]) {
736 peaksdet
[clkCnt
]=peakcnt
;
737 bestErr
[clkCnt
]=errCnt
;
744 //int ratio2; //debug
747 for (iii
=0; iii
< 7; ++iii
){
749 //ratio2=1000; //debug
750 //bits=size/clk[iii]; //debug
751 if (peaksdet
[iii
] > 0){
752 ratio
=bestErr
[iii
]/peaksdet
[iii
];
753 if (((bestErr
[best
]/peaksdet
[best
]) > (ratio
)+1)){
756 //ratio2=bits/peaksdet[iii]; //debug
758 //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);
763 //by marshmellow (attempt to get rid of high immediately after a low)
764 void pskCleanWave(uint8_t *bitStream
, size_t size
)
770 // int loopMax = 2048;
773 for (i
=0; i
< size
; ++i
){
774 if (bitStream
[i
] < low
) low
=bitStream
[i
];
775 if (bitStream
[i
] > high
) high
=bitStream
[i
];
777 high
= (int)(((high
-128)*.80)+128);
778 low
= (int)(((low
-128)*.90)+128);
779 //low = (uint8_t)(((int)(low)-128)*.80)+128;
780 for (i
=0; i
< size
; ++i
){
788 }else if (newHigh
== 1){
796 if (bitStream
[i
] <= low
) newLow
=1;
797 if (bitStream
[i
] >= high
) newHigh
=1;
803 //redesigned by marshmellow adjusted from existing decode functions
804 //indala id decoding - only tested on 26 bit tags, but attempted to make it work for more
805 int indala26decode(uint8_t *bitStream
, size_t *size
, uint8_t *invert
)
807 //26 bit 40134 format (don't know other formats)
809 int long_wait
=29;//29 leading zeros in format
815 // Finding the start of a UID
816 for (start
= 0; start
<= *size
- 250; start
++) {
817 first
= bitStream
[start
];
818 for (i
= start
; i
< start
+ long_wait
; i
++) {
819 if (bitStream
[i
] != first
) {
823 if (i
== (start
+ long_wait
)) {
827 if (start
== *size
- 250 + 1) {
828 // did not find start sequence
831 // Inverting signal if needed
833 for (i
= start
; i
< *size
; i
++) {
834 bitStream
[i
] = !bitStream
[i
];
840 //found start once now test length by finding next one
841 for (ii
=start
+29; ii
<= *size
- 250; ii
++) {
842 first2
= bitStream
[ii
];
843 for (iii
= ii
; iii
< ii
+ long_wait
; iii
++) {
844 if (bitStream
[iii
] != first2
) {
848 if (iii
== (ii
+ long_wait
)) {
852 if (ii
== *size
- 250 + 1){
853 // did not find second start sequence
860 for (ii
= 0; ii
< bitCnt
; ii
++) {
861 bitStream
[ii
] = bitStream
[i
++];
868 //by marshmellow - demodulate PSK wave or NRZ wave (both similar enough)
869 //peaks switch bit (high=1 low=0) each clock cycle = 1 bit determined by last peak
870 int pskNRZrawDemod(uint8_t *dest
, size_t *size
, int *clk
, int *invert
)
872 pskCleanWave(dest
,*size
);
873 int clk2
= DetectpskNRZClock(dest
, *size
, *clk
);
876 uint8_t high
=0, low
=255;
877 uint32_t gLen
= *size
;
878 if (gLen
> 1280) gLen
=1280;
880 for (i
=0; i
< gLen
; ++i
){
881 if (dest
[i
] > high
) high
= dest
[i
];
882 if (dest
[i
] < low
) low
= dest
[i
];
884 //fudge high/low bars by 25%
885 high
= (uint8_t)((((int)(high
)-128)*.75)+128);
886 low
= (uint8_t)((((int)(low
)-128)*.80)+128);
888 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
889 int lastBit
= 0; //set first clock check
890 uint32_t bitnum
= 0; //output counter
891 uint8_t tol
= 0; //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
892 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
895 uint32_t bestStart
= *size
;
896 uint32_t maxErr
= (*size
/1000);
897 uint32_t bestErrCnt
= maxErr
;
901 uint8_t ignorewin
=*clk
/8;
902 //PrintAndLog("DEBUG - lastbit - %d",lastBit);
903 //loop to find first wave that works - align to clock
904 for (iii
=0; iii
< gLen
; ++iii
){
905 if ((dest
[iii
]>=high
) || (dest
[iii
]<=low
)){
907 //loop through to see if this start location works
908 for (i
= iii
; i
< *size
; ++i
) {
909 //if we found a high bar and we are at a clock bit
910 if ((dest
[i
]>=high
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
915 //else if low bar found and we are at a clock point
916 }else if ((dest
[i
]<=low
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
921 //else if no bars found
922 }else if(dest
[i
] < high
&& dest
[i
] > low
) {
926 //if we are past a clock point
927 if (i
>= lastBit
+*clk
+tol
){ //clock val
931 //else if bar found but we are not at a clock bit and we did not just have a clock bit
932 }else if ((dest
[i
]>=high
|| dest
[i
]<=low
) && (i
<lastBit
+*clk
-tol
|| i
>lastBit
+*clk
+tol
) && (bitHigh
==0)){
933 //error bar found no clock...
936 if (bitnum
>=1000) break;
938 //we got more than 64 good bits and not all errors
939 if ((bitnum
> (64+errCnt
)) && (errCnt
< (maxErr
))) {
944 break; //great read - finish
946 if (bestStart
== iii
) break; //if current run == bestErrCnt run (after exhausted testing) then finish
947 if (errCnt
< bestErrCnt
){ //set this as new best run
954 if (bestErrCnt
< maxErr
){
955 //best run is good enough set to best run and set overwrite BinStream
957 lastBit
=bestStart
-*clk
;
959 for (i
= iii
; i
< *size
; ++i
) {
960 //if we found a high bar and we are at a clock bit
961 if ((dest
[i
] >= high
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
968 //else if low bar found and we are at a clock point
969 }else if ((dest
[i
]<=low
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
976 //else if no bars found
977 }else if(dest
[i
]<high
&& dest
[i
]>low
) {
981 //if we are past a clock point
982 if (i
>=lastBit
+*clk
+tol
){ //clock val
987 //else if bar found but we are not at a clock bit and we did not just have a clock bit
988 }else if ((dest
[i
]>=high
|| dest
[i
]<=low
) && ((i
<lastBit
+*clk
-tol
) || (i
>lastBit
+*clk
+tol
)) && (bitHigh
==0)){
989 //error bar found no clock...
995 if (bitnum
>=1000) break;