rtl_test.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #ifndef _WIN32
  24. #include <unistd.h>
  25. #else
  26. #include <Windows.h>
  27. #include "getopt/getopt.h"
  28. #endif
  29. #include "rtl-sdr.h"
  30. #define DEFAULT_SAMPLE_RATE 2048000
  31. #define DEFAULT_ASYNC_BUF_NUMBER 32
  32. #define DEFAULT_BUF_LENGTH (16 * 16384)
  33. #define MINIMAL_BUF_LENGTH 512
  34. #define MAXIMAL_BUF_LENGTH (256 * 16384)
  35. #define MHZ(x) ((x)*1000*1000)
  36. static int do_exit = 0;
  37. static rtlsdr_dev_t *dev = NULL;
  38. void usage(void)
  39. {
  40. fprintf(stderr,
  41. "rtl_test, a benchmark tool for RTL2832 based DVB-T receivers\n\n"
  42. "Usage:\n"
  43. "\t[-s samplerate (default: 2048000 Hz)]\n"
  44. "\t[-d device_index (default: 0)]\n"
  45. "\t[-t enable Elonics E4000 tuner benchmark]\n"
  46. "\t[-b output_block_size (default: 16 * 16384)]\n"
  47. "\t[-S force sync output (default: async)]\n");
  48. exit(1);
  49. }
  50. #ifdef _WIN32
  51. BOOL WINAPI
  52. sighandler(int signum)
  53. {
  54. if (CTRL_C_EVENT == signum) {
  55. fprintf(stderr, "Signal caught, exiting!\n");
  56. do_exit = 1;
  57. rtlsdr_cancel_async(dev);
  58. return TRUE;
  59. }
  60. return FALSE;
  61. }
  62. #else
  63. static void sighandler(int signum)
  64. {
  65. fprintf(stderr, "Signal caught, exiting!\n");
  66. do_exit = 1;
  67. rtlsdr_cancel_async(dev);
  68. }
  69. #endif
  70. uint8_t bcnt, uninit = 1;
  71. static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx)
  72. {
  73. uint32_t i, lost = 0;
  74. if (uninit) {
  75. bcnt = buf[0];
  76. uninit = 0;
  77. }
  78. for (i = 0; i < len; i++) {
  79. if(bcnt != buf[i]) {
  80. lost += (buf[i] > bcnt) ? (buf[i] - bcnt) : (bcnt - buf[i]);
  81. bcnt = buf[i];
  82. }
  83. bcnt++;
  84. }
  85. if (lost)
  86. printf("lost at least %d bytes\n", lost);
  87. }
  88. void e4k_benchmark(void)
  89. {
  90. uint32_t freq, gap_start = 0, gap_end = 0;
  91. uint32_t range_start = 0, range_end = 0;
  92. fprintf(stderr, "Benchmarking E4000 PLL...\n");
  93. /* find tuner range start */
  94. for (freq = MHZ(70); freq > MHZ(1); freq -= MHZ(1)) {
  95. if (rtlsdr_set_center_freq(dev, freq) < 0) {
  96. range_start = freq;
  97. break;
  98. }
  99. }
  100. /* find tuner range end */
  101. for (freq = MHZ(2000); freq < MHZ(2300UL); freq += MHZ(1)) {
  102. if (rtlsdr_set_center_freq(dev, freq) < 0) {
  103. range_end = freq;
  104. break;
  105. }
  106. }
  107. /* find start of L-band gap */
  108. for (freq = MHZ(1000); freq < MHZ(1300); freq += MHZ(1)) {
  109. if (rtlsdr_set_center_freq(dev, freq) < 0) {
  110. gap_start = freq;
  111. break;
  112. }
  113. }
  114. /* find end of L-band gap */
  115. for (freq = MHZ(1300); freq > MHZ(1000); freq -= MHZ(1)) {
  116. if (rtlsdr_set_center_freq(dev, freq) < 0) {
  117. gap_end = freq;
  118. break;
  119. }
  120. }
  121. fprintf(stderr, "E4K range: %i to %i MHz\n",
  122. range_start/MHZ(1) + 1, range_end/MHZ(1) - 1);
  123. fprintf(stderr, "E4K L-band gap: %i to %i MHz\n",
  124. gap_start/MHZ(1), gap_end/MHZ(1));
  125. }
  126. int main(int argc, char **argv)
  127. {
  128. #ifndef _WIN32
  129. struct sigaction sigact;
  130. #endif
  131. int n_read;
  132. int r, opt;
  133. int i, tuner_benchmark = 0;
  134. int sync_mode = 0;
  135. uint8_t *buffer;
  136. uint32_t dev_index = 0;
  137. uint32_t samp_rate = DEFAULT_SAMPLE_RATE;
  138. uint32_t out_block_size = DEFAULT_BUF_LENGTH;
  139. int device_count;
  140. while ((opt = getopt(argc, argv, "d:s:b:tS::")) != -1) {
  141. switch (opt) {
  142. case 'd':
  143. dev_index = atoi(optarg);
  144. break;
  145. case 's':
  146. samp_rate = (uint32_t)atof(optarg);
  147. break;
  148. case 'b':
  149. out_block_size = (uint32_t)atof(optarg);
  150. break;
  151. case 't':
  152. tuner_benchmark = 1;
  153. break;
  154. case 'S':
  155. sync_mode = 1;
  156. break;
  157. default:
  158. usage();
  159. break;
  160. }
  161. }
  162. if(out_block_size < MINIMAL_BUF_LENGTH ||
  163. out_block_size > MAXIMAL_BUF_LENGTH ){
  164. fprintf(stderr,
  165. "Output block size wrong value, falling back to default\n");
  166. fprintf(stderr,
  167. "Minimal length: %u\n", MINIMAL_BUF_LENGTH);
  168. fprintf(stderr,
  169. "Maximal length: %u\n", MAXIMAL_BUF_LENGTH);
  170. out_block_size = DEFAULT_BUF_LENGTH;
  171. }
  172. buffer = malloc(out_block_size * sizeof(uint8_t));
  173. device_count = rtlsdr_get_device_count();
  174. if (!device_count) {
  175. fprintf(stderr, "No supported devices found.\n");
  176. exit(1);
  177. }
  178. fprintf(stderr, "Found %d device(s):\n", device_count);
  179. for (i = 0; i < device_count; i++)
  180. fprintf(stderr, " %d: %s\n", i, rtlsdr_get_device_name(i));
  181. fprintf(stderr, "\n");
  182. fprintf(stderr, "Using device %d: %s\n",
  183. dev_index,
  184. rtlsdr_get_device_name(dev_index));
  185. r = rtlsdr_open(&dev, dev_index);
  186. if (r < 0) {
  187. fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
  188. exit(1);
  189. }
  190. #ifndef _WIN32
  191. sigact.sa_handler = sighandler;
  192. sigemptyset(&sigact.sa_mask);
  193. sigact.sa_flags = 0;
  194. sigaction(SIGINT, &sigact, NULL);
  195. sigaction(SIGTERM, &sigact, NULL);
  196. sigaction(SIGQUIT, &sigact, NULL);
  197. sigaction(SIGPIPE, &sigact, NULL);
  198. #else
  199. SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
  200. #endif
  201. /* Set the sample rate */
  202. r = rtlsdr_set_sample_rate(dev, samp_rate);
  203. if (r < 0)
  204. fprintf(stderr, "WARNING: Failed to set sample rate.\n");
  205. if (tuner_benchmark) {
  206. e4k_benchmark();
  207. goto exit;
  208. }
  209. /* Enable test mode */
  210. r = rtlsdr_set_testmode(dev, 1);
  211. /* Reset endpoint before we start reading from it (mandatory) */
  212. r = rtlsdr_reset_buffer(dev);
  213. if (r < 0)
  214. fprintf(stderr, "WARNING: Failed to reset buffers.\n");
  215. if (sync_mode) {
  216. fprintf(stderr, "Reading samples in sync mode...\n");
  217. while (!do_exit) {
  218. r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read);
  219. if (r < 0) {
  220. fprintf(stderr, "WARNING: sync read failed.\n");
  221. break;
  222. }
  223. if ((uint32_t)n_read < out_block_size) {
  224. fprintf(stderr, "Short read, samples lost, exiting!\n");
  225. break;
  226. }
  227. }
  228. } else {
  229. fprintf(stderr, "Reading samples in async mode...\n");
  230. r = rtlsdr_read_async(dev, rtlsdr_callback, NULL,
  231. DEFAULT_ASYNC_BUF_NUMBER, out_block_size);
  232. }
  233. if (do_exit)
  234. fprintf(stderr, "\nUser cancel, exiting...\n");
  235. else
  236. fprintf(stderr, "\nLibrary error %d, exiting...\n", r);
  237. exit:
  238. rtlsdr_close(dev);
  239. free (buffer);
  240. out:
  241. return r >= 0 ? r : -r;
  242. }