]> git.zerfleddert.de Git - proxmark3-svn/blame - client/graph.c
chg: clock marking for ask/fsk/psk, using @marshmellow42 's addition to get starti...
[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
ba1a299c 65void setGraphBuf(uint8_t *buff, size_t size)
d5a72d2f 66{
a1557c4c 67 if ( buff == NULL ) return;
68
c6e5c7ea 69 ClearGraph(0);
70
a1557c4c 71 if ( size > MAX_GRAPH_TRACE_LEN )
72 size = MAX_GRAPH_TRACE_LEN;
c6e5c7ea 73
74 for (uint16_t i = 0; i < size; ++i)
75 GraphBuffer[i] = buff[i] - 128;
76
77 GraphTraceLen = size;
b4fb11ba 78 RepaintGraphWindow();
79 return;
d5a72d2f 80}
ba1a299c 81size_t getFromGraphBuf(uint8_t *buff)
d5a72d2f 82{
e770c648 83 if (buff == NULL ) return 0;
b4fb11ba 84 uint32_t i;
c6e5c7ea 85 for (i=0; i < GraphTraceLen; ++i){
86 if (GraphBuffer[i] > 127) GraphBuffer[i] = 127; //trim
87 if (GraphBuffer[i] < -127) GraphBuffer[i] = -127; //trim
88 buff[i] = (uint8_t)(GraphBuffer[i]+128);
b4fb11ba 89 }
90 return i;
d5a72d2f 91}
ec75f5c1 92
a1557c4c 93// A simple test to see if there is any data inside Graphbuffer.
94bool HasGraphData(){
7fe9b0b7 95
a1557c4c 96 if ( GraphTraceLen <= 0) {
97 PrintAndLog("No data available, try reading something first");
98 return false;
99 }
100 return true;
101}
102
103// Detect high and lows in Grapbuffer.
104// Only loops the first 256 values.
105void DetectHighLowInGraph(int *high, int *low, bool addFuzz) {
106
107 uint8_t loopMax = 255;
108 if ( loopMax > GraphTraceLen)
109 loopMax = GraphTraceLen;
110
111 for (uint8_t i = 0; i < loopMax; ++i) {
112 if (GraphBuffer[i] > *high)
113 *high = GraphBuffer[i];
114 else if (GraphBuffer[i] < *low)
115 *low = GraphBuffer[i];
116 }
117
118 //12% fuzz in case highs and lows aren't clipped
119 if (addFuzz) {
120 *high = (int)(*high * .88);
121 *low = (int)(*low * .88);
122 }
7fe9b0b7 123}
ba1a299c 124
f3bf15e4 125// Get or auto-detect ask clock rate
126int GetAskClock(const char str[], bool printAns, bool verbose)
e770c648 127{
128 int clock;
129 sscanf(str, "%i", &clock);
130 if (!strcmp(str, ""))
131 clock = 0;
132
c6e5c7ea 133 if (clock != 0) return clock;
134
e770c648 135 // Auto-detect clock
f3bf15e4 136 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
137 size_t size = getFromGraphBuf(grph);
138 if (size == 0) {
139 if (verbose)
e770c648 140 PrintAndLog("Failed to copy from graphbuffer");
f3bf15e4 141 return -1;
142 }
c0f15a05 143 bool st = DetectST(grph, &size, &clock);
144 int start = 0;
c6e5c7ea 145 if (st == false)
c0f15a05 146 start = DetectASKClock(grph, size, &clock, 20);
c6e5c7ea 147
c6e5c7ea 148 if (printAns)
fef74fdc 149 PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock, start);
1ec412d9 150 SetGraphClock(clock, start);
e770c648 151 return clock;
152}
153
872e3d4d 154uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose)
155{
a47ded5b 156 uint8_t carrier = 0;
157 uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0};
872e3d4d 158 size_t size = getFromGraphBuf(grph);
159 if ( size == 0 ) {
160 if (verbose)
161 PrintAndLog("Failed to copy from graphbuffer");
162 return 0;
163 }
a47ded5b 164 carrier = countFC(grph, size, 0);
872e3d4d 165 // Only print this message if we're not looping something
a47ded5b 166 if (printAns)
872e3d4d 167 PrintAndLog("Auto-detected PSK carrier rate: %d", carrier);
a47ded5b 168
872e3d4d 169 return carrier;
170}
171
f3bf15e4 172int GetPskClock(const char str[], bool printAns, bool verbose)
173{
174 int clock;
175 sscanf(str, "%i", &clock);
176 if (!strcmp(str, ""))
177 clock = 0;
178
a47ded5b 179 if (clock != 0) return clock;
f3bf15e4 180 // Auto-detect clock
a47ded5b 181 uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0};
f3bf15e4 182 size_t size = getFromGraphBuf(grph);
183 if ( size == 0 ) {
1a4b9073 184 if (verbose) PrintAndLog("Failed to copy from graphbuffer");
f3bf15e4 185 return -1;
186 }
9833360b 187 int start = 0;
188 clock = DetectPSKClock_ext(grph, size, 0, &start);
189
190 if (printAns)
191 PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock, start);
192
193 SetGraphClock(clock, start);
f3bf15e4 194 return clock;
195}
196
197uint8_t GetNrzClock(const char str[], bool printAns, bool verbose)
4118b74d 198{
ba1a299c 199 int clock;
ba1a299c 200 sscanf(str, "%i", &clock);
201 if (!strcmp(str, ""))
202 clock = 0;
203
a47ded5b 204 if (clock != 0)
f3bf15e4 205 return clock;
ba1a299c 206 // Auto-detect clock
a47ded5b 207 uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0};
f3bf15e4 208 size_t size = getFromGraphBuf(grph);
209 if ( size == 0 ) {
210 if (verbose)
e629181f 211 PrintAndLog("Failed to copy from graphbuffer");
f3bf15e4 212 return -1;
213 }
9833360b 214 int start = 0;
215 clock = DetectNRZClock_ext(grph, size, 0, &start);
f3bf15e4 216 // Only print this message if we're not looping something
a47ded5b 217 if (printAns)
9833360b 218 PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock, start);
a47ded5b 219
9833360b 220 SetGraphClock(clock, start);
ba1a299c 221 return clock;
4118b74d 222}
f3bf15e4 223//by marshmellow
224//attempt to detect the field clock and bit clock for FSK
225uint8_t GetFskClock(const char str[], bool printAns, bool verbose)
226{
227 int clock;
228 sscanf(str, "%i", &clock);
229 if (!strcmp(str, ""))
230 clock = 0;
231 if (clock != 0) return (uint8_t)clock;
232
abd6112f 233
234 uint8_t fc1=0, fc2=0, rf1=0;
235 uint8_t ans = fskClocks(&fc1, &fc2, &rf1, verbose);
236 if (ans == 0) return 0;
237 if ((fc1==10 && fc2==8) || (fc1==8 && fc2==5)){
238 if (printAns) PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
239 return rf1;
240 }
241 if (verbose){
242 PrintAndLog("DEBUG: unknown fsk field clock detected");
243 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
244 }
245 return 0;
246}
247uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose)
248{
a47ded5b 249 uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0};
f3bf15e4 250 size_t size = getFromGraphBuf(BitStream);
a47ded5b 251 if (size == 0) return 0;
2eec55c8 252 uint16_t ans = countFC(BitStream, size, 1);
a47ded5b 253 if (ans == 0) {
1a4b9073 254 if (verbose || g_debugMode) PrintAndLog("DEBUG: No data found");
f3bf15e4 255 return 0;
256 }
abd6112f 257 *fc1 = (ans >> 8) & 0xFF;
258 *fc2 = ans & 0xFF;
f3bf15e4 259
9833360b 260 int start = 0;
261 *rf1 = detectFSKClk_ext(BitStream, size, *fc1, *fc2, &start);
a47ded5b 262 if (*rf1 == 0) {
1a4b9073 263 if (verbose || g_debugMode) PrintAndLog("DEBUG: Clock detect error");
f3bf15e4 264 return 0;
265 }
9833360b 266
267 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d | Best Starting Position: %d", *fc1, *fc2, *rf1, start);
268 SetGraphClock(*rf1, start);
abd6112f 269 return 1;
f3bf15e4 270}
3f84d473 271
272// test samples are not just noise
273bool graphJustNoise(int *BitStream, int size)
274{
275 //might not be high enough for noisy environments
276 #define THRESHOLD 15;
3f84d473 277 bool isNoise = TRUE;
278 for(int i=0; i < size && isNoise; i++){
279 isNoise = BitStream[i] < THRESHOLD;
280 }
281 return isNoise;
282}
Impressum, Datenschutz