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