]>
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 | ||
7bd30f12 | 12 | #include "ui.h" |
7fe9b0b7 | 13 | double CursorScaleFactor; |
7ddb9900 | 14 | int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64; |
7fe9b0b7 | 15 | int offline; |
8d0a3e87 | 16 | int flushAfterWrite = 0; |
9492e0b0 | 17 | extern pthread_mutex_t print_lock; |
18 | ||
7fe9b0b7 | 19 | static char *logfilename = "proxmark3.log"; |
20 | ||
21 | void PrintAndLog(char *fmt, ...) | |
22 | { | |
51969283 M |
23 | char *saved_line; |
24 | int saved_point; | |
9492e0b0 | 25 | va_list argptr, argptr2; |
26 | static FILE *logfile = NULL; | |
8d0a3e87 | 27 | static int logging = 1; |
7fe9b0b7 | 28 | |
9492e0b0 | 29 | // lock this section to avoid interlacing prints from different threats |
30 | pthread_mutex_lock(&print_lock); | |
31 | ||
32 | if (logging && !logfile) { | |
8d0a3e87 | 33 | logfile = fopen(logfilename, "a"); |
9492e0b0 | 34 | if (!logfile) { |
35 | fprintf(stderr, "Can't open logfile, logging disabled!\n"); | |
36 | logging=0; | |
37 | } | |
38 | } | |
51969283 M |
39 | |
40 | int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0; | |
7fe9b0b7 | 41 | |
51969283 M |
42 | if (need_hack) { |
43 | saved_point = rl_point; | |
44 | saved_line = rl_copy_text(0, rl_end); | |
45 | rl_save_prompt(); | |
46 | rl_replace_line("", 0); | |
47 | rl_redisplay(); | |
48 | } | |
49 | ||
9492e0b0 | 50 | va_start(argptr, fmt); |
51 | va_copy(argptr2, argptr); | |
52 | vprintf(fmt, argptr); | |
53 | printf(" "); // cleaning prompt | |
54 | va_end(argptr); | |
55 | printf("\n"); | |
51969283 M |
56 | |
57 | if (need_hack) { | |
58 | rl_restore_prompt(); | |
59 | rl_replace_line(saved_line, 0); | |
60 | rl_point = saved_point; | |
61 | rl_redisplay(); | |
62 | free(saved_line); | |
63 | } | |
64 | ||
9492e0b0 | 65 | if (logging && logfile) { |
66 | vfprintf(logfile, fmt, argptr2); | |
67 | fprintf(logfile,"\n"); | |
68 | fflush(logfile); | |
69 | } | |
70 | va_end(argptr2); | |
71 | ||
8d0a3e87 | 72 | if (flushAfterWrite == 1) { |
ed77aabe | 73 | fflush(NULL); |
74 | } | |
9492e0b0 | 75 | //release lock |
76 | pthread_mutex_unlock(&print_lock); | |
7fe9b0b7 | 77 | } |
78 | ||
d16b33fe | 79 | void SetLogFilename(char *fn) { |
7fe9b0b7 | 80 | logfilename = fn; |
81 | } | |
b44e5233 | 82 | |
7bd30f12 | 83 | void iceFsk3(int * data, const size_t len){ |
84 | ||
85 | int i,j; | |
149aeada | 86 | |
87 | int * output = (int* ) malloc(sizeof(int) * len); | |
88 | memset(output, 0x00, len); | |
89 | float fc = 0.1125f; // center frequency | |
081151ea | 90 | size_t adjustedLen = len; |
91 | ||
7bd30f12 | 92 | // create very simple low-pass filter to remove images (2nd-order Butterworth) |
93 | float complex iir_buf[3] = {0,0,0}; | |
94 | float b[3] = {0.003621681514929, 0.007243363029857, 0.003621681514929}; | |
95 | float a[3] = {1.000000000000000, -1.822694925196308, 0.837181651256023}; | |
96 | ||
081151ea | 97 | float sample = 0; // input sample read from file |
98 | float complex x_prime = 1.0f; // save sample for estimating frequency | |
7bd30f12 | 99 | float complex x; |
100 | ||
081151ea | 101 | for (i=0; i<adjustedLen; ++i) { |
7bd30f12 | 102 | |
081151ea | 103 | sample = data[i]+128; |
7bd30f12 | 104 | |
105 | // remove DC offset and mix to complex baseband | |
106 | x = (sample - 127.5f) * cexpf( _Complex_I * 2 * M_PI * fc * i ); | |
107 | ||
108 | // apply low-pass filter, removing spectral image (IIR using direct-form II) | |
109 | iir_buf[2] = iir_buf[1]; | |
110 | iir_buf[1] = iir_buf[0]; | |
111 | iir_buf[0] = x - a[1]*iir_buf[1] - a[2]*iir_buf[2]; | |
112 | x = b[0]*iir_buf[0] + | |
113 | b[1]*iir_buf[1] + | |
114 | b[2]*iir_buf[2]; | |
115 | ||
116 | // compute instantaneous frequency by looking at phase difference | |
117 | // between adjacent samples | |
118 | float freq = cargf(x*conjf(x_prime)); | |
119 | x_prime = x; // retain this sample for next iteration | |
120 | ||
121 | output[i] =(freq > 0)? 10 : -10; | |
122 | } | |
123 | ||
124 | // show data | |
081151ea | 125 | for (j=0; j<adjustedLen; ++j) |
7bd30f12 | 126 | data[j] = output[j]; |
127 | ||
128 | CmdLtrim("30"); | |
081151ea | 129 | adjustedLen -= 30; |
7bd30f12 | 130 | |
131 | // zero crossings. | |
081151ea | 132 | for (j=0; j<adjustedLen; ++j){ |
7bd30f12 | 133 | if ( data[j] == 10) break; |
134 | } | |
135 | int startOne =j; | |
136 | ||
081151ea | 137 | for (;j<adjustedLen; ++j){ |
7bd30f12 | 138 | if ( data[j] == -10 ) break; |
139 | } | |
140 | int stopOne = j-1; | |
141 | ||
142 | int fieldlen = stopOne-startOne; | |
7bd30f12 | 143 | |
fbceacc5 | 144 | fieldlen = (fieldlen == 39 || fieldlen == 41)? 40 : fieldlen; |
145 | fieldlen = (fieldlen == 59 || fieldlen == 51)? 50 : fieldlen; | |
146 | if ( fieldlen != 40 && fieldlen != 50){ | |
147 | printf("Detected field Length: %d \n", fieldlen); | |
081151ea | 148 | printf("Can only handle 40 or 50. Aborting...\n"); |
3f3fdce6 | 149 | free(output); |
fbceacc5 | 150 | return; |
151 | } | |
7bd30f12 | 152 | |
153 | // FSK sequence start == 000111 | |
154 | int startPos = 0; | |
081151ea | 155 | for (i =0; i<adjustedLen; ++i){ |
7bd30f12 | 156 | int dec = 0; |
157 | for ( j = 0; j < 6*fieldlen; ++j){ | |
158 | dec += data[i + j]; | |
159 | } | |
160 | if (dec == 0) { | |
161 | startPos = i; | |
162 | break; | |
163 | } | |
164 | } | |
165 | ||
166 | printf("000111 position: %d \n", startPos); | |
167 | ||
72e930ef | 168 | startPos += 6*fieldlen+5; |
7bd30f12 | 169 | |
72e930ef | 170 | int bit =0; |
7bd30f12 | 171 | printf("BINARY\n"); |
172 | printf("R/40 : "); | |
081151ea | 173 | for (i =startPos ; i < adjustedLen; i += 40){ |
72e930ef | 174 | bit = data[i]>0 ? 1:0; |
175 | printf("%d", bit ); | |
7bd30f12 | 176 | } |
177 | printf("\n"); | |
178 | ||
179 | printf("R/50 : "); | |
081151ea | 180 | for (i =startPos ; i < adjustedLen; i += 50){ |
72e930ef | 181 | bit = data[i]>0 ? 1:0; |
182 | printf("%d", bit ); } | |
7bd30f12 | 183 | printf("\n"); |
184 | ||
149aeada | 185 | free(output); |
7bd30f12 | 186 | } |
187 | ||
188 | float complex cexpf (float complex Z) | |
189 | { | |
190 | float complex Res; | |
191 | double rho = exp (__real__ Z); | |
192 | __real__ Res = rho * cosf(__imag__ Z); | |
193 | __imag__ Res = rho * sinf(__imag__ Z); | |
194 | return Res; | |
195 | } |