main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 USB-sticks\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;
  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. while ((opt = getopt(argc, argv, "f:s:")) != -1) {
  51. switch (opt) {
  52. case 'f':
  53. frequency = atoi(optarg);
  54. break;
  55. case 's':
  56. samp_rate = atoi(optarg);
  57. break;
  58. default:
  59. usage();
  60. break;
  61. }
  62. }
  63. if (argc <= optind) {
  64. usage();
  65. } else {
  66. filename = argv[optind];
  67. }
  68. rtlsdr_init();
  69. int device_count = rtlsdr_get_device_count();
  70. if (!device_count) {
  71. fprintf(stderr, "No supported devices found.\n");
  72. exit(1);
  73. }
  74. printf("Found %d device(s).\n", device_count);
  75. dev = rtlsdr_open(0); /* open the first device */
  76. if (NULL == dev) {
  77. fprintf(stderr, "Failed to open rtlsdr device.\n");
  78. exit(1);
  79. }
  80. sigact.sa_handler = sighandler;
  81. sigemptyset(&sigact.sa_mask);
  82. sigact.sa_flags = 0;
  83. sigaction(SIGINT, &sigact, NULL);
  84. sigaction(SIGTERM, &sigact, NULL);
  85. sigaction(SIGQUIT, &sigact, NULL);
  86. /* Set the sample rate */
  87. r = rtlsdr_set_sample_rate(dev, samp_rate);
  88. if (r < 0) {
  89. fprintf(stderr, "WARNING: Failed to set sample rate.\n");
  90. }
  91. /* Set the frequency */
  92. r = rtlsdr_set_center_freq(dev, frequency);
  93. if (r < 0) {
  94. fprintf(stderr, "WARNING: Failed to set center freq.\n");
  95. }
  96. file = fopen(filename, "wb");
  97. if (!file) {
  98. printf("Failed to open %s\n", filename);
  99. goto out;
  100. }
  101. /* Reset endpoint before we start reading from it */
  102. r = rtlsdr_reset_buffer(dev);
  103. if (r < 0) {
  104. fprintf(stderr, "WARNING: Failed to reset buffers.\n");
  105. }
  106. printf("Reading samples...\n");
  107. while (!do_exit) {
  108. r = rtlsdr_read_sync(dev, buffer, READLEN, &n_read);
  109. if (r < 0) {
  110. fprintf(stderr, "WARNING: sync read failed.\n");
  111. }
  112. fwrite(buffer, n_read, 1, file);
  113. if (n_read < READLEN) {
  114. printf("Short read, samples lost, exiting!\n");
  115. break;
  116. }
  117. }
  118. fclose(file);
  119. rtlsdr_exit();
  120. out:
  121. return r >= 0 ? r : -r;
  122. }