diff --git a/RtlSdrSource.cc b/RtlSdrSource.cc index bbbf7ff..4790cef 100644 --- a/RtlSdrSource.cc +++ b/RtlSdrSource.cc @@ -14,6 +14,10 @@ RtlSdrSource::RtlSdrSource(int dev_index) { int r; + const char *devname = rtlsdr_get_device_name(dev_index); + if (devname != NULL) + m_devname = devname; + r = rtlsdr_open(&m_dev, dev_index); if (r < 0) { m_error = "Failed to open RTL-SDR device ("; diff --git a/RtlSdrSource.h b/RtlSdrSource.h index 08c31f5..eb3e194 100644 --- a/RtlSdrSource.h +++ b/RtlSdrSource.h @@ -45,6 +45,12 @@ public: /** Return a list of supported tuner gain settings in dB. */ std::vector get_tuner_gains(); + /** Return name of opened RTL-SDR device. */ + std::string get_device_name() const + { + return m_devname; + } + /** * Fetch a bunch of samples from the device. * @@ -73,6 +79,7 @@ public: private: struct rtlsdr_dev * m_dev; int m_block_length; + std::string m_devname; std::string m_error; }; diff --git a/main.cc b/main.cc index 7b258a3..e62c04a 100644 --- a/main.cc +++ b/main.cc @@ -293,9 +293,8 @@ int main(int argc, char **argv) } break; case 'd': - if (!parse_opt(optarg, devidx) || devidx < 0) { - badarg("-d"); - } + if (!parse_opt(optarg, devidx)) + devidx = -1; break; case 's': if (!parse_opt(optarg, ifrate) || ifrate <= 0) { @@ -367,6 +366,17 @@ int main(int argc, char **argv) tuner_freq += 0.25 * ifrate; } + vector devnames = RtlSdrSource::get_device_names(); + if (devidx < 0 || (unsigned int)devidx >= devnames.size()) { + fprintf(stderr, "ERROR: invalid device index %d\n", devidx); + fprintf(stderr, "Found %u devices:\n", (unsigned int)devnames.size()); + for (unsigned int i = 0; i < devnames.size(); i++) { + fprintf(stderr, "%2u: %s\n", i, devnames[i].c_str()); + } + exit(1); + } + fprintf(stderr, "using device %d: %s\n", devidx, devnames[devidx].c_str()); + // Open RTL-SDR device. RtlSdrSource rtlsdr(devidx); if (!rtlsdr) {