From d3c9015d26e3d5bb310394fad7c41436f5268b01 Mon Sep 17 00:00:00 2001 From: Steve Markgraf Date: Sun, 4 Nov 2012 13:57:56 +0100 Subject: [PATCH] lib: add rtlsdr_get_index_by_serial() This allows to open a device by a name (serial number string) that has been programmed with rtl_eeprom -s. Signed-off-by: Steve Markgraf --- include/rtl-sdr.h | 11 +++++++++++ src/librtlsdr.c | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/include/rtl-sdr.h b/include/rtl-sdr.h index 6dca0d0..75bfd3f 100644 --- a/include/rtl-sdr.h +++ b/include/rtl-sdr.h @@ -49,6 +49,17 @@ RTLSDR_API int rtlsdr_get_device_usb_strings(uint32_t index, char *product, char *serial); +/*! + * Get device index by USB serial string descriptor. + * + * \param serial serial string of the device + * \return device index of first device where the name matched + * \return -1 if name is NULL + * \return -2 if no devices were found at all + * \return -3 if devices were found, but none with matching name + */ +RTLSDR_API int rtlsdr_get_index_by_serial(const char *serial); + RTLSDR_API int rtlsdr_open(rtlsdr_dev_t **dev, uint32_t index); RTLSDR_API int rtlsdr_close(rtlsdr_dev_t *dev); diff --git a/src/librtlsdr.c b/src/librtlsdr.c index 1af9f87..2ac457f 100644 --- a/src/librtlsdr.c +++ b/src/librtlsdr.c @@ -1264,6 +1264,28 @@ int rtlsdr_get_device_usb_strings(uint32_t index, char *manufact, return r; } +int rtlsdr_get_index_by_serial(const char *serial) +{ + int i, cnt, r; + char str[256]; + + if (!serial) + return -1; + + cnt = rtlsdr_get_device_count(); + + if (!cnt) + return -2; + + for (i = 0; i < cnt; i++) { + r = rtlsdr_get_device_usb_strings(i, NULL, NULL, str); + if (!r && !strcmp(serial, str)) + return i; + } + + return -3; +} + int rtlsdr_open(rtlsdr_dev_t **out_dev, uint32_t index) { int r;