-
Notifications
You must be signed in to change notification settings - Fork 14
/
fastcard.c
211 lines (178 loc) · 5.86 KB
/
fastcard.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <stdlib.h>
#include <string.h>
#include <volk/volk.h>
#include "fastcard.h"
#include "raw_reader.h"
#include "card_reader.h"
#include "rtlsdr_reader.h"
fastcard_t* fastcard_new(fargs_t* args) {
if (args->history_len > args->block_len) {
fprintf(stderr, "History length cannot be larger than block length.\n");
return NULL;
}
FILE* in;
if (strlen(args->input_file) == 0 || strcmp(args->input_file, "-") == 0) {
in = stdin;
} else if (strcmp(args->input_file, "rtlsdr") == 0) {
in = NULL;
} else {
in = fopen(args->input_file, "rb");
if (in == NULL) {
perror("failed to open input file");
return NULL;
}
}
if (args->wisdom_file != NULL && strlen(args->wisdom_file) > 0) {
int r = fftwf_import_wisdom_from_filename(args->wisdom_file);
if (r == 0) {
fprintf(stderr, "failed to import wisdom file\n");
}
}
fastcard_t* fc = malloc(sizeof(fastcard_t));
if (fc == NULL) {
return NULL;
}
// ensure free will only free the variables that have been initialized
fc->in = in;
fc->args = args;
fc->data.block = NULL;
fc->reader = NULL;
fc->samples_to_fft = NULL;
fc->data.fft_power = NULL;
fc->keep_running = true;
// init stuff
rawconv_init(&fc->rawconv);
fc->data.block = reader_block_new(args->block_len);
if (fc->data.block == NULL) {
goto fail;
}
reader_settings_t reader_settings;
reader_settings.output = fc->data.block;
reader_settings.block_size = args->block_len;
reader_settings.history_size = args->history_len;
if (in == NULL) {
rtlsdr_settings_t sdr_settings;
sdr_settings.sample_rate = args->sdr_sample_rate;
sdr_settings.gain = args->sdr_gain;
sdr_settings.frequency = args->sdr_freq;
sdr_settings.dev_index = args->sdr_dev_index;
fc->reader = rtlsdr_reader_new(reader_settings, &sdr_settings);
args->sdr_gain = sdr_settings.gain; // get exact gain
} else {
if (args->input_card) {
fc->reader = card_reader_new(reader_settings, in);
// don't skip blocks when reading from .card file
args->skip = 0;
} else {
fc->reader = raw_reader_new(reader_settings, in);
}
}
if (fc->reader == NULL) {
goto fail;
}
fc->samples_to_fft = fft_new(args->block_len, true);
fc->data.samples = fft_get_input(fc->samples_to_fft);
fc->data.fft = fft_get_output(fc->samples_to_fft);
size_t alignment = volk_get_alignment();
fc->data.fft_power = (float*) volk_malloc(args->block_len * sizeof(float),
alignment);
if (fc->data.fft_power == NULL) {
goto fail;
}
fc->cardet_settings.threshold_constant = args->threshold_const;
fc->cardet_settings.threshold_snr = args->threshold_snr;
fc->cardet_settings.carrier_freq_min = args->carrier_freq_min;
fc->cardet_settings.carrier_freq_max = args->carrier_freq_max;
fc->cardet_settings.fft_len = args->block_len;
if (cardet_normalize_window(&fc->cardet_settings) != 0) {
goto fail;
}
args->carrier_freq_min = fc->cardet_settings.carrier_freq_min;
args->carrier_freq_max = fc->cardet_settings.carrier_freq_max;
if (args->skip > 0) {
fc->data.block->index = ((int64_t)args->skip)*-1 - 1;
}
return fc;
fail:
fastcard_free(fc);
return NULL;
}
void fastcard_free(fastcard_t* fc) {
if (fc->in && fc->in != stdin) {
fclose(fc->in);
}
reader_block_free(fc->data.block);
reader_free(fc->reader);
fft_free(fc->samples_to_fft);
free(fc->data.fft_power);
free(fc);
}
int fastcard_start(fastcard_t* fc) {
reader_start(fc->reader);
return 0;
}
static void fastcard_stop(fastcard_t* fc) {
reader_stop(fc->reader);
// TODO: move this somewhere else
if (fc->args->wisdom_file != NULL && strlen(fc->args->wisdom_file) > 0) {
int r = fftwf_export_wisdom_to_filename(fc->args->wisdom_file);
if (r == 0) {
fprintf(stderr, "failed to export wisdom file\n");
}
}
}
// Read the next block of data
int fastcard_next(fastcard_t* fc) {
if (!fc->keep_running) {
fastcard_stop(fc);
return 1;
}
int ret = reader_next(fc->reader);
if (ret != 0) {
if (ret != 1) {
fprintf(stderr, "reader_next() failed\n");
}
fastcard_stop(fc);
return ret;
}
fastcard_data_t* d = &fc->data;
// TODO: move rawconv to reader
rawconv_to_complex(&fc->rawconv,
d->samples,
fc->data.block->raw_samples,
fc->args->block_len);
return 0;
}
// Process the last block of data.
// Should be called after fastcard_next.
int fastcard_process(fastcard_t* fc, const fastcard_data_t ** data) {
fastcard_data_t* d = &fc->data;
fft_perform(fc->samples_to_fft);
volk_32fc_magnitude_squared_32f_a(d->fft_power,
(lv_32fc_t*)d->fft,
fc->args->block_len);
d->detected = cardet_detect(&fc->cardet_settings,
&d->detection,
d->fft_power);
*data = d;
return 0;
}
// Convenience function for reading and processing the next block of data.
int fastcard_process_next(fastcard_t* fc, const fastcard_data_t ** data) {
int ret = fastcard_next(fc);
if (ret != 0) {
return ret;
}
ret = fastcard_process(fc, data);
return ret;
}
void fastcard_cancel(fastcard_t* fc) {
fc->keep_running = false;
reader_cancel(fc->reader);
}
void fastcard_print_stats(fastcard_t* fc, FILE* out) {
if (fc->in == NULL) {
// print SDR buffer histogram
rtlsdr_reader_print_histogram(fc->reader, out);
}
}