]>
git.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
);
45 // option '1' to save GraphBuffer any other to restore
46 void save_restoreGB(uint8_t saveOpt
)
48 static int SavedGB
[MAX_GRAPH_TRACE_LEN
];
49 static int SavedGBlen
;
50 static bool GB_Saved
= false;
52 if (saveOpt
==1) { //save
53 memcpy(SavedGB
, GraphBuffer
, sizeof(GraphBuffer
));
54 SavedGBlen
= GraphTraceLen
;
56 } else if (GB_Saved
){ //restore
57 memcpy(GraphBuffer
, SavedGB
, sizeof(GraphBuffer
));
58 GraphTraceLen
= SavedGBlen
;
64 // DETECT CLOCK NOW IN LFDEMOD.C
65 void setGraphBuf(uint8_t *buff
, size_t size
)
67 if ( buff
== NULL
) return;
71 if ( size
> MAX_GRAPH_TRACE_LEN
)
72 size
= MAX_GRAPH_TRACE_LEN
;
74 for (uint16_t i
= 0; i
< size
; ++i
)
75 GraphBuffer
[i
] = buff
[i
] - 128;
81 size_t getFromGraphBuf(uint8_t *buff
)
83 if (buff
== NULL
) return 0;
85 for (i
=0; i
< GraphTraceLen
; ++i
){
86 if (GraphBuffer
[i
] > 127) GraphBuffer
[i
] = 127; //trim
87 if (GraphBuffer
[i
] < -127) GraphBuffer
[i
] = -127; //trim
88 buff
[i
] = (uint8_t)(GraphBuffer
[i
]+128);
93 // A simple test to see if there is any data inside Graphbuffer.
96 if ( GraphTraceLen
<= 0) {
97 PrintAndLog("No data available, try reading something first");
103 // Detect high and lows in Grapbuffer.
104 // Only loops the first 256 values.
105 void DetectHighLowInGraph(int *high
, int *low
, bool addFuzz
) {
107 uint8_t loopMax
= 255;
108 if ( loopMax
> GraphTraceLen
)
109 loopMax
= GraphTraceLen
;
111 for (uint8_t i
= 0; i
< loopMax
; ++i
) {
112 if (GraphBuffer
[i
] > *high
)
113 *high
= GraphBuffer
[i
];
114 else if (GraphBuffer
[i
] < *low
)
115 *low
= GraphBuffer
[i
];
118 //12% fuzz in case highs and lows aren't clipped
120 *high
= (int)(*high
* .88);
121 *low
= (int)(*low
* .88);
125 // Get or auto-detect ask clock rate
126 int GetAskClock(const char str
[], bool printAns
, bool verbose
)
129 sscanf(str
, "%i", &clock
);
130 if (!strcmp(str
, ""))
133 if (clock
!= 0) return clock
;
136 uint8_t grph
[MAX_GRAPH_TRACE_LEN
]={0};
137 size_t size
= getFromGraphBuf(grph
);
140 PrintAndLog("Failed to copy from graphbuffer");
143 bool st
= DetectST(grph
, &size
, &clock
);
146 start
= DetectASKClock(grph
, size
, &clock
, 20);
149 PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock
, start
);
150 SetGraphClock(clock
, start
);
154 uint8_t GetPskCarrier(const char str
[], bool printAns
, bool verbose
)
157 uint8_t grph
[MAX_GRAPH_TRACE_LEN
] = {0};
158 size_t size
= getFromGraphBuf(grph
);
161 PrintAndLog("Failed to copy from graphbuffer");
164 carrier
= countFC(grph
, size
, 0);
165 // Only print this message if we're not looping something
167 PrintAndLog("Auto-detected PSK carrier rate: %d", carrier
);
172 int GetPskClock(const char str
[], bool printAns
, bool verbose
)
175 sscanf(str
, "%i", &clock
);
176 if (!strcmp(str
, ""))
179 if (clock
!= 0) return clock
;
181 uint8_t grph
[MAX_GRAPH_TRACE_LEN
] = {0};
182 size_t size
= getFromGraphBuf(grph
);
184 if (verbose
) PrintAndLog("Failed to copy from graphbuffer");
188 clock
= DetectPSKClock_ext(grph
, size
, 0, &start
);
191 PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock
, start
);
193 SetGraphClock(clock
, start
);
197 uint8_t GetNrzClock(const char str
[], bool printAns
, bool verbose
)
200 sscanf(str
, "%i", &clock
);
201 if (!strcmp(str
, ""))
207 uint8_t grph
[MAX_GRAPH_TRACE_LEN
] = {0};
208 size_t size
= getFromGraphBuf(grph
);
211 PrintAndLog("Failed to copy from graphbuffer");
215 clock
= DetectNRZClock_ext(grph
, size
, 0, &start
);
216 // Only print this message if we're not looping something
218 PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock
, start
);
220 SetGraphClock(clock
, start
);
224 //attempt to detect the field clock and bit clock for FSK
225 uint8_t GetFskClock(const char str
[], bool printAns
, bool verbose
)
228 sscanf(str
, "%i", &clock
);
229 if (!strcmp(str
, ""))
231 if (clock
!= 0) return (uint8_t)clock
;
234 uint8_t fc1
=0, fc2
=0, rf1
=0;
235 uint8_t ans
= fskClocks(&fc1
, &fc2
, &rf1
, verbose
);
236 if (ans
== 0) return 0;
237 if ((fc1
==10 && fc2
==8) || (fc1
==8 && fc2
==5)){
238 if (printAns
) PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1
, fc2
, rf1
);
242 PrintAndLog("DEBUG: unknown fsk field clock detected");
243 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1
, fc2
, rf1
);
247 uint8_t fskClocks(uint8_t *fc1
, uint8_t *fc2
, uint8_t *rf1
, bool verbose
)
249 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
] = {0};
250 size_t size
= getFromGraphBuf(BitStream
);
251 if (size
== 0) return 0;
252 uint16_t ans
= countFC(BitStream
, size
, 1);
254 if (verbose
|| g_debugMode
) PrintAndLog("DEBUG: No data found");
257 *fc1
= (ans
>> 8) & 0xFF;
261 *rf1
= detectFSKClk_ext(BitStream
, size
, *fc1
, *fc2
, &start
);
263 if (verbose
|| g_debugMode
) PrintAndLog("DEBUG: Clock detect error");
267 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d | Best Starting Position: %d", *fc1
, *fc2
, *rf1
, start
);
268 SetGraphClock(*rf1
, start
);
272 // test samples are not just noise
273 bool graphJustNoise(int *BitStream
, int size
)
275 //might not be high enough for noisy environments
276 #define THRESHOLD 15;
278 for(int i
=0; i
< size
&& isNoise
; i
++){
279 isNoise
= BitStream
[i
] < THRESHOLD
;