diff libuwrap/find_desc_str.c @ 11:fe4231326fb2

libuwrap: implement locating by description-string
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 07 Sep 2023 04:58:16 +0000
parents libuwrap/find_matchspec.c@ab506f6aa57c
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libuwrap/find_desc_str.c	Thu Sep 07 04:58:16 2023 +0000
@@ -0,0 +1,66 @@
+/*
+ * In this module we implement the function that locates a USB device
+ * by libftdi-style description-string.
+ */
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <usb.h>
+#include "find_dev.h"
+
+struct usb_device *
+find_usbdev_by_desc_string(const char *desc_string)
+{
+	struct usbdev_matchspec match;
+	const char *cp;
+	char *endp;
+
+	if (!desc_string[0] || desc_string[1] != ':') {
+inv_syntax:	fprintf(stderr,
+		"error: USB device selector string has invalid syntax\n");
+		exit(1);
+	}
+	switch (desc_string[0]) {
+	case 'd':
+		return find_usbdev_by_busdev(desc_string + 2);
+	case 'i':
+	case 's':
+		break;
+	default:
+		goto inv_syntax;
+	}
+	bzero(&match, sizeof match);
+	cp = desc_string + 2;
+	if (!isdigit(*cp))
+		goto inv_syntax;
+	match.usb_vid = strtoul(cp, &endp, 0);
+	cp = endp;
+	if (*cp++ != ':')
+		goto inv_syntax;
+	if (!isdigit(*cp))
+		goto inv_syntax;
+	match.usb_pid = strtoul(cp, &endp, 0);
+	cp = endp;
+	if (!*cp) {
+		if (desc_string[0] != 'i')
+			goto inv_syntax;
+		return find_usbdev_by_matchspec(&match);
+	}
+	if (*cp++ != ':')
+		goto inv_syntax;
+	switch (desc_string[0]) {
+	case 'i':
+		if (!isdigit(*cp))
+			goto inv_syntax;
+		match.index = strtoul(cp, &endp, 0);
+		if (*endp)
+			goto inv_syntax;
+		break;
+	case 's':
+		match.serial = cp;
+	}
+	return find_usbdev_by_matchspec(&match);
+}