]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/graph.c
lfem4x cleanup/add/fix
[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{
23 int i;
abd6112f 24 //set first half the clock bit (all 1's or 0's for a 0 or 1 bit)
7fe9b0b7 25 for (i = 0; i < (int)(clock / 2); ++i)
78f5b1a7 26 GraphBuffer[GraphTraceLen++] = bit ;
abd6112f 27 //set second half of the clock bit (all 0's or 1's for a 0 or 1 bit)
7fe9b0b7 28 for (i = (int)(clock / 2); i < clock; ++i)
78f5b1a7 29 GraphBuffer[GraphTraceLen++] = bit ^ 1;
7fe9b0b7 30
31 if (redraw)
32 RepaintGraphWindow();
33}
34
c12512e9 35// clear out our graph window
7fe9b0b7 36int ClearGraph(int redraw)
37{
38 int gtl = GraphTraceLen;
52ab55ab 39 memset(GraphBuffer, 0x00, GraphTraceLen);
40
7fe9b0b7 41 GraphTraceLen = 0;
42
43 if (redraw)
44 RepaintGraphWindow();
45
46 return gtl;
47}
23f0a7d8 48// option '1' to save GraphBuffer any other to restore
49void save_restoreGB(uint8_t saveOpt)
50{
51 static int SavedGB[MAX_GRAPH_TRACE_LEN];
52 static int SavedGBlen;
53 static bool GB_Saved = false;
54
55 if (saveOpt==1) { //save
56 memcpy(SavedGB,GraphBuffer, sizeof(GraphBuffer));
57 SavedGBlen = GraphTraceLen;
58 GB_Saved=true;
59 } else if (GB_Saved){
60 memcpy(GraphBuffer,SavedGB, sizeof(GraphBuffer));
61 GraphTraceLen = SavedGBlen;
62 }
63 return;
64}
7fe9b0b7 65
c12512e9 66// DETECT CLOCK NOW IN LFDEMOD.C
7fe9b0b7 67
ba1a299c 68void setGraphBuf(uint8_t *buff, size_t size)
d5a72d2f 69{
a1557c4c 70 if ( buff == NULL ) return;
71
72 uint16_t i = 0;
73 if ( size > MAX_GRAPH_TRACE_LEN )
74 size = MAX_GRAPH_TRACE_LEN;
b4fb11ba 75 ClearGraph(0);
76 for (; i < size; ++i){
ba1a299c 77 GraphBuffer[i]=buff[i]-128;
b4fb11ba 78 }
79 GraphTraceLen=size;
80 RepaintGraphWindow();
81 return;
d5a72d2f 82}
ba1a299c 83size_t getFromGraphBuf(uint8_t *buff)
d5a72d2f 84{
e770c648 85 if (buff == NULL ) return 0;
b4fb11ba 86 uint32_t i;
87 for (i=0;i<GraphTraceLen;++i){
88 if (GraphBuffer[i]>127) GraphBuffer[i]=127; //trim
89 if (GraphBuffer[i]<-127) GraphBuffer[i]=-127; //trim
90 buff[i]=(uint8_t)(GraphBuffer[i]+128);
91 }
92 return i;
d5a72d2f 93}
ec75f5c1 94
a1557c4c 95// A simple test to see if there is any data inside Graphbuffer.
96bool HasGraphData(){
7fe9b0b7 97
a1557c4c 98 if ( GraphTraceLen <= 0) {
99 PrintAndLog("No data available, try reading something first");
100 return false;
101 }
102 return true;
103}
104
105// Detect high and lows in Grapbuffer.
106// Only loops the first 256 values.
107void DetectHighLowInGraph(int *high, int *low, bool addFuzz) {
108
109 uint8_t loopMax = 255;
110 if ( loopMax > GraphTraceLen)
111 loopMax = GraphTraceLen;
112
113 for (uint8_t i = 0; i < loopMax; ++i) {
114 if (GraphBuffer[i] > *high)
115 *high = GraphBuffer[i];
116 else if (GraphBuffer[i] < *low)
117 *low = GraphBuffer[i];
118 }
119
120 //12% fuzz in case highs and lows aren't clipped
121 if (addFuzz) {
122 *high = (int)(*high * .88);
123 *low = (int)(*low * .88);
124 }
7fe9b0b7 125}
ba1a299c 126
f3bf15e4 127// Get or auto-detect ask clock rate
128int GetAskClock(const char str[], bool printAns, bool verbose)
e770c648 129{
130 int clock;
131 sscanf(str, "%i", &clock);
132 if (!strcmp(str, ""))
133 clock = 0;
134
f3bf15e4 135 if (clock != 0)
136 return clock;
e770c648 137 // Auto-detect clock
f3bf15e4 138 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
139 size_t size = getFromGraphBuf(grph);
140 if (size == 0) {
141 if (verbose)
e770c648 142 PrintAndLog("Failed to copy from graphbuffer");
f3bf15e4 143 return -1;
144 }
145 DetectASKClock(grph, size, &clock, 20);
146 // Only print this message if we're not looping something
147 if (printAns){
148 PrintAndLog("Auto-detected clock rate: %d", clock);
e770c648 149 }
150 return clock;
151}
152
872e3d4d 153uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose)
154{
155 uint8_t carrier=0;
156 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
157 size_t size = getFromGraphBuf(grph);
158 if ( size == 0 ) {
159 if (verbose)
160 PrintAndLog("Failed to copy from graphbuffer");
161 return 0;
162 }
163 //uint8_t countPSK_FC(uint8_t *BitStream, size_t size)
164
2eec55c8 165 carrier = countFC(grph,size,0);
872e3d4d 166 // Only print this message if we're not looping something
167 if (printAns){
168 PrintAndLog("Auto-detected PSK carrier rate: %d", carrier);
169 }
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
180 if (clock!=0)
181 return clock;
182 // Auto-detect clock
183 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
184 size_t size = getFromGraphBuf(grph);
185 if ( size == 0 ) {
186 if (verbose)
187 PrintAndLog("Failed to copy from graphbuffer");
188 return -1;
189 }
190 clock = DetectPSKClock(grph,size,0);
191 // Only print this message if we're not looping something
192 if (printAns){
193 PrintAndLog("Auto-detected clock rate: %d", clock);
194 }
195 return clock;
196}
197
198uint8_t GetNrzClock(const char str[], bool printAns, bool verbose)
4118b74d 199{
ba1a299c 200 int clock;
ba1a299c 201 sscanf(str, "%i", &clock);
202 if (!strcmp(str, ""))
203 clock = 0;
204
f3bf15e4 205 if (clock!=0)
206 return clock;
ba1a299c 207 // Auto-detect clock
f3bf15e4 208 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
209 size_t size = getFromGraphBuf(grph);
210 if ( size == 0 ) {
211 if (verbose)
e629181f 212 PrintAndLog("Failed to copy from graphbuffer");
f3bf15e4 213 return -1;
214 }
215 clock = DetectNRZClock(grph, size, 0);
216 // Only print this message if we're not looping something
217 if (printAns){
218 PrintAndLog("Auto-detected clock rate: %d", clock);
ba1a299c 219 }
220 return clock;
4118b74d 221}
f3bf15e4 222//by marshmellow
223//attempt to detect the field clock and bit clock for FSK
224uint8_t GetFskClock(const char str[], bool printAns, bool verbose)
225{
226 int clock;
227 sscanf(str, "%i", &clock);
228 if (!strcmp(str, ""))
229 clock = 0;
230 if (clock != 0) return (uint8_t)clock;
231
abd6112f 232
233 uint8_t fc1=0, fc2=0, rf1=0;
234 uint8_t ans = fskClocks(&fc1, &fc2, &rf1, verbose);
235 if (ans == 0) return 0;
236 if ((fc1==10 && fc2==8) || (fc1==8 && fc2==5)){
237 if (printAns) PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
238 return rf1;
239 }
240 if (verbose){
241 PrintAndLog("DEBUG: unknown fsk field clock detected");
242 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
243 }
244 return 0;
245}
246uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose)
247{
f3bf15e4 248 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
249 size_t size = getFromGraphBuf(BitStream);
250 if (size==0) return 0;
2eec55c8 251 uint16_t ans = countFC(BitStream, size, 1);
f3bf15e4 252 if (ans==0) {
253 if (verbose) PrintAndLog("DEBUG: No data found");
254 return 0;
255 }
abd6112f 256 *fc1 = (ans >> 8) & 0xFF;
257 *fc2 = ans & 0xFF;
f3bf15e4 258
abd6112f 259 *rf1 = detectFSKClk(BitStream, size, *fc1, *fc2);
260 if (*rf1==0) {
f3bf15e4 261 if (verbose) PrintAndLog("DEBUG: Clock detect error");
262 return 0;
263 }
abd6112f 264 return 1;
f3bf15e4 265}
Impressum, Datenschutz