rtl_test.c 9.5 KB

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