]>
Commit | Line | Data |
---|---|---|
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 | |
18 | int GraphBuffer[MAX_GRAPH_TRACE_LEN]; | |
19 | int GraphTraceLen; | |
20 | ||
abd6112f | 21 | /* write a manchester bit to the graph */ |
7fe9b0b7 | 22 | void 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 | 37 | int 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 | } | |
49 | ||
c12512e9 | 50 | // DETECT CLOCK NOW IN LFDEMOD.C |
7fe9b0b7 | 51 | |
ba1a299c | 52 | void setGraphBuf(uint8_t *buff, size_t size) |
d5a72d2f | 53 | { |
a1557c4c | 54 | if ( buff == NULL ) return; |
55 | ||
56 | uint16_t i = 0; | |
57 | if ( size > MAX_GRAPH_TRACE_LEN ) | |
58 | size = MAX_GRAPH_TRACE_LEN; | |
b4fb11ba | 59 | ClearGraph(0); |
60 | for (; i < size; ++i){ | |
ba1a299c | 61 | GraphBuffer[i]=buff[i]-128; |
b4fb11ba | 62 | } |
63 | GraphTraceLen=size; | |
64 | RepaintGraphWindow(); | |
65 | return; | |
d5a72d2f | 66 | } |
ba1a299c | 67 | size_t getFromGraphBuf(uint8_t *buff) |
d5a72d2f | 68 | { |
e770c648 | 69 | if (buff == NULL ) return 0; |
b4fb11ba | 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); | |
75 | } | |
76 | return i; | |
d5a72d2f | 77 | } |
ec75f5c1 | 78 | |
a1557c4c | 79 | // A simple test to see if there is any data inside Graphbuffer. |
80 | bool HasGraphData(){ | |
7fe9b0b7 | 81 | |
a1557c4c | 82 | if ( GraphTraceLen <= 0) { |
83 | PrintAndLog("No data available, try reading something first"); | |
84 | return false; | |
85 | } | |
86 | return true; | |
87 | } | |
88 | ||
89 | // Detect high and lows in Grapbuffer. | |
90 | // Only loops the first 256 values. | |
91 | void DetectHighLowInGraph(int *high, int *low, bool addFuzz) { | |
92 | ||
93 | uint8_t loopMax = 255; | |
94 | if ( loopMax > GraphTraceLen) | |
95 | loopMax = GraphTraceLen; | |
96 | ||
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]; | |
102 | } | |
103 | ||
104 | //12% fuzz in case highs and lows aren't clipped | |
105 | if (addFuzz) { | |
106 | *high = (int)(*high * .88); | |
107 | *low = (int)(*low * .88); | |
108 | } | |
7fe9b0b7 | 109 | } |
ba1a299c | 110 | |
f3bf15e4 | 111 | // Get or auto-detect ask clock rate |
112 | int GetAskClock(const char str[], bool printAns, bool verbose) | |
e770c648 | 113 | { |
114 | int clock; | |
115 | sscanf(str, "%i", &clock); | |
116 | if (!strcmp(str, "")) | |
117 | clock = 0; | |
118 | ||
f3bf15e4 | 119 | if (clock != 0) |
120 | return clock; | |
e770c648 | 121 | // Auto-detect clock |
f3bf15e4 | 122 | uint8_t grph[MAX_GRAPH_TRACE_LEN]={0}; |
123 | size_t size = getFromGraphBuf(grph); | |
124 | if (size == 0) { | |
125 | if (verbose) | |
e770c648 | 126 | PrintAndLog("Failed to copy from graphbuffer"); |
f3bf15e4 | 127 | return -1; |
128 | } | |
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); | |
e770c648 | 133 | } |
134 | return clock; | |
135 | } | |
136 | ||
872e3d4d | 137 | uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose) |
138 | { | |
139 | uint8_t carrier=0; | |
140 | uint8_t grph[MAX_GRAPH_TRACE_LEN]={0}; | |
141 | size_t size = getFromGraphBuf(grph); | |
142 | if ( size == 0 ) { | |
143 | if (verbose) | |
144 | PrintAndLog("Failed to copy from graphbuffer"); | |
145 | return 0; | |
146 | } | |
147 | //uint8_t countPSK_FC(uint8_t *BitStream, size_t size) | |
148 | ||
149 | carrier = countPSK_FC(grph,size); | |
150 | // Only print this message if we're not looping something | |
151 | if (printAns){ | |
152 | PrintAndLog("Auto-detected PSK carrier rate: %d", carrier); | |
153 | } | |
154 | return carrier; | |
155 | } | |
156 | ||
f3bf15e4 | 157 | int GetPskClock(const char str[], bool printAns, bool verbose) |
158 | { | |
159 | int clock; | |
160 | sscanf(str, "%i", &clock); | |
161 | if (!strcmp(str, "")) | |
162 | clock = 0; | |
163 | ||
164 | if (clock!=0) | |
165 | return clock; | |
166 | // Auto-detect clock | |
167 | uint8_t grph[MAX_GRAPH_TRACE_LEN]={0}; | |
168 | size_t size = getFromGraphBuf(grph); | |
169 | if ( size == 0 ) { | |
170 | if (verbose) | |
171 | PrintAndLog("Failed to copy from graphbuffer"); | |
172 | return -1; | |
173 | } | |
174 | clock = DetectPSKClock(grph,size,0); | |
175 | // Only print this message if we're not looping something | |
176 | if (printAns){ | |
177 | PrintAndLog("Auto-detected clock rate: %d", clock); | |
178 | } | |
179 | return clock; | |
180 | } | |
181 | ||
182 | uint8_t GetNrzClock(const char str[], bool printAns, bool verbose) | |
4118b74d | 183 | { |
ba1a299c | 184 | int clock; |
ba1a299c | 185 | sscanf(str, "%i", &clock); |
186 | if (!strcmp(str, "")) | |
187 | clock = 0; | |
188 | ||
f3bf15e4 | 189 | if (clock!=0) |
190 | return clock; | |
ba1a299c | 191 | // Auto-detect clock |
f3bf15e4 | 192 | uint8_t grph[MAX_GRAPH_TRACE_LEN]={0}; |
193 | size_t size = getFromGraphBuf(grph); | |
194 | if ( size == 0 ) { | |
195 | if (verbose) | |
e629181f | 196 | PrintAndLog("Failed to copy from graphbuffer"); |
f3bf15e4 | 197 | return -1; |
198 | } | |
199 | clock = DetectNRZClock(grph, size, 0); | |
200 | // Only print this message if we're not looping something | |
201 | if (printAns){ | |
202 | PrintAndLog("Auto-detected clock rate: %d", clock); | |
ba1a299c | 203 | } |
204 | return clock; | |
4118b74d | 205 | } |
f3bf15e4 | 206 | //by marshmellow |
207 | //attempt to detect the field clock and bit clock for FSK | |
208 | uint8_t GetFskClock(const char str[], bool printAns, bool verbose) | |
209 | { | |
210 | int clock; | |
211 | sscanf(str, "%i", &clock); | |
212 | if (!strcmp(str, "")) | |
213 | clock = 0; | |
214 | if (clock != 0) return (uint8_t)clock; | |
215 | ||
abd6112f | 216 | |
217 | uint8_t fc1=0, fc2=0, rf1=0; | |
218 | uint8_t ans = fskClocks(&fc1, &fc2, &rf1, verbose); | |
219 | if (ans == 0) return 0; | |
220 | if ((fc1==10 && fc2==8) || (fc1==8 && fc2==5)){ | |
221 | if (printAns) PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1); | |
222 | return rf1; | |
223 | } | |
224 | if (verbose){ | |
225 | PrintAndLog("DEBUG: unknown fsk field clock detected"); | |
226 | PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1); | |
227 | } | |
228 | return 0; | |
229 | } | |
230 | uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose) | |
231 | { | |
f3bf15e4 | 232 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; |
233 | size_t size = getFromGraphBuf(BitStream); | |
234 | if (size==0) return 0; | |
235 | uint8_t dummy = 0; | |
236 | uint16_t ans = countFC(BitStream, size, &dummy); | |
237 | if (ans==0) { | |
238 | if (verbose) PrintAndLog("DEBUG: No data found"); | |
239 | return 0; | |
240 | } | |
abd6112f | 241 | *fc1 = (ans >> 8) & 0xFF; |
242 | *fc2 = ans & 0xFF; | |
f3bf15e4 | 243 | |
abd6112f | 244 | *rf1 = detectFSKClk(BitStream, size, *fc1, *fc2); |
245 | if (*rf1==0) { | |
f3bf15e4 | 246 | if (verbose) PrintAndLog("DEBUG: Clock detect error"); |
247 | return 0; | |
248 | } | |
abd6112f | 249 | return 1; |
f3bf15e4 | 250 | } |