]> git.zerfleddert.de Git - proxmark3-svn/blob - client/graph.c
CHG: `data plot`- the marking of clock, looks better without borders. It only connec...
[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 // Get or auto-detect ask clock rate
127 int GetAskClock(const char str[], bool printAns, bool verbose)
128 {
129 int clock;
130 sscanf(str, "%i", &clock);
131 if (!strcmp(str, ""))
132 clock = 0;
133
134 if (clock != 0) return clock;
135
136 // Auto-detect clock
137 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
138 size_t size = getFromGraphBuf(grph);
139 if (size == 0) {
140 if (verbose)
141 PrintAndLog("Failed to copy from graphbuffer");
142 return -1;
143 }
144 bool st = DetectST(grph, &size, &clock);
145 int start = 0;
146 if (st == false)
147 start = DetectASKClock(grph, size, &clock, 20);
148
149 // Only print this message if we're not looping something
150 if (printAns)
151 PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock, start);
152 return clock;
153 }
154
155 uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose)
156 {
157 uint8_t carrier = 0;
158 uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0};
159 size_t size = getFromGraphBuf(grph);
160 if ( size == 0 ) {
161 if (verbose)
162 PrintAndLog("Failed to copy from graphbuffer");
163 return 0;
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) return clock;
181 // Auto-detect clock
182 uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0};
183 size_t size = getFromGraphBuf(grph);
184 if ( size == 0 ) {
185 if (verbose) PrintAndLog("Failed to copy from graphbuffer");
186 return -1;
187 }
188 clock = DetectPSKClock(grph, size, 0);
189 // Only print this message if we're not looping something
190 if (printAns) PrintAndLog("Auto-detected clock rate: %d", clock);
191 return clock;
192 }
193
194 uint8_t GetNrzClock(const char str[], bool printAns, bool verbose)
195 {
196 int clock;
197 sscanf(str, "%i", &clock);
198 if (!strcmp(str, ""))
199 clock = 0;
200
201 if (clock != 0)
202 return clock;
203 // Auto-detect clock
204 uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0};
205 size_t size = getFromGraphBuf(grph);
206 if ( size == 0 ) {
207 if (verbose)
208 PrintAndLog("Failed to copy from graphbuffer");
209 return -1;
210 }
211 clock = DetectNRZClock(grph, size, 0);
212 // Only print this message if we're not looping something
213 if (printAns)
214 PrintAndLog("Auto-detected clock rate: %d", clock);
215
216 return clock;
217 }
218 //by marshmellow
219 //attempt to detect the field clock and bit clock for FSK
220 uint8_t GetFskClock(const char str[], bool printAns, bool verbose)
221 {
222 int clock;
223 sscanf(str, "%i", &clock);
224 if (!strcmp(str, ""))
225 clock = 0;
226 if (clock != 0) return (uint8_t)clock;
227
228
229 uint8_t fc1=0, fc2=0, rf1=0;
230 uint8_t ans = fskClocks(&fc1, &fc2, &rf1, verbose);
231 if (ans == 0) return 0;
232 if ((fc1==10 && fc2==8) || (fc1==8 && fc2==5)){
233 if (printAns) PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
234 return rf1;
235 }
236 if (verbose){
237 PrintAndLog("DEBUG: unknown fsk field clock detected");
238 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
239 }
240 return 0;
241 }
242 uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose)
243 {
244 uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0};
245 size_t size = getFromGraphBuf(BitStream);
246 if (size == 0) return 0;
247 uint16_t ans = countFC(BitStream, size, 1);
248 if (ans == 0) {
249 if (verbose || g_debugMode) PrintAndLog("DEBUG: No data found");
250 return 0;
251 }
252 *fc1 = (ans >> 8) & 0xFF;
253 *fc2 = ans & 0xFF;
254
255 *rf1 = detectFSKClk(BitStream, size, *fc1, *fc2);
256 if (*rf1 == 0) {
257 if (verbose || g_debugMode) PrintAndLog("DEBUG: Clock detect error");
258 return 0;
259 }
260 return 1;
261 }
262
263 // test samples are not just noise
264 bool graphJustNoise(int *BitStream, int size)
265 {
266 //might not be high enough for noisy environments
267 #define THRESHOLD 15;
268
269 bool isNoise = TRUE;
270 for(int i=0; i < size && isNoise; i++){
271 isNoise = BitStream[i] < THRESHOLD;
272 }
273 return isNoise;
274 }
Impressum, Datenschutz