]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/graph.c
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 //-----------------------------------------------------------------------------
18 int GraphBuffer
[MAX_GRAPH_TRACE_LEN
];
20 /* write a manchester bit to the graph */
21 void AppendGraph(int redraw
, int clock
, int bit
)
24 //set first half the clock bit (all 1's or 0's for a 0 or 1 bit)
25 for (i
= 0; i
< (int)(clock
/ 2); ++i
)
26 GraphBuffer
[GraphTraceLen
++] = bit
;
27 //set second half of the clock bit (all 0's or 1's for a 0 or 1 bit)
28 for (i
= (int)(clock
/ 2); i
< clock
; ++i
)
29 GraphBuffer
[GraphTraceLen
++] = bit
^ 1;
35 // clear out our graph window
36 int ClearGraph(int redraw
)
38 int gtl
= GraphTraceLen
;
39 memset(GraphBuffer
, 0x00, GraphTraceLen
);
48 // option '1' to save GraphBuffer any other to restore
49 void save_restoreGB(uint8_t saveOpt
)
51 static int SavedGB
[MAX_GRAPH_TRACE_LEN
];
52 static int SavedGBlen
;
53 static bool GB_Saved
= false;
55 if (saveOpt
==1) { //save
56 memcpy(SavedGB
, GraphBuffer
, sizeof(GraphBuffer
));
57 SavedGBlen
= GraphTraceLen
;
59 } else if (GB_Saved
){ //restore
60 memcpy(GraphBuffer
, SavedGB
, sizeof(GraphBuffer
));
61 GraphTraceLen
= SavedGBlen
;
67 // DETECT CLOCK NOW IN LFDEMOD.C
69 void setGraphBuf(uint8_t *buff
, size_t size
)
71 if ( buff
== NULL
) return;
74 if ( size
> MAX_GRAPH_TRACE_LEN
)
75 size
= MAX_GRAPH_TRACE_LEN
;
77 for (; i
< size
; ++i
){
78 GraphBuffer
[i
]=buff
[i
]-128;
84 size_t getFromGraphBuf(uint8_t *buff
)
86 if (buff
== NULL
) return 0;
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);
96 // A simple test to see if there is any data inside Graphbuffer.
99 if ( GraphTraceLen
<= 0) {
100 PrintAndLog("No data available, try reading something first");
106 // Detect high and lows in Grapbuffer.
107 // Only loops the first 256 values.
108 void DetectHighLowInGraph(int *high
, int *low
, bool addFuzz
) {
110 uint8_t loopMax
= 255;
111 if ( loopMax
> GraphTraceLen
)
112 loopMax
= GraphTraceLen
;
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
];
121 //12% fuzz in case highs and lows aren't clipped
123 *high
= (int)(*high
* .88);
124 *low
= (int)(*low
* .88);
128 // Get or auto-detect ask clock rate
129 int GetAskClock(const char str
[], bool printAns
, bool verbose
)
132 sscanf(str
, "%i", &clock
);
133 if (!strcmp(str
, ""))
139 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
140 size_t size
= getFromGraphBuf(grph
);
143 PrintAndLog("Failed to copy from graphbuffer");
146 bool st
= DetectST(grph
, &size
, &clock
);
149 start
= DetectASKClock(grph
, size
, &clock
, 20);
151 // Only print this message if we're not looping something
153 PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock
, start
);
156 PlockClockStartIndex
= start
;
160 uint8_t GetPskCarrier(const char str
[], bool printAns
, bool verbose
)
163 uint8_t grph
[MAX_GRAPH_TRACE_LEN
] = {0};
164 size_t size
= getFromGraphBuf(grph
);
167 PrintAndLog("Failed to copy from graphbuffer");
170 carrier
= countFC(grph
, size
, 0);
171 // Only print this message if we're not looping something
173 PrintAndLog("Auto-detected PSK carrier rate: %d", carrier
);
178 int GetPskClock(const char str
[], bool printAns
, bool verbose
)
181 sscanf(str
, "%i", &clock
);
182 if (!strcmp(str
, ""))
185 if (clock
!= 0) return clock
;
187 uint8_t grph
[MAX_GRAPH_TRACE_LEN
] = {0};
188 size_t size
= getFromGraphBuf(grph
);
190 if (verbose
) PrintAndLog("Failed to copy from graphbuffer");
193 clock
= DetectPSKClock(grph
, size
, 0);
194 // Only print this message if we're not looping something
195 if (printAns
) PrintAndLog("Auto-detected clock rate: %d", clock
);
198 // PlockClockStartIndex = start;
202 uint8_t GetNrzClock(const char str
[], bool printAns
, bool verbose
)
205 sscanf(str
, "%i", &clock
);
206 if (!strcmp(str
, ""))
212 uint8_t grph
[MAX_GRAPH_TRACE_LEN
] = {0};
213 size_t size
= getFromGraphBuf(grph
);
216 PrintAndLog("Failed to copy from graphbuffer");
219 clock
= DetectNRZClock(grph
, size
, 0);
220 // Only print this message if we're not looping something
222 PrintAndLog("Auto-detected clock rate: %d", clock
);
225 //PlockClockStartIndex = start;
229 //attempt to detect the field clock and bit clock for FSK
230 uint8_t GetFskClock(const char str
[], bool printAns
, bool verbose
)
233 sscanf(str
, "%i", &clock
);
234 if (!strcmp(str
, ""))
236 if (clock
!= 0) return (uint8_t)clock
;
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
);
247 PrintAndLog("DEBUG: unknown fsk field clock detected");
248 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1
, fc2
, rf1
);
251 //PlockClockStartIndex = start;
254 uint8_t fskClocks(uint8_t *fc1
, uint8_t *fc2
, uint8_t *rf1
, bool verbose
)
256 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
] = {0};
257 size_t size
= getFromGraphBuf(BitStream
);
258 if (size
== 0) return 0;
259 uint16_t ans
= countFC(BitStream
, size
, 1);
261 if (verbose
|| g_debugMode
) PrintAndLog("DEBUG: No data found");
264 *fc1
= (ans
>> 8) & 0xFF;
267 *rf1
= detectFSKClk(BitStream
, size
, *fc1
, *fc2
);
269 if (verbose
|| g_debugMode
) PrintAndLog("DEBUG: Clock detect error");
275 // test samples are not just noise
276 bool graphJustNoise(int *BitStream
, int size
)
278 //might not be high enough for noisy environments
279 #define THRESHOLD 15;
282 for(int i
=0; i
< size
&& isNoise
; i
++){
283 isNoise
= BitStream
[i
] < THRESHOLD
;