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