Added brackets around crc field in protocol listings
[legacy-proxmark3.git] / client / graph.c
blob11dbc4d595294abaf178fa918b4411e58ac7243f
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 //-----------------------------------------------------------------------------
11 #include <stdio.h>
12 #include <stdbool.h>
13 #include <string.h>
14 #include "ui.h"
15 #include "graph.h"
16 #include "lfdemod.h"
18 int GraphBuffer[MAX_GRAPH_TRACE_LEN];
19 int GraphTraceLen;
21 /* write a bit to the graph */
22 void AppendGraph(int redraw, int clock, int bit)
24 int i;
26 for (i = 0; i < (int)(clock / 2); ++i)
27 GraphBuffer[GraphTraceLen++] = bit ^ 1;
29 for (i = (int)(clock / 2); i < clock; ++i)
30 GraphBuffer[GraphTraceLen++] = bit;
32 if (redraw)
33 RepaintGraphWindow();
36 // clear out our graph window
37 int ClearGraph(int redraw)
39 int gtl = GraphTraceLen;
40 memset(GraphBuffer, 0x00, GraphTraceLen);
42 GraphTraceLen = 0;
44 if (redraw)
45 RepaintGraphWindow();
47 return gtl;
50 // DETECT CLOCK NOW IN LFDEMOD.C
52 void setGraphBuf(uint8_t *buff, size_t size)
54 if ( buff == NULL ) return;
56 uint16_t i = 0;
57 if ( size > MAX_GRAPH_TRACE_LEN )
58 size = MAX_GRAPH_TRACE_LEN;
59 ClearGraph(0);
60 for (; i < size; ++i){
61 GraphBuffer[i]=buff[i]-128;
63 GraphTraceLen=size;
64 RepaintGraphWindow();
65 return;
67 size_t getFromGraphBuf(uint8_t *buff)
69 if (buff == NULL ) return 0;
70 uint32_t i;
71 for (i=0;i<GraphTraceLen;++i){
72 if (GraphBuffer[i]>127) GraphBuffer[i]=127; //trim
73 if (GraphBuffer[i]<-127) GraphBuffer[i]=-127; //trim
74 buff[i]=(uint8_t)(GraphBuffer[i]+128);
76 return i;
79 // A simple test to see if there is any data inside Graphbuffer.
80 bool HasGraphData(){
82 if ( GraphTraceLen <= 0) {
83 PrintAndLog("No data available, try reading something first");
84 return false;
86 return true;
89 // Detect high and lows in Grapbuffer.
90 // Only loops the first 256 values.
91 void DetectHighLowInGraph(int *high, int *low, bool addFuzz) {
93 uint8_t loopMax = 255;
94 if ( loopMax > GraphTraceLen)
95 loopMax = GraphTraceLen;
97 for (uint8_t i = 0; i < loopMax; ++i) {
98 if (GraphBuffer[i] > *high)
99 *high = GraphBuffer[i];
100 else if (GraphBuffer[i] < *low)
101 *low = GraphBuffer[i];
104 //12% fuzz in case highs and lows aren't clipped
105 if (addFuzz) {
106 *high = (int)(*high * .88);
107 *low = (int)(*low * .88);
111 // Get or auto-detect ask clock rate
112 int GetAskClock(const char str[], bool printAns, bool verbose)
114 int clock;
115 sscanf(str, "%i", &clock);
116 if (!strcmp(str, ""))
117 clock = 0;
119 if (clock != 0)
120 return clock;
121 // Auto-detect clock
122 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
123 size_t size = getFromGraphBuf(grph);
124 if (size == 0) {
125 if (verbose)
126 PrintAndLog("Failed to copy from graphbuffer");
127 return -1;
129 DetectASKClock(grph, size, &clock, 20);
130 // Only print this message if we're not looping something
131 if (printAns){
132 PrintAndLog("Auto-detected clock rate: %d", clock);
134 return clock;
137 int GetPskClock(const char str[], bool printAns, bool verbose)
139 int clock;
140 sscanf(str, "%i", &clock);
141 if (!strcmp(str, ""))
142 clock = 0;
144 if (clock!=0)
145 return clock;
146 // Auto-detect clock
147 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
148 size_t size = getFromGraphBuf(grph);
149 if ( size == 0 ) {
150 if (verbose)
151 PrintAndLog("Failed to copy from graphbuffer");
152 return -1;
154 clock = DetectPSKClock(grph,size,0);
155 // Only print this message if we're not looping something
156 if (printAns){
157 PrintAndLog("Auto-detected clock rate: %d", clock);
159 return clock;
162 uint8_t GetNrzClock(const char str[], bool printAns, bool verbose)
164 int clock;
165 sscanf(str, "%i", &clock);
166 if (!strcmp(str, ""))
167 clock = 0;
169 if (clock!=0)
170 return clock;
171 // Auto-detect clock
172 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
173 size_t size = getFromGraphBuf(grph);
174 if ( size == 0 ) {
175 if (verbose)
176 PrintAndLog("Failed to copy from graphbuffer");
177 return -1;
179 clock = DetectNRZClock(grph, size, 0);
180 // Only print this message if we're not looping something
181 if (printAns){
182 PrintAndLog("Auto-detected clock rate: %d", clock);
184 return clock;
186 //by marshmellow
187 //attempt to detect the field clock and bit clock for FSK
188 uint8_t GetFskClock(const char str[], bool printAns, bool verbose)
190 int clock;
191 sscanf(str, "%i", &clock);
192 if (!strcmp(str, ""))
193 clock = 0;
194 if (clock != 0) return (uint8_t)clock;
196 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
197 size_t size = getFromGraphBuf(BitStream);
198 if (size==0) return 0;
199 uint8_t dummy = 0;
200 uint16_t ans = countFC(BitStream, size, &dummy);
201 if (ans==0) {
202 if (verbose) PrintAndLog("DEBUG: No data found");
203 return 0;
205 uint8_t fc1, fc2;
206 fc1 = (ans >> 8) & 0xFF;
207 fc2 = ans & 0xFF;
209 uint8_t rf1 = detectFSKClk(BitStream, size, fc1, fc2);
210 if (rf1==0) {
211 if (verbose) PrintAndLog("DEBUG: Clock detect error");
212 return 0;
214 if ((fc1==10 && fc2==8) || (fc1==8 && fc2==5)){
215 if (printAns) PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
216 return rf1;
218 if (verbose){
219 PrintAndLog("DEBUG: unknown fsk field clock detected");
220 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
222 return 0;