rtl_sdr.c 6.4 KB

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