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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/*
* This file is part of the bladeRF project:
* http://www.github.com/nuand/bladeRF
*
* Copyright (C) 2013-2018 Nuand LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <libbladeRF.h>
#include "conversions.h"
#include "helpers/file.h"
#include "log.h"
#include "parse.h"
/******************************************************************************/
/* Config file stuff */
/******************************************************************************/
const struct numeric_suffix freq_suffixes[] = { { "G", 1000 * 1000 * 1000 },
{ "GHz", 1000 * 1000 * 1000 },
{ "M", 1000 * 1000 },
{ "MHz", 1000 * 1000 },
{ "k", 1000 },
{ "kHz", 1000 } };
#define NUM_FREQ_SUFFIXES (sizeof(freq_suffixes) / sizeof(freq_suffixes[0]))
#define MAX(a, b) (a > b ? a : b)
#define MIN(a, b) (a < b ? a : b)
static int apply_config_options(struct bladerf *dev, struct config_options opt)
{
int status;
bladerf_frequency freq;
bladerf_bandwidth bw;
uint32_t val;
bool ok;
bladerf_gain_mode gain_mode;
const struct bladerf_range *rx_range = NULL;
const struct bladerf_range *tx_range = NULL;
bladerf_sampling sampling_mode;
bladerf_vctcxo_tamer_mode tamer_mode = BLADERF_VCTCXO_TAMER_INVALID;
struct bladerf_rational_rate rate, actual;
status = BLADERF_ERR_INVAL;
if (!strcasecmp(opt.key, "biastee_tx")) {
bool enable = false;
status = str2bool(opt.value, &enable);
if (status < 0) {
return BLADERF_ERR_INVAL;
}
status = bladerf_set_bias_tee(dev, BLADERF_CHANNEL_TX(0), enable);
if (status < 0) {
return status;
}
} else if (!strcasecmp(opt.key, "biastee_rx")) {
bool enable = false;
status = str2bool(opt.value, &enable);
if (status < 0) {
return BLADERF_ERR_INVAL;
}
status = bladerf_set_bias_tee(dev, BLADERF_CHANNEL_RX(0), enable);
if (status < 0) {
return status;
}
} else if (!strcasecmp(opt.key, "fpga")) {
status = bladerf_load_fpga(dev, opt.value);
if (status < 0) {
log_warning("Config line %d: could not load FPGA from `%s'\n",
opt.lineno, opt.value);
}
return status;
} else if (!strcasecmp(opt.key, "frequency")) {
status =
bladerf_get_frequency_range(dev, BLADERF_CHANNEL_RX(0), &rx_range);
if (status < 0) {
return status;
}
status =
bladerf_get_frequency_range(dev, BLADERF_CHANNEL_TX(0), &tx_range);
if (status < 0) {
return status;
}
freq = str2uint64_suffix(opt.value, MAX(rx_range->min, tx_range->min),
MIN(rx_range->max, tx_range->max),
freq_suffixes, NUM_FREQ_SUFFIXES, &ok);
if (!ok) {
return BLADERF_ERR_INVAL;
}
status = bladerf_set_frequency(dev, BLADERF_CHANNEL_RX(0), freq);
if (status < 0) {
return status;
}
status = bladerf_set_frequency(dev, BLADERF_CHANNEL_TX(0), freq);
} else if (!strcasecmp(opt.key, "samplerate")) {
status = bladerf_get_sample_rate_range(dev, BLADERF_CHANNEL_RX(0),
&rx_range);
if (status < 0) {
return status;
}
status = bladerf_get_sample_rate_range(dev, BLADERF_CHANNEL_TX(0),
&tx_range);
if (status < 0) {
return status;
}
freq = str2uint64_suffix(opt.value, MAX(rx_range->min, tx_range->min),
MIN(rx_range->max, tx_range->max),
freq_suffixes, NUM_FREQ_SUFFIXES, &ok);
if (!ok) {
return BLADERF_ERR_INVAL;
}
rate.integer = freq;
rate.num = 0;
rate.den = 1;
status = bladerf_set_rational_sample_rate(dev, BLADERF_CHANNEL_RX(0),
&rate, &actual);
if (status < 0) {
return status;
}
status = bladerf_set_rational_sample_rate(dev, BLADERF_CHANNEL_TX(0),
&rate, &actual);
} else if (!strcasecmp(opt.key, "bandwidth")) {
status =
bladerf_get_bandwidth_range(dev, BLADERF_CHANNEL_RX(0), &rx_range);
if (status < 0) {
return status;
}
status =
bladerf_get_bandwidth_range(dev, BLADERF_CHANNEL_TX(0), &tx_range);
if (status < 0) {
return status;
}
if (MIN(rx_range->max, tx_range->max) >= UINT32_MAX) {
return BLADERF_ERR_INVAL;
}
bw = str2uint_suffix(
opt.value, (bladerf_bandwidth)MAX(rx_range->min, tx_range->min),
(bladerf_bandwidth)MIN(rx_range->max, tx_range->max), freq_suffixes,
NUM_FREQ_SUFFIXES, &ok);
if (!ok) {
return BLADERF_ERR_INVAL;
}
status = bladerf_set_bandwidth(dev, BLADERF_CHANNEL_RX(0), bw, NULL);
if (status < 0) {
return status;
}
status = bladerf_set_bandwidth(dev, BLADERF_CHANNEL_TX(0), bw, NULL);
} else if (!strcasecmp(opt.key, "agc")) {
bool agcval = false;
status = str2bool(opt.value, &agcval);
if (status != 0) {
return BLADERF_ERR_INVAL;
}
gain_mode = agcval ? BLADERF_GAIN_AUTOMATIC : BLADERF_GAIN_MANUAL;
status = bladerf_set_gain_mode(dev, BLADERF_CHANNEL_RX(0), gain_mode);
} else if (!strcasecmp(opt.key, "gpio")) {
val = str2uint(opt.key, 0, -1, &ok);
if (!ok) {
return BLADERF_ERR_INVAL;
}
status = bladerf_config_gpio_write(dev, val);
} else if (!strcasecmp(opt.key, "sampling")) {
if (!strcasecmp(opt.value, "internal")) {
sampling_mode = BLADERF_SAMPLING_INTERNAL;
} else if (!strcasecmp(opt.value, "external")) {
sampling_mode = BLADERF_SAMPLING_EXTERNAL;
} else {
return BLADERF_ERR_INVAL;
}
status = bladerf_set_sampling(dev, sampling_mode);
} else if (!strcasecmp(opt.key, "trimdac")) {
val = str2uint(opt.value, 0, -1, &ok);
if (!ok) {
return BLADERF_ERR_INVAL;
}
status = bladerf_dac_write(dev, val);
} else if (!strcasecmp(opt.key, "vctcxo_tamer")) {
if (!strcasecmp(opt.value, "disabled") ||
!strcasecmp(opt.value, "off")) {
tamer_mode = BLADERF_VCTCXO_TAMER_DISABLED;
} else if (!strcasecmp(opt.value, "1PPS") ||
!strcasecmp(opt.value, "1 PPS")) {
tamer_mode = BLADERF_VCTCXO_TAMER_1_PPS;
} else if (!strcasecmp(opt.value, "10MHZ") ||
!strcasecmp(opt.value, "10 MHZ")) {
tamer_mode = BLADERF_VCTCXO_TAMER_10_MHZ;
} else if (!strcasecmp(opt.value, "10M")) {
tamer_mode = BLADERF_VCTCXO_TAMER_10_MHZ;
} else {
return BLADERF_ERR_INVAL;
}
status = bladerf_set_vctcxo_tamer_mode(dev, tamer_mode);
} else if (!strcasecmp(opt.key, "clock_ref")) {
bool enable = false;
status = str2bool(opt.value, &enable);
if (status != 0) {
return BLADERF_ERR_INVAL;
}
status = bladerf_set_pll_enable(dev, enable);
} else if (!strcasecmp(opt.key, "refin_freq")) {
status = bladerf_get_pll_refclk_range(dev, &rx_range);
if (status < 0) {
return status;
}
freq = str2uint64_suffix(opt.value, rx_range->min, rx_range->max,
freq_suffixes, NUM_FREQ_SUFFIXES, &ok);
if (!ok) {
return BLADERF_ERR_INVAL;
}
status = bladerf_set_pll_refclk(dev, freq);
} else if (!strcasecmp(opt.key, "clock_sel")) {
bladerf_clock_select clock_sel = CLOCK_SELECT_ONBOARD;
if (!strcasecmp(opt.value, "onboard") ||
!strcasecmp(opt.value, "internal")) {
clock_sel = CLOCK_SELECT_ONBOARD;
} else if (!strcasecmp(opt.value, "external")) {
clock_sel = CLOCK_SELECT_EXTERNAL;
} else {
return BLADERF_ERR_INVAL;
}
status = bladerf_set_clock_select(dev, clock_sel);
} else if (!strcasecmp(opt.key, "clock_out")) {
bool enable = false;
status = str2bool(opt.value, &enable);
if (status != 0) {
return BLADERF_ERR_INVAL;
}
status = bladerf_set_clock_output(dev, enable);
} else {
log_warning("Invalid key `%s' on line %d\n", opt.key, opt.lineno);
}
if (status < 0)
log_warning("Error message for option (%s) on line %d:\n%s\n", opt.key,
opt.lineno, bladerf_strerror(status));
return status;
}
int config_load_options_file(struct bladerf *dev)
{
char *filename = NULL;
int status = 0;
uint8_t *buf = NULL;
size_t buf_size;
int optc;
int j;
struct config_options *optv;
filename = file_find("bladeRF.conf");
if (!filename) {
filename = file_find("bladerf.conf");
/* A missing file that is optional is not an error */
if (!filename) {
return 0;
}
}
status = file_read_buffer(filename, &buf, &buf_size);
if (status < 0) {
goto out;
}
optc = str2options(dev, (const char *)buf, buf_size, &optv);
if (optc < 0) {
status = BLADERF_ERR_INVAL;
goto out_buf;
}
for (j = 0; j < optc; j++) {
status = apply_config_options(dev, optv[j]);
if (status < 0) {
log_warning("Invalid config option `%s' on line %d\n", optv[j].key,
optv[j].lineno);
/* Some config options will require the FPGA to be loaded, however
* this function is called during bladerf_open(). The solution is
* to treat BLADERF_ERR_NOT_INIT as a warning and continue. */
if (status == BLADERF_ERR_NOT_INIT) {
status = 0;
} else {
break;
}
}
}
free_opts(optv, optc);
out_buf:
free(buf);
out:
free(filename);
return status;
}
|