rtl_sdr.c 6.6 KB

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