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