Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interface number #126

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ environment:
dependencies:
flutter:
sdk: flutter
libserialport: ^0.3.0
libserialport:
git: https://github.com/AurelienBallier/libserialport.dart

dev_dependencies:
flutter_test:
Expand Down
2 changes: 1 addition & 1 deletion third_party/libserialport/libserialport.h
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ SP_API enum sp_return sp_get_port_usb_bus_address(const struct sp_port *port,
*
* @since 0.1.1
*/
SP_API enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port, int *usb_vid, int *usb_pid);
SP_API enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port, int *usb_vid, int *usb_pid, int *usb_interface_number);

/**
* Get the USB manufacturer string of a USB serial adapter port.
Expand Down
1 change: 1 addition & 0 deletions third_party/libserialport/libserialport_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ struct sp_port {
int usb_address;
int usb_vid;
int usb_pid;
int usb_interface_number;
char *usb_manufacturer;
char *usb_product;
char *usb_serial;
Expand Down
11 changes: 10 additions & 1 deletion third_party/libserialport/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
* would not be user friendly anyway.
*/
char description[128];
int bus, address;
int bus, address, interface_number;
unsigned int vid, pid;
char manufacturer[128], product[128], serial[128];
char baddr[32];
Expand Down Expand Up @@ -105,10 +105,19 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
if (count != 1)
continue;

snprintf(file_name, sizeof(file_name), dir_name, dev, "", "bInterfaceNumber");
if (!(file = fopen_cloexec_rdonly(file_name)))
continue;
count = fscanf(file, "%d", &interface_number);
fclose(file);
if (count != 1)
continue;

port->usb_bus = bus;
port->usb_address = address;
port->usb_vid = vid;
port->usb_pid = pid;
port->usb_interface_number = interface_number;

snprintf(file_name, sizeof(file_name), dir_name, dev, sub_dir, "product");
if ((file = fopen_cloexec_rdonly(file_name))) {
Expand Down
5 changes: 4 additions & 1 deletion third_party/libserialport/serialport.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ SP_API enum sp_return sp_get_port_by_name(const char *portname, struct sp_port *
port->usb_address = -1;
port->usb_vid = -1;
port->usb_pid = -1;
port->usb_interface_number = -1;
port->usb_manufacturer = NULL;
port->usb_product = NULL;
port->usb_serial = NULL;
Expand Down Expand Up @@ -179,7 +180,7 @@ SP_API enum sp_return sp_get_port_usb_bus_address(const struct sp_port *port,
}

SP_API enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port,
int *usb_vid, int *usb_pid)
int *usb_vid, int *usb_pid, int *usb_interface_number)
{
TRACE("%p", port);

Expand All @@ -194,6 +195,8 @@ SP_API enum sp_return sp_get_port_usb_vid_pid(const struct sp_port *port,
*usb_vid = port->usb_vid;
if (usb_pid)
*usb_pid = port->usb_pid;
if (usb_interface_number)
*usb_interface_number = port->usb_interface_number;

RETURN_OK();
}
Expand Down
33 changes: 31 additions & 2 deletions third_party/libserialport/windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ static void enumerate_hub_ports(struct sp_port *port, HANDLE hub_device,

/* Finally grab detailed information regarding the device. */
port->usb_address = connection_info_ex->DeviceAddress + 1;
port->usb_vid = connection_info_ex->DeviceDescriptor.idVendor;
port->usb_pid = connection_info_ex->DeviceDescriptor.idProduct;
/*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,
Expand Down Expand Up @@ -417,6 +417,7 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
/* Get more informations for USB connected ports. */
if (port->transport == SP_TRANSPORT_USB) {
char usb_path[MAX_USB_PATH] = "", tmp[MAX_USB_PATH];
char hardware_ids[512];
char device_id[MAX_DEVICE_ID_LEN];

/* Recurse over parents to build the USB device path. */
Expand Down Expand Up @@ -457,9 +458,37 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
snprintf(usb_path, sizeof(usb_path), "%d%s%s",
(int)address, *tmp ? "." : "", tmp);
}

/* Grab hardware ids. */
size = sizeof(hardware_ids);
if (CM_Get_DevNode_Registry_PropertyA(dev_inst, CM_DRP_HARDWAREID ,
0, &hardware_ids, &size, 0) == CR_SUCCESS) {
//printf("Result hardware ids: %s\n", hardware_ids);
}
} while (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS);

port->usb_path = strdup(usb_path);

char *result = strstr(hardware_ids, "MI_");
size_t MI_idx = result - hardware_ids;
if((MI_idx >= 0) && ((MI_idx + 4) < sizeof(hardware_ids))) {
int a = hardware_ids[MI_idx + 3] - '0';
int b = hardware_ids[MI_idx + 4] -'0';
port->usb_interface_number = a * 10 + b;
}


result = strstr(hardware_ids, "VID_");
size_t VID_idx = result - hardware_ids;
if((VID_idx >= 0) && ((VID_idx + 7) < sizeof(hardware_ids))) {
sscanf((hardware_ids + VID_idx + 4), "%4x", &port->usb_vid);
}

result = strstr(hardware_ids, "PID_");
size_t PID_idx = result - hardware_ids;
if((PID_idx >= 0) && ((PID_idx + 7) < sizeof(hardware_ids))) {
sscanf((hardware_ids + PID_idx + 4), "%4x", &port->usb_pid);
}

/* Wake up the USB device to be able to read string descriptor. */
char *escaped_port_name;
Expand Down