]> git.zerfleddert.de Git - proxmark3-svn/blob - client/graph.c
ae318ddf4334d3890ed41ad245d2655b7d104c3c
[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 /* write a manchester bit to the graph */
21 void AppendGraph(int redraw, int clock, int bit)
22 {
23 int i;
24 //set first half the clock bit (all 1's or 0's for a 0 or 1 bit)
25 for (i = 0; i < (int)(clock / 2); ++i)
26 GraphBuffer[GraphTraceLen++] = bit ;
27 //set second half of the clock bit (all 0's or 1's for a 0 or 1 bit)
28 for (i = (int)(clock / 2); i < clock; ++i)
29 GraphBuffer[GraphTraceLen++] = bit ^ 1;
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 memset(GraphBuffer, 0x00, GraphTraceLen);
40
41 GraphTraceLen = 0;
42
43 if (redraw)
44 RepaintGraphWindow();
45
46 return gtl;
47 }
48 // option '1' to save GraphBuffer any other to restore
49 void save_restoreGB(uint8_t saveOpt)
50 {
51 static int SavedGB[MAX_GRAPH_TRACE_LEN];
52 static int SavedGBlen;
53 static bool GB_Saved = false;
54
55 if (saveOpt==1) { //save
56 memcpy(SavedGB,GraphBuffer, sizeof(GraphBuffer));
57 SavedGBlen = GraphTraceLen;
58 GB_Saved=true;
59 } else if (GB_Saved){
60 memcpy(GraphBuffer,SavedGB, sizeof(GraphBuffer));
61 GraphTraceLen = SavedGBlen;
62 }
63 return;
64 }
65
66 // DETECT CLOCK NOW IN LFDEMOD.C
67
68 void setGraphBuf(uint8_t *buff, size_t size)
69 {
70 if ( buff == NULL ) return;
71
72 uint16_t i = 0;
73 if ( size > MAX_GRAPH_TRACE_LEN )
74 size = MAX_GRAPH_TRACE_LEN;
75 ClearGraph(0);
76 for (; i < size; ++i){
77 GraphBuffer[i]=buff[i]-128;
78 }
79 GraphTraceLen=size;
80 RepaintGraphWindow();
81 return;
82 }
83 size_t getFromGraphBuf(uint8_t *buff)
84 {
85 if (buff == NULL ) return 0;
86 uint32_t i;
87 for (i=0;i<GraphTraceLen;++i){
88 if (GraphBuffer[i]>127) GraphBuffer[i]=127; //trim
89 if (GraphBuffer[i]<-127) GraphBuffer[i]=-127; //trim
90 buff[i]=(uint8_t)(GraphBuffer[i]+128);
91 }
92 return i;
93 }
94
95 // A simple test to see if there is any data inside Graphbuffer.
96 bool HasGraphData(){
97
98 if ( GraphTraceLen <= 0) {
99 PrintAndLog("No data available, try reading something first");
100 return false;
101 }
102 return true;
103 }
104
105 // Detect high and lows in Grapbuffer.
106 // Only loops the first 256 values.
107 void DetectHighLowInGraph(int *high, int *low, bool addFuzz) {
108
109 uint8_t loopMax = 255;
110 if ( loopMax > GraphTraceLen)
111 loopMax = GraphTraceLen;
112
113 for (uint8_t i = 0; i < loopMax; ++i) {
114 if (GraphBuffer[i] > *high)
115 *high = GraphBuffer[i];
116 else if (GraphBuffer[i] < *low)
117 *low = GraphBuffer[i];
118 }
119
120 //12% fuzz in case highs and lows aren't clipped
121 if (addFuzz) {
122 *high = (int)(*high * .88);
123 *low = (int)(*low * .88);
124 }
125 }
126
127 // Get or auto-detect ask clock rate
128 int GetAskClock(const char str[], bool printAns, bool verbose)
129 {
130 int clock;
131 sscanf(str, "%i", &clock);
132 if (!strcmp(str, ""))
133 clock = 0;
134
135 if (clock != 0)
136 return clock;
137 // Auto-detect clock
138 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
139 size_t size = getFromGraphBuf(grph);
140 if (size == 0) {
141 if (verbose)
142 PrintAndLog("Failed to copy from graphbuffer");
143 return -1;
144 }
145 DetectASKClock(grph, size, &clock, 20);
146 // Only print this message if we're not looping something
147 if (printAns){
148 PrintAndLog("Auto-detected clock rate: %d", clock);
149 }
150 return clock;
151 }
152
153 uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose)
154 {
155 uint8_t carrier=0;
156 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
157 size_t size = getFromGraphBuf(grph);
158 if ( size == 0 ) {
159 if (verbose)
160 PrintAndLog("Failed to copy from graphbuffer");
161 return 0;
162 }
163 //uint8_t countPSK_FC(uint8_t *BitStream, size_t size)
164
165 carrier = countFC(grph,size,0);
166 // Only print this message if we're not looping something
167 if (printAns){
168 PrintAndLog("Auto-detected PSK carrier rate: %d", carrier);
169 }
170 return carrier;
171 }
172
173 int GetPskClock(const char str[], bool printAns, bool verbose)
174 {
175 int clock;
176 sscanf(str, "%i", &clock);
177 if (!strcmp(str, ""))
178 clock = 0;
179
180 if (clock!=0)
181 return clock;
182 // Auto-detect clock
183 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
184 size_t size = getFromGraphBuf(grph);
185 if ( size == 0 ) {
186 if (verbose)
187 PrintAndLog("Failed to copy from graphbuffer");
188 return -1;
189 }
190 clock = DetectPSKClock(grph,size,0);
191 // Only print this message if we're not looping something
192 if (printAns){
193 PrintAndLog("Auto-detected clock rate: %d", clock);
194 }
195 return clock;
196 }
197
198 uint8_t GetNrzClock(const char str[], bool printAns, bool verbose)
199 {
200 int clock;
201 sscanf(str, "%i", &clock);
202 if (!strcmp(str, ""))
203 clock = 0;
204
205 if (clock!=0)
206 return clock;
207 // Auto-detect clock
208 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
209 size_t size = getFromGraphBuf(grph);
210 if ( size == 0 ) {
211 if (verbose)
212 PrintAndLog("Failed to copy from graphbuffer");
213 return -1;
214 }
215 clock = DetectNRZClock(grph, size, 0);
216 // Only print this message if we're not looping something
217 if (printAns){
218 PrintAndLog("Auto-detected clock rate: %d", clock);
219 }
220 return clock;
221 }
222 //by marshmellow
223 //attempt to detect the field clock and bit clock for FSK
224 uint8_t GetFskClock(const char str[], bool printAns, bool verbose)
225 {
226 int clock;
227 sscanf(str, "%i", &clock);
228 if (!strcmp(str, ""))
229 clock = 0;
230 if (clock != 0) return (uint8_t)clock;
231
232
233 uint8_t fc1=0, fc2=0, rf1=0;
234 uint8_t ans = fskClocks(&fc1, &fc2, &rf1, verbose);
235 if (ans == 0) return 0;
236 if ((fc1==10 && fc2==8) || (fc1==8 && fc2==5)){
237 if (printAns) PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
238 return rf1;
239 }
240 if (verbose){
241 PrintAndLog("DEBUG: unknown fsk field clock detected");
242 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
243 }
244 return 0;
245 }
246 uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose)
247 {
248 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
249 size_t size = getFromGraphBuf(BitStream);
250 if (size==0) return 0;
251 uint16_t ans = countFC(BitStream, size, 1);
252 if (ans==0) {
253 if (verbose) PrintAndLog("DEBUG: No data found");
254 return 0;
255 }
256 *fc1 = (ans >> 8) & 0xFF;
257 *fc2 = ans & 0xFF;
258
259 *rf1 = detectFSKClk(BitStream, size, *fc1, *fc2);
260 if (*rf1==0) {
261 if (verbose) PrintAndLog("DEBUG: Clock detect error");
262 return 0;
263 }
264 return 1;
265 }
Impressum, Datenschutz