1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // Low frequency demod/decode commands
19 // LF Demod functions are placed here to allow the flexibility to use client or
20 // device side. Most BUT NOT ALL of these functions are currenlty safe for
21 // device side use currently. (DetectST for example...)
23 // There are likely many improvements to the code that could be made, please
24 // make suggestions...
26 // we tried to include author comments so any questions could be directed to
29 // There are 4 main sections of code below:
32 // for general utilities used by multiple other functions
34 // Clock / Bitrate Detection Section:
35 // for clock detection functions for each modulation
37 // Modulation Demods &/or Decoding Section:
38 // for main general modulation demodulating and encoding decoding code.
40 // Tag format detection section:
41 // for detection of specific tag formats within demodulated data
44 //-----------------------------------------------------------------------------
47 #include <string.h> // for memset, memcmp and size_t
48 #include <stdlib.h> // qsort
49 #include "parity.h" // for parity test
50 #include "pm3_cmd.h" // error codes
51 #include "commonutil.h" // Arraylen
53 // **********************************************************************************************
54 // ---------------------------------Utilities Section--------------------------------------------
55 // **********************************************************************************************
56 #define LOWEST_DEFAULT_CLOCK 32
57 #define FSK_PSK_THRESHOLD 123
59 //to allow debug print calls when used not on dev
65 # define prnt(args...) PrintAndLogEx(DEBUG, ## args );
68 uint8_t g_debugMode
= 0;
69 # define prnt Dbprintf
72 static signal_t signalprop
= { 255, -255, 0, 0, true };
73 signal_t
*getSignalProperties(void) {
77 static void resetSignal(void) {
79 signalprop
.high
= -255;
81 signalprop
.amplitude
= 0;
82 signalprop
.isnoise
= true;
85 static void printSignal(void) {
86 prnt("LF signal properties:");
87 prnt(" high..........%d", signalprop
.high
);
88 prnt(" low...........%d", signalprop
.low
);
89 prnt(" mean..........%d", signalprop
.mean
);
90 prnt(" amplitude.....%d", signalprop
.amplitude
);
91 prnt(" is Noise......%s", (signalprop
.isnoise
) ? _RED_("Yes") : _GREEN_("No"));
92 prnt(" THRESHOLD noise amplitude......%d", NOISE_AMPLITUDE_THRESHOLD
);
96 static int cmp_uint8(const void *a
, const void *b
) {
97 if (*(const uint8_t *)a
< * (const uint8_t *)b
)
100 return *(const uint8_t *)a
> *(const uint8_t *)b
;
104 void computeSignalProperties(const uint8_t *samples
, uint32_t size
) {
107 if (samples
== NULL
|| size
< SIGNAL_MIN_SAMPLES
) return;
110 uint32_t offset_size
= size
- SIGNAL_IGNORE_FIRST_SAMPLES
;
113 uint8_t tmp
[offset_size
];
114 memcpy(tmp
, samples
+ SIGNAL_IGNORE_FIRST_SAMPLES
, sizeof(tmp
));
115 qsort(tmp
, sizeof(tmp
), sizeof(uint8_t), cmp_uint8
);
117 uint8_t low10
= 0.5 * (tmp
[(int)(offset_size
* 0.1)] + tmp
[(int)((offset_size
- 1) * 0.1)]);
118 uint8_t hi90
= 0.5 * (tmp
[(int)(offset_size
* 0.9)] + tmp
[(int)((offset_size
- 1) * 0.9)]);
120 for (uint32_t i
= SIGNAL_IGNORE_FIRST_SAMPLES
; i
< size
; i
++) {
122 if (samples
[i
] < signalprop
.low
) signalprop
.low
= samples
[i
];
123 if (samples
[i
] > signalprop
.high
) signalprop
.high
= samples
[i
];
125 if (samples
[i
] < low10
|| samples
[i
] > hi90
)
132 signalprop
.mean
= sum
/ cnt
;
136 for (uint32_t i
= SIGNAL_IGNORE_FIRST_SAMPLES
; i
< size
; i
++) {
137 if (samples
[i
] < signalprop
.low
) signalprop
.low
= samples
[i
];
138 if (samples
[i
] > signalprop
.high
) signalprop
.high
= samples
[i
];
141 signalprop
.mean
= sum
/ offset_size
;
144 // measure amplitude of signal
145 signalprop
.amplitude
= signalprop
.high
- signalprop
.mean
;
146 // By measuring mean and look at amplitude of signal from HIGH / LOW,
147 // we can detect noise
148 signalprop
.isnoise
= signalprop
.amplitude
< NOISE_AMPLITUDE_THRESHOLD
;
154 void removeSignalOffset(uint8_t *samples
, uint32_t size
) {
155 if (samples
== NULL
|| size
< SIGNAL_MIN_SAMPLES
) {
160 uint32_t offset_size
= size
- SIGNAL_IGNORE_FIRST_SAMPLES
;
164 uint8_t tmp
[offset_size
];
165 memcpy(tmp
, samples
+ SIGNAL_IGNORE_FIRST_SAMPLES
, sizeof(tmp
));
166 qsort(tmp
, sizeof(tmp
), sizeof(uint8_t), cmp_uint8
);
168 uint8_t low10
= 0.5 * (tmp
[(int)(offset_size
* 0.05)] + tmp
[(int)((offset_size
- 1) * 0.05)]);
169 uint8_t hi90
= 0.5 * (tmp
[(int)(offset_size
* 0.95)] + tmp
[(int)((offset_size
- 1) * 0.95)]);
171 for (uint32_t i
= SIGNAL_IGNORE_FIRST_SAMPLES
; i
< size
; i
++) {
173 if (samples
[i
] < low10
|| samples
[i
] > hi90
)
176 acc_off
+= samples
[i
] - 128;
184 for (uint32_t i
= SIGNAL_IGNORE_FIRST_SAMPLES
; i
< size
; i
++)
185 acc_off
+= samples
[i
] - 128;
187 acc_off
/= (int)offset_size
;
190 // shift and saturate samples to center the mean
191 for (uint32_t i
= 0; i
< size
; i
++) {
193 samples
[i
] = (samples
[i
] >= acc_off
) ? samples
[i
] - acc_off
: 0;
196 samples
[i
] = (255 - samples
[i
] >= -acc_off
) ? samples
[i
] - acc_off
: 255;
201 // 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
202 // void getHiLo(uint8_t *bits, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo) {
203 void getHiLo(int *high
, int *low
, uint8_t fuzzHi
, uint8_t fuzzLo
) {
205 *high
= (signalprop
.high
* fuzzHi
) / 100;
206 if (signalprop
.low
< 0) {
207 *low
= (signalprop
.low
* fuzzLo
) / 100;
209 uint8_t range
= signalprop
.high
- signalprop
.low
;
211 *low
= signalprop
.low
+ ((range
* (100 - fuzzLo
)) / 100);
214 // if fuzzing to great and overlap
216 *high
= signalprop
.high
;
217 *low
= signalprop
.low
;
220 // prnt("getHiLo fuzzed: High %d | Low %d", *high, *low);
223 // pass bits to be tested in bits, length bits passed in bitLen, and parity type (even=0 | odd=1) in pType
224 // returns 1 if passed
225 bool parityTest(uint32_t bits
, uint8_t bitLen
, uint8_t pType
) {
226 return oddparity32(bits
) ^ pType
;
229 // takes a array of binary values, start position, length of bits per parity (includes parity bit - MAX 32),
230 // Parity Type (1 for odd; 0 for even; 2 for Always 1's; 3 for Always 0's), and binary Length (length to run)
231 size_t removeParity(uint8_t *bits
, size_t startIdx
, uint8_t pLen
, uint8_t pType
, size_t bLen
) {
232 uint32_t parityWd
= 0;
234 for (int word
= 0; word
< (bLen
); word
+= pLen
) {
235 for (int bit
= 0; bit
< pLen
; bit
++) {
236 if (word
+ bit
>= bLen
) break;
237 parityWd
= (parityWd
<< 1) | bits
[startIdx
+ word
+ bit
];
238 bits
[bitCnt
++] = (bits
[startIdx
+ word
+ bit
]);
240 if (word
+ pLen
> bLen
) break;
242 bitCnt
--; // overwrite parity with next data
243 // if parity fails then return 0
246 if (bits
[bitCnt
] == 1) {
249 break; // should be 0 spacer bit
251 if (bits
[bitCnt
] == 0) {
254 break; // should be 1 spacer bit
256 if (parityTest(parityWd
, pLen
, pType
) == 0) { return 0; }
257 break; // test parity
261 // if we got here then all the parities passed
266 static size_t removeEm410xParity(uint8_t *bits
, size_t startIdx
, size_t *size
, bool *validShort
, bool *validShortExtended
, bool *validLong
) {
267 uint32_t parityWd
= 0;
269 bool validColParity
= false;
270 bool validRowParity
= true;
271 bool validRowParitySkipColP
= true;
273 *validShortExtended
= false;
289 uint16_t parityCol
[4] = { 0, 0, 0, 0 };
291 for (int word
= 0; word
< blen
; word
+= 5) {
292 for (int bit
= 0; bit
< 5; bit
++) {
294 if (word
+ bit
>= blen
) {
297 parityWd
= (parityWd
<< 1) | bits
[startIdx
+ word
+ bit
];
299 if ((word
<= 50) && (bit
< 4)) {
300 parityCol
[bit
] = (parityCol
[bit
] << 1) | bits
[startIdx
+ word
+ bit
];
303 bits
[bitCnt
++] = (bits
[startIdx
+ word
+ bit
]);
306 if (word
+ 5 > blen
) break;
308 bitCnt
--; // overwrite parity with next data
309 validRowParity
&= parityTest(parityWd
, 5, 0) != 0;
311 if (word
== 50) { // column parity nibble on short EM and on Electra
312 validColParity
= parityTest(parityCol
[0], 11, 0) != 0;
313 validColParity
&= parityTest(parityCol
[1], 11, 0) != 0;
314 validColParity
&= parityTest(parityCol
[2], 11, 0) != 0;
315 validColParity
&= parityTest(parityCol
[3], 11, 0) != 0;
317 validRowParitySkipColP
&= parityTest(parityWd
, 5, 0) != 0;
322 if ((blen
!= 128) && validRowParitySkipColP
&& validColParity
) {
326 if ((blen
== 128) && validRowParity
) {
330 if ((blen
== 128) && validRowParitySkipColP
&& validColParity
) {
331 *validShortExtended
= true;
334 if (*validShort
|| *validShortExtended
|| *validLong
) {
341 // takes a array of binary values, length of bits per parity (includes parity bit),
342 // Parity Type (1 for odd; 0 for even; 2 Always 1's; 3 Always 0's), and binary Length (length to run)
343 // Make sure *dest is long enough to store original sourceLen + #_of_parities_to_be_added
344 size_t addParity(const uint8_t *src
, uint8_t *dest
, uint8_t sourceLen
, uint8_t pLen
, uint8_t pType
) {
345 uint32_t parityWd
= 0;
346 size_t j
= 0, bitCnt
= 0;
347 for (int word
= 0; word
< sourceLen
; word
+= pLen
- 1) {
348 for (int bit
= 0; bit
< pLen
- 1; bit
++) {
349 parityWd
= (parityWd
<< 1) | src
[word
+ bit
];
350 dest
[j
++] = (src
[word
+ bit
]);
352 // if parity fails then return 0
356 break; // marker bit which should be a 0
359 break; // marker bit which should be a 1
361 dest
[j
++] = parityTest(parityWd
, pLen
- 1, pType
) ^ 1;
367 // if we got here then all the parities passed
368 //return ID start index and size
372 // array must be size dividable with 8
373 int bits_to_array(const uint8_t *bits
, size_t size
, uint8_t *dest
) {
374 if ((size
== 0) || (size
% 8) != 0) return PM3_EINVARG
;
376 for (uint32_t i
= 0; i
< (size
/ 8); i
++)
377 dest
[i
] = bytebits_to_byte((uint8_t *) bits
+ (i
* 8), 8);
382 uint32_t bytebits_to_byte(uint8_t *src
, size_t numbits
) {
384 for (int i
= 0 ; i
< numbits
; i
++) {
385 num
= (num
<< 1) | (*src
);
391 // least significant bit first
392 uint32_t bytebits_to_byteLSBF(uint8_t *src
, size_t numbits
) {
394 for (int i
= 0 ; i
< numbits
; i
++) {
395 num
= (num
<< 1) | *(src
+ (numbits
- (i
+ 1)));
400 // search for given preamble in given BitStream and return success = TRUE or fail = FALSE and startIndex and length
401 bool preambleSearch(uint8_t *bits
, uint8_t *preamble
, size_t pLen
, size_t *size
, size_t *startIdx
) {
402 return preambleSearchEx(bits
, preamble
, pLen
, size
, startIdx
, false);
404 // search for given preamble in given BitStream and return success=1 or fail=0 and startIndex (where it was found) and length if not fineone
405 // fineone does not look for a repeating preamble for em4x05/4x69 sends preamble once, so look for it once in the first pLen bits
406 // (iceman) FINDONE, only finds start index. NOT SIZE!. I see Em410xDecode (lfdemod.c) uses SIZE to determine success
407 bool preambleSearchEx(uint8_t *bits
, uint8_t *preamble
, size_t pLen
, size_t *size
, size_t *startIdx
, bool findone
) {
408 // Sanity check. If preamble length is bigger than bits length.
412 uint8_t foundCnt
= 0;
413 for (size_t idx
= 0; idx
< *size
- pLen
; idx
++) {
414 if (memcmp(bits
+ idx
, preamble
, pLen
) == 0) {
418 if (g_debugMode
>= 1) prnt("DEBUG: (preambleSearchEx) preamble found at %zu", idx
);
424 if (g_debugMode
>= 1) prnt("DEBUG: (preambleSearchEx) preamble 2 found at %zu", idx
);
425 *size
= idx
- *startIdx
;
430 return (foundCnt
> 0);
433 // find start of modulating data (for fsk and psk) in case of beginning noise or slow chip startup.
434 static size_t findModStart(const uint8_t *src
, size_t size
, uint8_t expWaveSize
) {
437 size_t waveSizeCnt
= 0;
438 uint8_t thresholdCnt
= 0;
440 // FSK_PSK_THRESHOLD;
441 bool isAboveThreshold
= (src
[i
++] >= signalprop
.mean
);
443 for (; i
< size
- 20; i
++) {
444 if (src
[i
] < signalprop
.mean
&& isAboveThreshold
) {
446 if (thresholdCnt
> 2 && waveSizeCnt
< expWaveSize
+ 1) {
449 isAboveThreshold
= false;
452 } else if (src
[i
] >= signalprop
.mean
&& !isAboveThreshold
) {
454 if (thresholdCnt
> 2 && waveSizeCnt
< expWaveSize
+ 1) {
457 isAboveThreshold
= true;
464 if (thresholdCnt
> 10) {
469 if (g_debugMode
== 2) {
470 prnt("DEBUG: threshold Count reached at index %zu, count: %u", i
, thresholdCnt
);
475 static int getClosestClock(int testclk
) {
476 const uint16_t clocks
[] = {8, 16, 32, 40, 50, 64, 100, 128, 256, 272, 384};
477 const uint8_t limit
[] = {1, 2, 4, 4, 5, 8, 8, 8, 8, 24, 24};
479 for (uint8_t i
= 0; i
< ARRAYLEN(clocks
); i
++) {
480 if (testclk
>= clocks
[i
] - limit
[i
] && testclk
<= clocks
[i
] + limit
[i
])
486 void getNextLow(const uint8_t *samples
, size_t size
, int low
, size_t *i
) {
487 while ((samples
[*i
] > low
) && (*i
< size
))
491 void getNextHigh(const uint8_t *samples
, size_t size
, int high
, size_t *i
) {
492 while ((samples
[*i
] < high
) && (*i
< size
))
496 // load wave counters
497 bool loadWaveCounters(uint8_t *samples
, size_t size
, int lowToLowWaveLen
[], int highToLowWaveLen
[], int *waveCnt
, int *skip
, int *minClk
, int *high
, int *low
) {
499 //size_t testsize = (size < 512) ? size : 512;
501 // just noise - no super good detection. good enough
502 if (signalprop
.isnoise
) {
503 if (g_debugMode
== 2) prnt("DEBUG STT: just noise detected - quitting");
507 getHiLo(high
, low
, 80, 80);
509 // get to first full low to prime loop and skip incomplete first pulse
510 getNextHigh(samples
, size
, *high
, &i
);
511 getNextLow(samples
, size
, *low
, &i
);
514 // populate tmpbuff buffer with pulse lengths
516 // measure from low to low
518 //find first high point for this wave
519 getNextHigh(samples
, size
, *high
, &i
);
520 size_t firstHigh
= i
;
522 getNextLow(samples
, size
, *low
, &i
);
524 if (*waveCnt
>= (size
/ LOWEST_DEFAULT_CLOCK
))
527 highToLowWaveLen
[*waveCnt
] = i
- firstHigh
; //first high to first low
528 lowToLowWaveLen
[*waveCnt
] = i
- firstLow
;
530 if (i
- firstLow
< *minClk
&& i
< size
) {
531 *minClk
= i
- firstLow
;
537 size_t pskFindFirstPhaseShift(const uint8_t *samples
, size_t size
, uint8_t *curPhase
, size_t waveStart
, uint16_t fc
, uint16_t *fullWaveLen
) {
538 uint16_t loopCnt
= (size
+ 3 < 4096) ? size
: 4096; //don't need to loop through entire array...
540 uint16_t avgWaveVal
= 0, lastAvgWaveVal
;
541 size_t i
= waveStart
, waveEnd
, waveLenCnt
, firstFullWave
;
542 for (; i
< loopCnt
; i
++) {
543 // find peak // was "samples[i] + fc" but why? must have been used to weed out some wave error... removed..
544 if (samples
[i
] < samples
[i
+ 1] && samples
[i
+ 1] >= samples
[i
+ 2]) {
546 if (g_debugMode
== 2) prnt("DEBUG PSK: waveEnd: %zu, waveStart: %zu", waveEnd
, waveStart
);
547 waveLenCnt
= waveEnd
- waveStart
;
548 if (waveLenCnt
> fc
&& waveStart
> fc
&& !(waveLenCnt
> fc
+ 8)) { //not first peak and is a large wave but not out of whack
549 lastAvgWaveVal
= avgWaveVal
/ (waveLenCnt
);
550 firstFullWave
= waveStart
;
551 *fullWaveLen
= waveLenCnt
;
552 //if average wave value is > graph 0 then it is an up wave or a 1 (could cause inverting)
553 if (lastAvgWaveVal
> FSK_PSK_THRESHOLD
) *curPhase
^= 1;
554 return firstFullWave
;
559 avgWaveVal
+= samples
[i
+ 2];
564 // amplify based on ask edge detection - not accurate enough to use all the time
565 void askAmp(uint8_t *bits
, size_t size
) {
567 for (size_t i
= 1; i
< size
; ++i
) {
568 if (bits
[i
] - bits
[i
- 1] >= 30) //large jump up
570 else if (bits
[i
- 1] - bits
[i
] >= 20) //large jump down
577 // iceman, simplify this
578 uint32_t manchesterEncode2Bytes(uint16_t datain
) {
580 for (uint8_t i
= 0; i
< 16; i
++) {
581 uint8_t b
= (datain
>> (15 - i
) & 1);
582 output
|= (1 << (((15 - i
) * 2) + b
));
587 void manchesterEncodeUint32(uint32_t data_in
, uint8_t bitlen_in
, uint8_t *bits_out
, uint16_t *index
) {
588 for (int i
= bitlen_in
- 1; i
>= 0; i
--) {
589 if ((data_in
>> i
) & 1) {
590 bits_out
[(*index
)++] = 1;
591 bits_out
[(*index
)++] = 0;
593 bits_out
[(*index
)++] = 0;
594 bits_out
[(*index
)++] = 1;
599 // encode binary data into binary manchester
600 // NOTE: bitstream must have triple the size of "size" available in memory to do the swap
601 int ManchesterEncode(uint8_t *bits
, size_t size
) {
602 //allow up to 4096b out (means bits must be at least 2048+4096 to handle the swap)
603 size
= (size
> 2048) ? 2048 : size
;
604 size_t modIdx
= size
;
606 for (size_t idx
= 0; idx
< size
; idx
++) {
607 bits
[idx
+ modIdx
++] = bits
[idx
];
608 bits
[idx
+ modIdx
++] = bits
[idx
] ^ 1;
610 for (i
= 0; i
< (size
* 2); i
++) {
611 bits
[i
] = bits
[i
+ size
];
616 // to detect a wave that has heavily clipped (clean) samples
617 // loop 1024 samples, if 250 of them is deemed maxed out, we assume the wave is clipped.
618 bool DetectCleanAskWave(const uint8_t *dest
, size_t size
, uint8_t high
, uint8_t low
) {
619 bool allArePeaks
= true;
620 uint16_t cntPeaks
= 0;
621 size_t loopEnd
= 1024 + 160;
624 if (loopEnd
> size
) loopEnd
= size
;
626 for (size_t i
= 160; i
< loopEnd
; i
++) {
628 if (dest
[i
] > low
&& dest
[i
] < high
)
632 //if (g_debugMode == 2) prnt("DEBUG DetectCleanAskWave: peaks (200) %u", cntPeaks);
633 if (cntPeaks
> 200) return true;
637 if (allArePeaks
== false) {
638 if (g_debugMode
== 2) prnt("DEBUG DetectCleanAskWave: peaks (200) %u", cntPeaks
);
639 if (cntPeaks
> 200) return true;
645 // **********************************************************************************************
646 // -------------------Clock / Bitrate Detection Section------------------------------------------
647 // **********************************************************************************************
649 // to help detect clocks on heavily clipped samples
650 // based on count of low to low
651 int DetectStrongAskClock(uint8_t *dest
, size_t size
, int high
, int low
, int *clock
) {
654 uint16_t shortestWaveIdx
= 0;
656 // get to first full low to prime loop and skip incomplete first pulse
657 getNextHigh(dest
, size
, high
, &i
);
658 getNextLow(dest
, size
, low
, &i
);
665 // clock, numoftimes, first idx
666 uint16_t tmpclk
[11][3] = {
680 // loop through all samples (well, we don't want to go out-of-bounds)
681 while (i
< (size
- 768)) {
682 // measure from low to low
683 size_t startwave
= i
;
685 getNextHigh(dest
, size
, high
, &i
);
686 getNextLow(dest
, size
, low
, &i
);
688 //get minimum measured distance
689 if (i
- startwave
< minClk
&& i
< size
) {
690 minClk
= i
- startwave
;
691 shortestWaveIdx
= startwave
;
694 int foo
= getClosestClock(minClk
);
696 for (uint8_t j
= 0; j
< 11; j
++) {
697 if (tmpclk
[j
][0] == foo
) {
700 if (tmpclk
[j
][2] == 0) {
701 tmpclk
[j
][2] = shortestWaveIdx
;
709 // find the clock with most hits and it the first index it was encountered.
710 int possible_clks
= 0;
711 for (uint8_t j
= 0; j
< 11; j
++) {
712 if (tmpclk
[j
][1] > 0) {
717 uint16_t second_shortest
= 0;
720 for (int j
= 10; j
> -1; j
--) {
721 if (g_debugMode
== 2) {
722 prnt("DEBUG, ASK, clocks %u | hits %u | idx %u"
729 if (max
< tmpclk
[j
][1]) {
731 second_shortest
= shortestWaveIdx
;
733 *clock
= tmpclk
[j
][0];
734 shortestWaveIdx
= tmpclk
[j
][2];
739 // ASK clock 8 is very rare and usually gives us false positives
740 if (possible_clks
> 1 && *clock
== 8) {
742 shortestWaveIdx
= second_shortest
;
748 return shortestWaveIdx
;
751 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
752 // maybe somehow adjust peak trimming value based on samples to fix?
753 // return start index of best starting position for that clock and return clock (by reference)
754 int DetectASKClock(uint8_t *dest
, size_t size
, int *clock
, int maxErr
) {
756 //don't need to loop through entire array. (cotag has clock of 384)
757 uint16_t loopCnt
= 1000;
759 // not enough samples
760 if (size
<= loopCnt
+ 60) {
761 if (g_debugMode
== 2) prnt("DEBUG DetectASKClock: not enough samples - aborting");
765 // just noise - no super good detection. good enough
766 if (signalprop
.isnoise
) {
767 if (g_debugMode
== 2) prnt("DEBUG DetectASKClock: just noise detected - aborting");
772 uint8_t num_clks
= 10;
773 // first 255 value pos0 is placeholder for user inputed clock.
774 uint16_t clk
[] = {255, 8, 16, 32, 40, 50, 64, 100, 128, 255, 272};
776 // sometimes there is a strange end wave - filter out this
780 // already have a valid clock?
781 uint8_t found_clk
= 0;
782 for (; i
< num_clks
; ++i
) {
783 if (clk
[i
] == *clock
) {
788 // threshold 75% of high, low peak
789 int peak_hi
, peak_low
;
790 getHiLo(&peak_hi
, &peak_low
, 75, 75);
792 // test for large clean, STRONG, CLIPPED peaks
796 if (DetectCleanAskWave(dest
, size
, peak_hi
, peak_low
)) {
798 int idx
= DetectStrongAskClock(dest
, size
, peak_hi
, peak_low
, clock
);
799 if (g_debugMode
== 2)
800 prnt("DEBUG ASK: DetectASKClock Clean ASK Wave detected: clk %i, Best Starting Position: %i", *clock
, idx
);
802 // return shortest wave start position
807 // test for weak peaks
809 // test clock if given as cmd parameter
815 uint16_t bestErr
[] = {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000};
816 uint8_t bestStart
[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
817 size_t errCnt
, arrLoc
, loopEnd
;
821 num_clks
= found_clk
+ 1;
826 //test each valid clock from smallest to greatest to see which lines up
827 for (; clkCnt
< num_clks
; clkCnt
++) {
828 if (clk
[clkCnt
] <= 32) {
833 //if no errors allowed - keep start within the first clock
834 if (!maxErr
&& size
> clk
[clkCnt
] * 2 + tol
&& clk
[clkCnt
] < 128)
835 loopCnt
= clk
[clkCnt
] * 2;
837 bestErr
[clkCnt
] = 1000;
839 //try lining up the peaks by moving starting point (try first few clocks)
841 // get to first full low to prime loop and skip incomplete first pulse
842 getNextHigh(dest
, size
, peak_hi
, &j
);
843 getNextLow(dest
, size
, peak_low
, &j
);
845 for (; j
< loopCnt
; j
++) {
847 // now that we have the first one lined up test rest of wave array
848 loopEnd
= ((size
- j
- tol
) / clk
[clkCnt
]) - 1;
849 for (i
= 0; i
< loopEnd
; ++i
) {
850 arrLoc
= j
+ (i
* clk
[clkCnt
]);
851 if (dest
[arrLoc
] >= peak_hi
|| dest
[arrLoc
] <= peak_low
) {
852 } else if (dest
[arrLoc
- tol
] >= peak_hi
|| dest
[arrLoc
- tol
] <= peak_low
) {
853 } else if (dest
[arrLoc
+ tol
] >= peak_hi
|| dest
[arrLoc
+ tol
] <= peak_low
) {
854 } else { //error no peak detected
858 // if we found no errors then we can stop here and a low clock (common clocks)
859 // this is correct one - return this clock
860 // if (g_debugMode == 2) prnt("DEBUG ASK: clk %d, err %d, startpos %d, endpos %d", clk[clkCnt], errCnt, j, i);
861 if (errCnt
== 0 && clkCnt
< 7) {
863 *clock
= clk
[clkCnt
];
866 // if we found errors see if it is lowest so far and save it as best run
867 if (errCnt
< bestErr
[clkCnt
]) {
868 bestErr
[clkCnt
] = errCnt
;
869 bestStart
[clkCnt
] = j
;
876 for (k
= 1; k
< num_clks
; ++k
) {
877 if (bestErr
[k
] < bestErr
[best
]) {
878 if (bestErr
[k
] == 0) bestErr
[k
] = 1;
879 // current best bit to error ratio vs new bit to error ratio
880 if ((size
/ clk
[best
]) / bestErr
[best
] < (size
/ clk
[k
]) / bestErr
[k
]) {
884 //if (g_debugMode == 2) prnt("DEBUG ASK: clk %d, # Errors %d, Current Best Clk %d, bestStart %d", clk[k], bestErr[k], clk[best], bestStart[best]);
888 for (i
= 0; i
< ARRAYLEN(bestErr
); i
++) {
889 chg
= (bestErr
[i
] != 1000);
892 chg
= (bestStart
[i
] != 0);
897 // just noise - no super good detection. good enough
899 if (g_debugMode
== 2) prnt("DEBUG DetectASKClock: no good values detected - aborting");
906 return bestStart
[best
];
909 int DetectStrongNRZClk(const uint8_t *dest
, size_t size
, int peak
, int low
, bool *strong
) {
910 //find shortest transition from high to low
913 size_t transition1
= 0;
914 int lowestTransition
= 255;
915 bool lastWasHigh
= false;
916 size_t transitionSampleCount
= 0;
917 //find first valid beginning of a high or low wave
918 while ((dest
[i
] >= peak
|| dest
[i
] <= low
) && (i
< size
))
920 while ((dest
[i
] < peak
&& dest
[i
] > low
) && (i
< size
))
923 lastWasHigh
= (dest
[i
] >= peak
);
930 for (; i
< size
; i
++) {
931 if ((dest
[i
] >= peak
&& !lastWasHigh
) || (dest
[i
] <= low
&& lastWasHigh
)) {
932 lastWasHigh
= (dest
[i
] >= peak
);
933 if (i
- transition1
< lowestTransition
)
934 lowestTransition
= i
- transition1
;
936 } else if (dest
[i
] < peak
&& dest
[i
] > low
) {
937 transitionSampleCount
++;
940 if (lowestTransition
== 255)
941 lowestTransition
= 0;
943 if (g_debugMode
== 2) prnt("DEBUG NRZ: detectstrongNRZclk smallest wave: %d", lowestTransition
);
944 // if less than 10% of the samples were not peaks (or 90% were peaks) then we have a strong wave
945 if (transitionSampleCount
/ size
< 10) {
947 lowestTransition
= getClosestClock(lowestTransition
);
949 return lowestTransition
;
952 // detect nrz clock by reading #peaks vs no peaks(or errors)
953 int DetectNRZClock(uint8_t *dest
, size_t size
, int clock
, size_t *clockStartIdx
) {
955 uint16_t clk
[] = {8, 16, 32, 40, 50, 64, 100, 128, 255, 272, 384};
956 size_t loopCnt
= 4096; //don't need to loop through entire array...
958 //if we already have a valid clock quit
959 for (; i
< ARRAYLEN(clk
); ++i
)
960 if (clk
[i
] == clock
) return clock
;
962 if (size
< 20) return 0;
963 // size must be larger than 20 here
964 if (size
< loopCnt
) loopCnt
= size
- 20;
967 // just noise - no super good detection. good enough
968 if (signalprop
.isnoise
) {
969 if (g_debugMode
== 2) prnt("DEBUG DetectNZRClock: just noise detected - quitting");
973 //get high and low peak
975 //getHiLo(dest, loopCnt, &peak, &low, 90, 90);
976 getHiLo(&peak
, &low
, 90, 90);
979 int lowestTransition
= DetectStrongNRZClk(dest
, size
- 20, peak
, low
, &strong
);
980 if (strong
) return lowestTransition
;
984 uint16_t smplCnt
= 0;
986 int16_t peaksdet
[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
987 uint16_t minPeak
= 255;
988 bool firstpeak
= true;
989 //test for large clipped waves - ignore first peak
990 for (i
= 0; i
< loopCnt
; i
++) {
991 if (dest
[i
] >= peak
|| dest
[i
] <= low
) {
992 if (firstpeak
) continue;
997 if (minPeak
> smplCnt
&& smplCnt
> 7) minPeak
= smplCnt
;
999 if (g_debugMode
== 2) prnt("DEBUG NRZ: minPeak: %d, smplCnt: %d, peakcnt: %d", minPeak
, smplCnt
, peakcnt
);
1004 if (minPeak
< 8) return 0;
1006 bool errBitHigh
= 0, bitHigh
= 0, lastPeakHigh
= 0;
1007 uint8_t ignoreCnt
= 0, ignoreWindow
= 4;
1009 size_t bestStart
[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1011 //test each valid clock from smallest to greatest to see which lines up
1012 for (clkCnt
= 0; clkCnt
< ARRAYLEN(bestStart
); ++clkCnt
) {
1013 //ignore clocks smaller than smallest peak
1014 if (clk
[clkCnt
] < minPeak
- (clk
[clkCnt
] / 4)) continue;
1015 //try lining up the peaks by moving starting point (try first 256)
1016 for (ii
= 20; ii
< loopCnt
; ++ii
) {
1017 if ((dest
[ii
] >= peak
) || (dest
[ii
] <= low
)) {
1021 lastBit
= ii
- clk
[clkCnt
];
1022 //loop through to see if this start location works
1023 for (i
= ii
; i
< size
- 20; ++i
) {
1024 //if we are at a clock bit
1025 if ((i
>= lastBit
+ clk
[clkCnt
] - tol
) && (i
<= lastBit
+ clk
[clkCnt
] + tol
)) {
1027 if (dest
[i
] >= peak
|| dest
[i
] <= low
) {
1028 //if same peak don't count it
1029 if ((dest
[i
] >= peak
&& !lastPeakHigh
) || (dest
[i
] <= low
&& lastPeakHigh
)) {
1032 lastPeakHigh
= (dest
[i
] >= peak
);
1035 ignoreCnt
= ignoreWindow
;
1036 lastBit
+= clk
[clkCnt
];
1037 } else if (i
== lastBit
+ clk
[clkCnt
] + tol
) {
1038 lastBit
+= clk
[clkCnt
];
1040 //else if not a clock bit and no peaks
1041 } else if (dest
[i
] < peak
&& dest
[i
] > low
) {
1042 if (ignoreCnt
== 0) {
1044 if (errBitHigh
== true)
1050 // else if not a clock bit but we have a peak
1051 } else if ((dest
[i
] >= peak
|| dest
[i
] <= low
) && (!bitHigh
)) {
1052 //error bar found no clock...
1056 if (peakcnt
> peaksdet
[clkCnt
]) {
1057 bestStart
[clkCnt
] = ii
;
1058 peaksdet
[clkCnt
] = peakcnt
;
1065 for (int m
= ARRAYLEN(peaksdet
) - 1; m
>= 0; m
--) {
1066 if ((peaksdet
[m
] >= (peaksdet
[best
] - 1)) && (peaksdet
[m
] <= peaksdet
[best
] + 1) && lowestTransition
) {
1067 if (clk
[m
] > (lowestTransition
- (clk
[m
] / 8)) && clk
[m
] < (lowestTransition
+ (clk
[m
] / 8))) {
1070 } else if (peaksdet
[m
] > peaksdet
[best
]) {
1073 if (g_debugMode
== 2) prnt("DEBUG NRZ: Clk: %d, peaks: %d, minPeak: %d, bestClk: %d, lowestTrs: %d", clk
[m
], peaksdet
[m
], minPeak
, clk
[best
], lowestTransition
);
1075 *clockStartIdx
= bestStart
[best
];
1079 // countFC is to detect the field clock lengths.
1080 // counts and returns the 2 most common wave lengths
1081 // mainly used for FSK field clock detection
1082 uint16_t countFC(const uint8_t *bits
, size_t size
, bool fskAdj
) {
1083 uint8_t fcLens
[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1084 uint16_t fcCnts
[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1085 uint8_t fcLensFnd
= 0;
1086 uint8_t lastFCcnt
= 0;
1087 uint8_t fcCounter
= 0;
1089 if (size
< 180) return 0;
1091 // prime i to first up transition
1092 for (i
= 160; i
< size
- 20; i
++)
1093 if (bits
[i
] > bits
[i
- 1] && bits
[i
] >= bits
[i
+ 1])
1096 for (; i
< size
- 20; i
++) {
1097 if (bits
[i
] > bits
[i
- 1] && bits
[i
] >= bits
[i
+ 1]) {
1098 // new up transition
1101 //if we had 5 and now have 9 then go back to 8 (for when we get a fc 9 instead of an 8)
1102 if (lastFCcnt
== 5 && fcCounter
== 9) fcCounter
--;
1104 //if fc=9 or 4 add one (for when we get a fc 9 instead of 10 or a 4 instead of a 5)
1105 if ((fcCounter
== 9) || fcCounter
== 4) fcCounter
++;
1106 // save last field clock count (fc/xx)
1107 lastFCcnt
= fcCounter
;
1109 // find which fcLens to save it to:
1110 for (int m
= 0; m
< 15; m
++) {
1111 if (fcLens
[m
] == fcCounter
) {
1117 if (fcCounter
> 0 && fcLensFnd
< 15) {
1119 fcCnts
[fcLensFnd
]++;
1120 fcLens
[fcLensFnd
++] = fcCounter
;
1129 uint8_t best1
= 14, best2
= 14, best3
= 14;
1130 uint16_t maxCnt1
= 0;
1131 // go through fclens and find which ones are bigest 2
1132 for (i
= 0; i
< 15; i
++) {
1133 // get the 3 best FC values
1134 if (fcCnts
[i
] > maxCnt1
) {
1137 maxCnt1
= fcCnts
[i
];
1139 } else if (fcCnts
[i
] > fcCnts
[best2
]) {
1142 } else if (fcCnts
[i
] > fcCnts
[best3
]) {
1145 if (g_debugMode
== 2) prnt("DEBUG countfc: FC %u, Cnt %u, best fc: %u, best2 fc: %u", fcLens
[i
], fcCnts
[i
], fcLens
[best1
], fcLens
[best2
]);
1146 if (fcLens
[i
] == 0) break;
1149 if (fcLens
[best1
] == 0) return 0;
1150 uint8_t fcH
= 0, fcL
= 0;
1151 if (fcLens
[best1
] > fcLens
[best2
]) {
1152 fcH
= fcLens
[best1
];
1153 fcL
= fcLens
[best2
];
1155 fcH
= fcLens
[best2
];
1156 fcL
= fcLens
[best1
];
1159 if ((size - 180) / fcH / 3 > fcCnts[best1] + fcCnts[best2]) {
1160 if (g_debugMode == 2) prnt("DEBUG countfc: fc is too large: %zu > %u. Not psk or fsk", (size - 180) / fcH / 3, fcCnts[best1] + fcCnts[best2]);
1161 return 0; //lots of waves not psk or fsk
1164 // TODO: take top 3 answers and compare to known Field clocks to get top 2
1166 uint16_t fcs
= (((uint16_t)fcH
) << 8) | fcL
;
1167 if (fskAdj
) return fcs
;
1168 return (uint16_t)fcLens
[best2
] << 8 | fcLens
[best1
];
1171 // detect psk clock by reading each phase shift
1172 // a phase shift is determined by measuring the sample length of each wave
1173 int DetectPSKClock(uint8_t *dest
, size_t size
, int clock
, size_t *firstPhaseShift
, uint8_t *curPhase
, uint8_t *fc
) {
1174 uint16_t clk
[] = {255, 16, 32, 40, 50, 64, 100, 128, 256, 272, 384}; // 255 is not a valid clock
1175 uint16_t loopCnt
= 4096; // don't need to loop through entire array...
1177 if (size
< 160 + 20) return 0;
1178 // size must be larger than 20 here, and 160 later on.
1179 if (size
< loopCnt
) loopCnt
= size
- 20;
1181 uint16_t fcs
= countFC(dest
, size
, 0);
1185 if (g_debugMode
== 2) prnt("DEBUG PSK: FC: %d, FC2: %d", *fc
, fcs
>> 8);
1187 if ((fcs
>> 8) == 10 && *fc
== 8) return 0;
1189 if (*fc
!= 2 && *fc
!= 4 && *fc
!= 8) return 0;
1192 size_t waveEnd
, firstFullWave
= 0;
1195 uint16_t waveLenCnt
, fullWaveLen
= 0;
1196 uint16_t bestErr
[] = {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000};
1197 uint16_t peaksdet
[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1199 //find start of modulating data in trace
1200 size_t i
= findModStart(dest
, size
, *fc
);
1202 firstFullWave
= pskFindFirstPhaseShift(dest
, size
, curPhase
, i
, *fc
, &fullWaveLen
);
1203 if (firstFullWave
== 0) {
1204 // no phase shift detected - could be all 1's or 0's - doesn't matter where we start
1205 // so skip a little to ensure we are past any Start Signal
1206 firstFullWave
= 160;
1210 *firstPhaseShift
= firstFullWave
;
1211 if (g_debugMode
== 2) prnt("DEBUG PSK: firstFullWave: %zu, waveLen: %d", firstFullWave
, fullWaveLen
);
1213 // Avoid autodetect if user selected a clock
1214 for (uint8_t validClk
= 1; validClk
< 8; validClk
++) {
1215 if (clock
== clk
[validClk
]) return (clock
);
1218 //test each valid clock from greatest to smallest to see which lines up
1219 for (clkCnt
= 9; clkCnt
>= 1 ; clkCnt
--) {
1220 uint8_t tol
= *fc
/ 2;
1221 size_t lastClkBit
= firstFullWave
; //set end of wave as clock align
1222 size_t waveStart
= 0;
1223 uint16_t errCnt
= 0;
1224 uint16_t peakcnt
= 0;
1225 if (g_debugMode
== 2) prnt("DEBUG PSK: clk: %d, lastClkBit: %zu", clk
[clkCnt
], lastClkBit
);
1227 for (i
= firstFullWave
+ fullWaveLen
- 1; i
< loopCnt
- 2; i
++) {
1228 //top edge of wave = start of new wave
1229 if (dest
[i
] < dest
[i
+ 1] && dest
[i
+ 1] >= dest
[i
+ 2]) {
1230 if (waveStart
== 0) {
1234 waveLenCnt
= waveEnd
- waveStart
;
1235 if (waveLenCnt
> *fc
) {
1236 //if this wave is a phase shift
1237 if (g_debugMode
== 2) prnt("DEBUG PSK: phase shift at: %zu, len: %d, nextClk: %zu, i: %zu, fc: %d", waveStart
, waveLenCnt
, lastClkBit
+ clk
[clkCnt
] - tol
, i
+ 1, *fc
);
1238 if (i
+ 1 >= lastClkBit
+ clk
[clkCnt
] - tol
) { //should be a clock bit
1240 lastClkBit
+= clk
[clkCnt
];
1241 } else if (i
< lastClkBit
+ 8) {
1242 //noise after a phase shift - ignore
1243 } else { //phase shift before supposed to based on clock
1246 } else if (i
+ 1 > lastClkBit
+ clk
[clkCnt
] + tol
+ *fc
) {
1247 lastClkBit
+= clk
[clkCnt
]; //no phase shift but clock bit
1253 if (errCnt
== 0) return clk
[clkCnt
];
1254 if (errCnt
<= bestErr
[clkCnt
]) bestErr
[clkCnt
] = errCnt
;
1255 if (peakcnt
> peaksdet
[clkCnt
]) peaksdet
[clkCnt
] = peakcnt
;
1257 //all tested with errors
1258 //return the highest clk with the most peaks found
1260 for (i
= 9; i
>= 1; i
--) {
1261 if (peaksdet
[i
] > peaksdet
[best
])
1264 if (g_debugMode
== 2) prnt("DEBUG PSK: Clk: %d, peaks: %d, errs: %d, bestClk: %d", clk
[i
], peaksdet
[i
], bestErr
[i
], clk
[best
]);
1269 // detects the bit clock for FSK given the high and low Field Clocks
1270 uint8_t detectFSKClk(const uint8_t *bits
, size_t size
, uint8_t fcHigh
, uint8_t fcLow
, int *firstClockEdge
) {
1275 uint8_t clk
[] = {8, 16, 32, 40, 50, 64, 100, 128, 0};
1276 uint16_t rfLens
[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1277 uint8_t rfCnts
[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1278 uint8_t rfLensFnd
= 0;
1279 uint8_t lastFCcnt
= 0;
1280 uint16_t fcCounter
= 0;
1281 uint16_t rfCounter
= 0;
1282 uint8_t firstBitFnd
= 0;
1284 uint8_t fcTol
= ((fcHigh
* 100 - fcLow
* 100) / 2 + 50) / 100; //(uint8_t)(0.5+(float)(fcHigh-fcLow)/2);
1286 // prime i to first peak / up transition
1287 for (i
= 160; i
< size
- 20; i
++)
1288 if (bits
[i
] > bits
[i
- 1] && bits
[i
] >= bits
[i
+ 1])
1291 for (; i
< size
- 20; i
++) {
1295 if (bits
[i
] <= bits
[i
- 1] || bits
[i
] < bits
[i
+ 1])
1298 // if we got less than the small fc + tolerance then set it to the small fc
1299 // if it is inbetween set it to the last counter
1300 if (fcCounter
< fcHigh
&& fcCounter
> fcLow
)
1301 fcCounter
= lastFCcnt
;
1302 else if (fcCounter
< fcLow
+ fcTol
)
1304 else //set it to the large fc
1307 //look for bit clock (rf/xx)
1308 if ((fcCounter
< lastFCcnt
|| fcCounter
> lastFCcnt
)) {
1309 //not the same size as the last wave - start of new bit sequence
1310 if (firstBitFnd
> 1) { //skip first wave change - probably not a complete bit
1311 for (int ii
= 0; ii
< 15; ii
++) {
1312 if (rfLens
[ii
] >= (rfCounter
- 4) && rfLens
[ii
] <= (rfCounter
+ 4)) {
1318 if (rfCounter
> 0 && rfLensFnd
< 15) {
1319 //prnt("DEBUG: rfCntr %d, fcCntr %d",rfCounter,fcCounter);
1320 rfCnts
[rfLensFnd
]++;
1321 rfLens
[rfLensFnd
++] = rfCounter
;
1324 *firstClockEdge
= i
;
1328 lastFCcnt
= fcCounter
;
1332 uint8_t rfHighest
= 15, rfHighest2
= 15, rfHighest3
= 15;
1334 for (i
= 0; i
< 15; i
++) {
1335 //get highest 2 RF values (might need to get more values to compare or compare all?)
1336 if (rfCnts
[i
] > rfCnts
[rfHighest
]) {
1337 rfHighest3
= rfHighest2
;
1338 rfHighest2
= rfHighest
;
1340 } else if (rfCnts
[i
] > rfCnts
[rfHighest2
]) {
1341 rfHighest3
= rfHighest2
;
1343 } else if (rfCnts
[i
] > rfCnts
[rfHighest3
]) {
1346 if (g_debugMode
== 2)
1347 prnt("DEBUG FSK: RF %d, cnts %d", rfLens
[i
], rfCnts
[i
]);
1349 // set allowed clock remainder tolerance to be 1 large field clock length+1
1350 // we could have mistakenly made a 9 a 10 instead of an 8 or visa versa so rfLens could be 1 FC off
1351 uint8_t tol1
= fcHigh
+ 1;
1353 if (g_debugMode
== 2)
1354 prnt("DEBUG FSK: most counted rf values: 1 %d, 2 %d, 3 %d", rfLens
[rfHighest
], rfLens
[rfHighest2
], rfLens
[rfHighest3
]);
1356 // loop to find the highest clock that has a remainder less than the tolerance
1357 // compare samples counted divided by
1358 // test 128 down to 32 (shouldn't be possible to have fc/10 & fc/8 and rf/16 or less)
1360 for (; m
>= 2; m
--) {
1361 if (rfLens
[rfHighest
] % clk
[m
] < tol1
|| rfLens
[rfHighest
] % clk
[m
] > clk
[m
] - tol1
) {
1362 if (rfLens
[rfHighest2
] % clk
[m
] < tol1
|| rfLens
[rfHighest2
] % clk
[m
] > clk
[m
] - tol1
) {
1363 if (rfLens
[rfHighest3
] % clk
[m
] < tol1
|| rfLens
[rfHighest3
] % clk
[m
] > clk
[m
] - tol1
) {
1364 if (g_debugMode
== 2)
1365 prnt("DEBUG FSK: clk %d divides into the 3 most rf values within tolerance", clk
[m
]);
1372 if (m
< 2) return 0; // oops we went too far
1378 // **********************************************************************************************
1379 // --------------------Modulation Demods &/or Decoding Section-----------------------------------
1380 // **********************************************************************************************
1383 // look for Sequence Terminator - should be pulses of clk*(1 or 2), clk*2, clk*(1.5 or 2), by idx we mean graph position index...
1384 static bool findST(int *stStopLoc
, int *stStartIdx
,
1385 const int lowToLowWaveLen
[], const int highToLowWaveLen
[],
1386 int clk
, int tol
, int buffSize
, size_t *i
) {
1387 if (buffSize
< *i
+ 4) return false;
1389 for (; *i
< buffSize
- 4; *i
+= 1) {
1390 *stStartIdx
+= lowToLowWaveLen
[*i
]; //caution part of this wave may be data and part may be ST.... to be accounted for in main function for now...
1391 if (lowToLowWaveLen
[*i
] >= clk
* 1 - tol
&& lowToLowWaveLen
[*i
] <= (clk
* 2) + tol
&& highToLowWaveLen
[*i
] < clk
+ tol
) { //1 to 2 clocks depending on 2 bits prior
1392 if (lowToLowWaveLen
[*i
+ 1] >= clk
* 2 - tol
&& lowToLowWaveLen
[*i
+ 1] <= clk
* 2 + tol
&& highToLowWaveLen
[*i
+ 1] > clk
* 3 / 2 - tol
) { //2 clocks and wave size is 1 1/2
1393 if (lowToLowWaveLen
[*i
+ 2] >= (clk
* 3) / 2 - tol
&& lowToLowWaveLen
[*i
+ 2] <= clk
* 2 + tol
&& highToLowWaveLen
[*i
+ 2] > clk
- tol
) { //1 1/2 to 2 clocks and at least one full clock wave
1394 if (lowToLowWaveLen
[*i
+ 3] >= clk
* 1 - tol
&& lowToLowWaveLen
[*i
+ 3] <= clk
* 2 + tol
) { //1 to 2 clocks for end of ST + first bit
1395 *stStopLoc
= *i
+ 3;
1405 // attempt to identify a Sequence Terminator in ASK modulated raw wave
1406 bool DetectST(uint8_t *buffer
, size_t *size
, int *foundclock
, size_t *ststart
, size_t *stend
) {
1407 size_t bufsize
= *size
;
1408 //need to loop through all samples and identify our clock, look for the ST pattern
1411 int j
= 0, high
, low
, skip
= 0, start
= 0, end
= 0, minClk
= 255;
1413 //probably should calloc... || test if memory is available ... handle device side? memory danger!!! [marshmellow]
1414 int tmpbuff
[bufsize
/ LOWEST_DEFAULT_CLOCK
]; // low to low wave count //guess rf/32 clock, if click is smaller we will only have room for a fraction of the samples captured
1415 int waveLen
[bufsize
/ LOWEST_DEFAULT_CLOCK
]; // high to low wave count //if clock is larger then we waste memory in array size that is not needed...
1416 //size_t testsize = (bufsize < 512) ? bufsize : 512;
1419 memset(tmpbuff
, 0, sizeof(tmpbuff
));
1420 memset(waveLen
, 0, sizeof(waveLen
));
1422 if (!loadWaveCounters(buffer
, bufsize
, tmpbuff
, waveLen
, &j
, &skip
, &minClk
, &high
, &low
)) return false;
1423 // set clock - might be able to get this externally and remove this work...
1424 clk
= getClosestClock(minClk
);
1425 // clock not found - ERROR
1427 if (g_debugMode
== 2) prnt("DEBUG STT: clock not found - quitting");
1433 if (!findST(&start
, &skip
, tmpbuff
, waveLen
, clk
, tol
, j
, &i
)) {
1434 // first ST not found - ERROR
1435 if (g_debugMode
== 2) prnt("DEBUG STT: first STT not found - quitting");
1438 if (g_debugMode
== 2) prnt("DEBUG STT: first STT found at wave: %i, skip: %i, j=%i", start
, skip
, j
);
1440 if (waveLen
[i
+ 2] > clk
* 1 + tol
)
1445 // skip over the remainder of ST
1446 skip
+= clk
* 7 / 2; //3.5 clocks from tmpbuff[i] = end of st - also aligns for ending point
1448 // now do it again to find the end
1452 if (!findST(&dummy1
, &end
, tmpbuff
, waveLen
, clk
, tol
, j
, &i
)) {
1453 //didn't find second ST - ERROR
1454 if (g_debugMode
== 2) prnt("DEBUG STT: second STT not found - quitting");
1458 if (g_debugMode
== 2) prnt("DEBUG STT: start of data: %d end of data: %d, datalen: %d, clk: %d, bits: %d, phaseoff: %d", skip
, end
, end
- skip
, clk
, (end
- skip
) / clk
, phaseoff
);
1459 //now begin to trim out ST so we can use normal demod cmds
1461 size_t datalen
= end
- start
;
1462 // check validity of datalen (should be even clock increments) - use a tolerance of up to 1/8th a clock
1463 if (clk
- (datalen
% clk
) <= clk
/ 8) {
1464 // padd the amount off - could be problematic... but shouldn't happen often
1465 datalen
+= clk
- (datalen
% clk
);
1466 } else if ((datalen
% clk
) <= clk
/ 8) {
1467 // padd the amount off - could be problematic... but shouldn't happen often
1468 datalen
-= datalen
% clk
;
1470 if (g_debugMode
== 2) prnt("DEBUG STT: datalen not divisible by clk: %zu %% %d = %zu - quitting", datalen
, clk
, datalen
% clk
);
1473 // if datalen is less than one t55xx block - ERROR
1474 if (datalen
/ clk
< 8 * 4) {
1475 if (g_debugMode
== 2) prnt("DEBUG STT: datalen is less than 1 full t55xx block - quitting");
1478 size_t dataloc
= start
;
1479 if (buffer
[dataloc
- (clk
* 4) - (clk
/ 4)] <= low
&& buffer
[dataloc
] <= low
&& buffer
[dataloc
- (clk
* 4)] >= high
) {
1480 //we have low drift (and a low just before the ST and a low just after the ST) - compensate by backing up the start
1481 for (i
= 0; i
<= (clk
/ 4); ++i
) {
1482 if (buffer
[dataloc
- (clk
* 4) - i
] <= low
) {
1491 if (g_debugMode
== 2) prnt("DEBUG STT: Starting STT trim - start: %zu, datalen: %zu ", dataloc
, datalen
);
1492 bool firstrun
= true;
1493 // warning - overwriting buffer given with raw wave data with ST removed...
1494 while (dataloc
< bufsize
- (clk
/ 2)) {
1495 //compensate for long high at end of ST not being high due to signal loss... (and we cut out the start of wave high part)
1496 if (buffer
[dataloc
] < high
&& buffer
[dataloc
] > low
&& buffer
[dataloc
+ clk
/ 4] < high
&& buffer
[dataloc
+ clk
/ 4] > low
) {
1497 for (i
= 0; i
< clk
/ 2 - tol
; ++i
) {
1498 buffer
[dataloc
+ i
] = high
+ 5;
1500 } //test for small spike outlier (high between two lows) in the case of very strong waves
1501 if (buffer
[dataloc
] > low
&& buffer
[dataloc
+ clk
/ 4] <= low
) {
1502 for (i
= 0; i
< clk
/ 4; ++i
) {
1503 buffer
[dataloc
+ i
] = buffer
[dataloc
+ clk
/ 4];
1508 *ststart
= dataloc
- (clk
* 4);
1511 for (i
= 0; i
< datalen
; ++i
) {
1512 if (i
+ newloc
< bufsize
) {
1513 if (i
+ newloc
< dataloc
)
1514 buffer
[i
+ newloc
] = buffer
[dataloc
];
1520 //skip next ST - we just assume it will be there from now on...
1521 if (g_debugMode
== 2) prnt("DEBUG STT: skipping STT at %zu to %zu", dataloc
, dataloc
+ (clk
* 4));
1528 // take 11 10 01 11 00 and make 01100 ... miller decoding
1529 // check for phase errors - should never have half a 1 or 0 by itself and should never exceed 1111 or 0000 in a row
1530 // decodes miller encoded binary
1531 // NOTE askrawdemod will NOT demod miller encoded ask unless the clock is manually set to 1/2 what it is detected as!
1533 static int millerRawDecode(uint8_t *bits, size_t *size, int invert) {
1534 if (*size < 16) return -1;
1536 uint16_t MaxBits = MAX_DEMODULATION_BITS, errCnt = 0;
1537 size_t i, bitCnt = 0;
1538 uint8_t alignCnt = 0, curBit = bits[0], alignedIdx = 0, halfClkErr = 0;
1540 //find alignment, needs 4 1s or 0s to properly align
1541 for (i = 1; i < *size - 1; i++) {
1542 alignCnt = (bits[i] == curBit) ? alignCnt + 1 : 0;
1544 if (alignCnt == 4) break;
1546 // for now error if alignment not found. later add option to run it with multiple offsets...
1547 if (alignCnt != 4) {
1548 if (g_debugMode) prnt("ERROR MillerDecode: alignment not found so either your bits is not miller or your data does not have a 101 in it");
1551 alignedIdx = (i - 1) % 2;
1552 for (i = alignedIdx; i < *size - 3; i += 2) {
1553 halfClkErr = (uint8_t)((halfClkErr << 1 | bits[i]) & 0xFF);
1554 if ((halfClkErr & 0x7) == 5 || (halfClkErr & 0x7) == 2 || (i > 2 && (halfClkErr & 0x7) == 0) || (halfClkErr & 0x1F) == 0x1F) {
1559 bits[bitCnt++] = bits[i] ^ bits[i + 1] ^ invert;
1561 if (bitCnt > MaxBits) break;
1568 // take 01 or 10 = 1 and 11 or 00 = 0
1569 // check for phase errors - should never have 111 or 000 should be 01001011 or 10110100 for 1010
1570 // decodes biphase or if inverted it is AKA conditional dephase encoding AKA differential manchester encoding
1571 int BiphaseRawDecode(uint8_t *bits
, size_t *size
, int *offset
, int invert
) {
1573 if (*size
< 51) return -1;
1575 if (*offset
< 0) *offset
= 0;
1577 uint16_t bitnum
= 0;
1578 uint16_t errCnt
= 0;
1580 uint16_t maxbits
= MAX_DEMODULATION_BITS
;
1582 //check for phase change faults - skip one sample if faulty
1583 bool offsetA
= true, offsetB
= true;
1584 for (; i
< *offset
+ 48; i
+= 2) {
1585 if (bits
[i
+ 1] == bits
[i
+ 2]) offsetA
= false;
1586 if (bits
[i
+ 2] == bits
[i
+ 3]) offsetB
= false;
1588 if (!offsetA
&& offsetB
) ++*offset
;
1591 for (i
= *offset
; i
< *size
- 1; i
+= 2) {
1592 //check for phase error
1593 if (bits
[i
+ 1] == bits
[i
+ 2]) {
1597 if ((bits
[i
] == 1 && bits
[i
+ 1] == 0) || (bits
[i
] == 0 && bits
[i
+ 1] == 1)) {
1598 bits
[bitnum
++] = 1 ^ invert
;
1599 } else if ((bits
[i
] == 0 && bits
[i
+ 1] == 0) || (bits
[i
] == 1 && bits
[i
+ 1] == 1)) {
1600 bits
[bitnum
++] = invert
;
1605 if (bitnum
> maxbits
) break;
1611 // take 10 and 01 and manchester decode
1612 // run through 2 times and take least errCnt
1613 // "," indicates 00 or 11 wrong bit
1614 uint16_t manrawdecode(uint8_t *bits
, size_t *size
, uint8_t invert
, uint8_t *alignPos
) {
1621 int errCnt
= 0, bestErr
= 1000;
1622 uint16_t bitnum
= 0, maxBits
= MAX_DEMODULATION_BITS
, bestRun
= 0;
1625 // find correct start position [alignment]
1626 for (uint8_t k
= 0; k
< 2; k
++) {
1628 for (i
= k
; i
< *size
- 1; i
+= 2) {
1630 if (bits
[i
] == bits
[i
+ 1]) {
1639 if (bestErr
> errCnt
) {
1644 if (g_debugMode
== 2) prnt("DEBUG manrawdecode: bestErr %d | bestRun %u", bestErr
, bestRun
);
1650 *alignPos
= bestRun
;
1652 for (i
= bestRun
; i
< *size
; i
+= 2) {
1654 if (bits
[i
] == 1 && (bits
[i
+ 1] == 0)) {
1655 bits
[bitnum
++] = invert
;
1656 } else if ((bits
[i
] == 0) && bits
[i
+ 1] == 1) {
1657 bits
[bitnum
++] = invert
^ 1;
1662 if (bitnum
> maxBits
) {
1671 // demodulates strong heavily clipped samples
1672 // RETURN: num of errors. if 0, is ok.
1673 static uint16_t cleanAskRawDemod(uint8_t *bits
, size_t *size
, int clk
, int invert
, int high
, int low
, int *startIdx
) {
1675 size_t bitCnt
= 0, smplCnt
= 1, errCnt
= 0, pos
= 0;
1676 uint8_t cl_4
= clk
/ 4;
1677 uint8_t cl_2
= clk
/ 2;
1678 bool waveHigh
= true;
1680 getNextHigh(bits
, *size
, high
, &pos
);
1681 // getNextLow(bits, *size, low, &pos);
1683 // do not skip first transition
1684 if ((pos
> cl_2
- cl_4
- 1) && (pos
<= clk
+ cl_4
+ 1)) {
1685 bits
[bitCnt
++] = invert
^ 1;
1688 // sample counts, like clock = 32.. it tries to find 32/4 = 8, 32/2 = 16
1689 for (size_t i
= pos
; i
< *size
; i
++) {
1690 if (bits
[i
] >= high
&& waveHigh
) {
1692 } else if (bits
[i
] <= low
&& !waveHigh
) {
1696 if ((bits
[i
] >= high
&& !waveHigh
) || (bits
[i
] <= low
&& waveHigh
)) {
1698 // 8 :: 8-2-1 = 5 8+2+1 = 11
1699 // 16 :: 16-4-1 = 11 16+4+1 = 21
1700 // 32 :: 32-8-1 = 23 32+8+1 = 41
1701 // 64 :: 64-16-1 = 47 64+16+1 = 81
1702 if (smplCnt
> clk
- cl_4
- 1) { //full clock
1704 if (smplCnt
> clk
+ cl_4
+ 1) {
1707 if (g_debugMode
== 2) prnt("DEBUG ASK: cleanAskRawDemod ASK Modulation Error FULL at: %zu [%zu > %u]", i
, smplCnt
, clk
+ cl_4
+ 1);
1709 } else if (waveHigh
) {
1710 bits
[bitCnt
++] = invert
;
1711 bits
[bitCnt
++] = invert
;
1713 bits
[bitCnt
++] = invert
^ 1;
1714 bits
[bitCnt
++] = invert
^ 1;
1716 if (*startIdx
== 0) {
1717 *startIdx
= i
- clk
;
1718 if (g_debugMode
== 2) prnt("DEBUG ASK: cleanAskRawDemod minus clock [%d]", *startIdx
);
1720 waveHigh
= !waveHigh
;
1724 } else if (smplCnt
> cl_2
- cl_4
- 1) { //half clock
1726 if (smplCnt
> cl_2
+ cl_4
+ 1) { //too many samples
1728 if (g_debugMode
== 2) prnt("DEBUG ASK: cleanAskRawDemod ASK Modulation Error HALF at: %zu [%zu]", i
, smplCnt
);
1733 bits
[bitCnt
++] = invert
;
1735 bits
[bitCnt
++] = invert
^ 1;
1738 if (*startIdx
== 0) {
1739 *startIdx
= i
- cl_2
;
1740 if (g_debugMode
== 2) prnt("DEBUG ASK: cleanAskRawDemod minus half clock [%d]", *startIdx
);
1742 waveHigh
= !waveHigh
;
1746 //transition bit oops
1748 } else { //haven't hit new high or new low yet
1756 if (g_debugMode
== 2) prnt("DEBUG ASK: cleanAskRawDemod Startidx %d", *startIdx
);
1761 // attempts to demodulate ask modulations, askType == 0 for ask/raw, askType==1 for ask/manchester
1762 int askdemod_ext(uint8_t *bits
, size_t *size
, int *clk
, int *invert
, int maxErr
, uint8_t amp
, uint8_t askType
, int *startIdx
) {
1764 if (*size
== 0) return -1;
1766 if (signalprop
.isnoise
) {
1767 if (g_debugMode
== 2) prnt("DEBUG (askdemod_ext) just noise detected - aborting");
1771 int start
= DetectASKClock(bits
, *size
, clk
, maxErr
);
1772 if (*clk
== 0 || start
< 0) return -3;
1774 if (*invert
!= 1) *invert
= 0;
1776 // amplify signal data.
1778 if (amp
== 1) askAmp(bits
, *size
);
1780 if (g_debugMode
== 2) prnt("DEBUG (askdemod_ext) clk %d, beststart %d, amp %d", *clk
, start
, amp
);
1782 // Detect high and lows
1783 //25% clip in case highs and lows aren't clipped [marshmellow]
1785 getHiLo(&high
, &low
, 75, 75);
1788 // if clean clipped waves detected run alternate demod
1789 if (DetectCleanAskWave(bits
, *size
, high
, low
)) {
1791 //start pos from detect ask clock is 1/2 clock offset
1792 // NOTE: can be negative (demod assumes rest of wave was there)
1793 *startIdx
= start
- (*clk
/ 2);
1794 if (g_debugMode
== 2) prnt("DEBUG: (askdemod_ext) Clean wave detected --- startindex %d", *startIdx
);
1796 errCnt
= cleanAskRawDemod(bits
, size
, *clk
, *invert
, high
, low
, startIdx
);
1798 if (askType
) { //ask/manchester
1799 uint8_t alignPos
= 0;
1800 errCnt
= manrawdecode(bits
, size
, 0, &alignPos
);
1801 *startIdx
+= ((*clk
/ 2) * alignPos
);
1803 if (g_debugMode
== 2) prnt("DEBUG: (askdemod_ext) CLEAN: startIdx %i, alignPos %u , bestError %zu", *startIdx
, alignPos
, errCnt
);
1808 *startIdx
= start
- (*clk
/ 2);
1809 if (g_debugMode
== 2) prnt("DEBUG: (askdemod_ext) Weak wave detected: startIdx %i", *startIdx
);
1811 int lastBit
; // set first clock check - can go negative
1812 size_t i
, bitnum
= 0; // output counter
1814 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
1815 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
1816 size_t MaxBits
= 3072; // max bits to collect
1817 lastBit
= start
- *clk
;
1819 for (i
= start
; i
< *size
; ++i
) {
1820 if (i
- lastBit
>= *clk
- tol
) {
1821 if (bits
[i
] >= high
) {
1822 bits
[bitnum
++] = *invert
;
1823 } else if (bits
[i
] <= low
) {
1824 bits
[bitnum
++] = *invert
^ 1;
1825 } else if (i
- lastBit
>= *clk
+ tol
) {
1827 // if (g_debugMode == 2) prnt("DEBUG: (askdemod_ext) Modulation Error at: %u", i);
1831 } else { //in tolerance - looking for peak
1836 } else if (i
- lastBit
>= (*clk
/ 2 - tol
) && !midBit
&& !askType
) {
1837 if (bits
[i
] >= high
) {
1838 bits
[bitnum
++] = *invert
;
1839 } else if (bits
[i
] <= low
) {
1840 bits
[bitnum
++] = *invert
^ 1;
1841 } else if (i
- lastBit
>= *clk
/ 2 + tol
) {
1843 bits
[bitnum
] = bits
[bitnum
- 1];
1849 } else { //in tolerance - looking for peak
1854 if (bitnum
>= MaxBits
) break;
1860 int askdemod(uint8_t *bits
, size_t *size
, int *clk
, int *invert
, int maxErr
, uint8_t amp
, uint8_t askType
) {
1862 return askdemod_ext(bits
, size
, clk
, invert
, maxErr
, amp
, askType
, &start
);
1865 // demodulate NRZ wave - requires a read with strong signal
1866 // peaks invert bit (high=1 low=0) each clock cycle = 1 bit determined by last peak
1867 int nrzRawDemod(uint8_t *dest
, size_t *size
, int *clk
, const int *invert
, int *startIdx
) {
1869 if (signalprop
.isnoise
) {
1870 if (g_debugMode
== 2) prnt("DEBUG nrzRawDemod: just noise detected - quitting");
1874 size_t clkStartIdx
= 0;
1875 *clk
= DetectNRZClock(dest
, *size
, *clk
, &clkStartIdx
);
1876 if (*clk
== 0) return -2;
1881 getHiLo(&high
, &low
, 75, 75);
1884 //convert wave samples to 1's and 0's
1885 for (i
= 20; i
< *size
- 20; i
++) {
1886 if (dest
[i
] >= high
) bit
= 1;
1887 if (dest
[i
] <= low
) bit
= 0;
1890 //now demod based on clock (rf/32 = 32 1's for one 1 bit, 32 0's for one 0 bit)
1893 for (i
= 21; i
< *size
- 20; i
++) {
1894 //if transition detected or large number of same bits - store the passed bits
1895 if (dest
[i
] != dest
[i
- 1] || (i
- lastBit
) == (10 * *clk
)) {
1896 memset(dest
+ numBits
, dest
[i
- 1] ^ *invert
, (i
- lastBit
+ (*clk
/ 4)) / *clk
);
1897 numBits
+= (i
- lastBit
+ (*clk
/ 4)) / *clk
;
1899 *startIdx
= i
- (numBits
* *clk
);
1900 if (g_debugMode
== 2) prnt("DEBUG NRZ: startIdx %i", *startIdx
);
1909 // translate wave to 11111100000 (1 for each short wave [higher freq] 0 for each long wave [lower freq])
1910 static size_t fsk_wave_demod(uint8_t *dest
, size_t size
, uint8_t fchigh
, uint8_t fclow
, int *startIdx
) {
1912 if (size
< 1024) return 0; // not enough samples
1914 if (fchigh
== 0) fchigh
= 10;
1915 if (fclow
== 0) fclow
= 8;
1917 //set the threshold close to 0 (graph) or 128 std to avoid static
1918 size_t preLastSample
, LastSample
= 0;
1919 size_t currSample
= 0, last_transition
= 0;
1920 size_t idx
, numBits
= 0;
1922 //find start of modulating data in trace
1923 idx
= findModStart(dest
, size
, fchigh
);
1924 // Need to threshold first sample
1925 dest
[idx
] = (dest
[idx
] < signalprop
.mean
) ? 0 : 1;
1927 last_transition
= idx
;
1930 // Definition: cycles between consecutive lo-hi transitions
1931 // Lets define some expected lengths. FSK1 is easier since it has bigger differences between.
1933 // 50/8 = 6 | 40/8 = 5 | 64/8 = 8
1934 // 50/5 = 10 | 40/5 = 8 | 64/5 = 12
1937 // 50/10 = 5 | 40/10 = 4 | 64/10 = 6
1938 // 50/8 = 6 | 40/8 = 5 | 64/8 = 8
1940 // count cycles between consecutive lo-hi transitions,
1941 // in practice due to noise etc we may end up with anywhere
1942 // To allow fuzz would mean +-1 on expected cycle width.
1944 // 50/8 = 6 (5-7) | 40/8 = 5 (4-6) | 64/8 = 8 (7-9)
1945 // 50/5 = 10 (9-11) | 40/5 = 8 (7-9) | 64/5 = 12 (11-13)
1948 // 50/10 = 5 (4-6) | 40/10 = 4 (3-5) | 64/10 = 6 (5-7)
1949 // 50/8 = 6 (5-7) | 40/8 = 5 (4-6) | 64/8 = 8 (7-9)
1951 // It easy to see to the overgaping, but luckily we the group value also, like 1111000001111
1952 // to separate between which bit to demodulate to.
1955 // count width from 0-1 transition to 1-0.
1956 // determine the width is within FUZZ_min and FUZZ_max tolerances
1957 // width should be divided with exp_one. i:e 6+7+6+2=21, 21/5 = 4,
1958 // the 1-0 to 0-1 width should be divided with exp_zero. Ie: 3+5+6+7 = 21/6 = 3
1960 for (; idx
< size
- 20; idx
++) {
1962 // threshold current value
1963 dest
[idx
] = (dest
[idx
] < signalprop
.mean
) ? 0 : 1;
1965 // Check for 0->1 transition
1966 if (dest
[idx
- 1] < dest
[idx
]) {
1967 preLastSample
= LastSample
;
1968 LastSample
= currSample
;
1969 currSample
= idx
- last_transition
;
1970 if (currSample
< (fclow
- 2)) { //0-5 = garbage noise (or 0-3)
1971 //do nothing with extra garbage
1972 } else if (currSample
< (fchigh
- 1)) { //6-8 = 8 sample waves (or 3-6 = 5)
1973 //correct previous 9 wave surrounded by 8 waves (or 6 surrounded by 5)
1974 if (numBits
> 1 && LastSample
> (fchigh
- 2) && (preLastSample
< (fchigh
- 1))) {
1975 dest
[numBits
- 1] = 1;
1977 dest
[numBits
++] = 1;
1980 if (numBits
> 0 && *startIdx
== 0)
1981 *startIdx
= idx
- fclow
;
1983 } else if (currSample
> (fchigh
+ 1) && numBits
< 3) { //12 + and first two bit = unusable garbage
1984 //do nothing with beginning garbage and reset.. should be rare..
1986 } else if (currSample
== (fclow
+ 1) && LastSample
== (fclow
- 1)) { // had a 7 then a 9 should be two 8's (or 4 then a 6 should be two 5's)
1987 dest
[numBits
++] = 1;
1988 if (numBits
> 0 && *startIdx
== 0) {
1989 *startIdx
= idx
- fclow
;
1991 } else { //9+ = 10 sample waves (or 6+ = 7)
1992 dest
[numBits
++] = 0;
1993 if (numBits
> 0 && *startIdx
== 0) {
1994 *startIdx
= idx
- fchigh
;
1997 last_transition
= idx
;
2000 return numBits
; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
2003 // translate 11111100000 to 10
2004 //rfLen = clock, fchigh = larger field clock, fclow = smaller field clock
2005 static size_t aggregate_bits(uint8_t *dest
, size_t size
, uint8_t clk
, uint8_t invert
, uint8_t fchigh
, uint8_t fclow
, int *startIdx
) {
2007 uint8_t lastval
= dest
[0];
2011 uint8_t hclk
= clk
/ 2;
2013 for (i
= 1; i
< size
; i
++) {
2015 if (dest
[i
] == lastval
) continue; //skip until we hit a transition
2017 //find out how many bits (n) we collected (use 1/2 clk tolerance)
2019 if (dest
[i
- 1] == 1)
2020 //if lastval was 1, we have a 1->0 crossing
2021 n
= (n
* fclow
+ hclk
) / clk
;
2024 n
= (n
* fchigh
+ hclk
) / clk
;
2029 //first transition - save startidx
2031 if (lastval
== 1) { //high to low
2032 *startIdx
+= (fclow
* i
) - (n
* clk
);
2033 if (g_debugMode
== 2) prnt("DEBUG (aggregate_bits) FSK startIdx %i, fclow*idx %zu, n*clk %u", *startIdx
, fclow
* i
, n
* clk
);
2035 *startIdx
+= (fchigh
* i
) - (n
* clk
);
2036 if (g_debugMode
== 2) prnt("DEBUG (aggregate_bits) FSK startIdx %i, fchigh*idx %zu, n*clk %u", *startIdx
, fchigh
* i
, n
* clk
);
2040 //add to our destination the bits we collected
2041 memset(dest
+ numBits
, dest
[i
- 1] ^ invert
, n
);
2049 // if valid extra bits at the end were all the same frequency - add them in
2050 if (n
> clk
/ fchigh
) {
2051 if (dest
[i
- 2] == 1) {
2052 n
= (n
* fclow
+ clk
/ 2) / clk
;
2054 n
= (n
* fchigh
+ clk
/ 2) / clk
;
2056 memset(dest
+ numBits
, dest
[i
- 1] ^ invert
, n
);
2058 if (g_debugMode
== 2) prnt("DEBUG (aggregate_bits) extra bits in the end");
2063 // full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod)
2064 size_t fskdemod(uint8_t *dest
, size_t size
, uint8_t rfLen
, uint8_t invert
, uint8_t fchigh
, uint8_t fclow
, int *start_idx
) {
2065 if (signalprop
.isnoise
) return 0;
2067 size
= fsk_wave_demod(dest
, size
, fchigh
, fclow
, start_idx
);
2068 if (g_debugMode
== 2) prnt("DEBUG (fskdemod) got %zu bits", size
);
2069 size
= aggregate_bits(dest
, size
, rfLen
, invert
, fchigh
, fclow
, start_idx
);
2070 if (g_debugMode
== 2) prnt("DEBUG (fskdemod) got %zu bits", size
);
2074 // convert psk1 demod to psk2 demod
2075 // only transition waves are 1s
2076 // TODO: Iceman - hard coded value 7, should be #define
2077 void psk1TOpsk2(uint8_t *bits
, size_t size
) {
2078 uint8_t lastbit
= bits
[0];
2079 for (size_t i
= 1; i
< size
; i
++) {
2081 if (bits
[i
] == 7) continue;
2083 if (lastbit
!= bits
[i
]) {
2092 // convert psk2 demod to psk1 demod
2093 // from only transition waves are 1s to phase shifts change bit
2094 void psk2TOpsk1(uint8_t *bits
, size_t size
) {
2096 for (size_t i
= 0; i
< size
; i
++) {
2104 // demodulate PSK1 wave
2105 // uses wave lengths (# Samples)
2106 // TODO: Iceman - hard coded value 7, should be #define
2107 int pskRawDemod_ext(uint8_t *dest
, size_t *size
, int *clock
, const int *invert
, int *startIdx
) {
2110 if (*size
< 170) return -1;
2112 uint8_t curPhase
= *invert
;
2114 size_t i
= 0, numBits
= 0, waveStart
= 1, waveEnd
, firstFullWave
= 0, lastClkBit
= 0;
2115 uint16_t fullWaveLen
= 0, waveLenCnt
;
2116 //uint16_t avgWaveVal = 0;
2117 uint16_t errCnt
= 0, errCnt2
= 0;
2119 *clock
= DetectPSKClock(dest
, *size
, *clock
, &firstFullWave
, &curPhase
, &fc
);
2120 if (*clock
<= 0) return -1;
2121 //if clock detect found firstfullwave...
2122 uint16_t tol
= fc
/ 2;
2123 if (firstFullWave
== 0) {
2124 //find start of modulating data in trace
2125 i
= findModStart(dest
, *size
, fc
);
2126 //find first phase shift
2127 firstFullWave
= pskFindFirstPhaseShift(dest
, *size
, &curPhase
, i
, fc
, &fullWaveLen
);
2128 if (firstFullWave
== 0) {
2129 // no phase shift detected - could be all 1's or 0's - doesn't matter where we start
2130 // so skip a little to ensure we are past any Start Signal
2131 firstFullWave
= 160;
2132 memset(dest
, curPhase
, firstFullWave
/ *clock
);
2134 memset(dest
, curPhase
^ 1, firstFullWave
/ *clock
);
2137 memset(dest
, curPhase
^ 1, firstFullWave
/ *clock
);
2140 numBits
+= (firstFullWave
/ *clock
);
2141 *startIdx
= firstFullWave
- (*clock
* numBits
) + 2;
2142 //set start of wave as clock align
2143 lastClkBit
= firstFullWave
;
2144 if (g_debugMode
== 2) {
2145 prnt("DEBUG PSK: firstFullWave: %zu, waveLen: %u, startIdx %i", firstFullWave
, fullWaveLen
, *startIdx
);
2146 prnt("DEBUG PSK: clk: %d, lastClkBit: %zu, fc: %u", *clock
, lastClkBit
, fc
);
2150 dest
[numBits
++] = curPhase
; //set first read bit
2151 for (i
= firstFullWave
+ fullWaveLen
- 1; i
< *size
- 3; i
++) {
2152 //top edge of wave = start of new wave
2153 if (dest
[i
] + fc
< dest
[i
+ 1] && dest
[i
+ 1] >= dest
[i
+ 2]) {
2154 if (waveStart
== 0) {
2156 //avgWaveVal = dest[i + 1];
2159 waveLenCnt
= waveEnd
- waveStart
;
2160 if (waveLenCnt
> fc
) {
2161 //this wave is a phase shift
2163 prnt("DEBUG: phase shift at: %d, len: %d, nextClk: %d, i: %d, fc: %d"
2166 , lastClkBit + *clock - tol
2170 if (i
+ 1 >= lastClkBit
+ *clock
- tol
) { //should be a clock bit
2172 dest
[numBits
++] = curPhase
;
2173 lastClkBit
+= *clock
;
2174 } else if (i
< lastClkBit
+ 10 + fc
) {
2175 //noise after a phase shift - ignore
2176 } else { //phase shift before supposed to based on clock
2178 dest
[numBits
++] = 7;
2180 } else if (i
+ 1 > lastClkBit
+ *clock
+ tol
+ fc
) {
2181 lastClkBit
+= *clock
; //no phase shift but clock bit
2182 dest
[numBits
++] = curPhase
;
2183 } else if (waveLenCnt
< fc
- 1) { //wave is smaller than field clock (shouldn't happen often)
2185 if (errCnt2
> 101) return errCnt2
;
2186 //avgWaveVal += dest[i + 1];
2193 //avgWaveVal += dest[i + 1];
2199 int pskRawDemod(uint8_t *dest
, size_t *size
, int *clock
, int *invert
) {
2201 return pskRawDemod_ext(dest
, size
, clock
, invert
, &start_idx
);
2205 // **********************************************************************************************
2206 // -----------------Tag format detection section-------------------------------------------------
2207 // **********************************************************************************************
2209 // FSK Demod then try to locate an AWID ID
2210 int detectAWID(uint8_t *dest
, size_t *size
, int *waveStartIdx
) {
2211 //make sure buffer has enough data (96bits * 50clock samples)
2212 if (*size
< 96 * 50) return -1;
2214 if (signalprop
.isnoise
) return -2;
2216 // FSK2a demodulator clock 50, invert 1, fcHigh 10, fcLow 8
2217 *size
= fskdemod(dest
, *size
, 50, 1, 10, 8, waveStartIdx
); //awid fsk2a
2219 //did we get a good demod?
2220 if (*size
< 96) return -3;
2222 size_t start_idx
= 0;
2223 uint8_t preamble
[] = {0, 0, 0, 0, 0, 0, 0, 1};
2224 if (!preambleSearch(dest
, preamble
, sizeof(preamble
), size
, &start_idx
))
2225 return -4; //preamble not found
2227 // wrong size? (between to preambles)
2228 if (*size
!= 96) return -5;
2230 return (int)start_idx
;
2233 // takes 1s and 0s and searches for EM410x format - output EM ID
2234 int Em410xDecode(uint8_t *bits
, size_t *size
, size_t *start_idx
, uint32_t *hi
, uint64_t *lo
) {
2236 if (bits
[1] > 1) return -1;
2237 if (*size
< 64) return -2;
2241 bool adjust
= false;
2246 // preamble 0111111111
2247 // include 0 in front to help get start pos
2248 uint8_t preamble
[] = {0, 1, 1, 1, 1, 1, 1, 1, 1, 1};
2249 if (!preambleSearch(bits
, preamble
, sizeof(preamble
), size
, start_idx
))
2252 bool validShort
= false;
2253 bool validShortExtended
= false;
2254 bool validLong
= false;
2256 // detect sledge of 0x05's
2258 uint8_t fives
[] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
2259 for (size_t x
= 0; x
< *size
- sizeof(fives
); x
+= 1) {
2260 if (memcmp(bits
+ x
, fives
, sizeof(fives
)) == 0) {
2261 // save first occasion
2269 size_t sidx
= *start_idx
+ sizeof(preamble
);
2279 // prnt("fix... %d size... %zu", fix, *size);
2284 *size
= removeEm410xParity(bits
, sidx
, size
, &validShort
, &validShortExtended
, &validLong
);
2287 // std em410x format
2289 *lo
= ((uint64_t)(bytebits_to_byte(bits
, 8)) << 32) | (bytebits_to_byte(bits
+ 8, 32));
2293 if (validShortExtended
|| validLong
) {
2294 // store in long em format
2295 *hi
= (bytebits_to_byte(bits
, 24));
2296 *lo
= ((uint64_t)(bytebits_to_byte(bits
+ 24, 32)) << 32) | (bytebits_to_byte(bits
+ 24 + 32, 32));
2298 // 4 = ShortExtended
2299 return ((int)validShortExtended
<< 2) + ((int)validLong
<< 1);
2304 // loop to get raw HID waveform then FSK demodulate the TAG ID from it
2305 int HIDdemodFSK(uint8_t *dest
, size_t *size
, uint32_t *hi2
, uint32_t *hi
, uint32_t *lo
, int *waveStartIdx
) {
2306 //make sure buffer has data
2307 if (*size
< 96 * 50) return -1;
2309 if (signalprop
.isnoise
) return -2;
2311 // FSK demodulator fsk2a so invert and fc/10/8
2312 *size
= fskdemod(dest
, *size
, 50, 1, 10, 8, waveStartIdx
); //hid fsk2a
2314 //did we get a good demod?
2315 if (*size
< 96 * 2) return -3;
2317 // 00011101 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1
2318 size_t start_idx
= 0;
2319 uint8_t preamble
[] = {0, 0, 0, 1, 1, 1, 0, 1};
2320 if (!preambleSearch(dest
, preamble
, sizeof(preamble
), size
, &start_idx
))
2321 return -4; //preamble not found
2323 // wrong size? (between to preambles)
2324 //if (*size != 96) return -5;
2326 size_t num_start
= start_idx
+ sizeof(preamble
);
2327 // final loop, go over previously decoded FSK data and manchester decode into usable tag ID
2328 for (size_t idx
= num_start
; (idx
- num_start
) < *size
- sizeof(preamble
); idx
+= 2) {
2329 if (dest
[idx
] == dest
[idx
+ 1]) {
2330 return -5; //not manchester data
2332 *hi2
= (*hi2
<< 1) | (*hi
>> 31);
2333 *hi
= (*hi
<< 1) | (*lo
>> 31);
2334 //Then, shift in a 0 or one into low
2336 if (dest
[idx
] && !dest
[idx
+ 1]) // 1 0
2341 return (int)start_idx
;
2344 int detectIOProx(uint8_t *dest
, size_t *size
, int *waveStartIdx
) {
2345 //make sure buffer has data
2346 if (*size
< 66 * 64) return -1;
2348 if (signalprop
.isnoise
) return -2;
2350 // FSK demodulator RF/64, fsk2a so invert, and fc/10/8
2351 *size
= fskdemod(dest
, *size
, 64, 1, 10, 8, waveStartIdx
); //io fsk2a
2353 //did we get enough demod data?
2354 if (*size
< 64) return -3;
2357 //0 10 20 30 40 50 60
2359 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
2360 //-----------------------------------------------------------------------------
2361 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
2363 //XSF(version)facility:codeone+codetwo
2365 size_t start_idx
= 0;
2366 uint8_t preamble
[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
2367 if (!preambleSearch(dest
, preamble
, sizeof(preamble
), size
, &start_idx
))
2368 return -4; //preamble not found
2370 // wrong size? (between to preambles)
2371 if (*size
!= 64) return -5;
2373 if (!dest
[start_idx
+ 8]
2374 && dest
[start_idx
+ 17] == 1
2375 && dest
[start_idx
+ 26] == 1
2376 && dest
[start_idx
+ 35] == 1
2377 && dest
[start_idx
+ 44] == 1
2378 && dest
[start_idx
+ 53] == 1) {
2379 //confirmed proper separator bits found
2380 //return start position
2381 return (int) start_idx
;