Skip to content

Commit

Permalink
add get_port_details variant (faster/less info)
Browse files Browse the repository at this point in the history
  • Loading branch information
facchinm committed Jan 15, 2016
1 parent f8ed8a6 commit 658f212
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 59 deletions.
2 changes: 1 addition & 1 deletion freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ static int populate_port_struct_from_libusb_desc(struct sp_port *const port,
return 0;
}

SP_PRIV enum sp_return get_port_details(struct sp_port *port)
SP_PRIV enum sp_return get_port_details(struct sp_port *port, bool fetchDescriptors)
{
int rc;
struct libusb20_backend *be;
Expand Down
3 changes: 2 additions & 1 deletion libserialport_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
#include <stdbool.h>
#ifdef _WIN32
#include <windows.h>
#include <tchar.h>
Expand Down Expand Up @@ -234,7 +235,7 @@ extern void (*sp_debug_handler)(const char *format, ...);
SP_PRIV struct sp_port **list_append(struct sp_port **list, const char *portname);

/* OS-specific Helper functions. */
SP_PRIV enum sp_return get_port_details(struct sp_port *port);
SP_PRIV enum sp_return get_port_details(struct sp_port *port, bool fetchDescriptors);
SP_PRIV enum sp_return list_ports(struct sp_port ***list);

#endif
2 changes: 1 addition & 1 deletion linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "libserialport.h"
#include "libserialport_internal.h"

SP_PRIV enum sp_return get_port_details(struct sp_port *port)
SP_PRIV enum sp_return get_port_details(struct sp_port *port, bool fetchDescriptors)
{
/*
* Description limited to 127 char, anything longer
Expand Down
57 changes: 30 additions & 27 deletions macosx.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "libserialport.h"
#include "libserialport_internal.h"

SP_PRIV enum sp_return get_port_details(struct sp_port *port)
SP_PRIV enum sp_return get_port_details(struct sp_port *port, bool fetchDescriptors)
{
/*
* Description limited to 127 char, anything longer
Expand Down Expand Up @@ -152,37 +152,40 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
if (cf_product)
CFRelease(cf_product);

if ((cf_property = IORegistryEntrySearchCFProperty(ioport,kIOServicePlane,
CFSTR("USB Vendor Name"), kCFAllocatorDefault,
kIORegistryIterateRecursively | kIORegistryIterateParents))) {
if (CFStringGetCString(cf_property, manufacturer, sizeof(manufacturer),
kCFStringEncodingASCII)) {
DEBUG_FMT("Found manufacturer %s", manufacturer);
port->usb_manufacturer = strdup(manufacturer);
if (fetchDescriptors == true) {

if ((cf_property = IORegistryEntrySearchCFProperty(ioport,kIOServicePlane,
CFSTR("USB Vendor Name"), kCFAllocatorDefault,
kIORegistryIterateRecursively | kIORegistryIterateParents))) {
if (CFStringGetCString(cf_property, manufacturer, sizeof(manufacturer),
kCFStringEncodingASCII)) {
DEBUG_FMT("Found manufacturer %s", manufacturer);
port->usb_manufacturer = strdup(manufacturer);
}
CFRelease(cf_property);
}
CFRelease(cf_property);
}

if ((cf_property = IORegistryEntrySearchCFProperty(ioport,kIOServicePlane,
CFSTR("USB Product Name"), kCFAllocatorDefault,
kIORegistryIterateRecursively | kIORegistryIterateParents))) {
if (CFStringGetCString(cf_property, product, sizeof(product),
kCFStringEncodingASCII)) {
DEBUG_FMT("Found product name %s", product);
port->usb_product = strdup(product);
if ((cf_property = IORegistryEntrySearchCFProperty(ioport,kIOServicePlane,
CFSTR("USB Product Name"), kCFAllocatorDefault,
kIORegistryIterateRecursively | kIORegistryIterateParents))) {
if (CFStringGetCString(cf_property, product, sizeof(product),
kCFStringEncodingASCII)) {
DEBUG_FMT("Found product name %s", product);
port->usb_product = strdup(product);
}
CFRelease(cf_property);
}
CFRelease(cf_property);
}

if ((cf_property = IORegistryEntrySearchCFProperty(ioport,kIOServicePlane,
CFSTR("USB Serial Number"), kCFAllocatorDefault,
kIORegistryIterateRecursively | kIORegistryIterateParents))) {
if (CFStringGetCString(cf_property, serial, sizeof(serial),
kCFStringEncodingASCII)) {
DEBUG_FMT("Found serial number %s", serial);
port->usb_serial = strdup(serial);
if ((cf_property = IORegistryEntrySearchCFProperty(ioport,kIOServicePlane,
CFSTR("USB Serial Number"), kCFAllocatorDefault,
kIORegistryIterateRecursively | kIORegistryIterateParents))) {
if (CFStringGetCString(cf_property, serial, sizeof(serial),
kCFStringEncodingASCII)) {
DEBUG_FMT("Found serial number %s", serial);
port->usb_serial = strdup(serial);
}
CFRelease(cf_property);
}
CFRelease(cf_property);
}

IOObjectRelease(ioport);
Expand Down
11 changes: 8 additions & 3 deletions serialport.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static enum sp_return get_config(struct sp_port *port, struct port_data *data,
static enum sp_return set_config(struct sp_port *port, struct port_data *data,
const struct sp_port_config *config);

SP_API enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
SP_API enum sp_return sp_get_port_by_name_desc(const char *portname, struct sp_port **port_ptr, bool fetchDescriptors)
{
struct sp_port *port;
#ifndef NO_PORT_METADATA
Expand Down Expand Up @@ -106,7 +106,7 @@ SP_API enum sp_return sp_get_port_by_name(const char *portname, struct sp_port *
port->bluetooth_address = NULL;

#ifndef NO_PORT_METADATA
if ((ret = get_port_details(port)) != SP_OK) {
if ((ret = get_port_details(port, fetchDescriptors)) != SP_OK) {
sp_free_port(port);
return ret;
}
Expand All @@ -117,6 +117,11 @@ SP_API enum sp_return sp_get_port_by_name(const char *portname, struct sp_port *
RETURN_OK();
}

SP_API enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
{
return sp_get_port_by_name_desc(portname, port_ptr, true);
}

SP_API char *sp_get_port_name(const struct sp_port *port)
{
TRACE("%p", port);
Expand Down Expand Up @@ -313,7 +318,7 @@ SP_PRIV struct sp_port **list_append(struct sp_port **list,
if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
goto fail;
list = tmp;
if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
if (sp_get_port_by_name_desc(portname, &list[count], false) != SP_OK)
goto fail;
list[count + 1] = NULL;
return list;
Expand Down
55 changes: 29 additions & 26 deletions windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#define MAX_USB_PATH ((8 * 3) + (7 * 1) + 1)

static void enumerate_hub(struct sp_port *port, const char *hub_name,
const char *parent_path, DEVINST dev_inst);
const char *parent_path, DEVINST dev_inst, bool fetchDescriptors);

static char *wc_to_utf8(PWCHAR wc_buffer, ULONG size)
{
Expand Down Expand Up @@ -149,7 +149,7 @@ static char *get_string_descriptor(HANDLE hub_device, ULONG connection_index,
}

static void enumerate_hub_ports(struct sp_port *port, HANDLE hub_device,
ULONG nb_ports, const char *parent_path, DEVINST dev_inst)
ULONG nb_ports, const char *parent_path, DEVINST dev_inst, bool fetchDescriptors)
{
char path[MAX_USB_PATH];
ULONG index = 0;
Expand Down Expand Up @@ -200,7 +200,7 @@ static void enumerate_hub_ports(struct sp_port *port, HANDLE hub_device,
if ((ext_hub_name = get_external_hub_name(hub_device, index))) {
snprintf(path, sizeof(path), "%s%ld.",
parent_path, connection_info_ex->ConnectionIndex);
enumerate_hub(port, ext_hub_name, path, dev_inst);
enumerate_hub(port, ext_hub_name, path, dev_inst, fetchDescriptors);
}
free(connection_info_ex);
} else {
Expand All @@ -218,21 +218,24 @@ static void enumerate_hub_ports(struct sp_port *port, HANDLE hub_device,
port->usb_vid = connection_info_ex->DeviceDescriptor.idVendor;
port->usb_pid = connection_info_ex->DeviceDescriptor.idProduct;

if (connection_info_ex->DeviceDescriptor.iManufacturer)
port->usb_manufacturer = get_string_descriptor(hub_device,index,
connection_info_ex->DeviceDescriptor.iManufacturer);
if (connection_info_ex->DeviceDescriptor.iProduct)
port->usb_product = get_string_descriptor(hub_device, index,
connection_info_ex->DeviceDescriptor.iProduct);
if (connection_info_ex->DeviceDescriptor.iSerialNumber) {
port->usb_serial = get_string_descriptor(hub_device, index,
connection_info_ex->DeviceDescriptor.iSerialNumber);
if (port->usb_serial == NULL) {
//composite device, get the parent's serial number
char device_id[MAX_DEVICE_ID_LEN];
if (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS) {
if (CM_Get_Device_IDA(dev_inst, device_id, sizeof(device_id), 0) == CR_SUCCESS)
port->usb_serial = strdup(strrchr(device_id, '\\')+1);
if (fetchDescriptors) {

if (connection_info_ex->DeviceDescriptor.iManufacturer)
port->usb_manufacturer = get_string_descriptor(hub_device,index,
connection_info_ex->DeviceDescriptor.iManufacturer);
if (connection_info_ex->DeviceDescriptor.iProduct)
port->usb_product = get_string_descriptor(hub_device, index,
connection_info_ex->DeviceDescriptor.iProduct);
if (connection_info_ex->DeviceDescriptor.iSerialNumber) {
port->usb_serial = get_string_descriptor(hub_device, index,
connection_info_ex->DeviceDescriptor.iSerialNumber);
if (port->usb_serial == NULL) {
//composite device, get the parent's serial number
char device_id[MAX_DEVICE_ID_LEN];
if (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS) {
if (CM_Get_Device_IDA(dev_inst, device_id, sizeof(device_id), 0) == CR_SUCCESS)
port->usb_serial = strdup(strrchr(device_id, '\\')+1);
}
}
}
}
Expand All @@ -244,7 +247,7 @@ static void enumerate_hub_ports(struct sp_port *port, HANDLE hub_device,
}

static void enumerate_hub(struct sp_port *port, const char *hub_name,
const char *parent_path, DEVINST dev_inst)
const char *parent_path, DEVINST dev_inst, bool fetchDescriptors)
{
USB_NODE_INFORMATION hub_info;
HANDLE hub_device;
Expand All @@ -267,24 +270,24 @@ static void enumerate_hub(struct sp_port *port, const char *hub_name,
&hub_info, size, &hub_info, size, &size, NULL))
/* Enumerate the ports of the hub. */
enumerate_hub_ports(port, hub_device,
hub_info.u.HubInformation.HubDescriptor.bNumberOfPorts, parent_path, dev_inst);
hub_info.u.HubInformation.HubDescriptor.bNumberOfPorts, parent_path, dev_inst, fetchDescriptors);

CloseHandle(hub_device);
}

