]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/graph.c
Merge branch 'master' of https://github.com/Proxmark/proxmark3
[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,int size)
145 {
146 int i=0;
147 ClearGraph(0);
148 for (; i < size; ++i){
149 GraphBuffer[i]=buff[i];
150 }
151 GraphTraceLen=size;
152 RepaintGraphWindow();
153 return;
154 }
155 int 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 int 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 }
Impressum, Datenschutz