R&Y: Added DAY RTA Tapp Card and Additional BKK BEM Stored Value Card AIDs to `aid_de...
[RRG-proxmark3.git] / common / lfdemod.c
blob6a4dbdf73a48389c26ebfea891fd8c2c6fdc6e65
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
3 //
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.
8 //
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
18 // NOTES:
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
27 // the source.
29 // There are 4 main sections of code below:
31 // Utilities Section:
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
43 // marshmellow
44 //-----------------------------------------------------------------------------
46 #include "lfdemod.h"
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
61 #ifndef ON_DEVICE
62 #include "ui.h"
63 #include "util.h"
64 # include "cmddata.h"
65 # define prnt(args...) PrintAndLogEx(DEBUG, ## args );
66 #else
67 # include "dbprint.h"
68 uint8_t g_debugMode = 0;
69 # define prnt Dbprintf
70 #endif
72 static signal_t signalprop = { 255, -255, 0, 0, true };
73 signal_t *getSignalProperties(void) {
74 return &signalprop;
77 static void resetSignal(void) {
78 signalprop.low = 255;
79 signalprop.high = -255;
80 signalprop.mean = 0;
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);
95 #ifndef ON_DEVICE
96 static int cmp_uint8(const void *a, const void *b) {
97 if (*(const uint8_t *)a < * (const uint8_t *)b)
98 return -1;
99 else
100 return *(const uint8_t *)a > *(const uint8_t *)b;
102 #endif
104 void computeSignalProperties(const uint8_t *samples, uint32_t size) {
105 resetSignal();
107 if (samples == NULL || size < SIGNAL_MIN_SAMPLES) return;
109 uint32_t sum = 0;
110 uint32_t offset_size = size - SIGNAL_IGNORE_FIRST_SAMPLES;
112 #ifndef ON_DEVICE
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)]);
119 uint32_t cnt = 0;
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)
126 continue;
128 sum += samples[i];
129 cnt++;
131 if (cnt > 0)
132 signalprop.mean = sum / cnt;
133 else
134 signalprop.mean = 0;
135 #else
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];
139 sum += samples[i];
141 signalprop.mean = sum / offset_size;
142 #endif
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;
150 if (g_debugMode)
151 printSignal();
154 void removeSignalOffset(uint8_t *samples, uint32_t size) {
155 if (samples == NULL || size < SIGNAL_MIN_SAMPLES) {
156 return;
159 int acc_off = 0;
160 uint32_t offset_size = size - SIGNAL_IGNORE_FIRST_SAMPLES;
162 #ifndef ON_DEVICE
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)]);
170 int32_t cnt = 0;
171 for (uint32_t i = SIGNAL_IGNORE_FIRST_SAMPLES; i < size; i++) {
173 if (samples[i] < low10 || samples[i] > hi90)
174 continue;
176 acc_off += samples[i] - 128;
177 cnt++;
179 if (cnt > 0)
180 acc_off /= cnt;
181 else
182 acc_off = 0;
183 #else
184 for (uint32_t i = SIGNAL_IGNORE_FIRST_SAMPLES; i < size; i++)
185 acc_off += samples[i] - 128;
187 acc_off /= (int)offset_size;
188 #endif
190 // shift and saturate samples to center the mean
191 for (uint32_t i = 0; i < size; i++) {
192 if (acc_off > 0) {
193 samples[i] = (samples[i] >= acc_off) ? samples[i] - acc_off : 0;
195 if (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) {
204 // add fuzz.
205 *high = (signalprop.high * fuzzHi) / 100;
206 if (signalprop.low < 0) {
207 *low = (signalprop.low * fuzzLo) / 100;
208 } else {
209 uint8_t range = signalprop.high - signalprop.low;
211 *low = signalprop.low + ((range * (100 - fuzzLo)) / 100);
214 // if fuzzing to great and overlap
215 if (*high <= *low) {
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;
233 size_t bitCnt = 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
244 switch (pType) {
245 case 3:
246 if (bits[bitCnt] == 1) {
247 return 0;
249 break; // should be 0 spacer bit
250 case 2:
251 if (bits[bitCnt] == 0) {
252 return 0;
254 break; // should be 1 spacer bit
255 default:
256 if (parityTest(parityWd, pLen, pType) == 0) { return 0; }
257 break; // test parity
259 parityWd = 0;
261 // if we got here then all the parities passed
262 //return size
263 return bitCnt;
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;
268 size_t bitCnt = 0;
269 bool validColParity = false;
270 bool validRowParity = true;
271 bool validRowParitySkipColP = true;
272 *validShort = false;
273 *validShortExtended = false;
274 *validLong = false;
276 uint8_t blen = 55;
277 switch (*size) {
278 case 128:
279 blen = 110;
280 break;
281 case 80:
282 blen = 70;
283 break;
284 default:
285 blen = 55;
286 break;
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) {
295 break;
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;
316 } else {
317 validRowParitySkipColP &= parityTest(parityWd, 5, 0) != 0;
319 parityWd = 0;
322 if ((blen != 128) && validRowParitySkipColP && validColParity) {
323 *validShort = true;
326 if ((blen == 128) && validRowParity) {
327 *validLong = true;
330 if ((blen == 128) && validRowParitySkipColP && validColParity) {
331 *validShortExtended = true;
334 if (*validShort || *validShortExtended || *validLong) {
335 return bitCnt;
336 } else {
337 return 0;
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
353 switch (pType) {
354 case 3:
355 dest[j++] = 0;
356 break; // marker bit which should be a 0
357 case 2:
358 dest[j++] = 1;
359 break; // marker bit which should be a 1
360 default:
361 dest[j++] = parityTest(parityWd, pLen - 1, pType) ^ 1;
362 break;
364 bitCnt += pLen;
365 parityWd = 0;
367 // if we got here then all the parities passed
368 //return ID start index and size
369 return bitCnt;
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);
379 return PM3_SUCCESS;
382 uint32_t bytebits_to_byte(uint8_t *src, size_t numbits) {
383 uint32_t num = 0;
384 for (int i = 0 ; i < numbits ; i++) {
385 num = (num << 1) | (*src);
386 src++;
388 return num;
391 // least significant bit first
392 uint32_t bytebits_to_byteLSBF(uint8_t *src, size_t numbits) {
393 uint32_t num = 0;
394 for (int i = 0 ; i < numbits ; i++) {
395 num = (num << 1) | *(src + (numbits - (i + 1)));
397 return num;
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.
409 if (*size <= pLen)
410 return false;
412 uint8_t foundCnt = 0;
413 for (size_t idx = 0; idx < *size - pLen; idx++) {
414 if (memcmp(bits + idx, preamble, pLen) == 0) {
415 //first index found
416 foundCnt++;
417 if (foundCnt == 1) {
418 if (g_debugMode >= 1) prnt("DEBUG: (preambleSearchEx) preamble found at %zu", idx);
419 *startIdx = idx;
420 if (findone)
421 return true;
423 if (foundCnt == 2) {
424 if (g_debugMode >= 1) prnt("DEBUG: (preambleSearchEx) preamble 2 found at %zu", idx);
425 *size = idx - *startIdx;
426 return true;
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) {
436 size_t i = 0;
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) {
445 thresholdCnt++;
446 if (thresholdCnt > 2 && waveSizeCnt < expWaveSize + 1) {
447 break;
449 isAboveThreshold = false;
450 waveSizeCnt = 0;
452 } else if (src[i] >= signalprop.mean && !isAboveThreshold) {
453 thresholdCnt++;
454 if (thresholdCnt > 2 && waveSizeCnt < expWaveSize + 1) {
455 break;
457 isAboveThreshold = true;
458 waveSizeCnt = 0;
460 } else {
461 waveSizeCnt++;
464 if (thresholdCnt > 10) {
465 break;
469 if (g_debugMode == 2) {
470 prnt("DEBUG: threshold Count reached at index %zu, count: %u", i, thresholdCnt);
472 return i;
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])
481 return clocks[i];
483 return 0;
486 void getNextLow(const uint8_t *samples, size_t size, int low, size_t *i) {
487 while ((samples[*i] > low) && (*i < size))
488 *i += 1;
491 void getNextHigh(const uint8_t *samples, size_t size, int high, size_t *i) {
492 while ((samples[*i] < high) && (*i < size))
493 *i += 1;
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) {
498 size_t i = 0;
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");
504 return false;
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);
512 *skip = i;
514 // populate tmpbuff buffer with pulse lengths
515 while (i < size) {
516 // measure from low to low
517 size_t firstLow = i;
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))
525 break;
527 highToLowWaveLen[*waveCnt] = i - firstHigh; //first high to first low
528 lowToLowWaveLen[*waveCnt] = i - firstLow;
529 *waveCnt += 1;
530 if (i - firstLow < *minClk && i < size) {
531 *minClk = i - firstLow;
534 return true;
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]) {
545 waveEnd = i + 1;
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;
556 waveStart = i + 1;
557 avgWaveVal = 0;
559 avgWaveVal += samples[i + 2];
561 return 0;
564 // amplify based on ask edge detection - not accurate enough to use all the time
565 void askAmp(uint8_t *bits, size_t size) {
566 uint8_t last = 128;
567 for (size_t i = 1; i < size; ++i) {
568 if (bits[i] - bits[i - 1] >= 30) //large jump up
569 last = 255;
570 else if (bits[i - 1] - bits[i] >= 20) //large jump down
571 last = 0;
573 bits[i] = last;
577 // iceman, simplify this
578 uint32_t manchesterEncode2Bytes(uint16_t datain) {
579 uint32_t output = 0;
580 for (uint8_t i = 0; i < 16; i++) {
581 uint8_t b = (datain >> (15 - i) & 1);
582 output |= (1 << (((15 - i) * 2) + b));
584 return output;
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;
592 } else {
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;
605 size_t i;
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];
613 return i;
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;
623 // sanity check
624 if (loopEnd > size) loopEnd = size;
626 for (size_t i = 160; i < loopEnd; i++) {
628 if (dest[i] > low && dest[i] < high)
629 allArePeaks = false;
630 else {
631 cntPeaks++;
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;
641 return allArePeaks;
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) {
652 size_t i = 100;
653 size_t minClk = 768;
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);
660 if (i == size)
661 return -1;
662 if (size < 768)
663 return -2;
665 // clock, numoftimes, first idx
666 uint16_t tmpclk[11][3] = {
667 {8, 0, 0},
668 {16, 0, 0},
669 {32, 0, 0},
670 {40, 0, 0},
671 {50, 0, 0},
672 {64, 0, 0},
673 {100, 0, 0},
674 {128, 0, 0},
675 {256, 0, 0},
676 {272, 0, 0},
677 {384, 0, 0},
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);
695 if (foo > 0) {
696 for (uint8_t j = 0; j < 11; j++) {
697 if (tmpclk[j][0] == foo) {
698 tmpclk[j][1]++;
700 if (tmpclk[j][2] == 0) {
701 tmpclk[j][2] = shortestWaveIdx;
703 break;
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) {
713 possible_clks++;
717 uint16_t second_shortest = 0;
718 int second = 0;
719 int max = 0;
720 for (int j = 10; j > -1; j--) {
721 if (g_debugMode == 2) {
722 prnt("DEBUG, ASK, clocks %u | hits %u | idx %u"
723 , tmpclk[j][0]
724 , tmpclk[j][1]
725 , tmpclk[j][2]
729 if (max < tmpclk[j][1]) {
730 second = *clock;
731 second_shortest = shortestWaveIdx;
733 *clock = tmpclk[j][0];
734 shortestWaveIdx = tmpclk[j][2];
735 max = tmpclk[j][1];
739 // ASK clock 8 is very rare and usually gives us false positives
740 if (possible_clks > 1 && *clock == 8) {
741 *clock = second;
742 shortestWaveIdx = second_shortest;
745 if (*clock == 0)
746 return -1;
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");
762 return -1;
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");
768 return -2;
771 size_t i = 1;
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
777 size -= 60;
779 // What is purpose?
780 // already have a valid clock?
781 uint8_t found_clk = 0;
782 for (; i < num_clks; ++i) {
783 if (clk[i] == *clock) {
784 found_clk = i;
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
794 if (!found_clk) {
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
803 if (idx > -1)
804 return idx;
807 // test for weak peaks
809 // test clock if given as cmd parameter
810 if (*clock > 0)
811 clk[0] = *clock;
813 uint8_t clkCnt, tol;
814 size_t j = 0;
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;
819 if (found_clk) {
820 clkCnt = found_clk;
821 num_clks = found_clk + 1;
822 } else {
823 clkCnt = 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) {
829 tol = 1;
830 } else {
831 tol = 0;
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++) {
846 errCnt = 0;
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
855 errCnt++;
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) {
862 if (!found_clk)
863 *clock = clk[clkCnt];
864 return j;
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;
874 uint8_t k, best = 0;
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]) {
881 best = 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]);
887 bool chg = false;
888 for (i = 0; i < ARRAYLEN(bestErr); i++) {
889 chg = (bestErr[i] != 1000);
890 if (chg)
891 break;
892 chg = (bestStart[i] != 0);
893 if (chg)
894 break;
897 // just noise - no super good detection. good enough
898 if (chg == false) {
899 if (g_debugMode == 2) prnt("DEBUG DetectASKClock: no good values detected - aborting");
900 return -2;
903 if (!found_clk)
904 *clock = clk[best];
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
911 *strong = false;
912 size_t i = 0;
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))
919 ++i;
920 while ((dest[i] < peak && dest[i] > low) && (i < size))
921 ++i;
923 lastWasHigh = (dest[i] >= peak);
925 if (i == size)
926 return 0;
928 transition1 = i;
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;
935 transition1 = i;
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) {
946 *strong = true;
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) {
954 size_t i = 0;
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");
970 return 0;
973 //get high and low peak
974 int peak, low;
975 //getHiLo(dest, loopCnt, &peak, &low, 90, 90);
976 getHiLo(&peak, &low, 90, 90);
978 bool strong = false;
979 int lowestTransition = DetectStrongNRZClk(dest, size - 20, peak, low, &strong);
980 if (strong) return lowestTransition;
981 size_t ii;
982 uint8_t clkCnt;
983 uint8_t tol = 0;
984 uint16_t smplCnt = 0;
985 int16_t peakcnt = 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;
993 smplCnt++;
994 } else {
995 firstpeak = false;
996 if (smplCnt > 0) {
997 if (minPeak > smplCnt && smplCnt > 7) minPeak = smplCnt;
998 peakcnt++;
999 if (g_debugMode == 2) prnt("DEBUG NRZ: minPeak: %d, smplCnt: %d, peakcnt: %d", minPeak, smplCnt, peakcnt);
1000 smplCnt = 0;
1004 if (minPeak < 8) return 0;
1006 bool errBitHigh = 0, bitHigh = 0, lastPeakHigh = 0;
1007 uint8_t ignoreCnt = 0, ignoreWindow = 4;
1008 int lastBit = 0;
1009 size_t bestStart[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1010 peakcnt = 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)) {
1018 peakcnt = 0;
1019 bitHigh = false;
1020 ignoreCnt = 0;
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)) {
1026 //test high/low
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)) {
1030 peakcnt++;
1032 lastPeakHigh = (dest[i] >= peak);
1033 bitHigh = true;
1034 errBitHigh = false;
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) {
1043 bitHigh = false;
1044 if (errBitHigh == true)
1045 peakcnt--;
1046 errBitHigh = false;
1047 } else {
1048 ignoreCnt--;
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...
1053 errBitHigh = true;
1056 if (peakcnt > peaksdet[clkCnt]) {
1057 bestStart[clkCnt] = ii;
1058 peaksdet[clkCnt] = peakcnt;
1064 uint8_t best = 0;
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))) {
1068 best = m;
1070 } else if (peaksdet[m] > peaksdet[best]) {
1071 best = m;
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];
1076 return clk[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;
1088 size_t i;
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])
1094 break;
1096 for (; i < size - 20; i++) {
1097 if (bits[i] > bits[i - 1] && bits[i] >= bits[i + 1]) {
1098 // new up transition
1099 fcCounter++;
1100 if (fskAdj) {
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) {
1112 fcCnts[m]++;
1113 fcCounter = 0;
1114 break;
1117 if (fcCounter > 0 && fcLensFnd < 15) {
1118 //add new fc length
1119 fcCnts[fcLensFnd]++;
1120 fcLens[fcLensFnd++] = fcCounter;
1122 fcCounter = 0;
1123 } else {
1124 // count sample
1125 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) {
1135 best3 = best2;
1136 best2 = best1;
1137 maxCnt1 = fcCnts[i];
1138 best1 = i;
1139 } else if (fcCnts[i] > fcCnts[best2]) {
1140 best3 = best2;
1141 best2 = i;
1142 } else if (fcCnts[i] > fcCnts[best3]) {
1143 best3 = i;
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];
1154 } else {
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);
1183 *fc = fcs & 0xFF;
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;
1194 uint8_t clkCnt;
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;
1207 fullWaveLen = 0;
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) {
1231 waveStart = i + 1;
1232 } else { //waveEnd
1233 waveEnd = i + 1;
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
1239 peakcnt++;
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
1244 errCnt++;
1246 } else if (i + 1 > lastClkBit + clk[clkCnt] + tol + *fc) {
1247 lastClkBit += clk[clkCnt]; //no phase shift but clock bit
1249 waveStart = i + 1;
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
1259 uint8_t best = 9;
1260 for (i = 9; i >= 1; i--) {
1261 if (peaksdet[i] > peaksdet[best])
1262 best = i;
1264 if (g_debugMode == 2) prnt("DEBUG PSK: Clk: %d, peaks: %d, errs: %d, bestClk: %d", clk[i], peaksdet[i], bestErr[i], clk[best]);
1266 return 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) {
1272 if (size == 0)
1273 return 0;
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;
1283 size_t i;
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])
1289 break;
1291 for (; i < size - 20; i++) {
1292 fcCounter++;
1293 rfCounter++;
1295 if (bits[i] <= bits[i - 1] || bits[i] < bits[i + 1])
1296 continue;
1297 // else new peak
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)
1303 fcCounter = fcLow;
1304 else //set it to the large fc
1305 fcCounter = fcHigh;
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)) {
1313 rfCnts[ii]++;
1314 rfCounter = 0;
1315 break;
1318 if (rfCounter > 0 && rfLensFnd < 15) {
1319 //prnt("DEBUG: rfCntr %d, fcCntr %d",rfCounter,fcCounter);
1320 rfCnts[rfLensFnd]++;
1321 rfLens[rfLensFnd++] = rfCounter;
1323 } else {
1324 *firstClockEdge = i;
1325 firstBitFnd++;
1327 rfCounter = 0;
1328 lastFCcnt = fcCounter;
1330 fcCounter = 0;
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;
1339 rfHighest = i;
1340 } else if (rfCnts[i] > rfCnts[rfHighest2]) {
1341 rfHighest3 = rfHighest2;
1342 rfHighest2 = i;
1343 } else if (rfCnts[i] > rfCnts[rfHighest3]) {
1344 rfHighest3 = i;
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)
1359 int m = 7;
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]);
1366 break;
1372 if (m < 2) return 0; // oops we went too far
1374 return clk[m];
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;
1396 return true;
1402 return false;
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
1409 int clk = 0;
1410 int tol = 0;
1411 int j = 0, high, low, skip = 0, start = 0, end = 0, minClk = 255;
1412 size_t i = 0;
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;
1417 int phaseoff = 0;
1418 high = low = 128;
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
1426 if (!clk) {
1427 if (g_debugMode == 2) prnt("DEBUG STT: clock not found - quitting");
1428 return false;
1430 *foundclock = clk;
1432 tol = clk / 8;
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");
1436 return false;
1437 } else {
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)
1441 phaseoff = 0;
1442 else
1443 phaseoff = clk / 2;
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
1449 int dummy1 = 0;
1450 end = skip;
1451 i += 3;
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");
1455 return false;
1457 end -= phaseoff;
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
1460 start = skip;
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;
1469 } else {
1470 if (g_debugMode == 2) prnt("DEBUG STT: datalen not divisible by clk: %zu %% %d = %zu - quitting", datalen, clk, datalen % clk);
1471 return false;
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");
1476 return false;
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) {
1483 dataloc -= i;
1484 break;
1489 size_t newloc = 0;
1490 i = 0;
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];
1506 if (firstrun) {
1507 *stend = dataloc;
1508 *ststart = dataloc - (clk * 4);
1509 firstrun = false;
1511 for (i = 0; i < datalen; ++i) {
1512 if (i + newloc < bufsize) {
1513 if (i + newloc < dataloc)
1514 buffer[i + newloc] = buffer[dataloc];
1516 dataloc++;
1519 newloc += i;
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));
1522 dataloc += clk * 4;
1524 *size = newloc;
1525 return true;
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;
1543 curBit = bits[i];
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");
1549 return -1;
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) {
1555 errCnt++;
1556 bits[bitCnt++] = 7;
1557 continue;
1559 bits[bitCnt++] = bits[i] ^ bits[i + 1] ^ invert;
1561 if (bitCnt > MaxBits) break;
1563 *size = bitCnt;
1564 return errCnt;
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) {
1572 //sanity check
1573 if (*size < 51) return -1;
1575 if (*offset < 0) *offset = 0;
1577 uint16_t bitnum = 0;
1578 uint16_t errCnt = 0;
1579 size_t i = *offset;
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;
1590 // main loop
1591 for (i = *offset; i < *size - 1; i += 2) {
1592 //check for phase error
1593 if (bits[i + 1] == bits[i + 2]) {
1594 bits[bitnum++] = 7;
1595 errCnt++;
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;
1601 } else {
1602 bits[bitnum++] = 7;
1603 errCnt++;
1605 if (bitnum > maxbits) break;
1607 *size = bitnum;
1608 return errCnt;
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) {
1616 // sanity check
1617 if (*size < 16) {
1618 return 0xFFFF;
1621 int errCnt = 0, bestErr = 1000;
1622 uint16_t bitnum = 0, maxBits = MAX_DEMODULATION_BITS, bestRun = 0;
1623 size_t i;
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]) {
1631 errCnt++;
1634 if (errCnt > 50) {
1635 break;
1639 if (bestErr > errCnt) {
1641 bestErr = errCnt;
1642 bestRun = k;
1644 if (g_debugMode == 2) prnt("DEBUG manrawdecode: bestErr %d | bestRun %u", bestErr, bestRun);
1647 errCnt = 0;
1650 *alignPos = bestRun;
1651 // decode
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;
1658 } else {
1659 bits[bitnum++] = 7;
1662 if (bitnum > maxBits) {
1663 break;
1667 *size = bitnum;
1668 return bestErr;
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) {
1674 *startIdx = 0;
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) {
1691 smplCnt++;
1692 } else if (bits[i] <= low && !waveHigh) {
1693 smplCnt++;
1694 } else {
1695 //transition
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) {
1705 //too many samples
1706 errCnt++;
1707 if (g_debugMode == 2) prnt("DEBUG ASK: cleanAskRawDemod ASK Modulation Error FULL at: %zu [%zu > %u]", i, smplCnt, clk + cl_4 + 1);
1708 bits[bitCnt++] = 7;
1709 } else if (waveHigh) {
1710 bits[bitCnt++] = invert;
1711 bits[bitCnt++] = invert;
1712 } else {
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;
1721 smplCnt = 0;
1723 // 16-8-1 = 7
1724 } else if (smplCnt > cl_2 - cl_4 - 1) { //half clock
1726 if (smplCnt > cl_2 + cl_4 + 1) { //too many samples
1727 errCnt++;
1728 if (g_debugMode == 2) prnt("DEBUG ASK: cleanAskRawDemod ASK Modulation Error HALF at: %zu [%zu]", i, smplCnt);
1729 bits[bitCnt++] = 7;
1732 if (waveHigh) {
1733 bits[bitCnt++] = invert;
1734 } else {
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;
1743 smplCnt = 0;
1744 } else {
1745 smplCnt++;
1746 //transition bit oops
1748 } else { //haven't hit new high or new low yet
1749 smplCnt++;
1754 *size = bitCnt;
1756 if (g_debugMode == 2) prnt("DEBUG ASK: cleanAskRawDemod Startidx %d", *startIdx);
1758 return errCnt;
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");
1768 return -2;
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.
1777 // ICEMAN todo,
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]
1784 int high, low;
1785 getHiLo(&high, &low, 75, 75);
1787 size_t errCnt = 0;
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);
1805 return 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
1813 uint8_t midBit = 0;
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) {
1826 if (bitnum > 0) {
1827 // if (g_debugMode == 2) prnt("DEBUG: (askdemod_ext) Modulation Error at: %u", i);
1828 bits[bitnum++] = 7;
1829 errCnt++;
1831 } else { //in tolerance - looking for peak
1832 continue;
1834 midBit = 0;
1835 lastBit += *clk;
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) {
1842 if (bitnum > 0) {
1843 bits[bitnum] = bits[bitnum - 1];
1844 bitnum++;
1845 } else {
1846 bits[bitnum] = 0;
1847 bitnum++;
1849 } else { //in tolerance - looking for peak
1850 continue;
1852 midBit = 1;
1854 if (bitnum >= MaxBits) break;
1856 *size = bitnum;
1857 return errCnt;
1860 int askdemod(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType) {
1861 int start = 0;
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");
1871 return -1;
1874 size_t clkStartIdx = 0;
1875 *clk = DetectNRZClock(dest, *size, *clk, &clkStartIdx);
1876 if (*clk == 0) return -2;
1878 size_t i;
1879 int high, low;
1881 getHiLo(&high, &low, 75, 75);
1883 uint8_t bit = 0;
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;
1888 dest[i] = bit;
1890 //now demod based on clock (rf/32 = 32 1's for one 1 bit, 32 0's for one 0 bit)
1891 size_t lastBit = 0;
1892 size_t numBits = 0;
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;
1898 if (lastBit == 0) {
1899 *startIdx = i - (numBits * *clk);
1900 if (g_debugMode == 2) prnt("DEBUG NRZ: startIdx %i", *startIdx);
1902 lastBit = i - 1;
1905 *size = numBits;
1906 return 0;
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;
1928 idx++;
1930 // Definition: cycles between consecutive lo-hi transitions
1931 // Lets define some expected lengths. FSK1 is easier since it has bigger differences between.
1932 // FSK1 8/5
1933 // 50/8 = 6 | 40/8 = 5 | 64/8 = 8
1934 // 50/5 = 10 | 40/5 = 8 | 64/5 = 12
1936 // FSK2 10/8
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.
1943 // FSK1 8/5
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)
1947 // FSK2 10/8
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.
1954 // process:
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..
1985 numBits = 0;
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];
2008 size_t i = 0;
2009 size_t numBits = 0;
2010 uint32_t n = 1;
2011 uint8_t hclk = clk / 2;
2013 for (i = 1; i < size; i++) {
2014 n++;
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;
2022 else
2023 // 0->1 crossing
2024 n = (n * fchigh + hclk) / clk;
2026 if (n == 0)
2027 n = 1;
2029 //first transition - save startidx
2030 if (numBits == 0) {
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);
2034 } else {
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);
2043 numBits += n;
2044 n = 0;
2045 lastval = dest[i];
2047 }//end for
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;
2053 } else {
2054 n = (n * fchigh + clk / 2) / clk;
2056 memset(dest + numBits, dest[i - 1] ^ invert, n);
2057 numBits += n;
2058 if (g_debugMode == 2) prnt("DEBUG (aggregate_bits) extra bits in the end");
2060 return numBits;
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;
2066 // FSK demodulator
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);
2071 return 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++) {
2080 //ignore errors
2081 if (bits[i] == 7) continue;
2083 if (lastbit != bits[i]) {
2084 lastbit = bits[i];
2085 bits[i] = 1;
2086 } else {
2087 bits[i] = 0;
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) {
2095 uint8_t phase = 0;
2096 for (size_t i = 0; i < size; i++) {
2097 if (bits[i] == 1) {
2098 phase ^= 1;
2100 bits[i] = phase;
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) {
2109 // sanity check
2110 if (*size < 170) return -1;
2112 uint8_t curPhase = *invert;
2113 uint8_t fc = 0;
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);
2133 } else {
2134 memset(dest, curPhase ^ 1, firstFullWave / *clock);
2136 } else {
2137 memset(dest, curPhase ^ 1, firstFullWave / *clock);
2139 //advance bits
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);
2149 waveStart = 0;
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) {
2155 waveStart = i + 1;
2156 //avgWaveVal = dest[i + 1];
2157 } else { //waveEnd
2158 waveEnd = 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"
2164 , waveStart
2165 , waveLenCnt
2166 , lastClkBit + *clock - tol
2167 , i + 1
2168 , fc);
2170 if (i + 1 >= lastClkBit + *clock - tol) { //should be a clock bit
2171 curPhase ^= 1;
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
2177 errCnt++;
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)
2184 errCnt2++;
2185 if (errCnt2 > 101) return errCnt2;
2186 //avgWaveVal += dest[i + 1];
2187 continue;
2189 //avgWaveVal = 0;
2190 waveStart = i + 1;
2193 //avgWaveVal += dest[i + 1];
2195 *size = numBits;
2196 return errCnt;
2199 int pskRawDemod(uint8_t *dest, size_t *size, int *clock, int *invert) {
2200 int start_idx = 0;
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) {
2235 // sanity check
2236 if (bits[1] > 1) return -1;
2237 if (*size < 64) return -2;
2239 *start_idx = 0;
2241 bool adjust = false;
2242 if (*size < 128) {
2243 adjust = true;
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))
2250 return -4;
2252 bool validShort = false;
2253 bool validShortExtended = false;
2254 bool validLong = false;
2256 // detect sledge of 0x05's
2257 int fix = -1;
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
2262 if (fix == -1) {
2263 fix = x;
2264 break;
2269 size_t sidx = *start_idx + sizeof(preamble);
2270 if (adjust) {
2271 sidx--;
2273 // not 128 or 55..
2274 if (fix != -1) {
2275 *size = 80;
2278 #ifndef ON_DEVICE
2279 // prnt("fix... %d size... %zu", fix, *size);
2280 #endif
2283 // HACK
2284 *size = removeEm410xParity(bits, sidx, size, &validShort, &validShortExtended, &validLong);
2286 if (validShort) {
2287 // std em410x format
2288 *hi = 0;
2289 *lo = ((uint64_t)(bytebits_to_byte(bits, 8)) << 32) | (bytebits_to_byte(bits + 8, 32));
2290 // 1 = Short
2291 return 1;
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));
2297 // 2 = Long
2298 // 4 = ShortExtended
2299 return ((int)validShortExtended << 2) + ((int)validLong << 1);
2301 return -6;
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
2335 *lo <<= 1;
2336 if (dest[idx] && !dest[idx + 1]) // 1 0
2337 *lo |= 1;
2338 else // 0 1
2339 *lo |= 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;
2356 //Index map
2357 //0 10 20 30 40 50 60
2358 //| | | | | | |
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;
2383 return -6;