From 7d90bb808aeac3f3a52a24f6729ed34b39584787 Mon Sep 17 00:00:00 2001 From: Dimitri Stolnikov Date: Sun, 20 May 2012 16:43:59 +0200 Subject: introduce api function to read usb string descriptors This API allows to read manufacturer and product names as well as the serial number advertized by the device on the bus. --- software/libosmosdr/include/osmosdr.h | 32 ++++++++++++ software/libosmosdr/src/libosmosdr.c | 93 +++++++++++++++++++++++++++++++++-- 2 files changed, 122 insertions(+), 3 deletions(-) diff --git a/software/libosmosdr/include/osmosdr.h b/software/libosmosdr/include/osmosdr.h index 262448a..9b4fa43 100644 --- a/software/libosmosdr/include/osmosdr.h +++ b/software/libosmosdr/include/osmosdr.h @@ -32,6 +32,22 @@ OSMOSDR_API uint32_t osmosdr_get_device_count(void); OSMOSDR_API const char* osmosdr_get_device_name(uint32_t index); +/*! + * Get USB device strings. + * + * NOTE: The string arguments must provide space for up to 256 bytes. + * + * \param index the device index + * \param manufact manufacturer name, may be NULL + * \param product product name, may be NULL + * \param serial serial number, may be NULL + * \return 0 on success + */ +OSMOSDR_API int osmosdr_get_device_usb_strings(uint32_t index, + char *manufact, + char *product, + char *serial); + OSMOSDR_API int osmosdr_open(osmosdr_dev_t **dev, uint32_t index); OSMOSDR_API int osmosdr_close(osmosdr_dev_t *dev); @@ -62,6 +78,20 @@ OSMOSDR_API int osmosdr_set_clock_freq(osmosdr_dev_t *dev, uint32_t adc_clock, OSMOSDR_API int osmosdr_get_clock_freq(osmosdr_dev_t *dev, uint32_t *adc_clock, uint32_t *tun_clock); +/*! + * Get USB device strings. + * + * NOTE: The string arguments must provide space for up to 256 bytes. + * + * \param dev the device handle given by osmosdr_open() + * \param manufact manufacturer name, may be NULL + * \param product product name, may be NULL + * \param serial serial number, may be NULL + * \return 0 on success + */ +OSMOSDR_API int osmosdr_get_usb_strings(osmosdr_dev_t *dev, char *manufact, + char *product, char *serial); + /*! * Set the frequency the device is tuned to. * @@ -124,6 +154,8 @@ OSMOSDR_API uint32_t osmosdr_get_sample_rate(osmosdr_dev_t *dev); /* streaming functions */ +OSMOSDR_API int osmosdr_reset_buffer(osmosdr_dev_t *dev); + OSMOSDR_API int osmosdr_read_sync(osmosdr_dev_t *dev, void *buf, int len, int *n_read); typedef void(*osmosdr_read_async_cb_t)(unsigned char *buf, uint32_t len, void *ctx); diff --git a/software/libosmosdr/src/libosmosdr.c b/software/libosmosdr/src/libosmosdr.c index fb90520..e825c8f 100644 --- a/software/libosmosdr/src/libosmosdr.c +++ b/software/libosmosdr/src/libosmosdr.c @@ -84,8 +84,8 @@ typedef struct osmosdr_dongle { } osmosdr_dongle_t; static osmosdr_dongle_t known_devices[] = { - { 0x16c0, 0x0763, "OsmoSDR" }, - /* for future modifications of the OsmoSDR */ + { 0x16c0, 0x0763, "sysmocom OsmoSDR" }, + /* here come future modifications of the OsmoSDR */ }; #define DEFAULT_BUF_NUMBER 32 @@ -159,6 +159,47 @@ int osmosdr_get_xtal_freq(osmosdr_dev_t *dev, uint32_t *adc_clock, uint32_t *tun return 0; } +int osmosdr_get_usb_strings(osmosdr_dev_t *dev, char *manufact, char *product, + char *serial) +{ + struct libusb_device_descriptor dd; + libusb_device *device = NULL; + const int buf_max = 256; + int r = 0; + + if (!dev || !dev->devh) + return -1; + + device = libusb_get_device(dev->devh); + + r = libusb_get_device_descriptor(device, &dd); + if (r < 0) + return -1; + + if (manufact) { + memset(manufact, 0, buf_max); + libusb_get_string_descriptor_ascii(dev->devh, dd.iManufacturer, + (unsigned char *)manufact, + buf_max); + } + + if (product) { + memset(product, 0, buf_max); + libusb_get_string_descriptor_ascii(dev->devh, dd.iProduct, + (unsigned char *)product, + buf_max); + } + + if (serial) { + memset(serial, 0, buf_max); + libusb_get_string_descriptor_ascii(dev->devh, dd.iSerialNumber, + (unsigned char *)serial, + buf_max); + } + + return 0; +} + int osmosdr_set_center_freq(osmosdr_dev_t *dev, uint32_t freq) { int r = -1; @@ -279,8 +320,8 @@ uint32_t osmosdr_get_device_count(void) int i; libusb_context *ctx; libusb_device **list; - uint32_t device_count = 0; struct libusb_device_descriptor dd; + uint32_t device_count = 0; ssize_t cnt; libusb_init(&ctx); @@ -338,6 +379,52 @@ const char *osmosdr_get_device_name(uint32_t index) return ""; } +int osmosdr_get_device_usb_strings(uint32_t index, char *manufact, + char *product, char *serial) +{ + int r = -2; + int i; + libusb_context *ctx; + libusb_device **list; + struct libusb_device_descriptor dd; + osmosdr_dongle_t *device = NULL; + osmosdr_dev_t devt; + uint32_t device_count = 0; + ssize_t cnt; + + libusb_init(&ctx); + + cnt = libusb_get_device_list(ctx, &list); + + for (i = 0; i < cnt; i++) { + libusb_get_device_descriptor(list[i], &dd); + + device = find_known_device(dd.idVendor, dd.idProduct); + + if (device) { + device_count++; + + if (index == device_count - 1) { + r = libusb_open(list[i], &devt.devh); + if (!r) { + r = osmosdr_get_usb_strings(&devt, + manufact, + product, + serial); + libusb_close(devt.devh); + } + break; + } + } + } + + libusb_free_device_list(list, 1); + + libusb_exit(ctx); + + return r; +} + int osmosdr_open(osmosdr_dev_t **out_dev, uint32_t index) { int r; -- cgit v1.2.3