]>
Commit | Line | Data |
---|---|---|
eb191de6 | 1 | //----------------------------------------------------------------------------- |
ba1a299c | 2 | // Copyright (C) 2014 |
eb191de6 | 3 | // |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
1e090a61 | 8 | // Low frequency demod/decode commands |
eb191de6 | 9 | //----------------------------------------------------------------------------- |
10 | ||
eb191de6 | 11 | #include <stdlib.h> |
eb191de6 | 12 | #include "lfdemod.h" |
05164399 | 13 | #include <string.h> |
6426f6ba | 14 | |
a126332a | 15 | //un_comment to allow debug print calls when used not on device |
6426f6ba | 16 | void dummy(char *fmt, ...){} |
17 | ||
18 | #ifndef ON_DEVICE | |
19 | #include "ui.h" | |
a126332a | 20 | #include "cmdparser.h" |
21 | #include "cmddata.h" | |
6426f6ba | 22 | #define prnt PrintAndLog |
23 | #else | |
a126332a | 24 | uint8_t g_debugMode=0; |
6426f6ba | 25 | #define prnt dummy |
26 | #endif | |
6426f6ba | 27 | |
a1d17964 | 28 | uint8_t justNoise(uint8_t *BitStream, size_t size) |
29 | { | |
30 | static const uint8_t THRESHOLD = 123; | |
31 | //test samples are not just noise | |
32 | uint8_t justNoise1 = 1; | |
33 | for(size_t idx=0; idx < size && justNoise1 ;idx++){ | |
34 | justNoise1 = BitStream[idx] < THRESHOLD; | |
35 | } | |
36 | return justNoise1; | |
37 | } | |
38 | ||
1e090a61 | 39 | //by marshmellow |
872e3d4d | 40 | //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 |
1e090a61 | 41 | int getHiLo(uint8_t *BitStream, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo) |
42 | { | |
43 | *high=0; | |
44 | *low=255; | |
45 | // get high and low thresholds | |
2eec55c8 | 46 | for (size_t i=0; i < size; i++){ |
1e090a61 | 47 | if (BitStream[i] > *high) *high = BitStream[i]; |
48 | if (BitStream[i] < *low) *low = BitStream[i]; | |
49 | } | |
50 | if (*high < 123) return -1; // just noise | |
75cbbe9a | 51 | *high = ((*high-128)*fuzzHi + 12800)/100; |
52 | *low = ((*low-128)*fuzzLo + 12800)/100; | |
1e090a61 | 53 | return 1; |
54 | } | |
55 | ||
a1d17964 | 56 | // by marshmellow |
57 | // pass bits to be tested in bits, length bits passed in bitLen, and parity type (even=0 | odd=1) in pType | |
58 | // returns 1 if passed | |
59 | uint8_t parityTest(uint32_t bits, uint8_t bitLen, uint8_t pType) | |
60 | { | |
61 | uint8_t ans = 0; | |
62 | for (uint8_t i = 0; i < bitLen; i++){ | |
63 | ans ^= ((bits >> i) & 1); | |
64 | } | |
f3bf15e4 | 65 | //PrintAndLog("DEBUG: ans: %d, ptype: %d",ans,pType); |
a1d17964 | 66 | return (ans == pType); |
67 | } | |
68 | ||
a126332a | 69 | //by marshmellow |
70 | // takes a array of binary values, start position, length of bits per parity (includes parity bit), | |
71 | // Parity Type (1 for odd; 0 for even; 2 Always 1's), and binary Length (length to run) | |
72 | size_t removeParity(uint8_t *BitStream, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen) | |
73 | { | |
74 | uint32_t parityWd = 0; | |
75 | size_t j = 0, bitCnt = 0; | |
76 | for (int word = 0; word < (bLen); word+=pLen){ | |
77 | for (int bit=0; bit < pLen; bit++){ | |
78 | parityWd = (parityWd << 1) | BitStream[startIdx+word+bit]; | |
79 | BitStream[j++] = (BitStream[startIdx+word+bit]); | |
80 | } | |
81 | j--; // overwrite parity with next data | |
82 | // if parity fails then return 0 | |
83 | if (pType == 2) { // then marker bit which should be a 1 | |
84 | if (!BitStream[j]) return 0; | |
85 | } else { | |
86 | if (parityTest(parityWd, pLen, pType) == 0) return 0; | |
87 | } | |
88 | bitCnt+=(pLen-1); | |
89 | parityWd = 0; | |
90 | } | |
91 | // if we got here then all the parities passed | |
92 | //return ID start index and size | |
93 | return bitCnt; | |
94 | } | |
95 | ||
96 | // by marshmellow | |
97 | // takes a array of binary values, length of bits per parity (includes parity bit), | |
0d2c5909 | 98 | // Parity Type (1 for odd; 0 for even; 2 Always 1's; 3 Always 0's), and binary Length (length to run) |
a126332a | 99 | size_t addParity(uint8_t *BitSource, uint8_t *dest, uint8_t sourceLen, uint8_t pLen, uint8_t pType) |
100 | { | |
101 | uint32_t parityWd = 0; | |
102 | size_t j = 0, bitCnt = 0; | |
103 | for (int word = 0; word < sourceLen; word+=pLen-1) { | |
104 | for (int bit=0; bit < pLen-1; bit++){ | |
105 | parityWd = (parityWd << 1) | BitSource[word+bit]; | |
106 | dest[j++] = (BitSource[word+bit]); | |
107 | } | |
0d2c5909 | 108 | |
a126332a | 109 | // if parity fails then return 0 |
0d2c5909 | 110 | switch (pType) { |
111 | case 3: dest[j++]=0; break; // marker bit which should be a 0 | |
112 | case 2: dest[j++]=1; break; // marker bit which should be a 1 | |
113 | default: | |
114 | dest[j++] = parityTest(parityWd, pLen-1, pType) ^ 1; | |
115 | break; | |
a126332a | 116 | } |
0d2c5909 | 117 | |
a126332a | 118 | bitCnt += pLen; |
119 | parityWd = 0; | |
120 | } | |
121 | // if we got here then all the parities passed | |
122 | //return ID start index and size | |
123 | return bitCnt; | |
124 | } | |
125 | ||
126 | uint32_t bytebits_to_byte(uint8_t *src, size_t numbits) | |
127 | { | |
128 | uint32_t num = 0; | |
0d2c5909 | 129 | for(int i = 0 ; i < numbits ; i++) { |
a126332a | 130 | num = (num << 1) | (*src); |
131 | src++; | |
132 | } | |
133 | return num; | |
134 | } | |
135 | ||
136 | //least significant bit first | |
137 | uint32_t bytebits_to_byteLSBF(uint8_t *src, size_t numbits) | |
138 | { | |
139 | uint32_t num = 0; | |
6b6815bc | 140 | for(int i = 0 ; i < numbits ; i++) { |
a126332a | 141 | num = (num << 1) | *(src + (numbits-(i+1))); |
142 | } | |
143 | return num; | |
144 | } | |
145 | ||
a1d17964 | 146 | //by marshmellow |
2147c307 | 147 | //search for given preamble in given BitStream and return success=1 or fail=0 and startIndex and length |
a1d17964 | 148 | uint8_t preambleSearch(uint8_t *BitStream, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx) |
149 | { | |
e0165dcf | 150 | uint8_t foundCnt=0; |
151 | for (int idx=0; idx < *size - pLen; idx++){ | |
152 | if (memcmp(BitStream+idx, preamble, pLen) == 0){ | |
153 | //first index found | |
154 | foundCnt++; | |
155 | if (foundCnt == 1){ | |
156 | *startIdx = idx; | |
157 | } | |
158 | if (foundCnt == 2){ | |
159 | *size = idx - *startIdx; | |
160 | return 1; | |
161 | } | |
162 | } | |
163 | } | |
164 | return 0; | |
a1d17964 | 165 | } |
166 | ||
2147c307 | 167 | //by marshmellow |
168 | //takes 1s and 0s and searches for EM410x format - output EM ID | |
169 | uint8_t Em410xDecode(uint8_t *BitStream, size_t *size, size_t *startIdx, uint32_t *hi, uint64_t *lo) | |
170 | { | |
e0165dcf | 171 | //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future |
172 | // otherwise could be a void with no arguments | |
173 | //set defaults | |
174 | uint32_t i = 0; | |
2767fc02 | 175 | if (BitStream[1]>1) return 0; //allow only 1s and 0s |
176 | ||
e0165dcf | 177 | // 111111111 bit pattern represent start of frame |
178 | // include 0 in front to help get start pos | |
179 | uint8_t preamble[] = {0,1,1,1,1,1,1,1,1,1}; | |
180 | uint32_t idx = 0; | |
181 | uint32_t parityBits = 0; | |
182 | uint8_t errChk = 0; | |
183 | uint8_t FmtLen = 10; | |
184 | *startIdx = 0; | |
185 | errChk = preambleSearch(BitStream, preamble, sizeof(preamble), size, startIdx); | |
186 | if (errChk == 0 || *size < 64) return 0; | |
187 | if (*size > 64) FmtLen = 22; | |
188 | *startIdx += 1; //get rid of 0 from preamble | |
189 | idx = *startIdx + 9; | |
190 | for (i=0; i<FmtLen; i++){ //loop through 10 or 22 sets of 5 bits (50-10p = 40 bits or 88 bits) | |
191 | parityBits = bytebits_to_byte(BitStream+(i*5)+idx,5); | |
2eec55c8 | 192 | //check even parity - quit if failed |
193 | if (parityTest(parityBits, 5, 0) == 0) return 0; | |
e0165dcf | 194 | //set uint64 with ID from BitStream |
195 | for (uint8_t ii=0; ii<4; ii++){ | |
196 | *hi = (*hi << 1) | (*lo >> 63); | |
197 | *lo = (*lo << 1) | (BitStream[(i*5)+ii+idx]); | |
198 | } | |
199 | } | |
200 | if (errChk != 0) return 1; | |
201 | //skip last 5 bit parity test for simplicity. | |
202 | // *size = 64 | 128; | |
203 | return 0; | |
2147c307 | 204 | } |
205 | ||
fef74fdc | 206 | //by marshmellow |
207 | //demodulates strong heavily clipped samples | |
23f0a7d8 | 208 | int cleanAskRawDemod(uint8_t *BinStream, size_t *size, int clk, int invert, int high, int low) |
209 | { | |
210 | size_t bitCnt=0, smplCnt=0, errCnt=0; | |
211 | uint8_t waveHigh = 0; | |
23f0a7d8 | 212 | for (size_t i=0; i < *size; i++){ |
213 | if (BinStream[i] >= high && waveHigh){ | |
214 | smplCnt++; | |
215 | } else if (BinStream[i] <= low && !waveHigh){ | |
216 | smplCnt++; | |
217 | } else { //transition | |
218 | if ((BinStream[i] >= high && !waveHigh) || (BinStream[i] <= low && waveHigh)){ | |
219 | if (smplCnt > clk-(clk/4)-1) { //full clock | |
220 | if (smplCnt > clk + (clk/4)+1) { //too many samples | |
221 | errCnt++; | |
05164399 | 222 | if (g_debugMode==2) prnt("DEBUG ASK: Modulation Error at: %u", i); |
2767fc02 | 223 | BinStream[bitCnt++]=7; |
23f0a7d8 | 224 | } else if (waveHigh) { |
225 | BinStream[bitCnt++] = invert; | |
226 | BinStream[bitCnt++] = invert; | |
227 | } else if (!waveHigh) { | |
228 | BinStream[bitCnt++] = invert ^ 1; | |
229 | BinStream[bitCnt++] = invert ^ 1; | |
230 | } | |
231 | waveHigh ^= 1; | |
232 | smplCnt = 0; | |
233 | } else if (smplCnt > (clk/2) - (clk/4)-1) { | |
234 | if (waveHigh) { | |
235 | BinStream[bitCnt++] = invert; | |
236 | } else if (!waveHigh) { | |
237 | BinStream[bitCnt++] = invert ^ 1; | |
238 | } | |
239 | waveHigh ^= 1; | |
240 | smplCnt = 0; | |
241 | } else if (!bitCnt) { | |
242 | //first bit | |
243 | waveHigh = (BinStream[i] >= high); | |
244 | smplCnt = 1; | |
245 | } else { | |
246 | smplCnt++; | |
247 | //transition bit oops | |
248 | } | |
249 | } else { //haven't hit new high or new low yet | |
250 | smplCnt++; | |
251 | } | |
252 | } | |
253 | } | |
254 | *size = bitCnt; | |
255 | return errCnt; | |
256 | } | |
257 | ||
eb191de6 | 258 | //by marshmellow |
fef74fdc | 259 | void askAmp(uint8_t *BitStream, size_t size) |
260 | { | |
261 | for(size_t i = 1; i<size; i++){ | |
262 | if (BitStream[i]-BitStream[i-1]>=30) //large jump up | |
263 | BitStream[i]=127; | |
264 | else if(BitStream[i]-BitStream[i-1]<=-20) //large jump down | |
265 | BitStream[i]=-127; | |
266 | } | |
267 | return; | |
268 | } | |
269 | ||
270 | //by marshmellow | |
271 | //attempts to demodulate ask modulations, askType == 0 for ask/raw, askType==1 for ask/manchester | |
272 | int askdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType) | |
eb191de6 | 273 | { |
fef74fdc | 274 | if (*size==0) return -1; |
6e984446 | 275 | int start = DetectASKClock(BinStream, *size, clk, maxErr); //clock default |
2eec55c8 | 276 | if (*clk==0 || start < 0) return -3; |
fef74fdc | 277 | if (*invert != 1) *invert = 0; |
278 | if (amp==1) askAmp(BinStream, *size); | |
05164399 | 279 | if (g_debugMode==2) prnt("DEBUG ASK: clk %d, beststart %d", *clk, start); |
fef74fdc | 280 | |
2eec55c8 | 281 | uint8_t initLoopMax = 255; |
282 | if (initLoopMax > *size) initLoopMax = *size; | |
ba1a299c | 283 | // Detect high and lows |
fef74fdc | 284 | //25% clip in case highs and lows aren't clipped [marshmellow] |
2eec55c8 | 285 | int high, low; |
fef74fdc | 286 | if (getHiLo(BinStream, initLoopMax, &high, &low, 75, 75) < 1) |
287 | return -2; //just noise | |
ba1a299c | 288 | |
fef74fdc | 289 | size_t errCnt = 0; |
23f0a7d8 | 290 | // if clean clipped waves detected run alternate demod |
291 | if (DetectCleanAskWave(BinStream, *size, high, low)) { | |
05164399 | 292 | if (g_debugMode==2) prnt("DEBUG ASK: Clean Wave Detected - using clean wave demod"); |
fef74fdc | 293 | errCnt = cleanAskRawDemod(BinStream, size, *clk, *invert, high, low); |
294 | if (askType) //askman | |
295 | return manrawdecode(BinStream, size, 0); | |
296 | else //askraw | |
297 | return errCnt; | |
23f0a7d8 | 298 | } |
05164399 | 299 | if (g_debugMode==2) prnt("DEBUG ASK: Weak Wave Detected - using weak wave demod"); |
23f0a7d8 | 300 | |
fef74fdc | 301 | int lastBit; //set first clock check - can go negative |
302 | size_t i, bitnum = 0; //output counter | |
303 | uint8_t midBit = 0; | |
2eec55c8 | 304 | 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 |
fef74fdc | 305 | 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 |
05164399 | 306 | size_t MaxBits = 3072; //max bits to collect |
6e984446 | 307 | lastBit = start - *clk; |
fef74fdc | 308 | |
6e984446 | 309 | for (i = start; i < *size; ++i) { |
fef74fdc | 310 | if (i-lastBit >= *clk-tol){ |
311 | if (BinStream[i] >= high) { | |
312 | BinStream[bitnum++] = *invert; | |
313 | } else if (BinStream[i] <= low) { | |
314 | BinStream[bitnum++] = *invert ^ 1; | |
315 | } else if (i-lastBit >= *clk+tol) { | |
316 | if (bitnum > 0) { | |
05164399 | 317 | if (g_debugMode==2) prnt("DEBUG ASK: Modulation Error at: %u", i); |
fef74fdc | 318 | BinStream[bitnum++]=7; |
319 | errCnt++; | |
320 | } | |
321 | } else { //in tolerance - looking for peak | |
322 | continue; | |
323 | } | |
324 | midBit = 0; | |
2eec55c8 | 325 | lastBit += *clk; |
fef74fdc | 326 | } else if (i-lastBit >= (*clk/2-tol) && !midBit && !askType){ |
327 | if (BinStream[i] >= high) { | |
328 | BinStream[bitnum++] = *invert; | |
329 | } else if (BinStream[i] <= low) { | |
330 | BinStream[bitnum++] = *invert ^ 1; | |
331 | } else if (i-lastBit >= *clk/2+tol) { | |
332 | BinStream[bitnum] = BinStream[bitnum-1]; | |
333 | bitnum++; | |
334 | } else { //in tolerance - looking for peak | |
335 | continue; | |
336 | } | |
337 | midBit = 1; | |
2eec55c8 | 338 | } |
339 | if (bitnum >= MaxBits) break; | |
ba1a299c | 340 | } |
2eec55c8 | 341 | *size = bitnum; |
6e984446 | 342 | return errCnt; |
eb191de6 | 343 | } |
344 | ||
345 | //by marshmellow | |
346 | //take 10 and 01 and manchester decode | |
347 | //run through 2 times and take least errCnt | |
fef74fdc | 348 | int manrawdecode(uint8_t * BitStream, size_t *size, uint8_t invert) |
eb191de6 | 349 | { |
13d77ef9 | 350 | uint16_t bitnum=0, MaxBits = 512, errCnt = 0; |
351 | size_t i, ii; | |
352 | uint16_t bestErr = 1000, bestRun = 0; | |
fef74fdc | 353 | if (*size < 16) return -1; |
2767fc02 | 354 | //find correct start position [alignment] |
13d77ef9 | 355 | for (ii=0;ii<2;++ii){ |
fef74fdc | 356 | for (i=ii; i<*size-3; i+=2) |
2eec55c8 | 357 | if (BitStream[i]==BitStream[i+1]) |
ba1a299c | 358 | errCnt++; |
2eec55c8 | 359 | |
ba1a299c | 360 | if (bestErr>errCnt){ |
361 | bestErr=errCnt; | |
362 | bestRun=ii; | |
363 | } | |
364 | errCnt=0; | |
365 | } | |
2767fc02 | 366 | //decode |
fef74fdc | 367 | for (i=bestRun; i < *size-3; i+=2){ |
23f0a7d8 | 368 | if(BitStream[i] == 1 && (BitStream[i+1] == 0)){ |
fef74fdc | 369 | BitStream[bitnum++]=invert; |
23f0a7d8 | 370 | } else if((BitStream[i] == 0) && BitStream[i+1] == 1){ |
fef74fdc | 371 | BitStream[bitnum++]=invert^1; |
23f0a7d8 | 372 | } else { |
2767fc02 | 373 | BitStream[bitnum++]=7; |
ba1a299c | 374 | } |
23f0a7d8 | 375 | if(bitnum>MaxBits) break; |
ba1a299c | 376 | } |
23f0a7d8 | 377 | *size=bitnum; |
2eec55c8 | 378 | return bestErr; |
f822a063 | 379 | } |
380 | ||
1d0ccbe0 | 381 | uint32_t manchesterEncode2Bytes(uint16_t datain) { |
382 | uint32_t output = 0; | |
383 | uint8_t curBit = 0; | |
384 | for (uint8_t i=0; i<16; i++) { | |
385 | curBit = (datain >> (15-i) & 1); | |
386 | output |= (1<<(((15-i)*2)+curBit)); | |
387 | } | |
388 | return output; | |
389 | } | |
390 | ||
fef74fdc | 391 | //by marshmellow |
392 | //encode binary data into binary manchester | |
393 | int ManchesterEncode(uint8_t *BitStream, size_t size) | |
394 | { | |
395 | size_t modIdx=20000, i=0; | |
396 | if (size>modIdx) return -1; | |
397 | for (size_t idx=0; idx < size; idx++){ | |
398 | BitStream[idx+modIdx++] = BitStream[idx]; | |
399 | BitStream[idx+modIdx++] = BitStream[idx]^1; | |
400 | } | |
401 | for (; i<(size*2); i++){ | |
402 | BitStream[i] = BitStream[i+20000]; | |
403 | } | |
404 | return i; | |
405 | } | |
406 | ||
f822a063 | 407 | //by marshmellow |
2147c307 | 408 | //take 01 or 10 = 1 and 11 or 00 = 0 |
409 | //check for phase errors - should never have 111 or 000 should be 01001011 or 10110100 for 1010 | |
13d77ef9 | 410 | //decodes biphase or if inverted it is AKA conditional dephase encoding AKA differential manchester encoding |
1e090a61 | 411 | int BiphaseRawDecode(uint8_t *BitStream, size_t *size, int offset, int invert) |
f822a063 | 412 | { |
2eec55c8 | 413 | uint16_t bitnum = 0; |
414 | uint16_t errCnt = 0; | |
415 | size_t i = offset; | |
2147c307 | 416 | uint16_t MaxBits=512; |
417 | //if not enough samples - error | |
418 | if (*size < 51) return -1; | |
419 | //check for phase change faults - skip one sample if faulty | |
420 | uint8_t offsetA = 1, offsetB = 1; | |
421 | for (; i<48; i+=2){ | |
422 | if (BitStream[i+1]==BitStream[i+2]) offsetA=0; | |
423 | if (BitStream[i+2]==BitStream[i+3]) offsetB=0; | |
424 | } | |
425 | if (!offsetA && offsetB) offset++; | |
426 | for (i=offset; i<*size-3; i+=2){ | |
427 | //check for phase error | |
13d77ef9 | 428 | if (BitStream[i+1]==BitStream[i+2]) { |
2767fc02 | 429 | BitStream[bitnum++]=7; |
2147c307 | 430 | errCnt++; |
431 | } | |
ba1a299c | 432 | if((BitStream[i]==1 && BitStream[i+1]==0) || (BitStream[i]==0 && BitStream[i+1]==1)){ |
1e090a61 | 433 | BitStream[bitnum++]=1^invert; |
ba1a299c | 434 | } else if((BitStream[i]==0 && BitStream[i+1]==0) || (BitStream[i]==1 && BitStream[i+1]==1)){ |
1e090a61 | 435 | BitStream[bitnum++]=invert; |
ba1a299c | 436 | } else { |
2767fc02 | 437 | BitStream[bitnum++]=7; |
ba1a299c | 438 | errCnt++; |
439 | } | |
6de43508 | 440 | if(bitnum>MaxBits) break; |
ba1a299c | 441 | } |
442 | *size=bitnum; | |
443 | return errCnt; | |
eb191de6 | 444 | } |
445 | ||
fef74fdc | 446 | // by marshmellow |
11081e04 | 447 | // demod gProxIIDemod |
448 | // error returns as -x | |
449 | // success returns start position in BitStream | |
450 | // BitStream must contain previously askrawdemod and biphasedemoded data | |
451 | int gProxII_Demod(uint8_t BitStream[], size_t *size) | |
452 | { | |
453 | size_t startIdx=0; | |
454 | uint8_t preamble[] = {1,1,1,1,1,0}; | |
455 | ||
456 | uint8_t errChk = preambleSearch(BitStream, preamble, sizeof(preamble), size, &startIdx); | |
457 | if (errChk == 0) return -3; //preamble not found | |
458 | if (*size != 96) return -2; //should have found 96 bits | |
459 | //check first 6 spacer bits to verify format | |
460 | if (!BitStream[startIdx+5] && !BitStream[startIdx+10] && !BitStream[startIdx+15] && !BitStream[startIdx+20] && !BitStream[startIdx+25] && !BitStream[startIdx+30]){ | |
461 | //confirmed proper separator bits found | |
462 | //return start position | |
463 | return (int) startIdx; | |
464 | } | |
465 | return -5; | |
466 | } | |
467 | ||
ba1a299c | 468 | //translate wave to 11111100000 (1 for each short wave 0 for each long wave) |
f822a063 | 469 | size_t fsk_wave_demod(uint8_t * dest, size_t size, uint8_t fchigh, uint8_t fclow) |
eb191de6 | 470 | { |
2eec55c8 | 471 | size_t last_transition = 0; |
472 | size_t idx = 1; | |
ac3ba7ee | 473 | //uint32_t maxVal=0; |
ba1a299c | 474 | if (fchigh==0) fchigh=10; |
475 | if (fclow==0) fclow=8; | |
84871873 | 476 | //set the threshold close to 0 (graph) or 128 std to avoid static |
477 | uint8_t threshold_value = 123; | |
ac2df346 | 478 | size_t preLastSample = 0; |
479 | size_t LastSample = 0; | |
480 | size_t currSample = 0; | |
ba1a299c | 481 | // sync to first lo-hi transition, and threshold |
482 | ||
483 | // Need to threshold first sample | |
6426f6ba | 484 | // skip 160 samples to allow antenna/samples to settle |
485 | if(dest[160] < threshold_value) dest[0] = 0; | |
ba1a299c | 486 | else dest[0] = 1; |
487 | ||
488 | size_t numBits = 0; | |
489 | // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8) | |
cd0bed3c | 490 | // or 10 (fc/10) cycles but in practice due to noise etc we may end up with anywhere |
ba1a299c | 491 | // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10 |
6426f6ba | 492 | for(idx = 161; idx < size-20; idx++) { |
ba1a299c | 493 | // threshold current value |
494 | ||
495 | if (dest[idx] < threshold_value) dest[idx] = 0; | |
496 | else dest[idx] = 1; | |
497 | ||
498 | // Check for 0->1 transition | |
499 | if (dest[idx-1] < dest[idx]) { // 0 -> 1 transition | |
ac2df346 | 500 | preLastSample = LastSample; |
501 | LastSample = currSample; | |
502 | currSample = idx-last_transition; | |
6426f6ba | 503 | if (currSample < (fclow-2)){ //0-5 = garbage noise (or 0-3) |
ba1a299c | 504 | //do nothing with extra garbage |
6426f6ba | 505 | } else if (currSample < (fchigh-1)) { //6-8 = 8 sample waves or 3-6 = 5 |
506 | if (LastSample > (fchigh-2) && (preLastSample < (fchigh-1) || preLastSample == 0 )){ | |
507 | dest[numBits-1]=1; //correct previous 9 wave surrounded by 8 waves | |
ac2df346 | 508 | } |
2eec55c8 | 509 | dest[numBits++]=1; |
ac2df346 | 510 | |
6426f6ba | 511 | } else if (currSample > (fchigh) && !numBits) { //12 + and first bit = garbage |
13d77ef9 | 512 | //do nothing with beginning garbage |
ac2df346 | 513 | } else if (currSample == (fclow+1) && LastSample == (fclow-1)) { // had a 7 then a 9 should be two 8's |
514 | dest[numBits++]=1; | |
515 | } else { //9+ = 10 sample waves | |
2eec55c8 | 516 | dest[numBits++]=0; |
ba1a299c | 517 | } |
518 | last_transition = idx; | |
ba1a299c | 519 | } |
520 | } | |
521 | return numBits; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0 | |
eb191de6 | 522 | } |
523 | ||
ba1a299c | 524 | //translate 11111100000 to 10 |
2eec55c8 | 525 | size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t rfLen, |
e0165dcf | 526 | uint8_t invert, uint8_t fchigh, uint8_t fclow) |
eb191de6 | 527 | { |
ba1a299c | 528 | uint8_t lastval=dest[0]; |
2eec55c8 | 529 | size_t idx=0; |
ba1a299c | 530 | size_t numBits=0; |
531 | uint32_t n=1; | |
ba1a299c | 532 | for( idx=1; idx < size; idx++) { |
13d77ef9 | 533 | n++; |
2eec55c8 | 534 | if (dest[idx]==lastval) continue; |
535 | ||
ba1a299c | 536 | //if lastval was 1, we have a 1->0 crossing |
13d77ef9 | 537 | if (dest[idx-1]==1) { |
75cbbe9a | 538 | n = (n * fclow + rfLen/2) / rfLen; |
13d77ef9 | 539 | } else {// 0->1 crossing |
75cbbe9a | 540 | n = (n * fchigh + rfLen/2) / rfLen; |
ba1a299c | 541 | } |
542 | if (n == 0) n = 1; | |
543 | ||
2eec55c8 | 544 | memset(dest+numBits, dest[idx-1]^invert , n); |
545 | numBits += n; | |
ba1a299c | 546 | n=0; |
547 | lastval=dest[idx]; | |
548 | }//end for | |
13d77ef9 | 549 | // if valid extra bits at the end were all the same frequency - add them in |
75cbbe9a | 550 | if (n > rfLen/fchigh) { |
13d77ef9 | 551 | if (dest[idx-2]==1) { |
75cbbe9a | 552 | n = (n * fclow + rfLen/2) / rfLen; |
13d77ef9 | 553 | } else { |
75cbbe9a | 554 | n = (n * fchigh + rfLen/2) / rfLen; |
13d77ef9 | 555 | } |
2eec55c8 | 556 | memset(dest+numBits, dest[idx-1]^invert , n); |
13d77ef9 | 557 | numBits += n; |
558 | } | |
ba1a299c | 559 | return numBits; |
eb191de6 | 560 | } |
6426f6ba | 561 | |
eb191de6 | 562 | //by marshmellow (from holiman's base) |
563 | // full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod) | |
f822a063 | 564 | int fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow) |
eb191de6 | 565 | { |
ba1a299c | 566 | // FSK demodulator |
567 | size = fsk_wave_demod(dest, size, fchigh, fclow); | |
2eec55c8 | 568 | size = aggregate_bits(dest, size, rfLen, invert, fchigh, fclow); |
ba1a299c | 569 | return size; |
eb191de6 | 570 | } |
a1d17964 | 571 | |
eb191de6 | 572 | // loop to get raw HID waveform then FSK demodulate the TAG ID from it |
ec75f5c1 | 573 | int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo) |
eb191de6 | 574 | { |
e0165dcf | 575 | if (justNoise(dest, *size)) return -1; |
576 | ||
577 | size_t numStart=0, size2=*size, startIdx=0; | |
578 | // FSK demodulator | |
579 | *size = fskdemod(dest, size2,50,1,10,8); //fsk2a | |
2eec55c8 | 580 | if (*size < 96*2) return -2; |
e0165dcf | 581 | // 00011101 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1 |
582 | uint8_t preamble[] = {0,0,0,1,1,1,0,1}; | |
583 | // find bitstring in array | |
584 | uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx); | |
585 | if (errChk == 0) return -3; //preamble not found | |
586 | ||
587 | numStart = startIdx + sizeof(preamble); | |
588 | // final loop, go over previously decoded FSK data and manchester decode into usable tag ID | |
589 | for (size_t idx = numStart; (idx-numStart) < *size - sizeof(preamble); idx+=2){ | |
590 | if (dest[idx] == dest[idx+1]){ | |
591 | return -4; //not manchester data | |
592 | } | |
593 | *hi2 = (*hi2<<1)|(*hi>>31); | |
594 | *hi = (*hi<<1)|(*lo>>31); | |
595 | //Then, shift in a 0 or one into low | |
596 | if (dest[idx] && !dest[idx+1]) // 1 0 | |
597 | *lo=(*lo<<1)|1; | |
598 | else // 0 1 | |
599 | *lo=(*lo<<1)|0; | |
600 | } | |
601 | return (int)startIdx; | |
eb191de6 | 602 | } |
603 | ||
ec75f5c1 | 604 | // loop to get raw paradox waveform then FSK demodulate the TAG ID from it |
a1d17964 | 605 | int ParadoxdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo) |
ec75f5c1 | 606 | { |
a1d17964 | 607 | if (justNoise(dest, *size)) return -1; |
608 | ||
609 | size_t numStart=0, size2=*size, startIdx=0; | |
ec75f5c1 | 610 | // FSK demodulator |
a1d17964 | 611 | *size = fskdemod(dest, size2,50,1,10,8); //fsk2a |
612 | if (*size < 96) return -2; | |
ec75f5c1 | 613 | |
a1d17964 | 614 | // 00001111 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1 |
615 | uint8_t preamble[] = {0,0,0,0,1,1,1,1}; | |
616 | ||
617 | uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx); | |
618 | if (errChk == 0) return -3; //preamble not found | |
619 | ||
620 | numStart = startIdx + sizeof(preamble); | |
621 | // final loop, go over previously decoded FSK data and manchester decode into usable tag ID | |
622 | for (size_t idx = numStart; (idx-numStart) < *size - sizeof(preamble); idx+=2){ | |
623 | if (dest[idx] == dest[idx+1]) | |
624 | return -4; //not manchester data | |
625 | *hi2 = (*hi2<<1)|(*hi>>31); | |
626 | *hi = (*hi<<1)|(*lo>>31); | |
627 | //Then, shift in a 0 or one into low | |
628 | if (dest[idx] && !dest[idx+1]) // 1 0 | |
629 | *lo=(*lo<<1)|1; | |
630 | else // 0 1 | |
631 | *lo=(*lo<<1)|0; | |
ec75f5c1 | 632 | } |
a1d17964 | 633 | return (int)startIdx; |
ec75f5c1 | 634 | } |
635 | ||
eb191de6 | 636 | int IOdemodFSK(uint8_t *dest, size_t size) |
637 | { | |
a1d17964 | 638 | if (justNoise(dest, size)) return -1; |
ba1a299c | 639 | //make sure buffer has data |
a1d17964 | 640 | if (size < 66*64) return -2; |
ba1a299c | 641 | // FSK demodulator |
a1d17964 | 642 | size = fskdemod(dest, size, 64, 1, 10, 8); // FSK2a RF/64 |
643 | if (size < 65) return -3; //did we get a good demod? | |
ba1a299c | 644 | //Index map |
645 | //0 10 20 30 40 50 60 | |
646 | //| | | | | | | | |
647 | //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23 | |
648 | //----------------------------------------------------------------------------- | |
649 | //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11 | |
650 | // | |
651 | //XSF(version)facility:codeone+codetwo | |
652 | //Handle the data | |
a1d17964 | 653 | size_t startIdx = 0; |
654 | uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,1}; | |
655 | uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), &size, &startIdx); | |
656 | if (errChk == 0) return -4; //preamble not found | |
eb191de6 | 657 | |
a1d17964 | 658 | if (!dest[startIdx+8] && dest[startIdx+17]==1 && dest[startIdx+26]==1 && dest[startIdx+35]==1 && dest[startIdx+44]==1 && dest[startIdx+53]==1){ |
659 | //confirmed proper separator bits found | |
660 | //return start position | |
661 | return (int) startIdx; | |
1e090a61 | 662 | } |
a1d17964 | 663 | return -5; |
1e090a61 | 664 | } |
665 | ||
70459879 | 666 | // by marshmellow |
667 | // find viking preamble 0xF200 in already demoded data | |
668 | int VikingDemod_AM(uint8_t *dest, size_t *size) { | |
70459879 | 669 | //make sure buffer has data |
670 | if (*size < 64*2) return -2; | |
671 | ||
672 | size_t startIdx = 0; | |
673 | uint8_t preamble[] = {1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
674 | uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx); | |
675 | if (errChk == 0) return -4; //preamble not found | |
676 | uint32_t checkCalc = bytebits_to_byte(dest+startIdx,8) ^ bytebits_to_byte(dest+startIdx+8,8) ^ bytebits_to_byte(dest+startIdx+16,8) | |
677 | ^ bytebits_to_byte(dest+startIdx+24,8) ^ bytebits_to_byte(dest+startIdx+32,8) ^ bytebits_to_byte(dest+startIdx+40,8) | |
678 | ^ bytebits_to_byte(dest+startIdx+48,8) ^ bytebits_to_byte(dest+startIdx+56,8); | |
679 | if ( checkCalc != 0xA8 ) return -5; | |
57c7b44b | 680 | if (*size != 64) return -6; |
70459879 | 681 | //return start position |
682 | return (int) startIdx; | |
683 | } | |
684 | ||
4469412e | 685 | // find presco preamble 0x10D in already demoded data |
686 | int PrescoDemod(uint8_t *dest, size_t *size) { | |
687 | //make sure buffer has data | |
688 | if (*size < 64*2) return -2; | |
689 | ||
690 | size_t startIdx = 0; | |
691 | uint8_t preamble[] = {1,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0}; | |
692 | uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx); | |
693 | if (errChk == 0) return -4; //preamble not found | |
694 | //return start position | |
695 | return (int) startIdx; | |
696 | } | |
697 | ||
698 | ||
615f21dd | 699 | // Ask/Biphase Demod then try to locate an ISO 11784/85 ID |
0df669a2 | 700 | // BitStream must contain previously askrawdemod and biphasedemoded data |
ad6219fc | 701 | int FDXBdemodBI(uint8_t *dest, size_t *size) |
615f21dd | 702 | { |
703 | //make sure buffer has enough data | |
0df669a2 | 704 | if (*size < 128) return -1; |
615f21dd | 705 | |
615f21dd | 706 | size_t startIdx = 0; |
0df669a2 | 707 | uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,0,1}; |
708 | ||
615f21dd | 709 | uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx); |
0df669a2 | 710 | if (errChk == 0) return -2; //preamble not found |
615f21dd | 711 | return (int)startIdx; |
712 | } | |
1e090a61 | 713 | |
714 | // by marshmellow | |
715 | // FSK Demod then try to locate an AWID ID | |
a1d17964 | 716 | int AWIDdemodFSK(uint8_t *dest, size_t *size) |
1e090a61 | 717 | { |
a1d17964 | 718 | //make sure buffer has enough data |
719 | if (*size < 96*50) return -1; | |
720 | ||
721 | if (justNoise(dest, *size)) return -2; | |
1e090a61 | 722 | |
723 | // FSK demodulator | |
a1d17964 | 724 | *size = fskdemod(dest, *size, 50, 1, 10, 8); // fsk2a RF/50 |
725 | if (*size < 96) return -3; //did we get a good demod? | |
726 | ||
727 | uint8_t preamble[] = {0,0,0,0,0,0,0,1}; | |
728 | size_t startIdx = 0; | |
729 | uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx); | |
730 | if (errChk == 0) return -4; //preamble not found | |
731 | if (*size != 96) return -5; | |
732 | return (int)startIdx; | |
1e090a61 | 733 | } |
734 | ||
735 | // by marshmellow | |
6426f6ba | 736 | // FSK Demod then try to locate a Farpointe Data (pyramid) ID |
a1d17964 | 737 | int PyramiddemodFSK(uint8_t *dest, size_t *size) |
1e090a61 | 738 | { |
f3bf15e4 | 739 | //make sure buffer has data |
740 | if (*size < 128*50) return -5; | |
a1d17964 | 741 | |
f3bf15e4 | 742 | //test samples are not just noise |
743 | if (justNoise(dest, *size)) return -1; | |
1e090a61 | 744 | |
f3bf15e4 | 745 | // FSK demodulator |
746 | *size = fskdemod(dest, *size, 50, 1, 10, 8); // fsk2a RF/50 | |
747 | if (*size < 128) return -2; //did we get a good demod? | |
a1d17964 | 748 | |
f3bf15e4 | 749 | uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; |
a1d17964 | 750 | size_t startIdx = 0; |
751 | uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx); | |
752 | if (errChk == 0) return -4; //preamble not found | |
753 | if (*size != 128) return -3; | |
754 | return (int)startIdx; | |
1e090a61 | 755 | } |
756 | ||
fef74fdc | 757 | // by marshmellow |
758 | // to detect a wave that has heavily clipped (clean) samples | |
cc15a118 | 759 | uint8_t DetectCleanAskWave(uint8_t dest[], size_t size, uint8_t high, uint8_t low) |
6de43508 | 760 | { |
6426f6ba | 761 | bool allArePeaks = true; |
6de43508 | 762 | uint16_t cntPeaks=0; |
6426f6ba | 763 | size_t loopEnd = 512+160; |
1fbf8956 | 764 | if (loopEnd > size) loopEnd = size; |
6426f6ba | 765 | for (size_t i=160; i<loopEnd; i++){ |
6de43508 | 766 | if (dest[i]>low && dest[i]<high) |
6426f6ba | 767 | allArePeaks = false; |
6de43508 | 768 | else |
769 | cntPeaks++; | |
770 | } | |
6426f6ba | 771 | if (!allArePeaks){ |
772 | if (cntPeaks > 300) return true; | |
6de43508 | 773 | } |
6426f6ba | 774 | return allArePeaks; |
6de43508 | 775 | } |
2eec55c8 | 776 | // by marshmellow |
777 | // to help detect clocks on heavily clipped samples | |
cc15a118 | 778 | // based on count of low to low |
779 | int DetectStrongAskClock(uint8_t dest[], size_t size, uint8_t high, uint8_t low) | |
13d77ef9 | 780 | { |
cc15a118 | 781 | uint8_t fndClk[] = {8,16,32,40,50,64,128}; |
782 | size_t startwave; | |
6426f6ba | 783 | size_t i = 100; |
cc15a118 | 784 | size_t minClk = 255; |
785 | // get to first full low to prime loop and skip incomplete first pulse | |
786 | while ((dest[i] < high) && (i < size)) | |
787 | ++i; | |
788 | while ((dest[i] > low) && (i < size)) | |
789 | ++i; | |
790 | ||
791 | // loop through all samples | |
792 | while (i < size) { | |
793 | // measure from low to low | |
794 | while ((dest[i] > low) && (i < size)) | |
795 | ++i; | |
796 | startwave= i; | |
797 | while ((dest[i] < high) && (i < size)) | |
798 | ++i; | |
799 | while ((dest[i] > low) && (i < size)) | |
800 | ++i; | |
801 | //get minimum measured distance | |
802 | if (i-startwave < minClk && i < size) | |
803 | minClk = i - startwave; | |
13d77ef9 | 804 | } |
cc15a118 | 805 | // set clock |
a126332a | 806 | if (g_debugMode==2) prnt("DEBUG ASK: detectstrongASKclk smallest wave: %d",minClk); |
cc15a118 | 807 | for (uint8_t clkCnt = 0; clkCnt<7; clkCnt++) { |
808 | if (minClk >= fndClk[clkCnt]-(fndClk[clkCnt]/8) && minClk <= fndClk[clkCnt]+1) | |
809 | return fndClk[clkCnt]; | |
13d77ef9 | 810 | } |
cc15a118 | 811 | return 0; |
13d77ef9 | 812 | } |
813 | ||
eb191de6 | 814 | // by marshmellow |
815 | // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping) | |
816 | // maybe somehow adjust peak trimming value based on samples to fix? | |
6de43508 | 817 | // return start index of best starting position for that clock and return clock (by reference) |
818 | int DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr) | |
eb191de6 | 819 | { |
6e984446 | 820 | size_t i=1; |
cc15a118 | 821 | uint8_t clk[] = {255,8,16,32,40,50,64,100,128,255}; |
822 | uint8_t clkEnd = 9; | |
2eec55c8 | 823 | uint8_t loopCnt = 255; //don't need to loop through entire array... |
6426f6ba | 824 | if (size <= loopCnt+60) return -1; //not enough samples |
825 | size -= 60; //sometimes there is a strange end wave - filter out this.... | |
6e984446 | 826 | //if we already have a valid clock |
827 | uint8_t clockFnd=0; | |
cc15a118 | 828 | for (;i<clkEnd;++i) |
829 | if (clk[i] == *clock) clockFnd = i; | |
6e984446 | 830 | //clock found but continue to find best startpos |
e0165dcf | 831 | |
832 | //get high and low peak | |
833 | int peak, low; | |
2eec55c8 | 834 | if (getHiLo(dest, loopCnt, &peak, &low, 75, 75) < 1) return -1; |
e0165dcf | 835 | |
836 | //test for large clean peaks | |
cc15a118 | 837 | if (!clockFnd){ |
838 | if (DetectCleanAskWave(dest, size, peak, low)==1){ | |
839 | int ans = DetectStrongAskClock(dest, size, peak, low); | |
a126332a | 840 | if (g_debugMode==2) prnt("DEBUG ASK: detectaskclk Clean Ask Wave Detected: clk %d",ans); |
cc15a118 | 841 | for (i=clkEnd-1; i>0; i--){ |
842 | if (clk[i] == ans) { | |
843 | *clock = ans; | |
844 | //clockFnd = i; | |
845 | return 0; // for strong waves i don't use the 'best start position' yet... | |
846 | //break; //clock found but continue to find best startpos [not yet] | |
847 | } | |
e0165dcf | 848 | } |
849 | } | |
850 | } | |
2eec55c8 | 851 | uint8_t ii; |
852 | uint8_t clkCnt, tol = 0; | |
853 | uint16_t bestErr[]={1000,1000,1000,1000,1000,1000,1000,1000,1000}; | |
854 | uint8_t bestStart[]={0,0,0,0,0,0,0,0,0}; | |
855 | size_t errCnt = 0; | |
856 | size_t arrLoc, loopEnd; | |
6e984446 | 857 | |
cc15a118 | 858 | if (clockFnd>0) { |
859 | clkCnt = clockFnd; | |
860 | clkEnd = clockFnd+1; | |
861 | } | |
862 | else clkCnt=1; | |
863 | ||
864 | //test each valid clock from smallest to greatest to see which lines up | |
865 | for(; clkCnt < clkEnd; clkCnt++){ | |
fef74fdc | 866 | if (clk[clkCnt] <= 32){ |
e0165dcf | 867 | tol=1; |
868 | }else{ | |
869 | tol=0; | |
870 | } | |
2767fc02 | 871 | //if no errors allowed - keep start within the first clock |
cc15a118 | 872 | if (!maxErr && size > clk[clkCnt]*2 + tol && clk[clkCnt]<128) loopCnt=clk[clkCnt]*2; |
e0165dcf | 873 | bestErr[clkCnt]=1000; |
6e984446 | 874 | //try lining up the peaks by moving starting point (try first few clocks) |
cc15a118 | 875 | for (ii=0; ii < loopCnt; ii++){ |
2eec55c8 | 876 | if (dest[ii] < peak && dest[ii] > low) continue; |
877 | ||
878 | errCnt=0; | |
879 | // now that we have the first one lined up test rest of wave array | |
880 | loopEnd = ((size-ii-tol) / clk[clkCnt]) - 1; | |
881 | for (i=0; i < loopEnd; ++i){ | |
882 | arrLoc = ii + (i * clk[clkCnt]); | |
883 | if (dest[arrLoc] >= peak || dest[arrLoc] <= low){ | |
884 | }else if (dest[arrLoc-tol] >= peak || dest[arrLoc-tol] <= low){ | |
885 | }else if (dest[arrLoc+tol] >= peak || dest[arrLoc+tol] <= low){ | |
886 | }else{ //error no peak detected | |
887 | errCnt++; | |
e0165dcf | 888 | } |
889 | } | |
cc15a118 | 890 | //if we found no errors then we can stop here and a low clock (common clocks) |
2eec55c8 | 891 | // this is correct one - return this clock |
a126332a | 892 | if (g_debugMode == 2) prnt("DEBUG ASK: clk %d, err %d, startpos %d, endpos %d",clk[clkCnt],errCnt,ii,i); |
cc15a118 | 893 | if(errCnt==0 && clkCnt<7) { |
894 | if (!clockFnd) *clock = clk[clkCnt]; | |
2eec55c8 | 895 | return ii; |
896 | } | |
897 | //if we found errors see if it is lowest so far and save it as best run | |
898 | if(errCnt<bestErr[clkCnt]){ | |
899 | bestErr[clkCnt]=errCnt; | |
900 | bestStart[clkCnt]=ii; | |
901 | } | |
e0165dcf | 902 | } |
903 | } | |
cc15a118 | 904 | uint8_t iii; |
e0165dcf | 905 | uint8_t best=0; |
cc15a118 | 906 | for (iii=1; iii<clkEnd; ++iii){ |
2eec55c8 | 907 | if (bestErr[iii] < bestErr[best]){ |
908 | if (bestErr[iii] == 0) bestErr[iii]=1; | |
e0165dcf | 909 | // current best bit to error ratio vs new bit to error ratio |
2eec55c8 | 910 | if ( (size/clk[best])/bestErr[best] < (size/clk[iii])/bestErr[iii] ){ |
e0165dcf | 911 | best = iii; |
912 | } | |
913 | } | |
a126332a | 914 | if (g_debugMode == 2) prnt("DEBUG ASK: clk %d, # Errors %d, Current Best Clk %d, bestStart %d",clk[iii],bestErr[iii],clk[best],bestStart[best]); |
e0165dcf | 915 | } |
cc15a118 | 916 | if (!clockFnd) *clock = clk[best]; |
e0165dcf | 917 | return bestStart[best]; |
eb191de6 | 918 | } |
ba1a299c | 919 | |
920 | //by marshmellow | |
6de43508 | 921 | //detect psk clock by reading each phase shift |
922 | // a phase shift is determined by measuring the sample length of each wave | |
923 | int DetectPSKClock(uint8_t dest[], size_t size, int clock) | |
ba1a299c | 924 | { |
e0165dcf | 925 | uint8_t clk[]={255,16,32,40,50,64,100,128,255}; //255 is not a valid clock |
926 | uint16_t loopCnt = 4096; //don't need to loop through entire array... | |
927 | if (size == 0) return 0; | |
6426f6ba | 928 | if (size<loopCnt) loopCnt = size-20; |
e0165dcf | 929 | |
930 | //if we already have a valid clock quit | |
931 | size_t i=1; | |
932 | for (; i < 8; ++i) | |
933 | if (clk[i] == clock) return clock; | |
934 | ||
935 | size_t waveStart=0, waveEnd=0, firstFullWave=0, lastClkBit=0; | |
936 | uint8_t clkCnt, fc=0, fullWaveLen=0, tol=1; | |
937 | uint16_t peakcnt=0, errCnt=0, waveLenCnt=0; | |
938 | uint16_t bestErr[]={1000,1000,1000,1000,1000,1000,1000,1000,1000}; | |
939 | uint16_t peaksdet[]={0,0,0,0,0,0,0,0,0}; | |
2eec55c8 | 940 | fc = countFC(dest, size, 0); |
941 | if (fc!=2 && fc!=4 && fc!=8) return -1; | |
a126332a | 942 | if (g_debugMode==2) prnt("DEBUG PSK: FC: %d",fc); |
e0165dcf | 943 | |
944 | //find first full wave | |
6426f6ba | 945 | for (i=160; i<loopCnt; i++){ |
e0165dcf | 946 | if (dest[i] < dest[i+1] && dest[i+1] >= dest[i+2]){ |
947 | if (waveStart == 0) { | |
948 | waveStart = i+1; | |
6426f6ba | 949 | //prnt("DEBUG: waveStart: %d",waveStart); |
e0165dcf | 950 | } else { |
951 | waveEnd = i+1; | |
6426f6ba | 952 | //prnt("DEBUG: waveEnd: %d",waveEnd); |
e0165dcf | 953 | waveLenCnt = waveEnd-waveStart; |
954 | if (waveLenCnt > fc){ | |
955 | firstFullWave = waveStart; | |
956 | fullWaveLen=waveLenCnt; | |
957 | break; | |
958 | } | |
959 | waveStart=0; | |
960 | } | |
961 | } | |
962 | } | |
a126332a | 963 | if (g_debugMode ==2) prnt("DEBUG PSK: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen); |
e0165dcf | 964 | |
965 | //test each valid clock from greatest to smallest to see which lines up | |
966 | for(clkCnt=7; clkCnt >= 1 ; clkCnt--){ | |
967 | lastClkBit = firstFullWave; //set end of wave as clock align | |
968 | waveStart = 0; | |
969 | errCnt=0; | |
970 | peakcnt=0; | |
a126332a | 971 | if (g_debugMode == 2) prnt("DEBUG PSK: clk: %d, lastClkBit: %d",clk[clkCnt],lastClkBit); |
e0165dcf | 972 | |
973 | for (i = firstFullWave+fullWaveLen-1; i < loopCnt-2; i++){ | |
974 | //top edge of wave = start of new wave | |
975 | if (dest[i] < dest[i+1] && dest[i+1] >= dest[i+2]){ | |
976 | if (waveStart == 0) { | |
977 | waveStart = i+1; | |
978 | waveLenCnt=0; | |
979 | } else { //waveEnd | |
980 | waveEnd = i+1; | |
981 | waveLenCnt = waveEnd-waveStart; | |
982 | if (waveLenCnt > fc){ | |
983 | //if this wave is a phase shift | |
a126332a | 984 | if (g_debugMode == 2) prnt("DEBUG PSK: phase shift at: %d, len: %d, nextClk: %d, i: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+clk[clkCnt]-tol,i+1,fc); |
e0165dcf | 985 | if (i+1 >= lastClkBit + clk[clkCnt] - tol){ //should be a clock bit |
986 | peakcnt++; | |
987 | lastClkBit+=clk[clkCnt]; | |
988 | } else if (i<lastClkBit+8){ | |
989 | //noise after a phase shift - ignore | |
990 | } else { //phase shift before supposed to based on clock | |
991 | errCnt++; | |
992 | } | |
993 | } else if (i+1 > lastClkBit + clk[clkCnt] + tol + fc){ | |
994 | lastClkBit+=clk[clkCnt]; //no phase shift but clock bit | |
995 | } | |
996 | waveStart=i+1; | |
997 | } | |
998 | } | |
999 | } | |
1000 | if (errCnt == 0){ | |
1001 | return clk[clkCnt]; | |
1002 | } | |
1003 | if (errCnt <= bestErr[clkCnt]) bestErr[clkCnt]=errCnt; | |
1004 | if (peakcnt > peaksdet[clkCnt]) peaksdet[clkCnt]=peakcnt; | |
1005 | } | |
1006 | //all tested with errors | |
1007 | //return the highest clk with the most peaks found | |
1008 | uint8_t best=7; | |
1009 | for (i=7; i>=1; i--){ | |
1010 | if (peaksdet[i] > peaksdet[best]) { | |
1011 | best = i; | |
1012 | } | |
a126332a | 1013 | if (g_debugMode == 2) prnt("DEBUG PSK: Clk: %d, peaks: %d, errs: %d, bestClk: %d",clk[i],peaksdet[i],bestErr[i],clk[best]); |
e0165dcf | 1014 | } |
1015 | return clk[best]; | |
ba1a299c | 1016 | } |
1017 | ||
6426f6ba | 1018 | int DetectStrongNRZClk(uint8_t *dest, size_t size, int peak, int low){ |
1019 | //find shortest transition from high to low | |
1020 | size_t i = 0; | |
1021 | size_t transition1 = 0; | |
1022 | int lowestTransition = 255; | |
1023 | bool lastWasHigh = false; | |
1024 | ||
1025 | //find first valid beginning of a high or low wave | |
1026 | while ((dest[i] >= peak || dest[i] <= low) && (i < size)) | |
1027 | ++i; | |
1028 | while ((dest[i] < peak && dest[i] > low) && (i < size)) | |
1029 | ++i; | |
1030 | lastWasHigh = (dest[i] >= peak); | |
1031 | ||
1032 | if (i==size) return 0; | |
1033 | transition1 = i; | |
1034 | ||
1035 | for (;i < size; i++) { | |
1036 | if ((dest[i] >= peak && !lastWasHigh) || (dest[i] <= low && lastWasHigh)) { | |
1037 | lastWasHigh = (dest[i] >= peak); | |
1038 | if (i-transition1 < lowestTransition) lowestTransition = i-transition1; | |
1039 | transition1 = i; | |
1040 | } | |
1041 | } | |
6426f6ba | 1042 | if (lowestTransition == 255) lowestTransition = 0; |
a126332a | 1043 | if (g_debugMode==2) prnt("DEBUG NRZ: detectstrongNRZclk smallest wave: %d",lowestTransition); |
6426f6ba | 1044 | return lowestTransition; |
1045 | } | |
1046 | ||
6de43508 | 1047 | //by marshmellow |
1048 | //detect nrz clock by reading #peaks vs no peaks(or errors) | |
1049 | int DetectNRZClock(uint8_t dest[], size_t size, int clock) | |
ba1a299c | 1050 | { |
2eec55c8 | 1051 | size_t i=0; |
1052 | uint8_t clk[]={8,16,32,40,50,64,100,128,255}; | |
1053 | size_t loopCnt = 4096; //don't need to loop through entire array... | |
e0165dcf | 1054 | if (size == 0) return 0; |
6426f6ba | 1055 | if (size<loopCnt) loopCnt = size-20; |
e0165dcf | 1056 | //if we already have a valid clock quit |
1057 | for (; i < 8; ++i) | |
1058 | if (clk[i] == clock) return clock; | |
1059 | ||
1060 | //get high and low peak | |
1061 | int peak, low; | |
2eec55c8 | 1062 | if (getHiLo(dest, loopCnt, &peak, &low, 75, 75) < 1) return 0; |
e0165dcf | 1063 | |
6426f6ba | 1064 | int lowestTransition = DetectStrongNRZClk(dest, size-20, peak, low); |
2eec55c8 | 1065 | size_t ii; |
e0165dcf | 1066 | uint8_t clkCnt; |
1067 | uint8_t tol = 0; | |
6426f6ba | 1068 | uint16_t smplCnt = 0; |
1069 | int16_t peakcnt = 0; | |
1070 | int16_t peaksdet[] = {0,0,0,0,0,0,0,0}; | |
1071 | uint16_t maxPeak = 255; | |
1072 | bool firstpeak = false; | |
e0165dcf | 1073 | //test for large clipped waves |
1074 | for (i=0; i<loopCnt; i++){ | |
1075 | if (dest[i] >= peak || dest[i] <= low){ | |
6426f6ba | 1076 | if (!firstpeak) continue; |
1077 | smplCnt++; | |
e0165dcf | 1078 | } else { |
6426f6ba | 1079 | firstpeak=true; |
1080 | if (smplCnt > 6 ){ | |
1081 | if (maxPeak > smplCnt){ | |
1082 | maxPeak = smplCnt; | |
1083 | //prnt("maxPk: %d",maxPeak); | |
1084 | } | |
1085 | peakcnt++; | |
1086 | //prnt("maxPk: %d, smplCnt: %d, peakcnt: %d",maxPeak,smplCnt,peakcnt); | |
1087 | smplCnt=0; | |
e0165dcf | 1088 | } |
e0165dcf | 1089 | } |
1090 | } | |
6426f6ba | 1091 | bool errBitHigh = 0; |
1092 | bool bitHigh = 0; | |
1093 | uint8_t ignoreCnt = 0; | |
1094 | uint8_t ignoreWindow = 4; | |
1095 | bool lastPeakHigh = 0; | |
1096 | int lastBit = 0; | |
e0165dcf | 1097 | peakcnt=0; |
1098 | //test each valid clock from smallest to greatest to see which lines up | |
1099 | for(clkCnt=0; clkCnt < 8; ++clkCnt){ | |
6426f6ba | 1100 | //ignore clocks smaller than smallest peak |
1101 | if (clk[clkCnt] < maxPeak - (clk[clkCnt]/4)) continue; | |
e0165dcf | 1102 | //try lining up the peaks by moving starting point (try first 256) |
6426f6ba | 1103 | for (ii=20; ii < loopCnt; ++ii){ |
e0165dcf | 1104 | if ((dest[ii] >= peak) || (dest[ii] <= low)){ |
1105 | peakcnt=0; | |
6426f6ba | 1106 | bitHigh = false; |
1107 | ignoreCnt = 0; | |
1108 | lastBit = ii-clk[clkCnt]; | |
1109 | //loop through to see if this start location works | |
1110 | for (i = ii; i < size-20; ++i) { | |
1111 | //if we are at a clock bit | |
1112 | if ((i >= lastBit + clk[clkCnt] - tol) && (i <= lastBit + clk[clkCnt] + tol)) { | |
1113 | //test high/low | |
1114 | if (dest[i] >= peak || dest[i] <= low) { | |
1115 | //if same peak don't count it | |
1116 | if ((dest[i] >= peak && !lastPeakHigh) || (dest[i] <= low && lastPeakHigh)) { | |
e0165dcf | 1117 | peakcnt++; |
6426f6ba | 1118 | } |
1119 | lastPeakHigh = (dest[i] >= peak); | |
1120 | bitHigh = true; | |
1121 | errBitHigh = false; | |
1122 | ignoreCnt = ignoreWindow; | |
1123 | lastBit += clk[clkCnt]; | |
1124 | } else if (i == lastBit + clk[clkCnt] + tol) { | |
1125 | lastBit += clk[clkCnt]; | |
1126 | } | |
1127 | //else if not a clock bit and no peaks | |
1128 | } else if (dest[i] < peak && dest[i] > low){ | |
1129 | if (ignoreCnt==0){ | |
1130 | bitHigh=false; | |
1131 | if (errBitHigh==true) peakcnt--; | |
1132 | errBitHigh=false; | |
1133 | } else { | |
1134 | ignoreCnt--; | |
1135 | } | |
1136 | // else if not a clock bit but we have a peak | |
1137 | } else if ((dest[i]>=peak || dest[i]<=low) && (!bitHigh)) { | |
1138 | //error bar found no clock... | |
1139 | errBitHigh=true; | |
e0165dcf | 1140 | } |
1141 | } | |
1142 | if(peakcnt>peaksdet[clkCnt]) { | |
1143 | peaksdet[clkCnt]=peakcnt; | |
1144 | } | |
1145 | } | |
1146 | } | |
1147 | } | |
1148 | int iii=7; | |
2eec55c8 | 1149 | uint8_t best=0; |
e0165dcf | 1150 | for (iii=7; iii > 0; iii--){ |
6426f6ba | 1151 | if ((peaksdet[iii] >= (peaksdet[best]-1)) && (peaksdet[iii] <= peaksdet[best]+1) && lowestTransition) { |
1152 | if (clk[iii] > (lowestTransition - (clk[iii]/8)) && clk[iii] < (lowestTransition + (clk[iii]/8))) { | |
1153 | best = iii; | |
1154 | } | |
1155 | } else if (peaksdet[iii] > peaksdet[best]){ | |
e0165dcf | 1156 | best = iii; |
1157 | } | |
a126332a | 1158 | if (g_debugMode==2) prnt("DEBUG NRZ: Clk: %d, peaks: %d, maxPeak: %d, bestClk: %d, lowestTrs: %d",clk[iii],peaksdet[iii],maxPeak, clk[best], lowestTransition); |
e0165dcf | 1159 | } |
6426f6ba | 1160 | |
e0165dcf | 1161 | return clk[best]; |
ba1a299c | 1162 | } |
1163 | ||
04d2721b | 1164 | // by marshmellow |
1165 | // convert psk1 demod to psk2 demod | |
1166 | // only transition waves are 1s | |
1167 | void psk1TOpsk2(uint8_t *BitStream, size_t size) | |
1168 | { | |
1169 | size_t i=1; | |
1170 | uint8_t lastBit=BitStream[0]; | |
1171 | for (; i<size; i++){ | |
2767fc02 | 1172 | if (BitStream[i]==7){ |
7a8a982b | 1173 | //ignore errors |
1174 | } else if (lastBit!=BitStream[i]){ | |
04d2721b | 1175 | lastBit=BitStream[i]; |
1176 | BitStream[i]=1; | |
1177 | } else { | |
1178 | BitStream[i]=0; | |
1179 | } | |
1180 | } | |
1181 | return; | |
1182 | } | |
ba1a299c | 1183 | |
3bc66a96 | 1184 | // by marshmellow |
1185 | // convert psk2 demod to psk1 demod | |
1186 | // from only transition waves are 1s to phase shifts change bit | |
1187 | void psk2TOpsk1(uint8_t *BitStream, size_t size) | |
1188 | { | |
712ebfa6 | 1189 | uint8_t phase=0; |
1190 | for (size_t i=0; i<size; i++){ | |
1191 | if (BitStream[i]==1){ | |
3bc66a96 | 1192 | phase ^=1; |
1193 | } | |
1194 | BitStream[i]=phase; | |
1195 | } | |
1196 | return; | |
1197 | } | |
1198 | ||
04d2721b | 1199 | // redesigned by marshmellow adjusted from existing decode functions |
1200 | // indala id decoding - only tested on 26 bit tags, but attempted to make it work for more | |
ba1a299c | 1201 | int indala26decode(uint8_t *bitStream, size_t *size, uint8_t *invert) |
1202 | { | |
1203 | //26 bit 40134 format (don't know other formats) | |
57c7b44b | 1204 | uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; |
1205 | uint8_t preamble_i[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}; | |
1206 | size_t startidx = 0; | |
1207 | if (!preambleSearch(bitStream, preamble, sizeof(preamble), size, &startidx)){ | |
1208 | // if didn't find preamble try again inverting | |
1209 | if (!preambleSearch(bitStream, preamble_i, sizeof(preamble_i), size, &startidx)) return -1; | |
1210 | *invert ^= 1; | |
1211 | } | |
1212 | if (*size != 64 && *size != 224) return -2; | |
1213 | if (*invert==1) | |
1214 | for (size_t i = startidx; i < *size; i++) | |
1215 | bitStream[i] ^= 1; | |
ba1a299c | 1216 | |
57c7b44b | 1217 | return (int) startidx; |
ba1a299c | 1218 | } |
1219 | ||
05164399 | 1220 | // by marshmellow - demodulate NRZ wave - requires a read with strong signal |
04d2721b | 1221 | // peaks invert bit (high=1 low=0) each clock cycle = 1 bit determined by last peak |
6426f6ba | 1222 | int nrzRawDemod(uint8_t *dest, size_t *size, int *clk, int *invert){ |
e0165dcf | 1223 | if (justNoise(dest, *size)) return -1; |
1224 | *clk = DetectNRZClock(dest, *size, *clk); | |
1225 | if (*clk==0) return -2; | |
2eec55c8 | 1226 | size_t i, gLen = 4096; |
6426f6ba | 1227 | if (gLen>*size) gLen = *size-20; |
e0165dcf | 1228 | int high, low; |
1229 | if (getHiLo(dest, gLen, &high, &low, 75, 75) < 1) return -3; //25% fuzz on high 25% fuzz on low | |
6426f6ba | 1230 | |
1231 | uint8_t bit=0; | |
1232 | //convert wave samples to 1's and 0's | |
1233 | for(i=20; i < *size-20; i++){ | |
1234 | if (dest[i] >= high) bit = 1; | |
1235 | if (dest[i] <= low) bit = 0; | |
1236 | dest[i] = bit; | |
e0165dcf | 1237 | } |
6426f6ba | 1238 | //now demod based on clock (rf/32 = 32 1's for one 1 bit, 32 0's for one 0 bit) |
1239 | size_t lastBit = 0; | |
1240 | size_t numBits = 0; | |
1241 | for(i=21; i < *size-20; i++) { | |
1242 | //if transition detected or large number of same bits - store the passed bits | |
1243 | if (dest[i] != dest[i-1] || (i-lastBit) == (10 * *clk)) { | |
1244 | memset(dest+numBits, dest[i-1] ^ *invert, (i - lastBit + (*clk/4)) / *clk); | |
1245 | numBits += (i - lastBit + (*clk/4)) / *clk; | |
1246 | lastBit = i-1; | |
e0165dcf | 1247 | } |
e0165dcf | 1248 | } |
6426f6ba | 1249 | *size = numBits; |
1250 | return 0; | |
ba1a299c | 1251 | } |
1252 | ||
1e090a61 | 1253 | //by marshmellow |
03e6bb4a | 1254 | //detects the bit clock for FSK given the high and low Field Clocks |
1255 | uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow) | |
1e090a61 | 1256 | { |
e0165dcf | 1257 | uint8_t clk[] = {8,16,32,40,50,64,100,128,0}; |
1258 | uint16_t rfLens[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
1259 | uint8_t rfCnts[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
1260 | uint8_t rfLensFnd = 0; | |
2eec55c8 | 1261 | uint8_t lastFCcnt = 0; |
1262 | uint16_t fcCounter = 0; | |
e0165dcf | 1263 | uint16_t rfCounter = 0; |
1264 | uint8_t firstBitFnd = 0; | |
1265 | size_t i; | |
1266 | if (size == 0) return 0; | |
1267 | ||
6426f6ba | 1268 | uint8_t fcTol = ((fcHigh*100 - fcLow*100)/2 + 50)/100; //(uint8_t)(0.5+(float)(fcHigh-fcLow)/2); |
e0165dcf | 1269 | rfLensFnd=0; |
1270 | fcCounter=0; | |
1271 | rfCounter=0; | |
1272 | firstBitFnd=0; | |
1273 | //PrintAndLog("DEBUG: fcTol: %d",fcTol); | |
6426f6ba | 1274 | // prime i to first peak / up transition |
1275 | for (i = 160; i < size-20; i++) | |
e0165dcf | 1276 | if (BitStream[i] > BitStream[i-1] && BitStream[i]>=BitStream[i+1]) |
1277 | break; | |
1278 | ||
6426f6ba | 1279 | for (; i < size-20; i++){ |
2eec55c8 | 1280 | fcCounter++; |
1281 | rfCounter++; | |
1282 | ||
1283 | if (BitStream[i] <= BitStream[i-1] || BitStream[i] < BitStream[i+1]) | |
1284 | continue; | |
1285 | // else new peak | |
1286 | // if we got less than the small fc + tolerance then set it to the small fc | |
1287 | if (fcCounter < fcLow+fcTol) | |
1288 | fcCounter = fcLow; | |
1289 | else //set it to the large fc | |
1290 | fcCounter = fcHigh; | |
1291 | ||
1292 | //look for bit clock (rf/xx) | |
1293 | if ((fcCounter < lastFCcnt || fcCounter > lastFCcnt)){ | |
1294 | //not the same size as the last wave - start of new bit sequence | |
1295 | if (firstBitFnd > 1){ //skip first wave change - probably not a complete bit | |
1296 | for (int ii=0; ii<15; ii++){ | |
6426f6ba | 1297 | if (rfLens[ii] >= (rfCounter-4) && rfLens[ii] <= (rfCounter+4)){ |
2eec55c8 | 1298 | rfCnts[ii]++; |
1299 | rfCounter = 0; | |
1300 | break; | |
e0165dcf | 1301 | } |
e0165dcf | 1302 | } |
2eec55c8 | 1303 | if (rfCounter > 0 && rfLensFnd < 15){ |
1304 | //PrintAndLog("DEBUG: rfCntr %d, fcCntr %d",rfCounter,fcCounter); | |
1305 | rfCnts[rfLensFnd]++; | |
1306 | rfLens[rfLensFnd++] = rfCounter; | |
1307 | } | |
1308 | } else { | |
1309 | firstBitFnd++; | |
e0165dcf | 1310 | } |
2eec55c8 | 1311 | rfCounter=0; |
1312 | lastFCcnt=fcCounter; | |
e0165dcf | 1313 | } |
2eec55c8 | 1314 | fcCounter=0; |
e0165dcf | 1315 | } |
1316 | uint8_t rfHighest=15, rfHighest2=15, rfHighest3=15; | |
1317 | ||
1318 | for (i=0; i<15; i++){ | |
e0165dcf | 1319 | //get highest 2 RF values (might need to get more values to compare or compare all?) |
1320 | if (rfCnts[i]>rfCnts[rfHighest]){ | |
1321 | rfHighest3=rfHighest2; | |
1322 | rfHighest2=rfHighest; | |
1323 | rfHighest=i; | |
1324 | } else if(rfCnts[i]>rfCnts[rfHighest2]){ | |
1325 | rfHighest3=rfHighest2; | |
1326 | rfHighest2=i; | |
1327 | } else if(rfCnts[i]>rfCnts[rfHighest3]){ | |
1328 | rfHighest3=i; | |
1329 | } | |
a126332a | 1330 | if (g_debugMode==2) prnt("DEBUG FSK: RF %d, cnts %d",rfLens[i], rfCnts[i]); |
e0165dcf | 1331 | } |
1332 | // set allowed clock remainder tolerance to be 1 large field clock length+1 | |
1333 | // we could have mistakenly made a 9 a 10 instead of an 8 or visa versa so rfLens could be 1 FC off | |
1334 | uint8_t tol1 = fcHigh+1; | |
1335 | ||
a126332a | 1336 | if (g_debugMode==2) prnt("DEBUG FSK: most counted rf values: 1 %d, 2 %d, 3 %d",rfLens[rfHighest],rfLens[rfHighest2],rfLens[rfHighest3]); |
e0165dcf | 1337 | |
1338 | // loop to find the highest clock that has a remainder less than the tolerance | |
1339 | // compare samples counted divided by | |
6426f6ba | 1340 | // test 128 down to 32 (shouldn't be possible to have fc/10 & fc/8 and rf/16 or less) |
e0165dcf | 1341 | int ii=7; |
6426f6ba | 1342 | for (; ii>=2; ii--){ |
e0165dcf | 1343 | if (rfLens[rfHighest] % clk[ii] < tol1 || rfLens[rfHighest] % clk[ii] > clk[ii]-tol1){ |
1344 | if (rfLens[rfHighest2] % clk[ii] < tol1 || rfLens[rfHighest2] % clk[ii] > clk[ii]-tol1){ | |
1345 | if (rfLens[rfHighest3] % clk[ii] < tol1 || rfLens[rfHighest3] % clk[ii] > clk[ii]-tol1){ | |
a126332a | 1346 | if (g_debugMode==2) prnt("DEBUG FSK: clk %d divides into the 3 most rf values within tolerance",clk[ii]); |
e0165dcf | 1347 | break; |
1348 | } | |
1349 | } | |
1350 | } | |
1351 | } | |
1352 | ||
1353 | if (ii<0) return 0; // oops we went too far | |
1354 | ||
1355 | return clk[ii]; | |
03e6bb4a | 1356 | } |
1e090a61 | 1357 | |
03e6bb4a | 1358 | //by marshmellow |
1359 | //countFC is to detect the field clock lengths. | |
1360 | //counts and returns the 2 most common wave lengths | |
6de43508 | 1361 | //mainly used for FSK field clock detection |
2eec55c8 | 1362 | uint16_t countFC(uint8_t *BitStream, size_t size, uint8_t fskAdj) |
03e6bb4a | 1363 | { |
6426f6ba | 1364 | uint8_t fcLens[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
1365 | uint16_t fcCnts[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
e0165dcf | 1366 | uint8_t fcLensFnd = 0; |
1367 | uint8_t lastFCcnt=0; | |
2eec55c8 | 1368 | uint8_t fcCounter = 0; |
e0165dcf | 1369 | size_t i; |
1370 | if (size == 0) return 0; | |
1371 | ||
1372 | // prime i to first up transition | |
6426f6ba | 1373 | for (i = 160; i < size-20; i++) |
e0165dcf | 1374 | if (BitStream[i] > BitStream[i-1] && BitStream[i] >= BitStream[i+1]) |
1375 | break; | |
1376 | ||
6426f6ba | 1377 | for (; i < size-20; i++){ |
e0165dcf | 1378 | if (BitStream[i] > BitStream[i-1] && BitStream[i] >= BitStream[i+1]){ |
1379 | // new up transition | |
1380 | fcCounter++; | |
2eec55c8 | 1381 | if (fskAdj){ |
1382 | //if we had 5 and now have 9 then go back to 8 (for when we get a fc 9 instead of an 8) | |
1383 | if (lastFCcnt==5 && fcCounter==9) fcCounter--; | |
1384 | //if fc=9 or 4 add one (for when we get a fc 9 instead of 10 or a 4 instead of a 5) | |
1385 | if ((fcCounter==9) || fcCounter==4) fcCounter++; | |
e0165dcf | 1386 | // save last field clock count (fc/xx) |
2eec55c8 | 1387 | lastFCcnt = fcCounter; |
1388 | } | |
e0165dcf | 1389 | // find which fcLens to save it to: |
6426f6ba | 1390 | for (int ii=0; ii<15; ii++){ |
e0165dcf | 1391 | if (fcLens[ii]==fcCounter){ |
1392 | fcCnts[ii]++; | |
1393 | fcCounter=0; | |
1394 | break; | |
1395 | } | |
1396 | } | |
6426f6ba | 1397 | if (fcCounter>0 && fcLensFnd<15){ |
e0165dcf | 1398 | //add new fc length |
1399 | fcCnts[fcLensFnd]++; | |
1400 | fcLens[fcLensFnd++]=fcCounter; | |
1401 | } | |
1402 | fcCounter=0; | |
1403 | } else { | |
1404 | // count sample | |
1405 | fcCounter++; | |
1406 | } | |
1407 | } | |
1408 | ||
6426f6ba | 1409 | uint8_t best1=14, best2=14, best3=14; |
e0165dcf | 1410 | uint16_t maxCnt1=0; |
1411 | // go through fclens and find which ones are bigest 2 | |
6426f6ba | 1412 | for (i=0; i<15; i++){ |
e0165dcf | 1413 | // get the 3 best FC values |
1414 | if (fcCnts[i]>maxCnt1) { | |
1415 | best3=best2; | |
1416 | best2=best1; | |
1417 | maxCnt1=fcCnts[i]; | |
1418 | best1=i; | |
1419 | } else if(fcCnts[i]>fcCnts[best2]){ | |
1420 | best3=best2; | |
1421 | best2=i; | |
1422 | } else if(fcCnts[i]>fcCnts[best3]){ | |
1423 | best3=i; | |
1424 | } | |
a126332a | 1425 | 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]); |
e0165dcf | 1426 | } |
6426f6ba | 1427 | if (fcLens[best1]==0) return 0; |
e0165dcf | 1428 | uint8_t fcH=0, fcL=0; |
1429 | if (fcLens[best1]>fcLens[best2]){ | |
1430 | fcH=fcLens[best1]; | |
1431 | fcL=fcLens[best2]; | |
1432 | } else{ | |
1433 | fcH=fcLens[best2]; | |
1434 | fcL=fcLens[best1]; | |
1435 | } | |
a126332a | 1436 | if ((size-180)/fcH/3 > fcCnts[best1]+fcCnts[best2]) { |
1437 | if (g_debugMode==2) prnt("DEBUG countfc: fc is too large: %u > %u. Not psk or fsk",(size-180)/fcH/3,fcCnts[best1]+fcCnts[best2]); | |
1438 | return 0; //lots of waves not psk or fsk | |
1439 | } | |
e0165dcf | 1440 | // TODO: take top 3 answers and compare to known Field clocks to get top 2 |
1441 | ||
1442 | uint16_t fcs = (((uint16_t)fcH)<<8) | fcL; | |
2eec55c8 | 1443 | if (fskAdj) return fcs; |
1444 | return fcLens[best1]; | |
6de43508 | 1445 | } |
1446 | ||
1447 | //by marshmellow - demodulate PSK1 wave | |
1448 | //uses wave lengths (# Samples) | |
1449 | int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert) | |
1450 | { | |
e0165dcf | 1451 | if (size == 0) return -1; |
2eec55c8 | 1452 | uint16_t loopCnt = 4096; //don't need to loop through entire array... |
e0165dcf | 1453 | if (*size<loopCnt) loopCnt = *size; |
1454 | ||
6426f6ba | 1455 | size_t numBits=0; |
e0165dcf | 1456 | uint8_t curPhase = *invert; |
1457 | size_t i, waveStart=1, waveEnd=0, firstFullWave=0, lastClkBit=0; | |
1458 | uint8_t fc=0, fullWaveLen=0, tol=1; | |
1459 | uint16_t errCnt=0, waveLenCnt=0; | |
2eec55c8 | 1460 | fc = countFC(dest, *size, 0); |
e0165dcf | 1461 | if (fc!=2 && fc!=4 && fc!=8) return -1; |
1462 | //PrintAndLog("DEBUG: FC: %d",fc); | |
1463 | *clock = DetectPSKClock(dest, *size, *clock); | |
2eec55c8 | 1464 | if (*clock == 0) return -1; |
e0165dcf | 1465 | int avgWaveVal=0, lastAvgWaveVal=0; |
1466 | //find first phase shift | |
1467 | for (i=0; i<loopCnt; i++){ | |
1468 | if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){ | |
1469 | waveEnd = i+1; | |
1470 | //PrintAndLog("DEBUG: waveEnd: %d",waveEnd); | |
1471 | waveLenCnt = waveEnd-waveStart; | |
6426f6ba | 1472 | if (waveLenCnt > fc && waveStart > fc && !(waveLenCnt > fc+2)){ //not first peak and is a large wave but not out of whack |
e0165dcf | 1473 | lastAvgWaveVal = avgWaveVal/(waveLenCnt); |
1474 | firstFullWave = waveStart; | |
1475 | fullWaveLen=waveLenCnt; | |
1476 | //if average wave value is > graph 0 then it is an up wave or a 1 | |
2eec55c8 | 1477 | if (lastAvgWaveVal > 123) curPhase ^= 1; //fudge graph 0 a little 123 vs 128 |
e0165dcf | 1478 | break; |
1479 | } | |
1480 | waveStart = i+1; | |
1481 | avgWaveVal = 0; | |
1482 | } | |
2eec55c8 | 1483 | avgWaveVal += dest[i+2]; |
e0165dcf | 1484 | } |
6426f6ba | 1485 | if (firstFullWave == 0) { |
1486 | // no phase shift detected - could be all 1's or 0's - doesn't matter where we start | |
1487 | // so skip a little to ensure we are past any Start Signal | |
1488 | firstFullWave = 160; | |
1489 | memset(dest, curPhase, firstFullWave / *clock); | |
1490 | } else { | |
1491 | memset(dest, curPhase^1, firstFullWave / *clock); | |
1492 | } | |
1493 | //advance bits | |
1494 | numBits += (firstFullWave / *clock); | |
1495 | //set start of wave as clock align | |
1496 | lastClkBit = firstFullWave; | |
e0165dcf | 1497 | //PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen); |
e0165dcf | 1498 | //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d", *clock, lastClkBit); |
1499 | waveStart = 0; | |
e0165dcf | 1500 | dest[numBits++] = curPhase; //set first read bit |
2eec55c8 | 1501 | for (i = firstFullWave + fullWaveLen - 1; i < *size-3; i++){ |
e0165dcf | 1502 | //top edge of wave = start of new wave |
1503 | if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){ | |
1504 | if (waveStart == 0) { | |
1505 | waveStart = i+1; | |
2eec55c8 | 1506 | waveLenCnt = 0; |
e0165dcf | 1507 | avgWaveVal = dest[i+1]; |
1508 | } else { //waveEnd | |
1509 | waveEnd = i+1; | |
1510 | waveLenCnt = waveEnd-waveStart; | |
1511 | lastAvgWaveVal = avgWaveVal/waveLenCnt; | |
1512 | if (waveLenCnt > fc){ | |
1513 | //PrintAndLog("DEBUG: avgWaveVal: %d, waveSum: %d",lastAvgWaveVal,avgWaveVal); | |
2eec55c8 | 1514 | //this wave is a phase shift |
e0165dcf | 1515 | //PrintAndLog("DEBUG: phase shift at: %d, len: %d, nextClk: %d, i: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+*clock-tol,i+1,fc); |
1516 | if (i+1 >= lastClkBit + *clock - tol){ //should be a clock bit | |
2eec55c8 | 1517 | curPhase ^= 1; |
e0165dcf | 1518 | dest[numBits++] = curPhase; |
1519 | lastClkBit += *clock; | |
2eec55c8 | 1520 | } else if (i < lastClkBit+10+fc){ |
e0165dcf | 1521 | //noise after a phase shift - ignore |
1522 | } else { //phase shift before supposed to based on clock | |
1523 | errCnt++; | |
2767fc02 | 1524 | dest[numBits++] = 7; |
e0165dcf | 1525 | } |
1526 | } else if (i+1 > lastClkBit + *clock + tol + fc){ | |
1527 | lastClkBit += *clock; //no phase shift but clock bit | |
1528 | dest[numBits++] = curPhase; | |
1529 | } | |
2eec55c8 | 1530 | avgWaveVal = 0; |
1531 | waveStart = i+1; | |
e0165dcf | 1532 | } |
1533 | } | |
2eec55c8 | 1534 | avgWaveVal += dest[i+1]; |
e0165dcf | 1535 | } |
1536 | *size = numBits; | |
1537 | return errCnt; | |
6de43508 | 1538 | } |
05164399 | 1539 | |
1540 | //by marshmellow | |
1541 | //attempt to identify a Sequence Terminator in ASK modulated raw wave | |
1542 | bool DetectST(uint8_t buffer[], size_t *size, int *foundclock) { | |
1543 | size_t bufsize = *size; | |
1544 | //need to loop through all samples and identify our clock, look for the ST pattern | |
1545 | uint8_t fndClk[] = {8,16,32,40,50,64,128}; | |
1546 | int clk = 0; | |
1547 | int tol = 0; | |
1548 | int i, j, skip, start, end, low, high, minClk, waveStart; | |
1549 | bool complete = false; | |
1550 | int tmpbuff[bufsize / 64]; | |
1551 | int waveLen[bufsize / 64]; | |
1552 | size_t testsize = (bufsize < 512) ? bufsize : 512; | |
1553 | int phaseoff = 0; | |
1554 | high = low = 128; | |
1555 | memset(tmpbuff, 0, sizeof(tmpbuff)); | |
1556 | ||
1557 | if ( getHiLo(buffer, testsize, &high, &low, 80, 80) == -1 ) { | |
1558 | if (g_debugMode==2) prnt("DEBUG STT: just noise detected - quitting"); | |
1559 | return false; //just noise | |
1560 | } | |
1561 | i = 0; | |
1562 | j = 0; | |
1563 | minClk = 255; | |
1564 | // get to first full low to prime loop and skip incomplete first pulse | |
1565 | while ((buffer[i] < high) && (i < bufsize)) | |
1566 | ++i; | |
1567 | while ((buffer[i] > low) && (i < bufsize)) | |
1568 | ++i; | |
1569 | skip = i; | |
1570 | ||
1571 | // populate tmpbuff buffer with pulse lengths | |
1572 | while (i < bufsize) { | |
1573 | // measure from low to low | |
1574 | while ((buffer[i] > low) && (i < bufsize)) | |
1575 | ++i; | |
1576 | start= i; | |
1577 | while ((buffer[i] < high) && (i < bufsize)) | |
1578 | ++i; | |
1579 | //first high point for this wave | |
1580 | waveStart = i; | |
1581 | while ((buffer[i] > low) && (i < bufsize)) | |
1582 | ++i; | |
1583 | if (j >= (bufsize/64)) { | |
1584 | break; | |
1585 | } | |
1586 | waveLen[j] = i - waveStart; //first high to first low | |
1587 | tmpbuff[j++] = i - start; | |
1588 | if (i-start < minClk && i < bufsize) { | |
1589 | minClk = i - start; | |
1590 | } | |
1591 | } | |
1592 | // set clock - might be able to get this externally and remove this work... | |
1593 | if (!clk) { | |
1594 | for (uint8_t clkCnt = 0; clkCnt<7; clkCnt++) { | |
1595 | tol = fndClk[clkCnt]/8; | |
1596 | if (minClk >= fndClk[clkCnt]-tol && minClk <= fndClk[clkCnt]+1) { | |
1597 | clk=fndClk[clkCnt]; | |
1598 | break; | |
1599 | } | |
1600 | } | |
1601 | // clock not found - ERROR | |
1602 | if (!clk) { | |
1603 | if (g_debugMode==2) prnt("DEBUG STT: clock not found - quitting"); | |
1604 | return false; | |
1605 | } | |
1606 | } else tol = clk/8; | |
1607 | ||
1608 | *foundclock = clk; | |
1609 | ||
1610 | // look for Sequence Terminator - should be pulses of clk*(1 or 1.5), clk*2, clk*(1.5 or 2) | |
1611 | start = -1; | |
1612 | for (i = 0; i < j - 4; ++i) { | |
1613 | skip += tmpbuff[i]; | |
1614 | if (tmpbuff[i] >= clk*1-tol && tmpbuff[i] <= (clk*2)+tol && waveLen[i] < clk+tol) { //1 to 2 clocks depending on 2 bits prior | |
1615 | if (tmpbuff[i+1] >= clk*2-tol && tmpbuff[i+1] <= clk*2+tol && waveLen[i+1] > clk*3/2-tol) { //2 clocks and wave size is 1 1/2 | |
1616 | if (tmpbuff[i+2] >= (clk*3)/2-tol && tmpbuff[i+2] <= clk*2+tol && waveLen[i+2] > clk-tol) { //1 1/2 to 2 clocks and at least one full clock wave | |
1617 | if (tmpbuff[i+3] >= clk*1-tol && tmpbuff[i+3] <= clk*2+tol) { //1 to 2 clocks for end of ST + first bit | |
1618 | start = i + 3; | |
1619 | break; | |
1620 | } | |
1621 | } | |
1622 | } | |
1623 | } | |
1624 | } | |
1625 | // first ST not found - ERROR | |
1626 | if (start < 0) { | |
1627 | if (g_debugMode==2) prnt("DEBUG STT: first STT not found - quitting"); | |
1628 | return false; | |
1629 | } | |
1630 | if (waveLen[i+2] > clk*1+tol) | |
1631 | phaseoff = 0; | |
1632 | else | |
1633 | phaseoff = clk/2; | |
1634 | ||
1635 | // skip over the remainder of ST | |
1636 | skip += clk*7/2; //3.5 clocks from tmpbuff[i] = end of st - also aligns for ending point | |
1637 | ||
1638 | // now do it again to find the end | |
1639 | end = skip; | |
1640 | for (i += 3; i < j - 4; ++i) { | |
1641 | end += tmpbuff[i]; | |
1642 | if (tmpbuff[i] >= clk*1-tol && tmpbuff[i] <= (clk*2)+tol) { //1 to 2 clocks depending on 2 bits prior | |
1643 | if (tmpbuff[i+1] >= clk*2-tol && tmpbuff[i+1] <= clk*2+tol && waveLen[i+1] > clk*3/2-tol) { //2 clocks and wave size is 1 1/2 | |
1644 | if (tmpbuff[i+2] >= (clk*3)/2-tol && tmpbuff[i+2] <= clk*2+tol && waveLen[i+2] > clk-tol) { //1 1/2 to 2 clocks and at least one full clock wave | |
1645 | if (tmpbuff[i+3] >= clk*1-tol && tmpbuff[i+3] <= clk*2+tol) { //1 to 2 clocks for end of ST + first bit | |
1646 | complete = true; | |
1647 | break; | |
1648 | } | |
1649 | } | |
1650 | } | |
1651 | } | |
1652 | } | |
1653 | end -= phaseoff; | |
1654 | //didn't find second ST - ERROR | |
1655 | if (!complete) { | |
1656 | if (g_debugMode==2) prnt("DEBUG STT: second STT not found - quitting"); | |
1657 | return false; | |
1658 | } | |
1659 | 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); | |
1660 | //now begin to trim out ST so we can use normal demod cmds | |
1661 | start = skip; | |
1662 | size_t datalen = end - start; | |
1663 | // check validity of datalen (should be even clock increments) - use a tolerance of up to 1/8th a clock | |
1664 | if (datalen % clk > clk/8) { | |
1665 | if (g_debugMode==2) prnt("DEBUG STT: datalen not divisible by clk: %u %% %d = %d - quitting", datalen, clk, datalen % clk); | |
1666 | return false; | |
1667 | } else { | |
1668 | // padd the amount off - could be problematic... but shouldn't happen often | |
1669 | datalen += datalen % clk; | |
1670 | } | |
1671 | // if datalen is less than one t55xx block - ERROR | |
1672 | if (datalen/clk < 8*4) { | |
1673 | if (g_debugMode==2) prnt("DEBUG STT: datalen is less than 1 full t55xx block - quitting"); | |
1674 | return false; | |
1675 | } | |
1676 | size_t dataloc = start; | |
1677 | size_t newloc = 0; | |
1678 | i=0; | |
1679 | // warning - overwriting buffer given with raw wave data with ST removed... | |
1680 | while ( dataloc < bufsize-(clk/2) ) { | |
1681 | //compensate for long high at end of ST not being high... (we cut out the high part) | |
1682 | if (buffer[dataloc]<high && buffer[dataloc]>low && buffer[dataloc+3]<high && buffer[dataloc+3]>low) { | |
1683 | for(i=0; i < clk/2-tol; ++i) { | |
1684 | buffer[dataloc+i] = high+5; | |
1685 | } | |
1686 | } | |
1687 | for (i=0; i<datalen; ++i) { | |
1688 | if (i+newloc < bufsize) { | |
1689 | if (i+newloc < dataloc) | |
1690 | buffer[i+newloc] = buffer[dataloc]; | |
1691 | ||
1692 | dataloc++; | |
1693 | } | |
1694 | } | |
1695 | newloc += i; | |
1696 | //skip next ST | |
1697 | dataloc += clk*4; | |
1698 | } | |
1699 | *size = newloc; | |
1700 | return true; | |
1701 | } |