]>
Commit | Line | Data |
---|---|---|
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 <stdbool.h> | |
16 | #include <time.h> | |
17 | #include <readline/readline.h> | |
18 | #include <pthread.h> | |
19 | #include "loclass/cipherutils.h" | |
20 | #include "ui.h" | |
21 | #include "cmdmain.h" | |
22 | #include "cmddata.h" | |
23 | #include "graph.h" | |
24 | #define M_PI 3.14159265358979323846264338327 | |
25 | ||
26 | double CursorScaleFactor; | |
27 | int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64; | |
28 | int offline; | |
29 | int flushAfterWrite = 0; | |
30 | extern pthread_mutex_t print_lock; | |
31 | ||
32 | static char *logfilename = "proxmark3.log"; | |
33 | ||
34 | void PrintAndLog(char *fmt, ...) | |
35 | { | |
36 | char *saved_line; | |
37 | int saved_point; | |
38 | va_list argptr, argptr2; | |
39 | static FILE *logfile = NULL; | |
40 | static int logging = 1; | |
41 | ||
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 | } | |
52 | ||
53 | int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0; | |
54 | ||
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 | ||
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"); | |
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 | ||
78 | if (logging && logfile) { | |
79 | vfprintf(logfile, fmt, argptr2); | |
80 | fprintf(logfile,"\n"); | |
81 | fflush(logfile); | |
82 | } | |
83 | va_end(argptr2); | |
84 | ||
85 | if (flushAfterWrite == 1) { | |
86 | fflush(NULL); | |
87 | } | |
88 | //release lock | |
89 | pthread_mutex_unlock(&print_lock); | |
90 | } | |
91 | ||
92 | void SetLogFilename(char *fn) { | |
93 | logfilename = fn; | |
94 | } | |
95 | ||
96 | void iceFsk3(int * data, const size_t len){ | |
97 | ||
98 | int i,j; | |
99 | ||
100 | int * output = (int* ) malloc(sizeof(int) * len); | |
101 | memset(output, 0x00, len); | |
102 | float fc = 0.1125f; // center frequency | |
103 | size_t adjustedLen = len; | |
104 | ||
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 | ||
110 | float sample = 0; // input sample read from file | |
111 | float complex x_prime = 1.0f; // save sample for estimating frequency | |
112 | float complex x; | |
113 | ||
114 | for (i=0; i<adjustedLen; ++i) { | |
115 | ||
116 | sample = data[i]+128; | |
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 | |
138 | for (j=0; j<adjustedLen; ++j) | |
139 | data[j] = output[j]; | |
140 | ||
141 | CmdLtrim("30"); | |
142 | adjustedLen -= 30; | |
143 | ||
144 | // zero crossings. | |
145 | for (j=0; j<adjustedLen; ++j){ | |
146 | if ( data[j] == 10) break; | |
147 | } | |
148 | int startOne =j; | |
149 | ||
150 | for (;j<adjustedLen; ++j){ | |
151 | if ( data[j] == -10 ) break; | |
152 | } | |
153 | int stopOne = j-1; | |
154 | ||
155 | int fieldlen = stopOne-startOne; | |
156 | ||
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); | |
161 | printf("Can only handle 40 or 50. Aborting...\n"); | |
162 | free(output); | |
163 | return; | |
164 | } | |
165 | ||
166 | // FSK sequence start == 000111 | |
167 | int startPos = 0; | |
168 | for (i =0; i<adjustedLen; ++i){ | |
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 | ||
181 | startPos += 6*fieldlen+5; | |
182 | ||
183 | int bit =0; | |
184 | printf("BINARY\n"); | |
185 | printf("R/40 : "); | |
186 | for (i =startPos ; i < adjustedLen; i += 40){ | |
187 | bit = data[i]>0 ? 1:0; | |
188 | printf("%d", bit ); | |
189 | } | |
190 | printf("\n"); | |
191 | ||
192 | printf("R/50 : "); | |
193 | for (i =startPos ; i < adjustedLen; i += 50){ | |
194 | bit = data[i]>0 ? 1:0; | |
195 | printf("%d", bit ); } | |
196 | printf("\n"); | |
197 | ||
198 | free(output); | |
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 | } |