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