]>
Commit | Line | Data |
---|---|---|
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 | #define M_PI 3.14159265358979323846264338327 |
7fe9b0b7 | 25 | |
26 | double CursorScaleFactor; | |
7ddb9900 | 27 | int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64; |
7fe9b0b7 | 28 | int offline; |
8d0a3e87 | 29 | int flushAfterWrite = 0; |
9492e0b0 | 30 | extern pthread_mutex_t print_lock; |
31 | ||
7fe9b0b7 | 32 | static char *logfilename = "proxmark3.log"; |
33 | ||
34 | void 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; | |
8d0a3e87 | 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) { | |
8d0a3e87 | 46 | logfile = fopen(logfilename, "a"); |
9492e0b0 | 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 | ||
8d0a3e87 | 85 | if (flushAfterWrite == 1) { |
ed77aabe | 86 | fflush(NULL); |
87 | } | |
9492e0b0 | 88 | //release lock |
89 | pthread_mutex_unlock(&print_lock); | |
7fe9b0b7 | 90 | } |
91 | ||
d16b33fe | 92 | void SetLogFilename(char *fn) { |
7fe9b0b7 | 93 | logfilename = fn; |
94 | } | |
b44e5233 | 95 | |
7bd30f12 | 96 | void iceFsk3(int * data, const size_t len){ |
97 | ||
98 | int i,j; | |
149aeada | 99 | |
100 | int * output = (int* ) malloc(sizeof(int) * len); | |
101 | memset(output, 0x00, len); | |
102 | float fc = 0.1125f; // center frequency | |
081151ea | 103 | size_t adjustedLen = len; |
104 | ||
7bd30f12 | 105 | // create very simple low-pass filter to remove images (2nd-order Butterworth) |
106 | float complex iir_buf[3] = {0,0,0}; | |
107 | float b[3] = {0.003621681514929, 0.007243363029857, 0.003621681514929}; | |
108 | float a[3] = {1.000000000000000, -1.822694925196308, 0.837181651256023}; | |
109 | ||
081151ea | 110 | float sample = 0; // input sample read from file |
111 | float complex x_prime = 1.0f; // save sample for estimating frequency | |
7bd30f12 | 112 | float complex x; |
113 | ||
081151ea | 114 | for (i=0; i<adjustedLen; ++i) { |
7bd30f12 | 115 | |
081151ea | 116 | sample = data[i]+128; |
7bd30f12 | 117 | |
118 | // remove DC offset and mix to complex baseband | |
119 | x = (sample - 127.5f) * cexpf( _Complex_I * 2 * M_PI * fc * i ); | |
120 | ||
121 | // apply low-pass filter, removing spectral image (IIR using direct-form II) | |
122 | iir_buf[2] = iir_buf[1]; | |
123 | iir_buf[1] = iir_buf[0]; | |
124 | iir_buf[0] = x - a[1]*iir_buf[1] - a[2]*iir_buf[2]; | |
125 | x = b[0]*iir_buf[0] + | |
126 | b[1]*iir_buf[1] + | |
127 | b[2]*iir_buf[2]; | |
128 | ||
129 | // compute instantaneous frequency by looking at phase difference | |
130 | // between adjacent samples | |
131 | float freq = cargf(x*conjf(x_prime)); | |
132 | x_prime = x; // retain this sample for next iteration | |
133 | ||
134 | output[i] =(freq > 0)? 10 : -10; | |
135 | } | |
136 | ||
137 | // show data | |
081151ea | 138 | for (j=0; j<adjustedLen; ++j) |
7bd30f12 | 139 | data[j] = output[j]; |
140 | ||
141 | CmdLtrim("30"); | |
081151ea | 142 | adjustedLen -= 30; |
7bd30f12 | 143 | |
144 | // zero crossings. | |
081151ea | 145 | for (j=0; j<adjustedLen; ++j){ |
7bd30f12 | 146 | if ( data[j] == 10) break; |
147 | } | |
148 | int startOne =j; | |
149 | ||
081151ea | 150 | for (;j<adjustedLen; ++j){ |
7bd30f12 | 151 | if ( data[j] == -10 ) break; |
152 | } | |
153 | int stopOne = j-1; | |
154 | ||
155 | int fieldlen = stopOne-startOne; | |
7bd30f12 | 156 | |
fbceacc5 | 157 | fieldlen = (fieldlen == 39 || fieldlen == 41)? 40 : fieldlen; |
158 | fieldlen = (fieldlen == 59 || fieldlen == 51)? 50 : fieldlen; | |
159 | if ( fieldlen != 40 && fieldlen != 50){ | |
160 | printf("Detected field Length: %d \n", fieldlen); | |
081151ea | 161 | printf("Can only handle 40 or 50. Aborting...\n"); |
3f3fdce6 | 162 | free(output); |
fbceacc5 | 163 | return; |
164 | } | |
7bd30f12 | 165 | |
166 | // FSK sequence start == 000111 | |
167 | int startPos = 0; | |
081151ea | 168 | for (i =0; i<adjustedLen; ++i){ |
7bd30f12 | 169 | int dec = 0; |
170 | for ( j = 0; j < 6*fieldlen; ++j){ | |
171 | dec += data[i + j]; | |
172 | } | |
173 | if (dec == 0) { | |
174 | startPos = i; | |
175 | break; | |
176 | } | |
177 | } | |
178 | ||
179 | printf("000111 position: %d \n", startPos); | |
180 | ||
72e930ef | 181 | startPos += 6*fieldlen+5; |
7bd30f12 | 182 | |
72e930ef | 183 | int bit =0; |
7bd30f12 | 184 | printf("BINARY\n"); |
185 | printf("R/40 : "); | |
081151ea | 186 | for (i =startPos ; i < adjustedLen; i += 40){ |
72e930ef | 187 | bit = data[i]>0 ? 1:0; |
188 | printf("%d", bit ); | |
7bd30f12 | 189 | } |
190 | printf("\n"); | |
191 | ||
192 | printf("R/50 : "); | |
081151ea | 193 | for (i =startPos ; i < adjustedLen; i += 50){ |
72e930ef | 194 | bit = data[i]>0 ? 1:0; |
195 | printf("%d", bit ); } | |
7bd30f12 | 196 | printf("\n"); |
197 | ||
149aeada | 198 | free(output); |
7bd30f12 | 199 | } |
200 | ||
201 | float complex cexpf (float complex Z) | |
202 | { | |
203 | float complex Res; | |
204 | double rho = exp (__real__ Z); | |
205 | __real__ Res = rho * cosf(__imag__ Z); | |
206 | __imag__ Res = rho * sinf(__imag__ Z); | |
207 | return Res; | |
208 | } |