view libuwrap/find_desc_str.c @ 18:fce3d4baa2b4

libftmini: implement EEPROM write
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 07 Sep 2023 22:43:53 +0000
parents fe4231326fb2
children
line wrap: on
line source

/*
 * 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);
}