Fix test of strstr() for Vendor string match.

The sense of the test of strstr() return value was reversed.  Corrected
so Vendor string match would pass.  Tested with FUNcube.
Hamlib-1.2.14
Nate Bargmann 2011-07-07 22:53:24 -05:00
rodzic dfc6db399b
commit ba5720d1a9
1 zmienionych plików z 152 dodań i 138 usunięć

Wyświetl plik

@ -65,28 +65,34 @@
*/
static int usbGetStringAscii(usb_dev_handle *udh, int index, int langid, char *buf, int buflen)
{
char buffer[256];
int rval, i;
char buffer[256];
int rval, i;
rval = usb_control_msg(udh, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
(USB_DT_STRING << 8) + index, langid, buffer, sizeof(buffer), 1000);
if(rval < 0)
return rval;
if(buffer[1] != USB_DT_STRING)
return 0;
if((unsigned char)buffer[0] < rval)
rval = (unsigned char)buffer[0];
rval /= 2;
/* lossy conversion to ISO Latin1 */
for(i=1;i<rval;i++){
if(i > buflen) /* destination buffer overflow */
break;
buf[i-1] = buffer[2 * i];
if(buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */
buf[i-1] = '?';
}
buf[i-1] = 0;
return i-1;
rval = usb_control_msg(udh, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
(USB_DT_STRING << 8) + index, langid, buffer, sizeof(buffer), 1000);
if(rval < 0)
return rval;
if(buffer[1] != USB_DT_STRING)
return 0;
if((unsigned char)buffer[0] < rval)
rval = (unsigned char)buffer[0];
rval /= 2;
/* lossy conversion to ISO Latin1 */
for(i=1;i<rval;i++) {
if(i > buflen) /* destination buffer overflow */
break;
buf[i - 1] = buffer[2 * i];
if(buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */
buf[i - 1] = '?';
}
buf[i - 1] = 0;
return i - 1;
}
/**
@ -96,69 +102,78 @@ static int usbGetStringAscii(usb_dev_handle *udh, int index, int langid, char *b
*/
static struct usb_dev_handle *find_and_open_device(const hamlib_port_t *port)
{
struct usb_bus *bus;
struct usb_device *dev;
struct usb_dev_handle *udh;
char string[256];
int len;
struct usb_bus *bus;
struct usb_device *dev;
struct usb_dev_handle *udh;
char string[256];
int len;
rig_debug(RIG_DEBUG_TRACE, "%s: looking for device %04x:%04x...", __func__,
port->parm.usb.vid, port->parm.usb.pid);
rig_debug(RIG_DEBUG_VERBOSE, "%s: looking for device %04x:%04x...",
__func__, port->parm.usb.vid, port->parm.usb.pid);
for (bus = usb_get_busses(); bus != NULL; bus = bus->next) {
for (dev = bus->devices; dev != NULL; dev = dev->next) {
for (bus = usb_get_busses(); bus != NULL; bus = bus->next) {
for (dev = bus->devices; dev != NULL; dev = dev->next) {
rig_debug(RIG_DEBUG_TRACE, " %04x:%04x,", dev->descriptor.idVendor,
dev->descriptor.idProduct);
if (dev->descriptor.idVendor == port->parm.usb.vid &&
dev->descriptor.idProduct == port->parm.usb.pid) {
rig_debug(RIG_DEBUG_VERBOSE, " %04x:%04x,",
dev->descriptor.idVendor, dev->descriptor.idProduct);
if (dev->descriptor.idVendor == port->parm.usb.vid &&
dev->descriptor.idProduct == port->parm.usb.pid) {
/* we need to open the device in order to query strings */
udh = usb_open(dev);
if (!udh){
rig_debug(RIG_DEBUG_WARN, "Warning: cannot open USB device: %s\n", usb_strerror());
continue;
}
/* now check whether the names match: */
if (port->parm.usb.vendor_name) {
len = usbGetStringAscii(udh, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string));
if (len < 0) {
rig_debug(RIG_DEBUG_WARN, "Warning: cannot query manufacturer for USB device: %s\n", usb_strerror());
usb_close(udh);
continue;
}
rig_debug(RIG_DEBUG_TRACE, " vendor >%s<", string);
if (strcmp(string, port->parm.usb.vendor_name) != 0){
usb_close(udh);
continue;
}
}
if (port->parm.usb.product) {
len = usbGetStringAscii(udh, dev->descriptor.iProduct, 0x0409, string, sizeof(string));
if (len < 0) {
rig_debug(RIG_DEBUG_WARN, "Warning: cannot query product for USB device: %s\n", usb_strerror());
usb_close(udh);
continue;
}
rig_debug(RIG_DEBUG_TRACE, " product >%s<", string);
if (strcmp(string, port->parm.usb.product) != 0){
if (strstr(string, port->parm.usb.product) != 0){
usb_close(udh);
/* we need to open the device in order to query strings */
udh = usb_open(dev);
if (!udh) {
rig_debug(RIG_DEBUG_WARN, "%s: Warning: Cannot open USB device: %s\n", __func__, usb_strerror());
continue;
}
}
}
rig_debug(RIG_DEBUG_TRACE, " -> found\n");
return udh;
}
}
}
rig_debug(RIG_DEBUG_TRACE, " -> not found\n");
return NULL; /* not found */
/* now check whether the names match: */
if (port->parm.usb.vendor_name) {
len = usbGetStringAscii(udh, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string));
if (len < 0) {
rig_debug(RIG_DEBUG_WARN, "Warning: cannot query manufacturer for USB device: %s\n", usb_strerror());
usb_close(udh);
continue;
}
rig_debug(RIG_DEBUG_VERBOSE, " vendor >%s<", string);
if (strcmp(string, port->parm.usb.vendor_name) != 0) {
rig_debug(RIG_DEBUG_WARN, "%s: Warning: Vendor name string mismatch!\n", __func__);
usb_close(udh);
continue;
}
}
if (port->parm.usb.product) {
len = usbGetStringAscii(udh, dev->descriptor.iProduct, 0x0409, string, sizeof(string));
if (len < 0) {
rig_debug(RIG_DEBUG_WARN, "Warning: cannot query product for USB device: %s\n", usb_strerror());
usb_close(udh);
continue;
}
rig_debug(RIG_DEBUG_VERBOSE, " product >%s<", string);
if (strcmp(string, port->parm.usb.product) != 0) {
/* strstr() returns a pointer to the beginning of
* port->parm.usb.product on match, and NULL otherwise
*/
if (strstr(string, port->parm.usb.product) == NULL) {
rig_debug(RIG_DEBUG_WARN, "%s: Warning: Product string mismatch!\n", __func__);
usb_close(udh);
continue;
}
}
}
rig_debug(RIG_DEBUG_VERBOSE, " -> found\n");
return udh;
}
}
}
rig_debug(RIG_DEBUG_VERBOSE, " -> not found\n");
return NULL; /* not found */
}
@ -170,55 +185,54 @@ static struct usb_dev_handle *find_and_open_device(const hamlib_port_t *port)
int usb_port_open(hamlib_port_t *port)
{
struct usb_dev_handle *udh;
char *p, *q;
char *p, *q;
usb_init (); /* usb library init */
if (usb_find_busses () < 0)
rig_debug(RIG_DEBUG_ERR, "%s: usb_find_busses failed %s\n",
usb_init (); /* usb library init */
if (usb_find_busses () < 0)
rig_debug(RIG_DEBUG_ERR, "%s: usb_find_busses failed %s\n",
__func__, usb_strerror());
if (usb_find_devices() < 0)
rig_debug(RIG_DEBUG_ERR, "%s: usb_find_devices failed %s\n",
if (usb_find_devices() < 0)
rig_debug(RIG_DEBUG_ERR, "%s: usb_find_devices failed %s\n",
__func__, usb_strerror());
p = port->pathname;
q = strchr(p, ':');
if (q) {
if (q != p+1)
port->parm.usb.vid = strtol(q, NULL, 16);
p = q+1;
q = strchr(p, ':');
if (q) {
if (q != p+1)
port->parm.usb.pid = strtol(q, NULL, 16);
p = q+1;
q = strchr(p, ':');
if (q) {
if (q != p+1)
port->parm.usb.vendor_name = q;
p = q+1;
q = strchr(p, ':');
if (q) {
if (q != p+1)
port->parm.usb.product = q;
}
}
}
}
p = port->pathname;
q = strchr(p, ':');
if (q) {
if (q != p+1)
port->parm.usb.vid = strtol(q, NULL, 16);
p = q+1;
q = strchr(p, ':');
if (q) {
if (q != p+1)
port->parm.usb.pid = strtol(q, NULL, 16);
p = q+1;
q = strchr(p, ':');
if (q) {
if (q != p+1)
port->parm.usb.vendor_name = q;
p = q+1;
q = strchr(p, ':');
if (q) {
if (q != p+1)
port->parm.usb.product = q;
}
}
}
}
udh = find_and_open_device(port);
if (udh == 0)
return -RIG_EIO;
udh = find_and_open_device(port);
if (udh == 0)
return -RIG_EIO;
#ifdef LIBUSB_HAS_GET_DRIVER_NP
/* Try to detach ftdi_sio kernel module
/* Try to detach ftdi_sio kernel module
* This should be performed only for devices using
* USB-serial converters (like FTDI chips), for other
* devices this may cause problems, so do not do it.
*/
char dname[32] = {0};
int retval = usb_get_driver_np(udh, port->parm.usb.iface, dname, 31);
if (!retval)
{
if (!retval) {
/* Try to detach ftdi_sio kernel module
* Returns ENODATA if driver is not loaded.
* Don't check return value, let it fail later.
@ -227,42 +241,42 @@ int usb_port_open(hamlib_port_t *port)
}
#endif
if (port->parm.usb.iface >= 0) {
if (port->parm.usb.iface >= 0) {
#ifdef _WIN32
if (port->parm.usb.conf >= 0 &&
usb_set_configuration (udh, port->parm.usb.conf) < 0){
rig_debug(RIG_DEBUG_ERR, "%s: usb_set_configuration: failed conf %d: %s\n",
__func__,port->parm.usb.conf, usb_strerror());
usb_close (udh);
return -RIG_EIO;
}
if (port->parm.usb.conf >= 0 &&
usb_set_configuration (udh, port->parm.usb.conf) < 0) {
rig_debug(RIG_DEBUG_ERR, "%s: usb_set_configuration: failed conf %d: %s\n",
__func__, port->parm.usb.conf, usb_strerror());
usb_close (udh);
return -RIG_EIO;
}
#endif
rig_debug(RIG_DEBUG_VERBOSE, "%s: claiming %d\n", __func__, port->parm.usb.iface);
rig_debug(RIG_DEBUG_VERBOSE, "%s: claiming %d\n", __func__, port->parm.usb.iface);
if (usb_claim_interface (udh, port->parm.usb.iface) < 0){
rig_debug(RIG_DEBUG_ERR, "%s:usb_claim_interface: failed interface %d: %s\n",
__func__,port->parm.usb.iface, usb_strerror());
usb_close (udh);
return -RIG_EIO;
}
if (usb_claim_interface (udh, port->parm.usb.iface) < 0) {
rig_debug(RIG_DEBUG_ERR, "%s:usb_claim_interface: failed interface %d: %s\n",
__func__,port->parm.usb.iface, usb_strerror());
usb_close (udh);
return -RIG_EIO;
}
#if 0
if (usb_set_altinterface (udh, port->parm.usb.alt) < 0){
fprintf (stderr, "%s:usb_set_alt_interface: failed: %s\n", __func__,
usb_strerror());
usb_release_interface (udh, port->parm.usb.iface);
usb_close (udh);
return -RIG_EIO;
}
if (usb_set_altinterface (udh, port->parm.usb.alt) < 0) {
fprintf (stderr, "%s:usb_set_alt_interface: failed: %s\n", __func__,
usb_strerror());
usb_release_interface (udh, port->parm.usb.iface);
usb_close (udh);
return -RIG_EIO;
}
#endif
}
}
port->handle = (void*) udh;
port->handle = (void*) udh;
return RIG_OK;
return RIG_OK;
}
/**