]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - common/lfdemod.c
1 //-----------------------------------------------------------------------------
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Low frequency demod/decode commands
9 //-----------------------------------------------------------------------------
16 uint8_t justNoise(uint8_t *BitStream
, size_t size
)
18 static const uint8_t THRESHOLD
= 123;
19 //test samples are not just noise
20 uint8_t justNoise1
= 1;
21 for(size_t idx
=0; idx
< size
&& justNoise1
;idx
++){
22 justNoise1
= BitStream
[idx
] < THRESHOLD
;
28 //get high and low values of a wave with passed in fuzz factor. also return noise test = 1 for passed or 0 for only noise
29 int getHiLo(uint8_t *BitStream
, size_t size
, int *high
, int *low
, uint8_t fuzzHi
, uint8_t fuzzLo
)
33 // get high and low thresholds
34 for (int i
=0; i
< size
; i
++){
35 if (BitStream
[i
] > *high
) *high
= BitStream
[i
];
36 if (BitStream
[i
] < *low
) *low
= BitStream
[i
];
38 if (*high
< 123) return -1; // just noise
39 *high
= (int)(((*high
-128)*(((float)fuzzHi
)/100))+128);
40 *low
= (int)(((*low
-128)*(((float)fuzzLo
)/100))+128);
45 // pass bits to be tested in bits, length bits passed in bitLen, and parity type (even=0 | odd=1) in pType
46 // returns 1 if passed
47 uint8_t parityTest(uint32_t bits
, uint8_t bitLen
, uint8_t pType
)
50 for (uint8_t i
= 0; i
< bitLen
; i
++){
51 ans
^= ((bits
>> i
) & 1);
53 //PrintAndLog("DEBUG: ans: %d, ptype: %d",ans,pType);
54 return (ans
== pType
);
58 //search for given preamble in given BitStream and return success=1 or fail=0 and startIndex and length
59 uint8_t preambleSearch(uint8_t *BitStream
, uint8_t *preamble
, size_t pLen
, size_t *size
, size_t *startIdx
)
62 for (int idx
=0; idx
< *size
- pLen
; idx
++){
63 if (memcmp(BitStream
+idx
, preamble
, pLen
) == 0){
70 *size
= idx
- *startIdx
;
79 //takes 1s and 0s and searches for EM410x format - output EM ID
80 uint8_t Em410xDecode(uint8_t *BitStream
, size_t *size
, size_t *startIdx
, uint32_t *hi
, uint64_t *lo
)
82 //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
83 // otherwise could be a void with no arguments
86 if (BitStream
[1]>1){ //allow only 1s and 0s
87 // PrintAndLog("no data found");
90 // 111111111 bit pattern represent start of frame
91 uint8_t preamble
[] = {1,1,1,1,1,1,1,1,1};
93 uint32_t parityBits
= 0;
97 for (uint8_t extraBitChk
=0; extraBitChk
<5; extraBitChk
++){
98 errChk
= preambleSearch(BitStream
+extraBitChk
+*startIdx
, preamble
, sizeof(preamble
), size
, startIdx
);
99 if (errChk
== 0) return 0;
100 if (*size
<64) return 0;
101 if (*size
>64) FmtLen
= 22;
102 if (*size
<64) return 0;
104 for (i
=0; i
<FmtLen
; i
++){ //loop through 10 or 22 sets of 5 bits (50-10p = 40 bits or 88 bits)
105 parityBits
= bytebits_to_byte(BitStream
+(i
*5)+idx
,5);
107 if (parityTest(parityBits
, 5, 0) == 0){
108 //parity failed try next bit (in the case of 1111111111) but last 9 = preamble
113 //set uint64 with ID from BitStream
114 for (uint8_t ii
=0; ii
<4; ii
++){
115 *hi
= (*hi
<< 1) | (*lo
>> 63);
116 *lo
= (*lo
<< 1) | (BitStream
[(i
*5)+ii
+idx
]);
119 if (errChk
!= 0) return 1;
120 //skip last 5 bit parity test for simplicity.
127 //takes 3 arguments - clock, invert, maxErr as integers
128 //attempts to demodulate ask while decoding manchester
129 //prints binary found and saves in graphbuffer for further commands
130 int askmandemod(uint8_t *BinStream
, size_t *size
, int *clk
, int *invert
, int maxErr
)
134 int start
= DetectASKClock(BinStream
, *size
, clk
, 20); //clock default
135 if (*clk
==0) return -3;
136 if (start
< 0) return -3;
137 // if autodetected too low then adjust //MAY NEED ADJUSTMENT
138 //if (clk2==0 && *clk<8) *clk =64;
139 //if (clk2==0 && *clk<32) *clk=32;
140 if (*invert
!= 0 && *invert
!= 1) *invert
=0;
141 uint32_t initLoopMax
= 200;
142 if (initLoopMax
> *size
) initLoopMax
=*size
;
143 // Detect high and lows
144 // 25% fuzz in case highs and lows aren't clipped [marshmellow]
146 ans
= getHiLo(BinStream
, initLoopMax
, &high
, &low
, 75, 75);
147 if (ans
<1) return -2; //just noise
149 // PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
150 int lastBit
= 0; //set first clock check
151 uint32_t bitnum
= 0; //output counter
152 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
153 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
155 uint32_t gLen
= *size
;
156 if (gLen
> 3000) gLen
=3000;
157 //if 0 errors allowed then only try first 2 clock cycles as we want a low tolerance
158 if (!maxErr
) gLen
=*clk
*2;
160 uint16_t MaxBits
= 500;
161 uint32_t bestStart
= *size
;
162 int bestErrCnt
= maxErr
+1;
163 // PrintAndLog("DEBUG - lastbit - %d",lastBit);
164 // loop to find first wave that works
165 for (iii
=0; iii
< gLen
; ++iii
){
166 if ((BinStream
[iii
] >= high
) || (BinStream
[iii
] <= low
)){
169 // loop through to see if this start location works
170 for (i
= iii
; i
< *size
; ++i
) {
171 if ((BinStream
[i
] >= high
) && ((i
-lastBit
) > (*clk
-tol
))){
173 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
) > (*clk
-tol
))){
174 //low found and we are expecting a bar
177 //mid value found or no bar supposed to be here
178 if ((i
-lastBit
)>(*clk
+tol
)){
179 //should have hit a high or low based on clock!!
182 //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);
185 lastBit
+=*clk
;//skip over until hit too many errors
186 if (errCnt
>(maxErr
)) break; //allow 1 error for every 1000 samples else start over
189 if ((i
-iii
) >(MaxBits
* *clk
)) break; //got plenty of bits
191 //we got more than 64 good bits and not all errors
192 if ((((i
-iii
)/ *clk
) > (64)) && (errCnt
<=maxErr
)) {
197 break; //great read - finish
199 if (errCnt
<bestErrCnt
){ //set this as new best run
206 if (bestErrCnt
<=maxErr
){
207 //best run is good enough set to best run and set overwrite BinStream
209 lastBit
= bestStart
- *clk
;
211 for (i
= iii
; i
< *size
; ++i
) {
212 if ((BinStream
[i
] >= high
) && ((i
-lastBit
) > (*clk
-tol
))){
214 BinStream
[bitnum
] = *invert
;
216 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
) > (*clk
-tol
))){
217 //low found and we are expecting a bar
219 BinStream
[bitnum
] = 1-*invert
;
222 //mid value found or no bar supposed to be here
223 if ((i
-lastBit
)>(*clk
+tol
)){
224 //should have hit a high or low based on clock!!
227 //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);
229 BinStream
[bitnum
]=77;
233 lastBit
+=*clk
;//skip over error
236 if (bitnum
>=MaxBits
) break;
248 //encode binary data into binary manchester
249 int ManchesterEncode(uint8_t *BitStream
, size_t size
)
251 size_t modIdx
=20000, i
=0;
252 if (size
>modIdx
) return -1;
253 for (size_t idx
=0; idx
< size
; idx
++){
254 BitStream
[idx
+modIdx
++] = BitStream
[idx
];
255 BitStream
[idx
+modIdx
++] = BitStream
[idx
]^1;
257 for (; i
<(size
*2); i
++){
258 BitStream
[i
] = BitStream
[i
+20000];
264 //take 10 and 01 and manchester decode
265 //run through 2 times and take least errCnt
266 int manrawdecode(uint8_t * BitStream
, size_t *size
)
268 uint16_t bitnum
=0, MaxBits
= 512, errCnt
= 0;
270 uint16_t bestErr
= 1000, bestRun
= 0;
271 if (size
== 0) return -1;
272 for (ii
=0;ii
<2;++ii
){
274 for (i
=i
+ii
;i
<*size
-2;i
+=2){
275 if(BitStream
[i
]==1 && (BitStream
[i
+1]==0)){
276 } else if((BitStream
[i
]==0)&& BitStream
[i
+1]==1){
280 if(bitnum
>MaxBits
) break;
292 for (i
=i
+ii
; i
< *size
-2; i
+=2){
293 if(BitStream
[i
] == 1 && (BitStream
[i
+1] == 0)){
294 BitStream
[bitnum
++]=0;
295 } else if((BitStream
[i
] == 0) && BitStream
[i
+1] == 1){
296 BitStream
[bitnum
++]=1;
298 BitStream
[bitnum
++]=77;
301 if(bitnum
>MaxBits
) break;
309 //take 01 or 10 = 1 and 11 or 00 = 0
310 //check for phase errors - should never have 111 or 000 should be 01001011 or 10110100 for 1010
311 //decodes biphase or if inverted it is AKA conditional dephase encoding AKA differential manchester encoding
312 int BiphaseRawDecode(uint8_t *BitStream
, size_t *size
, int offset
, int invert
)
317 uint16_t MaxBits
=512;
318 //if not enough samples - error
319 if (*size
< 51) return -1;
320 //check for phase change faults - skip one sample if faulty
321 uint8_t offsetA
= 1, offsetB
= 1;
323 if (BitStream
[i
+1]==BitStream
[i
+2]) offsetA
=0;
324 if (BitStream
[i
+2]==BitStream
[i
+3]) offsetB
=0;
326 if (!offsetA
&& offsetB
) offset
++;
327 for (i
=offset
; i
<*size
-3; i
+=2){
328 //check for phase error
329 if (BitStream
[i
+1]==BitStream
[i
+2]) {
330 BitStream
[bitnum
++]=77;
333 if((BitStream
[i
]==1 && BitStream
[i
+1]==0) || (BitStream
[i
]==0 && BitStream
[i
+1]==1)){
334 BitStream
[bitnum
++]=1^invert
;
335 } else if((BitStream
[i
]==0 && BitStream
[i
+1]==0) || (BitStream
[i
]==1 && BitStream
[i
+1]==1)){
336 BitStream
[bitnum
++]=invert
;
338 BitStream
[bitnum
++]=77;
341 if(bitnum
>MaxBits
) break;
348 void askAmp(uint8_t *BitStream
, size_t size
)
352 for(int i
= 1; i
<size
; i
++){
353 if (BitStream
[i
]-BitStream
[i
-1]>=30) //large jump up
355 else if(BitStream
[i
]-BitStream
[i
-1]<=-20) //large jump down
358 shiftedVal
=BitStream
[i
]+shift
;
362 else if (shiftedVal
<0)
364 BitStream
[i
-1] = shiftedVal
;
369 int cleanAskRawDemod(uint8_t *BinStream
, size_t *size
, int clk
, int invert
, int high
, int low
)
371 size_t bitCnt
=0, smplCnt
=0, errCnt
=0;
372 uint8_t waveHigh
= 0;
373 //PrintAndLog("clk: %d", clk);
374 for (size_t i
=0; i
< *size
; i
++){
375 if (BinStream
[i
] >= high
&& waveHigh
){
377 } else if (BinStream
[i
] <= low
&& !waveHigh
){
379 } else { //transition
380 if ((BinStream
[i
] >= high
&& !waveHigh
) || (BinStream
[i
] <= low
&& waveHigh
)){
381 if (smplCnt
> clk
-(clk
/4)-1) { //full clock
382 if (smplCnt
> clk
+ (clk
/4)+1) { //too many samples
384 BinStream
[bitCnt
++]=77;
385 } else if (waveHigh
) {
386 BinStream
[bitCnt
++] = invert
;
387 BinStream
[bitCnt
++] = invert
;
388 } else if (!waveHigh
) {
389 BinStream
[bitCnt
++] = invert
^ 1;
390 BinStream
[bitCnt
++] = invert
^ 1;
394 } else if (smplCnt
> (clk
/2) - (clk
/4)-1) {
396 BinStream
[bitCnt
++] = invert
;
397 } else if (!waveHigh
) {
398 BinStream
[bitCnt
++] = invert
^ 1;
402 } else if (!bitCnt
) {
404 waveHigh
= (BinStream
[i
] >= high
);
408 //transition bit oops
410 } else { //haven't hit new high or new low yet
420 //takes 3 arguments - clock, invert and maxErr as integers
421 //attempts to demodulate ask only
422 int askrawdemod(uint8_t *BinStream
, size_t *size
, int *clk
, int *invert
, int maxErr
, uint8_t amp
)
425 if (*size
==0) return -1;
426 int start
= DetectASKClock(BinStream
, *size
, clk
, 20); //clock default
427 if (*clk
==0) return -1;
428 if (start
<0) return -1;
429 if (*invert
!= 0 && *invert
!= 1) *invert
=0;
430 if (amp
==1) askAmp(BinStream
, *size
);
432 uint32_t initLoopMax
= 200;
433 if (initLoopMax
> *size
) initLoopMax
=*size
;
434 // Detect high and lows
435 //25% clip in case highs and lows aren't clipped [marshmellow]
438 ans
= getHiLo(BinStream
, initLoopMax
, &high
, &low
, clip
, clip
);
439 if (ans
<1) return -1; //just noise
441 if (DetectCleanAskWave(BinStream
, *size
, high
, low
)) {
442 //PrintAndLog("Clean");
443 return cleanAskRawDemod(BinStream
, size
, *clk
, *invert
, high
, low
);
446 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
447 int lastBit
= 0; //set first clock check
448 uint32_t bitnum
= 0; //output counter
449 uint8_t tol
= 0; //clock tolerance adjust - waves will be accepted as within the clock
450 // if they fall + or - this value + clock from last valid wave
451 if (*clk
== 32) tol
=0; //clock tolerance may not be needed anymore currently set to
452 // + or - 1 but could be increased for poor waves or removed entirely
454 uint32_t gLen
= *size
;
455 if (gLen
> 500) gLen
=500;
456 //if 0 errors allowed then only try first 2 clock cycles as we want a low tolerance
457 if (!maxErr
) gLen
= *clk
* 2;
459 uint32_t bestStart
= *size
;
460 uint32_t bestErrCnt
= maxErr
; //(*size/1000);
462 uint16_t MaxBits
=1000;
464 //PrintAndLog("DEBUG - lastbit - %d",lastBit);
465 //loop to find first wave that works
466 for (iii
=start
; iii
< gLen
; ++iii
){
467 if ((BinStream
[iii
]>=high
) || (BinStream
[iii
]<=low
)){
470 //loop through to see if this start location works
471 for (i
= iii
; i
< *size
; ++i
) {
472 if ((BinStream
[i
] >= high
) && ((i
-lastBit
)>(*clk
-tol
))){
475 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
)>(*clk
-tol
))){
476 //low found and we are expecting a bar
479 } else if ((BinStream
[i
]<=low
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
482 } else if ((BinStream
[i
]>=high
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
485 } else if ((i
-lastBit
)>((*clk
/2)+tol
) && (midBit
==0)){
489 //mid value found or no bar supposed to be here
491 if ((i
-lastBit
)>(*clk
+tol
)){
492 //should have hit a high or low based on clock!!
494 //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);
497 lastBit
+=*clk
;//skip over until hit too many errors
498 if (errCnt
> maxErr
){
504 if ((i
-iii
)>(MaxBits
* *clk
)) break; //got enough bits
506 //we got more than 64 good bits and not all errors
507 if ((((i
-iii
)/ *clk
) > (64)) && (errCnt
<=maxErr
)) {
512 break; //great read - finish
514 if (errCnt
<bestErrCnt
){ //set this as new best run
521 if (bestErrCnt
<=maxErr
){
522 //best run is good enough - set to best run and overwrite BinStream
524 lastBit
= bestStart
- *clk
;
526 for (i
= iii
; i
< *size
; ++i
) {
527 if ((BinStream
[i
] >= high
) && ((i
-lastBit
) > (*clk
-tol
))){
529 BinStream
[bitnum
] = *invert
;
532 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
) > (*clk
-tol
))){
533 //low found and we are expecting a bar
535 BinStream
[bitnum
] = 1 - *invert
;
538 } else if ((BinStream
[i
]<=low
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
541 BinStream
[bitnum
] = 1 - *invert
;
543 } else if ((BinStream
[i
]>=high
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
546 BinStream
[bitnum
] = *invert
;
548 } else if ((i
-lastBit
)>((*clk
/2)+tol
) && (midBit
==0)){
551 if (bitnum
!=0) BinStream
[bitnum
] = BinStream
[bitnum
-1];
555 //mid value found or no bar supposed to be here
556 if ((i
-lastBit
)>(*clk
+tol
)){
557 //should have hit a high or low based on clock!!
560 //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);
562 BinStream
[bitnum
]=77;
565 lastBit
+=*clk
;//skip over error
568 if (bitnum
>= MaxBits
) break;
579 // demod gProxIIDemod
580 // error returns as -x
581 // success returns start position in BitStream
582 // BitStream must contain previously askrawdemod and biphasedemoded data
583 int gProxII_Demod(uint8_t BitStream
[], size_t *size
)
586 uint8_t preamble
[] = {1,1,1,1,1,0};
588 uint8_t errChk
= preambleSearch(BitStream
, preamble
, sizeof(preamble
), size
, &startIdx
);
589 if (errChk
== 0) return -3; //preamble not found
590 if (*size
!= 96) return -2; //should have found 96 bits
591 //check first 6 spacer bits to verify format
592 if (!BitStream
[startIdx
+5] && !BitStream
[startIdx
+10] && !BitStream
[startIdx
+15] && !BitStream
[startIdx
+20] && !BitStream
[startIdx
+25] && !BitStream
[startIdx
+30]){
593 //confirmed proper separator bits found
594 //return start position
595 return (int) startIdx
;
600 //translate wave to 11111100000 (1 for each short wave 0 for each long wave)
601 size_t fsk_wave_demod(uint8_t * dest
, size_t size
, uint8_t fchigh
, uint8_t fclow
)
603 uint32_t last_transition
= 0;
606 if (fchigh
==0) fchigh
=10;
607 if (fclow
==0) fclow
=8;
608 //set the threshold close to 0 (graph) or 128 std to avoid static
609 uint8_t threshold_value
= 123;
611 // sync to first lo-hi transition, and threshold
613 // Need to threshold first sample
615 if(dest
[0] < threshold_value
) dest
[0] = 0;
619 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
620 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
621 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
622 for(idx
= 1; idx
< size
; idx
++) {
623 // threshold current value
625 if (dest
[idx
] < threshold_value
) dest
[idx
] = 0;
628 // Check for 0->1 transition
629 if (dest
[idx
-1] < dest
[idx
]) { // 0 -> 1 transition
630 if ((idx
-last_transition
)<(fclow
-2)){ //0-5 = garbage noise
631 //do nothing with extra garbage
632 } else if ((idx
-last_transition
) < (fchigh
-1)) { //6-8 = 8 waves
634 } else if ((idx
-last_transition
) > (fchigh
+1) && !numBits
) { //12 + and first bit = garbage
635 //do nothing with beginning garbage
636 } else { //9+ = 10 waves
639 last_transition
= idx
;
643 return numBits
; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
646 uint32_t myround2(float f
)
648 if (f
>= 2000) return 2000;//something bad happened
649 return (uint32_t) (f
+ (float)0.5);
652 //translate 11111100000 to 10
653 size_t aggregate_bits(uint8_t *dest
, size_t size
, uint8_t rfLen
, uint8_t maxConsequtiveBits
,
654 uint8_t invert
, uint8_t fchigh
, uint8_t fclow
)
656 uint8_t lastval
=dest
[0];
660 float lowWaves
= (((float)(rfLen
))/((float)fclow
));
661 float highWaves
= (((float)(rfLen
))/((float)fchigh
));
662 for( idx
=1; idx
< size
; idx
++) {
664 if (dest
[idx
]==lastval
) {
669 //if lastval was 1, we have a 1->0 crossing
670 if (dest
[idx
-1]==1) {
671 if (!numBits
&& n
< (uint8_t)lowWaves
) {
676 n
=myround2(((float)n
)/lowWaves
);
677 } else {// 0->1 crossing
678 //test first bitsample too small
679 if (!numBits
&& n
< (uint8_t)highWaves
) {
684 n
= myround2(((float)n
)/highWaves
); //-1 for fudge factor
688 if(n
< maxConsequtiveBits
) //Consecutive
690 if(invert
==0){ //invert bits
691 memset(dest
+numBits
, dest
[idx
-1] , n
);
693 memset(dest
+numBits
, dest
[idx
-1]^1 , n
);
701 // if valid extra bits at the end were all the same frequency - add them in
702 if (n
> lowWaves
&& n
> highWaves
) {
703 if (dest
[idx
-2]==1) {
704 n
=myround2((float)(n
+1)/((float)(rfLen
)/(float)fclow
));
706 n
=myround2((float)(n
+1)/((float)(rfLen
-1)/(float)fchigh
)); //-1 for fudge factor
708 memset(dest
, dest
[idx
-1]^invert
, n
);
713 //by marshmellow (from holiman's base)
714 // full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod)
715 int fskdemod(uint8_t *dest
, size_t size
, uint8_t rfLen
, uint8_t invert
, uint8_t fchigh
, uint8_t fclow
)
718 size
= fsk_wave_demod(dest
, size
, fchigh
, fclow
);
719 size
= aggregate_bits(dest
, size
, rfLen
, 192, invert
, fchigh
, fclow
);
723 // loop to get raw HID waveform then FSK demodulate the TAG ID from it
724 int HIDdemodFSK(uint8_t *dest
, size_t *size
, uint32_t *hi2
, uint32_t *hi
, uint32_t *lo
)
726 if (justNoise(dest
, *size
)) return -1;
728 size_t numStart
=0, size2
=*size
, startIdx
=0;
730 *size
= fskdemod(dest
, size2
,50,1,10,8); //fsk2a
731 if (*size
< 96) return -2;
732 // 00011101 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1
733 uint8_t preamble
[] = {0,0,0,1,1,1,0,1};
734 // find bitstring in array
735 uint8_t errChk
= preambleSearch(dest
, preamble
, sizeof(preamble
), size
, &startIdx
);
736 if (errChk
== 0) return -3; //preamble not found
738 numStart
= startIdx
+ sizeof(preamble
);
739 // final loop, go over previously decoded FSK data and manchester decode into usable tag ID
740 for (size_t idx
= numStart
; (idx
-numStart
) < *size
- sizeof(preamble
); idx
+=2){
741 if (dest
[idx
] == dest
[idx
+1]){
742 return -4; //not manchester data
744 *hi2
= (*hi2
<<1)|(*hi
>>31);
745 *hi
= (*hi
<<1)|(*lo
>>31);
746 //Then, shift in a 0 or one into low
747 if (dest
[idx
] && !dest
[idx
+1]) // 1 0
752 return (int)startIdx
;
755 // loop to get raw paradox waveform then FSK demodulate the TAG ID from it
756 int ParadoxdemodFSK(uint8_t *dest
, size_t *size
, uint32_t *hi2
, uint32_t *hi
, uint32_t *lo
)
758 if (justNoise(dest
, *size
)) return -1;
760 size_t numStart
=0, size2
=*size
, startIdx
=0;
762 *size
= fskdemod(dest
, size2
,50,1,10,8); //fsk2a
763 if (*size
< 96) return -2;
765 // 00001111 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1
766 uint8_t preamble
[] = {0,0,0,0,1,1,1,1};
768 uint8_t errChk
= preambleSearch(dest
, preamble
, sizeof(preamble
), size
, &startIdx
);
769 if (errChk
== 0) return -3; //preamble not found
771 numStart
= startIdx
+ sizeof(preamble
);
772 // final loop, go over previously decoded FSK data and manchester decode into usable tag ID
773 for (size_t idx
= numStart
; (idx
-numStart
) < *size
- sizeof(preamble
); idx
+=2){
774 if (dest
[idx
] == dest
[idx
+1])
775 return -4; //not manchester data
776 *hi2
= (*hi2
<<1)|(*hi
>>31);
777 *hi
= (*hi
<<1)|(*lo
>>31);
778 //Then, shift in a 0 or one into low
779 if (dest
[idx
] && !dest
[idx
+1]) // 1 0
784 return (int)startIdx
;
787 uint32_t bytebits_to_byte(uint8_t* src
, size_t numbits
)
790 for(int i
= 0 ; i
< numbits
; i
++)
792 num
= (num
<< 1) | (*src
);
798 int IOdemodFSK(uint8_t *dest
, size_t size
)
800 if (justNoise(dest
, size
)) return -1;
801 //make sure buffer has data
802 if (size
< 66*64) return -2;
804 size
= fskdemod(dest
, size
, 64, 1, 10, 8); // FSK2a RF/64
805 if (size
< 65) return -3; //did we get a good demod?
807 //0 10 20 30 40 50 60
809 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
810 //-----------------------------------------------------------------------------
811 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
813 //XSF(version)facility:codeone+codetwo
816 uint8_t preamble
[] = {0,0,0,0,0,0,0,0,0,1};
817 uint8_t errChk
= preambleSearch(dest
, preamble
, sizeof(preamble
), &size
, &startIdx
);
818 if (errChk
== 0) return -4; //preamble not found
820 if (!dest
[startIdx
+8] && dest
[startIdx
+17]==1 && dest
[startIdx
+26]==1 && dest
[startIdx
+35]==1 && dest
[startIdx
+44]==1 && dest
[startIdx
+53]==1){
821 //confirmed proper separator bits found
822 //return start position
823 return (int) startIdx
;
829 // takes a array of binary values, start position, length of bits per parity (includes parity bit),
830 // Parity Type (1 for odd 0 for even), and binary Length (length to run)
831 size_t removeParity(uint8_t *BitStream
, size_t startIdx
, uint8_t pLen
, uint8_t pType
, size_t bLen
)
833 uint32_t parityWd
= 0;
834 size_t j
= 0, bitCnt
= 0;
835 for (int word
= 0; word
< (bLen
); word
+=pLen
){
836 for (int bit
=0; bit
< pLen
; bit
++){
837 parityWd
= (parityWd
<< 1) | BitStream
[startIdx
+word
+bit
];
838 BitStream
[j
++] = (BitStream
[startIdx
+word
+bit
]);
841 // if parity fails then return 0
842 if (parityTest(parityWd
, pLen
, pType
) == 0) return -1;
846 // if we got here then all the parities passed
847 //return ID start index and size
852 // FSK Demod then try to locate an AWID ID
853 int AWIDdemodFSK(uint8_t *dest
, size_t *size
)
855 //make sure buffer has enough data
856 if (*size
< 96*50) return -1;
858 if (justNoise(dest
, *size
)) return -2;
861 *size
= fskdemod(dest
, *size
, 50, 1, 10, 8); // fsk2a RF/50
862 if (*size
< 96) return -3; //did we get a good demod?
864 uint8_t preamble
[] = {0,0,0,0,0,0,0,1};
866 uint8_t errChk
= preambleSearch(dest
, preamble
, sizeof(preamble
), size
, &startIdx
);
867 if (errChk
== 0) return -4; //preamble not found
868 if (*size
!= 96) return -5;
869 return (int)startIdx
;
873 // FSK Demod then try to locate an Farpointe Data (pyramid) ID
874 int PyramiddemodFSK(uint8_t *dest
, size_t *size
)
876 //make sure buffer has data
877 if (*size
< 128*50) return -5;
879 //test samples are not just noise
880 if (justNoise(dest
, *size
)) return -1;
883 *size
= fskdemod(dest
, *size
, 50, 1, 10, 8); // fsk2a RF/50
884 if (*size
< 128) return -2; //did we get a good demod?
886 uint8_t preamble
[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
888 uint8_t errChk
= preambleSearch(dest
, preamble
, sizeof(preamble
), size
, &startIdx
);
889 if (errChk
== 0) return -4; //preamble not found
890 if (*size
!= 128) return -3;
891 return (int)startIdx
;
895 uint8_t DetectCleanAskWave(uint8_t dest
[], size_t size
, int high
, int low
)
899 size_t loopEnd
= 572;
900 if (loopEnd
> size
) loopEnd
= size
;
901 for (size_t i
=60; i
<loopEnd
; i
++){
902 if (dest
[i
]>low
&& dest
[i
]<high
)
908 if (cntPeaks
> 300) return 1;
913 int DetectStrongAskClock(uint8_t dest
[], size_t size
)
915 int clk
[]={0,8,16,32,40,50,64,100,128,256};
921 for (;idx
< size
; idx
++){
926 if (highCnt
!= 0) highCnt2
= highCnt
;
928 } else if (cnt
> highCnt2
) {
935 } else if (dest
[idx
] <= 128){
939 if (highCnt
!= 0) highCnt2
= highCnt
;
941 } else if (cnt
> highCnt2
) {
951 for (idx
=8; idx
>0; idx
--){
953 if (clk
[idx
] >= highCnt
- tol
&& clk
[idx
] <= highCnt
+ tol
)
955 if (clk
[idx
] >= highCnt2
- tol
&& clk
[idx
] <= highCnt2
+ tol
)
962 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
963 // maybe somehow adjust peak trimming value based on samples to fix?
964 // return start index of best starting position for that clock and return clock (by reference)
965 int DetectASKClock(uint8_t dest
[], size_t size
, int *clock
, int maxErr
)
968 int clk
[]={8,16,32,40,50,64,100,128,256};
969 int loopCnt
= 256; //don't need to loop through entire array...
970 if (size
== 0) return -1;
971 if (size
<loopCnt
) loopCnt
= size
;
972 //if we already have a valid clock quit
975 if (clk
[i
] == *clock
) return 0;
977 //get high and low peak
979 getHiLo(dest
, loopCnt
, &peak
, &low
, 75, 75);
981 //test for large clean peaks
982 if (DetectCleanAskWave(dest
, size
, peak
, low
)==1){
983 int ans
= DetectStrongAskClock(dest
, size
);
994 int bestErr
[]={1000,1000,1000,1000,1000,1000,1000,1000,1000};
995 int bestStart
[]={0,0,0,0,0,0,0,0,0};
997 //test each valid clock from smallest to greatest to see which lines up
998 for(clkCnt
=0; clkCnt
< 8; clkCnt
++){
999 if (clk
[clkCnt
] == 32){
1004 if (!maxErr
) loopCnt
=clk
[clkCnt
]*2;
1005 bestErr
[clkCnt
]=1000;
1006 //try lining up the peaks by moving starting point (try first 256)
1007 for (ii
=0; ii
< loopCnt
; ii
++){
1008 if ((dest
[ii
] >= peak
) || (dest
[ii
] <= low
)){
1010 // now that we have the first one lined up test rest of wave array
1011 for (i
=0; i
<((int)((size
-ii
-tol
)/clk
[clkCnt
])-1); ++i
){
1012 if (dest
[ii
+(i
*clk
[clkCnt
])]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])]<=low
){
1013 }else if(dest
[ii
+(i
*clk
[clkCnt
])-tol
]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])-tol
]<=low
){
1014 }else if(dest
[ii
+(i
*clk
[clkCnt
])+tol
]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])+tol
]<=low
){
1015 }else{ //error no peak detected
1019 //if we found no errors then we can stop here
1020 // this is correct one - return this clock
1021 //PrintAndLog("DEBUG: clk %d, err %d, ii %d, i %d",clk[clkCnt],errCnt,ii,i);
1022 if(errCnt
==0 && clkCnt
<6) {
1023 *clock
= clk
[clkCnt
];
1026 //if we found errors see if it is lowest so far and save it as best run
1027 if(errCnt
<bestErr
[clkCnt
]){
1028 bestErr
[clkCnt
]=errCnt
;
1029 bestStart
[clkCnt
]=ii
;
1036 for (iii
=0; iii
<8; ++iii
){
1037 if (bestErr
[iii
]<bestErr
[best
]){
1038 if (bestErr
[iii
]==0) bestErr
[iii
]=1;
1039 // current best bit to error ratio vs new bit to error ratio
1040 if (((size
/clk
[best
])/bestErr
[best
] < (size
/clk
[iii
])/bestErr
[iii
]) ){
1045 if (bestErr
[best
]>maxErr
) return -1;
1047 return bestStart
[best
];
1051 //detect psk clock by reading each phase shift
1052 // a phase shift is determined by measuring the sample length of each wave
1053 int DetectPSKClock(uint8_t dest
[], size_t size
, int clock
)
1055 uint8_t clk
[]={255,16,32,40,50,64,100,128,255}; //255 is not a valid clock
1056 uint16_t loopCnt
= 4096; //don't need to loop through entire array...
1057 if (size
== 0) return 0;
1058 if (size
<loopCnt
) loopCnt
= size
;
1060 //if we already have a valid clock quit
1063 if (clk
[i
] == clock
) return clock
;
1065 size_t waveStart
=0, waveEnd
=0, firstFullWave
=0, lastClkBit
=0;
1066 uint8_t clkCnt
, fc
=0, fullWaveLen
=0, tol
=1;
1067 uint16_t peakcnt
=0, errCnt
=0, waveLenCnt
=0;
1068 uint16_t bestErr
[]={1000,1000,1000,1000,1000,1000,1000,1000,1000};
1069 uint16_t peaksdet
[]={0,0,0,0,0,0,0,0,0};
1070 countFC(dest
, size
, &fc
);
1071 //PrintAndLog("DEBUG: FC: %d",fc);
1073 //find first full wave
1074 for (i
=0; i
<loopCnt
; i
++){
1075 if (dest
[i
] < dest
[i
+1] && dest
[i
+1] >= dest
[i
+2]){
1076 if (waveStart
== 0) {
1078 //PrintAndLog("DEBUG: waveStart: %d",waveStart);
1081 //PrintAndLog("DEBUG: waveEnd: %d",waveEnd);
1082 waveLenCnt
= waveEnd
-waveStart
;
1083 if (waveLenCnt
> fc
){
1084 firstFullWave
= waveStart
;
1085 fullWaveLen
=waveLenCnt
;
1092 //PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);
1094 //test each valid clock from greatest to smallest to see which lines up
1095 for(clkCnt
=7; clkCnt
>= 1 ; clkCnt
--){
1096 lastClkBit
= firstFullWave
; //set end of wave as clock align
1100 //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d",clk[clkCnt],lastClkBit);
1102 for (i
= firstFullWave
+fullWaveLen
-1; i
< loopCnt
-2; i
++){
1103 //top edge of wave = start of new wave
1104 if (dest
[i
] < dest
[i
+1] && dest
[i
+1] >= dest
[i
+2]){
1105 if (waveStart
== 0) {
1110 waveLenCnt
= waveEnd
-waveStart
;
1111 if (waveLenCnt
> fc
){
1112 //if this wave is a phase shift
1113 //PrintAndLog("DEBUG: phase shift at: %d, len: %d, nextClk: %d, ii: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+clk[clkCnt]-tol,ii+1,fc);
1114 if (i
+1 >= lastClkBit
+ clk
[clkCnt
] - tol
){ //should be a clock bit
1116 lastClkBit
+=clk
[clkCnt
];
1117 } else if (i
<lastClkBit
+8){
1118 //noise after a phase shift - ignore
1119 } else { //phase shift before supposed to based on clock
1122 } else if (i
+1 > lastClkBit
+ clk
[clkCnt
] + tol
+ fc
){
1123 lastClkBit
+=clk
[clkCnt
]; //no phase shift but clock bit
1132 if (errCnt
<= bestErr
[clkCnt
]) bestErr
[clkCnt
]=errCnt
;
1133 if (peakcnt
> peaksdet
[clkCnt
]) peaksdet
[clkCnt
]=peakcnt
;
1135 //all tested with errors
1136 //return the highest clk with the most peaks found
1138 for (i
=7; i
>=1; i
--){
1139 if (peaksdet
[i
] > peaksdet
[best
]) {
1142 //PrintAndLog("DEBUG: Clk: %d, peaks: %d, errs: %d, bestClk: %d",clk[iii],peaksdet[iii],bestErr[iii],clk[best]);
1148 //detect nrz clock by reading #peaks vs no peaks(or errors)
1149 int DetectNRZClock(uint8_t dest
[], size_t size
, int clock
)
1152 int clk
[]={8,16,32,40,50,64,100,128,256};
1153 int loopCnt
= 4096; //don't need to loop through entire array...
1154 if (size
== 0) return 0;
1155 if (size
<loopCnt
) loopCnt
= size
;
1157 //if we already have a valid clock quit
1159 if (clk
[i
] == clock
) return clock
;
1161 //get high and low peak
1163 getHiLo(dest
, loopCnt
, &peak
, &low
, 75, 75);
1165 //PrintAndLog("DEBUG: peak: %d, low: %d",peak,low);
1170 int peaksdet
[]={0,0,0,0,0,0,0,0};
1172 //test for large clipped waves
1173 for (i
=0; i
<loopCnt
; i
++){
1174 if (dest
[i
] >= peak
|| dest
[i
] <= low
){
1177 if (peakcnt
>0 && maxPeak
< peakcnt
){
1184 //test each valid clock from smallest to greatest to see which lines up
1185 for(clkCnt
=0; clkCnt
< 8; ++clkCnt
){
1186 //ignore clocks smaller than largest peak
1187 if (clk
[clkCnt
]<maxPeak
) continue;
1189 //try lining up the peaks by moving starting point (try first 256)
1190 for (ii
=0; ii
< loopCnt
; ++ii
){
1191 if ((dest
[ii
] >= peak
) || (dest
[ii
] <= low
)){
1193 // now that we have the first one lined up test rest of wave array
1194 for (i
=0; i
< ((int)((size
-ii
-tol
)/clk
[clkCnt
])-1); ++i
){
1195 if (dest
[ii
+(i
*clk
[clkCnt
])]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])]<=low
){
1199 if(peakcnt
>peaksdet
[clkCnt
]) {
1200 peaksdet
[clkCnt
]=peakcnt
;
1207 for (iii
=7; iii
> 0; iii
--){
1208 if (peaksdet
[iii
] > peaksdet
[best
]){
1211 //PrintAndLog("DEBUG: Clk: %d, peaks: %d, errs: %d, bestClk: %d",clk[iii],peaksdet[iii],bestErr[iii],clk[best]);
1217 // convert psk1 demod to psk2 demod
1218 // only transition waves are 1s
1219 void psk1TOpsk2(uint8_t *BitStream
, size_t size
)
1222 uint8_t lastBit
=BitStream
[0];
1223 for (; i
<size
; i
++){
1224 if (BitStream
[i
]==77){
1226 } else if (lastBit
!=BitStream
[i
]){
1227 lastBit
=BitStream
[i
];
1237 // convert psk2 demod to psk1 demod
1238 // from only transition waves are 1s to phase shifts change bit
1239 void psk2TOpsk1(uint8_t *BitStream
, size_t size
)
1242 for (size_t i
=0; i
<size
; i
++){
1243 if (BitStream
[i
]==1){
1251 // redesigned by marshmellow adjusted from existing decode functions
1252 // indala id decoding - only tested on 26 bit tags, but attempted to make it work for more
1253 int indala26decode(uint8_t *bitStream
, size_t *size
, uint8_t *invert
)
1255 //26 bit 40134 format (don't know other formats)
1257 int long_wait
=29;//29 leading zeros in format
1263 // Finding the start of a UID
1264 for (start
= 0; start
<= *size
- 250; start
++) {
1265 first
= bitStream
[start
];
1266 for (i
= start
; i
< start
+ long_wait
; i
++) {
1267 if (bitStream
[i
] != first
) {
1271 if (i
== (start
+ long_wait
)) {
1275 if (start
== *size
- 250 + 1) {
1276 // did not find start sequence
1279 // Inverting signal if needed
1281 for (i
= start
; i
< *size
; i
++) {
1282 bitStream
[i
] = !bitStream
[i
];
1288 //found start once now test length by finding next one
1289 for (ii
=start
+29; ii
<= *size
- 250; ii
++) {
1290 first2
= bitStream
[ii
];
1291 for (iii
= ii
; iii
< ii
+ long_wait
; iii
++) {
1292 if (bitStream
[iii
] != first2
) {
1296 if (iii
== (ii
+ long_wait
)) {
1300 if (ii
== *size
- 250 + 1){
1301 // did not find second start sequence
1308 for (ii
= 0; ii
< bitCnt
; ii
++) {
1309 bitStream
[ii
] = bitStream
[i
++];
1315 // by marshmellow - demodulate NRZ wave (both similar enough)
1316 // peaks invert bit (high=1 low=0) each clock cycle = 1 bit determined by last peak
1317 // there probably is a much simpler way to do this....
1318 int nrzRawDemod(uint8_t *dest
, size_t *size
, int *clk
, int *invert
, int maxErr
)
1320 if (justNoise(dest
, *size
)) return -1;
1321 *clk
= DetectNRZClock(dest
, *size
, *clk
);
1322 if (*clk
==0) return -2;
1324 uint32_t gLen
= 4096;
1325 if (gLen
>*size
) gLen
= *size
;
1327 if (getHiLo(dest
, gLen
, &high
, &low
, 75, 75) < 1) return -3; //25% fuzz on high 25% fuzz on low
1328 int lastBit
= 0; //set first clock check
1329 uint32_t bitnum
= 0; //output counter
1330 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
1333 uint16_t MaxBits
= 1000;
1334 uint32_t bestErrCnt
= maxErr
+1;
1335 uint32_t bestPeakCnt
= 0;
1336 uint32_t bestPeakStart
=0;
1337 uint8_t bestFirstPeakHigh
=0;
1338 uint8_t firstPeakHigh
=0;
1341 uint8_t errBitHigh
=0;
1343 uint8_t ignoreWindow
=4;
1344 uint8_t ignoreCnt
=ignoreWindow
; //in case of noice near peak
1345 //loop to find first wave that works - align to clock
1346 for (iii
=0; iii
< gLen
; ++iii
){
1347 if ((dest
[iii
]>=high
) || (dest
[iii
]<=low
)){
1348 if (dest
[iii
]>=high
) firstPeakHigh
=1;
1349 else firstPeakHigh
=0;
1354 //loop through to see if this start location works
1355 for (i
= iii
; i
< *size
; ++i
) {
1356 //if we found a high bar and we are at a clock bit
1357 if ((dest
[i
]>=high
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
1363 ignoreCnt
=ignoreWindow
;
1364 //else if low bar found and we are at a clock point
1365 }else if ((dest
[i
]<=low
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
1371 ignoreCnt
=ignoreWindow
;
1372 //else if no bars found
1373 }else if(dest
[i
] < high
&& dest
[i
] > low
) {
1383 //if we are past a clock point
1384 if (i
>= lastBit
+*clk
+tol
){ //clock val
1388 //else if bar found but we are not at a clock bit and we did not just have a clock bit
1389 }else if ((dest
[i
]>=high
|| dest
[i
]<=low
) && (i
<lastBit
+*clk
-tol
|| i
>lastBit
+*clk
+tol
) && (bitHigh
==0)){
1390 //error bar found no clock...
1393 if (bitnum
>=MaxBits
) break;
1395 //we got more than 64 good bits and not all errors
1396 if (bitnum
> (64) && (errCnt
<= (maxErr
))) {
1397 //possible good read
1400 bestFirstPeakHigh
=firstPeakHigh
;
1401 bestErrCnt
= errCnt
;
1402 bestPeakCnt
= peakCnt
;
1403 bestPeakStart
= iii
;
1404 break; //great read - finish
1406 if (errCnt
< bestErrCnt
){ //set this as new best run
1407 bestErrCnt
= errCnt
;
1410 if (peakCnt
> bestPeakCnt
){
1411 bestFirstPeakHigh
=firstPeakHigh
;
1412 bestPeakCnt
=peakCnt
;
1418 //PrintAndLog("DEBUG: bestErrCnt: %d, maxErr: %d, bestStart: %d, bestPeakCnt: %d, bestPeakStart: %d",bestErrCnt,maxErr,bestStart,bestPeakCnt,bestPeakStart);
1419 if (bestErrCnt
<= maxErr
){
1420 //best run is good enough set to best run and set overwrite BinStream
1422 lastBit
=bestPeakStart
-*clk
;
1424 memset(dest
, bestFirstPeakHigh
^1, bestPeakStart
/ *clk
);
1425 bitnum
+= (bestPeakStart
/ *clk
);
1426 for (i
= iii
; i
< *size
; ++i
) {
1427 //if we found a high bar and we are at a clock bit
1428 if ((dest
[i
] >= high
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
1432 dest
[bitnum
]=curBit
;
1435 ignoreCnt
=ignoreWindow
;
1436 //else if low bar found and we are at a clock point
1437 }else if ((dest
[i
]<=low
) && (i
>=lastBit
+*clk
-tol
&& i
<=lastBit
+*clk
+tol
)){
1441 dest
[bitnum
]=curBit
;
1444 ignoreCnt
=ignoreWindow
;
1445 //else if no bars found
1446 }else if(dest
[i
]<high
&& dest
[i
]>low
) {
1449 //if peak is done was it an error peak?
1459 //if we are past a clock point
1460 if (i
>=lastBit
+*clk
+tol
){ //clock val
1462 dest
[bitnum
]=curBit
;
1465 //else if bar found but we are not at a clock bit and we did not just have a clock bit
1466 }else if ((dest
[i
]>=high
|| dest
[i
]<=low
) && ((i
<lastBit
+*clk
-tol
) || (i
>lastBit
+*clk
+tol
)) && (bitHigh
==0)){
1467 //error bar found no clock...
1470 if (bitnum
>= MaxBits
) break;
1485 //detects the bit clock for FSK given the high and low Field Clocks
1486 uint8_t detectFSKClk(uint8_t *BitStream
, size_t size
, uint8_t fcHigh
, uint8_t fcLow
)
1488 uint8_t clk
[] = {8,16,32,40,50,64,100,128,0};
1489 uint16_t rfLens
[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
1490 uint8_t rfCnts
[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
1491 uint8_t rfLensFnd
= 0;
1492 uint8_t lastFCcnt
=0;
1493 uint32_t fcCounter
= 0;
1494 uint16_t rfCounter
= 0;
1495 uint8_t firstBitFnd
= 0;
1497 if (size
== 0) return 0;
1499 uint8_t fcTol
= (uint8_t)(0.5+(float)(fcHigh
-fcLow
)/2);
1504 //PrintAndLog("DEBUG: fcTol: %d",fcTol);
1505 // prime i to first up transition
1506 for (i
= 1; i
< size
-1; i
++)
1507 if (BitStream
[i
] > BitStream
[i
-1] && BitStream
[i
]>=BitStream
[i
+1])
1510 for (; i
< size
-1; i
++){
1511 if (BitStream
[i
] > BitStream
[i
-1] && BitStream
[i
]>=BitStream
[i
+1]){
1515 // if we got less than the small fc + tolerance then set it to the small fc
1516 if (fcCounter
< fcLow
+fcTol
)
1518 else //set it to the large fc
1521 //look for bit clock (rf/xx)
1522 if ((fcCounter
<lastFCcnt
|| fcCounter
>lastFCcnt
)){
1523 //not the same size as the last wave - start of new bit sequence
1525 if (firstBitFnd
>1){ //skip first wave change - probably not a complete bit
1526 for (int ii
=0; ii
<15; ii
++){
1527 if (rfLens
[ii
]==rfCounter
){
1533 if (rfCounter
>0 && rfLensFnd
<15){
1534 //PrintAndLog("DEBUG: rfCntr %d, fcCntr %d",rfCounter,fcCounter);
1535 rfCnts
[rfLensFnd
]++;
1536 rfLens
[rfLensFnd
++]=rfCounter
;
1542 lastFCcnt
=fcCounter
;
1551 uint8_t rfHighest
=15, rfHighest2
=15, rfHighest3
=15;
1553 for (i
=0; i
<15; i
++){
1554 //PrintAndLog("DEBUG: RF %d, cnts %d",rfLens[i], rfCnts[i]);
1555 //get highest 2 RF values (might need to get more values to compare or compare all?)
1556 if (rfCnts
[i
]>rfCnts
[rfHighest
]){
1557 rfHighest3
=rfHighest2
;
1558 rfHighest2
=rfHighest
;
1560 } else if(rfCnts
[i
]>rfCnts
[rfHighest2
]){
1561 rfHighest3
=rfHighest2
;
1563 } else if(rfCnts
[i
]>rfCnts
[rfHighest3
]){
1567 // set allowed clock remainder tolerance to be 1 large field clock length+1
1568 // we could have mistakenly made a 9 a 10 instead of an 8 or visa versa so rfLens could be 1 FC off
1569 uint8_t tol1
= fcHigh
+1;
1571 //PrintAndLog("DEBUG: hightest: 1 %d, 2 %d, 3 %d",rfLens[rfHighest],rfLens[rfHighest2],rfLens[rfHighest3]);
1573 // loop to find the highest clock that has a remainder less than the tolerance
1574 // compare samples counted divided by
1576 for (; ii
>=0; ii
--){
1577 if (rfLens
[rfHighest
] % clk
[ii
] < tol1
|| rfLens
[rfHighest
] % clk
[ii
] > clk
[ii
]-tol1
){
1578 if (rfLens
[rfHighest2
] % clk
[ii
] < tol1
|| rfLens
[rfHighest2
] % clk
[ii
] > clk
[ii
]-tol1
){
1579 if (rfLens
[rfHighest3
] % clk
[ii
] < tol1
|| rfLens
[rfHighest3
] % clk
[ii
] > clk
[ii
]-tol1
){
1586 if (ii
<0) return 0; // oops we went too far
1592 //countFC is to detect the field clock lengths.
1593 //counts and returns the 2 most common wave lengths
1594 //mainly used for FSK field clock detection
1595 uint16_t countFC(uint8_t *BitStream
, size_t size
, uint8_t *mostFC
)
1597 uint8_t fcLens
[] = {0,0,0,0,0,0,0,0,0,0};
1598 uint16_t fcCnts
[] = {0,0,0,0,0,0,0,0,0,0};
1599 uint8_t fcLensFnd
= 0;
1600 uint8_t lastFCcnt
=0;
1601 uint32_t fcCounter
= 0;
1603 if (size
== 0) return 0;
1605 // prime i to first up transition
1606 for (i
= 1; i
< size
-1; i
++)
1607 if (BitStream
[i
] > BitStream
[i
-1] && BitStream
[i
] >= BitStream
[i
+1])
1610 for (; i
< size
-1; i
++){
1611 if (BitStream
[i
] > BitStream
[i
-1] && BitStream
[i
] >= BitStream
[i
+1]){
1612 // new up transition
1615 //if we had 5 and now have 9 then go back to 8 (for when we get a fc 9 instead of an 8)
1616 if (lastFCcnt
==5 && fcCounter
==9) fcCounter
--;
1617 //if odd and not rc/5 add one (for when we get a fc 9 instead of 10)
1618 if ((fcCounter
==9 && fcCounter
& 1) || fcCounter
==4) fcCounter
++;
1620 // save last field clock count (fc/xx)
1621 // find which fcLens to save it to:
1622 for (int ii
=0; ii
<10; ii
++){
1623 if (fcLens
[ii
]==fcCounter
){
1629 if (fcCounter
>0 && fcLensFnd
<10){
1631 fcCnts
[fcLensFnd
]++;
1632 fcLens
[fcLensFnd
++]=fcCounter
;
1641 uint8_t best1
=9, best2
=9, best3
=9;
1643 // go through fclens and find which ones are bigest 2
1644 for (i
=0; i
<10; i
++){
1645 // PrintAndLog("DEBUG: FC %d, Cnt %d, Errs %d",fcLens[i],fcCnts[i],errCnt);
1646 // get the 3 best FC values
1647 if (fcCnts
[i
]>maxCnt1
) {
1652 } else if(fcCnts
[i
]>fcCnts
[best2
]){
1655 } else if(fcCnts
[i
]>fcCnts
[best3
]){
1659 uint8_t fcH
=0, fcL
=0;
1660 if (fcLens
[best1
]>fcLens
[best2
]){
1668 *mostFC
=fcLens
[best1
];
1669 // TODO: take top 3 answers and compare to known Field clocks to get top 2
1671 uint16_t fcs
= (((uint16_t)fcH
)<<8) | fcL
;
1672 // PrintAndLog("DEBUG: Best %d best2 %d best3 %d",fcLens[best1],fcLens[best2],fcLens[best3]);
1678 //countPSK_FC is to detect the psk carrier clock length.
1679 //counts and returns the 1 most common wave length
1680 uint8_t countPSK_FC(uint8_t *BitStream
, size_t size
)
1682 uint8_t fcLens
[] = {0,0,0,0,0,0,0,0,0,0};
1683 uint16_t fcCnts
[] = {0,0,0,0,0,0,0,0,0,0};
1684 uint8_t fcLensFnd
= 0;
1685 uint32_t fcCounter
= 0;
1687 if (size
== 0) return 0;
1689 // prime i to first up transition
1690 for (i
= 1; i
< size
-1; i
++)
1691 if (BitStream
[i
] > BitStream
[i
-1] && BitStream
[i
] >= BitStream
[i
+1])
1694 for (; i
< size
-1; i
++){
1695 if (BitStream
[i
] > BitStream
[i
-1] && BitStream
[i
] >= BitStream
[i
+1]){
1696 // new up transition
1699 // save last field clock count (fc/xx)
1700 // find which fcLens to save it to:
1701 for (int ii
=0; ii
<10; ii
++){
1702 if (fcLens
[ii
]==fcCounter
){
1708 if (fcCounter
>0 && fcLensFnd
<10){
1710 fcCnts
[fcLensFnd
]++;
1711 fcLens
[fcLensFnd
++]=fcCounter
;
1722 // go through fclens and find which ones are bigest
1723 for (i
=0; i
<10; i
++){
1724 //PrintAndLog("DEBUG: FC %d, Cnt %d",fcLens[i],fcCnts[i]);
1725 // get the best FC value
1726 if (fcCnts
[i
]>maxCnt1
) {
1731 return fcLens
[best1
];
1734 //by marshmellow - demodulate PSK1 wave
1735 //uses wave lengths (# Samples)
1736 int pskRawDemod(uint8_t dest
[], size_t *size
, int *clock
, int *invert
)
1738 uint16_t loopCnt
= 4096; //don't need to loop through entire array...
1739 if (size
== 0) return -1;
1740 if (*size
<loopCnt
) loopCnt
= *size
;
1742 uint8_t curPhase
= *invert
;
1743 size_t i
, waveStart
=1, waveEnd
=0, firstFullWave
=0, lastClkBit
=0;
1744 uint8_t fc
=0, fullWaveLen
=0, tol
=1;
1745 uint16_t errCnt
=0, waveLenCnt
=0;
1746 fc
= countPSK_FC(dest
, *size
);
1747 if (fc
!=2 && fc
!=4 && fc
!=8) return -1;
1748 //PrintAndLog("DEBUG: FC: %d",fc);
1749 *clock
= DetectPSKClock(dest
, *size
, *clock
);
1750 if (*clock
==0) return -1;
1751 int avgWaveVal
=0, lastAvgWaveVal
=0;
1752 //find first phase shift
1753 for (i
=0; i
<loopCnt
; i
++){
1754 if (dest
[i
]+fc
< dest
[i
+1] && dest
[i
+1] >= dest
[i
+2]){
1756 //PrintAndLog("DEBUG: waveEnd: %d",waveEnd);
1757 waveLenCnt
= waveEnd
-waveStart
;
1758 if (waveLenCnt
> fc
&& waveStart
> fc
){ //not first peak and is a large wave
1759 lastAvgWaveVal
= avgWaveVal
/(waveLenCnt
);
1760 firstFullWave
= waveStart
;
1761 fullWaveLen
=waveLenCnt
;
1762 //if average wave value is > graph 0 then it is an up wave or a 1
1763 if (lastAvgWaveVal
> 123) curPhase
^=1; //fudge graph 0 a little 123 vs 128
1769 avgWaveVal
+=dest
[i
+2];
1771 //PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);
1772 lastClkBit
= firstFullWave
; //set start of wave as clock align
1773 //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d", *clock, lastClkBit);
1778 memset(dest
,curPhase
^1,firstFullWave
/ *clock
);
1779 numBits
+= (firstFullWave
/ *clock
);
1780 dest
[numBits
++] = curPhase
; //set first read bit
1781 for (i
= firstFullWave
+fullWaveLen
-1; i
< *size
-3; i
++){
1782 //top edge of wave = start of new wave
1783 if (dest
[i
]+fc
< dest
[i
+1] && dest
[i
+1] >= dest
[i
+2]){
1784 if (waveStart
== 0) {
1787 avgWaveVal
= dest
[i
+1];
1790 waveLenCnt
= waveEnd
-waveStart
;
1791 lastAvgWaveVal
= avgWaveVal
/waveLenCnt
;
1792 if (waveLenCnt
> fc
){
1793 //PrintAndLog("DEBUG: avgWaveVal: %d, waveSum: %d",lastAvgWaveVal,avgWaveVal);
1794 //if this wave is a phase shift
1795 //PrintAndLog("DEBUG: phase shift at: %d, len: %d, nextClk: %d, i: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+*clock-tol,i+1,fc);
1796 if (i
+1 >= lastClkBit
+ *clock
- tol
){ //should be a clock bit
1798 dest
[numBits
++] = curPhase
;
1799 lastClkBit
+= *clock
;
1800 } else if (i
<lastClkBit
+10+fc
){
1801 //noise after a phase shift - ignore
1802 } else { //phase shift before supposed to based on clock
1804 dest
[numBits
++] = 77;
1806 } else if (i
+1 > lastClkBit
+ *clock
+ tol
+ fc
){
1807 lastClkBit
+= *clock
; //no phase shift but clock bit
1808 dest
[numBits
++] = curPhase
;
1814 avgWaveVal
+=dest
[i
+1];