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