]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/graph.c
Merge pull request #1 from bforbort/master
[proxmark3-svn] / client / graph.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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 // Graph utilities
9 //-----------------------------------------------------------------------------
10
11 #include <stdio.h>
12 #include <stdbool.h>
13 #include <string.h>
14 #include "ui.h"
15 #include "graph.h"
16 #include "lfdemod.h"
17
18 int GraphBuffer[MAX_GRAPH_TRACE_LEN];
19 int GraphTraceLen;
20
21 /* write a bit to the graph */
22 void AppendGraph(int redraw, int clock, int bit)
23 {
24 int i;
25 int half = (int)(clock/2);
26 int firstbit = bit ^ 1;
27
28 for (i = 0; i < half; ++i)
29 GraphBuffer[GraphTraceLen++] = firstbit;
30
31 for (i = 0; i <= half; ++i)
32 GraphBuffer[GraphTraceLen++] = bit;
33
34 if (redraw)
35 RepaintGraphWindow();
36 }
37
38 /* clear out our graph window */
39 int ClearGraph(int redraw)
40 {
41 int gtl = GraphTraceLen;
42 memset(GraphBuffer, 0x00, GraphTraceLen);
43
44 GraphTraceLen = 0;
45
46 if (redraw)
47 RepaintGraphWindow();
48
49 return gtl;
50 }
51
52 /*
53 * Detect clock rate
54 */
55 //decommissioned - has difficulty detecting rf/32
56 /*
57 int DetectClockOld(int peak)
58 {
59 int i;
60 int clock = 0xFFFF;
61 int lastpeak = 0;
62
63 // Detect peak if we don't have one
64 if (!peak)
65 for (i = 0; i < GraphTraceLen; ++i)
66 if (GraphBuffer[i] > peak)
67 peak = GraphBuffer[i];
68
69 // peak=(int)(peak*.75);
70 for (i = 1; i < GraphTraceLen; ++i)
71 {
72 // If this is the beginning of a peak
73 if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] >= peak)
74 {
75 // Find lowest difference between peaks
76 if (lastpeak && i - lastpeak < clock)
77 clock = i - lastpeak;
78 lastpeak = i;
79 }
80 }
81
82 return clock;
83 }
84 */
85 /*
86 NOW IN LFDEMOD.C
87
88 // by marshmellow
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)
92 {
93 int i=0;
94 int low=0;
95 int clk[]={16,32,40,50,64,100,128,256};
96 int loopCnt = 256;
97 if (GraphTraceLen<loopCnt) loopCnt = GraphTraceLen;
98 if (!peak){
99 for (i=0;i<loopCnt;++i){
100 if(GraphBuffer[i]>peak){
101 peak = GraphBuffer[i];
102 }
103 if(GraphBuffer[i]<low){
104 low = GraphBuffer[i];
105 }
106 }
107 peak=(int)(peak*.75);
108 low= (int)(low*.75);
109 }
110 int ii;
111 int clkCnt;
112 int tol = 0;
113 int bestErr=1000;
114 int errCnt[]={0,0,0,0,0,0,0,0};
115 for(clkCnt=0; clkCnt<6;++clkCnt){
116 if (clk[clkCnt]==32){
117 tol=1;
118 }else{
119 tol=0;
120 }
121 bestErr=1000;
122 for (ii=0; ii<loopCnt; ++ii){
123 if ((GraphBuffer[ii]>=peak) || (GraphBuffer[ii]<=low)){
124 errCnt[clkCnt]=0;
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
130 errCnt[clkCnt]++;
131 }
132 }
133 if(errCnt[clkCnt]==0) return clk[clkCnt];
134 if(errCnt[clkCnt]<bestErr) bestErr=errCnt[clkCnt];
135 }
136 }
137 }
138 int iii=0;
139 int best=0;
140 for (iii=0; iii<6;++iii){
141 if (errCnt[iii]<errCnt[best]){
142 best = iii;
143 }
144 }
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]);
146 return clk[best];
147 }
148 */
149 void setGraphBuf(uint8_t *buff,int size)
150 {
151 int i=0;
152 ClearGraph(0);
153 for (; i < size; ++i){
154 GraphBuffer[i]=buff[i];
155 }
156 GraphTraceLen=size;
157 RepaintGraphWindow();
158 return;
159 }
160 int getFromGraphBuf(uint8_t *buff)
161 {
162 uint32_t i;
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);
167 }
168 return i;
169 }
170 /* Get or auto-detect clock rate */
171 int GetClock(const char *str, int peak, int verbose)
172 {
173 int clock;
174
175 sscanf(str, "%i", &clock);
176 if (!strcmp(str, ""))
177 clock = 0;
178
179 /* Auto-detect clock */
180 if (!clock)
181 {
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 */
187 if (!verbose)
188 PrintAndLog("Auto-detected clock rate: %d", clock);
189 }
190
191 return clock;
192 }
193
194
195 /* A simple test to see if there is any data inside Graphbuffer.
196 */
197 bool HasGraphData(){
198
199 if ( GraphTraceLen <= 0) {
200 PrintAndLog("No data available, try reading something first");
201 return false;
202 }
203 return true;
204 }
Impressum, Datenschutz