]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/graph.c
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 //-----------------------------------------------------------------------------
18 int GraphBuffer
[MAX_GRAPH_TRACE_LEN
];
21 /* write a bit to the graph */
22 void AppendGraph(int redraw
, int clock
, int bit
)
25 int half
= (int)(clock
/2);
26 int firstbit
= bit
^ 1;
28 for (i
= 0; i
< half
; ++i
)
29 GraphBuffer
[GraphTraceLen
++] = firstbit
;
31 for (i
= 0; i
<= half
; ++i
)
32 GraphBuffer
[GraphTraceLen
++] = bit
;
38 /* clear out our graph window */
39 int ClearGraph(int redraw
)
41 int gtl
= GraphTraceLen
;
42 memset(GraphBuffer
, 0x00, GraphTraceLen
);
55 //decommissioned - has difficulty detecting rf/32
57 int DetectClockOld(int peak)
63 // Detect peak if we don't have one
65 for (i = 0; i < GraphTraceLen; ++i)
66 if (GraphBuffer[i] > peak)
67 peak = GraphBuffer[i];
69 // peak=(int)(peak*.75);
70 for (i = 1; i < GraphTraceLen; ++i)
72 // If this is the beginning of a peak
73 if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] >= peak)
75 // Find lowest difference between peaks
76 if (lastpeak && i - lastpeak < clock)
89 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
90 // maybe somehow adjust peak trimming value based on samples to fix?
91 int DetectASKClock(int peak)
95 int clk[]={16,32,40,50,64,100,128,256};
97 if (GraphTraceLen<loopCnt) loopCnt = GraphTraceLen;
99 for (i=0;i<loopCnt;++i){
100 if(GraphBuffer[i]>peak){
101 peak = GraphBuffer[i];
103 if(GraphBuffer[i]<low){
104 low = GraphBuffer[i];
107 peak=(int)(peak*.75);
114 int errCnt[]={0,0,0,0,0,0,0,0};
115 for(clkCnt=0; clkCnt<6;++clkCnt){
116 if (clk[clkCnt]==32){
122 for (ii=0; ii<loopCnt; ++ii){
123 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){
127 }else if(GraphBuffer[ii+(i*clk[clkCnt])-tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])-tol]<=low){
128 }else if(GraphBuffer[ii+(i*clk[clkCnt])+tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])+tol]<=low){
129 }else{ //error no peak detected
133 if(errCnt[clkCnt]==0) return clk[clkCnt];
134 if(errCnt[clkCnt]<bestErr) bestErr=errCnt[clkCnt];
140 for (iii=0; iii<6;++iii){
141 if (errCnt[iii]<errCnt[best]){
145 // PrintAndLog("DEBUG: clkCnt: %d, ii: %d, i: %d peak: %d, low: %d, errcnt: %d, errCnt64: %d",clkCnt,ii,i,peak,low,errCnt[best],errCnt[4]);
149 void setGraphBuf(uint8_t *buff
,int size
)
153 for (; i
< size
; ++i
){
154 GraphBuffer
[i
]=buff
[i
];
157 RepaintGraphWindow();
160 int getFromGraphBuf(uint8_t *buff
)
163 for (i
=0;i
<GraphTraceLen
;++i
){
164 if (GraphBuffer
[i
]>127) GraphBuffer
[i
]=127; //trim
165 if (GraphBuffer
[i
]<-127) GraphBuffer
[i
]=-127; //trim
166 buff
[i
]=(uint8_t)(GraphBuffer
[i
]+128);
170 /* Get or auto-detect clock rate */
171 int GetClock(const char *str
, int peak
, int verbose
)
175 sscanf(str
, "%i", &clock
);
176 if (!strcmp(str
, ""))
179 /* Auto-detect clock */
182 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
183 int size
= getFromGraphBuf(grph
);
184 clock
= DetectASKClock(grph
,size
,0);
185 //clock2 = DetectClock2(peak);
186 /* Only print this message if we're not looping something */
188 PrintAndLog("Auto-detected clock rate: %d", clock
);
195 /* A simple test to see if there is any data inside Graphbuffer.
199 if ( GraphTraceLen
<= 0) {
200 PrintAndLog("No data available, try reading something first");