]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/ui.c
Call to GetClock had wrong number of inparameters
[proxmark3-svn] / client / ui.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
212ef3a0 2// Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
a553f267 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
7fe9b0b7 12#include <stdarg.h>
51969283 13#include <stdlib.h>
7fe9b0b7 14#include <stdio.h>
f6c18637 15#include <stdbool.h>
7fe9b0b7 16#include <time.h>
51969283 17#include <readline/readline.h>
9492e0b0 18#include <pthread.h>
f6c18637 19#include "loclass/cipherutils.h"
7bd30f12 20#include "ui.h"
081151ea 21#include "cmdmain.h"
22#include "cmddata.h"
8d0a3e87 23#include "graph.h"
7bd30f12 24//#include <liquid/liquid.h>
25#define M_PI 3.14159265358979323846264338327
7fe9b0b7 26
27double CursorScaleFactor;
7ddb9900 28int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64;
7fe9b0b7 29int offline;
8d0a3e87 30int flushAfterWrite = 0;
9492e0b0 31extern pthread_mutex_t print_lock;
32
7fe9b0b7 33static char *logfilename = "proxmark3.log";
34
35void PrintAndLog(char *fmt, ...)
36{
51969283
M
37 char *saved_line;
38 int saved_point;
9492e0b0 39 va_list argptr, argptr2;
40 static FILE *logfile = NULL;
8d0a3e87 41 static int logging = 1;
7fe9b0b7 42
9492e0b0 43 // lock this section to avoid interlacing prints from different threats
44 pthread_mutex_lock(&print_lock);
45
46 if (logging && !logfile) {
8d0a3e87 47 logfile = fopen(logfilename, "a");
9492e0b0 48 if (!logfile) {
49 fprintf(stderr, "Can't open logfile, logging disabled!\n");
50 logging=0;
51 }
52 }
51969283
M
53
54 int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
7fe9b0b7 55
51969283
M
56 if (need_hack) {
57 saved_point = rl_point;
58 saved_line = rl_copy_text(0, rl_end);
59 rl_save_prompt();
60 rl_replace_line("", 0);
61 rl_redisplay();
62 }
63
9492e0b0 64 va_start(argptr, fmt);
65 va_copy(argptr2, argptr);
66 vprintf(fmt, argptr);
67 printf(" "); // cleaning prompt
68 va_end(argptr);
69 printf("\n");
51969283
M
70
71 if (need_hack) {
72 rl_restore_prompt();
73 rl_replace_line(saved_line, 0);
74 rl_point = saved_point;
75 rl_redisplay();
76 free(saved_line);
77 }
78
9492e0b0 79 if (logging && logfile) {
80 vfprintf(logfile, fmt, argptr2);
81 fprintf(logfile,"\n");
82 fflush(logfile);
83 }
84 va_end(argptr2);
85
8d0a3e87 86 if (flushAfterWrite == 1) {
ed77aabe 87 fflush(NULL);
88 }
9492e0b0 89 //release lock
90 pthread_mutex_unlock(&print_lock);
7fe9b0b7 91}
92
93void SetLogFilename(char *fn)
94{
95 logfilename = fn;
96}
f38a1528 97
c6be64da 98int manchester_decode( int * data, const size_t len, uint8_t * dataout, size_t dataoutlen){
f38a1528 99
b44e5233 100 int bitlength = 0;
8d0a3e87 101 int clock, high, low, startindex;
b44e5233 102 low = startindex = 0;
f38a1528 103 high = 1;
c6be64da 104 uint8_t * bitStream = (uint8_t* ) malloc(sizeof(uint8_t) * dataoutlen);
105 memset(bitStream, 0x00, dataoutlen);
b44e5233 106
f38a1528 107 /* Detect high and lows */
8d0a3e87 108 DetectHighLowInGraph(&high, &low, TRUE);
109
f38a1528 110 /* get clock */
21e06301 111 clock = GetClock("",0, 0);
8d0a3e87 112
f6c18637 113 startindex = DetectFirstTransition(data, len, high);
b44e5233 114
b44e5233 115 if (high != 1)
8d0a3e87 116 // decode "raw"
c6be64da 117 bitlength = ManchesterConvertFrom255(data, len, bitStream, dataoutlen, high, low, clock, startindex);
b44e5233 118 else
8d0a3e87 119 // decode manchester
120 bitlength = ManchesterConvertFrom1(data, len, bitStream, dataoutlen, clock, startindex);
b44e5233 121
b44e5233 122 memcpy(dataout, bitStream, bitlength);
149aeada 123 free(bitStream);
b44e5233 124 return bitlength;
125}
b44e5233 126
f6c18637 127 int DetectFirstTransition(const int * data, const size_t len, int threshold){
b44e5233 128
8d0a3e87 129 int i = 0;
f6c18637 130 /* now look for the first threshold */
131 for (; i < len; ++i) {
132 if (data[i] == threshold) {
f38a1528 133 break;
134 }
f6c18637 135 }
136 return i;
b44e5233 137 }
138
c6be64da 139 int ManchesterConvertFrom255(const int * data, const size_t len, uint8_t * dataout, int dataoutlen, int high, int low, int clock, int startIndex){
b44e5233 140
f6c18637 141 int i, j, z, hithigh, hitlow, bitIndex, startType;
142 i = 0;
b44e5233 143 bitIndex = 0;
f6c18637 144
145 int isDamp = 0;
146 int damplimit = (int)((high / 2) * 0.3);
147 int dampHi = (high/2)+damplimit;
148 int dampLow = (high/2)-damplimit;
149 int firstST = 0;
b44e5233 150
f6c18637 151 // i = clock frame of data
c6be64da 152 for (; i < (int)(len/clock); i++)
f38a1528 153 {
f38a1528 154 hithigh = 0;
155 hitlow = 0;
f6c18637 156 startType = -1;
157 z = startIndex + (i*clock);
158 isDamp = 0;
77376577 159
f38a1528 160 /* Find out if we hit both high and low peaks */
161 for (j = 0; j < clock; j++)
f6c18637 162 {
163 if (data[z+j] == high){
f38a1528 164 hithigh = 1;
f6c18637 165 if ( startType == -1)
166 startType = 1;
167 }
168
169 if (data[z+j] == low ){
f38a1528 170 hitlow = 1;
f6c18637 171 if ( startType == -1)
172 startType = 0;
173 }
174
f38a1528 175 if (hithigh && hitlow)
176 break;
b44e5233 177 }
f6c18637 178
179 // No high value found, are we in a dampening field?
180 if ( !hithigh ) {
181 //PrintAndLog(" # Entering damp test at index : %d (%d)", z+j, j);
081151ea 182 for (j = 0; j < clock; j++) {
f6c18637 183 if (
184 (data[z+j] <= dampHi && data[z+j] >= dampLow)
185 ){
77376577 186 isDamp++;
f6c18637 187 }
f6c18637 188 }
189 }
f38a1528 190
f6c18637 191 /* Manchester Switching..
192 0: High -> Low
193 1: Low -> High
194 */
195 if (startType == 0)
196 dataout[bitIndex++] = 1;
197 else if (startType == 1)
198 dataout[bitIndex++] = 0;
199 else
200 dataout[bitIndex++] = 2;
201
77376577 202 if ( isDamp > clock/2 ) {
f6c18637 203 firstST++;
204 }
205
206 if ( firstST == 4)
207 break;
c6be64da 208 if ( bitIndex >= dataoutlen-1 )
209 break;
f38a1528 210 }
b44e5233 211 return bitIndex;
212 }
213
c6be64da 214 int ManchesterConvertFrom1(const int * data, const size_t len, uint8_t * dataout,int dataoutlen, int clock, int startIndex){
b44e5233 215
f6c18637 216 PrintAndLog(" Path B");
217
b44e5233 218 int i,j, bitindex, lc, tolerance, warnings;
219 warnings = 0;
220 int upperlimit = len*2/clock+8;
221 i = startIndex;
222 j = 0;
223 tolerance = clock/4;
224 uint8_t decodedArr[len];
225
f6c18637 226 /* Detect duration between 2 successive transitions */
b44e5233 227 for (bitindex = 1; i < len; i++) {
228
229 if (data[i-1] != data[i]) {
230 lc = i - startIndex;
231 startIndex = i;
232
233 // Error check: if bitindex becomes too large, we do not
234 // have a Manchester encoded bitstream or the clock is really wrong!
235 if (bitindex > upperlimit ) {
236 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
237 return 0;
238 }
239 // Then switch depending on lc length:
240 // Tolerance is 1/4 of clock rate (arbitrary)
241 if (abs((lc-clock)/2) < tolerance) {
242 // Short pulse : either "1" or "0"
243 decodedArr[bitindex++] = data[i-1];
244 } else if (abs(lc-clock) < tolerance) {
245 // Long pulse: either "11" or "00"
246 decodedArr[bitindex++] = data[i-1];
247 decodedArr[bitindex++] = data[i-1];
248 } else {
249 ++warnings;
250 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
251 if (warnings > 10) {
252 PrintAndLog("Error: too many detection errors, aborting.");
253 return 0;
f38a1528 254 }
255 }
256 }
257 }
b44e5233 258
259 /*
260 * We have a decodedArr of "01" ("1") or "10" ("0")
261 * parse it into final decoded dataout
262 */
263 for (i = 0; i < bitindex; i += 2) {
264
265 if ((decodedArr[i] == 0) && (decodedArr[i+1] == 1)) {
266 dataout[j++] = 1;
267 } else if ((decodedArr[i] == 1) && (decodedArr[i+1] == 0)) {
268 dataout[j++] = 0;
269 } else {
f38a1528 270 i++;
271 warnings++;
272 PrintAndLog("Unsynchronized, resync...");
b44e5233 273 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
274
275 if (warnings > 10) {
f38a1528 276 PrintAndLog("Error: too many decode errors, aborting.");
277 return 0;
278 }
279 }
280 }
b44e5233 281
282 PrintAndLog("%s", sprint_hex(dataout, j));
283 return j;
284 }
285
286 void ManchesterDiffDecodedString(const uint8_t* bitstream, size_t len, uint8_t invert){
287 /*
288 * We have a bitstream of "01" ("1") or "10" ("0")
289 * parse it into final decoded bitstream
290 */
291 int i, j, warnings;
292 uint8_t decodedArr[(len/2)+1];
f38a1528 293
b44e5233 294 j = warnings = 0;
f38a1528 295
b44e5233 296 uint8_t lastbit = 0;
f38a1528 297
b44e5233 298 for (i = 0; i < len; i += 2) {
299
300 uint8_t first = bitstream[i];
301 uint8_t second = bitstream[i+1];
f38a1528 302
b44e5233 303 if ( first == second ) {
304 ++i;
305 ++warnings;
306 if (warnings > 10) {
307 PrintAndLog("Error: too many decode errors, aborting.");
308 return;
309 }
310 }
311 else if ( lastbit != first ) {
312 decodedArr[j++] = 0 ^ invert;
313 }
314 else {
315 decodedArr[j++] = 1 ^ invert;
316 }
317 lastbit = second;
318 }
319
320 PrintAndLog("%s", sprint_hex(decodedArr, j));
321}
322
f38a1528 323void PrintPaddedManchester( uint8_t* bitStream, size_t len, size_t blocksize){
324
f6c18637 325 PrintAndLog(" Manchester decoded : %d bits", len);
f38a1528 326
f6c18637 327 uint8_t mod = len % blocksize;
328 uint8_t div = len / blocksize;
329 int i;
330
331 // Now output the bitstream to the scrollback by line of 16 bits
332 for (i = 0; i < div*blocksize; i+=blocksize) {
f38a1528 333 PrintAndLog(" %s", sprint_bin(bitStream+i,blocksize) );
f6c18637 334 }
335
336 if ( mod > 0 )
337 PrintAndLog(" %s", sprint_bin(bitStream+i, mod) );
7bd30f12 338}
339
7bd30f12 340/* Sliding DFT
341 Smooths out
342*/
343void iceFsk2(int * data, const size_t len){
344
345 int i, j;
149aeada 346 int * output = (int* ) malloc(sizeof(int) * len);
347 memset(output, 0x00, len);
348
7bd30f12 349 // for (i=0; i<len-5; ++i){
350 // for ( j=1; j <=5; ++j) {
351 // output[i] += data[i*j];
352 // }
353 // output[i] /= 5;
354 // }
355 int rest = 127;
356 int tmp =0;
357 for (i=0; i<len; ++i){
358 if ( data[i] < 127)
359 output[i] = 0;
360 else {
361 tmp = (100 * (data[i]-rest)) / rest;
362 output[i] = (tmp > 60)? 100:0;
363 }
364 }
365
366 for (j=0; j<len; ++j)
367 data[j] = output[j];
149aeada 368
369 free(output);
7bd30f12 370}
371
372void iceFsk3(int * data, const size_t len){
373
374 int i,j;
149aeada 375
376 int * output = (int* ) malloc(sizeof(int) * len);
377 memset(output, 0x00, len);
378 float fc = 0.1125f; // center frequency
081151ea 379 size_t adjustedLen = len;
380
7bd30f12 381 // create very simple low-pass filter to remove images (2nd-order Butterworth)
382 float complex iir_buf[3] = {0,0,0};
383 float b[3] = {0.003621681514929, 0.007243363029857, 0.003621681514929};
384 float a[3] = {1.000000000000000, -1.822694925196308, 0.837181651256023};
385
081151ea 386 float sample = 0; // input sample read from file
387 float complex x_prime = 1.0f; // save sample for estimating frequency
7bd30f12 388 float complex x;
389
081151ea 390 for (i=0; i<adjustedLen; ++i) {
7bd30f12 391
081151ea 392 sample = data[i]+128;
7bd30f12 393
394 // remove DC offset and mix to complex baseband
395 x = (sample - 127.5f) * cexpf( _Complex_I * 2 * M_PI * fc * i );
396
397 // apply low-pass filter, removing spectral image (IIR using direct-form II)
398 iir_buf[2] = iir_buf[1];
399 iir_buf[1] = iir_buf[0];
400 iir_buf[0] = x - a[1]*iir_buf[1] - a[2]*iir_buf[2];
401 x = b[0]*iir_buf[0] +
402 b[1]*iir_buf[1] +
403 b[2]*iir_buf[2];
404
405 // compute instantaneous frequency by looking at phase difference
406 // between adjacent samples
407 float freq = cargf(x*conjf(x_prime));
408 x_prime = x; // retain this sample for next iteration
409
410 output[i] =(freq > 0)? 10 : -10;
411 }
412
413 // show data
081151ea 414 for (j=0; j<adjustedLen; ++j)
7bd30f12 415 data[j] = output[j];
416
417 CmdLtrim("30");
081151ea 418 adjustedLen -= 30;
7bd30f12 419
420 // zero crossings.
081151ea 421 for (j=0; j<adjustedLen; ++j){
7bd30f12 422 if ( data[j] == 10) break;
423 }
424 int startOne =j;
425
081151ea 426 for (;j<adjustedLen; ++j){
7bd30f12 427 if ( data[j] == -10 ) break;
428 }
429 int stopOne = j-1;
430
431 int fieldlen = stopOne-startOne;
7bd30f12 432
fbceacc5 433 fieldlen = (fieldlen == 39 || fieldlen == 41)? 40 : fieldlen;
434 fieldlen = (fieldlen == 59 || fieldlen == 51)? 50 : fieldlen;
435 if ( fieldlen != 40 && fieldlen != 50){
436 printf("Detected field Length: %d \n", fieldlen);
081151ea 437 printf("Can only handle 40 or 50. Aborting...\n");
fbceacc5 438 return;
439 }
7bd30f12 440
441 // FSK sequence start == 000111
442 int startPos = 0;
081151ea 443 for (i =0; i<adjustedLen; ++i){
7bd30f12 444 int dec = 0;
445 for ( j = 0; j < 6*fieldlen; ++j){
446 dec += data[i + j];
447 }
448 if (dec == 0) {
449 startPos = i;
450 break;
451 }
452 }
453
454 printf("000111 position: %d \n", startPos);
455
72e930ef 456 startPos += 6*fieldlen+5;
7bd30f12 457
72e930ef 458 int bit =0;
7bd30f12 459 printf("BINARY\n");
460 printf("R/40 : ");
081151ea 461 for (i =startPos ; i < adjustedLen; i += 40){
72e930ef 462 bit = data[i]>0 ? 1:0;
463 printf("%d", bit );
7bd30f12 464 }
465 printf("\n");
466
467 printf("R/50 : ");
081151ea 468 for (i =startPos ; i < adjustedLen; i += 50){
72e930ef 469 bit = data[i]>0 ? 1:0;
470 printf("%d", bit ); }
7bd30f12 471 printf("\n");
472
149aeada 473 free(output);
7bd30f12 474}
475
476float complex cexpf (float complex Z)
477{
478 float complex Res;
479 double rho = exp (__real__ Z);
480 __real__ Res = rho * cosf(__imag__ Z);
481 __imag__ Res = rho * sinf(__imag__ Z);
482 return Res;
483}
Impressum, Datenschutz