static void enumerate_host_controller(struct sp_port *port,
HANDLE host_controller_device,
DEVINST dev_inst)
DEVINST dev_inst, bool fetchDescriptors)
{
char *root_hub_name;

if ((root_hub_name = get_root_hub_name(host_controller_device))) {
enumerate_hub(port, root_hub_name, "", dev_inst);
enumerate_hub(port, root_hub_name, "", dev_inst, fetchDescriptors);
free(root_hub_name);
}
}

static void get_usb_details(struct sp_port *port, DEVINST dev_inst_match)
static void get_usb_details(struct sp_port *port, DEVINST dev_inst_match, bool fetchDescriptors)
{
HDEVINFO device_info;
SP_DEVINFO_DATA device_info_data;
Expand Down Expand Up @@ -334,7 +337,7 @@ static void get_usb_details(struct sp_port *port, DEVINST dev_inst_match)
GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (host_controller_device != INVALID_HANDLE_VALUE) {
enumerate_host_controller(port, host_controller_device, dev_inst_match);
enumerate_host_controller(port, host_controller_device, dev_inst_match, fetchDescriptors);
CloseHandle(host_controller_device);
}
free(device_detail_data);
Expand All @@ -344,7 +347,7 @@ static void get_usb_details(struct sp_port *port, DEVINST dev_inst_match)
return;
}

SP_PRIV enum sp_return get_port_details(struct sp_port *port)
SP_PRIV enum sp_return get_port_details(struct sp_port *port, bool fetchDescriptors)
{
/*
* Description limited to 127 char, anything longer
Expand Down Expand Up @@ -465,7 +468,7 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
CloseHandle(handle);

/* Retrieve USB device details from the device descriptor. */
get_usb_details(port, device_info_data.DevInst);
get_usb_details(port, device_info_data.DevInst, fetchDescriptors);
}
break;
}
Expand Down

0 comments on commit 658f212

Please sign in to comment.