]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/graph.c
bb8159951b2eb8c4637b5a0664f3fbb33d690ed3
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
17 int GraphBuffer
[MAX_GRAPH_TRACE_LEN
];
20 /* write a bit to the graph */
21 void AppendGraph(int redraw
, int clock
, int bit
)
24 int half
= (int)(clock
/2);
25 int firstbit
= bit
^ 1;
27 for (i
= 0; i
< half
; ++i
)
28 GraphBuffer
[GraphTraceLen
++] = firstbit
;
30 for (i
= 0; i
<= half
; ++i
)
31 GraphBuffer
[GraphTraceLen
++] = bit
;
37 /* clear out our graph window */
38 int ClearGraph(int redraw
)
40 int gtl
= GraphTraceLen
;
41 memset(GraphBuffer
, 0x00, GraphTraceLen
);
54 //decommissioned - has difficulty detecting rf/32 and only works if data is manchester encoded
56 int DetectClock2(int peak)
62 // Detect peak if we don't have one
64 for (i = 0; i < GraphTraceLen; ++i)
65 if (GraphBuffer[i] > peak)
66 peak = GraphBuffer[i];
68 for (i = 1; i < GraphTraceLen; ++i)
70 // If this is the beginning of a peak
71 if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] >= peak)
73 // Find lowest difference between peaks
74 if (lastpeak && i - lastpeak < clock)
85 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
86 // maybe somehow adjust peak trimming value based on samples to fix?
87 int DetectClock(int peak
)
91 int clk
[]={16,32,40,50,64,100,128,256};
93 for (i
=0;i
<GraphTraceLen
;++i
){
94 if(GraphBuffer
[i
]>peak
){
95 peak
= GraphBuffer
[i
];
97 if(GraphBuffer
[i
]<low
){
101 peak
=(int)(peak
*.75);
107 if (GraphTraceLen
<loopCnt
) loopCnt
= GraphTraceLen
;
111 int errCnt
[]={0,0,0,0,0,0,0,0};
113 for(clkCnt
=0; clkCnt
<6;++clkCnt
){
114 if (clk
[clkCnt
]==32){
120 for (ii
=0; ii
<loopCnt
; ++ii
){
121 if ((GraphBuffer
[ii
]>=peak
) || (GraphBuffer
[ii
]<=low
)){
125 for (i
=0; i
<((int)(GraphTraceLen
/clk
[clkCnt
])-1); ++i
){
126 if (GraphBuffer
[ii
+(i
*clk
[clkCnt
])]>=peak
|| GraphBuffer
[ii
+(i
*clk
[clkCnt
])]<=low
){
128 }else if(GraphBuffer
[ii
+(i
*clk
[clkCnt
])-tol
]>=peak
|| GraphBuffer
[ii
+(i
*clk
[clkCnt
])-tol
]<=low
){
129 }else if(GraphBuffer
[ii
+(i
*clk
[clkCnt
])+tol
]>=peak
|| GraphBuffer
[ii
+(i
*clk
[clkCnt
])+tol
]<=low
){
130 }else{ //error no peak detected
137 if(errCnt
[clkCnt
]==0) return clk
[clkCnt
];
138 if(errCnt
[clkCnt
]<bestErr
) bestErr
=errCnt
[clkCnt
];
141 errCnt
[clkCnt
]=bestErr
;
145 for (iii
=0; iii
<6;++iii
){
146 if (errCnt
[iii
]<errCnt
[best
]){
150 PrintAndLog("clkCnt: %d, ii: %d, i: %d peak: %d, low: %d, errcnt: %d, errCnt64: %d",clkCnt
,ii
,i
,peak
,low
,errCnt
[best
],errCnt
[4]);
155 /* Get or auto-detect clock rate */
156 int GetClock(const char *str
, int peak
, int verbose
)
160 sscanf(str
, "%i", &clock
);
161 if (!strcmp(str
, ""))
164 /* Auto-detect clock */
167 clock
= DetectClock(peak
);
168 //clock2 = DetectClock2(peak);
169 /* Only print this message if we're not looping something */
171 PrintAndLog("Auto-detected clock rate: %d", clock
);
178 /* A simple test to see if there is any data inside Graphbuffer.
182 if ( GraphTraceLen
<= 0) {
183 PrintAndLog("No data available, try reading something first");