]> git.zerfleddert.de Git - proxmark3-svn/blame - client/graph.c
Merge branch 'graphwork2' into graphwork
[proxmark3-svn] / client / graph.c
CommitLineData
a553f267 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
7fe9b0b7 11#include <stdio.h>
a1557c4c 12#include <stdbool.h>
7fe9b0b7 13#include <string.h>
14#include "ui.h"
15#include "graph.h"
d5a72d2f 16#include "lfdemod.h"
2d99d991 17#include "cmddata.h" //for g_debugmode
7fe9b0b7 18
19int GraphBuffer[MAX_GRAPH_TRACE_LEN];
20int GraphTraceLen;
b8fdac9e 21
22int s_Buff[MAX_GRAPH_TRACE_LEN];
23
abd6112f 24/* write a manchester bit to the graph */
7fe9b0b7 25void AppendGraph(int redraw, int clock, int bit)
26{
27 int i;
abd6112f 28 //set first half the clock bit (all 1's or 0's for a 0 or 1 bit)
7fe9b0b7 29 for (i = 0; i < (int)(clock / 2); ++i)
78f5b1a7 30 GraphBuffer[GraphTraceLen++] = bit ;
abd6112f 31 //set second half of the clock bit (all 0's or 1's for a 0 or 1 bit)
7fe9b0b7 32 for (i = (int)(clock / 2); i < clock; ++i)
78f5b1a7 33 GraphBuffer[GraphTraceLen++] = bit ^ 1;
7fe9b0b7 34
35 if (redraw)
36 RepaintGraphWindow();
37}
38
c12512e9 39// clear out our graph window
7fe9b0b7 40int ClearGraph(int redraw)
41{
42 int gtl = GraphTraceLen;
52ab55ab 43 memset(GraphBuffer, 0x00, GraphTraceLen);
44
7fe9b0b7 45 GraphTraceLen = 0;
46
47 if (redraw)
48 RepaintGraphWindow();
49
50 return gtl;
51}
23f0a7d8 52// option '1' to save GraphBuffer any other to restore
53void 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
2767fc02 60 memcpy(SavedGB, GraphBuffer, sizeof(GraphBuffer));
23f0a7d8 61 SavedGBlen = GraphTraceLen;
62 GB_Saved=true;
49bbc60a 63 } else if (GB_Saved){ //restore
2767fc02 64 memcpy(GraphBuffer, SavedGB, sizeof(GraphBuffer));
23f0a7d8 65 GraphTraceLen = SavedGBlen;
49bbc60a 66 RepaintGraphWindow();
23f0a7d8 67 }
68 return;
69}
7fe9b0b7 70
c12512e9 71// DETECT CLOCK NOW IN LFDEMOD.C
7fe9b0b7 72
ba1a299c 73void setGraphBuf(uint8_t *buff, size_t size)
d5a72d2f 74{
a1557c4c 75 if ( buff == NULL ) return;
76
77 uint16_t i = 0;
78 if ( size > MAX_GRAPH_TRACE_LEN )
79 size = MAX_GRAPH_TRACE_LEN;
b4fb11ba 80 ClearGraph(0);
81 for (; i < size; ++i){
ba1a299c 82 GraphBuffer[i]=buff[i]-128;
b4fb11ba 83 }
84 GraphTraceLen=size;
85 RepaintGraphWindow();
86 return;
d5a72d2f 87}
ba1a299c 88size_t getFromGraphBuf(uint8_t *buff)
d5a72d2f 89{
e770c648 90 if (buff == NULL ) return 0;
b4fb11ba 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;
d5a72d2f 98}
ec75f5c1 99
a1557c4c 100// A simple test to see if there is any data inside Graphbuffer.
101bool HasGraphData(){
7fe9b0b7 102
a1557c4c 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.
112void 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 }
7fe9b0b7 130}
ba1a299c 131
f3bf15e4 132// Get or auto-detect ask clock rate
133int GetAskClock(const char str[], bool printAns, bool verbose)
e770c648 134{
135 int clock;
136 sscanf(str, "%i", &clock);
137 if (!strcmp(str, ""))
138 clock = 0;
139
f3bf15e4 140 if (clock != 0)
141 return clock;
e770c648 142 // Auto-detect clock
f3bf15e4 143 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
144 size_t size = getFromGraphBuf(grph);
145 if (size == 0) {
146 if (verbose)
e770c648 147 PrintAndLog("Failed to copy from graphbuffer");
f3bf15e4 148 return -1;
149 }
0f321d63 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;
b66ff081 154 if (st == false) {
155 start = DetectASKClock(grph, size, &clock, 20);
156 }
0f321d63 157 setClockGrid(clock, start);
f3bf15e4 158 // Only print this message if we're not looping something
bf85d22f 159 if (printAns || g_debugMode) {
fef74fdc 160 PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock, start);
e770c648 161 }
162 return clock;
163}
164
872e3d4d 165uint8_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 }
b97311b1 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;
872e3d4d 179 // Only print this message if we're not looping something
b97311b1 180 if (printAns) {
872e3d4d 181 PrintAndLog("Auto-detected PSK carrier rate: %d", carrier);
182 }
183 return carrier;
184}
185
f3bf15e4 186int 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 }
b97311b1 203 size_t firstPhaseShiftLoc = 0;
204 uint8_t curPhase = 0, fc = 0;
205 clock = DetectPSKClock(grph, size, 0, &firstPhaseShiftLoc, &curPhase, &fc);
0f321d63 206 setClockGrid(clock, firstPhaseShiftLoc);
f3bf15e4 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
214uint8_t GetNrzClock(const char str[], bool printAns, bool verbose)
4118b74d 215{
ba1a299c 216 int clock;
ba1a299c 217 sscanf(str, "%i", &clock);
218 if (!strcmp(str, ""))
219 clock = 0;
220
f3bf15e4 221 if (clock!=0)
222 return clock;
ba1a299c 223 // Auto-detect clock
f3bf15e4 224 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
225 size_t size = getFromGraphBuf(grph);
226 if ( size == 0 ) {
227 if (verbose)
e629181f 228 PrintAndLog("Failed to copy from graphbuffer");
f3bf15e4 229 return -1;
230 }
6f36848f 231 size_t clkStartIdx = 0;
232 clock = DetectNRZClock(grph, size, 0, &clkStartIdx);
0f321d63 233 setClockGrid(clock, clkStartIdx);
f3bf15e4 234 // Only print this message if we're not looping something
235 if (printAns){
236 PrintAndLog("Auto-detected clock rate: %d", clock);
ba1a299c 237 }
238 return clock;
4118b74d 239}
f3bf15e4 240//by marshmellow
241//attempt to detect the field clock and bit clock for FSK
242uint8_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
abd6112f 250
251 uint8_t fc1=0, fc2=0, rf1=0;
0f321d63 252 int firstClockEdge = 0;
253 uint8_t ans = fskClocks(&fc1, &fc2, &rf1, verbose, &firstClockEdge);
abd6112f 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);
0f321d63 257 setClockGrid(rf1, firstClockEdge);
abd6112f 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}
0f321d63 266uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose, int *firstClockEdge)
abd6112f 267{
f3bf15e4 268 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
269 size_t size = getFromGraphBuf(BitStream);
270 if (size==0) return 0;
2eec55c8 271 uint16_t ans = countFC(BitStream, size, 1);
f3bf15e4 272 if (ans==0) {
2d99d991 273 if (verbose || g_debugMode) PrintAndLog("DEBUG: No data found");
f3bf15e4 274 return 0;
275 }
abd6112f 276 *fc1 = (ans >> 8) & 0xFF;
277 *fc2 = ans & 0xFF;
0f321d63 278 //int firstClockEdge = 0;
279 *rf1 = detectFSKClk(BitStream, size, *fc1, *fc2, firstClockEdge);
abd6112f 280 if (*rf1==0) {
2d99d991 281 if (verbose || g_debugMode) PrintAndLog("DEBUG: Clock detect error");
f3bf15e4 282 return 0;
283 }
abd6112f 284 return 1;
f3bf15e4 285}
38cb7c71 286bool graphJustNoise(int *BitStream, int size)
287{
e04475c4 288 static const uint8_t THRESHOLD = 15; //might not be high enough for noisy environments
38cb7c71 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