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