main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * rtl-sdr, a poor man's SDR using a Realtek RTL2832 based DVB-stick
  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. void usage(void)
  29. {
  30. printf("rtl-sdr, an I/Q recorder for RTL2832 based DVB-T receivers\n\n"
  31. "Usage:\t -f frequency to tune to [Hz]\n"
  32. "\t[-s samplerate (default: 2048000 Hz)]\n"
  33. "\toutput filename\n");
  34. exit(1);
  35. }
  36. static void sighandler(int signum)
  37. {
  38. do_exit = 1;
  39. }
  40. int main(int argc, char **argv)
  41. {
  42. struct sigaction sigact;
  43. int r, opt;
  44. char *filename = NULL;
  45. uint32_t frequency = 0, samp_rate = 2048000;
  46. uint8_t buffer[READLEN];
  47. uint32_t n_read;
  48. FILE *file;
  49. rtlsdr_dev_t *dev = NULL;
  50. uint32_t dev_index = 0;
  51. while ((opt = getopt(argc, argv, "d:f:s:")) != -1) {
  52. switch (opt) {
  53. case 'd':
  54. dev_index = atoi(optarg);
  55. break;
  56. case 'f':
  57. frequency = atoi(optarg);
  58. break;
  59. case 's':
  60. samp_rate = atoi(optarg);
  61. break;
  62. default:
  63. usage();
  64. break;
  65. }
  66. }
  67. if (argc <= optind) {
  68. usage();
  69. } else {
  70. filename = argv[optind];
  71. }
  72. rtlsdr_init();
  73. int device_count = rtlsdr_get_device_count();
  74. if (!device_count) {
  75. fprintf(stderr, "No supported devices found.\n");
  76. exit(1);
  77. }
  78. printf("Found %d device(s).\n", device_count);
  79. dev = rtlsdr_open(dev_index);
  80. if (NULL == dev) {
  81. fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
  82. exit(1);
  83. }
  84. printf("Using %s\n", rtlsdr_get_device_name(dev_index));
  85. sigact.sa_handler = sighandler;
  86. sigemptyset(&sigact.sa_mask);
  87. sigact.sa_flags = 0;
  88. sigaction(SIGINT, &sigact, NULL);
  89. sigaction(SIGTERM, &sigact, NULL);
  90. sigaction(SIGQUIT, &sigact, NULL);
  91. /* Set the sample rate */
  92. r = rtlsdr_set_sample_rate(dev, samp_rate);
  93. if (r < 0)
  94. fprintf(stderr, "WARNING: Failed to set sample rate.\n");
  95. /* Set the frequency */
  96. r = rtlsdr_set_center_freq(dev, frequency);
  97. if (r < 0)
  98. fprintf(stderr, "WARNING: Failed to set center freq.\n");
  99. file = fopen(filename, "wb");
  100. if (!file) {
  101. printf("Failed to open %s\n", filename);
  102. goto out;
  103. }
  104. /* Reset endpoint before we start reading from it (mandatory) */
  105. r = rtlsdr_reset_buffer(dev);
  106. if (r < 0)
  107. fprintf(stderr, "WARNING: Failed to reset buffers.\n");
  108. printf("Reading samples...\n");
  109. while (!do_exit) {
  110. r = rtlsdr_read_sync(dev, buffer, READLEN, &n_read);
  111. if (r < 0)
  112. fprintf(stderr, "WARNING: sync read failed.\n");
  113. fwrite(buffer, n_read, 1, file);
  114. if (n_read < READLEN) {
  115. printf("Short read, samples lost, exiting!\n");
  116. break;
  117. }
  118. }
  119. fclose(file);
  120. rtlsdr_close(dev);
  121. rtlsdr_exit();
  122. out:
  123. return r >= 0 ? r : -r;
  124. }