rtl_sdr.c 7.2 KB

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