]> git.zerfleddert.de Git - proxmark3-svn/blob - common/lfdemod.c
slight adjustment to include
[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 <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 //#include <inttypes.h>
15 //#include <limits.h>
16 #include "lfdemod.h"
17 //#include "proxmark3.h"
18 //#include "data.h"
19 //#include "ui.h"
20 //#include "graph.h"
21 //#include "cmdparser.h"
22 //#include "util.h"
23 //#include "cmdmain.h"
24 //#include "cmddata.h"
25 //uint8_t BinStream[MAX_GRAPH_TRACE_LEN];
26 //uint8_t BinStreamLen;
27
28 //by marshmellow
29 //takes 1s and 0s and searches for EM410x format - output EM ID
30 uint64_t Em410xDecode(uint8_t BitStream[],uint32_t BitLen)
31 {
32 //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
33 // otherwise could be a void with no arguments
34 //set defaults
35 int high=0, low=0;
36 uint64_t lo=0; //hi=0,
37
38 uint32_t i = 0;
39 uint32_t initLoopMax = 1000;
40 if (initLoopMax>BitLen) initLoopMax=BitLen;
41
42 for (;i < initLoopMax; ++i) //1000 samples should be plenty to find high and low values
43 {
44 if (BitStream[i] > high)
45 high = BitStream[i];
46 else if (BitStream[i] < low)
47 low = BitStream[i];
48 }
49 if (((high !=1)||(low !=0))){ //allow only 1s and 0s
50 // PrintAndLog("no data found");
51 return 0;
52 }
53 uint8_t parityTest=0;
54 // 111111111 bit pattern represent start of frame
55 uint8_t frame_marker_mask[] = {1,1,1,1,1,1,1,1,1};
56 uint32_t idx = 0;
57 uint32_t ii=0;
58 uint8_t resetCnt = 0;
59 while( (idx + 64) < BitLen) {
60 restart:
61 // search for a start of frame marker
62 if ( memcmp(BitStream+idx, frame_marker_mask, sizeof(frame_marker_mask)) == 0)
63 { // frame marker found
64 idx+=9;//sizeof(frame_marker_mask);
65 for (i=0; i<10;i++){
66 for(ii=0; ii<5; ++ii){
67 parityTest += BitStream[(i*5)+ii+idx];
68 }
69 if (parityTest== ((parityTest>>1)<<1)){
70 parityTest=0;
71 for (ii=0; ii<4;++ii){
72 //hi = (hi<<1)|(lo>>31);
73 lo=(lo<<1LL)|(BitStream[(i*5)+ii+idx]);
74 }
75 //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);
76 }else {//parity failed
77 //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]);
78 parityTest=0;
79 idx-=8;
80 if (resetCnt>5)return 0;
81 resetCnt++;
82 goto restart;//continue;
83 }
84 }
85 //skip last 5 bit parity test for simplicity.
86 return lo;
87 }else{
88 idx++;
89 }
90 }
91 return 0;
92 }
93
94 //by marshmellow
95 //takes 2 arguments - clock and invert both as integers
96 //attempts to demodulate ask while decoding manchester
97 //prints binary found and saves in graphbuffer for further commands
98 int askmandemod(uint8_t * BinStream,uint32_t *BitLen,int *clk, int *invert)
99 {
100 uint32_t i;
101 //int invert=0; //invert default
102 int high = 0, low = 0;
103 *clk=DetectClock2(BinStream,(size_t)*BitLen,*clk); //clock default
104 uint8_t BitStream[MAX_BitStream_LEN] = {0};
105
106 //sscanf(Cmd, "%i %i", &clk, &invert);
107 if (*clk<8) *clk =64;
108 if (*clk<32) *clk=32;
109 if (*invert != 0 && *invert != 1) *invert=0;
110 uint32_t initLoopMax = 1000;
111 if (initLoopMax>*BitLen) initLoopMax=*BitLen;
112 // Detect high and lows
113 //PrintAndLog("Using Clock: %d and invert=%d",clk,invert);
114 for (i = 0; i < initLoopMax; ++i) //1000 samples should be plenty to find high and low values
115 {
116 if (BinStream[i] > high)
117 high = BinStream[i];
118 else if (BinStream[i] < low)
119 low = BinStream[i];
120 }
121 if ((high < 30) && ((high !=1)||(low !=-1))){ //throw away static - allow 1 and -1 (in case of threshold command first)
122 //PrintAndLog("no data found");
123 return -1;
124 }
125 //13% fuzz in case highs and lows aren't clipped [marshmellow]
126 high=(int)(0.75*high);
127 low=(int)(0.75*low);
128
129 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
130 int lastBit = 0; //set first clock check
131 uint32_t bitnum = 0; //output counter
132 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
133 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
134 uint32_t iii = 0;
135 uint32_t gLen = *BitLen;
136 if (gLen > 500) gLen=500;
137 uint8_t errCnt =0;
138 uint32_t bestStart = *BitLen;
139 uint32_t bestErrCnt = (*BitLen/1000);
140 //PrintAndLog("DEBUG - lastbit - %d",lastBit);
141 //loop to find first wave that works
142 for (iii=0; iii < gLen; ++iii){
143 if ((BinStream[iii]>=high)||(BinStream[iii]<=low)){
144 lastBit=iii-*clk;
145 //loop through to see if this start location works
146 for (i = iii; i < *BitLen; ++i) {
147 if ((BinStream[i] >= high) && ((i-lastBit)>(*clk-tol))){
148 lastBit+=*clk;
149 BitStream[bitnum] = *invert;
150 bitnum++;
151 } else if ((BinStream[i] <= low) && ((i-lastBit)>(*clk-tol))){
152 //low found and we are expecting a bar
153 lastBit+=*clk;
154 BitStream[bitnum] = 1-*invert;
155 bitnum++;
156 } else {
157 //mid value found or no bar supposed to be here
158 if ((i-lastBit)>(*clk+tol)){
159 //should have hit a high or low based on clock!!
160
161
162 //debug
163 //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);
164 if (bitnum > 0){
165 BitStream[bitnum]=77;
166 bitnum++;
167 }
168
169
170 errCnt++;
171 lastBit+=*clk;//skip over until hit too many errors
172 if (errCnt>((*BitLen/1000))){ //allow 1 error for every 1000 samples else start over
173 errCnt=0;
174 bitnum=0;//start over
175 break;
176 }
177 }
178 }
179 }
180 //we got more than 64 good bits and not all errors
181 if ((bitnum > (64+errCnt)) && (errCnt<(*BitLen/1000))) {
182 //possible good read
183 if (errCnt==0) break; //great read - finish
184 if (bestStart == iii) break; //if current run == bestErrCnt run (after exhausted testing) then finish
185 if (errCnt<bestErrCnt){ //set this as new best run
186 bestErrCnt=errCnt;
187 bestStart = iii;
188 }
189 }
190 }
191 if (iii>=gLen){ //exhausted test
192 //if there was a ok test go back to that one and re-run the best run (then dump after that run)
193 if (bestErrCnt < (*BitLen/1000)) iii=bestStart;
194 }
195 }
196 if (bitnum>16){
197
198 // PrintAndLog("Data start pos:%d, lastBit:%d, stop pos:%d, numBits:%d",iii,lastBit,i,bitnum);
199 //move BitStream back to GraphBuffer
200 //ClearGraph(0);
201 for (i=0; i < bitnum; ++i){
202 BinStream[i]=BitStream[i];
203 }
204 *BitLen=bitnum;
205 //RepaintGraphWindow();
206 //output
207 //if (errCnt>0){
208 // PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
209 //}
210 // PrintAndLog("ASK decoded bitstream:");
211 // Now output the bitstream to the scrollback by line of 16 bits
212 // printBitStream2(BitStream,bitnum);
213 // Em410xDecode(Cmd);
214 }
215 return errCnt;
216 }
217
218 //by marshmellow
219 //take 10 and 01 and manchester decode
220 //run through 2 times and take least errCnt
221 int manrawdemod(uint8_t * BitStream, int *bitLen)
222 {
223 uint8_t BitStream2[MAX_BitStream_LEN]={0};
224 int bitnum=0;
225 int errCnt =0;
226 int i=1;
227 int bestErr = 1000;
228 int bestRun = 0;
229 int finish = 0;
230 int ii=1;
231 for (ii=1;ii<3;++ii){
232 i=1;
233 for (i=i+ii;i<*bitLen-2;i+=2){
234 if(BitStream[i]==1 && (BitStream[i+1]==0)){
235 BitStream2[bitnum++]=0;
236 } else if((BitStream[i]==0)&& BitStream[i+1]==1){
237 BitStream2[bitnum++]=1;
238 } else {
239 BitStream2[bitnum++]=77;
240 errCnt++;
241 }
242 }
243 if (bestErr>errCnt){
244 bestErr=errCnt;
245 bestRun=ii;
246 }
247 if (ii>1 || finish==1) {
248 if (bestRun==ii) {
249 break;
250 } else{
251 ii=bestRun-1;
252 finish=1;
253 }
254 }
255 errCnt=0;
256 bitnum=0;
257 }
258 errCnt=bestErr;
259 if (errCnt<10){
260 for (i=0; i<bitnum;++i){
261 BitStream[i]=BitStream2[i];
262 }
263 *bitLen=bitnum;
264 }
265 return errCnt;
266 }
267
268 //by marshmellow
269 //takes 2 arguments - clock and invert both as integers
270 //attempts to demodulate ask only
271 //prints binary found and saves in graphbuffer for further commands
272 int askrawdemod(uint8_t *BinStream, int *bitLen,int *clk, int *invert)
273 {
274 uint32_t i;
275 // int invert=0; //invert default
276 int high = 0, low = 0;
277 *clk=DetectClock2(BinStream,*bitLen,*clk); //clock default
278 uint8_t BitStream[MAX_BitStream_LEN] = {0};
279
280 if (*clk<8) *clk =64;
281 if (*clk<32) *clk=32;
282 if (*invert != 0 && *invert != 1) *invert =0;
283 uint32_t initLoopMax = 1000;
284 if (initLoopMax>*bitLen) initLoopMax=*bitLen;
285 // Detect high and lows
286 for (i = 0; i < initLoopMax; ++i) //1000 samples should be plenty to find high and low values
287 {
288 if (BinStream[i] > high)
289 high = BinStream[i];
290 else if (BinStream[i] < low)
291 low = BinStream[i];
292 }
293 if ((high < 30) && ((high !=1)||(low !=-1))){ //throw away static - allow 1 and -1 (in case of threshold command first)
294 // PrintAndLog("no data found");
295 return -1;
296 }
297 //13% fuzz in case highs and lows aren't clipped [marshmellow]
298 high=(int)(0.75*high);
299 low=(int)(0.75*low);
300
301 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
302 int lastBit = 0; //set first clock check
303 uint32_t bitnum = 0; //output counter
304 uint8_t tol = 0; //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
305 if (*clk==32)tol=1; //clock tolerance may not be needed anymore currently set to + or - 1 but could be increased for poor waves or removed entirely
306 uint32_t iii = 0;
307 uint32_t gLen = *bitLen;
308 if (gLen > 500) gLen=500;
309 uint8_t errCnt =0;
310 uint32_t bestStart = *bitLen;
311 uint32_t bestErrCnt = (*bitLen/1000);
312 uint8_t midBit=0;
313 //PrintAndLog("DEBUG - lastbit - %d",lastBit);
314 //loop to find first wave that works
315 for (iii=0; iii < gLen; ++iii){
316 if ((BinStream[iii]>=high)||(BinStream[iii]<=low)){
317 lastBit=iii-*clk;
318 //loop through to see if this start location works
319 for (i = iii; i < *bitLen; ++i) {
320 if ((BinStream[i] >= high) && ((i-lastBit)>(*clk-tol))){
321 lastBit+=*clk;
322 BitStream[bitnum] = *invert;
323 bitnum++;
324 midBit=0;
325 } else if ((BinStream[i] <= low) && ((i-lastBit)>(*clk-tol))){
326 //low found and we are expecting a bar
327 lastBit+=*clk;
328 BitStream[bitnum] = 1-*invert;
329 bitnum++;
330 midBit=0;
331 } else if ((BinStream[i]<=low) && (midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
332 //mid bar?
333 midBit=1;
334 BitStream[bitnum]= 1-*invert;
335 bitnum++;
336 } else if ((BinStream[i]>=high)&&(midBit==0) && ((i-lastBit)>((*clk/2)-tol))){
337 //mid bar?
338 midBit=1;
339 BitStream[bitnum]= *invert;
340 bitnum++;
341 } else if ((i-lastBit)>((*clk/2)+tol)&&(midBit==0)){
342 //no mid bar found
343 midBit=1;
344 BitStream[bitnum]= BitStream[bitnum-1];
345 bitnum++;
346 } else {
347 //mid value found or no bar supposed to be here
348
349 if ((i-lastBit)>(*clk+tol)){
350 //should have hit a high or low based on clock!!
351 //debug
352 //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);
353 if (bitnum > 0){
354 BitStream[bitnum]=77;
355 bitnum++;
356 }
357
358
359 errCnt++;
360 lastBit+=*clk;//skip over until hit too many errors
361 if (errCnt>((*bitLen/1000))){ //allow 1 error for every 1000 samples else start over
362 errCnt=0;
363 bitnum=0;//start over
364 break;
365 }
366 }
367 }
368 }
369 //we got more than 64 good bits and not all errors
370 if ((bitnum > (64+errCnt)) && (errCnt<(*bitLen/1000))) {
371 //possible good read
372 if (errCnt==0) break; //great read - finish
373 if (bestStart == iii) break; //if current run == bestErrCnt run (after exhausted testing) then finish
374 if (errCnt<bestErrCnt){ //set this as new best run
375 bestErrCnt=errCnt;
376 bestStart = iii;
377 }
378 }
379 }
380 if (iii>=gLen){ //exhausted test
381 //if there was a ok test go back to that one and re-run the best run (then dump after that run)
382 if (bestErrCnt < (*bitLen/1000)) iii=bestStart;
383 }
384 }
385 if (bitnum>16){
386
387 // PrintAndLog("Data start pos:%d, lastBit:%d, stop pos:%d, numBits:%d",iii,lastBit,i,bitnum);
388 //move BitStream back to BinStream
389 // ClearGraph(0);
390 for (i=0; i < bitnum; ++i){
391 BinStream[i]=BitStream[i];
392 }
393 *bitLen=bitnum;
394 // RepaintGraphWindow();
395 //output
396 // if (errCnt>0){
397 // PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
398 // }
399 // PrintAndLog("ASK decoded bitstream:");
400 // Now output the bitstream to the scrollback by line of 16 bits
401 // printBitStream2(BitStream,bitnum);
402 //int errCnt=0;
403 //errCnt=manrawdemod(BitStream,bitnum);
404
405 // Em410xDecode(Cmd);
406 } else return -1;
407 return errCnt;
408 }
409 //translate wave to 11111100000 (1 for each short wave 0 for each long wave)
410 size_t fsk_wave_demod2(uint8_t * dest, size_t size)
411 {
412 uint32_t last_transition = 0;
413 uint32_t idx = 1;
414 uint32_t maxVal=0;
415 // // we don't care about actual value, only if it's more or less than a
416 // // threshold essentially we capture zero crossings for later analysis
417
418 // we do care about the actual value as sometimes near the center of the
419 // wave we may get static that changes direction of wave for one value
420 // if our value is too low it might affect the read. and if our tag or
421 // antenna is weak a setting too high might not see anything. [marshmellow]
422 if (size<100) return 0;
423 for(idx=1; idx<100; idx++){
424 if(maxVal<dest[idx]) maxVal = dest[idx];
425 }
426 // set close to the top of the wave threshold with 13% margin for error
427 // less likely to get a false transition up there.
428 // (but have to be careful not to go too high and miss some short waves)
429 uint32_t threshold_value = (uint32_t)(maxVal*.87); idx=1;
430 //uint8_t threshold_value = 127;
431
432 // sync to first lo-hi transition, and threshold
433
434 // Need to threshold first sample
435 if(dest[0] < threshold_value) dest[0] = 0;
436 else dest[0] = 1;
437
438 size_t numBits = 0;
439 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
440 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
441 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
442 for(idx = 1; idx < size; idx++) {
443 // threshold current value
444 if (dest[idx] < threshold_value) dest[idx] = 0;
445 else dest[idx] = 1;
446
447 // Check for 0->1 transition
448 if (dest[idx-1] < dest[idx]) { // 0 -> 1 transition
449 if (idx-last_transition<6){
450 //do nothing with extra garbage
451 } else if (idx-last_transition < 9) {
452 dest[numBits]=1;
453 } else {
454 dest[numBits]=0;
455 }
456 last_transition = idx;
457 numBits++;
458 }
459 }
460 return numBits; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
461 }
462
463 uint32_t myround2(float f)
464 {
465 if (f >= 2000) return 2000;//something bad happened
466 return (uint32_t) (f + (float)0.5);
467 }
468
469 //translate 11111100000 to 10
470 size_t aggregate_bits2(uint8_t *dest,size_t size, uint8_t rfLen, uint8_t maxConsequtiveBits, uint8_t invert )// uint8_t h2l_crossing_value,uint8_t l2h_crossing_value,
471 {
472 uint8_t lastval=dest[0];
473 uint32_t idx=0;
474 size_t numBits=0;
475 uint32_t n=1;
476
477 for( idx=1; idx < size; idx++) {
478
479 if (dest[idx]==lastval) {
480 n++;
481 continue;
482 }
483 //if lastval was 1, we have a 1->0 crossing
484 if ( dest[idx-1]==1 ) {
485 n=myround2((float)(n+1)/((float)(rfLen)/(float)8));
486 //n=(n+1) / h2l_crossing_value;
487 } else {// 0->1 crossing
488 n=myround2((float)(n+1)/((float)(rfLen-2)/(float)10));
489 //n=(n+1) / l2h_crossing_value;
490 }
491 if (n == 0) n = 1;
492
493 if(n < maxConsequtiveBits) //Consecutive
494 {
495 if(invert==0){ //invert bits
496 memset(dest+numBits, dest[idx-1] , n);
497 }else{
498 memset(dest+numBits, dest[idx-1]^1 , n);
499 }
500 numBits += n;
501 }
502 n=0;
503 lastval=dest[idx];
504 }//end for
505 return numBits;
506 }
507 //by marshmellow (from holiman's base)
508 // full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod)
509 int fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert)
510 {
511 //uint8_t h2l_crossing_value = 6;
512 //uint8_t l2h_crossing_value = 5;
513
514 // if (rfLen==64) //currently only know settings for RF/64 change from default if option entered
515 // {
516 // h2l_crossing_value=8; //or 8 as 64/8 = 8
517 // l2h_crossing_value=6; //or 6.4 as 64/10 = 6.4
518 // }
519 // size_t size = GraphTraceLen;
520 // FSK demodulator
521 size = fsk_wave_demod2(dest, size);
522 size = aggregate_bits2(dest, size,rfLen,192,invert);
523 // size = aggregate_bits(size, h2l_crossing_value, l2h_crossing_value,192, invert); //192=no limit to same values
524 //done messing with GraphBuffer - repaint
525 //RepaintGraphWindow();
526 return size;
527 }
528 // loop to get raw HID waveform then FSK demodulate the TAG ID from it
529 int HIDdemodFSK(uint8_t *dest, size_t size, uint32_t *hi2, uint32_t *hi, uint32_t *lo)
530 {
531
532 size_t idx=0; //, found=0; //size=0,
533 // FSK demodulator
534 size = fskdemod(dest, size,50,0);
535
536 // final loop, go over previously decoded manchester data and decode into usable tag ID
537 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
538 uint8_t frame_marker_mask[] = {1,1,1,0,0,0};
539 int numshifts = 0;
540 idx = 0;
541 //one scan
542 while( idx + sizeof(frame_marker_mask) < size) {
543 // search for a start of frame marker
544 if ( memcmp(dest+idx, frame_marker_mask, sizeof(frame_marker_mask)) == 0)
545 { // frame marker found
546 idx+=sizeof(frame_marker_mask);
547 while(dest[idx] != dest[idx+1] && idx < size-2)
548 {
549 // Keep going until next frame marker (or error)
550 // Shift in a bit. Start by shifting high registers
551 *hi2 = (*hi2<<1)|(*hi>>31);
552 *hi = (*hi<<1)|(*lo>>31);
553 //Then, shift in a 0 or one into low
554 if (dest[idx] && !dest[idx+1]) // 1 0
555 *lo=(*lo<<1)|0;
556 else // 0 1
557 *lo=(*lo<<1)|1;
558 numshifts++;
559 idx += 2;
560 }
561 // Hopefully, we read a tag and hit upon the next frame marker
562 if(idx + sizeof(frame_marker_mask) < size)
563 {
564 if ( memcmp(dest+idx, frame_marker_mask, sizeof(frame_marker_mask)) == 0)
565 {
566 //good return
567 return idx;
568 }
569 }
570 // reset
571 *hi2 = *hi = *lo = 0;
572 numshifts = 0;
573 }else {
574 idx++;
575 }
576 }
577 return -1;
578 }
579
580 uint32_t bytebits_to_byte(uint8_t* src, int numbits)
581 {
582 uint32_t num = 0;
583 for(int i = 0 ; i < numbits ; i++)
584 {
585 num = (num << 1) | (*src);
586 src++;
587 }
588 return num;
589 }
590
591 int IOdemodFSK(uint8_t *dest, size_t size)
592 {
593 size_t idx=0;
594 //make sure buffer has data
595 if (size < 64) return -1;
596 //test samples are not just noise
597 uint8_t testMax=0;
598 for(idx=0;idx<64;idx++){
599 if (testMax<dest[idx]) testMax=dest[idx];
600 }
601 idx=0;
602 //if not just noise
603 if (testMax>170){
604 // FSK demodulator
605 size = fskdemod(dest, size,64,1);
606 //Index map
607 //0 10 20 30 40 50 60
608 //| | | | | | |
609 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
610 //-----------------------------------------------------------------------------
611 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
612 //
613 //XSF(version)facility:codeone+codetwo
614 //Handle the data
615 uint8_t mask[] = {0,0,0,0,0,0,0,0,0,1};
616 for( idx=0; idx < (size - 74); idx++) {
617 if ( memcmp(dest + idx, mask, sizeof(mask))==0) {
618 //frame marker found
619 if (!dest[idx+8] && dest[idx+17]==1 && dest[idx+26]==1 && dest[idx+35]==1 && dest[idx+44]==1 && dest[idx+53]==1){
620 //confirmed proper separator bits found
621 //return start position
622 return idx;
623 }
624 }
625 }
626 }
627 return 0;
628 }
629
630 // by marshmellow
631 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
632 // maybe somehow adjust peak trimming value based on samples to fix?
633 int DetectClock2(uint8_t dest[], size_t size, int clock)
634 {
635 int i=0;
636 int peak=0;
637 int low=0;
638 int clk[]={16,32,40,50,64,100,128,256};
639 for (;i<8;++i)
640 if (clk[i]==clock) return clock;
641 if (!peak){
642 for (i=0;i<size;++i){
643 if(dest[i]>peak){
644 peak = dest[i];
645 }
646 if(dest[i]<low){
647 low = dest[i];
648 }
649 }
650 peak=(int)(peak*.75);
651 low= (int)(low*.75);
652 }
653 int ii;
654 int loopCnt = 256;
655 if (size<loopCnt) loopCnt = size;
656 int clkCnt;
657 int tol = 0;
658 int bestErr=1000;
659 int errCnt[]={0,0,0,0,0,0,0,0};
660 for(clkCnt=0; clkCnt<6;++clkCnt){
661 if (clk[clkCnt]==32){
662 tol=1;
663 }else{
664 tol=0;
665 }
666 bestErr=1000;
667 for (ii=0; ii<loopCnt; ++ii){
668 if ((dest[ii]>=peak) || (dest[ii]<=low)){
669 errCnt[clkCnt]=0;
670 for (i=0; i<((int)(size/clk[clkCnt])-1); ++i){
671 if (dest[ii+(i*clk[clkCnt])]>=peak || dest[ii+(i*clk[clkCnt])]<=low){
672 }else if(dest[ii+(i*clk[clkCnt])-tol]>=peak || dest[ii+(i*clk[clkCnt])-tol]<=low){
673 }else if(dest[ii+(i*clk[clkCnt])+tol]>=peak || dest[ii+(i*clk[clkCnt])+tol]<=low){
674 }else{ //error no peak detected
675 errCnt[clkCnt]++;
676 }
677 }
678 if(errCnt[clkCnt]==0) return clk[clkCnt];
679 if(errCnt[clkCnt]<bestErr) bestErr=errCnt[clkCnt];
680 }
681 }
682 errCnt[clkCnt]=bestErr;
683 }
684 int iii=0;
685 int best=0;
686 for (iii=0; iii<6;++iii){
687 if (errCnt[iii]<errCnt[best]){
688 best = iii;
689 }
690 }
691 return clk[best];
692 }
Impressum, Datenschutz