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