]> cvs.zerfleddert.de Git - proxmark3-svn/blob - common/lfdemod.c
Merge branch 'master' of https://github.com/Proxmark/proxmark3
[proxmark3-svn] / common / lfdemod.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2014
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 //-----------------------------------------------------------------------------
8 // Low frequency commands
9 //-----------------------------------------------------------------------------
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include "lfdemod.h"
14
15 //by marshmellow
16 //takes 1s and 0s and searches for EM410x format - output EM ID
17 uint64_t Em410xDecode(uint8_t *BitStream, size_t size)
18 {
19 //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
20 // otherwise could be a void with no arguments
21 //set defaults
22 int high=0, low=128;
23 uint64_t lo=0;
24
25 uint32_t i = 0;
26 uint32_t initLoopMax = 65;
27 if (initLoopMax>size) initLoopMax=size;
28
29 for (;i < initLoopMax; ++i) //65 samples should be plenty to find high and low values
30 {
31 if (BitStream[i] > high)
32 high = BitStream[i];
33 else if (BitStream[i] < low)
34 low = BitStream[i];
35 }
36 if (((high !=1)||(low !=0))){ //allow only 1s and 0s
37 // PrintAndLog("no data found");
38 return 0;
39 }
40 uint8_t parityTest=0;
41 // 111111111 bit pattern represent start of frame
42 uint8_t frame_marker_mask[] = {1,1,1,1,1,1,1,1,1};
43 uint32_t idx = 0;
44 uint32_t ii=0;
45 uint8_t resetCnt = 0;
46 while( (idx + 64) < size) {
47 restart:
48 // search for a start of frame marker
49 if ( memcmp(BitStream+idx, frame_marker_mask, sizeof(frame_marker_mask)) == 0)
50 { // frame marker found
51 idx+=9;
52 for (i=0; i<10;i++){
53 for(ii=0; ii<5; ++ii){
54 parityTest += BitStream[(i*5)+ii+idx];
55 }
56 if (parityTest== ((parityTest>>1)<<1)){
57 parityTest=0;
58 for (ii=0; ii<4;++ii){
59 lo=(lo<<1LL)|(BitStream[(i*5)+ii+idx]);
60 }
61 //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);
62 }else {//parity failed
63 //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]);
64 parityTest=0;
65 idx-=8;
66 if (resetCnt>5)return 0;
67 resetCnt++;
68 goto restart;//continue;
69 }
70 }
71 //skip last 5 bit parity test for simplicity.
72 return lo;
73 }else{
74 idx++;
75 }
76 }
77 return 0;
78 }
79
80 //by marshmellow
81 //takes 2 arguments - clock and invert both as integers
82 //attempts to demodulate ask while decoding manchester
83 //prints binary found and saves in graphbuffer for further commands
84 int askmandemod(uint8_t *BinStream, size_t *size, int *clk, int *invert)
85 {
86 int i;
87 int high = 0, low = 128;
88 *clk=DetectASKClock(BinStream, *size, *clk); //clock default
89
90 if (*clk<8) *clk =64;
91 if (*clk<32) *clk=32;
92 if (*invert != 0 && *invert != 1) *invert=0;
93 uint32_t initLoopMax = 200;
94 if (initLoopMax > *size) initLoopMax=*size;
95 // Detect high and lows
96 for (i = 0; i < initLoopMax; ++i) //200 samples should be enough to find high and low values
97 {
98 if (BinStream[i] > high)
99 high = BinStream[i];
100 else if (BinStream[i] < low)
101 low = BinStream[i];
102 }
103 if ((high < 158) ){ //throw away static
104 //PrintAndLog("no data found");
105 return -2;
106 }
107 //25% fuzz in case highs and lows aren't clipped [marshmellow]
108 high=(int)(((high-128)*.75)+128);
109 low= (int)(((low-128)*.75)+128);
110
111 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
112 int lastBit = 0; //set first clock check
113 uint32_t bitnum = 0; //output counter
114 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
115 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
116 int iii = 0;
117 uint32_t gLen = *size;
118 if (gLen > 3000) gLen=3000;
119 uint8_t errCnt =0;
120 uint32_t bestStart = *size;
121 uint32_t bestErrCnt = (*size/1000);
122 uint32_t maxErr = (*size/1000);
123 //PrintAndLog("DEBUG - lastbit - %d",lastBit);
124 //loop to find first wave that works
125 for (iii=0; iii < gLen; ++iii){
126 if ((BinStream[iii] >= high) || (BinStream[iii] <= low)){
127 lastBit=iii-*clk;
128 errCnt=0;
129 //loop through to see if this start location works
130 for (i = iii; i < *size; ++i) {
131 if ((BinStream[i] >= high) && ((i-lastBit) > (*clk-tol))){
132 lastBit+=*clk;
133 } else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){
134 //low found and we are expecting a bar
135 lastBit+=*clk;
136 } else {
137 //mid value found or no bar supposed to be here
138 if ((i-lastBit)>(*clk+tol)){
139 //should have hit a high or low based on clock!!
140
141 //debug
142 //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);
143
144 errCnt++;
145 lastBit+=*clk;//skip over until hit too many errors
146 if (errCnt>(maxErr)) break; //allow 1 error for every 1000 samples else start over
147 }
148 }
149 if ((i-iii) >(400 * *clk)) break; //got plenty of bits
150 }
151 //we got more than 64 good bits and not all errors
152 if ((((i-iii)/ *clk) > (64+errCnt)) && (errCnt<maxErr)) {
153 //possible good read
154 if (errCnt==0){
155 bestStart=iii;
156 bestErrCnt=errCnt;
157 break; //great read - finish
158 }
159 if (errCnt<bestErrCnt){ //set this as new best run
160 bestErrCnt=errCnt;
161 bestStart = iii;
162 }
163 }
164 }
165 }
166 if (bestErrCnt<maxErr){
167 //best run is good enough set to best run and set overwrite BinStream
168 iii=bestStart;
169 lastBit = bestStart - *clk;
170 bitnum=0;
171 for (i = iii; i < *size; ++i) {
172 if ((BinStream[i] >= high) && ((i-lastBit) > (*clk-tol))){
173 lastBit += *clk;
174 BinStream[bitnum] = *invert;
175 bitnum++;
176 } else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){
177 //low found and we are expecting a bar
178 lastBit+=*clk;
179 BinStream[bitnum] = 1-*invert;
180 bitnum++;
181 } else {
182 //mid value found or no bar supposed to be here
183 if ((i-lastBit)>(*clk+tol)){
184 //should have hit a high or low based on clock!!
185
186 //debug
187 //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);
188 if (bitnum > 0){
189 BinStream[bitnum]=77;
190 bitnum++;
191 }
192
193 lastBit+=*clk;//skip over error
194 }
195 }
196 if (bitnum >=400) break;
197 }
198 *size=bitnum;
199 } else{
200 *invert=bestStart;
201 *clk=iii;
202 return -1;
203 }
204 return bestErrCnt;
205 }
206
207 //by marshmellow
208 //take 10 and 01 and manchester decode
209 //run through 2 times and take least errCnt
210 int manrawdecode(uint8_t * BitStream, size_t *size)
211 {
212 int bitnum=0;
213 int errCnt =0;
214 int i=1;
215 int bestErr = 1000;
216 int bestRun = 0;
217 int ii=1;
218 for (ii=1;ii<3;++ii){
219 i=1;
220 for (i=i+ii;i<*size-2;i+=2){
221 if(BitStream[i]==1 && (BitStream[i+1]==0)){
222 } else if((BitStream[i]==0)&& BitStream[i+1]==1){
223 } else {
224 errCnt++;
225 }
226 if(bitnum>300) break;
227 }
228 if (bestErr>errCnt){
229 bestErr=errCnt;
230 bestRun=ii;
231 }
232 errCnt=0;
233 }
234 errCnt=bestErr;
235 if (errCnt<20){
236 ii=bestRun;
237 i=1;
238 for (i=i+ii;i < *size-2;i+=2){
239 if(BitStream[i] == 1 && (BitStream[i+1] == 0)){
240 BitStream[bitnum++]=0;
241 } else if((BitStream[i] == 0) && BitStream[i+1] == 1){
242 BitStream[bitnum++]=1;
243 } else {
244 BitStream[bitnum++]=77;
245 //errCnt++;
246 }
247 if(bitnum>300) break;
248 }
249 *size=bitnum;
250 }
251 return errCnt;
252 }
253
254
255 //by marshmellow
256 //take 01 or 10 = 0 and 11 or 00 = 1
257 int BiphaseRawDecode(uint8_t *BitStream, size_t *size, int offset)
258 {
259 uint8_t bitnum=0;
260 uint32_t errCnt =0;
261 uint32_t i=1;
262 i=offset;
263 for (;i<*size-2;i+=2){
264 if((BitStream[i]==1 && BitStream[i+1]==0) || (BitStream[i]==0 && BitStream[i+1]==1)){
265 BitStream[bitnum++]=1;
266 } else if((BitStream[i]==0 && BitStream[i+1]==0) || (BitStream[i]==1 && BitStream[i+1]==1)){
267 BitStream[bitnum++]=0;
268 } else {
269 BitStream[bitnum++]=77;
270 errCnt++;
271 }
272 if(bitnum>250) break;
273 }
274 *size=bitnum;
275 return errCnt;
276 }
277
278 //by marshmellow
279 //takes 2 arguments - clock and invert both as integers
280 //attempts to demodulate ask only
281 //prints binary found and saves in graphbuffer for further commands
282 int askrawdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert)
283 {
284 uint32_t i;
285 // int invert=0; //invert default
286 int high = 0, low = 128;
287 *clk=DetectASKClock(BinStream, *size, *clk); //clock default
288 uint8_t BitStream[502] = {0};
289
290 if (*clk<8) *clk =64;
291 if (*clk<32) *clk=32;
292 if (*invert != 0 && *invert != 1) *invert =0;
293 uint32_t initLoopMax = 200;
294 if (initLoopMax > *size) initLoopMax=*size;
295 // Detect high and lows
296 for (i = 0; i < initLoopMax; ++i) //200 samples should be plenty to find high and low values
297 {
298 if (BinStream[i] > high)
299 high = BinStream[i];
300 else if (BinStream[i] < low)
301 low = BinStream[i];
302 }
303 if ((high < 158)){ //throw away static
304 // PrintAndLog("no data found");
305 return -2;
306 }
307 //25% fuzz in case highs and lows aren't clipped [marshmellow]
308 high=(int)(((high-128)*.75)+128);
309 low= (int)(((low-128)*.75)+128);
310
311 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
312 int lastBit = 0; //set first clock check
313 uint32_t bitnum = 0; //output counter
314 uint8_t tol = 0; //clock tolerance adjust - waves will be accepted as within the clock
315 // if they fall + or - this value + clock from last valid wave
316 if (*clk == 32) tol=1; //clock tolerance may not be needed anymore currently set to
317 // + or - 1 but could be increased for poor waves or removed entirely
318 uint32_t iii = 0;
319 uint32_t gLen = *size;
320 if (gLen > 500) gLen=500;
321 uint8_t errCnt =0;
322 uint32_t bestStart = *size;
323 uint32_t bestErrCnt = (*size/1000);
324 uint8_t midBit=0;
325 //PrintAndLog("DEBUG - lastbit - %d",lastBit);
326 //loop to find first wave that works
327 for (iii=0; iii < gLen; ++iii){
328 if ((BinStream[iii]>=high) || (BinStream[iii]<=low)){
329 lastBit=iii-*clk;
330 //loop through to see if this start location works
331 for (i = iii; i < *size; ++i) {
332 if ((BinStream[i] >= high) && ((i-lastBit)>(*clk-tol))){
333 lastBit+=*clk;
334 BitStream[bitnum] = *invert;
335 bitnum++;
336 midBit=0;
337 } else if ((BinStream[i] <= low) && ((i-lastBit)>(*clk-tol))){
338 //low found and we are expecting a bar
339 lastBit+=*clk;
340 BitStream[bitnum] = 1- *invert;
341 bitnum++;
342 midBit=0;
343 } else if ((BinStream[i]<=low) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
344 //mid bar?
345 midBit=1;
346 BitStream[bitnum]= 1- *invert;
347 bitnum++;
348 } else if ((BinStream[i]>=high) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
349 //mid bar?
350 midBit=1;
351 BitStream[bitnum]= *invert;
352 bitnum++;
353 } else if ((i-lastBit)>((*clk/2)+tol) && (midBit==0)){
354 //no mid bar found
355 midBit=1;
356 BitStream[bitnum]= BitStream[bitnum-1];
357 bitnum++;
358 } else {
359 //mid value found or no bar supposed to be here
360
361 if ((i-lastBit)>(*clk+tol)){
362 //should have hit a high or low based on clock!!
363 //debug
364 //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);
365 if (bitnum > 0){
366 BitStream[bitnum]=77;
367 bitnum++;
368 }
369
370 errCnt++;
371 lastBit+=*clk;//skip over until hit too many errors
372 if (errCnt > ((*size/1000))){ //allow 1 error for every 1000 samples else start over
373 errCnt=0;
374 bitnum=0;//start over
375 break;
376 }
377 }
378 }
379 if (bitnum>500) break;
380 }
381 //we got more than 64 good bits and not all errors
382 if ((bitnum > (64+errCnt)) && (errCnt<(*size/1000))) {
383 //possible good read
384 if (errCnt==0) break; //great read - finish
385 if (bestStart == iii) break; //if current run == bestErrCnt run (after exhausted testing) then finish
386 if (errCnt<bestErrCnt){ //set this as new best run
387 bestErrCnt=errCnt;
388 bestStart = iii;
389 }
390 }
391 }
392 if (iii>=gLen){ //exhausted test
393 //if there was a ok test go back to that one and re-run the best run (then dump after that run)
394 if (bestErrCnt < (*size/1000)) iii=bestStart;
395 }
396 }
397 if (bitnum>16){
398 for (i=0; i < bitnum; ++i){
399 BinStream[i]=BitStream[i];
400 }
401 *size=bitnum;
402 } else return -1;
403 return errCnt;
404 }
405 //translate wave to 11111100000 (1 for each short wave 0 for each long wave)
406 size_t fsk_wave_demod(uint8_t * dest, size_t size, uint8_t fchigh, uint8_t fclow)
407 {
408 uint32_t last_transition = 0;
409 uint32_t idx = 1;
410 uint32_t maxVal=0;
411 if (fchigh==0) fchigh=10;
412 if (fclow==0) fclow=8;
413 // we do care about the actual theshold value as sometimes near the center of the
414 // wave we may get static that changes direction of wave for one value
415 // if our value is too low it might affect the read. and if our tag or
416 // antenna is weak a setting too high might not see anything. [marshmellow]
417 if (size<100) return 0;
418 for(idx=1; idx<100; idx++){
419 if(maxVal<dest[idx]) maxVal = dest[idx];
420 }
421 // set close to the top of the wave threshold with 25% margin for error
422 // less likely to get a false transition up there.
423 // (but have to be careful not to go too high and miss some short waves)
424 uint8_t threshold_value = (uint8_t)(((maxVal-128)*.75)+128);
425
426 // sync to first lo-hi transition, and threshold
427
428 // Need to threshold first sample
429
430 if(dest[0] < threshold_value) dest[0] = 0;
431 else dest[0] = 1;
432
433 size_t numBits = 0;
434 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
435 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
436 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
437 for(idx = 1; idx < size; idx++) {
438 // threshold current value
439
440 if (dest[idx] < threshold_value) dest[idx] = 0;
441 else dest[idx] = 1;
442
443 // Check for 0->1 transition
444 if (dest[idx-1] < dest[idx]) { // 0 -> 1 transition
445 if ((idx-last_transition)<(fclow-2)){ //0-5 = garbage noise
446 //do nothing with extra garbage
447 } else if ((idx-last_transition) < (fchigh-1)) { //6-8 = 8 waves
448 dest[numBits]=1;
449 } else { //9+ = 10 waves
450 dest[numBits]=0;
451 }
452 last_transition = idx;
453 numBits++;
454 }
455 }
456 return numBits; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
457 }
458
459 uint32_t myround2(float f)
460 {
461 if (f >= 2000) return 2000;//something bad happened
462 return (uint32_t) (f + (float)0.5);
463 }
464
465 //translate 11111100000 to 10
466 size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t maxConsequtiveBits,
467 uint8_t invert, uint8_t fchigh, uint8_t fclow)
468 {
469 uint8_t lastval=dest[0];
470 uint32_t idx=0;
471 size_t numBits=0;
472 uint32_t n=1;
473
474 for( idx=1; idx < size; idx++) {
475
476 if (dest[idx]==lastval) {
477 n++;
478 continue;
479 }
480 //if lastval was 1, we have a 1->0 crossing
481 if ( dest[idx-1]==1 ) {
482 n=myround2((float)(n+1)/((float)(rfLen)/(float)fclow));
483 } else {// 0->1 crossing
484 n=myround2((float)(n+1)/((float)(rfLen-2)/(float)fchigh)); //-2 for fudge factor
485 }
486 if (n == 0) n = 1;
487
488 if(n < maxConsequtiveBits) //Consecutive
489 {
490 if(invert==0){ //invert bits
491 memset(dest+numBits, dest[idx-1] , n);
492 }else{
493 memset(dest+numBits, dest[idx-1]^1 , n);
494 }
495 numBits += n;
496 }
497 n=0;
498 lastval=dest[idx];
499 }//end for
500 return numBits;
501 }
502 //by marshmellow (from holiman's base)
503 // full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod)
504 int fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow)
505 {
506 // FSK demodulator
507 size = fsk_wave_demod(dest, size, fchigh, fclow);
508 size = aggregate_bits(dest, size, rfLen, 192, invert, fchigh, fclow);
509 return size;
510 }
511 // loop to get raw HID waveform then FSK demodulate the TAG ID from it
512 int HIDdemodFSK(uint8_t *dest, size_t size, uint32_t *hi2, uint32_t *hi, uint32_t *lo)
513 {
514
515 size_t idx=0; //, found=0; //size=0,
516 // FSK demodulator
517 size = fskdemod(dest, size,50,0,10,8);
518
519 // final loop, go over previously decoded manchester data and decode into usable tag ID
520 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
521 uint8_t frame_marker_mask[] = {1,1,1,0,0,0};
522 int numshifts = 0;
523 idx = 0;
524 //one scan
525 while( idx + sizeof(frame_marker_mask) < size) {
526 // search for a start of frame marker
527 if ( memcmp(dest+idx, frame_marker_mask, sizeof(frame_marker_mask)) == 0)
528 { // frame marker found
529 idx+=sizeof(frame_marker_mask);
530 while(dest[idx] != dest[idx+1] && idx < size-2)
531 {
532 // Keep going until next frame marker (or error)
533 // Shift in a bit. Start by shifting high registers
534 *hi2 = (*hi2<<1)|(*hi>>31);
535 *hi = (*hi<<1)|(*lo>>31);
536 //Then, shift in a 0 or one into low
537 if (dest[idx] && !dest[idx+1]) // 1 0
538 *lo=(*lo<<1)|0;
539 else // 0 1
540 *lo=(*lo<<1)|1;
541 numshifts++;
542 idx += 2;
543 }
544 // Hopefully, we read a tag and hit upon the next frame marker
545 if(idx + sizeof(frame_marker_mask) < size)
546 {
547 if ( memcmp(dest+idx, frame_marker_mask, sizeof(frame_marker_mask)) == 0)
548 {
549 //good return
550 return idx;
551 }
552 }
553 // reset
554 *hi2 = *hi = *lo = 0;
555 numshifts = 0;
556 }else {
557 idx++;
558 }
559 }
560 return -1;
561 }
562
563 uint32_t bytebits_to_byte(uint8_t* src, size_t numbits)
564 {
565 uint32_t num = 0;
566 for(int i = 0 ; i < numbits ; i++)
567 {
568 num = (num << 1) | (*src);
569 src++;
570 }
571 return num;
572 }
573
574 int IOdemodFSK(uint8_t *dest, size_t size)
575 {
576 static const uint8_t THRESHOLD = 140;
577 uint32_t idx=0;
578 //make sure buffer has data
579 if (size < 66) return -1;
580 //test samples are not just noise
581 uint8_t justNoise = 1;
582 for(idx=0;idx< size && justNoise ;idx++){
583 justNoise = dest[idx] < THRESHOLD;
584 }
585 if(justNoise) return 0;
586
587 // FSK demodulator
588 size = fskdemod(dest, size, 64, 1, 10, 8); // RF/64 and invert
589 if (size < 65) return -1; //did we get a good demod?
590 //Index map
591 //0 10 20 30 40 50 60
592 //| | | | | | |
593 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
594 //-----------------------------------------------------------------------------
595 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
596 //
597 //XSF(version)facility:codeone+codetwo
598 //Handle the data
599 uint8_t mask[] = {0,0,0,0,0,0,0,0,0,1};
600 for( idx=0; idx < (size - 65); idx++) {
601 if ( memcmp(dest + idx, mask, sizeof(mask))==0) {
602 //frame marker found
603 if (!dest[idx+8] && dest[idx+17]==1 && dest[idx+26]==1 && dest[idx+35]==1 && dest[idx+44]==1 && dest[idx+53]==1){
604 //confirmed proper separator bits found
605 //return start position
606 return (int) idx;
607 }
608 }
609 }
610 return 0;
611 }
612
613 // by marshmellow
614 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
615 // maybe somehow adjust peak trimming value based on samples to fix?
616 int DetectASKClock(uint8_t dest[], size_t size, int clock)
617 {
618 int i=0;
619 int peak=0;
620 int low=128;
621 int clk[]={16,32,40,50,64,100,128,256};
622 int loopCnt = 256; //don't need to loop through entire array...
623 if (size<loopCnt) loopCnt = size;
624
625 //if we already have a valid clock quit
626 for (;i<8;++i)
627 if (clk[i] == clock) return clock;
628
629 //get high and low peak
630 for (i=0; i < loopCnt; ++i){
631 if(dest[i] > peak){
632 peak = dest[i];
633 }
634 if(dest[i] < low){
635 low = dest[i];
636 }
637 }
638 peak=(int)(((peak-128)*.75)+128);
639 low= (int)(((low-128)*.75)+128);
640 int ii;
641 int clkCnt;
642 int tol = 0;
643 int bestErr[]={1000,1000,1000,1000,1000,1000,1000,1000};
644 int errCnt=0;
645 //test each valid clock from smallest to greatest to see which lines up
646 for(clkCnt=0; clkCnt < 6; ++clkCnt){
647 if (clk[clkCnt] == 32){
648 tol=1;
649 }else{
650 tol=0;
651 }
652 bestErr[clkCnt]=1000;
653 //try lining up the peaks by moving starting point (try first 256)
654 for (ii=0; ii< loopCnt; ++ii){
655 if ((dest[ii] >= peak) || (dest[ii] <= low)){
656 errCnt=0;
657 // now that we have the first one lined up test rest of wave array
658 for (i=0; i<((int)(size/clk[clkCnt])-1); ++i){
659 if (dest[ii+(i*clk[clkCnt])]>=peak || dest[ii+(i*clk[clkCnt])]<=low){
660 }else if(dest[ii+(i*clk[clkCnt])-tol]>=peak || dest[ii+(i*clk[clkCnt])-tol]<=low){
661 }else if(dest[ii+(i*clk[clkCnt])+tol]>=peak || dest[ii+(i*clk[clkCnt])+tol]<=low){
662 }else{ //error no peak detected
663 errCnt++;
664 }
665 }
666 //if we found no errors this is correct one - return this clock
667 if(errCnt==0) return clk[clkCnt];
668 //if we found errors see if it is lowest so far and save it as best run
669 if(errCnt<bestErr[clkCnt]) bestErr[clkCnt]=errCnt;
670 }
671 }
672 }
673 int iii=0;
674 int best=0;
675 for (iii=0; iii<7;++iii){
676 if (bestErr[iii]<bestErr[best]){
677 // current best bit to error ratio vs new bit to error ratio
678 if (((size/clk[best])/bestErr[best] < (size/clk[iii])/bestErr[iii]) ){
679 best = iii;
680 }
681 }
682 }
683 return clk[best];
684 }
685
686 //by marshmellow
687 //detect psk clock by reading #peaks vs no peaks(or errors)
688 int DetectpskNRZClock(uint8_t dest[], size_t size, int clock)
689 {
690 int i=0;
691 int peak=0;
692 int low=128;
693 int clk[]={16,32,40,50,64,100,128,256};
694 int loopCnt = 2048; //don't need to loop through entire array...
695 if (size<loopCnt) loopCnt = size;
696
697 //if we already have a valid clock quit
698 for (; i < 8; ++i)
699 if (clk[i] == clock) return clock;
700
701 //get high and low peak
702 for (i=0; i < loopCnt; ++i){
703 if(dest[i] > peak){
704 peak = dest[i];
705 }
706 if(dest[i] < low){
707 low = dest[i];
708 }
709 }
710 peak=(int)(((peak-128)*.90)+128);
711 low= (int)(((low-128)*.90)+128);
712 //PrintAndLog("DEBUG: peak: %d, low: %d",peak,low);
713 int ii;
714 uint8_t clkCnt;
715 uint8_t tol = 0;
716 int peakcnt=0;
717 int errCnt=0;
718 int bestErr[]={1000,1000,1000,1000,1000,1000,1000,1000,1000};
719 int peaksdet[]={0,0,0,0,0,0,0,0,0};
720 //test each valid clock from smallest to greatest to see which lines up
721 for(clkCnt=0; clkCnt < 6; ++clkCnt){
722 if (clk[clkCnt] == 32){
723 tol=0;
724 }else{
725 tol=0;
726 }
727 //try lining up the peaks by moving starting point (try first 256)
728 for (ii=0; ii< loopCnt; ++ii){
729 if ((dest[ii] >= peak) || (dest[ii] <= low)){
730 errCnt=0;
731 peakcnt=0;
732 // now that we have the first one lined up test rest of wave array
733 for (i=0; i < ((int)(size/clk[clkCnt])-1); ++i){
734 if (dest[ii+(i*clk[clkCnt])]>=peak || dest[ii+(i*clk[clkCnt])]<=low){
735 peakcnt++;
736 }else if(dest[ii+(i*clk[clkCnt])-tol]>=peak || dest[ii+(i*clk[clkCnt])-tol]<=low){
737 peakcnt++;
738 }else if(dest[ii+(i*clk[clkCnt])+tol]>=peak || dest[ii+(i*clk[clkCnt])+tol]<=low){
739 peakcnt++;
740 }else{ //error no peak detected
741 errCnt++;
742 }
743 }
744 if(peakcnt>peaksdet[clkCnt]) {
745 peaksdet[clkCnt]=peakcnt;
746 bestErr[clkCnt]=errCnt;
747 }
748 }
749 }
750 }
751 int iii=0;
752 int best=0;
753 //int ratio2; //debug
754 int ratio;
755 //int bits;
756 for (iii=0; iii < 7; ++iii){
757 ratio=1000;
758 //ratio2=1000; //debug
759 //bits=size/clk[iii]; //debug
760 if (peaksdet[iii] > 0){
761 ratio=bestErr[iii]/peaksdet[iii];
762 if (((bestErr[best]/peaksdet[best]) > (ratio)+1)){
763 best = iii;
764 }
765 //ratio2=bits/peaksdet[iii]; //debug
766 }
767 //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);
768 }
769 return clk[best];
770 }
771
772 //by marshmellow (attempt to get rid of high immediately after a low)
773 void pskCleanWave(uint8_t *bitStream, size_t size)
774 {
775 int i;
776 int low=128;
777 int high=0;
778 int gap = 4;
779 // int loopMax = 2048;
780 int newLow=0;
781 int newHigh=0;
782 for (i=0; i < size; ++i){
783 if (bitStream[i] < low) low=bitStream[i];
784 if (bitStream[i] > high) high=bitStream[i];
785 }
786 high = (int)(((high-128)*.80)+128);
787 low = (int)(((low-128)*.90)+128);
788 //low = (uint8_t)(((int)(low)-128)*.80)+128;
789 for (i=0; i < size; ++i){
790 if (newLow == 1){
791 bitStream[i]=low+8;
792 gap--;
793 if (gap == 0){
794 newLow=0;
795 gap=4;
796 }
797 }else if (newHigh == 1){
798 bitStream[i]=high-8;
799 gap--;
800 if (gap == 0){
801 newHigh=0;
802 gap=4;
803 }
804 }
805 if (bitStream[i] <= low) newLow=1;
806 if (bitStream[i] >= high) newHigh=1;
807 }
808 return;
809 }
810
811
812 //redesigned by marshmellow adjusted from existing decode functions
813 //indala id decoding - only tested on 26 bit tags, but attempted to make it work for more
814 int indala26decode(uint8_t *bitStream, size_t *size, uint8_t *invert)
815 {
816 //26 bit 40134 format (don't know other formats)
817 int i;
818 int long_wait;
819 long_wait = 29;//29 leading zeros in format
820 int start;
821 int first = 0;
822 int first2 = 0;
823 int bitCnt = 0;
824 int ii;
825 // Finding the start of a UID
826 for (start = 0; start <= *size - 250; start++) {
827 first = bitStream[start];
828 for (i = start; i < start + long_wait; i++) {
829 if (bitStream[i] != first) {
830 break;
831 }
832 }
833 if (i == (start + long_wait)) {
834 break;
835 }
836 }
837 if (start == *size - 250 + 1) {
838 // did not find start sequence
839 return -1;
840 }
841 //found start once now test length by finding next one
842 // Inverting signal if needed
843 if (first == 1) {
844 for (i = start; i < *size; i++) {
845 bitStream[i] = !bitStream[i];
846 }
847 *invert = 1;
848 }else *invert=0;
849
850 int iii;
851 for (ii=start+29; ii <= *size - 250; ii++) {
852 first2 = bitStream[ii];
853 for (iii = ii; iii < ii + long_wait; iii++) {
854 if (bitStream[iii] != first2) {
855 break;
856 }
857 }
858 if (iii == (ii + long_wait)) {
859 break;
860 }
861 }
862 if (ii== *size - 250 + 1){
863 // did not find second start sequence
864 return -2;
865 }
866 bitCnt=ii-start;
867
868 // Dumping UID
869 i = start;
870 for (ii = 0; ii < bitCnt; ii++) {
871 bitStream[ii] = bitStream[i++];
872 }
873 *size=bitCnt;
874 return 1;
875 }
876
877
878 //by marshmellow - demodulate PSK wave or NRZ wave (both similar enough)
879 //peaks switch bit (high=1 low=0) each clock cycle = 1 bit determined by last peak
880 int pskNRZrawDemod(uint8_t *dest, size_t *size, int *clk, int *invert)
881 {
882 pskCleanWave(dest,*size);
883 int clk2 = DetectpskNRZClock(dest, *size, *clk);
884 *clk=clk2;
885 uint32_t i;
886 uint8_t high=0, low=128;
887 uint32_t gLen = *size;
888 if (gLen > 1280) gLen=1280;
889 // get high
890 for (i=0; i < gLen; ++i){
891 if (dest[i] > high) high = dest[i];
892 if (dest[i] < low) low = dest[i];
893 }
894 //fudge high/low bars by 25%
895 high = (uint8_t)((((int)(high)-128)*.75)+128);
896 low = (uint8_t)((((int)(low)-128)*.80)+128);
897
898 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
899 int lastBit = 0; //set first clock check
900 uint32_t bitnum = 0; //output counter
901 uint8_t tol = 0; //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
902 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
903 uint32_t iii = 0;
904 uint8_t errCnt =0;
905 uint32_t bestStart = *size;
906 uint32_t maxErr = (*size/1000);
907 uint32_t bestErrCnt = maxErr;
908 //uint8_t midBit=0;
909 uint8_t curBit=0;
910 uint8_t bitHigh=0;
911 uint8_t ignorewin=*clk/8;
912 //PrintAndLog("DEBUG - lastbit - %d",lastBit);
913 //loop to find first wave that works - align to clock
914 for (iii=0; iii < gLen; ++iii){
915 if ((dest[iii]>=high) || (dest[iii]<=low)){
916 lastBit=iii-*clk;
917 //loop through to see if this start location works
918 for (i = iii; i < *size; ++i) {
919 //if we found a high bar and we are at a clock bit
920 if ((dest[i]>=high ) && (i>=lastBit+*clk-tol && i<=lastBit+*clk+tol)){
921 bitHigh=1;
922 lastBit+=*clk;
923 ignorewin=*clk/8;
924 bitnum++;
925 //else if low bar found and we are at a clock point
926 }else if ((dest[i]<=low ) && (i>=lastBit+*clk-tol && i<=lastBit+*clk+tol)){
927 bitHigh=1;
928 lastBit+=*clk;
929 ignorewin=*clk/8;
930 bitnum++;
931 //else if no bars found
932 }else if(dest[i] < high && dest[i] > low) {
933 if (ignorewin==0){
934 bitHigh=0;
935 }else ignorewin--;
936 //if we are past a clock point
937 if (i >= lastBit+*clk+tol){ //clock val
938 lastBit+=*clk;
939 bitnum++;
940 }
941 //else if bar found but we are not at a clock bit and we did not just have a clock bit
942 }else if ((dest[i]>=high || dest[i]<=low) && (i<lastBit+*clk-tol || i>lastBit+*clk+tol) && (bitHigh==0)){
943 //error bar found no clock...
944 errCnt++;
945 }
946 if (bitnum>=1000) break;
947 }
948 //we got more than 64 good bits and not all errors
949 if ((bitnum > (64+errCnt)) && (errCnt < (maxErr))) {
950 //possible good read
951 if (errCnt == 0){
952 bestStart = iii;
953 bestErrCnt = errCnt;
954 break; //great read - finish
955 }
956 if (bestStart == iii) break; //if current run == bestErrCnt run (after exhausted testing) then finish
957 if (errCnt < bestErrCnt){ //set this as new best run
958 bestErrCnt = errCnt;
959 bestStart = iii;
960 }
961 }
962 }
963 }
964 if (bestErrCnt < maxErr){
965 //best run is good enough set to best run and set overwrite BinStream
966 iii=bestStart;
967 lastBit=bestStart-*clk;
968 bitnum=0;
969 for (i = iii; i < *size; ++i) {
970 //if we found a high bar and we are at a clock bit
971 if ((dest[i] >= high ) && (i>=lastBit+*clk-tol && i<=lastBit+*clk+tol)){
972 bitHigh=1;
973 lastBit+=*clk;
974 curBit=1-*invert;
975 dest[bitnum]=curBit;
976 ignorewin=*clk/8;
977 bitnum++;
978 //else if low bar found and we are at a clock point
979 }else if ((dest[i]<=low ) && (i>=lastBit+*clk-tol && i<=lastBit+*clk+tol)){
980 bitHigh=1;
981 lastBit+=*clk;
982 curBit=*invert;
983 dest[bitnum]=curBit;
984 ignorewin=*clk/8;
985 bitnum++;
986 //else if no bars found
987 }else if(dest[i]<high && dest[i]>low) {
988 if (ignorewin==0){
989 bitHigh=0;
990 }else ignorewin--;
991 //if we are past a clock point
992 if (i>=lastBit+*clk+tol){ //clock val
993 lastBit+=*clk;
994 dest[bitnum]=curBit;
995 bitnum++;
996 }
997 //else if bar found but we are not at a clock bit and we did not just have a clock bit
998 }else if ((dest[i]>=high || dest[i]<=low) && ((i<lastBit+*clk-tol) || (i>lastBit+*clk+tol)) && (bitHigh==0)){
999 //error bar found no clock...
1000 bitHigh=1;
1001 dest[bitnum]=77;
1002 bitnum++;
1003 errCnt++;
1004 }
1005 if (bitnum >=1000) break;
1006 }
1007 *size=bitnum;
1008 } else{
1009 *size=bitnum;
1010 *clk=bestStart;
1011 return -1;
1012 }
1013
1014 if (bitnum>16){
1015 *size=bitnum;
1016 } else return -1;
1017 return errCnt;
1018 }
1019
Impressum, Datenschutz