rtl_test.c 6.8 KB

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