]>
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 //-----------------------------------------------------------------------------
14 uint8_t justNoise ( uint8_t * BitStream
, size_t size
)
16 static const uint8_t THRESHOLD
= 123 ;
17 //test samples are not just noise
18 uint8_t justNoise1
= 1 ;
19 for ( size_t idx
= 0 ; idx
< size
&& justNoise1
; idx
++){
20 justNoise1
= BitStream
[ idx
] < THRESHOLD
;
26 //get high and low values of a wave with passed in fuzz factor. also return noise test = 1 for passed or 0 for only noise
27 int getHiLo ( uint8_t * BitStream
, size_t size
, int * high
, int * low
, uint8_t fuzzHi
, uint8_t fuzzLo
)
31 // get high and low thresholds
32 for ( size_t i
= 0 ; i
< size
; i
++){
33 if ( BitStream
[ i
] > * high
) * high
= BitStream
[ i
];
34 if ( BitStream
[ i
] < * low
) * low
= BitStream
[ i
];
36 if (* high
< 123 ) return - 1 ; // just noise
37 * high
= ((* high
- 128 )* fuzzHi
+ 12800 )/ 100 ;
38 * low
= ((* low
- 128 )* fuzzLo
+ 12800 )/ 100 ;
43 // pass bits to be tested in bits, length bits passed in bitLen, and parity type (even=0 | odd=1) in pType
44 // returns 1 if passed
45 uint8_t parityTest ( uint32_t bits
, uint8_t bitLen
, uint8_t pType
)
48 for ( uint8_t i
= 0 ; i
< bitLen
; i
++){
49 ans
^= (( bits
>> i
) & 1 );
51 //PrintAndLog("DEBUG: ans: %d, ptype: %d",ans,pType);
52 return ( ans
== pType
);
56 //search for given preamble in given BitStream and return success=1 or fail=0 and startIndex and length
57 uint8_t preambleSearch ( uint8_t * BitStream
, uint8_t * preamble
, size_t pLen
, size_t * size
, size_t * startIdx
)
60 for ( int idx
= 0 ; idx
< * size
- pLen
; idx
++){
61 if ( memcmp ( BitStream
+ idx
, preamble
, pLen
) == 0 ){
68 * size
= idx
- * startIdx
;
77 //takes 1s and 0s and searches for EM410x format - output EM ID
78 uint8_t Em410xDecode ( uint8_t * BitStream
, size_t * size
, size_t * startIdx
, uint32_t * hi
, uint64_t * lo
)
80 //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
81 // otherwise could be a void with no arguments
84 if ( BitStream
[ 1 ]> 1 ) return 0 ; //allow only 1s and 0s
86 // 111111111 bit pattern represent start of frame
87 // include 0 in front to help get start pos
88 uint8_t preamble
[] = { 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 };
90 uint32_t parityBits
= 0 ;
94 errChk
= preambleSearch ( BitStream
, preamble
, sizeof ( preamble
), size
, startIdx
);
95 if ( errChk
== 0 || * size
< 64 ) return 0 ;
96 if (* size
> 64 ) FmtLen
= 22 ;
97 * startIdx
+= 1 ; //get rid of 0 from preamble
99 for ( i
= 0 ; i
< FmtLen
; i
++){ //loop through 10 or 22 sets of 5 bits (50-10p = 40 bits or 88 bits)
100 parityBits
= bytebits_to_byte ( BitStream
+( i
* 5 )+ idx
, 5 );
101 //check even parity - quit if failed
102 if ( parityTest ( parityBits
, 5 , 0 ) == 0 ) return 0 ;
103 //set uint64 with ID from BitStream
104 for ( uint8_t ii
= 0 ; ii
< 4 ; ii
++){
105 * hi
= (* hi
<< 1 ) | (* lo
>> 63 );
106 * lo
= (* lo
<< 1 ) | ( BitStream
[( i
* 5 )+ ii
+ idx
]);
109 if ( errChk
!= 0 ) return 1 ;
110 //skip last 5 bit parity test for simplicity.
116 //demodulates strong heavily clipped samples
117 int cleanAskRawDemod ( uint8_t * BinStream
, size_t * size
, int clk
, int invert
, int high
, int low
)
119 size_t bitCnt
= 0 , smplCnt
= 0 , errCnt
= 0 ;
120 uint8_t waveHigh
= 0 ;
121 for ( size_t i
= 0 ; i
< * size
; i
++){
122 if ( BinStream
[ i
] >= high
&& waveHigh
){
124 } else if ( BinStream
[ i
] <= low
&& ! waveHigh
){
126 } else { //transition
127 if (( BinStream
[ i
] >= high
&& ! waveHigh
) || ( BinStream
[ i
] <= low
&& waveHigh
)){
128 if ( smplCnt
> clk
-( clk
/ 4 )- 1 ) { //full clock
129 if ( smplCnt
> clk
+ ( clk
/ 4 )+ 1 ) { //too many samples
131 BinStream
[ bitCnt
++]= 7 ;
132 } else if ( waveHigh
) {
133 BinStream
[ bitCnt
++] = invert
;
134 BinStream
[ bitCnt
++] = invert
;
135 } else if (! waveHigh
) {
136 BinStream
[ bitCnt
++] = invert
^ 1 ;
137 BinStream
[ bitCnt
++] = invert
^ 1 ;
141 } else if ( smplCnt
> ( clk
/ 2 ) - ( clk
/ 4 )- 1 ) {
143 BinStream
[ bitCnt
++] = invert
;
144 } else if (! waveHigh
) {
145 BinStream
[ bitCnt
++] = invert
^ 1 ;
149 } else if (! bitCnt
) {
151 waveHigh
= ( BinStream
[ i
] >= high
);
155 //transition bit oops
157 } else { //haven't hit new high or new low yet
167 void askAmp ( uint8_t * BitStream
, size_t size
)
169 for ( size_t i
= 1 ; i
< size
; i
++){
170 if ( BitStream
[ i
]- BitStream
[ i
- 1 ]>= 30 ) //large jump up
172 else if ( BitStream
[ i
]- BitStream
[ i
- 1 ]<=- 20 ) //large jump down
179 //attempts to demodulate ask modulations, askType == 0 for ask/raw, askType==1 for ask/manchester
180 int askdemod ( uint8_t * BinStream
, size_t * size
, int * clk
, int * invert
, int maxErr
, uint8_t amp
, uint8_t askType
)
182 if (* size
== 0 ) return - 1 ;
183 int start
= DetectASKClock ( BinStream
, * size
, clk
, maxErr
); //clock default
184 if (* clk
== 0 || start
< 0 ) return - 3 ;
185 if (* invert
!= 1 ) * invert
= 0 ;
186 if ( amp
== 1 ) askAmp ( BinStream
, * size
);
188 uint8_t initLoopMax
= 255 ;
189 if ( initLoopMax
> * size
) initLoopMax
= * size
;
190 // Detect high and lows
191 //25% clip in case highs and lows aren't clipped [marshmellow]
193 if ( getHiLo ( BinStream
, initLoopMax
, & high
, & low
, 75 , 75 ) < 1 )
194 return - 2 ; //just noise
197 // if clean clipped waves detected run alternate demod
198 if ( DetectCleanAskWave ( BinStream
, * size
, high
, low
)) {
199 errCnt
= cleanAskRawDemod ( BinStream
, size
, * clk
, * invert
, high
, low
);
200 if ( askType
) //askman
201 return manrawdecode ( BinStream
, size
, 0 );
206 int lastBit
; //set first clock check - can go negative
207 size_t i
, bitnum
= 0 ; //output counter
209 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
210 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
211 size_t MaxBits
= 1024 ;
212 lastBit
= start
- * clk
;
214 for ( i
= start
; i
< * size
; ++ i
) {
215 if ( i
- lastBit
>= * clk
- tol
){
216 if ( BinStream
[ i
] >= high
) {
217 BinStream
[ bitnum
++] = * invert
;
218 } else if ( BinStream
[ i
] <= low
) {
219 BinStream
[ bitnum
++] = * invert
^ 1 ;
220 } else if ( i
- lastBit
>= * clk
+ tol
) {
222 BinStream
[ bitnum
++]= 7 ;
225 } else { //in tolerance - looking for peak
230 } else if ( i
- lastBit
>= (* clk
/ 2 - tol
) && ! midBit
&& ! askType
){
231 if ( BinStream
[ i
] >= high
) {
232 BinStream
[ bitnum
++] = * invert
;
233 } else if ( BinStream
[ i
] <= low
) {
234 BinStream
[ bitnum
++] = * invert
^ 1 ;
235 } else if ( i
- lastBit
>= * clk
/ 2 + tol
) {
236 BinStream
[ bitnum
] = BinStream
[ bitnum
- 1 ];
238 } else { //in tolerance - looking for peak
243 if ( bitnum
>= MaxBits
) break ;
250 //take 10 and 01 and manchester decode
251 //run through 2 times and take least errCnt
252 int manrawdecode ( uint8_t * BitStream
, size_t * size
, uint8_t invert
)
254 uint16_t bitnum
= 0 , MaxBits
= 512 , errCnt
= 0 ;
256 uint16_t bestErr
= 1000 , bestRun
= 0 ;
257 if (* size
< 16 ) return - 1 ;
258 //find correct start position [alignment]
259 for ( ii
= 0 ; ii
< 2 ;++ ii
){
260 for ( i
= ii
; i
<* size
- 3 ; i
+= 2 )
261 if ( BitStream
[ i
]== BitStream
[ i
+ 1 ])
271 for ( i
= bestRun
; i
< * size
- 3 ; i
+= 2 ){
272 if ( BitStream
[ i
] == 1 && ( BitStream
[ i
+ 1 ] == 0 )){
273 BitStream
[ bitnum
++]= invert
;
274 } else if (( BitStream
[ i
] == 0 ) && BitStream
[ i
+ 1 ] == 1 ){
275 BitStream
[ bitnum
++]= invert
^ 1 ;
277 BitStream
[ bitnum
++]= 7 ;
279 if ( bitnum
> MaxBits
) break ;
285 uint32_t manchesterEncode2Bytes ( uint16_t datain
) {
288 for ( uint8_t i
= 0 ; i
< 16 ; i
++) {
289 curBit
= ( datain
>> ( 15 - i
) & 1 );
290 output
|= ( 1 <<((( 15 - i
)* 2 )+ curBit
));
296 //encode binary data into binary manchester
297 int ManchesterEncode ( uint8_t * BitStream
, size_t size
)
299 size_t modIdx
= 20000 , i
= 0 ;
300 if ( size
> modIdx
) return - 1 ;
301 for ( size_t idx
= 0 ; idx
< size
; idx
++){
302 BitStream
[ idx
+ modIdx
++] = BitStream
[ idx
];
303 BitStream
[ idx
+ modIdx
++] = BitStream
[ idx
]^ 1 ;
305 for (; i
<( size
* 2 ); i
++){
306 BitStream
[ i
] = BitStream
[ i
+ 20000 ];
312 //take 01 or 10 = 1 and 11 or 00 = 0
313 //check for phase errors - should never have 111 or 000 should be 01001011 or 10110100 for 1010
314 //decodes biphase or if inverted it is AKA conditional dephase encoding AKA differential manchester encoding
315 int BiphaseRawDecode ( uint8_t * BitStream
, size_t * size
, int offset
, int invert
)
320 uint16_t MaxBits
= 512 ;
321 //if not enough samples - error
322 if (* size
< 51 ) return - 1 ;
323 //check for phase change faults - skip one sample if faulty
324 uint8_t offsetA
= 1 , offsetB
= 1 ;
326 if ( BitStream
[ i
+ 1 ]== BitStream
[ i
+ 2 ]) offsetA
= 0 ;
327 if ( BitStream
[ i
+ 2 ]== BitStream
[ i
+ 3 ]) offsetB
= 0 ;
329 if (! offsetA
&& offsetB
) offset
++;
330 for ( i
= offset
; i
<* size
- 3 ; i
+= 2 ){
331 //check for phase error
332 if ( BitStream
[ i
+ 1 ]== BitStream
[ i
+ 2 ]) {
333 BitStream
[ bitnum
++]= 7 ;
336 if (( BitStream
[ i
]== 1 && BitStream
[ i
+ 1 ]== 0 ) || ( BitStream
[ i
]== 0 && BitStream
[ i
+ 1 ]== 1 )){
337 BitStream
[ bitnum
++]= 1 ^ invert
;
338 } else if (( BitStream
[ i
]== 0 && BitStream
[ i
+ 1 ]== 0 ) || ( BitStream
[ i
]== 1 && BitStream
[ i
+ 1 ]== 1 )){
339 BitStream
[ bitnum
++]= invert
;
341 BitStream
[ bitnum
++]= 7 ;
344 if ( bitnum
> MaxBits
) break ;
351 // demod gProxIIDemod
352 // error returns as -x
353 // success returns start position in BitStream
354 // BitStream must contain previously askrawdemod and biphasedemoded data
355 int gProxII_Demod ( uint8_t BitStream
[], size_t * size
)
358 uint8_t preamble
[] = { 1 , 1 , 1 , 1 , 1 , 0 };
360 uint8_t errChk
= preambleSearch ( BitStream
, preamble
, sizeof ( preamble
), size
, & startIdx
);
361 if ( errChk
== 0 ) return - 3 ; //preamble not found
362 if (* size
!= 96 ) return - 2 ; //should have found 96 bits
363 //check first 6 spacer bits to verify format
364 if (! BitStream
[ startIdx
+ 5 ] && ! BitStream
[ startIdx
+ 10 ] && ! BitStream
[ startIdx
+ 15 ] && ! BitStream
[ startIdx
+ 20 ] && ! BitStream
[ startIdx
+ 25 ] && ! BitStream
[ startIdx
+ 30 ]){
365 //confirmed proper separator bits found
366 //return start position
367 return ( int ) startIdx
;
372 //translate wave to 11111100000 (1 for each short wave 0 for each long wave)
373 size_t fsk_wave_demod ( uint8_t * dest
, size_t size
, uint8_t fchigh
, uint8_t fclow
)
375 size_t last_transition
= 0 ;
378 if ( fchigh
== 0 ) fchigh
= 10 ;
379 if ( fclow
== 0 ) fclow
= 8 ;
380 //set the threshold close to 0 (graph) or 128 std to avoid static
381 uint8_t threshold_value
= 123 ;
382 size_t preLastSample
= 0 ;
383 size_t LastSample
= 0 ;
384 size_t currSample
= 0 ;
385 // sync to first lo-hi transition, and threshold
387 // Need to threshold first sample
389 if ( dest
[ 0 ] < threshold_value
) dest
[ 0 ] = 0 ;
393 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
394 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
395 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
396 for ( idx
= 1 ; idx
< size
; idx
++) {
397 // threshold current value
399 if ( dest
[ idx
] < threshold_value
) dest
[ idx
] = 0 ;
402 // Check for 0->1 transition
403 if ( dest
[ idx
- 1 ] < dest
[ idx
]) { // 0 -> 1 transition
404 preLastSample
= LastSample
;
405 LastSample
= currSample
;
406 currSample
= idx
- last_transition
;
407 if ( currSample
< ( fclow
- 2 )){ //0-5 = garbage noise
408 //do nothing with extra garbage
409 } else if ( currSample
< ( fchigh
- 1 )) { //6-8 = 8 sample waves
410 if ( LastSample
> ( fchigh
- 2 ) && preLastSample
< ( fchigh
- 1 )){
411 dest
[ numBits
- 1 ]= 1 ; //correct last 9 wave surrounded by 8 waves
415 } else if ( currSample
> ( fchigh
+ 1 ) && ! numBits
) { //12 + and first bit = garbage
416 //do nothing with beginning garbage
417 } else if ( currSample
== ( fclow
+ 1 ) && LastSample
== ( fclow
- 1 )) { // had a 7 then a 9 should be two 8's
419 } else { //9+ = 10 sample waves
422 last_transition
= idx
;
425 return numBits
; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
428 //translate 11111100000 to 10
429 size_t aggregate_bits ( uint8_t * dest
, size_t size
, uint8_t rfLen
,
430 uint8_t invert
, uint8_t fchigh
, uint8_t fclow
)
432 uint8_t lastval
= dest
[ 0 ];
436 for ( idx
= 1 ; idx
< size
; idx
++) {
438 if ( dest
[ idx
]== lastval
) continue ;
440 //if lastval was 1, we have a 1->0 crossing
441 if ( dest
[ idx
- 1 ]== 1 ) {
442 if (! numBits
&& n
< rfLen
/ fclow
) {
447 n
= ( n
* fclow
+ rfLen
/ 2 ) / rfLen
;
448 } else { // 0->1 crossing
449 //test first bitsample too small
450 if (! numBits
&& n
< rfLen
/ fchigh
) {
455 n
= ( n
* fchigh
+ rfLen
/ 2 ) / rfLen
;
459 memset ( dest
+ numBits
, dest
[ idx
- 1 ]^ invert
, n
);
464 // if valid extra bits at the end were all the same frequency - add them in
465 if ( n
> rfLen
/ fchigh
) {
466 if ( dest
[ idx
- 2 ]== 1 ) {
467 n
= ( n
* fclow
+ rfLen
/ 2 ) / rfLen
;
469 n
= ( n
* fchigh
+ rfLen
/ 2 ) / rfLen
;
471 memset ( dest
+ numBits
, dest
[ idx
- 1 ]^ invert
, n
);
476 //by marshmellow (from holiman's base)
477 // full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod)
478 int fskdemod ( uint8_t * dest
, size_t size
, uint8_t rfLen
, uint8_t invert
, uint8_t fchigh
, uint8_t fclow
)
481 size
= fsk_wave_demod ( dest
, size
, fchigh
, fclow
);
482 size
= aggregate_bits ( dest
, size
, rfLen
, invert
, fchigh
, fclow
);
486 // loop to get raw HID waveform then FSK demodulate the TAG ID from it
487 int HIDdemodFSK ( uint8_t * dest
, size_t * size
, uint32_t * hi2
, uint32_t * hi
, uint32_t * lo
)
489 if ( justNoise ( dest
, * size
)) return - 1 ;
491 size_t numStart
= 0 , size2
=* size
, startIdx
= 0 ;
493 * size
= fskdemod ( dest
, size2
, 50 , 1 , 10 , 8 ); //fsk2a
494 if (* size
< 96 * 2 ) return - 2 ;
495 // 00011101 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1
496 uint8_t preamble
[] = { 0 , 0 , 0 , 1 , 1 , 1 , 0 , 1 };
497 // find bitstring in array
498 uint8_t errChk
= preambleSearch ( dest
, preamble
, sizeof ( preamble
), size
, & startIdx
);
499 if ( errChk
== 0 ) return - 3 ; //preamble not found
501 numStart
= startIdx
+ sizeof ( preamble
);
502 // final loop, go over previously decoded FSK data and manchester decode into usable tag ID
503 for ( size_t idx
= numStart
; ( idx
- numStart
) < * size
- sizeof ( preamble
); idx
+= 2 ){
504 if ( dest
[ idx
] == dest
[ idx
+ 1 ]){
505 return - 4 ; //not manchester data
507 * hi2
= (* hi2
<< 1 )|(* hi
>> 31 );
508 * hi
= (* hi
<< 1 )|(* lo
>> 31 );
509 //Then, shift in a 0 or one into low
510 if ( dest
[ idx
] && ! dest
[ idx
+ 1 ]) // 1 0
515 return ( int ) startIdx
;
518 // loop to get raw paradox waveform then FSK demodulate the TAG ID from it
519 int ParadoxdemodFSK ( uint8_t * dest
, size_t * size
, uint32_t * hi2
, uint32_t * hi
, uint32_t * lo
)
521 if ( justNoise ( dest
, * size
)) return - 1 ;
523 size_t numStart
= 0 , size2
=* size
, startIdx
= 0 ;
525 * size
= fskdemod ( dest
, size2
, 50 , 1 , 10 , 8 ); //fsk2a
526 if (* size
< 96 ) return - 2 ;
528 // 00001111 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1
529 uint8_t preamble
[] = { 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 };
531 uint8_t errChk
= preambleSearch ( dest
, preamble
, sizeof ( preamble
), size
, & startIdx
);
532 if ( errChk
== 0 ) return - 3 ; //preamble not found
534 numStart
= startIdx
+ sizeof ( preamble
);
535 // final loop, go over previously decoded FSK data and manchester decode into usable tag ID
536 for ( size_t idx
= numStart
; ( idx
- numStart
) < * size
- sizeof ( preamble
); idx
+= 2 ){
537 if ( dest
[ idx
] == dest
[ idx
+ 1 ])
538 return - 4 ; //not manchester data
539 * hi2
= (* hi2
<< 1 )|(* hi
>> 31 );
540 * hi
= (* hi
<< 1 )|(* lo
>> 31 );
541 //Then, shift in a 0 or one into low
542 if ( dest
[ idx
] && ! dest
[ idx
+ 1 ]) // 1 0
547 return ( int ) startIdx
;
550 uint32_t bytebits_to_byte ( uint8_t * src
, size_t numbits
)
553 for ( int i
= 0 ; i
< numbits
; i
++)
555 num
= ( num
<< 1 ) | (* src
);
561 //least significant bit first
562 uint32_t bytebits_to_byteLSBF ( uint8_t * src
, size_t numbits
)
565 for ( int i
= 0 ; i
< numbits
; i
++)
567 num
= ( num
<< 1 ) | *( src
+ ( numbits
-( i
+ 1 )));
572 int IOdemodFSK ( uint8_t * dest
, size_t size
)
574 if ( justNoise ( dest
, size
)) return - 1 ;
575 //make sure buffer has data
576 if ( size
< 66 * 64 ) return - 2 ;
578 size
= fskdemod ( dest
, size
, 64 , 1 , 10 , 8 ); // FSK2a RF/64
579 if ( size
< 65 ) return - 3 ; //did we get a good demod?
581 //0 10 20 30 40 50 60
583 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
584 //-----------------------------------------------------------------------------
585 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
587 //XSF(version)facility:codeone+codetwo
590 uint8_t preamble
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 };
591 uint8_t errChk
= preambleSearch ( dest
, preamble
, sizeof ( preamble
), & size
, & startIdx
);
592 if ( errChk
== 0 ) return - 4 ; //preamble not found
594 if (! dest
[ startIdx
+ 8 ] && dest
[ startIdx
+ 17 ]== 1 && dest
[ startIdx
+ 26 ]== 1 && dest
[ startIdx
+ 35 ]== 1 && dest
[ startIdx
+ 44 ]== 1 && dest
[ startIdx
+ 53 ]== 1 ){
595 //confirmed proper separator bits found
596 //return start position
597 return ( int ) startIdx
;
603 // find viking preamble 0xF200 in already demoded data
604 int VikingDemod_AM ( uint8_t * dest
, size_t * size
) {
605 if ( justNoise ( dest
, * size
)) return - 1 ;
606 //make sure buffer has data
607 if (* size
< 64 * 2 ) return - 2 ;
610 uint8_t preamble
[] = { 1 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
611 uint8_t errChk
= preambleSearch ( dest
, preamble
, sizeof ( preamble
), size
, & startIdx
);
612 if ( errChk
== 0 ) return - 4 ; //preamble not found
614 if (* size
!= 64 ) return - 5 ;
615 //return start position
616 return ( int ) startIdx
;
620 // takes a array of binary values, start position, length of bits per parity (includes parity bit),
621 // Parity Type (1 for odd; 0 for even; 2 Always 1's), and binary Length (length to run)
622 size_t removeParity ( uint8_t * BitStream
, size_t startIdx
, uint8_t pLen
, uint8_t pType
, size_t bLen
)
624 uint32_t parityWd
= 0 ;
625 size_t j
= 0 , bitCnt
= 0 ;
626 for ( int word
= 0 ; word
< ( bLen
); word
+= pLen
){
627 for ( int bit
= 0 ; bit
< pLen
; bit
++){
628 parityWd
= ( parityWd
<< 1 ) | BitStream
[ startIdx
+ word
+ bit
];
629 BitStream
[ j
++] = ( BitStream
[ startIdx
+ word
+ bit
]);
631 j
--; // overwrite parity with next data
632 // if parity fails then return 0
633 if ( pType
== 2 ) { // then marker bit which should be a 1
634 if (! BitStream
[ j
]) return 0 ;
636 if ( parityTest ( parityWd
, pLen
, pType
) == 0 ) return 0 ;
641 // if we got here then all the parities passed
642 //return ID start index and size
646 // Ask/Biphase Demod then try to locate an ISO 11784/85 ID
647 // BitStream must contain previously askrawdemod and biphasedemoded data
648 int FDXBdemodBI ( uint8_t * dest
, size_t * size
)
650 //make sure buffer has enough data
651 if (* size
< 128 ) return - 1 ;
654 uint8_t preamble
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 };
656 uint8_t errChk
= preambleSearch ( dest
, preamble
, sizeof ( preamble
), size
, & startIdx
);
657 if ( errChk
== 0 ) return - 2 ; //preamble not found
658 return ( int ) startIdx
;
662 // FSK Demod then try to locate an AWID ID
663 int AWIDdemodFSK ( uint8_t * dest
, size_t * size
)
665 //make sure buffer has enough data
666 if (* size
< 96 * 50 ) return - 1 ;
668 if ( justNoise ( dest
, * size
)) return - 2 ;
671 * size
= fskdemod ( dest
, * size
, 50 , 1 , 10 , 8 ); // fsk2a RF/50
672 if (* size
< 96 ) return - 3 ; //did we get a good demod?
674 uint8_t preamble
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 };
676 uint8_t errChk
= preambleSearch ( dest
, preamble
, sizeof ( preamble
), size
, & startIdx
);
677 if ( errChk
== 0 ) return - 4 ; //preamble not found
678 if (* size
!= 96 ) return - 5 ;
679 return ( int ) startIdx
;
683 // FSK Demod then try to locate an Farpointe Data (pyramid) ID
684 int PyramiddemodFSK ( uint8_t * dest
, size_t * size
)
686 //make sure buffer has data
687 if (* size
< 128 * 50 ) return - 5 ;
689 //test samples are not just noise
690 if ( justNoise ( dest
, * size
)) return - 1 ;
693 * size
= fskdemod ( dest
, * size
, 50 , 1 , 10 , 8 ); // fsk2a RF/50
694 if (* size
< 128 ) return - 2 ; //did we get a good demod?
696 uint8_t preamble
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 };
698 uint8_t errChk
= preambleSearch ( dest
, preamble
, sizeof ( preamble
), size
, & startIdx
);
699 if ( errChk
== 0 ) return - 4 ; //preamble not found
700 if (* size
!= 128 ) return - 3 ;
701 return ( int ) startIdx
;
705 // to detect a wave that has heavily clipped (clean) samples
706 uint8_t DetectCleanAskWave ( uint8_t dest
[], size_t size
, uint8_t high
, uint8_t low
)
710 size_t loopEnd
= 512 + 60 ;
711 if ( loopEnd
> size
) loopEnd
= size
;
712 for ( size_t i
= 60 ; i
< loopEnd
; i
++){
713 if ( dest
[ i
]> low
&& dest
[ i
]< high
)
719 if ( cntPeaks
> 300 ) return 1 ;
725 // to help detect clocks on heavily clipped samples
726 // based on count of low to low
727 int DetectStrongAskClock ( uint8_t dest
[], size_t size
, uint8_t high
, uint8_t low
)
729 uint8_t fndClk
[] = { 8 , 16 , 32 , 40 , 50 , 64 , 128 };
733 // get to first full low to prime loop and skip incomplete first pulse
734 while (( dest
[ i
] < high
) && ( i
< size
))
736 while (( dest
[ i
] > low
) && ( i
< size
))
739 // loop through all samples
741 // measure from low to low
742 while (( dest
[ i
] > low
) && ( i
< size
))
745 while (( dest
[ i
] < high
) && ( i
< size
))
747 while (( dest
[ i
] > low
) && ( i
< size
))
749 //get minimum measured distance
750 if ( i
- startwave
< minClk
&& i
< size
)
751 minClk
= i
- startwave
;
754 for ( uint8_t clkCnt
= 0 ; clkCnt
< 7 ; clkCnt
++) {
755 if ( minClk
>= fndClk
[ clkCnt
]-( fndClk
[ clkCnt
]/ 8 ) && minClk
<= fndClk
[ clkCnt
]+ 1 )
756 return fndClk
[ clkCnt
];
762 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
763 // maybe somehow adjust peak trimming value based on samples to fix?
764 // return start index of best starting position for that clock and return clock (by reference)
765 int DetectASKClock ( uint8_t dest
[], size_t size
, int * clock
, int maxErr
)
768 uint8_t clk
[] = { 255 , 8 , 16 , 32 , 40 , 50 , 64 , 100 , 128 , 255 };
770 uint8_t loopCnt
= 255 ; //don't need to loop through entire array...
771 if ( size
<= loopCnt
) return - 1 ; //not enough samples
773 //if we already have a valid clock
776 if ( clk
[ i
] == * clock
) clockFnd
= i
;
777 //clock found but continue to find best startpos
779 //get high and low peak
781 if ( getHiLo ( dest
, loopCnt
, & peak
, & low
, 75 , 75 ) < 1 ) return - 1 ;
783 //test for large clean peaks
785 if ( DetectCleanAskWave ( dest
, size
, peak
, low
)== 1 ){
786 int ans
= DetectStrongAskClock ( dest
, size
, peak
, low
);
787 for ( i
= clkEnd
- 1 ; i
> 0 ; i
--){
791 return 0 ; // for strong waves i don't use the 'best start position' yet...
792 //break; //clock found but continue to find best startpos [not yet]
799 uint8_t clkCnt
, tol
= 0 ;
800 uint16_t bestErr
[]={ 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 };
801 uint8_t bestStart
[]={ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
803 size_t arrLoc
, loopEnd
;
811 //test each valid clock from smallest to greatest to see which lines up
812 for (; clkCnt
< clkEnd
; clkCnt
++){
813 if ( clk
[ clkCnt
] <= 32 ){
818 //if no errors allowed - keep start within the first clock
819 if (! maxErr
&& size
> clk
[ clkCnt
]* 2 + tol
&& clk
[ clkCnt
]< 128 ) loopCnt
= clk
[ clkCnt
]* 2 ;
820 bestErr
[ clkCnt
]= 1000 ;
821 //try lining up the peaks by moving starting point (try first few clocks)
822 for ( ii
= 0 ; ii
< loopCnt
; ii
++){
823 if ( dest
[ ii
] < peak
&& dest
[ ii
] > low
) continue ;
826 // now that we have the first one lined up test rest of wave array
827 loopEnd
= (( size
- ii
- tol
) / clk
[ clkCnt
]) - 1 ;
828 for ( i
= 0 ; i
< loopEnd
; ++ i
){
829 arrLoc
= ii
+ ( i
* clk
[ clkCnt
]);
830 if ( dest
[ arrLoc
] >= peak
|| dest
[ arrLoc
] <= low
){
831 } else if ( dest
[ arrLoc
- tol
] >= peak
|| dest
[ arrLoc
- tol
] <= low
){
832 } else if ( dest
[ arrLoc
+ tol
] >= peak
|| dest
[ arrLoc
+ tol
] <= low
){
833 } else { //error no peak detected
837 //if we found no errors then we can stop here and a low clock (common clocks)
838 // this is correct one - return this clock
839 //PrintAndLog("DEBUG: clk %d, err %d, ii %d, i %d",clk[clkCnt],errCnt,ii,i);
840 if ( errCnt
== 0 && clkCnt
< 7 ) {
841 if (! clockFnd
) * clock
= clk
[ clkCnt
];
844 //if we found errors see if it is lowest so far and save it as best run
845 if ( errCnt
< bestErr
[ clkCnt
]){
846 bestErr
[ clkCnt
]= errCnt
;
847 bestStart
[ clkCnt
]= ii
;
853 for ( iii
= 1 ; iii
< clkEnd
; ++ iii
){
854 if ( bestErr
[ iii
] < bestErr
[ best
]){
855 if ( bestErr
[ iii
] == 0 ) bestErr
[ iii
]= 1 ;
856 // current best bit to error ratio vs new bit to error ratio
857 if ( ( size
/ clk
[ best
])/ bestErr
[ best
] < ( size
/ clk
[ iii
])/ bestErr
[ iii
] ){
862 //if (bestErr[best] > maxErr) return -1;
863 if (! clockFnd
) * clock
= clk
[ best
];
864 return bestStart
[ best
];
868 //detect psk clock by reading each phase shift
869 // a phase shift is determined by measuring the sample length of each wave
870 int DetectPSKClock ( uint8_t dest
[], size_t size
, int clock
)
872 uint8_t clk
[]={ 255 , 16 , 32 , 40 , 50 , 64 , 100 , 128 , 255 }; //255 is not a valid clock
873 uint16_t loopCnt
= 4096 ; //don't need to loop through entire array...
874 if ( size
== 0 ) return 0 ;
875 if ( size
< loopCnt
) loopCnt
= size
;
877 //if we already have a valid clock quit
880 if ( clk
[ i
] == clock
) return clock
;
882 size_t waveStart
= 0 , waveEnd
= 0 , firstFullWave
= 0 , lastClkBit
= 0 ;
883 uint8_t clkCnt
, fc
= 0 , fullWaveLen
= 0 , tol
= 1 ;
884 uint16_t peakcnt
= 0 , errCnt
= 0 , waveLenCnt
= 0 ;
885 uint16_t bestErr
[]={ 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 };
886 uint16_t peaksdet
[]={ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
887 fc
= countFC ( dest
, size
, 0 );
888 if ( fc
!= 2 && fc
!= 4 && fc
!= 8 ) return - 1 ;
889 //PrintAndLog("DEBUG: FC: %d",fc);
891 //find first full wave
892 for ( i
= 0 ; i
< loopCnt
; i
++){
893 if ( dest
[ i
] < dest
[ i
+ 1 ] && dest
[ i
+ 1 ] >= dest
[ i
+ 2 ]){
894 if ( waveStart
== 0 ) {
896 //PrintAndLog("DEBUG: waveStart: %d",waveStart);
899 //PrintAndLog("DEBUG: waveEnd: %d",waveEnd);
900 waveLenCnt
= waveEnd
- waveStart
;
901 if ( waveLenCnt
> fc
){
902 firstFullWave
= waveStart
;
903 fullWaveLen
= waveLenCnt
;
910 //PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);
912 //test each valid clock from greatest to smallest to see which lines up
913 for ( clkCnt
= 7 ; clkCnt
>= 1 ; clkCnt
--){
914 lastClkBit
= firstFullWave
; //set end of wave as clock align
918 //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d",clk[clkCnt],lastClkBit);
920 for ( i
= firstFullWave
+ fullWaveLen
- 1 ; i
< loopCnt
- 2 ; i
++){
921 //top edge of wave = start of new wave
922 if ( dest
[ i
] < dest
[ i
+ 1 ] && dest
[ i
+ 1 ] >= dest
[ i
+ 2 ]){
923 if ( waveStart
== 0 ) {
928 waveLenCnt
= waveEnd
- waveStart
;
929 if ( waveLenCnt
> fc
){
930 //if this wave is a phase shift
931 //PrintAndLog("DEBUG: phase shift at: %d, len: %d, nextClk: %d, ii: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+clk[clkCnt]-tol,ii+1,fc);
932 if ( i
+ 1 >= lastClkBit
+ clk
[ clkCnt
] - tol
){ //should be a clock bit
934 lastClkBit
+= clk
[ clkCnt
];
935 } else if ( i
< lastClkBit
+ 8 ){
936 //noise after a phase shift - ignore
937 } else { //phase shift before supposed to based on clock
940 } else if ( i
+ 1 > lastClkBit
+ clk
[ clkCnt
] + tol
+ fc
){
941 lastClkBit
+= clk
[ clkCnt
]; //no phase shift but clock bit
950 if ( errCnt
<= bestErr
[ clkCnt
]) bestErr
[ clkCnt
]= errCnt
;
951 if ( peakcnt
> peaksdet
[ clkCnt
]) peaksdet
[ clkCnt
]= peakcnt
;
953 //all tested with errors
954 //return the highest clk with the most peaks found
956 for ( i
= 7 ; i
>= 1 ; i
--){
957 if ( peaksdet
[ i
] > peaksdet
[ best
]) {
960 //PrintAndLog("DEBUG: Clk: %d, peaks: %d, errs: %d, bestClk: %d",clk[iii],peaksdet[iii],bestErr[iii],clk[best]);
966 //detect nrz clock by reading #peaks vs no peaks(or errors)
967 int DetectNRZClock ( uint8_t dest
[], size_t size
, int clock
)
970 uint8_t clk
[]={ 8 , 16 , 32 , 40 , 50 , 64 , 100 , 128 , 255 };
971 size_t loopCnt
= 4096 ; //don't need to loop through entire array...
972 if ( size
== 0 ) return 0 ;
973 if ( size
< loopCnt
) loopCnt
= size
;
975 //if we already have a valid clock quit
977 if ( clk
[ i
] == clock
) return clock
;
979 //get high and low peak
981 if ( getHiLo ( dest
, loopCnt
, & peak
, & low
, 75 , 75 ) < 1 ) return 0 ;
983 //PrintAndLog("DEBUG: peak: %d, low: %d",peak,low);
988 uint16_t peaksdet
[]={ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
990 //test for large clipped waves
991 for ( i
= 0 ; i
< loopCnt
; i
++){
992 if ( dest
[ i
] >= peak
|| dest
[ i
] <= low
){
995 if ( peakcnt
> 0 && maxPeak
< peakcnt
){
1002 //test each valid clock from smallest to greatest to see which lines up
1003 for ( clkCnt
= 0 ; clkCnt
< 8 ; ++ clkCnt
){
1004 //ignore clocks smaller than largest peak
1005 if ( clk
[ clkCnt
]< maxPeak
) continue ;
1007 //try lining up the peaks by moving starting point (try first 256)
1008 for ( ii
= 0 ; ii
< loopCnt
; ++ ii
){
1009 if (( dest
[ ii
] >= peak
) || ( dest
[ ii
] <= low
)){
1011 // now that we have the first one lined up test rest of wave array
1012 for ( i
= 0 ; i
< (( int )(( size
- ii
- tol
)/ clk
[ clkCnt
])- 1 ); ++ i
){
1013 if ( dest
[ ii
+( i
* clk
[ clkCnt
])]>= peak
|| dest
[ ii
+( i
* clk
[ clkCnt
])]<= low
){
1017 if ( peakcnt
> peaksdet
[ clkCnt
]) {
1018 peaksdet
[ clkCnt
]= peakcnt
;
1025 for ( iii
= 7 ; iii
> 0 ; iii
--){
1026 if ( peaksdet
[ iii
] > peaksdet
[ best
]){
1029 //PrintAndLog("DEBUG: Clk: %d, peaks: %d, errs: %d, bestClk: %d",clk[iii],peaksdet[iii],bestErr[iii],clk[best]);
1035 // convert psk1 demod to psk2 demod
1036 // only transition waves are 1s
1037 void psk1TOpsk2 ( uint8_t * BitStream
, size_t size
)
1040 uint8_t lastBit
= BitStream
[ 0 ];
1041 for (; i
< size
; i
++){
1042 if ( BitStream
[ i
]== 7 ){
1044 } else if ( lastBit
!= BitStream
[ i
]){
1045 lastBit
= BitStream
[ i
];
1055 // convert psk2 demod to psk1 demod
1056 // from only transition waves are 1s to phase shifts change bit
1057 void psk2TOpsk1 ( uint8_t * BitStream
, size_t size
)
1060 for ( size_t i
= 0 ; i
< size
; i
++){
1061 if ( BitStream
[ i
]== 1 ){
1069 // redesigned by marshmellow adjusted from existing decode functions
1070 // indala id decoding - only tested on 26 bit tags, but attempted to make it work for more
1071 int indala26decode ( uint8_t * bitStream
, size_t * size
, uint8_t * invert
)
1073 //26 bit 40134 format (don't know other formats)
1075 int long_wait
= 29 ; //29 leading zeros in format
1081 // Finding the start of a UID
1082 for ( start
= 0 ; start
<= * size
- 250 ; start
++) {
1083 first
= bitStream
[ start
];
1084 for ( i
= start
; i
< start
+ long_wait
; i
++) {
1085 if ( bitStream
[ i
] != first
) {
1089 if ( i
== ( start
+ long_wait
)) {
1093 if ( start
== * size
- 250 + 1 ) {
1094 // did not find start sequence
1097 // Inverting signal if needed
1099 for ( i
= start
; i
< * size
; i
++) {
1100 bitStream
[ i
] = ! bitStream
[ i
];
1106 //found start once now test length by finding next one
1107 for ( ii
= start
+ 29 ; ii
<= * size
- 250 ; ii
++) {
1108 first2
= bitStream
[ ii
];
1109 for ( iii
= ii
; iii
< ii
+ long_wait
; iii
++) {
1110 if ( bitStream
[ iii
] != first2
) {
1114 if ( iii
== ( ii
+ long_wait
)) {
1118 if ( ii
== * size
- 250 + 1 ){
1119 // did not find second start sequence
1126 for ( ii
= 0 ; ii
< bitCnt
; ii
++) {
1127 bitStream
[ ii
] = bitStream
[ i
++];
1133 // by marshmellow - demodulate NRZ wave (both similar enough)
1134 // peaks invert bit (high=1 low=0) each clock cycle = 1 bit determined by last peak
1135 // there probably is a much simpler way to do this....
1136 int nrzRawDemod ( uint8_t * dest
, size_t * size
, int * clk
, int * invert
, int maxErr
)
1138 if ( justNoise ( dest
, * size
)) return - 1 ;
1139 * clk
= DetectNRZClock ( dest
, * size
, * clk
);
1140 if (* clk
== 0 ) return - 2 ;
1141 size_t i
, gLen
= 4096 ;
1142 if ( gLen
>* size
) gLen
= * size
;
1144 if ( getHiLo ( dest
, gLen
, & high
, & low
, 75 , 75 ) < 1 ) return - 3 ; //25% fuzz on high 25% fuzz on low
1145 int lastBit
= 0 ; //set first clock check
1146 size_t iii
= 0 , bitnum
= 0 ; //bitnum counter
1147 uint16_t errCnt
= 0 , MaxBits
= 1000 ;
1148 size_t bestErrCnt
= maxErr
+ 1 ;
1149 size_t bestPeakCnt
= 0 , bestPeakStart
= 0 ;
1150 uint8_t bestFirstPeakHigh
= 0 , firstPeakHigh
= 0 , curBit
= 0 , bitHigh
= 0 , errBitHigh
= 0 ;
1151 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
1153 uint8_t ignoreWindow
= 4 ;
1154 uint8_t ignoreCnt
= ignoreWindow
; //in case of noise near peak
1155 //loop to find first wave that works - align to clock
1156 for ( iii
= 0 ; iii
< gLen
; ++ iii
){
1157 if (( dest
[ iii
]>= high
) || ( dest
[ iii
]<= low
)){
1158 if ( dest
[ iii
]>= high
) firstPeakHigh
= 1 ;
1159 else firstPeakHigh
= 0 ;
1163 //loop through to see if this start location works
1164 for ( i
= iii
; i
< * size
; ++ i
) {
1165 // if we are at a clock bit
1166 if (( i
>= lastBit
+ * clk
- tol
) && ( i
<= lastBit
+ * clk
+ tol
)) {
1168 if ( dest
[ i
] >= high
|| dest
[ i
] <= low
) {
1172 ignoreCnt
= ignoreWindow
;
1174 } else if ( i
== lastBit
+ * clk
+ tol
) {
1177 //else if no bars found
1178 } else if ( dest
[ i
] < high
&& dest
[ i
] > low
){
1181 if ( errBitHigh
== 1 ) errCnt
++;
1186 } else if (( dest
[ i
]>= high
|| dest
[ i
]<= low
) && ( bitHigh
== 0 )) {
1187 //error bar found no clock...
1190 if ((( i
- iii
) / * clk
)>= MaxBits
) break ;
1192 //we got more than 64 good bits and not all errors
1193 if ((( i
- iii
) / * clk
) > 64 && ( errCnt
<= ( maxErr
))) {
1194 //possible good read
1195 if (! errCnt
|| peakCnt
> bestPeakCnt
){
1196 bestFirstPeakHigh
= firstPeakHigh
;
1197 bestErrCnt
= errCnt
;
1198 bestPeakCnt
= peakCnt
;
1199 bestPeakStart
= iii
;
1200 if (! errCnt
) break ; //great read - finish
1205 //PrintAndLog("DEBUG: bestErrCnt: %d, maxErr: %d, bestStart: %d, bestPeakCnt: %d, bestPeakStart: %d",bestErrCnt,maxErr,bestStart,bestPeakCnt,bestPeakStart);
1206 if ( bestErrCnt
> maxErr
) return bestErrCnt
;
1208 //best run is good enough set to best run and set overwrite BinStream
1209 lastBit
= bestPeakStart
- * clk
;
1210 memset ( dest
, bestFirstPeakHigh
^ 1 , bestPeakStart
/ * clk
);
1211 bitnum
+= ( bestPeakStart
/ * clk
);
1212 for ( i
= bestPeakStart
; i
< * size
; ++ i
) {
1213 // if expecting a clock bit
1214 if (( i
>= lastBit
+ * clk
- tol
) && ( i
<= lastBit
+ * clk
+ tol
)) {
1216 if ( dest
[ i
] >= high
|| dest
[ i
] <= low
) {
1220 ignoreCnt
= ignoreWindow
;
1222 if ( dest
[ i
] >= high
) curBit
^= 1 ;
1223 dest
[ bitnum
++] = curBit
;
1225 //else no bars found in clock area
1226 } else if ( i
== lastBit
+ * clk
+ tol
) {
1227 dest
[ bitnum
++] = curBit
;
1230 //else if no bars found
1231 } else if ( dest
[ i
] < high
&& dest
[ i
] > low
){
1232 if ( ignoreCnt
== 0 ){
1234 if ( errBitHigh
== 1 ){
1242 } else if (( dest
[ i
] >= high
|| dest
[ i
] <= low
) && ( bitHigh
== 0 )) {
1243 //error bar found no clock...
1246 if ( bitnum
>= MaxBits
) break ;
1253 //detects the bit clock for FSK given the high and low Field Clocks
1254 uint8_t detectFSKClk ( uint8_t * BitStream
, size_t size
, uint8_t fcHigh
, uint8_t fcLow
)
1256 uint8_t clk
[] = { 8 , 16 , 32 , 40 , 50 , 64 , 100 , 128 , 0 };
1257 uint16_t rfLens
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1258 uint8_t rfCnts
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1259 uint8_t rfLensFnd
= 0 ;
1260 uint8_t lastFCcnt
= 0 ;
1261 uint16_t fcCounter
= 0 ;
1262 uint16_t rfCounter
= 0 ;
1263 uint8_t firstBitFnd
= 0 ;
1265 if ( size
== 0 ) return 0 ;
1267 uint8_t fcTol
= ( uint8_t )( 0.5 +( float )( fcHigh
- fcLow
)/ 2 );
1272 //PrintAndLog("DEBUG: fcTol: %d",fcTol);
1273 // prime i to first up transition
1274 for ( i
= 1 ; i
< size
- 1 ; i
++)
1275 if ( BitStream
[ i
] > BitStream
[ i
- 1 ] && BitStream
[ i
]>= BitStream
[ i
+ 1 ])
1278 for (; i
< size
- 1 ; i
++){
1282 if ( BitStream
[ i
] <= BitStream
[ i
- 1 ] || BitStream
[ i
] < BitStream
[ i
+ 1 ])
1285 // if we got less than the small fc + tolerance then set it to the small fc
1286 if ( fcCounter
< fcLow
+ fcTol
)
1288 else //set it to the large fc
1291 //look for bit clock (rf/xx)
1292 if (( fcCounter
< lastFCcnt
|| fcCounter
> lastFCcnt
)){
1293 //not the same size as the last wave - start of new bit sequence
1294 if ( firstBitFnd
> 1 ){ //skip first wave change - probably not a complete bit
1295 for ( int ii
= 0 ; ii
< 15 ; ii
++){
1296 if ( rfLens
[ ii
] == rfCounter
){
1302 if ( rfCounter
> 0 && rfLensFnd
< 15 ){
1303 //PrintAndLog("DEBUG: rfCntr %d, fcCntr %d",rfCounter,fcCounter);
1304 rfCnts
[ rfLensFnd
]++;
1305 rfLens
[ rfLensFnd
++] = rfCounter
;
1311 lastFCcnt
= fcCounter
;
1315 uint8_t rfHighest
= 15 , rfHighest2
= 15 , rfHighest3
= 15 ;
1317 for ( i
= 0 ; i
< 15 ; i
++){
1318 //PrintAndLog("DEBUG: RF %d, cnts %d",rfLens[i], rfCnts[i]);
1319 //get highest 2 RF values (might need to get more values to compare or compare all?)
1320 if ( rfCnts
[ i
]> rfCnts
[ rfHighest
]){
1321 rfHighest3
= rfHighest2
;
1322 rfHighest2
= rfHighest
;
1324 } else if ( rfCnts
[ i
]> rfCnts
[ rfHighest2
]){
1325 rfHighest3
= rfHighest2
;
1327 } else if ( rfCnts
[ i
]> rfCnts
[ rfHighest3
]){
1331 // set allowed clock remainder tolerance to be 1 large field clock length+1
1332 // we could have mistakenly made a 9 a 10 instead of an 8 or visa versa so rfLens could be 1 FC off
1333 uint8_t tol1
= fcHigh
+ 1 ;
1335 //PrintAndLog("DEBUG: hightest: 1 %d, 2 %d, 3 %d",rfLens[rfHighest],rfLens[rfHighest2],rfLens[rfHighest3]);
1337 // loop to find the highest clock that has a remainder less than the tolerance
1338 // compare samples counted divided by
1340 for (; ii
>= 0 ; ii
--){
1341 if ( rfLens
[ rfHighest
] % clk
[ ii
] < tol1
|| rfLens
[ rfHighest
] % clk
[ ii
] > clk
[ ii
]- tol1
){
1342 if ( rfLens
[ rfHighest2
] % clk
[ ii
] < tol1
|| rfLens
[ rfHighest2
] % clk
[ ii
] > clk
[ ii
]- tol1
){
1343 if ( rfLens
[ rfHighest3
] % clk
[ ii
] < tol1
|| rfLens
[ rfHighest3
] % clk
[ ii
] > clk
[ ii
]- tol1
){
1350 if ( ii
< 0 ) return 0 ; // oops we went too far
1356 //countFC is to detect the field clock lengths.
1357 //counts and returns the 2 most common wave lengths
1358 //mainly used for FSK field clock detection
1359 uint16_t countFC ( uint8_t * BitStream
, size_t size
, uint8_t fskAdj
)
1361 uint8_t fcLens
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1362 uint16_t fcCnts
[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1363 uint8_t fcLensFnd
= 0 ;
1364 uint8_t lastFCcnt
= 0 ;
1365 uint8_t fcCounter
= 0 ;
1367 if ( size
== 0 ) return 0 ;
1369 // prime i to first up transition
1370 for ( i
= 1 ; i
< size
- 1 ; i
++)
1371 if ( BitStream
[ i
] > BitStream
[ i
- 1 ] && BitStream
[ i
] >= BitStream
[ i
+ 1 ])
1374 for (; i
< size
- 1 ; i
++){
1375 if ( BitStream
[ i
] > BitStream
[ i
- 1 ] && BitStream
[ i
] >= BitStream
[ i
+ 1 ]){
1376 // new up transition
1379 //if we had 5 and now have 9 then go back to 8 (for when we get a fc 9 instead of an 8)
1380 if ( lastFCcnt
== 5 && fcCounter
== 9 ) fcCounter
--;
1381 //if fc=9 or 4 add one (for when we get a fc 9 instead of 10 or a 4 instead of a 5)
1382 if (( fcCounter
== 9 ) || fcCounter
== 4 ) fcCounter
++;
1383 // save last field clock count (fc/xx)
1384 lastFCcnt
= fcCounter
;
1386 // find which fcLens to save it to:
1387 for ( int ii
= 0 ; ii
< 10 ; ii
++){
1388 if ( fcLens
[ ii
]== fcCounter
){
1394 if ( fcCounter
> 0 && fcLensFnd
< 10 ){
1396 fcCnts
[ fcLensFnd
]++;
1397 fcLens
[ fcLensFnd
++]= fcCounter
;
1406 uint8_t best1
= 9 , best2
= 9 , best3
= 9 ;
1408 // go through fclens and find which ones are bigest 2
1409 for ( i
= 0 ; i
< 10 ; i
++){
1410 // PrintAndLog("DEBUG: FC %d, Cnt %d, Errs %d",fcLens[i],fcCnts[i],errCnt);
1411 // get the 3 best FC values
1412 if ( fcCnts
[ i
]> maxCnt1
) {
1417 } else if ( fcCnts
[ i
]> fcCnts
[ best2
]){
1420 } else if ( fcCnts
[ i
]> fcCnts
[ best3
]){
1424 uint8_t fcH
= 0 , fcL
= 0 ;
1425 if ( fcLens
[ best1
]> fcLens
[ best2
]){
1433 // TODO: take top 3 answers and compare to known Field clocks to get top 2
1435 uint16_t fcs
= ((( uint16_t ) fcH
)<< 8 ) | fcL
;
1436 // PrintAndLog("DEBUG: Best %d best2 %d best3 %d",fcLens[best1],fcLens[best2],fcLens[best3]);
1437 if ( fskAdj
) return fcs
;
1438 return fcLens
[ best1
];
1441 //by marshmellow - demodulate PSK1 wave
1442 //uses wave lengths (# Samples)
1443 int pskRawDemod ( uint8_t dest
[], size_t * size
, int * clock
, int * invert
)
1445 if ( size
== 0 ) return - 1 ;
1446 uint16_t loopCnt
= 4096 ; //don't need to loop through entire array...
1447 if (* size
< loopCnt
) loopCnt
= * size
;
1449 uint8_t curPhase
= * invert
;
1450 size_t i
, waveStart
= 1 , waveEnd
= 0 , firstFullWave
= 0 , lastClkBit
= 0 ;
1451 uint8_t fc
= 0 , fullWaveLen
= 0 , tol
= 1 ;
1452 uint16_t errCnt
= 0 , waveLenCnt
= 0 ;
1453 fc
= countFC ( dest
, * size
, 0 );
1454 if ( fc
!= 2 && fc
!= 4 && fc
!= 8 ) return - 1 ;
1455 //PrintAndLog("DEBUG: FC: %d",fc);
1456 * clock
= DetectPSKClock ( dest
, * size
, * clock
);
1457 if (* clock
== 0 ) return - 1 ;
1458 int avgWaveVal
= 0 , lastAvgWaveVal
= 0 ;
1459 //find first phase shift
1460 for ( i
= 0 ; i
< loopCnt
; i
++){
1461 if ( dest
[ i
]+ fc
< dest
[ i
+ 1 ] && dest
[ i
+ 1 ] >= dest
[ i
+ 2 ]){
1463 //PrintAndLog("DEBUG: waveEnd: %d",waveEnd);
1464 waveLenCnt
= waveEnd
- waveStart
;
1465 if ( waveLenCnt
> fc
&& waveStart
> fc
){ //not first peak and is a large wave
1466 lastAvgWaveVal
= avgWaveVal
/( waveLenCnt
);
1467 firstFullWave
= waveStart
;
1468 fullWaveLen
= waveLenCnt
;
1469 //if average wave value is > graph 0 then it is an up wave or a 1
1470 if ( lastAvgWaveVal
> 123 ) curPhase
^= 1 ; //fudge graph 0 a little 123 vs 128
1476 avgWaveVal
+= dest
[ i
+ 2 ];
1478 //PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);
1479 lastClkBit
= firstFullWave
; //set start of wave as clock align
1480 //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d", *clock, lastClkBit);
1484 memset ( dest
, curPhase
^ 1 , firstFullWave
/ * clock
);
1485 numBits
+= ( firstFullWave
/ * clock
);
1486 dest
[ numBits
++] = curPhase
; //set first read bit
1487 for ( i
= firstFullWave
+ fullWaveLen
- 1 ; i
< * size
- 3 ; i
++){
1488 //top edge of wave = start of new wave
1489 if ( dest
[ i
]+ fc
< dest
[ i
+ 1 ] && dest
[ i
+ 1 ] >= dest
[ i
+ 2 ]){
1490 if ( waveStart
== 0 ) {
1493 avgWaveVal
= dest
[ i
+ 1 ];
1496 waveLenCnt
= waveEnd
- waveStart
;
1497 lastAvgWaveVal
= avgWaveVal
/ waveLenCnt
;
1498 if ( waveLenCnt
> fc
){
1499 //PrintAndLog("DEBUG: avgWaveVal: %d, waveSum: %d",lastAvgWaveVal,avgWaveVal);
1500 //this wave is a phase shift
1501 //PrintAndLog("DEBUG: phase shift at: %d, len: %d, nextClk: %d, i: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+*clock-tol,i+1,fc);
1502 if ( i
+ 1 >= lastClkBit
+ * clock
- tol
){ //should be a clock bit
1504 dest
[ numBits
++] = curPhase
;
1505 lastClkBit
+= * clock
;
1506 } else if ( i
< lastClkBit
+ 10 + fc
){
1507 //noise after a phase shift - ignore
1508 } else { //phase shift before supposed to based on clock
1510 dest
[ numBits
++] = 7 ;
1512 } else if ( i
+ 1 > lastClkBit
+ * clock
+ tol
+ fc
){
1513 lastClkBit
+= * clock
; //no phase shift but clock bit
1514 dest
[ numBits
++] = curPhase
;
1520 avgWaveVal
+= dest
[ i
+ 1 ];