]> git.zerfleddert.de Git - proxmark3-svn/blob - client/graph.c
f41568e4f57c887b3c3a6bc304aecb5ebd8d89bb
[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 <string.h>
13 #include "ui.h"
14 #include "graph.h"
15 #include "lfdemod.h"
16
17 int GraphBuffer[MAX_GRAPH_TRACE_LEN];
18 int GraphTraceLen;
19
20 /* write a bit to the graph */
21 void AppendGraph(int redraw, int clock, int bit)
22 {
23 int i;
24
25 for (i = 0; i < (int)(clock / 2); ++i)
26 GraphBuffer[GraphTraceLen++] = bit ^ 1;
27
28 for (i = (int)(clock / 2); i < clock; ++i)
29 GraphBuffer[GraphTraceLen++] = bit;
30
31 if (redraw)
32 RepaintGraphWindow();
33 }
34
35 /* clear out our graph window */
36 int ClearGraph(int redraw)
37 {
38 int gtl = GraphTraceLen;
39 GraphTraceLen = 0;
40
41 if (redraw)
42 RepaintGraphWindow();
43
44 return gtl;
45 }
46
47 /*
48 * Detect clock rate
49 */
50 //decommissioned - has difficulty detecting rf/32
51 /*
52 int DetectClockOld(int peak)
53 {
54 int i;
55 int clock = 0xFFFF;
56 int lastpeak = 0;
57
58 // Detect peak if we don't have one
59 if (!peak)
60 for (i = 0; i < GraphTraceLen; ++i)
61 if (GraphBuffer[i] > peak)
62 peak = GraphBuffer[i];
63
64 // peak=(int)(peak*.75);
65 for (i = 1; i < GraphTraceLen; ++i)
66 {
67 // If this is the beginning of a peak
68 if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] >= peak)
69 {
70 // Find lowest difference between peaks
71 if (lastpeak && i - lastpeak < clock)
72 clock = i - lastpeak;
73 lastpeak = i;
74 }
75 }
76
77 return clock;
78 }
79 */
80 /*
81 NOW IN LFDEMOD.C
82
83 // by marshmellow
84 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
85 // maybe somehow adjust peak trimming value based on samples to fix?
86 int DetectASKClock(int peak)
87 {
88 int i=0;
89 int low=0;
90 int clk[]={16,32,40,50,64,100,128,256};
91 int loopCnt = 256;
92 if (GraphTraceLen<loopCnt) loopCnt = GraphTraceLen;
93 if (!peak){
94 for (i=0;i<loopCnt;++i){
95 if(GraphBuffer[i]>peak){
96 peak = GraphBuffer[i];
97 }
98 if(GraphBuffer[i]<low){
99 low = GraphBuffer[i];
100 }
101 }
102 peak=(int)(peak*.75);
103 low= (int)(low*.75);
104 }
105 int ii;
106 int clkCnt;
107 int tol = 0;
108 int bestErr=1000;
109 int errCnt[]={0,0,0,0,0,0,0,0};
110 for(clkCnt=0; clkCnt<6;++clkCnt){
111 if (clk[clkCnt]==32){
112 tol=1;
113 }else{
114 tol=0;
115 }
116 bestErr=1000;
117 for (ii=0; ii<loopCnt; ++ii){
118 if ((GraphBuffer[ii]>=peak) || (GraphBuffer[ii]<=low)){
119 errCnt[clkCnt]=0;
120 for (i=0; i<((int)(GraphTraceLen/clk[clkCnt])-1); ++i){
121 if (GraphBuffer[ii+(i*clk[clkCnt])]>=peak || GraphBuffer[ii+(i*clk[clkCnt])]<=low){
122 }else if(GraphBuffer[ii+(i*clk[clkCnt])-tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])-tol]<=low){
123 }else if(GraphBuffer[ii+(i*clk[clkCnt])+tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])+tol]<=low){
124 }else{ //error no peak detected
125 errCnt[clkCnt]++;
126 }
127 }
128 if(errCnt[clkCnt]==0) return clk[clkCnt];
129 if(errCnt[clkCnt]<bestErr) bestErr=errCnt[clkCnt];
130 }
131 }
132 }
133 int iii=0;
134 int best=0;
135 for (iii=0; iii<6;++iii){
136 if (errCnt[iii]<errCnt[best]){
137 best = iii;
138 }
139 }
140 // 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]);
141 return clk[best];
142 }
143 */
144 void setGraphBuf(uint8_t *buff, size_t size)
145 {
146 int i=0;
147 ClearGraph(0);
148 for (; i < size; ++i){
149 GraphBuffer[i]=buff[i]-128;
150 }
151 GraphTraceLen=size;
152 RepaintGraphWindow();
153 return;
154 }
155 size_t getFromGraphBuf(uint8_t *buff)
156 {
157 uint32_t i;
158 for (i=0;i<GraphTraceLen;++i){
159 if (GraphBuffer[i]>127) GraphBuffer[i]=127; //trim
160 if (GraphBuffer[i]<-127) GraphBuffer[i]=-127; //trim
161 buff[i]=(uint8_t)(GraphBuffer[i]+128);
162 }
163 return i;
164 }
165 /* Get or auto-detect clock rate */
166 int GetClock(const char *str, int peak, int verbose)
167 {
168 int clock;
169 // int clock2;
170 sscanf(str, "%i", &clock);
171 if (!strcmp(str, ""))
172 clock = 0;
173
174 /* Auto-detect clock */
175 if (!clock)
176 {
177 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
178 size_t size = getFromGraphBuf(grph);
179 clock = DetectASKClock(grph,size,0);
180 //clock2 = DetectClock2(peak);
181 /* Only print this message if we're not looping something */
182 if (!verbose){
183 PrintAndLog("Auto-detected clock rate: %d", clock);
184 //PrintAndLog("clock2: %d",clock2);
185 }
186 }
187
188 return clock;
189 }
190 int GetNRZpskClock(const char *str, int peak, int verbose)
191 {
192 // return GetClock(str,peak,verbose);
193 int clock;
194 // int clock2;
195 sscanf(str, "%i", &clock);
196 if (!strcmp(str, ""))
197 clock = 0;
198
199 /* Auto-detect clock */
200 if (!clock)
201 {
202 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
203 size_t size = getFromGraphBuf(grph);
204 clock = DetectpskNRZClock(grph,size,0);
205 //clock2 = DetectClock2(peak);
206 /* Only print this message if we're not looping something */
207 if (!verbose){
208 PrintAndLog("Auto-detected clock rate: %d", clock);
209 //PrintAndLog("clock2: %d",clock2);
210 }
211 }
212 return clock;
213 }
214 // Get or auto-detect clock rate
215 /*
216 int GetNRZpskClock(const char *str, int peak, int verbose)
217 {
218 int clock;
219 // int clock2;
220 sscanf(str, "%i", &clock);
221 if (!strcmp(str, ""))
222 clock = 0;
223
224 // Auto-detect clock
225 if (!clock)
226 {
227 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
228 int size = getFromGraphBuf(grph);
229 clock = DetectASKClock(grph,size,0);
230 //clock2 = DetectClock2(peak);
231 // Only print this message if we're not looping something
232 if (!verbose){
233 PrintAndLog("Auto-detected clock rate: %d", clock);
234 //PrintAndLog("clock2: %d",clock2);
235 }
236 }
237 return clock;
238 }
239 */
Impressum, Datenschutz