]>
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 | |
1e090a61 | 15 | //by marshmellow |
16 | //get high and low with passed in fuzz factor. also return noise test = 1 for passed or 0 for only noise | |
17 | int getHiLo(uint8_t *BitStream, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo) | |
18 | { | |
19 | *high=0; | |
20 | *low=255; | |
21 | // get high and low thresholds | |
22 | for (int i=0; i < size; i++){ | |
23 | if (BitStream[i] > *high) *high = BitStream[i]; | |
24 | if (BitStream[i] < *low) *low = BitStream[i]; | |
25 | } | |
26 | if (*high < 123) return -1; // just noise | |
27 | *high = (int)(((*high-128)*(((float)fuzzHi)/100))+128); | |
28 | *low = (int)(((*low-128)*(((float)fuzzLo)/100))+128); | |
29 | return 1; | |
30 | } | |
31 | ||
eb191de6 | 32 | //by marshmellow |
33 | //takes 1s and 0s and searches for EM410x format - output EM ID | |
ec75f5c1 | 34 | uint64_t Em410xDecode(uint8_t *BitStream, size_t *size, size_t *startIdx) |
eb191de6 | 35 | { |
ba1a299c | 36 | //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future |
37 | // otherwise could be a void with no arguments | |
38 | //set defaults | |
c12512e9 | 39 | uint64_t lo=0; |
ba1a299c | 40 | uint32_t i = 0; |
dc065b4e | 41 | if (BitStream[10]>1){ //allow only 1s and 0s |
ba1a299c | 42 | // PrintAndLog("no data found"); |
43 | return 0; | |
44 | } | |
45 | uint8_t parityTest=0; | |
46 | // 111111111 bit pattern represent start of frame | |
47 | uint8_t frame_marker_mask[] = {1,1,1,1,1,1,1,1,1}; | |
48 | uint32_t idx = 0; | |
49 | uint32_t ii=0; | |
50 | uint8_t resetCnt = 0; | |
ec75f5c1 | 51 | while( (idx + 64) < *size) { |
ba1a299c | 52 | restart: |
53 | // search for a start of frame marker | |
54 | if ( memcmp(BitStream+idx, frame_marker_mask, sizeof(frame_marker_mask)) == 0) | |
55 | { // frame marker found | |
ec75f5c1 | 56 | *startIdx=idx; |
c12512e9 | 57 | idx+=9; |
ba1a299c | 58 | for (i=0; i<10;i++){ |
59 | for(ii=0; ii<5; ++ii){ | |
dc065b4e | 60 | parityTest ^= BitStream[(i*5)+ii+idx]; |
ba1a299c | 61 | } |
ec75f5c1 | 62 | if (!parityTest){ //even parity |
ba1a299c | 63 | parityTest=0; |
64 | for (ii=0; ii<4;++ii){ | |
ba1a299c | 65 | lo=(lo<<1LL)|(BitStream[(i*5)+ii+idx]); |
66 | } | |
67 | //PrintAndLog("DEBUG: EM parity passed parity val: %d, i:%d, ii:%d,idx:%d, Buffer: %d%d%d%d%d,lo: %d",parityTest,i,ii,idx,BitStream[idx+ii+(i*5)-5],BitStream[idx+ii+(i*5)-4],BitStream[idx+ii+(i*5)-3],BitStream[idx+ii+(i*5)-2],BitStream[idx+ii+(i*5)-1],lo); | |
68 | }else {//parity failed | |
69 | //PrintAndLog("DEBUG: EM parity failed parity val: %d, i:%d, ii:%d,idx:%d, Buffer: %d%d%d%d%d",parityTest,i,ii,idx,BitStream[idx+ii+(i*5)-5],BitStream[idx+ii+(i*5)-4],BitStream[idx+ii+(i*5)-3],BitStream[idx+ii+(i*5)-2],BitStream[idx+ii+(i*5)-1]); | |
70 | parityTest=0; | |
71 | idx-=8; | |
dc065b4e | 72 | if (resetCnt>5)return 0; //try 5 times |
ba1a299c | 73 | resetCnt++; |
74 | goto restart;//continue; | |
75 | } | |
76 | } | |
77 | //skip last 5 bit parity test for simplicity. | |
ec75f5c1 | 78 | *size = 64; |
ba1a299c | 79 | return lo; |
80 | }else{ | |
81 | idx++; | |
82 | } | |
83 | } | |
84 | return 0; | |
eb191de6 | 85 | } |
86 | ||
87 | //by marshmellow | |
88 | //takes 2 arguments - clock and invert both as integers | |
ba1a299c | 89 | //attempts to demodulate ask while decoding manchester |
eb191de6 | 90 | //prints binary found and saves in graphbuffer for further commands |
ba1a299c | 91 | int askmandemod(uint8_t *BinStream, size_t *size, int *clk, int *invert) |
eb191de6 | 92 | { |
ba1a299c | 93 | int i; |
ec75f5c1 | 94 | int clk2=*clk; |
ba1a299c | 95 | *clk=DetectASKClock(BinStream, *size, *clk); //clock default |
96 | ||
ec75f5c1 | 97 | // if autodetected too low then adjust //MAY NEED ADJUSTMENT |
98 | if (clk2==0 && *clk<8) *clk =64; | |
99 | if (clk2==0 && *clk<32) *clk=32; | |
ba1a299c | 100 | if (*invert != 0 && *invert != 1) *invert=0; |
101 | uint32_t initLoopMax = 200; | |
102 | if (initLoopMax > *size) initLoopMax=*size; | |
103 | // Detect high and lows | |
1e090a61 | 104 | // 25% fuzz in case highs and lows aren't clipped [marshmellow] |
105 | int high, low, ans; | |
106 | ans = getHiLo(BinStream, initLoopMax, &high, &low, 75, 75); | |
107 | if (ans<1) return -2; //just noise | |
ba1a299c | 108 | |
1e090a61 | 109 | // PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low); |
ba1a299c | 110 | int lastBit = 0; //set first clock check |
111 | uint32_t bitnum = 0; //output counter | |
112 | 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 | |
ec75f5c1 | 113 | 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 | 114 | int iii = 0; |
115 | uint32_t gLen = *size; | |
116 | if (gLen > 3000) gLen=3000; | |
117 | uint8_t errCnt =0; | |
118 | uint32_t bestStart = *size; | |
119 | uint32_t bestErrCnt = (*size/1000); | |
120 | uint32_t maxErr = (*size/1000); | |
1e090a61 | 121 | // PrintAndLog("DEBUG - lastbit - %d",lastBit); |
122 | // loop to find first wave that works | |
ba1a299c | 123 | for (iii=0; iii < gLen; ++iii){ |
124 | if ((BinStream[iii] >= high) || (BinStream[iii] <= low)){ | |
125 | lastBit=iii-*clk; | |
126 | errCnt=0; | |
1e090a61 | 127 | // loop through to see if this start location works |
ba1a299c | 128 | for (i = iii; i < *size; ++i) { |
129 | if ((BinStream[i] >= high) && ((i-lastBit) > (*clk-tol))){ | |
130 | lastBit+=*clk; | |
131 | } else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){ | |
132 | //low found and we are expecting a bar | |
133 | lastBit+=*clk; | |
134 | } else { | |
135 | //mid value found or no bar supposed to be here | |
136 | if ((i-lastBit)>(*clk+tol)){ | |
137 | //should have hit a high or low based on clock!! | |
138 | ||
139 | //debug | |
140 | //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); | |
141 | ||
142 | errCnt++; | |
143 | lastBit+=*clk;//skip over until hit too many errors | |
144 | if (errCnt>(maxErr)) break; //allow 1 error for every 1000 samples else start over | |
145 | } | |
146 | } | |
147 | if ((i-iii) >(400 * *clk)) break; //got plenty of bits | |
148 | } | |
149 | //we got more than 64 good bits and not all errors | |
150 | if ((((i-iii)/ *clk) > (64+errCnt)) && (errCnt<maxErr)) { | |
151 | //possible good read | |
152 | if (errCnt==0){ | |
153 | bestStart=iii; | |
154 | bestErrCnt=errCnt; | |
155 | break; //great read - finish | |
156 | } | |
157 | if (errCnt<bestErrCnt){ //set this as new best run | |
158 | bestErrCnt=errCnt; | |
159 | bestStart = iii; | |
160 | } | |
161 | } | |
162 | } | |
163 | } | |
164 | if (bestErrCnt<maxErr){ | |
165 | //best run is good enough set to best run and set overwrite BinStream | |
166 | iii=bestStart; | |
167 | lastBit = bestStart - *clk; | |
168 | bitnum=0; | |
169 | for (i = iii; i < *size; ++i) { | |
170 | if ((BinStream[i] >= high) && ((i-lastBit) > (*clk-tol))){ | |
171 | lastBit += *clk; | |
172 | BinStream[bitnum] = *invert; | |
173 | bitnum++; | |
174 | } else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){ | |
175 | //low found and we are expecting a bar | |
176 | lastBit+=*clk; | |
177 | BinStream[bitnum] = 1-*invert; | |
178 | bitnum++; | |
179 | } else { | |
180 | //mid value found or no bar supposed to be here | |
181 | if ((i-lastBit)>(*clk+tol)){ | |
182 | //should have hit a high or low based on clock!! | |
183 | ||
184 | //debug | |
185 | //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); | |
186 | if (bitnum > 0){ | |
187 | BinStream[bitnum]=77; | |
188 | bitnum++; | |
189 | } | |
190 | ||
191 | lastBit+=*clk;//skip over error | |
192 | } | |
193 | } | |
194 | if (bitnum >=400) break; | |
195 | } | |
196 | *size=bitnum; | |
197 | } else{ | |
198 | *invert=bestStart; | |
199 | *clk=iii; | |
200 | return -1; | |
201 | } | |
202 | return bestErrCnt; | |
eb191de6 | 203 | } |
204 | ||
ec75f5c1 | 205 | //by marshmellow |
206 | //encode binary data into binary manchester | |
207 | int ManchesterEncode(uint8_t *BitStream, size_t size) | |
208 | { | |
209 | size_t modIdx=20000, i=0; | |
210 | if (size>modIdx) return -1; | |
211 | for (size_t idx=0; idx < size; idx++){ | |
212 | BitStream[idx+modIdx++] = BitStream[idx]; | |
213 | BitStream[idx+modIdx++] = BitStream[idx]^1; | |
214 | } | |
215 | for (; i<(size*2); i++){ | |
216 | BitStream[i] = BitStream[i+20000]; | |
217 | } | |
218 | return i; | |
219 | } | |
220 | ||
eb191de6 | 221 | //by marshmellow |
222 | //take 10 and 01 and manchester decode | |
223 | //run through 2 times and take least errCnt | |
ba1a299c | 224 | int manrawdecode(uint8_t * BitStream, size_t *size) |
eb191de6 | 225 | { |
ba1a299c | 226 | int bitnum=0; |
227 | int errCnt =0; | |
228 | int i=1; | |
229 | int bestErr = 1000; | |
230 | int bestRun = 0; | |
231 | int ii=1; | |
232 | for (ii=1;ii<3;++ii){ | |
233 | i=1; | |
234 | for (i=i+ii;i<*size-2;i+=2){ | |
235 | if(BitStream[i]==1 && (BitStream[i+1]==0)){ | |
236 | } else if((BitStream[i]==0)&& BitStream[i+1]==1){ | |
237 | } else { | |
238 | errCnt++; | |
239 | } | |
240 | if(bitnum>300) break; | |
241 | } | |
242 | if (bestErr>errCnt){ | |
243 | bestErr=errCnt; | |
244 | bestRun=ii; | |
245 | } | |
246 | errCnt=0; | |
247 | } | |
248 | errCnt=bestErr; | |
249 | if (errCnt<20){ | |
250 | ii=bestRun; | |
251 | i=1; | |
252 | for (i=i+ii;i < *size-2;i+=2){ | |
253 | if(BitStream[i] == 1 && (BitStream[i+1] == 0)){ | |
254 | BitStream[bitnum++]=0; | |
255 | } else if((BitStream[i] == 0) && BitStream[i+1] == 1){ | |
256 | BitStream[bitnum++]=1; | |
257 | } else { | |
258 | BitStream[bitnum++]=77; | |
259 | //errCnt++; | |
260 | } | |
261 | if(bitnum>300) break; | |
262 | } | |
263 | *size=bitnum; | |
264 | } | |
265 | return errCnt; | |
f822a063 | 266 | } |
267 | ||
f822a063 | 268 | //by marshmellow |
269 | //take 01 or 10 = 0 and 11 or 00 = 1 | |
1e090a61 | 270 | int BiphaseRawDecode(uint8_t *BitStream, size_t *size, int offset, int invert) |
f822a063 | 271 | { |
ba1a299c | 272 | uint8_t bitnum=0; |
273 | uint32_t errCnt =0; | |
1e090a61 | 274 | uint32_t i; |
ba1a299c | 275 | i=offset; |
1e090a61 | 276 | for (;i<*size-2; i+=2){ |
ba1a299c | 277 | if((BitStream[i]==1 && BitStream[i+1]==0) || (BitStream[i]==0 && BitStream[i+1]==1)){ |
1e090a61 | 278 | BitStream[bitnum++]=1^invert; |
ba1a299c | 279 | } else if((BitStream[i]==0 && BitStream[i+1]==0) || (BitStream[i]==1 && BitStream[i+1]==1)){ |
1e090a61 | 280 | BitStream[bitnum++]=invert; |
ba1a299c | 281 | } else { |
282 | BitStream[bitnum++]=77; | |
283 | errCnt++; | |
284 | } | |
285 | if(bitnum>250) break; | |
286 | } | |
287 | *size=bitnum; | |
288 | return errCnt; | |
eb191de6 | 289 | } |
290 | ||
291 | //by marshmellow | |
292 | //takes 2 arguments - clock and invert both as integers | |
293 | //attempts to demodulate ask only | |
294 | //prints binary found and saves in graphbuffer for further commands | |
ba1a299c | 295 | int askrawdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert) |
eb191de6 | 296 | { |
ba1a299c | 297 | uint32_t i; |
298 | // int invert=0; //invert default | |
1e090a61 | 299 | int clk2 = *clk; |
ba1a299c | 300 | *clk=DetectASKClock(BinStream, *size, *clk); //clock default |
1e090a61 | 301 | //uint8_t BitStream[502] = {0}; |
ba1a299c | 302 | |
1e090a61 | 303 | //HACK: if clock not detected correctly - default |
ec75f5c1 | 304 | if (clk2==0 && *clk<8) *clk =64; |
305 | if (clk2==0 && *clk<32 && clk2==0) *clk=32; | |
ba1a299c | 306 | if (*invert != 0 && *invert != 1) *invert =0; |
307 | uint32_t initLoopMax = 200; | |
c12512e9 | 308 | if (initLoopMax > *size) initLoopMax=*size; |
ba1a299c | 309 | // Detect high and lows |
ba1a299c | 310 | //25% fuzz in case highs and lows aren't clipped [marshmellow] |
1e090a61 | 311 | int high, low, ans; |
312 | ans = getHiLo(BinStream, initLoopMax, &high, &low, 75, 75); | |
313 | if (ans<1) return -2; //just noise | |
ba1a299c | 314 | |
315 | //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low); | |
316 | int lastBit = 0; //set first clock check | |
317 | uint32_t bitnum = 0; //output counter | |
c12512e9 | 318 | uint8_t tol = 0; //clock tolerance adjust - waves will be accepted as within the clock |
319 | // if they fall + or - this value + clock from last valid wave | |
320 | if (*clk == 32) tol=1; //clock tolerance may not be needed anymore currently set to | |
321 | // + or - 1 but could be increased for poor waves or removed entirely | |
ba1a299c | 322 | uint32_t iii = 0; |
323 | uint32_t gLen = *size; | |
324 | if (gLen > 500) gLen=500; | |
325 | uint8_t errCnt =0; | |
326 | uint32_t bestStart = *size; | |
327 | uint32_t bestErrCnt = (*size/1000); | |
1e090a61 | 328 | uint32_t maxErr = bestErrCnt; |
ba1a299c | 329 | uint8_t midBit=0; |
330 | //PrintAndLog("DEBUG - lastbit - %d",lastBit); | |
331 | //loop to find first wave that works | |
332 | for (iii=0; iii < gLen; ++iii){ | |
333 | if ((BinStream[iii]>=high) || (BinStream[iii]<=low)){ | |
334 | lastBit=iii-*clk; | |
335 | //loop through to see if this start location works | |
336 | for (i = iii; i < *size; ++i) { | |
337 | if ((BinStream[i] >= high) && ((i-lastBit)>(*clk-tol))){ | |
338 | lastBit+=*clk; | |
ba1a299c | 339 | midBit=0; |
340 | } else if ((BinStream[i] <= low) && ((i-lastBit)>(*clk-tol))){ | |
341 | //low found and we are expecting a bar | |
342 | lastBit+=*clk; | |
ba1a299c | 343 | midBit=0; |
344 | } else if ((BinStream[i]<=low) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){ | |
345 | //mid bar? | |
346 | midBit=1; | |
ba1a299c | 347 | } else if ((BinStream[i]>=high) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){ |
348 | //mid bar? | |
349 | midBit=1; | |
ba1a299c | 350 | } else if ((i-lastBit)>((*clk/2)+tol) && (midBit==0)){ |
351 | //no mid bar found | |
352 | midBit=1; | |
ba1a299c | 353 | } else { |
354 | //mid value found or no bar supposed to be here | |
355 | ||
356 | if ((i-lastBit)>(*clk+tol)){ | |
357 | //should have hit a high or low based on clock!! | |
358 | //debug | |
359 | //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 | 360 | |
ba1a299c | 361 | errCnt++; |
362 | lastBit+=*clk;//skip over until hit too many errors | |
363 | if (errCnt > ((*size/1000))){ //allow 1 error for every 1000 samples else start over | |
364 | errCnt=0; | |
ba1a299c | 365 | break; |
366 | } | |
367 | } | |
368 | } | |
1e090a61 | 369 | if ((i-iii)>(500 * *clk)) break; //got enough bits |
ba1a299c | 370 | } |
371 | //we got more than 64 good bits and not all errors | |
1e090a61 | 372 | if ((((i-iii)/ *clk) > (64+errCnt)) && (errCnt<(*size/1000))) { |
ba1a299c | 373 | //possible good read |
1e090a61 | 374 | if (errCnt==0){ |
375 | bestStart=iii; | |
376 | bestErrCnt=errCnt; | |
377 | break; //great read - finish | |
378 | } | |
ba1a299c | 379 | if (errCnt<bestErrCnt){ //set this as new best run |
380 | bestErrCnt=errCnt; | |
381 | bestStart = iii; | |
382 | } | |
383 | } | |
384 | } | |
ba1a299c | 385 | } |
1e090a61 | 386 | if (bestErrCnt<maxErr){ |
387 | //best run is good enough - set to best run and overwrite BinStream | |
388 | iii=bestStart; | |
389 | lastBit = bestStart - *clk; | |
390 | bitnum=0; | |
391 | for (i = iii; i < *size; ++i) { | |
392 | if ((BinStream[i] >= high) && ((i-lastBit) > (*clk-tol))){ | |
393 | lastBit += *clk; | |
394 | BinStream[bitnum] = *invert; | |
395 | bitnum++; | |
396 | midBit=0; | |
397 | } else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){ | |
398 | //low found and we are expecting a bar | |
399 | lastBit+=*clk; | |
400 | BinStream[bitnum] = 1-*invert; | |
401 | bitnum++; | |
402 | midBit=0; | |
403 | } else if ((BinStream[i]<=low) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){ | |
404 | //mid bar? | |
405 | midBit=1; | |
406 | BinStream[bitnum] = 1 - *invert; | |
407 | bitnum++; | |
408 | } else if ((BinStream[i]>=high) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){ | |
409 | //mid bar? | |
410 | midBit=1; | |
411 | BinStream[bitnum] = *invert; | |
412 | bitnum++; | |
413 | } else if ((i-lastBit)>((*clk/2)+tol) && (midBit==0)){ | |
414 | //no mid bar found | |
415 | midBit=1; | |
416 | if (bitnum!=0) BinStream[bitnum] = BinStream[bitnum-1]; | |
417 | bitnum++; | |
418 | ||
419 | } else { | |
420 | //mid value found or no bar supposed to be here | |
421 | if ((i-lastBit)>(*clk+tol)){ | |
422 | //should have hit a high or low based on clock!! | |
423 | ||
424 | //debug | |
425 | //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); | |
426 | if (bitnum > 0){ | |
427 | BinStream[bitnum]=77; | |
428 | bitnum++; | |
429 | } | |
430 | ||
431 | lastBit+=*clk;//skip over error | |
432 | } | |
433 | } | |
434 | if (bitnum >=400) break; | |
ba1a299c | 435 | } |
436 | *size=bitnum; | |
1e090a61 | 437 | } else{ |
438 | *invert=bestStart; | |
439 | *clk=iii; | |
440 | return -1; | |
441 | } | |
442 | return bestErrCnt; | |
eb191de6 | 443 | } |
ba1a299c | 444 | //translate wave to 11111100000 (1 for each short wave 0 for each long wave) |
f822a063 | 445 | size_t fsk_wave_demod(uint8_t * dest, size_t size, uint8_t fchigh, uint8_t fclow) |
eb191de6 | 446 | { |
ba1a299c | 447 | uint32_t last_transition = 0; |
448 | uint32_t idx = 1; | |
ac3ba7ee | 449 | //uint32_t maxVal=0; |
ba1a299c | 450 | if (fchigh==0) fchigh=10; |
451 | if (fclow==0) fclow=8; | |
84871873 | 452 | //set the threshold close to 0 (graph) or 128 std to avoid static |
453 | uint8_t threshold_value = 123; | |
ba1a299c | 454 | |
455 | // sync to first lo-hi transition, and threshold | |
456 | ||
457 | // Need to threshold first sample | |
458 | ||
459 | if(dest[0] < threshold_value) dest[0] = 0; | |
460 | else dest[0] = 1; | |
461 | ||
462 | size_t numBits = 0; | |
463 | // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8) | |
464 | // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere | |
465 | // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10 | |
466 | for(idx = 1; idx < size; idx++) { | |
467 | // threshold current value | |
468 | ||
469 | if (dest[idx] < threshold_value) dest[idx] = 0; | |
470 | else dest[idx] = 1; | |
471 | ||
472 | // Check for 0->1 transition | |
473 | if (dest[idx-1] < dest[idx]) { // 0 -> 1 transition | |
474 | if ((idx-last_transition)<(fclow-2)){ //0-5 = garbage noise | |
475 | //do nothing with extra garbage | |
476 | } else if ((idx-last_transition) < (fchigh-1)) { //6-8 = 8 waves | |
477 | dest[numBits]=1; | |
478 | } else { //9+ = 10 waves | |
479 | dest[numBits]=0; | |
480 | } | |
481 | last_transition = idx; | |
482 | numBits++; | |
483 | } | |
484 | } | |
485 | return numBits; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0 | |
eb191de6 | 486 | } |
487 | ||
488 | uint32_t myround2(float f) | |
489 | { | |
ba1a299c | 490 | if (f >= 2000) return 2000;//something bad happened |
491 | return (uint32_t) (f + (float)0.5); | |
eb191de6 | 492 | } |
493 | ||
ba1a299c | 494 | //translate 11111100000 to 10 |
495 | size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t maxConsequtiveBits, | |
496 | uint8_t invert, uint8_t fchigh, uint8_t fclow) | |
eb191de6 | 497 | { |
ba1a299c | 498 | uint8_t lastval=dest[0]; |
499 | uint32_t idx=0; | |
500 | size_t numBits=0; | |
501 | uint32_t n=1; | |
502 | ||
503 | for( idx=1; idx < size; idx++) { | |
504 | ||
505 | if (dest[idx]==lastval) { | |
506 | n++; | |
507 | continue; | |
508 | } | |
509 | //if lastval was 1, we have a 1->0 crossing | |
510 | if ( dest[idx-1]==1 ) { | |
511 | n=myround2((float)(n+1)/((float)(rfLen)/(float)fclow)); | |
ba1a299c | 512 | } else {// 0->1 crossing |
84871873 | 513 | n=myround2((float)(n+1)/((float)(rfLen-1)/(float)fchigh)); //-1 for fudge factor |
ba1a299c | 514 | } |
515 | if (n == 0) n = 1; | |
516 | ||
517 | if(n < maxConsequtiveBits) //Consecutive | |
518 | { | |
519 | if(invert==0){ //invert bits | |
520 | memset(dest+numBits, dest[idx-1] , n); | |
521 | }else{ | |
522 | memset(dest+numBits, dest[idx-1]^1 , n); | |
523 | } | |
524 | numBits += n; | |
525 | } | |
526 | n=0; | |
527 | lastval=dest[idx]; | |
528 | }//end for | |
529 | return numBits; | |
eb191de6 | 530 | } |
531 | //by marshmellow (from holiman's base) | |
532 | // full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod) | |
f822a063 | 533 | int fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow) |
eb191de6 | 534 | { |
ba1a299c | 535 | // FSK demodulator |
536 | size = fsk_wave_demod(dest, size, fchigh, fclow); | |
537 | size = aggregate_bits(dest, size, rfLen, 192, invert, fchigh, fclow); | |
538 | return size; | |
eb191de6 | 539 | } |
540 | // loop to get raw HID waveform then FSK demodulate the TAG ID from it | |
ec75f5c1 | 541 | int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo) |
eb191de6 | 542 | { |
3400a435 | 543 | |
ec75f5c1 | 544 | size_t idx=0, size2=*size, startIdx=0; |
ba1a299c | 545 | // FSK demodulator |
ec75f5c1 | 546 | |
547 | *size = fskdemod(dest, size2,50,0,10,8); | |
ba1a299c | 548 | |
549 | // final loop, go over previously decoded manchester data and decode into usable tag ID | |
550 | // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0 | |
551 | uint8_t frame_marker_mask[] = {1,1,1,0,0,0}; | |
552 | int numshifts = 0; | |
553 | idx = 0; | |
554 | //one scan | |
ec75f5c1 | 555 | while( idx + sizeof(frame_marker_mask) < *size) { |
ba1a299c | 556 | // search for a start of frame marker |
557 | if ( memcmp(dest+idx, frame_marker_mask, sizeof(frame_marker_mask)) == 0) | |
558 | { // frame marker found | |
ec75f5c1 | 559 | startIdx=idx; |
ba1a299c | 560 | idx+=sizeof(frame_marker_mask); |
ec75f5c1 | 561 | while(dest[idx] != dest[idx+1] && idx < *size-2) |
ba1a299c | 562 | { |
563 | // Keep going until next frame marker (or error) | |
564 | // Shift in a bit. Start by shifting high registers | |
565 | *hi2 = (*hi2<<1)|(*hi>>31); | |
566 | *hi = (*hi<<1)|(*lo>>31); | |
567 | //Then, shift in a 0 or one into low | |
568 | if (dest[idx] && !dest[idx+1]) // 1 0 | |
569 | *lo=(*lo<<1)|0; | |
570 | else // 0 1 | |
571 | *lo=(*lo<<1)|1; | |
572 | numshifts++; | |
573 | idx += 2; | |
574 | } | |
575 | // Hopefully, we read a tag and hit upon the next frame marker | |
ec75f5c1 | 576 | if(idx + sizeof(frame_marker_mask) < *size) |
ba1a299c | 577 | { |
578 | if ( memcmp(dest+idx, frame_marker_mask, sizeof(frame_marker_mask)) == 0) | |
579 | { | |
580 | //good return | |
ec75f5c1 | 581 | *size=idx-startIdx; |
582 | return startIdx; | |
ba1a299c | 583 | } |
584 | } | |
585 | // reset | |
586 | *hi2 = *hi = *lo = 0; | |
587 | numshifts = 0; | |
588 | }else { | |
589 | idx++; | |
590 | } | |
591 | } | |
592 | return -1; | |
eb191de6 | 593 | } |
594 | ||
ec75f5c1 | 595 | // loop to get raw paradox waveform then FSK demodulate the TAG ID from it |
596 | size_t ParadoxdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo) | |
597 | { | |
598 | ||
599 | size_t idx=0, size2=*size; | |
600 | // FSK demodulator | |
601 | ||
602 | *size = fskdemod(dest, size2,50,1,10,8); | |
603 | ||
604 | // final loop, go over previously decoded manchester data and decode into usable tag ID | |
605 | // 00001111 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0 | |
606 | uint8_t frame_marker_mask[] = {0,0,0,0,1,1,1,1}; | |
607 | uint16_t numshifts = 0; | |
608 | idx = 0; | |
609 | //one scan | |
610 | while( idx + sizeof(frame_marker_mask) < *size) { | |
611 | // search for a start of frame marker | |
612 | if ( memcmp(dest+idx, frame_marker_mask, sizeof(frame_marker_mask)) == 0) | |
613 | { // frame marker found | |
614 | size2=idx; | |
615 | idx+=sizeof(frame_marker_mask); | |
616 | while(dest[idx] != dest[idx+1] && idx < *size-2) | |
617 | { | |
618 | // Keep going until next frame marker (or error) | |
619 | // Shift in a bit. Start by shifting high registers | |
620 | *hi2 = (*hi2<<1)|(*hi>>31); | |
621 | *hi = (*hi<<1)|(*lo>>31); | |
622 | //Then, shift in a 0 or one into low | |
623 | if (dest[idx] && !dest[idx+1]) // 1 0 | |
624 | *lo=(*lo<<1)|1; | |
625 | else // 0 1 | |
626 | *lo=(*lo<<1)|0; | |
627 | numshifts++; | |
628 | idx += 2; | |
629 | } | |
630 | // Hopefully, we read a tag and hit upon the next frame marker and got enough bits | |
631 | if(idx + sizeof(frame_marker_mask) < *size && numshifts > 40) | |
632 | { | |
633 | if ( memcmp(dest+idx, frame_marker_mask, sizeof(frame_marker_mask)) == 0) | |
634 | { | |
635 | //good return - return start grid position and bits found | |
636 | *size = ((numshifts*2)+8); | |
637 | return size2; | |
638 | } | |
639 | } | |
640 | // reset | |
641 | *hi2 = *hi = *lo = 0; | |
642 | numshifts = 0; | |
643 | }else { | |
644 | idx++; | |
645 | } | |
646 | } | |
647 | return 0; | |
648 | } | |
649 | ||
ba1a299c | 650 | uint32_t bytebits_to_byte(uint8_t* src, size_t numbits) |
eb191de6 | 651 | { |
ba1a299c | 652 | uint32_t num = 0; |
653 | for(int i = 0 ; i < numbits ; i++) | |
654 | { | |
655 | num = (num << 1) | (*src); | |
656 | src++; | |
657 | } | |
658 | return num; | |
eb191de6 | 659 | } |
660 | ||
661 | int IOdemodFSK(uint8_t *dest, size_t size) | |
662 | { | |
84871873 | 663 | static const uint8_t THRESHOLD = 129; |
ba1a299c | 664 | uint32_t idx=0; |
665 | //make sure buffer has data | |
666 | if (size < 66) return -1; | |
667 | //test samples are not just noise | |
668 | uint8_t justNoise = 1; | |
669 | for(idx=0;idx< size && justNoise ;idx++){ | |
670 | justNoise = dest[idx] < THRESHOLD; | |
671 | } | |
672 | if(justNoise) return 0; | |
673 | ||
674 | // FSK demodulator | |
675 | size = fskdemod(dest, size, 64, 1, 10, 8); // RF/64 and invert | |
676 | if (size < 65) return -1; //did we get a good demod? | |
677 | //Index map | |
678 | //0 10 20 30 40 50 60 | |
679 | //| | | | | | | | |
680 | //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23 | |
681 | //----------------------------------------------------------------------------- | |
682 | //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11 | |
683 | // | |
684 | //XSF(version)facility:codeone+codetwo | |
685 | //Handle the data | |
686 | uint8_t mask[] = {0,0,0,0,0,0,0,0,0,1}; | |
687 | for( idx=0; idx < (size - 65); idx++) { | |
688 | if ( memcmp(dest + idx, mask, sizeof(mask))==0) { | |
689 | //frame marker found | |
690 | if (!dest[idx+8] && dest[idx+17]==1 && dest[idx+26]==1 && dest[idx+35]==1 && dest[idx+44]==1 && dest[idx+53]==1){ | |
691 | //confirmed proper separator bits found | |
692 | //return start position | |
693 | return (int) idx; | |
694 | } | |
695 | } | |
696 | } | |
697 | return 0; | |
eb191de6 | 698 | } |
699 | ||
1e090a61 | 700 | // by marshmellow |
701 | // pass bits to be tested in bits, length bits passed in bitLen, and parity type (even=0 | odd=1) in pType | |
702 | // returns 1 if passed | |
ec75f5c1 | 703 | uint8_t parityTest(uint32_t bits, uint8_t bitLen, uint8_t pType) |
1e090a61 | 704 | { |
705 | uint8_t ans = 0; | |
ec75f5c1 | 706 | for (uint8_t i = 0; i < bitLen; i++){ |
1e090a61 | 707 | ans ^= ((bits >> i) & 1); |
708 | } | |
709 | //PrintAndLog("DEBUG: ans: %d, ptype: %d",ans,pType); | |
710 | return (ans == pType); | |
711 | } | |
712 | ||
713 | // by marshmellow | |
714 | // takes a array of binary values, start position, length of bits per parity (includes parity bit), | |
715 | // Parity Type (1 for odd 0 for even), and binary Length (length to run) | |
716 | size_t removeParity(uint8_t *BitStream, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen) | |
717 | { | |
718 | uint32_t parityWd = 0; | |
719 | size_t j = 0, bitCnt = 0; | |
720 | for (int word = 0; word < (bLen); word+=pLen){ | |
721 | for (int bit=0; bit < pLen; bit++){ | |
722 | parityWd = (parityWd << 1) | BitStream[startIdx+word+bit]; | |
723 | BitStream[j++] = (BitStream[startIdx+word+bit]); | |
724 | } | |
725 | j--; | |
726 | // if parity fails then return 0 | |
727 | if (parityTest(parityWd, pLen, pType) == 0) return -1; | |
728 | bitCnt+=(pLen-1); | |
729 | parityWd = 0; | |
730 | } | |
731 | // if we got here then all the parities passed | |
732 | //return ID start index and size | |
733 | return bitCnt; | |
734 | } | |
735 | ||
736 | // by marshmellow | |
737 | // FSK Demod then try to locate an AWID ID | |
738 | int AWIDdemodFSK(uint8_t *dest, size_t size) | |
739 | { | |
740 | static const uint8_t THRESHOLD = 123; | |
ec75f5c1 | 741 | uint32_t idx=0, idx2=0; |
1e090a61 | 742 | //make sure buffer has data |
743 | if (size < 96*50) return -1; | |
744 | //test samples are not just noise | |
745 | uint8_t justNoise = 1; | |
746 | for(idx=0; idx < size && justNoise ;idx++){ | |
747 | justNoise = dest[idx] < THRESHOLD; | |
748 | } | |
749 | if(justNoise) return -2; | |
750 | ||
751 | // FSK demodulator | |
752 | size = fskdemod(dest, size, 50, 1, 10, 8); // RF/64 and invert | |
753 | if (size < 96) return -3; //did we get a good demod? | |
754 | ||
755 | uint8_t mask[] = {0,0,0,0,0,0,0,1}; | |
756 | for( idx=0; idx < (size - 96); idx++) { | |
757 | if ( memcmp(dest + idx, mask, sizeof(mask))==0) { | |
758 | // frame marker found | |
ec75f5c1 | 759 | //return ID start index |
760 | if (idx2 == 0) idx2=idx; | |
761 | else if(idx-idx2==96) return idx2; | |
762 | else return -5; | |
763 | ||
764 | // should always get 96 bits if it is awid | |
1e090a61 | 765 | } |
766 | } | |
767 | //never found mask | |
768 | return -4; | |
769 | } | |
770 | ||
771 | // by marshmellow | |
772 | // FSK Demod then try to locate an Farpointe Data (pyramid) ID | |
773 | int PyramiddemodFSK(uint8_t *dest, size_t size) | |
774 | { | |
775 | static const uint8_t THRESHOLD = 123; | |
ec75f5c1 | 776 | uint32_t idx=0, idx2=0; |
777 | // size_t size2 = size; | |
1e090a61 | 778 | //make sure buffer has data |
779 | if (size < 128*50) return -5; | |
780 | //test samples are not just noise | |
781 | uint8_t justNoise = 1; | |
782 | for(idx=0; idx < size && justNoise ;idx++){ | |
783 | justNoise = dest[idx] < THRESHOLD; | |
784 | } | |
785 | if(justNoise) return -1; | |
786 | ||
787 | // FSK demodulator | |
788 | size = fskdemod(dest, size, 50, 1, 10, 8); // RF/64 and invert | |
789 | if (size < 128) return -2; //did we get a good demod? | |
790 | ||
791 | uint8_t mask[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; | |
792 | for( idx=0; idx < (size - 128); idx++) { | |
793 | if ( memcmp(dest + idx, mask, sizeof(mask))==0) { | |
794 | // frame marker found | |
ec75f5c1 | 795 | if (idx2==0) idx2=idx; |
796 | else if (idx-idx2==128) return idx2; | |
797 | else return -3; | |
1e090a61 | 798 | } |
799 | } | |
800 | //never found mask | |
801 | return -4; | |
802 | } | |
803 | ||
eb191de6 | 804 | // by marshmellow |
805 | // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping) | |
806 | // maybe somehow adjust peak trimming value based on samples to fix? | |
f822a063 | 807 | int DetectASKClock(uint8_t dest[], size_t size, int clock) |
eb191de6 | 808 | { |
ec75f5c1 | 809 | int i=0; |
810 | int clk[]={8,16,32,40,50,64,100,128,256}; | |
811 | int loopCnt = 256; //don't need to loop through entire array... | |
812 | if (size<loopCnt) loopCnt = size; | |
813 | ||
814 | //if we already have a valid clock quit | |
815 | ||
816 | for (;i<8;++i) | |
817 | if (clk[i] == clock) return clock; | |
818 | ||
819 | //get high and low peak | |
820 | int peak, low; | |
821 | getHiLo(dest, loopCnt, &peak, &low, 75, 75); | |
822 | ||
823 | int ii; | |
824 | int clkCnt; | |
825 | int tol = 0; | |
826 | int bestErr[]={1000,1000,1000,1000,1000,1000,1000,1000,1000}; | |
827 | int errCnt=0; | |
828 | //test each valid clock from smallest to greatest to see which lines up | |
829 | for(clkCnt=0; clkCnt < 8; ++clkCnt){ | |
830 | if (clk[clkCnt] == 32){ | |
831 | tol=1; | |
832 | }else{ | |
833 | tol=0; | |
834 | } | |
835 | bestErr[clkCnt]=1000; | |
836 | //try lining up the peaks by moving starting point (try first 256) | |
837 | for (ii=0; ii < loopCnt; ++ii){ | |
838 | if ((dest[ii] >= peak) || (dest[ii] <= low)){ | |
839 | errCnt=0; | |
840 | // now that we have the first one lined up test rest of wave array | |
841 | for (i=0; i<((int)((size-ii-tol)/clk[clkCnt])-1); ++i){ | |
842 | if (dest[ii+(i*clk[clkCnt])]>=peak || dest[ii+(i*clk[clkCnt])]<=low){ | |
843 | }else if(dest[ii+(i*clk[clkCnt])-tol]>=peak || dest[ii+(i*clk[clkCnt])-tol]<=low){ | |
844 | }else if(dest[ii+(i*clk[clkCnt])+tol]>=peak || dest[ii+(i*clk[clkCnt])+tol]<=low){ | |
845 | }else{ //error no peak detected | |
846 | errCnt++; | |
847 | } | |
848 | } | |
849 | //if we found no errors then we can stop here | |
850 | // this is correct one - return this clock | |
851 | //PrintAndLog("DEBUG: clk %d, err %d, ii %d, i %d",clk[clkCnt],errCnt,ii,i); | |
852 | if(errCnt==0 && clkCnt<6) return clk[clkCnt]; | |
853 | //if we found errors see if it is lowest so far and save it as best run | |
854 | if(errCnt<bestErr[clkCnt]) bestErr[clkCnt]=errCnt; | |
855 | } | |
856 | } | |
857 | } | |
858 | uint8_t iii=0; | |
859 | uint8_t best=0; | |
860 | for (iii=0; iii<8; ++iii){ | |
861 | if (bestErr[iii]<bestErr[best]){ | |
862 | if (bestErr[iii]==0) bestErr[iii]=1; | |
863 | // current best bit to error ratio vs new bit to error ratio | |
864 | if (((size/clk[best])/bestErr[best] < (size/clk[iii])/bestErr[iii]) ){ | |
865 | best = iii; | |
866 | } | |
867 | } | |
868 | } | |
869 | return clk[best]; | |
eb191de6 | 870 | } |
ba1a299c | 871 | |
872 | //by marshmellow | |
873 | //detect psk clock by reading #peaks vs no peaks(or errors) | |
874 | int DetectpskNRZClock(uint8_t dest[], size_t size, int clock) | |
875 | { | |
876 | int i=0; | |
ba1a299c | 877 | int clk[]={16,32,40,50,64,100,128,256}; |
878 | int loopCnt = 2048; //don't need to loop through entire array... | |
879 | if (size<loopCnt) loopCnt = size; | |
880 | ||
881 | //if we already have a valid clock quit | |
ec75f5c1 | 882 | for (; i < 7; ++i) |
c12512e9 | 883 | if (clk[i] == clock) return clock; |
ba1a299c | 884 | |
885 | //get high and low peak | |
1e090a61 | 886 | int peak, low; |
887 | getHiLo(dest, loopCnt, &peak, &low, 75, 75); | |
888 | ||
ba1a299c | 889 | //PrintAndLog("DEBUG: peak: %d, low: %d",peak,low); |
890 | int ii; | |
891 | uint8_t clkCnt; | |
892 | uint8_t tol = 0; | |
893 | int peakcnt=0; | |
894 | int errCnt=0; | |
ec75f5c1 | 895 | int bestErr[]={1000,1000,1000,1000,1000,1000,1000,1000}; |
896 | int peaksdet[]={0,0,0,0,0,0,0,0}; | |
ba1a299c | 897 | //test each valid clock from smallest to greatest to see which lines up |
ec75f5c1 | 898 | for(clkCnt=0; clkCnt < 7; ++clkCnt){ |
899 | if (clk[clkCnt] <= 32){ | |
ac3ba7ee | 900 | tol=1; |
ba1a299c | 901 | }else{ |
902 | tol=0; | |
903 | } | |
904 | //try lining up the peaks by moving starting point (try first 256) | |
c12512e9 | 905 | for (ii=0; ii< loopCnt; ++ii){ |
906 | if ((dest[ii] >= peak) || (dest[ii] <= low)){ | |
ba1a299c | 907 | errCnt=0; |
908 | peakcnt=0; | |
909 | // now that we have the first one lined up test rest of wave array | |
ec75f5c1 | 910 | for (i=0; i < ((int)((size-ii-tol)/clk[clkCnt])-1); ++i){ |
ba1a299c | 911 | if (dest[ii+(i*clk[clkCnt])]>=peak || dest[ii+(i*clk[clkCnt])]<=low){ |
912 | peakcnt++; | |
913 | }else if(dest[ii+(i*clk[clkCnt])-tol]>=peak || dest[ii+(i*clk[clkCnt])-tol]<=low){ | |
914 | peakcnt++; | |
915 | }else if(dest[ii+(i*clk[clkCnt])+tol]>=peak || dest[ii+(i*clk[clkCnt])+tol]<=low){ | |
916 | peakcnt++; | |
917 | }else{ //error no peak detected | |
918 | errCnt++; | |
919 | } | |
920 | } | |
921 | if(peakcnt>peaksdet[clkCnt]) { | |
922 | peaksdet[clkCnt]=peakcnt; | |
923 | bestErr[clkCnt]=errCnt; | |
924 | } | |
925 | } | |
926 | } | |
927 | } | |
928 | int iii=0; | |
929 | int best=0; | |
930 | //int ratio2; //debug | |
931 | int ratio; | |
932 | //int bits; | |
c12512e9 | 933 | for (iii=0; iii < 7; ++iii){ |
ba1a299c | 934 | ratio=1000; |
935 | //ratio2=1000; //debug | |
936 | //bits=size/clk[iii]; //debug | |
c12512e9 | 937 | if (peaksdet[iii] > 0){ |
ba1a299c | 938 | ratio=bestErr[iii]/peaksdet[iii]; |
c12512e9 | 939 | if (((bestErr[best]/peaksdet[best]) > (ratio)+1)){ |
ba1a299c | 940 | best = iii; |
941 | } | |
942 | //ratio2=bits/peaksdet[iii]; //debug | |
943 | } | |
944 | //PrintAndLog("DEBUG: Clk: %d, peaks: %d, errs: %d, bestClk: %d, ratio: %d, bits: %d, peakbitr: %d",clk[iii],peaksdet[iii],bestErr[iii],clk[best],ratio, bits,ratio2); | |
945 | } | |
946 | return clk[best]; | |
947 | } | |
948 | ||
04d2721b | 949 | // by marshmellow (attempt to get rid of high immediately after a low) |
1e090a61 | 950 | void pskCleanWave(uint8_t *BitStream, size_t size) |
ba1a299c | 951 | { |
952 | int i; | |
ba1a299c | 953 | int gap = 4; |
1e090a61 | 954 | int newLow=0; |
ba1a299c | 955 | int newHigh=0; |
1e090a61 | 956 | int high, low; |
957 | getHiLo(BitStream, size, &high, &low, 80, 90); | |
958 | ||
959 | for (i=0; i < size; ++i){ | |
c12512e9 | 960 | if (newLow == 1){ |
1e090a61 | 961 | if (BitStream[i]>low){ |
962 | BitStream[i]=low+8; | |
963 | gap--; | |
964 | } | |
c12512e9 | 965 | if (gap == 0){ |
ba1a299c | 966 | newLow=0; |
967 | gap=4; | |
968 | } | |
c12512e9 | 969 | }else if (newHigh == 1){ |
1e090a61 | 970 | if (BitStream[i]<high){ |
971 | BitStream[i]=high-8; | |
972 | gap--; | |
973 | } | |
c12512e9 | 974 | if (gap == 0){ |
ba1a299c | 975 | newHigh=0; |
976 | gap=4; | |
977 | } | |
978 | } | |
1e090a61 | 979 | if (BitStream[i] <= low) newLow=1; |
980 | if (BitStream[i] >= high) newHigh=1; | |
ba1a299c | 981 | } |
982 | return; | |
983 | } | |
984 | ||
04d2721b | 985 | // by marshmellow |
986 | // convert psk1 demod to psk2 demod | |
987 | // only transition waves are 1s | |
988 | void psk1TOpsk2(uint8_t *BitStream, size_t size) | |
989 | { | |
990 | size_t i=1; | |
991 | uint8_t lastBit=BitStream[0]; | |
992 | for (; i<size; i++){ | |
993 | if (lastBit!=BitStream[i]){ | |
994 | lastBit=BitStream[i]; | |
995 | BitStream[i]=1; | |
996 | } else { | |
997 | BitStream[i]=0; | |
998 | } | |
999 | } | |
1000 | return; | |
1001 | } | |
ba1a299c | 1002 | |
04d2721b | 1003 | // redesigned by marshmellow adjusted from existing decode functions |
1004 | // indala id decoding - only tested on 26 bit tags, but attempted to make it work for more | |
ba1a299c | 1005 | int indala26decode(uint8_t *bitStream, size_t *size, uint8_t *invert) |
1006 | { | |
1007 | //26 bit 40134 format (don't know other formats) | |
1008 | int i; | |
84871873 | 1009 | int long_wait=29;//29 leading zeros in format |
ba1a299c | 1010 | int start; |
1011 | int first = 0; | |
1012 | int first2 = 0; | |
1013 | int bitCnt = 0; | |
1014 | int ii; | |
1015 | // Finding the start of a UID | |
1016 | for (start = 0; start <= *size - 250; start++) { | |
1017 | first = bitStream[start]; | |
1018 | for (i = start; i < start + long_wait; i++) { | |
1019 | if (bitStream[i] != first) { | |
1020 | break; | |
1021 | } | |
1022 | } | |
1023 | if (i == (start + long_wait)) { | |
1024 | break; | |
1025 | } | |
1026 | } | |
1027 | if (start == *size - 250 + 1) { | |
1028 | // did not find start sequence | |
1029 | return -1; | |
1030 | } | |
ba1a299c | 1031 | // Inverting signal if needed |
1032 | if (first == 1) { | |
1033 | for (i = start; i < *size; i++) { | |
1034 | bitStream[i] = !bitStream[i]; | |
1035 | } | |
1036 | *invert = 1; | |
1037 | }else *invert=0; | |
1038 | ||
1039 | int iii; | |
84871873 | 1040 | //found start once now test length by finding next one |
ba1a299c | 1041 | for (ii=start+29; ii <= *size - 250; ii++) { |
1042 | first2 = bitStream[ii]; | |
1043 | for (iii = ii; iii < ii + long_wait; iii++) { | |
1044 | if (bitStream[iii] != first2) { | |
1045 | break; | |
1046 | } | |
1047 | } | |
1048 | if (iii == (ii + long_wait)) { | |
1049 | break; | |
1050 | } | |
1051 | } | |
1052 | if (ii== *size - 250 + 1){ | |
1053 | // did not find second start sequence | |
1054 | return -2; | |
1055 | } | |
1056 | bitCnt=ii-start; | |
1057 | ||
1058 | // Dumping UID | |
1059 | i = start; | |
1060 | for (ii = 0; ii < bitCnt; ii++) { | |
1061 | bitStream[ii] = bitStream[i++]; | |
1062 | } | |
1063 | *size=bitCnt; | |
1064 | return 1; | |
1065 | } | |
1066 | ||
04d2721b | 1067 | // by marshmellow - demodulate PSK1 wave or NRZ wave (both similar enough) |
1068 | // peaks invert bit (high=1 low=0) each clock cycle = 1 bit determined by last peak | |
ba1a299c | 1069 | int pskNRZrawDemod(uint8_t *dest, size_t *size, int *clk, int *invert) |
1070 | { | |
1071 | pskCleanWave(dest,*size); | |
1072 | int clk2 = DetectpskNRZClock(dest, *size, *clk); | |
1073 | *clk=clk2; | |
1074 | uint32_t i; | |
1e090a61 | 1075 | int high, low, ans; |
1076 | ans = getHiLo(dest, 1260, &high, &low, 75, 80); //25% fuzz on high 20% fuzz on low | |
1077 | if (ans<1) return -2; //just noise | |
ba1a299c | 1078 | uint32_t gLen = *size; |
ba1a299c | 1079 | //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low); |
1080 | int lastBit = 0; //set first clock check | |
1081 | uint32_t bitnum = 0; //output counter | |
1e090a61 | 1082 | 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 |
84871873 | 1083 | if (*clk==32) tol = 2; //clock tolerance may not be needed anymore currently set to + or - 1 but could be increased for poor waves or removed entirely |
ba1a299c | 1084 | uint32_t iii = 0; |
1085 | uint8_t errCnt =0; | |
1086 | uint32_t bestStart = *size; | |
1087 | uint32_t maxErr = (*size/1000); | |
1088 | uint32_t bestErrCnt = maxErr; | |
ba1a299c | 1089 | uint8_t curBit=0; |
1090 | uint8_t bitHigh=0; | |
1091 | uint8_t ignorewin=*clk/8; | |
1092 | //PrintAndLog("DEBUG - lastbit - %d",lastBit); | |
1093 | //loop to find first wave that works - align to clock | |
1094 | for (iii=0; iii < gLen; ++iii){ | |
c12512e9 | 1095 | if ((dest[iii]>=high) || (dest[iii]<=low)){ |
ba1a299c | 1096 | lastBit=iii-*clk; |
1097 | //loop through to see if this start location works | |
1098 | for (i = iii; i < *size; ++i) { | |
1099 | //if we found a high bar and we are at a clock bit | |
1100 | if ((dest[i]>=high ) && (i>=lastBit+*clk-tol && i<=lastBit+*clk+tol)){ | |
1101 | bitHigh=1; | |
1102 | lastBit+=*clk; | |
1103 | ignorewin=*clk/8; | |
1104 | bitnum++; | |
1105 | //else if low bar found and we are at a clock point | |
1106 | }else if ((dest[i]<=low ) && (i>=lastBit+*clk-tol && i<=lastBit+*clk+tol)){ | |
1107 | bitHigh=1; | |
1108 | lastBit+=*clk; | |
1109 | ignorewin=*clk/8; | |
1110 | bitnum++; | |
1111 | //else if no bars found | |
c12512e9 | 1112 | }else if(dest[i] < high && dest[i] > low) { |
ba1a299c | 1113 | if (ignorewin==0){ |
1114 | bitHigh=0; | |
1115 | }else ignorewin--; | |
1116 | //if we are past a clock point | |
c12512e9 | 1117 | if (i >= lastBit+*clk+tol){ //clock val |
ba1a299c | 1118 | lastBit+=*clk; |
1119 | bitnum++; | |
1120 | } | |
1121 | //else if bar found but we are not at a clock bit and we did not just have a clock bit | |
1122 | }else if ((dest[i]>=high || dest[i]<=low) && (i<lastBit+*clk-tol || i>lastBit+*clk+tol) && (bitHigh==0)){ | |
1123 | //error bar found no clock... | |
1124 | errCnt++; | |
1125 | } | |
1126 | if (bitnum>=1000) break; | |
1127 | } | |
1128 | //we got more than 64 good bits and not all errors | |
c12512e9 | 1129 | if ((bitnum > (64+errCnt)) && (errCnt < (maxErr))) { |
ba1a299c | 1130 | //possible good read |
c12512e9 | 1131 | if (errCnt == 0){ |
ba1a299c | 1132 | bestStart = iii; |
c12512e9 | 1133 | bestErrCnt = errCnt; |
ba1a299c | 1134 | break; //great read - finish |
1135 | } | |
c12512e9 | 1136 | if (errCnt < bestErrCnt){ //set this as new best run |
1137 | bestErrCnt = errCnt; | |
ba1a299c | 1138 | bestStart = iii; |
1139 | } | |
1140 | } | |
1141 | } | |
1142 | } | |
c12512e9 | 1143 | if (bestErrCnt < maxErr){ |
ba1a299c | 1144 | //best run is good enough set to best run and set overwrite BinStream |
1145 | iii=bestStart; | |
1146 | lastBit=bestStart-*clk; | |
1147 | bitnum=0; | |
1148 | for (i = iii; i < *size; ++i) { | |
1149 | //if we found a high bar and we are at a clock bit | |
c12512e9 | 1150 | if ((dest[i] >= high ) && (i>=lastBit+*clk-tol && i<=lastBit+*clk+tol)){ |
ba1a299c | 1151 | bitHigh=1; |
1152 | lastBit+=*clk; | |
1153 | curBit=1-*invert; | |
1154 | dest[bitnum]=curBit; | |
1155 | ignorewin=*clk/8; | |
1156 | bitnum++; | |
1157 | //else if low bar found and we are at a clock point | |
1158 | }else if ((dest[i]<=low ) && (i>=lastBit+*clk-tol && i<=lastBit+*clk+tol)){ | |
1159 | bitHigh=1; | |
1160 | lastBit+=*clk; | |
1161 | curBit=*invert; | |
1162 | dest[bitnum]=curBit; | |
1163 | ignorewin=*clk/8; | |
1164 | bitnum++; | |
1165 | //else if no bars found | |
1166 | }else if(dest[i]<high && dest[i]>low) { | |
1167 | if (ignorewin==0){ | |
1168 | bitHigh=0; | |
1169 | }else ignorewin--; | |
1170 | //if we are past a clock point | |
1171 | if (i>=lastBit+*clk+tol){ //clock val | |
1172 | lastBit+=*clk; | |
1173 | dest[bitnum]=curBit; | |
1174 | bitnum++; | |
1175 | } | |
1176 | //else if bar found but we are not at a clock bit and we did not just have a clock bit | |
1177 | }else if ((dest[i]>=high || dest[i]<=low) && ((i<lastBit+*clk-tol) || (i>lastBit+*clk+tol)) && (bitHigh==0)){ | |
1178 | //error bar found no clock... | |
1179 | bitHigh=1; | |
1180 | dest[bitnum]=77; | |
1181 | bitnum++; | |
1182 | errCnt++; | |
1183 | } | |
1184 | if (bitnum >=1000) break; | |
1185 | } | |
1186 | *size=bitnum; | |
1187 | } else{ | |
1188 | *size=bitnum; | |
1189 | *clk=bestStart; | |
1190 | return -1; | |
1191 | } | |
1192 | ||
1193 | if (bitnum>16){ | |
1194 | *size=bitnum; | |
1195 | } else return -1; | |
1196 | return errCnt; | |
1197 | } | |
1198 | ||
1e090a61 | 1199 | //by marshmellow |
03e6bb4a | 1200 | //detects the bit clock for FSK given the high and low Field Clocks |
1201 | uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow) | |
1e090a61 | 1202 | { |
03e6bb4a | 1203 | uint8_t clk[] = {8,16,32,40,50,64,100,128,0}; |
1204 | uint16_t rfLens[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
1205 | uint8_t rfCnts[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
1e090a61 | 1206 | uint8_t rfLensFnd = 0; |
1e090a61 | 1207 | uint8_t lastFCcnt=0; |
1e090a61 | 1208 | uint32_t fcCounter = 0; |
03e6bb4a | 1209 | uint16_t rfCounter = 0; |
1e090a61 | 1210 | uint8_t firstBitFnd = 0; |
03e6bb4a | 1211 | size_t i; |
1212 | ||
1213 | uint8_t fcTol = (uint8_t)(0.5+(float)(fcHigh-fcLow)/2); | |
1214 | rfLensFnd=0; | |
1215 | fcCounter=0; | |
1216 | rfCounter=0; | |
1217 | firstBitFnd=0; | |
1218 | //PrintAndLog("DEBUG: fcTol: %d",fcTol); | |
1e090a61 | 1219 | // prime i to first up transition |
03e6bb4a | 1220 | for (i = 1; i < size-1; i++) |
1221 | if (BitStream[i] > BitStream[i-1] && BitStream[i]>=BitStream[i+1]) | |
1e090a61 | 1222 | break; |
1223 | ||
03e6bb4a | 1224 | for (; i < size-1; i++){ |
1225 | if (BitStream[i] > BitStream[i-1] && BitStream[i]>=BitStream[i+1]){ | |
1226 | // new peak | |
1e090a61 | 1227 | fcCounter++; |
1228 | rfCounter++; | |
03e6bb4a | 1229 | // if we got less than the small fc + tolerance then set it to the small fc |
1230 | if (fcCounter < fcLow+fcTol) | |
1231 | fcCounter = fcLow; | |
1232 | else //set it to the large fc | |
1233 | fcCounter = fcHigh; | |
1e090a61 | 1234 | |
03e6bb4a | 1235 | //look for bit clock (rf/xx) |
1236 | if ((fcCounter<lastFCcnt || fcCounter>lastFCcnt)){ | |
1237 | //not the same size as the last wave - start of new bit sequence | |
1238 | ||
1239 | if (firstBitFnd>1){ //skip first wave change - probably not a complete bit | |
1240 | for (int ii=0; ii<15; ii++){ | |
1241 | if (rfLens[ii]==rfCounter){ | |
1242 | rfCnts[ii]++; | |
1243 | rfCounter=0; | |
1244 | break; | |
1e090a61 | 1245 | } |
1e090a61 | 1246 | } |
03e6bb4a | 1247 | if (rfCounter>0 && rfLensFnd<15){ |
1248 | //PrintAndLog("DEBUG: rfCntr %d, fcCntr %d",rfCounter,fcCounter); | |
1249 | rfCnts[rfLensFnd]++; | |
1250 | rfLens[rfLensFnd++]=rfCounter; | |
1e090a61 | 1251 | } |
03e6bb4a | 1252 | } else { |
1253 | firstBitFnd++; | |
1e090a61 | 1254 | } |
03e6bb4a | 1255 | rfCounter=0; |
1256 | lastFCcnt=fcCounter; | |
1e090a61 | 1257 | } |
1e090a61 | 1258 | fcCounter=0; |
1259 | } else { | |
1260 | // count sample | |
1261 | fcCounter++; | |
1262 | rfCounter++; | |
1263 | } | |
1264 | } | |
03e6bb4a | 1265 | uint8_t rfHighest=15, rfHighest2=15, rfHighest3=15; |
1e090a61 | 1266 | |
03e6bb4a | 1267 | for (i=0; i<15; i++){ |
1268 | //PrintAndLog("DEBUG: RF %d, cnts %d",rfLens[i], rfCnts[i]); | |
1e090a61 | 1269 | //get highest 2 RF values (might need to get more values to compare or compare all?) |
03e6bb4a | 1270 | if (rfCnts[i]>rfCnts[rfHighest]){ |
1e090a61 | 1271 | rfHighest3=rfHighest2; |
1272 | rfHighest2=rfHighest; | |
1273 | rfHighest=i; | |
03e6bb4a | 1274 | } else if(rfCnts[i]>rfCnts[rfHighest2]){ |
1e090a61 | 1275 | rfHighest3=rfHighest2; |
1276 | rfHighest2=i; | |
03e6bb4a | 1277 | } else if(rfCnts[i]>rfCnts[rfHighest3]){ |
1e090a61 | 1278 | rfHighest3=i; |
1279 | } | |
03e6bb4a | 1280 | } |
1281 | // set allowed clock remainder tolerance to be 1 large field clock length+1 | |
1282 | // we could have mistakenly made a 9 a 10 instead of an 8 or visa versa so rfLens could be 1 FC off | |
1283 | uint8_t tol1 = fcHigh+1; | |
1e090a61 | 1284 | |
03e6bb4a | 1285 | //PrintAndLog("DEBUG: hightest: 1 %d, 2 %d, 3 %d",rfLens[rfHighest],rfLens[rfHighest2],rfLens[rfHighest3]); |
1286 | ||
1e090a61 | 1287 | // loop to find the highest clock that has a remainder less than the tolerance |
03e6bb4a | 1288 | // compare samples counted divided by |
1e090a61 | 1289 | int ii=7; |
1290 | for (; ii>=0; ii--){ | |
1291 | if (rfLens[rfHighest] % clk[ii] < tol1 || rfLens[rfHighest] % clk[ii] > clk[ii]-tol1){ | |
1292 | if (rfLens[rfHighest2] % clk[ii] < tol1 || rfLens[rfHighest2] % clk[ii] > clk[ii]-tol1){ | |
1293 | if (rfLens[rfHighest3] % clk[ii] < tol1 || rfLens[rfHighest3] % clk[ii] > clk[ii]-tol1){ | |
1294 | break; | |
1295 | } | |
1296 | } | |
1297 | } | |
1298 | } | |
1299 | ||
03e6bb4a | 1300 | if (ii<0) return 0; // oops we went too far |
1e090a61 | 1301 | |
03e6bb4a | 1302 | return clk[ii]; |
1303 | } | |
1e090a61 | 1304 | |
03e6bb4a | 1305 | //by marshmellow |
1306 | //countFC is to detect the field clock lengths. | |
1307 | //counts and returns the 2 most common wave lengths | |
1308 | uint16_t countFC(uint8_t *BitStream, size_t size) | |
1309 | { | |
1310 | uint8_t fcLens[] = {0,0,0,0,0,0,0,0,0,0}; | |
1311 | uint16_t fcCnts[] = {0,0,0,0,0,0,0,0,0,0}; | |
1312 | uint8_t fcLensFnd = 0; | |
1313 | uint8_t lastFCcnt=0; | |
1314 | uint32_t fcCounter = 0; | |
1315 | size_t i; | |
1316 | ||
1317 | // prime i to first up transition | |
1318 | for (i = 1; i < size-1; i++) | |
1319 | if (BitStream[i] > BitStream[i-1] && BitStream[i] >= BitStream[i+1]) | |
1320 | break; | |
1e090a61 | 1321 | |
03e6bb4a | 1322 | for (; i < size-1; i++){ |
1323 | if (BitStream[i] > BitStream[i-1] && BitStream[i] >= BitStream[i+1]){ | |
1324 | // new up transition | |
1325 | fcCounter++; | |
1326 | ||
1327 | //if we had 5 and now have 9 then go back to 8 (for when we get a fc 9 instead of an 8) | |
1328 | if (lastFCcnt==5 && fcCounter==9) fcCounter--; | |
1329 | //if odd and not rc/5 add one (for when we get a fc 9 instead of 10) | |
1330 | if ((fcCounter==9 && fcCounter & 1) || fcCounter==4) fcCounter++; | |
1331 | ||
1332 | // save last field clock count (fc/xx) | |
1333 | // find which fcLens to save it to: | |
1334 | for (int ii=0; ii<10; ii++){ | |
1335 | if (fcLens[ii]==fcCounter){ | |
1336 | fcCnts[ii]++; | |
1337 | fcCounter=0; | |
1338 | break; | |
1339 | } | |
1340 | } | |
1341 | if (fcCounter>0 && fcLensFnd<10){ | |
1342 | //add new fc length | |
1343 | fcCnts[fcLensFnd]++; | |
1344 | fcLens[fcLensFnd++]=fcCounter; | |
1345 | } | |
1346 | fcCounter=0; | |
1347 | } else { | |
1348 | // count sample | |
1349 | fcCounter++; | |
1350 | } | |
1351 | } | |
1352 | ||
1353 | uint8_t best1=9, best2=9, best3=9; | |
1354 | uint16_t maxCnt1=0; | |
1355 | // go through fclens and find which ones are bigest 2 | |
1356 | for (i=0; i<10; i++){ | |
1357 | // PrintAndLog("DEBUG: FC %d, Cnt %d, Errs %d",fcLens[i],fcCnts[i],errCnt); | |
1358 | // get the 3 best FC values | |
1359 | if (fcCnts[i]>maxCnt1) { | |
1360 | best3=best2; | |
1361 | best2=best1; | |
1362 | maxCnt1=fcCnts[i]; | |
1363 | best1=i; | |
1364 | } else if(fcCnts[i]>fcCnts[best2]){ | |
1365 | best3=best2; | |
1366 | best2=i; | |
1367 | } else if(fcCnts[i]>fcCnts[best3]){ | |
1368 | best3=i; | |
1369 | } | |
1370 | } | |
1371 | uint8_t fcH=0, fcL=0; | |
1e090a61 | 1372 | if (fcLens[best1]>fcLens[best2]){ |
03e6bb4a | 1373 | fcH=fcLens[best1]; |
1374 | fcL=fcLens[best2]; | |
1375 | } else{ | |
1376 | fcH=fcLens[best2]; | |
1377 | fcL=fcLens[best1]; | |
1e090a61 | 1378 | } |
03e6bb4a | 1379 | |
1380 | // TODO: take top 3 answers and compare to known Field clocks to get top 2 | |
1e090a61 | 1381 | |
03e6bb4a | 1382 | uint16_t fcs = (((uint16_t)fcH)<<8) | fcL; |
1383 | // PrintAndLog("DEBUG: Best %d best2 %d best3 %d",fcLens[best1],fcLens[best2],fcLens[best3]); | |
1384 | ||
1e090a61 | 1385 | return fcs; |
1386 | } |