changeset 11:fe4231326fb2

libuwrap: implement locating by description-string
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 07 Sep 2023 04:58:16 +0000
parents fd3fcba5a8ac
children 80e521d6609c
files libuwrap/Makefile libuwrap/find_desc_str.c libuwrap/find_dev.h
diffstat 3 files changed, 70 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libuwrap/Makefile	Thu Sep 07 04:18:30 2023 +0000
+++ b/libuwrap/Makefile	Thu Sep 07 04:58:16 2023 +0000
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-OBJS=	find_busdev.o find_matchspec.o prelim_init.o
+OBJS=	find_busdev.o find_desc_str.o find_matchspec.o prelim_init.o
 LIB=	libuwrap.a
 
 all:	${LIB}
--- /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);
+}
--- a/libuwrap/find_dev.h	Thu Sep 07 04:18:30 2023 +0000
+++ b/libuwrap/find_dev.h	Thu Sep 07 04:58:16 2023 +0000
@@ -6,9 +6,9 @@
 struct usbdev_matchspec {
 	unsigned	usb_vid;
 	unsigned	usb_pid;
-	char		*manuf_string;
-	char		*product_string;
-	char		*serial;
+	const char	*manuf_string;
+	const char	*product_string;
+	const char	*serial;
 	unsigned	index;
 };