main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include <unistd.h>
  25. #include "rtl-sdr.h"
  26. #define READLEN (16 * 16384)
  27. static int do_exit = 0;
  28. static rtlsdr_dev_t *dev = NULL;
  29. void usage(void)
  30. {
  31. fprintf(stderr,
  32. "rtl-sdr, an I/Q recorder for RTL2832 based DVB-T receivers\n\n"
  33. "Usage:\t -f frequency to tune to [Hz]\n"
  34. "\t[-s samplerate (default: 2048000 Hz)]\n"
  35. "\t[-d device index (default: 0)]\n"
  36. "\t[-g tuner gain (default: 0 dB)]\n"
  37. "\toutput filename\n");
  38. exit(1);
  39. }
  40. static void sighandler(int signum)
  41. {
  42. do_exit = 1;
  43. rtlsdr_cancel_async(dev);
  44. }
  45. static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx)
  46. {
  47. if (ctx)
  48. fwrite(buf, len, 1, (FILE*)ctx);
  49. }
  50. int main(int argc, char **argv)
  51. {
  52. struct sigaction sigact;
  53. int r, opt;
  54. char *filename = NULL;
  55. uint32_t frequency = 0, samp_rate = 2048000;
  56. uint8_t buffer[READLEN];
  57. int n_read;
  58. FILE *file;
  59. uint32_t dev_index = 0;
  60. int i, gain = 0;
  61. while ((opt = getopt(argc, argv, "d:f:g:s:")) != -1) {
  62. switch (opt) {
  63. case 'd':
  64. dev_index = atoi(optarg);
  65. break;
  66. case 'f':
  67. frequency = (uint32_t)atof(optarg);
  68. break;
  69. case 'g':
  70. gain = atoi(optarg);
  71. break;
  72. case 's':
  73. samp_rate = (int)atof(optarg);
  74. break;
  75. default:
  76. usage();
  77. break;
  78. }
  79. }
  80. if (argc <= optind) {
  81. usage();
  82. } else {
  83. filename = argv[optind];
  84. }
  85. int device_count = rtlsdr_get_device_count();
  86. if (!device_count) {
  87. fprintf(stderr, "No supported devices found.\n");
  88. exit(1);
  89. }
  90. fprintf(stderr, "Found %d device(s):\n", device_count);
  91. for (i = 0; i < device_count; i++)
  92. fprintf(stderr, " %d: %s\n", i, rtlsdr_get_device_name(i));
  93. fprintf(stderr, "\n");
  94. fprintf(stderr, "Using device %d: %s\n",
  95. dev_index,
  96. rtlsdr_get_device_name(dev_index));
  97. r = rtlsdr_open(&dev, dev_index);
  98. if (r < 0) {
  99. fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
  100. exit(1);
  101. }
  102. sigact.sa_handler = sighandler;
  103. sigemptyset(&sigact.sa_mask);
  104. sigact.sa_flags = 0;
  105. sigaction(SIGINT, &sigact, NULL);
  106. sigaction(SIGTERM, &sigact, NULL);
  107. sigaction(SIGQUIT, &sigact, NULL);
  108. /* Set the sample rate */
  109. r = rtlsdr_set_sample_rate(dev, samp_rate);
  110. if (r < 0)
  111. fprintf(stderr, "WARNING: Failed to set sample rate.\n");
  112. /* Set the frequency */
  113. r = rtlsdr_set_center_freq(dev, frequency);
  114. if (r < 0)
  115. fprintf(stderr, "WARNING: Failed to set center freq.\n");
  116. else
  117. fprintf(stderr, "Tuned to %u Hz.\n", frequency);
  118. /* Set the tuner gain */
  119. r = rtlsdr_set_tuner_gain(dev, gain);
  120. if (r < 0)
  121. fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
  122. else
  123. fprintf(stderr, "Tuner gain set to %i dB.\n", gain);
  124. file = fopen(filename, "wb");
  125. if (!file) {
  126. printf("Failed to open %s\n", filename);
  127. goto out;
  128. }
  129. /* Reset endpoint before we start reading from it (mandatory) */
  130. r = rtlsdr_reset_buffer(dev);
  131. if (r < 0)
  132. fprintf(stderr, "WARNING: Failed to reset buffers.\n");
  133. fprintf(stderr, "Reading samples...\n");
  134. #if 0
  135. while (!do_exit) {
  136. r = rtlsdr_read_sync(dev, buffer, READLEN, &n_read);
  137. if (r < 0)
  138. fprintf(stderr, "WARNING: sync read failed.\n");
  139. fwrite(buffer, n_read, 1, file);
  140. if (n_read < READLEN) {
  141. printf("Short read, samples lost, exiting!\n");
  142. break;
  143. }
  144. }
  145. #else
  146. rtlsdr_read_async(dev, rtlsdr_callback, (void *)file, 0, 0);
  147. #endif
  148. if (do_exit)
  149. fprintf(stderr, "\nUser cancel, exiting...\n");
  150. else
  151. fprintf(stderr, "\nSystem cancel, exiting...\n");
  152. fclose(file);
  153. rtlsdr_close(dev);
  154. out:
  155. return r >= 0 ? r : -r;
  156. }