main.c 6.3 KB

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