rtl_test.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
  3. * rtl_test, test and benchmark tool
  4. *
  5. * Copyright (C) 2012-2014 by Steve Markgraf <steve@steve-m.de>
  6. * Copyright (C) 2012-2014 by Kyle Keen <keenerd@gmail.com>
  7. * Copyright (C) 2014 by Michael Tatarinov <kukabu@gmail.com>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <errno.h>
  23. #include <signal.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <math.h>
  28. #ifdef __APPLE__
  29. #include <sys/time.h>
  30. #else
  31. #include <time.h>
  32. #endif
  33. #ifndef _WIN32
  34. #include <unistd.h>
  35. #else
  36. #include <windows.h>
  37. #include "getopt/getopt.h"
  38. #endif
  39. #include "rtl-sdr.h"
  40. #include "convenience/convenience.h"
  41. #define DEFAULT_SAMPLE_RATE 2048000
  42. #define DEFAULT_BUF_LENGTH (16 * 16384)
  43. #define MINIMAL_BUF_LENGTH 512
  44. #define MAXIMAL_BUF_LENGTH (256 * 16384)
  45. #define MHZ(x) ((x)*1000*1000)
  46. #define PPM_DURATION 10
  47. #define PPM_DUMP_TIME 5
  48. static enum {
  49. NO_BENCHMARK,
  50. TUNER_BENCHMARK,
  51. PPM_BENCHMARK
  52. } test_mode = NO_BENCHMARK;
  53. static int do_exit = 0;
  54. static rtlsdr_dev_t *dev = NULL;
  55. static uint32_t samp_rate = DEFAULT_SAMPLE_RATE;
  56. static uint32_t total_samples = 0;
  57. static uint32_t dropped_samples = 0;
  58. static unsigned int ppm_duration = PPM_DURATION;
  59. void usage(void)
  60. {
  61. fprintf(stderr,
  62. "rtl_test, a benchmark tool for RTL2832 based DVB-T receivers\n\n"
  63. "Usage:\n"
  64. "\t[-s samplerate (default: 2048000 Hz)]\n"
  65. "\t[-d device_index (default: 0)]\n"
  66. "\t[-t enable Elonics E4000 tuner benchmark]\n"
  67. #ifndef _WIN32
  68. "\t[-p[seconds] enable PPM error measurement (default: 10 seconds)]\n"
  69. #endif
  70. "\t[-b output_block_size (default: 16 * 16384)]\n"
  71. "\t[-S force sync output (default: async)]\n");
  72. exit(1);
  73. }
  74. #ifdef _WIN32
  75. BOOL WINAPI
  76. sighandler(int signum)
  77. {
  78. if (CTRL_C_EVENT == signum) {
  79. fprintf(stderr, "Signal caught, exiting!\n");
  80. do_exit = 1;
  81. rtlsdr_cancel_async(dev);
  82. return TRUE;
  83. }
  84. return FALSE;
  85. }
  86. #else
  87. static void sighandler(int signum)
  88. {
  89. fprintf(stderr, "Signal caught, exiting!\n");
  90. do_exit = 1;
  91. rtlsdr_cancel_async(dev);
  92. }
  93. #endif
  94. static void underrun_test(unsigned char *buf, uint32_t len, int mute)
  95. {
  96. uint32_t i, lost = 0;
  97. static uint8_t bcnt, uninit = 1;
  98. if (uninit) {
  99. bcnt = buf[0];
  100. uninit = 0;
  101. }
  102. for (i = 0; i < len; i++) {
  103. if(bcnt != buf[i]) {
  104. lost += (buf[i] > bcnt) ? (buf[i] - bcnt) : (bcnt - buf[i]);
  105. bcnt = buf[i];
  106. }
  107. bcnt++;
  108. }
  109. total_samples += len;
  110. dropped_samples += lost;
  111. if (mute)
  112. return;
  113. if (lost)
  114. printf("lost at least %d bytes\n", lost);
  115. }
  116. #ifndef _WIN32
  117. static int ppm_gettime(struct timespec *ts)
  118. {
  119. int rv = ENOSYS;
  120. #ifdef __unix__
  121. rv = clock_gettime(CLOCK_MONOTONIC, ts);
  122. #elif __APPLE__
  123. struct timeval tv;
  124. rv = gettimeofday(&tv, NULL);
  125. ts->tv_sec = tv.tv_sec;
  126. ts->tv_nsec = tv.tv_usec * 1000;
  127. #endif
  128. return rv;
  129. }
  130. static int ppm_report(uint64_t nsamples, uint64_t interval)
  131. {
  132. double real_rate, ppm;
  133. real_rate = nsamples * 1e9 / interval;
  134. ppm = 1e6 * (real_rate / (double)samp_rate - 1.);
  135. return (int)round(ppm);
  136. }
  137. static void ppm_test(uint32_t len)
  138. {
  139. static uint64_t nsamples = 0;
  140. static uint64_t interval = 0;
  141. static uint64_t nsamples_total = 0;
  142. static uint64_t interval_total = 0;
  143. struct timespec ppm_now;
  144. static struct timespec ppm_recent;
  145. static enum {
  146. PPM_INIT_NO,
  147. PPM_INIT_DUMP,
  148. PPM_INIT_RUN
  149. } ppm_init = PPM_INIT_NO;
  150. ppm_gettime(&ppm_now);
  151. if (ppm_init != PPM_INIT_RUN) {
  152. /*
  153. * Kyle Keen wrote:
  154. * PPM_DUMP_TIME throws out the first N seconds of data.
  155. * The dongle's PPM is usually very bad when first starting up,
  156. * typically incorrect by more than twice the final value.
  157. * Discarding the first few seconds allows the value to stabilize much faster.
  158. */
  159. if (ppm_init == PPM_INIT_NO) {
  160. ppm_recent.tv_sec = ppm_now.tv_sec + PPM_DUMP_TIME;
  161. ppm_init = PPM_INIT_DUMP;
  162. return;
  163. }
  164. if (ppm_init == PPM_INIT_DUMP && ppm_recent.tv_sec < ppm_now.tv_sec)
  165. return;
  166. ppm_recent.tv_sec = ppm_now.tv_sec;
  167. ppm_recent.tv_nsec = ppm_now.tv_nsec;
  168. ppm_init = PPM_INIT_RUN;
  169. return;
  170. }
  171. nsamples += (uint64_t)(len / 2UL);
  172. interval = (uint64_t)(ppm_now.tv_sec - ppm_recent.tv_sec);
  173. if (interval < ppm_duration)
  174. return;
  175. interval *= 1000000000UL;
  176. interval += (int64_t)(ppm_now.tv_nsec - ppm_recent.tv_nsec);
  177. nsamples_total += nsamples;
  178. interval_total += interval;
  179. printf("real sample rate: %i current PPM: %i cumulative PPM: %i\n",
  180. (int)((1000000000UL * nsamples) / interval),
  181. ppm_report(nsamples, interval),
  182. ppm_report(nsamples_total, interval_total));
  183. ppm_recent.tv_sec = ppm_now.tv_sec;
  184. ppm_recent.tv_nsec = ppm_now.tv_nsec;
  185. nsamples = 0;
  186. }
  187. #endif
  188. static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx)
  189. {
  190. underrun_test(buf, len, 0);
  191. #ifndef _WIN32
  192. if (test_mode == PPM_BENCHMARK)
  193. ppm_test(len);
  194. #endif
  195. }
  196. void e4k_benchmark(void)
  197. {
  198. uint32_t freq, gap_start = 0, gap_end = 0;
  199. uint32_t range_start = 0, range_end = 0;
  200. fprintf(stderr, "Benchmarking E4000 PLL...\n");
  201. /* find tuner range start */
  202. for (freq = MHZ(70); freq > MHZ(1); freq -= MHZ(1)) {
  203. if (rtlsdr_set_center_freq(dev, freq) < 0) {
  204. range_start = freq;
  205. break;
  206. }
  207. }
  208. /* find tuner range end */
  209. for (freq = MHZ(2000); freq < MHZ(2300UL); freq += MHZ(1)) {
  210. if (rtlsdr_set_center_freq(dev, freq) < 0) {
  211. range_end = freq;
  212. break;
  213. }
  214. }
  215. /* find start of L-band gap */
  216. for (freq = MHZ(1000); freq < MHZ(1300); freq += MHZ(1)) {
  217. if (rtlsdr_set_center_freq(dev, freq) < 0) {
  218. gap_start = freq;
  219. break;
  220. }
  221. }
  222. /* find end of L-band gap */
  223. for (freq = MHZ(1300); freq > MHZ(1000); freq -= MHZ(1)) {
  224. if (rtlsdr_set_center_freq(dev, freq) < 0) {
  225. gap_end = freq;
  226. break;
  227. }
  228. }
  229. fprintf(stderr, "E4K range: %i to %i MHz\n",
  230. range_start/MHZ(1) + 1, range_end/MHZ(1) - 1);
  231. fprintf(stderr, "E4K L-band gap: %i to %i MHz\n",
  232. gap_start/MHZ(1), gap_end/MHZ(1));
  233. }
  234. int main(int argc, char **argv)
  235. {
  236. #ifndef _WIN32
  237. struct sigaction sigact;
  238. #endif
  239. int n_read, r, opt, i;
  240. int sync_mode = 0;
  241. uint8_t *buffer;
  242. int dev_index = 0;
  243. int dev_given = 0;
  244. uint32_t out_block_size = DEFAULT_BUF_LENGTH;
  245. int count;
  246. int gains[100];
  247. while ((opt = getopt(argc, argv, "d:s:b:tp::Sh")) != -1) {
  248. switch (opt) {
  249. case 'd':
  250. dev_index = verbose_device_search(optarg);
  251. dev_given = 1;
  252. break;
  253. case 's':
  254. samp_rate = (uint32_t)atof(optarg);
  255. break;
  256. case 'b':
  257. out_block_size = (uint32_t)atof(optarg);
  258. break;
  259. case 't':
  260. test_mode = TUNER_BENCHMARK;
  261. break;
  262. case 'p':
  263. test_mode = PPM_BENCHMARK;
  264. if (optarg)
  265. ppm_duration = atoi(optarg);
  266. break;
  267. case 'S':
  268. sync_mode = 1;
  269. break;
  270. case 'h':
  271. default:
  272. usage();
  273. break;
  274. }
  275. }
  276. if(out_block_size < MINIMAL_BUF_LENGTH ||
  277. out_block_size > MAXIMAL_BUF_LENGTH ){
  278. fprintf(stderr,
  279. "Output block size wrong value, falling back to default\n");
  280. fprintf(stderr,
  281. "Minimal length: %u\n", MINIMAL_BUF_LENGTH);
  282. fprintf(stderr,
  283. "Maximal length: %u\n", MAXIMAL_BUF_LENGTH);
  284. out_block_size = DEFAULT_BUF_LENGTH;
  285. }
  286. buffer = malloc(out_block_size * sizeof(uint8_t));
  287. if (!dev_given) {
  288. dev_index = verbose_device_search("0");
  289. }
  290. if (dev_index < 0) {
  291. exit(1);
  292. }
  293. r = rtlsdr_open(&dev, (uint32_t)dev_index);
  294. if (r < 0) {
  295. fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
  296. exit(1);
  297. }
  298. #ifndef _WIN32
  299. sigact.sa_handler = sighandler;
  300. sigemptyset(&sigact.sa_mask);
  301. sigact.sa_flags = 0;
  302. sigaction(SIGINT, &sigact, NULL);
  303. sigaction(SIGTERM, &sigact, NULL);
  304. sigaction(SIGQUIT, &sigact, NULL);
  305. sigaction(SIGPIPE, &sigact, NULL);
  306. #else
  307. SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
  308. #endif
  309. count = rtlsdr_get_tuner_gains(dev, NULL);
  310. fprintf(stderr, "Supported gain values (%d): ", count);
  311. count = rtlsdr_get_tuner_gains(dev, gains);
  312. for (i = 0; i < count; i++)
  313. fprintf(stderr, "%.1f ", gains[i] / 10.0);
  314. fprintf(stderr, "\n");
  315. /* Set the sample rate */
  316. verbose_set_sample_rate(dev, samp_rate);
  317. if (test_mode == TUNER_BENCHMARK) {
  318. if (rtlsdr_get_tuner_type(dev) == RTLSDR_TUNER_E4000)
  319. e4k_benchmark();
  320. else
  321. fprintf(stderr, "No E4000 tuner found, aborting.\n");
  322. goto exit;
  323. }
  324. /* Enable test mode */
  325. r = rtlsdr_set_testmode(dev, 1);
  326. /* Reset endpoint before we start reading from it (mandatory) */
  327. verbose_reset_buffer(dev);
  328. if ((test_mode == PPM_BENCHMARK) && !sync_mode) {
  329. fprintf(stderr, "Reporting PPM error measurement every %i seconds...\n", ppm_duration);
  330. fprintf(stderr, "Press ^C after a few minutes.\n");
  331. }
  332. if (test_mode == NO_BENCHMARK) {
  333. fprintf(stderr, "\nInfo: This tool will continuously"
  334. " read from the device, and report if\n"
  335. "samples get lost. If you observe no "
  336. "further output, everything is fine.\n\n");
  337. }
  338. if (sync_mode) {
  339. fprintf(stderr, "Reading samples in sync mode...\n");
  340. fprintf(stderr, "(Samples are being lost but not reported.)\n");
  341. while (!do_exit) {
  342. r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read);
  343. if (r < 0) {
  344. fprintf(stderr, "WARNING: sync read failed.\n");
  345. break;
  346. }
  347. if ((uint32_t)n_read < out_block_size) {
  348. fprintf(stderr, "Short read, samples lost, exiting!\n");
  349. break;
  350. }
  351. underrun_test(buffer, n_read, 1);
  352. }
  353. } else {
  354. fprintf(stderr, "Reading samples in async mode...\n");
  355. r = rtlsdr_read_async(dev, rtlsdr_callback, NULL,
  356. 0, out_block_size);
  357. }
  358. if (do_exit) {
  359. fprintf(stderr, "\nUser cancel, exiting...\n");
  360. fprintf(stderr, "Samples per million lost (minimum): %i\n", (int)(1000000L * dropped_samples / total_samples));
  361. }
  362. else
  363. fprintf(stderr, "\nLibrary error %d, exiting...\n", r);
  364. exit:
  365. rtlsdr_close(dev);
  366. free (buffer);
  367. return r >= 0 ? r : -r;
  368. }