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