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