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