]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/graph.c
547e9b30d8e26672ef54dc48332dfd1cc6053a13
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
17 #include "cmddata.h" //for g_debugmode
19 int GraphBuffer
[MAX_GRAPH_TRACE_LEN
];
22 int s_Buff
[MAX_GRAPH_TRACE_LEN
];
24 /* write a manchester bit to the graph */
25 void AppendGraph(int redraw
, int clock
, int bit
)
28 //set first half the clock bit (all 1's or 0's for a 0 or 1 bit)
29 for (i
= 0; i
< (int)(clock
/ 2); ++i
)
30 GraphBuffer
[GraphTraceLen
++] = bit
;
31 //set second half of the clock bit (all 0's or 1's for a 0 or 1 bit)
32 for (i
= (int)(clock
/ 2); i
< clock
; ++i
)
33 GraphBuffer
[GraphTraceLen
++] = bit
^ 1;
39 // clear out our graph window
40 int ClearGraph(int redraw
)
42 int gtl
= GraphTraceLen
;
43 memset(GraphBuffer
, 0x00, GraphTraceLen
);
52 // option '1' to save GraphBuffer any other to restore
53 void save_restoreGB(uint8_t saveOpt
)
55 static int SavedGB
[MAX_GRAPH_TRACE_LEN
];
56 static int SavedGBlen
;
57 static bool GB_Saved
= false;
59 if (saveOpt
==1) { //save
60 memcpy(SavedGB
, GraphBuffer
, sizeof(GraphBuffer
));
61 SavedGBlen
= GraphTraceLen
;
63 } else if (GB_Saved
){ //restore
64 memcpy(GraphBuffer
, SavedGB
, sizeof(GraphBuffer
));
65 GraphTraceLen
= SavedGBlen
;
71 // DETECT CLOCK NOW IN LFDEMOD.C
73 void setGraphBuf(uint8_t *buff
, size_t size
)
75 if ( buff
== NULL
) return;
78 if ( size
> MAX_GRAPH_TRACE_LEN
)
79 size
= MAX_GRAPH_TRACE_LEN
;
81 for (; i
< size
; ++i
){
82 GraphBuffer
[i
]=buff
[i
]-128;
88 size_t getFromGraphBuf(uint8_t *buff
)
90 if (buff
== NULL
) return 0;
92 for (i
=0;i
<GraphTraceLen
;++i
){
93 if (GraphBuffer
[i
]>127) GraphBuffer
[i
]=127; //trim
94 if (GraphBuffer
[i
]<-127) GraphBuffer
[i
]=-127; //trim
95 buff
[i
]=(uint8_t)(GraphBuffer
[i
]+128);
100 // A simple test to see if there is any data inside Graphbuffer.
103 if ( GraphTraceLen
<= 0) {
104 PrintAndLog("No data available, try reading something first");
110 // Detect high and lows in Grapbuffer.
111 // Only loops the first 256 values.
112 void DetectHighLowInGraph(int *high
, int *low
, bool addFuzz
) {
114 uint8_t loopMax
= 255;
115 if ( loopMax
> GraphTraceLen
)
116 loopMax
= GraphTraceLen
;
118 for (uint8_t i
= 0; i
< loopMax
; ++i
) {
119 if (GraphBuffer
[i
] > *high
)
120 *high
= GraphBuffer
[i
];
121 else if (GraphBuffer
[i
] < *low
)
122 *low
= GraphBuffer
[i
];
125 //12% fuzz in case highs and lows aren't clipped
127 *high
= (int)(*high
* .88);
128 *low
= (int)(*low
* .88);
132 // Get or auto-detect ask clock rate
133 int GetAskClock(const char str
[], bool printAns
, bool verbose
)
136 sscanf(str
, "%i", &clock
);
137 if (!strcmp(str
, ""))
143 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
144 size_t size
= getFromGraphBuf(grph
);
147 PrintAndLog("Failed to copy from graphbuffer");
150 //, size_t *ststart, size_t *stend
151 size_t ststart
= 0, stend
= 0;
152 bool st
= DetectST(grph
, &size
, &clock
, &ststart
, &stend
);
155 start
= DetectASKClock(grph
, size
, &clock
, 20);
157 setClockGrid(clock
, start
);
158 // Only print this message if we're not looping something
159 if (printAns
|| g_debugMode
) {
160 PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock
, start
);
165 uint8_t GetPskCarrier(const char str
[], bool printAns
, bool verbose
)
168 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
169 size_t size
= getFromGraphBuf(grph
);
172 PrintAndLog("Failed to copy from graphbuffer");
175 uint16_t fc
= countFC(grph
,size
,0);
177 if (carrier
!= 2 && carrier
!= 4 && carrier
!= 8) return 0;
178 if ((fc
>>8) == 10 && carrier
== 8) return 0;
179 // Only print this message if we're not looping something
181 PrintAndLog("Auto-detected PSK carrier rate: %d", carrier
);
186 int GetPskClock(const char str
[], bool printAns
, bool verbose
)
189 sscanf(str
, "%i", &clock
);
190 if (!strcmp(str
, ""))
196 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
197 size_t size
= getFromGraphBuf(grph
);
200 PrintAndLog("Failed to copy from graphbuffer");
203 size_t firstPhaseShiftLoc
= 0;
204 uint8_t curPhase
= 0, fc
= 0;
205 clock
= DetectPSKClock(grph
, size
, 0, &firstPhaseShiftLoc
, &curPhase
, &fc
);
206 setClockGrid(clock
, firstPhaseShiftLoc
);
207 // Only print this message if we're not looping something
209 PrintAndLog("Auto-detected clock rate: %d", clock
);
214 uint8_t GetNrzClock(const char str
[], bool printAns
, bool verbose
)
217 sscanf(str
, "%i", &clock
);
218 if (!strcmp(str
, ""))
224 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
225 size_t size
= getFromGraphBuf(grph
);
228 PrintAndLog("Failed to copy from graphbuffer");
231 size_t clkStartIdx
= 0;
232 clock
= DetectNRZClock(grph
, size
, 0, &clkStartIdx
);
233 setClockGrid(clock
, clkStartIdx
);
234 // Only print this message if we're not looping something
236 PrintAndLog("Auto-detected clock rate: %d", clock
);
241 //attempt to detect the field clock and bit clock for FSK
242 uint8_t GetFskClock(const char str
[], bool printAns
, bool verbose
)
245 sscanf(str
, "%i", &clock
);
246 if (!strcmp(str
, ""))
248 if (clock
!= 0) return (uint8_t)clock
;
251 uint8_t fc1
=0, fc2
=0, rf1
=0;
252 int firstClockEdge
= 0;
253 uint8_t ans
= fskClocks(&fc1
, &fc2
, &rf1
, verbose
, &firstClockEdge
);
254 if (ans
== 0) return 0;
255 if ((fc1
==10 && fc2
==8) || (fc1
==8 && fc2
==5)){
256 if (printAns
) PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1
, fc2
, rf1
);
257 setClockGrid(rf1
, firstClockEdge
);
261 PrintAndLog("DEBUG: unknown fsk field clock detected");
262 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1
, fc2
, rf1
);
266 uint8_t fskClocks(uint8_t *fc1
, uint8_t *fc2
, uint8_t *rf1
, bool verbose
, int *firstClockEdge
)
268 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
269 size_t size
= getFromGraphBuf(BitStream
);
270 if (size
==0) return 0;
271 uint16_t ans
= countFC(BitStream
, size
, 1);
273 if (verbose
|| g_debugMode
) PrintAndLog("DEBUG: No data found");
276 *fc1
= (ans
>> 8) & 0xFF;
278 //int firstClockEdge = 0;
279 *rf1
= detectFSKClk(BitStream
, size
, *fc1
, *fc2
, firstClockEdge
);
281 if (verbose
|| g_debugMode
) PrintAndLog("DEBUG: Clock detect error");
286 bool graphJustNoise(int *BitStream
, int size
)
288 static const uint8_t THRESHOLD
= 15; //might not be high enough for noisy environments
289 //test samples are not just noise
291 for(int idx
=0; idx
< size
&& justNoise1
;idx
++){
292 justNoise1
= BitStream
[idx
] < THRESHOLD
;
296 int autoCorr(const int* in
, int *out
, size_t len
, int window
)
298 static int CorrelBuffer
[MAX_GRAPH_TRACE_LEN
];
301 PrintAndLog("needs a window");
305 PrintAndLog("window must be smaller than trace (%d samples)",
310 PrintAndLog("performing %d correlations", len
- window
);
312 for (int i
= 0; i
< len
- window
; ++i
) {
314 for (int j
= 0; j
< window
; ++j
) {
315 sum
+= (in
[j
]*in
[i
+ j
]) / 256;
317 CorrelBuffer
[i
] = sum
;
319 //GraphTraceLen = GraphTraceLen - window;
320 memcpy(out
, CorrelBuffer
, len
* sizeof (int));
323 int directionalThreshold(const int* in
, int *out
, size_t len
, int8_t up
, int8_t down
)
325 int lastValue
= in
[0];
326 out
[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in.
328 for (int i
= 1; i
< len
; ++i
) {
329 // Apply first threshold to samples heading up
330 if (in
[i
] >= up
&& in
[i
] > lastValue
)
332 lastValue
= out
[i
]; // Buffer last value as we overwrite it.
335 // Apply second threshold to samples heading down
336 else if (in
[i
] <= down
&& in
[i
] < lastValue
)
338 lastValue
= out
[i
]; // Buffer last value as we overwrite it.
343 lastValue
= out
[i
]; // Buffer last value as we overwrite it.
347 out
[0] = out
[1]; // Align with first edited sample.