main.c 6.5 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. #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. 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 (fwrite(buf, 1, len, (FILE*)ctx) != len) {
  80. fprintf(stderr, "Short write, samples lost, exiting!\n");
  81. rtlsdr_cancel_async(dev);
  82. }
  83. }
  84. }
  85. int main(int argc, char **argv)
  86. {
  87. #ifndef _WIN32
  88. struct sigaction sigact;
  89. #endif
  90. char *filename = NULL;
  91. int n_read;
  92. int r, opt;
  93. int i, gain = 0;
  94. int sync_mode = 0;
  95. FILE *file;
  96. uint8_t *buffer;
  97. uint32_t dev_index = 0;
  98. uint32_t frequency = 0;
  99. uint32_t samp_rate = DEFAULT_SAMPLE_RATE;
  100. uint32_t out_block_size = DEFAULT_BUF_LENGTH;
  101. int device_count;
  102. #ifndef _WIN32
  103. while ((opt = getopt(argc, argv, "d:f:g:s:b:S::")) != -1) {
  104. switch (opt) {
  105. case 'd':
  106. dev_index = atoi(optarg);
  107. break;
  108. case 'f':
  109. frequency = (uint32_t)atof(optarg);
  110. break;
  111. case 'g':
  112. gain = atoi(optarg);
  113. break;
  114. case 's':
  115. samp_rate = (int)atof(optarg);
  116. break;
  117. case 'b':
  118. out_block_size = (uint32_t)atof(optarg);
  119. break;
  120. case 'S':
  121. sync_mode = 1;
  122. break;
  123. default:
  124. usage();
  125. break;
  126. }
  127. }
  128. if (argc <= optind) {
  129. usage();
  130. } else {
  131. filename = argv[optind];
  132. }
  133. #else
  134. if(argc <6)
  135. usage();
  136. dev_index = atoi(argv[1]);
  137. samp_rate = atoi(argv[2])*1000;
  138. gain=atoi(argv[3]);
  139. frequency = atoi(argv[4]);
  140. filename = argv[5];
  141. #endif
  142. if(out_block_size < MINIMAL_BUF_LENGTH ||
  143. out_block_size > MAXIMAL_BUF_LENGTH ){
  144. fprintf(stderr,
  145. "Output block size wrong value, falling back to default\n");
  146. fprintf(stderr,
  147. "Minimal length: %u\n", MINIMAL_BUF_LENGTH);
  148. fprintf(stderr,
  149. "Maximal length: %u\n", MAXIMAL_BUF_LENGTH);
  150. out_block_size = DEFAULT_BUF_LENGTH;
  151. }
  152. buffer = malloc(out_block_size * sizeof(uint8_t));
  153. device_count = rtlsdr_get_device_count();
  154. if (!device_count) {
  155. fprintf(stderr, "No supported devices found.\n");
  156. exit(1);
  157. }
  158. fprintf(stderr, "Found %d device(s):\n", device_count);
  159. for (i = 0; i < device_count; i++)
  160. fprintf(stderr, " %d: %s\n", i, rtlsdr_get_device_name(i));
  161. fprintf(stderr, "\n");
  162. fprintf(stderr, "Using device %d: %s\n",
  163. dev_index,
  164. rtlsdr_get_device_name(dev_index));
  165. r = rtlsdr_open(&dev, 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. r = rtlsdr_set_sample_rate(dev, samp_rate);
  183. if (r < 0)
  184. fprintf(stderr, "WARNING: Failed to set sample rate.\n");
  185. /* Set the frequency */
  186. r = rtlsdr_set_center_freq(dev, frequency);
  187. if (r < 0)
  188. fprintf(stderr, "WARNING: Failed to set center freq.\n");
  189. else
  190. fprintf(stderr, "Tuned to %u Hz.\n", frequency);
  191. /* Set the tuner gain */
  192. r = rtlsdr_set_tuner_gain(dev, gain);
  193. if (r < 0)
  194. fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
  195. else
  196. fprintf(stderr, "Tuner gain set to %i dB.\n", gain);
  197. if(strcmp(filename, "-") == 0) { /* Write samples to stdout */
  198. file = stdout;
  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. r = rtlsdr_reset_buffer(dev);
  208. if (r < 0)
  209. fprintf(stderr, "WARNING: Failed to reset buffers.\n");
  210. if (sync_mode) {
  211. fprintf(stderr, "Reading samples in sync mode...\n");
  212. while (!do_exit) {
  213. r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read);
  214. if (r < 0) {
  215. fprintf(stderr, "WARNING: sync read failed.\n");
  216. break;
  217. }
  218. if (fwrite(buffer, 1, n_read, file) != n_read) {
  219. fprintf(stderr, "Short write, samples lost, exiting!\n");
  220. break;
  221. }
  222. if (n_read < out_block_size) {
  223. fprintf(stderr, "Short read, samples lost, exiting!\n");
  224. break;
  225. }
  226. }
  227. } else {
  228. fprintf(stderr, "Reading samples in async mode...\n");
  229. r = rtlsdr_read_async(dev, rtlsdr_callback, (void *)file,
  230. DEFAULT_ASYNC_BUF_NUMBER, out_block_size);
  231. }
  232. if (do_exit)
  233. fprintf(stderr, "\nUser cancel, exiting...\n");
  234. else
  235. fprintf(stderr, "\nLibrary error %d, exiting...\n", r);
  236. if (file != stdout)
  237. fclose(file);
  238. rtlsdr_close(dev);
  239. free (buffer);
  240. out:
  241. return r >= 0 ? r : -r;
  242. }