]> git.zerfleddert.de Git - proxmark3-svn/blame - client/graph.c
CHG: `data plot`- the marking of clock, looks better without borders. It only connec...
[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
f3bf15e4 126// Get or auto-detect ask clock rate
127int GetAskClock(const char str[], bool printAns, bool verbose)
e770c648 128{
129 int clock;
130 sscanf(str, "%i", &clock);
131 if (!strcmp(str, ""))
132 clock = 0;
133
c6e5c7ea 134 if (clock != 0) return clock;
135
e770c648 136 // Auto-detect clock
f3bf15e4 137 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
138 size_t size = getFromGraphBuf(grph);
139 if (size == 0) {
140 if (verbose)
e770c648 141 PrintAndLog("Failed to copy from graphbuffer");
f3bf15e4 142 return -1;
143 }
c0f15a05 144 bool st = DetectST(grph, &size, &clock);
145 int start = 0;
c6e5c7ea 146 if (st == false)
c0f15a05 147 start = DetectASKClock(grph, size, &clock, 20);
c6e5c7ea 148
f3bf15e4 149 // Only print this message if we're not looping something
c6e5c7ea 150 if (printAns)
fef74fdc 151 PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock, start);
e770c648 152 return clock;
153}
154
872e3d4d 155uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose)
156{
a47ded5b 157 uint8_t carrier = 0;
158 uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0};
872e3d4d 159 size_t size = getFromGraphBuf(grph);
160 if ( size == 0 ) {
161 if (verbose)
162 PrintAndLog("Failed to copy from graphbuffer");
163 return 0;
164 }
a47ded5b 165 carrier = countFC(grph, size, 0);
872e3d4d 166 // Only print this message if we're not looping something
a47ded5b 167 if (printAns)
872e3d4d 168 PrintAndLog("Auto-detected PSK carrier rate: %d", carrier);
a47ded5b 169
872e3d4d 170 return carrier;
171}
172
f3bf15e4 173int 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
a47ded5b 180 if (clock != 0) return clock;
f3bf15e4 181 // Auto-detect clock
a47ded5b 182 uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0};
f3bf15e4 183 size_t size = getFromGraphBuf(grph);
184 if ( size == 0 ) {
1a4b9073 185 if (verbose) PrintAndLog("Failed to copy from graphbuffer");
f3bf15e4 186 return -1;
187 }
a47ded5b 188 clock = DetectPSKClock(grph, size, 0);
f3bf15e4 189 // Only print this message if we're not looping something
1a4b9073 190 if (printAns) PrintAndLog("Auto-detected clock rate: %d", clock);
f3bf15e4 191 return clock;
192}
193
194uint8_t GetNrzClock(const char str[], bool printAns, bool verbose)
4118b74d 195{
ba1a299c 196 int clock;
ba1a299c 197 sscanf(str, "%i", &clock);
198 if (!strcmp(str, ""))
199 clock = 0;
200
a47ded5b 201 if (clock != 0)
f3bf15e4 202 return clock;
ba1a299c 203 // Auto-detect clock
a47ded5b 204 uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0};
f3bf15e4 205 size_t size = getFromGraphBuf(grph);
206 if ( size == 0 ) {
207 if (verbose)
e629181f 208 PrintAndLog("Failed to copy from graphbuffer");
f3bf15e4 209 return -1;
210 }
211 clock = DetectNRZClock(grph, size, 0);
212 // Only print this message if we're not looping something
a47ded5b 213 if (printAns)
f3bf15e4 214 PrintAndLog("Auto-detected clock rate: %d", clock);
a47ded5b 215
ba1a299c 216 return clock;
4118b74d 217}
f3bf15e4 218//by marshmellow
219//attempt to detect the field clock and bit clock for FSK
220uint8_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
abd6112f 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}
242uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose)
243{
a47ded5b 244 uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0};
f3bf15e4 245 size_t size = getFromGraphBuf(BitStream);
a47ded5b 246 if (size == 0) return 0;
2eec55c8 247 uint16_t ans = countFC(BitStream, size, 1);
a47ded5b 248 if (ans == 0) {
1a4b9073 249 if (verbose || g_debugMode) PrintAndLog("DEBUG: No data found");
f3bf15e4 250 return 0;
251 }
abd6112f 252 *fc1 = (ans >> 8) & 0xFF;
253 *fc2 = ans & 0xFF;
f3bf15e4 254
abd6112f 255 *rf1 = detectFSKClk(BitStream, size, *fc1, *fc2);
a47ded5b 256 if (*rf1 == 0) {
1a4b9073 257 if (verbose || g_debugMode) PrintAndLog("DEBUG: Clock detect error");
f3bf15e4 258 return 0;
259 }
abd6112f 260 return 1;
f3bf15e4 261}
3f84d473 262
263// test samples are not just noise
264bool 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