]> git.zerfleddert.de Git - proxmark3-svn/blob - client/ui.c
First check in.
[proxmark3-svn] / client / ui.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 //
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
7 // the license.
8 //-----------------------------------------------------------------------------
9 // UI utilities
10 //-----------------------------------------------------------------------------
11
12 #include <stdarg.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <time.h>
16 #include <readline/readline.h>
17 #include <pthread.h>
18
19 #include "ui.h"
20
21 double CursorScaleFactor;
22 int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64;
23 int offline;
24 int flushAfterWrite = 0; //buzzy
25 extern pthread_mutex_t print_lock;
26
27 static char *logfilename = "proxmark3.log";
28
29 void PrintAndLog(char *fmt, ...)
30 {
31 char *saved_line;
32 int saved_point;
33 va_list argptr, argptr2;
34 static FILE *logfile = NULL;
35 static int logging=1;
36
37 // lock this section to avoid interlacing prints from different threats
38 pthread_mutex_lock(&print_lock);
39
40 if (logging && !logfile) {
41 logfile=fopen(logfilename, "a");
42 if (!logfile) {
43 fprintf(stderr, "Can't open logfile, logging disabled!\n");
44 logging=0;
45 }
46 }
47
48 int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
49
50 if (need_hack) {
51 saved_point = rl_point;
52 saved_line = rl_copy_text(0, rl_end);
53 rl_save_prompt();
54 rl_replace_line("", 0);
55 rl_redisplay();
56 }
57
58 va_start(argptr, fmt);
59 va_copy(argptr2, argptr);
60 vprintf(fmt, argptr);
61 printf(" "); // cleaning prompt
62 va_end(argptr);
63 printf("\n");
64
65 if (need_hack) {
66 rl_restore_prompt();
67 rl_replace_line(saved_line, 0);
68 rl_point = saved_point;
69 rl_redisplay();
70 free(saved_line);
71 }
72
73 if (logging && logfile) {
74 vfprintf(logfile, fmt, argptr2);
75 fprintf(logfile,"\n");
76 fflush(logfile);
77 }
78 va_end(argptr2);
79
80 if (flushAfterWrite == 1) //buzzy
81 {
82 fflush(NULL);
83 }
84 //release lock
85 pthread_mutex_unlock(&print_lock);
86 }
87
88
89 void SetLogFilename(char *fn)
90 {
91 logfilename = fn;
92 }
93
94
95 uint8_t manchester_decode(const uint8_t * data, const size_t len, uint8_t * dataout){
96
97 size_t bytelength = len;
98
99 uint8_t bitStream[bytelength];
100 memset(bitStream, 0x00, bytelength);
101
102 int clock,high, low, bit, hithigh, hitlow, first, bit2idx, lastpeak;
103 int i,invert, lastval;
104 int bitidx = 0;
105 int lc = 0;
106 int warnings = 0;
107 high = 1;
108 low = bit = bit2idx = lastpeak = invert = lastval = hithigh = hitlow = first = 0;
109 clock = 0xFFFF;
110
111 /* Detect high and lows */
112 for (i = 0; i < bytelength; i++) {
113 if (data[i] > high)
114 high = data[i];
115 else if (data[i] < low)
116 low = data[i];
117 }
118
119 /* get clock */
120 int j=0;
121 for (i = 1; i < bytelength; i++) {
122 /* if this is the beginning of a peak */
123 j = i-1;
124 if ( data[j] != data[i] &&
125 data[i] == high)
126 {
127 /* find lowest difference between peaks */
128 if (lastpeak && i - lastpeak < clock)
129 clock = i - lastpeak;
130 lastpeak = i;
131 }
132 }
133
134 int tolerance = clock/4;
135 PrintAndLog(" Detected clock: %d",clock);
136
137 /* Detect first transition */
138 /* Lo-Hi (arbitrary) */
139 /* skip to the first high */
140 for (i= 0; i < bytelength; i++)
141 if (data[i] == high)
142 break;
143
144 /* now look for the first low */
145 for (; i < bytelength; i++) {
146 if (data[i] == low) {
147 lastval = i;
148 break;
149 }
150 }
151
152 /* If we're not working with 1/0s, demod based off clock */
153 if (high != 1)
154 {
155 bit = 0; /* We assume the 1st bit is zero, it may not be
156 * the case: this routine (I think) has an init problem.
157 * Ed.
158 */
159 for (; i < (int)(bytelength / clock); i++)
160 {
161 hithigh = 0;
162 hitlow = 0;
163 first = 1;
164
165 /* Find out if we hit both high and low peaks */
166 for (j = 0; j < clock; j++)
167 {
168 if (data[(i * clock) + j] == high)
169 hithigh = 1;
170 else if (data[(i * clock) + j] == low)
171 hitlow = 1;
172
173 /* it doesn't count if it's the first part of our read
174 because it's really just trailing from the last sequence */
175 if (first && (hithigh || hitlow))
176 hithigh = hitlow = 0;
177 else
178 first = 0;
179
180 if (hithigh && hitlow)
181 break;
182 }
183
184 /* If we didn't hit both high and low peaks, we had a bit transition */
185 if (!hithigh || !hitlow)
186 bit ^= 1;
187
188 bitStream[bit2idx++] = bit ^ invert;
189 }
190 }
191 /* standard 1/0 bitstream */
192 else {
193 /* Then detect duration between 2 successive transitions */
194 for (bitidx = 1; i < bytelength; i++) {
195
196 if (data[i-1] != data[i]) {
197 lc = i-lastval;
198 lastval = i;
199
200 // Error check: if bitidx becomes too large, we do not
201 // have a Manchester encoded bitstream or the clock is really
202 // wrong!
203 if (bitidx > (bytelength*2/clock+8) ) {
204 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
205 return 0;
206 }
207 // Then switch depending on lc length:
208 // Tolerance is 1/4 of clock rate (arbitrary)
209 if (abs(lc-clock/2) < tolerance) {
210 // Short pulse : either "1" or "0"
211 bitStream[bitidx++] = data[i-1];
212 } else if (abs(lc-clock) < tolerance) {
213 // Long pulse: either "11" or "00"
214 bitStream[bitidx++] = data[i-1];
215 bitStream[bitidx++] = data[i-1];
216 } else {
217 // Error
218 warnings++;
219 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
220 if (warnings > 10) {
221 PrintAndLog("Error: too many detection errors, aborting.");
222 return 0;
223 }
224 }
225 }
226 }
227 }
228 // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
229 // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
230 // to stop output at the final bitidx2 value, not bitidx
231 for (i = 0; i < bitidx; i += 2) {
232 if ((bitStream[i] == 0) && (bitStream[i+1] == 1)) {
233 bitStream[bit2idx++] = 1 ^ invert;
234 }
235 else if ((bitStream[i] == 1) && (bitStream[i+1] == 0)) {
236 bitStream[bit2idx++] = 0 ^ invert;
237 }
238 else {
239 // We cannot end up in this state, this means we are unsynchronized,
240 // move up 1 bit:
241 i++;
242 warnings++;
243 PrintAndLog("Unsynchronized, resync...");
244 if (warnings > 10) {
245 PrintAndLog("Error: too many decode errors, aborting.");
246 return 0;
247 }
248 }
249 }
250
251 // PrintAndLog(" Manchester decoded bitstream : %d bits", (bit2idx-16));
252 // uint8_t mod = (bit2idx-16) % blocksize;
253 // uint8_t div = (bit2idx-16) / blocksize;
254
255 // // Now output the bitstream to the scrollback by line of 16 bits
256 // for (i = 0; i < div*blocksize; i+=blocksize) {
257 // PrintAndLog(" %s", sprint_bin(bitStream+i,blocksize) );
258 // }
259 // if ( mod > 0 ){
260 // PrintAndLog(" %s", sprint_bin(bitStream+i, mod) );
261 // }
262
263 if ( bit2idx > 0 )
264 memcpy(dataout, bitStream, bit2idx);
265
266 free(bitStream);
267 return bit2idx;
268 }
269
270 void PrintPaddedManchester( uint8_t* bitStream, size_t len, size_t blocksize){
271
272 PrintAndLog(" Manchester decoded bitstream : %d bits", len);
273
274 uint8_t mod = len % blocksize;
275 uint8_t div = len / blocksize;
276 int i;
277 // Now output the bitstream to the scrollback by line of 16 bits
278 for (i = 0; i < div*blocksize; i+=blocksize) {
279 PrintAndLog(" %s", sprint_bin(bitStream+i,blocksize) );
280 }
281 if ( mod > 0 ){
282 PrintAndLog(" %s", sprint_bin(bitStream+i, mod) );
283 }
284 }
Impressum, Datenschutz