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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
/*
* rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
* rtl_test, test and benchmark tool
*
* Copyright (C) 2012-2014 by Steve Markgraf <steve@steve-m.de>
* Copyright (C) 2012-2014 by Kyle Keen <keenerd@gmail.com>
* Copyright (C) 2014 by Michael Tatarinov <kukabu@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#ifdef __APPLE__
#include <sys/time.h>
#else
#include <time.h>
#endif
#ifndef _WIN32
#include <unistd.h>
#else
#include <windows.h>
#include "getopt/getopt.h"
#endif
#include "rtl-sdr.h"
#include "convenience/convenience.h"
#define DEFAULT_SAMPLE_RATE 2048000
#define DEFAULT_BUF_LENGTH (16 * 16384)
#define MINIMAL_BUF_LENGTH 512
#define MAXIMAL_BUF_LENGTH (256 * 16384)
#define MHZ(x) ((x)*1000*1000)
#define PPM_DURATION 10
#define PPM_DUMP_TIME 5
static enum {
NO_BENCHMARK,
TUNER_BENCHMARK,
PPM_BENCHMARK
} test_mode = NO_BENCHMARK;
static int do_exit = 0;
static rtlsdr_dev_t *dev = NULL;
static uint32_t samp_rate = DEFAULT_SAMPLE_RATE;
static uint32_t total_samples = 0;
static uint32_t dropped_samples = 0;
static unsigned int ppm_duration = PPM_DURATION;
void usage(void)
{
fprintf(stderr,
"rtl_test, a benchmark tool for RTL2832 based DVB-T receivers\n\n"
"Usage:\n"
"\t[-s samplerate (default: 2048000 Hz)]\n"
"\t[-d device_index (default: 0)]\n"
"\t[-t enable Elonics E4000 tuner benchmark]\n"
#ifndef _WIN32
"\t[-p[seconds] enable PPM error measurement (default: 10 seconds)]\n"
#endif
"\t[-b output_block_size (default: 16 * 16384)]\n"
"\t[-S force sync output (default: async)]\n");
exit(1);
}
#ifdef _WIN32
BOOL WINAPI
sighandler(int signum)
{
if (CTRL_C_EVENT == signum) {
fprintf(stderr, "Signal caught, exiting!\n");
do_exit = 1;
rtlsdr_cancel_async(dev);
return TRUE;
}
return FALSE;
}
#else
static void sighandler(int signum)
{
fprintf(stderr, "Signal caught, exiting!\n");
do_exit = 1;
rtlsdr_cancel_async(dev);
}
#endif
static void underrun_test(unsigned char *buf, uint32_t len, int mute)
{
uint32_t i, lost = 0;
static uint8_t bcnt, uninit = 1;
if (uninit) {
bcnt = buf[0];
uninit = 0;
}
for (i = 0; i < len; i++) {
if(bcnt != buf[i]) {
lost += (buf[i] > bcnt) ? (buf[i] - bcnt) : (bcnt - buf[i]);
bcnt = buf[i];
}
bcnt++;
}
total_samples += len;
dropped_samples += lost;
if (mute)
return;
if (lost)
printf("lost at least %d bytes\n", lost);
}
#ifndef _WIN32
static int ppm_gettime(struct timespec *ts)
{
int rv = ENOSYS;
#ifdef __unix__
rv = clock_gettime(CLOCK_MONOTONIC, ts);
#elif __APPLE__
struct timeval tv;
rv = gettimeofday(&tv, NULL);
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000;
#endif
return rv;
}
static int ppm_report(uint64_t nsamples, uint64_t interval)
{
double real_rate, ppm;
real_rate = nsamples * 1e9 / interval;
ppm = 1e6 * (real_rate / (double)samp_rate - 1.);
return (int)round(ppm);
}
static void ppm_test(uint32_t len)
{
static uint64_t nsamples = 0;
static uint64_t interval = 0;
static uint64_t nsamples_total = 0;
static uint64_t interval_total = 0;
struct timespec ppm_now;
static struct timespec ppm_recent;
static enum {
PPM_INIT_NO,
PPM_INIT_DUMP,
PPM_INIT_RUN
} ppm_init = PPM_INIT_NO;
ppm_gettime(&ppm_now);
if (ppm_init != PPM_INIT_RUN) {
/*
* Kyle Keen wrote:
* PPM_DUMP_TIME throws out the first N seconds of data.
* The dongle's PPM is usually very bad when first starting up,
* typically incorrect by more than twice the final value.
* Discarding the first few seconds allows the value to stabilize much faster.
*/
if (ppm_init == PPM_INIT_NO) {
ppm_recent.tv_sec = ppm_now.tv_sec + PPM_DUMP_TIME;
ppm_init = PPM_INIT_DUMP;
return;
}
if (ppm_init == PPM_INIT_DUMP && ppm_recent.tv_sec < ppm_now.tv_sec)
return;
ppm_recent.tv_sec = ppm_now.tv_sec;
ppm_recent.tv_nsec = ppm_now.tv_nsec;
ppm_init = PPM_INIT_RUN;
return;
}
nsamples += (uint64_t)(len / 2UL);
interval = (uint64_t)(ppm_now.tv_sec - ppm_recent.tv_sec);
if (interval < ppm_duration)
return;
interval *= 1000000000UL;
interval += (int64_t)(ppm_now.tv_nsec - ppm_recent.tv_nsec);
nsamples_total += nsamples;
interval_total += interval;
printf("real sample rate: %i current PPM: %i cumulative PPM: %i\n",
(int)((1000000000UL * nsamples) / interval),
ppm_report(nsamples, interval),
ppm_report(nsamples_total, interval_total));
ppm_recent.tv_sec = ppm_now.tv_sec;
ppm_recent.tv_nsec = ppm_now.tv_nsec;
nsamples = 0;
}
#endif
static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx)
{
underrun_test(buf, len, 0);
#ifndef _WIN32
if (test_mode == PPM_BENCHMARK)
ppm_test(len);
#endif
}
void e4k_benchmark(void)
{
uint32_t freq, gap_start = 0, gap_end = 0;
uint32_t range_start = 0, range_end = 0;
fprintf(stderr, "Benchmarking E4000 PLL...\n");
/* find tuner range start */
for (freq = MHZ(70); freq > MHZ(1); freq -= MHZ(1)) {
if (rtlsdr_set_center_freq(dev, freq) < 0) {
range_start = freq;
break;
}
}
/* find tuner range end */
for (freq = MHZ(2000); freq < MHZ(2300UL); freq += MHZ(1)) {
if (rtlsdr_set_center_freq(dev, freq) < 0) {
range_end = freq;
break;
}
}
/* find start of L-band gap */
for (freq = MHZ(1000); freq < MHZ(1300); freq += MHZ(1)) {
if (rtlsdr_set_center_freq(dev, freq) < 0) {
gap_start = freq;
break;
}
}
/* find end of L-band gap */
for (freq = MHZ(1300); freq > MHZ(1000); freq -= MHZ(1)) {
if (rtlsdr_set_center_freq(dev, freq) < 0) {
gap_end = freq;
break;
}
}
fprintf(stderr, "E4K range: %i to %i MHz\n",
range_start/MHZ(1) + 1, range_end/MHZ(1) - 1);
fprintf(stderr, "E4K L-band gap: %i to %i MHz\n",
gap_start/MHZ(1), gap_end/MHZ(1));
}
int main(int argc, char **argv)
{
#ifndef _WIN32
struct sigaction sigact;
#endif
int n_read, r, opt, i;
int sync_mode = 0;
uint8_t *buffer;
int dev_index = 0;
int dev_given = 0;
uint32_t out_block_size = DEFAULT_BUF_LENGTH;
int count;
int gains[100];
while ((opt = getopt(argc, argv, "d:s:b:tp::Sh")) != -1) {
switch (opt) {
case 'd':
dev_index = verbose_device_search(optarg);
dev_given = 1;
break;
case 's':
samp_rate = (uint32_t)atof(optarg);
break;
case 'b':
out_block_size = (uint32_t)atof(optarg);
break;
case 't':
test_mode = TUNER_BENCHMARK;
break;
case 'p':
test_mode = PPM_BENCHMARK;
if (optarg)
ppm_duration = atoi(optarg);
break;
case 'S':
sync_mode = 1;
break;
case 'h':
default:
usage();
break;
}
}
if(out_block_size < MINIMAL_BUF_LENGTH ||
out_block_size > MAXIMAL_BUF_LENGTH ){
fprintf(stderr,
"Output block size wrong value, falling back to default\n");
fprintf(stderr,
"Minimal length: %u\n", MINIMAL_BUF_LENGTH);
fprintf(stderr,
"Maximal length: %u\n", MAXIMAL_BUF_LENGTH);
out_block_size = DEFAULT_BUF_LENGTH;
}
buffer = malloc(out_block_size * sizeof(uint8_t));
if (!dev_given) {
dev_index = verbose_device_search("0");
}
if (dev_index < 0) {
exit(1);
}
r = rtlsdr_open(&dev, (uint32_t)dev_index);
if (r < 0) {
fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
exit(1);
}
#ifndef _WIN32
sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGQUIT, &sigact, NULL);
sigaction(SIGPIPE, &sigact, NULL);
#else
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
#endif
count = rtlsdr_get_tuner_gains(dev, NULL);
fprintf(stderr, "Supported gain values (%d): ", count);
count = rtlsdr_get_tuner_gains(dev, gains);
for (i = 0; i < count; i++)
fprintf(stderr, "%.1f ", gains[i] / 10.0);
fprintf(stderr, "\n");
/* Set the sample rate */
verbose_set_sample_rate(dev, samp_rate);
if (test_mode == TUNER_BENCHMARK) {
if (rtlsdr_get_tuner_type(dev) == RTLSDR_TUNER_E4000)
e4k_benchmark();
else
fprintf(stderr, "No E4000 tuner found, aborting.\n");
goto exit;
}
/* Enable test mode */
r = rtlsdr_set_testmode(dev, 1);
/* Reset endpoint before we start reading from it (mandatory) */
verbose_reset_buffer(dev);
if ((test_mode == PPM_BENCHMARK) && !sync_mode) {
fprintf(stderr, "Reporting PPM error measurement every %i seconds...\n", ppm_duration);
fprintf(stderr, "Press ^C after a few minutes.\n");
}
if (test_mode == NO_BENCHMARK) {
fprintf(stderr, "\nInfo: This tool will continuously"
" read from the device, and report if\n"
"samples get lost. If you observe no "
"further output, everything is fine.\n\n");
}
if (sync_mode) {
fprintf(stderr, "Reading samples in sync mode...\n");
fprintf(stderr, "(Samples are being lost but not reported.)\n");
while (!do_exit) {
r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read);
if (r < 0) {
fprintf(stderr, "WARNING: sync read failed.\n");
break;
}
if ((uint32_t)n_read < out_block_size) {
fprintf(stderr, "Short read, samples lost, exiting!\n");
break;
}
underrun_test(buffer, n_read, 1);
}
} else {
fprintf(stderr, "Reading samples in async mode...\n");
r = rtlsdr_read_async(dev, rtlsdr_callback, NULL,
0, out_block_size);
}
if (do_exit) {
fprintf(stderr, "\nUser cancel, exiting...\n");
fprintf(stderr, "Samples per million lost (minimum): %i\n", (int)(1000000L * dropped_samples / total_samples));
}
else
fprintf(stderr, "\nLibrary error %d, exiting...\n", r);
exit:
rtlsdr_close(dev);
free (buffer);
return r >= 0 ? r : -r;
}
|