]> git.zerfleddert.de Git - proxmark3-svn/blame - client/graph.c
FIX: moved from lfdemod.c -> graph.c SetGraphClock.
[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"
7fe9b0b7 17
18int GraphBuffer[MAX_GRAPH_TRACE_LEN];
19int GraphTraceLen;
abd6112f 20/* write a manchester bit to the graph */
7fe9b0b7 21void AppendGraph(int redraw, int clock, int bit)
22{
c6e5c7ea 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;
7fe9b0b7 30
c6e5c7ea 31 if (redraw)
32 RepaintGraphWindow();
7fe9b0b7 33}
34
c12512e9 35// clear out our graph window
7fe9b0b7 36int ClearGraph(int redraw)
37{
c6e5c7ea 38 int gtl = GraphTraceLen;
39 memset(GraphBuffer, 0x00, GraphTraceLen);
40 GraphTraceLen = 0;
41 if (redraw)
42 RepaintGraphWindow();
43 return gtl;
7fe9b0b7 44}
23f0a7d8 45// option '1' to save GraphBuffer any other to restore
46void 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
2767fc02 53 memcpy(SavedGB, GraphBuffer, sizeof(GraphBuffer));
23f0a7d8 54 SavedGBlen = GraphTraceLen;
55 GB_Saved=true;
49bbc60a 56 } else if (GB_Saved){ //restore
2767fc02 57 memcpy(GraphBuffer, SavedGB, sizeof(GraphBuffer));
23f0a7d8 58 GraphTraceLen = SavedGBlen;
49bbc60a 59 RepaintGraphWindow();
23f0a7d8 60 }
61 return;
62}
7fe9b0b7 63
c12512e9 64// DETECT CLOCK NOW IN LFDEMOD.C
7fe9b0b7 65
ba1a299c 66void setGraphBuf(uint8_t *buff, size_t size)
d5a72d2f 67{
a1557c4c 68 if ( buff == NULL ) return;
69
c6e5c7ea 70 ClearGraph(0);
71
a1557c4c 72 if ( size > MAX_GRAPH_TRACE_LEN )
73 size = MAX_GRAPH_TRACE_LEN;
c6e5c7ea 74
75 for (uint16_t i = 0; i < size; ++i)
76 GraphBuffer[i] = buff[i] - 128;
77
78 GraphTraceLen = size;
b4fb11ba 79 RepaintGraphWindow();
80 return;
d5a72d2f 81}
ba1a299c 82size_t getFromGraphBuf(uint8_t *buff)
d5a72d2f 83{
e770c648 84 if (buff == NULL ) return 0;
b4fb11ba 85 uint32_t i;
c6e5c7ea 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);
b4fb11ba 90 }
91 return i;
d5a72d2f 92}
ec75f5c1 93
a1557c4c 94// A simple test to see if there is any data inside Graphbuffer.
95bool HasGraphData(){
7fe9b0b7 96
a1557c4c 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.
106void 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 }
7fe9b0b7 124}
ba1a299c 125
1ec412d9 126void SetGraphClock( int clock, int startidx){
127 PlotClock = clock;
128 PlockClockStartIndex = startidx;
129}
130
f3bf15e4 131// Get or auto-detect ask clock rate
132int GetAskClock(const char str[], bool printAns, bool verbose)
e770c648 133{
134 int clock;
135 sscanf(str, "%i", &clock);
136 if (!strcmp(str, ""))
137 clock = 0;
138
c6e5c7ea 139 if (clock != 0) return clock;
140
e770c648 141 // Auto-detect clock
f3bf15e4 142 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
143 size_t size = getFromGraphBuf(grph);
144 if (size == 0) {
145 if (verbose)
e770c648 146 PrintAndLog("Failed to copy from graphbuffer");
f3bf15e4 147 return -1;
148 }
c0f15a05 149 bool st = DetectST(grph, &size, &clock);
150 int start = 0;
c6e5c7ea 151 if (st == false)
c0f15a05 152 start = DetectASKClock(grph, size, &clock, 20);
c6e5c7ea 153
f3bf15e4 154 // Only print this message if we're not looping something
c6e5c7ea 155 if (printAns)
fef74fdc 156 PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock, start);
1ec412d9 157 SetGraphClock(clock, start);
e770c648 158 return clock;
159}
160
872e3d4d 161uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose)
162{
a47ded5b 163 uint8_t carrier = 0;
164 uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0};
872e3d4d 165 size_t size = getFromGraphBuf(grph);
166 if ( size == 0 ) {
167 if (verbose)
168 PrintAndLog("Failed to copy from graphbuffer");
169 return 0;
170 }
a47ded5b 171 carrier = countFC(grph, size, 0);
872e3d4d 172 // Only print this message if we're not looping something
a47ded5b 173 if (printAns)
872e3d4d 174 PrintAndLog("Auto-detected PSK carrier rate: %d", carrier);
a47ded5b 175
872e3d4d 176 return carrier;
177}
178
f3bf15e4 179int GetPskClock(const char str[], bool printAns, bool verbose)
180{
181 int clock;
182 sscanf(str, "%i", &clock);
183 if (!strcmp(str, ""))
184 clock = 0;
185
a47ded5b 186 if (clock != 0) return clock;
f3bf15e4 187 // Auto-detect clock
a47ded5b 188 uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0};
f3bf15e4 189 size_t size = getFromGraphBuf(grph);
190 if ( size == 0 ) {
1a4b9073 191 if (verbose) PrintAndLog("Failed to copy from graphbuffer");
f3bf15e4 192 return -1;
193 }
a47ded5b 194 clock = DetectPSKClock(grph, size, 0);
f3bf15e4 195 // Only print this message if we're not looping something
1a4b9073 196 if (printAns) PrintAndLog("Auto-detected clock rate: %d", clock);
f3bf15e4 197 return clock;
198}
199
200uint8_t GetNrzClock(const char str[], bool printAns, bool verbose)
4118b74d 201{
ba1a299c 202 int clock;
ba1a299c 203 sscanf(str, "%i", &clock);
204 if (!strcmp(str, ""))
205 clock = 0;
206
a47ded5b 207 if (clock != 0)
f3bf15e4 208 return clock;
ba1a299c 209 // Auto-detect clock
a47ded5b 210 uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0};
f3bf15e4 211 size_t size = getFromGraphBuf(grph);
212 if ( size == 0 ) {
213 if (verbose)
e629181f 214 PrintAndLog("Failed to copy from graphbuffer");
f3bf15e4 215 return -1;
216 }
217 clock = DetectNRZClock(grph, size, 0);
218 // Only print this message if we're not looping something
a47ded5b 219 if (printAns)
f3bf15e4 220 PrintAndLog("Auto-detected clock rate: %d", clock);
a47ded5b 221
ba1a299c 222 return clock;
4118b74d 223}
f3bf15e4 224//by marshmellow
225//attempt to detect the field clock and bit clock for FSK
226uint8_t GetFskClock(const char str[], bool printAns, bool verbose)
227{
228 int clock;
229 sscanf(str, "%i", &clock);
230 if (!strcmp(str, ""))
231 clock = 0;
232 if (clock != 0) return (uint8_t)clock;
233
abd6112f 234
235 uint8_t fc1=0, fc2=0, rf1=0;
236 uint8_t ans = fskClocks(&fc1, &fc2, &rf1, verbose);
237 if (ans == 0) return 0;
238 if ((fc1==10 && fc2==8) || (fc1==8 && fc2==5)){
239 if (printAns) PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
240 return rf1;
241 }
242 if (verbose){
243 PrintAndLog("DEBUG: unknown fsk field clock detected");
244 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
245 }
246 return 0;
247}
248uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose)
249{
a47ded5b 250 uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0};
f3bf15e4 251 size_t size = getFromGraphBuf(BitStream);
a47ded5b 252 if (size == 0) return 0;
2eec55c8 253 uint16_t ans = countFC(BitStream, size, 1);
a47ded5b 254 if (ans == 0) {
1a4b9073 255 if (verbose || g_debugMode) PrintAndLog("DEBUG: No data found");
f3bf15e4 256 return 0;
257 }
abd6112f 258 *fc1 = (ans >> 8) & 0xFF;
259 *fc2 = ans & 0xFF;
f3bf15e4 260
abd6112f 261 *rf1 = detectFSKClk(BitStream, size, *fc1, *fc2);
a47ded5b 262 if (*rf1 == 0) {
1a4b9073 263 if (verbose || g_debugMode) PrintAndLog("DEBUG: Clock detect error");
f3bf15e4 264 return 0;
265 }
abd6112f 266 return 1;
f3bf15e4 267}
3f84d473 268
269// test samples are not just noise
270bool graphJustNoise(int *BitStream, int size)
271{
272 //might not be high enough for noisy environments
273 #define THRESHOLD 15;
274
275 bool isNoise = TRUE;
276 for(int i=0; i < size && isNoise; i++){
277 isNoise = BitStream[i] < THRESHOLD;
278 }
279 return isNoise;
280}
Impressum, Datenschutz