]>
git.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 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 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 Em410xDecode ( 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 for ( uint8_t ii
= 0 ; ii
< 4 ; ii
++){
112 lo
= ( lo
<< 1LL ) | ( BitStream
[( i
* 5 )+ ii
+ idx
]);
115 if ( errChk
!= 0 ) return lo
;
116 //skip last 5 bit parity test for simplicity.
123 //takes 2 arguments - clock and invert both as integers
124 //attempts to demodulate ask while decoding manchester
125 //prints binary found and saves in graphbuffer for further commands
126 int askmandemod ( uint8_t * BinStream
, size_t * size
, int * clk
, int * invert
)
130 * clk
= DetectASKClock ( BinStream
, * size
, * clk
); //clock default
132 // if autodetected too low then adjust //MAY NEED ADJUSTMENT
133 if ( clk2
== 0 && * clk
< 8 ) * clk
= 64 ;
134 if ( clk2
== 0 && * clk
< 32 ) * clk
= 32 ;
135 if (* invert
!= 0 && * invert
!= 1 ) * invert
= 0 ;
136 uint32_t initLoopMax
= 200 ;
137 if ( initLoopMax
> * size
) initLoopMax
=* size
;
138 // Detect high and lows
139 // 25% fuzz in case highs and lows aren't clipped [marshmellow]
141 ans
= getHiLo ( BinStream
, initLoopMax
, & high
, & low
, 75 , 75 );
142 if ( ans
< 1 ) return - 2 ; //just noise
144 // PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
145 int lastBit
= 0 ; //set first clock check
146 uint32_t bitnum
= 0 ; //output counter
147 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
148 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
150 uint32_t gLen
= * size
;
151 if ( gLen
> 3000 ) gLen
= 3000 ;
153 uint32_t bestStart
= * size
;
154 uint32_t bestErrCnt
= (* size
/ 1000 );
155 uint32_t maxErr
= (* size
/ 1000 );
156 // PrintAndLog("DEBUG - lastbit - %d",lastBit);
157 // loop to find first wave that works
158 for ( iii
= 0 ; iii
< gLen
; ++ iii
){
159 if (( BinStream
[ iii
] >= high
) || ( BinStream
[ iii
] <= low
)){
162 // loop through to see if this start location works
163 for ( i
= iii
; i
< * size
; ++ i
) {
164 if (( BinStream
[ i
] >= high
) && (( i
- lastBit
) > (* clk
- tol
))){
166 } else if (( BinStream
[ i
] <= low
) && (( i
- lastBit
) > (* clk
- tol
))){
167 //low found and we are expecting a bar
170 //mid value found or no bar supposed to be here
171 if (( i
- lastBit
)>(* clk
+ tol
)){
172 //should have hit a high or low based on clock!!
175 //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);
178 lastBit
+=* clk
; //skip over until hit too many errors
179 if ( errCnt
>( maxErr
)) break ; //allow 1 error for every 1000 samples else start over
182 if (( i
- iii
) >( 400 * * clk
)) break ; //got plenty of bits
184 //we got more than 64 good bits and not all errors
185 if (((( i
- iii
)/ * clk
) > ( 64 + errCnt
)) && ( errCnt
< maxErr
)) {
190 break ; //great read - finish
192 if ( errCnt
< bestErrCnt
){ //set this as new best run
199 if ( bestErrCnt
< maxErr
){
200 //best run is good enough set to best run and set overwrite BinStream
202 lastBit
= bestStart
- * clk
;
204 for ( i
= iii
; i
< * size
; ++ i
) {
205 if (( BinStream
[ i
] >= high
) && (( i
- lastBit
) > (* clk
- tol
))){
207 BinStream
[ bitnum
] = * invert
;
209 } else if (( BinStream
[ i
] <= low
) && (( i
- lastBit
) > (* clk
- tol
))){
210 //low found and we are expecting a bar
212 BinStream
[ bitnum
] = 1 -* invert
;
215 //mid value found or no bar supposed to be here
216 if (( i
- lastBit
)>(* clk
+ tol
)){
217 //should have hit a high or low based on clock!!
220 //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);
222 BinStream
[ bitnum
]= 77 ;
226 lastBit
+=* clk
; //skip over error
229 if ( bitnum
>= 400 ) break ;
241 //encode binary data into binary manchester
242 int ManchesterEncode ( uint8_t * BitStream
, size_t size
)
244 size_t modIdx
= 20000 , i
= 0 ;
245 if ( size
> modIdx
) return - 1 ;
246 for ( size_t idx
= 0 ; idx
< size
; idx
++){
247 BitStream
[ idx
+ modIdx
++] = BitStream
[ idx
];
248 BitStream
[ idx
+ modIdx
++] = BitStream
[ idx
]^ 1 ;
250 for (; i
<( size
* 2 ); i
++){
251 BitStream
[ i
] = BitStream
[ i
+ 20000 ];
257 //take 10 and 01 and manchester decode
258 //run through 2 times and take least errCnt
259 int manrawdecode ( uint8_t * BitStream
, size_t * size
)
267 for ( ii
= 1 ; ii
< 3 ;++ ii
){
269 for ( i
= i
+ ii
; i
<* size
- 2 ; i
+= 2 ){
270 if ( BitStream
[ i
]== 1 && ( BitStream
[ i
+ 1 ]== 0 )){
271 } else if (( BitStream
[ i
]== 0 )&& BitStream
[ i
+ 1 ]== 1 ){
275 if ( bitnum
> 300 ) break ;
287 for ( i
= i
+ ii
; i
< * size
- 2 ; i
+= 2 ){
288 if ( BitStream
[ i
] == 1 && ( BitStream
[ i
+ 1 ] == 0 )){
289 BitStream
[ bitnum
++]= 0 ;
290 } else if (( BitStream
[ i
] == 0 ) && BitStream
[ i
+ 1 ] == 1 ){
291 BitStream
[ bitnum
++]= 1 ;
293 BitStream
[ bitnum
++]= 77 ;
296 if ( bitnum
> 300 ) break ;
304 //take 01 or 10 = 0 and 11 or 00 = 1
305 int BiphaseRawDecode ( uint8_t * BitStream
, size_t * size
, int offset
, int invert
)
311 for (; i
<* size
- 2 ; i
+= 2 ){
312 if (( BitStream
[ i
]== 1 && BitStream
[ i
+ 1 ]== 0 ) || ( BitStream
[ i
]== 0 && BitStream
[ i
+ 1 ]== 1 )){
313 BitStream
[ bitnum
++]= 1 ^ invert
;
314 } else if (( BitStream
[ i
]== 0 && BitStream
[ i
+ 1 ]== 0 ) || ( BitStream
[ i
]== 1 && BitStream
[ i
+ 1 ]== 1 )){
315 BitStream
[ bitnum
++]= invert
;
317 BitStream
[ bitnum
++]= 77 ;
320 if ( bitnum
> 250 ) break ;
327 //takes 2 arguments - clock and invert both as integers
328 //attempts to demodulate ask only
329 //prints binary found and saves in graphbuffer for further commands
330 int askrawdemod ( uint8_t * BinStream
, size_t * size
, int * clk
, int * invert
)
333 // int invert=0; //invert default
335 * clk
= DetectASKClock ( BinStream
, * size
, * clk
); //clock default
336 //uint8_t BitStream[502] = {0};
338 //HACK: if clock not detected correctly - default
339 if ( clk2
== 0 && * clk
< 8 ) * clk
= 64 ;
340 if ( clk2
== 0 && * clk
< 32 && clk2
== 0 ) * clk
= 32 ;
341 if (* invert
!= 0 && * invert
!= 1 ) * invert
= 0 ;
342 uint32_t initLoopMax
= 200 ;
343 if ( initLoopMax
> * size
) initLoopMax
=* size
;
344 // Detect high and lows
345 //25% fuzz in case highs and lows aren't clipped [marshmellow]
347 ans
= getHiLo ( BinStream
, initLoopMax
, & high
, & low
, 75 , 75 );
348 if ( ans
< 1 ) return - 2 ; //just noise
350 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
351 int lastBit
= 0 ; //set first clock check
352 uint32_t bitnum
= 0 ; //output counter
353 uint8_t tol
= 0 ; //clock tolerance adjust - waves will be accepted as within the clock
354 // if they fall + or - this value + clock from last valid wave
355 if (* clk
== 32 ) tol
= 1 ; //clock tolerance may not be needed anymore currently set to
356 // + or - 1 but could be increased for poor waves or removed entirely
358 uint32_t gLen
= * size
;
359 if ( gLen
> 500 ) gLen
= 500 ;
361 uint32_t bestStart
= * size
;
362 uint32_t bestErrCnt
= (* size
/ 1000 );
363 uint32_t maxErr
= bestErrCnt
;
365 //PrintAndLog("DEBUG - lastbit - %d",lastBit);
366 //loop to find first wave that works
367 for ( iii
= 0 ; iii
< gLen
; ++ iii
){
368 if (( BinStream
[ iii
]>= high
) || ( BinStream
[ iii
]<= low
)){
370 //loop through to see if this start location works
371 for ( i
= iii
; i
< * size
; ++ i
) {
372 if (( BinStream
[ i
] >= high
) && (( i
- lastBit
)>(* clk
- tol
))){
375 } else if (( BinStream
[ i
] <= low
) && (( i
- lastBit
)>(* clk
- tol
))){
376 //low found and we are expecting a bar
379 } else if (( BinStream
[ i
]<= low
) && ( midBit
== 0 ) && (( i
- lastBit
)>((* clk
/ 2 )- tol
))){
382 } else if (( BinStream
[ i
]>= high
) && ( midBit
== 0 ) && (( i
- lastBit
)>((* clk
/ 2 )- tol
))){
385 } else if (( i
- lastBit
)>((* clk
/ 2 )+ tol
) && ( midBit
== 0 )){
389 //mid value found or no bar supposed to be here
391 if (( i
- lastBit
)>(* clk
+ tol
)){
392 //should have hit a high or low based on clock!!
394 //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);
397 lastBit
+=* clk
; //skip over until hit too many errors
398 if ( errCnt
> ((* size
/ 1000 ))){ //allow 1 error for every 1000 samples else start over
404 if (( i
- iii
)>( 500 * * clk
)) break ; //got enough bits
406 //we got more than 64 good bits and not all errors
407 if (((( i
- iii
)/ * clk
) > ( 64 + errCnt
)) && ( errCnt
<(* size
/ 1000 ))) {
412 break ; //great read - finish
414 if ( errCnt
< bestErrCnt
){ //set this as new best run
421 if ( bestErrCnt
< maxErr
){
422 //best run is good enough - set to best run and overwrite BinStream
424 lastBit
= bestStart
- * clk
;
426 for ( i
= iii
; i
< * size
; ++ i
) {
427 if (( BinStream
[ i
] >= high
) && (( i
- lastBit
) > (* clk
- tol
))){
429 BinStream
[ bitnum
] = * invert
;
432 } else if (( BinStream
[ i
] <= low
) && (( i
- lastBit
) > (* clk
- tol
))){
433 //low found and we are expecting a bar
435 BinStream
[ bitnum
] = 1 -* invert
;
438 } else if (( BinStream
[ i
]<= low
) && ( midBit
== 0 ) && (( i
- lastBit
)>((* clk
/ 2 )- tol
))){
441 BinStream
[ bitnum
] = 1 - * invert
;
443 } else if (( BinStream
[ i
]>= high
) && ( midBit
== 0 ) && (( i
- lastBit
)>((* clk
/ 2 )- tol
))){
446 BinStream
[ bitnum
] = * invert
;
448 } else if (( i
- lastBit
)>((* clk
/ 2 )+ tol
) && ( midBit
== 0 )){
451 if ( bitnum
!= 0 ) BinStream
[ bitnum
] = BinStream
[ bitnum
- 1 ];
455 //mid value found or no bar supposed to be here
456 if (( i
- lastBit
)>(* clk
+ tol
)){
457 //should have hit a high or low based on clock!!
460 //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);
462 BinStream
[ bitnum
]= 77 ;
466 lastBit
+=* clk
; //skip over error
469 if ( bitnum
>= 400 ) break ;
479 //translate wave to 11111100000 (1 for each short wave 0 for each long wave)
480 size_t fsk_wave_demod ( uint8_t * dest
, size_t size
, uint8_t fchigh
, uint8_t fclow
)
482 uint32_t last_transition
= 0 ;
485 if ( fchigh
== 0 ) fchigh
= 10 ;
486 if ( fclow
== 0 ) fclow
= 8 ;
487 //set the threshold close to 0 (graph) or 128 std to avoid static
488 uint8_t threshold_value
= 123 ;
490 // sync to first lo-hi transition, and threshold
492 // Need to threshold first sample
494 if ( dest
[ 0 ] < threshold_value
) dest
[ 0 ] = 0 ;
498 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
499 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
500 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
501 for ( idx
= 1 ; idx
< size
; idx
++) {
502 // threshold current value
504 if ( dest
[ idx
] < threshold_value
) dest
[ idx
] = 0 ;
507 // Check for 0->1 transition
508 if ( dest
[ idx
- 1 ] < dest
[ idx
]) { // 0 -> 1 transition
509 if (( idx
- last_transition
)<( fclow
- 2 )){ //0-5 = garbage noise
510 //do nothing with extra garbage
511 } else if (( idx
- last_transition
) < ( fchigh
- 1 )) { //6-8 = 8 waves
513 } else { //9+ = 10 waves
516 last_transition
= idx
;
520 return numBits
; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
523 uint32_t myround2 ( float f
)
525 if ( f
>= 2000 ) return 2000 ; //something bad happened
526 return ( uint32_t ) ( f
+ ( float ) 0.5 );
529 //translate 11111100000 to 10
530 size_t aggregate_bits ( uint8_t * dest
, size_t size
, uint8_t rfLen
, uint8_t maxConsequtiveBits
,
531 uint8_t invert
, uint8_t fchigh
, uint8_t fclow
)
533 uint8_t lastval
= dest
[ 0 ];
538 for ( idx
= 1 ; idx
< size
; idx
++) {
540 if ( dest
[ idx
]== lastval
) {
544 //if lastval was 1, we have a 1->0 crossing
545 if ( dest
[ idx
- 1 ]== 1 ) {
546 n
= myround2 (( float )( n
+ 1 )/(( float )( rfLen
)/( float ) fclow
));
547 } else { // 0->1 crossing
548 n
= myround2 (( float )( n
+ 1 )/(( float )( rfLen
- 1 )/( float ) fchigh
)); //-1 for fudge factor
552 if ( n
< maxConsequtiveBits
) //Consecutive
554 if ( invert
== 0 ){ //invert bits
555 memset ( dest
+ numBits
, dest
[ idx
- 1 ] , n
);
557 memset ( dest
+ numBits
, dest
[ idx
- 1 ]^ 1 , n
);
566 //by marshmellow (from holiman's base)
567 // full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod)
568 int fskdemod ( uint8_t * dest
, size_t size
, uint8_t rfLen
, uint8_t invert
, uint8_t fchigh
, uint8_t fclow
)
571 size
= fsk_wave_demod ( dest
, size
, fchigh
, fclow
);
572 size
= aggregate_bits ( dest
, size
, rfLen
, 192 , invert
, fchigh
, fclow
);
576 // loop to get raw HID waveform then FSK demodulate the TAG ID from it
577 int HIDdemodFSK ( uint8_t * dest
, size_t * size
, uint32_t * hi2
, uint32_t * hi
, uint32_t * lo
)
579 if ( justNoise ( dest
, * size
)) return - 1 ;
581 size_t numStart
= 0 , size2
=* size
, startIdx
= 0 ;
583 * size
= fskdemod ( dest
, size2
, 50 , 1 , 10 , 8 ); //fsk2a
584 if (* size
< 96 ) return - 2 ;
585 // 00011101 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1
586 uint8_t preamble
[] = { 0 , 0 , 0 , 1 , 1 , 1 , 0 , 1 };
587 // find bitstring in array
588 uint8_t errChk
= preambleSearch ( dest
, preamble
, sizeof ( preamble
), size
, & startIdx
);
589 if ( errChk
== 0 ) return - 3 ; //preamble not found
591 numStart
= startIdx
+ sizeof ( preamble
);
592 // final loop, go over previously decoded FSK data and manchester decode into usable tag ID
593 for ( size_t idx
= numStart
; ( idx
- numStart
) < * size
- sizeof ( preamble
); idx
+= 2 ){
594 if ( dest
[ idx
] == dest
[ idx
+ 1 ]){
595 return - 4 ; //not manchester data
597 * hi2
= (* hi2
<< 1 )|(* hi
>> 31 );
598 * hi
= (* hi
<< 1 )|(* lo
>> 31 );
599 //Then, shift in a 0 or one into low
600 if ( dest
[ idx
] && ! dest
[ idx
+ 1 ]) // 1 0
605 return ( int ) startIdx
;
608 // loop to get raw paradox waveform then FSK demodulate the TAG ID from it
609 int ParadoxdemodFSK ( uint8_t * dest
, size_t * size
, uint32_t * hi2
, uint32_t * hi
, uint32_t * lo
)
611 if ( justNoise ( dest
, * size
)) return - 1 ;
613 size_t numStart
= 0 , size2
=* size
, startIdx
= 0 ;
615 * size
= fskdemod ( dest
, size2
, 50 , 1 , 10 , 8 ); //fsk2a
616 if (* size
< 96 ) return - 2 ;
618 // 00001111 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1
619 uint8_t preamble
[] = { 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 };
621 uint8_t errChk
= preambleSearch ( dest
, preamble
, sizeof ( preamble
), size
, & startIdx
);
622 if ( errChk
== 0 ) return - 3 ; //preamble not found
624 numStart
= startIdx
+ sizeof ( preamble
);
625 // final loop, go over previously decoded FSK data and manchester decode into usable tag ID
626 for ( size_t idx
= numStart
; ( idx
- numStart
) < * size
- sizeof ( preamble
); idx
+= 2 ){
627 if ( dest
[ idx
] == dest
[ idx
+ 1 ])
628 return - 4 ; //not manchester data
629 * hi2
= (* hi2
<< 1 )|(* hi
>> 31 );
630 * hi
= (* hi
<< 1 )|(* lo
>> 31 );
631 //Then, shift in a 0 or one into low
632 if ( dest
[ idx
] && ! dest
[ idx
+ 1 ]) // 1 0
637 return ( int ) startIdx
;
640 uint32_t bytebits_to_byte ( uint8_t * src
, size_t numbits
)
643 for ( int i
= 0 ; i
< numbits
; i
++)
645 num
= ( num
<< 1 ) | (* src
);
651 int IOdemodFSK ( uint8_t * dest
, size_t size
)
653 if ( justNoise ( dest
, size
)) return - 1 ;
654 //make sure buffer has data
655 if ( size
< 66 * 64 ) return - 2 ;
657 size
= fskdemod ( dest
, size
, 64 , 1 , 10 , 8 ); // FSK2a RF/64
658 if ( size
< 65 ) return - 3 ; //did we get a good demod?
660 //0 10 20 30 40 50 60
662 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
663 //-----------------------------------------------------------------------------
664 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
666 //XSF(version)facility:codeone+codetwo
669 uint8_t preamble
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 };
670 uint8_t errChk
= preambleSearch ( dest
, preamble
, sizeof ( preamble
), & size
, & startIdx
);
671 if ( errChk
== 0 ) return - 4 ; //preamble not found
673 if (! dest
[ startIdx
+ 8 ] && dest
[ startIdx
+ 17 ]== 1 && dest
[ startIdx
+ 26 ]== 1 && dest
[ startIdx
+ 35 ]== 1 && dest
[ startIdx
+ 44 ]== 1 && dest
[ startIdx
+ 53 ]== 1 ){
674 //confirmed proper separator bits found
675 //return start position
676 return ( int ) startIdx
;
682 // takes a array of binary values, start position, length of bits per parity (includes parity bit),
683 // Parity Type (1 for odd 0 for even), and binary Length (length to run)
684 size_t removeParity ( uint8_t * BitStream
, size_t startIdx
, uint8_t pLen
, uint8_t pType
, size_t bLen
)
686 uint32_t parityWd
= 0 ;
687 size_t j
= 0 , bitCnt
= 0 ;
688 for ( int word
= 0 ; word
< ( bLen
); word
+= pLen
){
689 for ( int bit
= 0 ; bit
< pLen
; bit
++){
690 parityWd
= ( parityWd
<< 1 ) | BitStream
[ startIdx
+ word
+ bit
];
691 BitStream
[ j
++] = ( BitStream
[ startIdx
+ word
+ bit
]);
694 // if parity fails then return 0
695 if ( parityTest ( parityWd
, pLen
, pType
) == 0 ) return - 1 ;
699 // if we got here then all the parities passed
700 //return ID start index and size
705 // FSK Demod then try to locate an AWID ID
706 int AWIDdemodFSK ( uint8_t * dest
, size_t * size
)
708 //make sure buffer has enough data
709 if (* size
< 96 * 50 ) return - 1 ;
711 if ( justNoise ( dest
, * size
)) return - 2 ;
714 * size
= fskdemod ( dest
, * size
, 50 , 1 , 10 , 8 ); // fsk2a RF/50
715 if (* size
< 96 ) return - 3 ; //did we get a good demod?
717 uint8_t preamble
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 };
719 uint8_t errChk
= preambleSearch ( dest
, preamble
, sizeof ( preamble
), size
, & startIdx
);
720 if ( errChk
== 0 ) return - 4 ; //preamble not found
721 if (* size
!= 96 ) return - 5 ;
722 return ( int ) startIdx
;
726 // FSK Demod then try to locate an Farpointe Data (pyramid) ID
727 int PyramiddemodFSK ( uint8_t * dest
, size_t * size
)
729 //make sure buffer has data
730 if (* size
< 128 * 50 ) return - 5 ;
732 //test samples are not just noise
733 if ( justNoise ( dest
, * size
)) return - 1 ;
736 * size
= fskdemod ( dest
, * size
, 50 , 1 , 10 , 8 ); // fsk2a RF/50
737 if (* size
< 128 ) return - 2 ; //did we get a good demod?
739 uint8_t preamble
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 };
741 uint8_t errChk
= preambleSearch ( dest
, preamble
, sizeof ( preamble
), size
, & startIdx
);
742 if ( errChk
== 0 ) return - 4 ; //preamble not found
743 if (* size
!= 128 ) return - 3 ;
744 return ( int ) startIdx
;
748 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
749 // maybe somehow adjust peak trimming value based on samples to fix?
750 int DetectASKClock ( uint8_t dest
[], size_t size
, int clock
)
753 int clk
[]={ 8 , 16 , 32 , 40 , 50 , 64 , 100 , 128 , 256 };
754 int loopCnt
= 256 ; //don't need to loop through entire array...
755 if ( size
< loopCnt
) loopCnt
= size
;
757 //if we already have a valid clock quit
760 if ( clk
[ i
] == clock
) return clock
;
762 //get high and low peak
764 getHiLo ( dest
, loopCnt
, & peak
, & low
, 75 , 75 );
769 int bestErr
[]={ 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 };
771 //test each valid clock from smallest to greatest to see which lines up
772 for ( clkCnt
= 0 ; clkCnt
< 8 ; ++ clkCnt
){
773 if ( clk
[ clkCnt
] == 32 ){
778 bestErr
[ clkCnt
]= 1000 ;
779 //try lining up the peaks by moving starting point (try first 256)
780 for ( ii
= 0 ; ii
< loopCnt
; ++ ii
){
781 if (( dest
[ ii
] >= peak
) || ( dest
[ ii
] <= low
)){
783 // now that we have the first one lined up test rest of wave array
784 for ( i
= 0 ; i
<(( int )(( size
- ii
- tol
)/ clk
[ clkCnt
])- 1 ); ++ i
){
785 if ( dest
[ ii
+( i
* clk
[ clkCnt
])]>= peak
|| dest
[ ii
+( i
* clk
[ clkCnt
])]<= low
){
786 } else if ( dest
[ ii
+( i
* clk
[ clkCnt
])- tol
]>= peak
|| dest
[ ii
+( i
* clk
[ clkCnt
])- tol
]<= low
){
787 } else if ( dest
[ ii
+( i
* clk
[ clkCnt
])+ tol
]>= peak
|| dest
[ ii
+( i
* clk
[ clkCnt
])+ tol
]<= low
){
788 } else { //error no peak detected
792 //if we found no errors then we can stop here
793 // this is correct one - return this clock
794 //PrintAndLog("DEBUG: clk %d, err %d, ii %d, i %d",clk[clkCnt],errCnt,ii,i);
795 if ( errCnt
== 0 && clkCnt
< 6 ) return clk
[ clkCnt
];
796 //if we found errors see if it is lowest so far and save it as best run
797 if ( errCnt
< bestErr
[ clkCnt
]) bestErr
[ clkCnt
]= errCnt
;
803 for ( iii
= 0 ; iii
< 8 ; ++ iii
){
804 if ( bestErr
[ iii
]< bestErr
[ best
]){
805 if ( bestErr
[ iii
]== 0 ) bestErr
[ iii
]= 1 ;
806 // current best bit to error ratio vs new bit to error ratio
807 if ((( size
/ clk
[ best
])/ bestErr
[ best
] < ( size
/ clk
[ iii
])/ bestErr
[ iii
]) ){
816 //detect psk clock by reading #peaks vs no peaks(or errors)
817 int DetectpskNRZClock ( uint8_t dest
[], size_t size
, int clock
)
820 int clk
[]={ 16 , 32 , 40 , 50 , 64 , 100 , 128 , 256 };
821 int loopCnt
= 2048 ; //don't need to loop through entire array...
822 if ( size
< loopCnt
) loopCnt
= size
;
824 //if we already have a valid clock quit
826 if ( clk
[ i
] == clock
) return clock
;
828 //get high and low peak
830 getHiLo ( dest
, loopCnt
, & peak
, & low
, 75 , 75 );
832 //PrintAndLog("DEBUG: peak: %d, low: %d",peak,low);
838 int bestErr
[]={ 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 };
839 int peaksdet
[]={ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
840 //test each valid clock from smallest to greatest to see which lines up
841 for ( clkCnt
= 0 ; clkCnt
< 7 ; ++ clkCnt
){
842 if ( clk
[ clkCnt
] <= 32 ){
847 //try lining up the peaks by moving starting point (try first 256)
848 for ( ii
= 0 ; ii
< loopCnt
; ++ ii
){
849 if (( dest
[ ii
] >= peak
) || ( dest
[ ii
] <= low
)){
852 // now that we have the first one lined up test rest of wave array
853 for ( i
= 0 ; i
< (( int )(( size
- ii
- tol
)/ clk
[ clkCnt
])- 1 ); ++ i
){
854 if ( dest
[ ii
+( i
* clk
[ clkCnt
])]>= peak
|| dest
[ ii
+( i
* clk
[ clkCnt
])]<= low
){
856 } else if ( dest
[ ii
+( i
* clk
[ clkCnt
])- tol
]>= peak
|| dest
[ ii
+( i
* clk
[ clkCnt
])- tol
]<= low
){
858 } else if ( dest
[ ii
+( i
* clk
[ clkCnt
])+ tol
]>= peak
|| dest
[ ii
+( i
* clk
[ clkCnt
])+ tol
]<= low
){
860 } else { //error no peak detected
864 if ( peakcnt
> peaksdet
[ clkCnt
]) {
865 peaksdet
[ clkCnt
]= peakcnt
;
866 bestErr
[ clkCnt
]= errCnt
;
873 //int ratio2; //debug
876 for ( iii
= 0 ; iii
< 7 ; ++ iii
){
878 //ratio2=1000; //debug
879 //bits=size/clk[iii]; //debug
880 if ( peaksdet
[ iii
] > 0 ){
881 ratio
= bestErr
[ iii
]/ peaksdet
[ iii
];
882 if ((( bestErr
[ best
]/ peaksdet
[ best
]) > ( ratio
)+ 1 )){
885 //ratio2=bits/peaksdet[iii]; //debug
887 //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);
892 // by marshmellow (attempt to get rid of high immediately after a low)
893 void pskCleanWave ( uint8_t * BitStream
, size_t size
)
900 getHiLo ( BitStream
, size
, & high
, & low
, 80 , 90 );
902 for ( i
= 0 ; i
< size
; ++ i
){
904 if ( BitStream
[ i
]> low
){
912 } else if ( newHigh
== 1 ){
913 if ( BitStream
[ i
]< high
){
922 if ( BitStream
[ i
] <= low
) newLow
= 1 ;
923 if ( BitStream
[ i
] >= high
) newHigh
= 1 ;
929 // convert psk1 demod to psk2 demod
930 // only transition waves are 1s
931 void psk1TOpsk2 ( uint8_t * BitStream
, size_t size
)
934 uint8_t lastBit
= BitStream
[ 0 ];
936 if ( lastBit
!= BitStream
[ i
]){
937 lastBit
= BitStream
[ i
];
946 // redesigned by marshmellow adjusted from existing decode functions
947 // indala id decoding - only tested on 26 bit tags, but attempted to make it work for more
948 int indala26decode ( uint8_t * bitStream
, size_t * size
, uint8_t * invert
)
950 //26 bit 40134 format (don't know other formats)
952 int long_wait
= 29 ; //29 leading zeros in format
958 // Finding the start of a UID
959 for ( start
= 0 ; start
<= * size
- 250 ; start
++) {
960 first
= bitStream
[ start
];
961 for ( i
= start
; i
< start
+ long_wait
; i
++) {
962 if ( bitStream
[ i
] != first
) {
966 if ( i
== ( start
+ long_wait
)) {
970 if ( start
== * size
- 250 + 1 ) {
971 // did not find start sequence
974 // Inverting signal if needed
976 for ( i
= start
; i
< * size
; i
++) {
977 bitStream
[ i
] = ! bitStream
[ i
];
983 //found start once now test length by finding next one
984 for ( ii
= start
+ 29 ; ii
<= * size
- 250 ; ii
++) {
985 first2
= bitStream
[ ii
];
986 for ( iii
= ii
; iii
< ii
+ long_wait
; iii
++) {
987 if ( bitStream
[ iii
] != first2
) {
991 if ( iii
== ( ii
+ long_wait
)) {
995 if ( ii
== * size
- 250 + 1 ){
996 // did not find second start sequence
1003 for ( ii
= 0 ; ii
< bitCnt
; ii
++) {
1004 bitStream
[ ii
] = bitStream
[ i
++];
1010 // by marshmellow - demodulate PSK1 wave or NRZ wave (both similar enough)
1011 // peaks invert bit (high=1 low=0) each clock cycle = 1 bit determined by last peak
1012 int pskNRZrawDemod ( uint8_t * dest
, size_t * size
, int * clk
, int * invert
)
1014 if ( justNoise ( dest
, * size
)) return - 1 ;
1015 pskCleanWave ( dest
,* size
);
1016 int clk2
= DetectpskNRZClock ( dest
, * size
, * clk
);
1020 ans
= getHiLo ( dest
, 1260 , & high
, & low
, 75 , 80 ); //25% fuzz on high 20% fuzz on low
1021 if ( ans
< 1 ) return - 2 ; //just noise
1022 uint32_t gLen
= * size
;
1023 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
1024 int lastBit
= 0 ; //set first clock check
1025 uint32_t bitnum
= 0 ; //output counter
1026 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
1027 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
1030 uint32_t bestStart
= * size
;
1031 uint32_t maxErr
= (* size
/ 1000 );
1032 uint32_t bestErrCnt
= maxErr
;
1035 uint8_t ignorewin
=* clk
/ 8 ;
1036 //PrintAndLog("DEBUG - lastbit - %d",lastBit);
1037 //loop to find first wave that works - align to clock
1038 for ( iii
= 0 ; iii
< gLen
; ++ iii
){
1039 if (( dest
[ iii
]>= high
) || ( dest
[ iii
]<= low
)){
1041 //loop through to see if this start location works
1042 for ( i
= iii
; i
< * size
; ++ i
) {
1043 //if we found a high bar and we are at a clock bit
1044 if (( dest
[ i
]>= high
) && ( i
>= lastBit
+* clk
- tol
&& i
<= lastBit
+* clk
+ tol
)){
1049 //else if low bar found and we are at a clock point
1050 } else if (( dest
[ i
]<= low
) && ( i
>= lastBit
+* clk
- tol
&& i
<= lastBit
+* clk
+ tol
)){
1055 //else if no bars found
1056 } else if ( dest
[ i
] < high
&& dest
[ i
] > low
) {
1060 //if we are past a clock point
1061 if ( i
>= lastBit
+* clk
+ tol
){ //clock val
1065 //else if bar found but we are not at a clock bit and we did not just have a clock bit
1066 } else if (( dest
[ i
]>= high
|| dest
[ i
]<= low
) && ( i
< lastBit
+* clk
- tol
|| i
> lastBit
+* clk
+ tol
) && ( bitHigh
== 0 )){
1067 //error bar found no clock...
1070 if ( bitnum
>= 1000 ) break ;
1072 //we got more than 64 good bits and not all errors
1073 if (( bitnum
> ( 64 + errCnt
)) && ( errCnt
< ( maxErr
))) {
1074 //possible good read
1077 bestErrCnt
= errCnt
;
1078 break ; //great read - finish
1080 if ( errCnt
< bestErrCnt
){ //set this as new best run
1081 bestErrCnt
= errCnt
;
1087 if ( bestErrCnt
< maxErr
){
1088 //best run is good enough set to best run and set overwrite BinStream
1090 lastBit
= bestStart
-* clk
;
1092 for ( i
= iii
; i
< * size
; ++ i
) {
1093 //if we found a high bar and we are at a clock bit
1094 if (( dest
[ i
] >= high
) && ( i
>= lastBit
+* clk
- tol
&& i
<= lastBit
+* clk
+ tol
)){
1098 dest
[ bitnum
]= curBit
;
1101 //else if low bar found and we are at a clock point
1102 } else if (( dest
[ i
]<= low
) && ( i
>= lastBit
+* clk
- tol
&& i
<= lastBit
+* clk
+ tol
)){
1106 dest
[ bitnum
]= curBit
;
1109 //else if no bars found
1110 } else if ( dest
[ i
]< high
&& dest
[ i
]> low
) {
1114 //if we are past a clock point
1115 if ( i
>= lastBit
+* clk
+ tol
){ //clock val
1117 dest
[ bitnum
]= curBit
;
1120 //else if bar found but we are not at a clock bit and we did not just have a clock bit
1121 } else if (( dest
[ i
]>= high
|| dest
[ i
]<= low
) && (( i
< lastBit
+* clk
- tol
) || ( i
> lastBit
+* clk
+ tol
)) && ( bitHigh
== 0 )){
1122 //error bar found no clock...
1128 if ( bitnum
>= 1000 ) break ;
1144 //detects the bit clock for FSK given the high and low Field Clocks
1145 uint8_t detectFSKClk ( uint8_t * BitStream
, size_t size
, uint8_t fcHigh
, uint8_t fcLow
)
1147 uint8_t clk
[] = { 8 , 16 , 32 , 40 , 50 , 64 , 100 , 128 , 0 };
1148 uint16_t rfLens
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1149 uint8_t rfCnts
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1150 uint8_t rfLensFnd
= 0 ;
1151 uint8_t lastFCcnt
= 0 ;
1152 uint32_t fcCounter
= 0 ;
1153 uint16_t rfCounter
= 0 ;
1154 uint8_t firstBitFnd
= 0 ;
1157 uint8_t fcTol
= ( uint8_t )( 0.5 +( float )( fcHigh
- fcLow
)/ 2 );
1162 //PrintAndLog("DEBUG: fcTol: %d",fcTol);
1163 // prime i to first up transition
1164 for ( i
= 1 ; i
< size
- 1 ; i
++)
1165 if ( BitStream
[ i
] > BitStream
[ i
- 1 ] && BitStream
[ i
]>= BitStream
[ i
+ 1 ])
1168 for (; i
< size
- 1 ; i
++){
1169 if ( BitStream
[ i
] > BitStream
[ i
- 1 ] && BitStream
[ i
]>= BitStream
[ i
+ 1 ]){
1173 // if we got less than the small fc + tolerance then set it to the small fc
1174 if ( fcCounter
< fcLow
+ fcTol
)
1176 else //set it to the large fc
1179 //look for bit clock (rf/xx)
1180 if (( fcCounter
< lastFCcnt
|| fcCounter
> lastFCcnt
)){
1181 //not the same size as the last wave - start of new bit sequence
1183 if ( firstBitFnd
> 1 ){ //skip first wave change - probably not a complete bit
1184 for ( int ii
= 0 ; ii
< 15 ; ii
++){
1185 if ( rfLens
[ ii
]== rfCounter
){
1191 if ( rfCounter
> 0 && rfLensFnd
< 15 ){
1192 //PrintAndLog("DEBUG: rfCntr %d, fcCntr %d",rfCounter,fcCounter);
1193 rfCnts
[ rfLensFnd
]++;
1194 rfLens
[ rfLensFnd
++]= rfCounter
;
1200 lastFCcnt
= fcCounter
;
1209 uint8_t rfHighest
= 15 , rfHighest2
= 15 , rfHighest3
= 15 ;
1211 for ( i
= 0 ; i
< 15 ; i
++){
1212 //PrintAndLog("DEBUG: RF %d, cnts %d",rfLens[i], rfCnts[i]);
1213 //get highest 2 RF values (might need to get more values to compare or compare all?)
1214 if ( rfCnts
[ i
]> rfCnts
[ rfHighest
]){
1215 rfHighest3
= rfHighest2
;
1216 rfHighest2
= rfHighest
;
1218 } else if ( rfCnts
[ i
]> rfCnts
[ rfHighest2
]){
1219 rfHighest3
= rfHighest2
;
1221 } else if ( rfCnts
[ i
]> rfCnts
[ rfHighest3
]){
1225 // set allowed clock remainder tolerance to be 1 large field clock length+1
1226 // we could have mistakenly made a 9 a 10 instead of an 8 or visa versa so rfLens could be 1 FC off
1227 uint8_t tol1
= fcHigh
+ 1 ;
1229 //PrintAndLog("DEBUG: hightest: 1 %d, 2 %d, 3 %d",rfLens[rfHighest],rfLens[rfHighest2],rfLens[rfHighest3]);
1231 // loop to find the highest clock that has a remainder less than the tolerance
1232 // compare samples counted divided by
1234 for (; ii
>= 0 ; ii
--){
1235 if ( rfLens
[ rfHighest
] % clk
[ ii
] < tol1
|| rfLens
[ rfHighest
] % clk
[ ii
] > clk
[ ii
]- tol1
){
1236 if ( rfLens
[ rfHighest2
] % clk
[ ii
] < tol1
|| rfLens
[ rfHighest2
] % clk
[ ii
] > clk
[ ii
]- tol1
){
1237 if ( rfLens
[ rfHighest3
] % clk
[ ii
] < tol1
|| rfLens
[ rfHighest3
] % clk
[ ii
] > clk
[ ii
]- tol1
){
1244 if ( ii
< 0 ) return 0 ; // oops we went too far
1250 //countFC is to detect the field clock lengths.
1251 //counts and returns the 2 most common wave lengths
1252 uint16_t countFC ( uint8_t * BitStream
, size_t size
)
1254 uint8_t fcLens
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1255 uint16_t fcCnts
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1256 uint8_t fcLensFnd
= 0 ;
1257 uint8_t lastFCcnt
= 0 ;
1258 uint32_t fcCounter
= 0 ;
1261 // prime i to first up transition
1262 for ( i
= 1 ; i
< size
- 1 ; i
++)
1263 if ( BitStream
[ i
] > BitStream
[ i
- 1 ] && BitStream
[ i
] >= BitStream
[ i
+ 1 ])
1266 for (; i
< size
- 1 ; i
++){
1267 if ( BitStream
[ i
] > BitStream
[ i
- 1 ] && BitStream
[ i
] >= BitStream
[ i
+ 1 ]){
1268 // new up transition
1271 //if we had 5 and now have 9 then go back to 8 (for when we get a fc 9 instead of an 8)
1272 if ( lastFCcnt
== 5 && fcCounter
== 9 ) fcCounter
--;
1273 //if odd and not rc/5 add one (for when we get a fc 9 instead of 10)
1274 if (( fcCounter
== 9 && fcCounter
& 1 ) || fcCounter
== 4 ) fcCounter
++;
1276 // save last field clock count (fc/xx)
1277 // find which fcLens to save it to:
1278 for ( int ii
= 0 ; ii
< 10 ; ii
++){
1279 if ( fcLens
[ ii
]== fcCounter
){
1285 if ( fcCounter
> 0 && fcLensFnd
< 10 ){
1287 fcCnts
[ fcLensFnd
]++;
1288 fcLens
[ fcLensFnd
++]= fcCounter
;
1297 uint8_t best1
= 9 , best2
= 9 , best3
= 9 ;
1299 // go through fclens and find which ones are bigest 2
1300 for ( i
= 0 ; i
< 10 ; i
++){
1301 // PrintAndLog("DEBUG: FC %d, Cnt %d, Errs %d",fcLens[i],fcCnts[i],errCnt);
1302 // get the 3 best FC values
1303 if ( fcCnts
[ i
]> maxCnt1
) {
1308 } else if ( fcCnts
[ i
]> fcCnts
[ best2
]){
1311 } else if ( fcCnts
[ i
]> fcCnts
[ best3
]){
1315 uint8_t fcH
= 0 , fcL
= 0 ;
1316 if ( fcLens
[ best1
]> fcLens
[ best2
]){
1324 // TODO: take top 3 answers and compare to known Field clocks to get top 2
1326 uint16_t fcs
= ((( uint16_t ) fcH
)<< 8 ) | fcL
;
1327 // PrintAndLog("DEBUG: Best %d best2 %d best3 %d",fcLens[best1],fcLens[best2],fcLens[best3]);