changeset 98:1cacc1ae56f0

cp2102 tools: convert to -d option for non-default device
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Sep 2023 04:45:13 +0000
parents 8d35346f1d46
children c59011177e2e
files cp2102/find_dev.c cp2102/read_baudtab.c cp2102/read_eeprom_main.c cp2102/read_partno.c cp2102/write_eeprom_main.c
diffstat 5 files changed, 100 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/cp2102/find_dev.c	Thu Sep 28 03:26:11 2023 +0000
+++ b/cp2102/find_dev.c	Thu Sep 28 04:45:13 2023 +0000
@@ -22,20 +22,20 @@
 {
 	struct usb_device *dev;
 
-	if (!strcmp(devsel_arg, "default")) {
+	if (devsel_arg) {
+		dev = find_usbdev_by_desc_string(devsel_arg);
+		if (!dev) {
+			fprintf(stderr,
+				"error: specified USB device not found\n");
+			exit(1);
+		}
+	} else {
 		dev = find_usbdev_by_matchspec(&default_id);
 		if (!dev) {
 			fprintf(stderr,
 				"error: no default-ID CP2102 device found\n");
 			exit(1);
 		}
-	} else {
-		dev = find_usbdev_by_desc_string(devsel_arg);
-		if (!dev) {
-			fprintf(stderr,
-				"error: specified USB device not found\n");
-			exit(1);
-		}
 	}
 	return dev;
 }
--- a/cp2102/read_baudtab.c	Thu Sep 28 03:26:11 2023 +0000
+++ b/cp2102/read_baudtab.c	Thu Sep 28 04:45:13 2023 +0000
@@ -17,6 +17,30 @@
 extern struct usb_device *find_cp2102_device();
 
 u_char eeprom[SIZE_EEPROM];
+char *device_selector;
+
+process_cmdline(argc, argv)
+	char **argv;
+{
+	int c;
+	extern int optind;
+	extern char *optarg;
+
+	while ((c = getopt(argc, argv, "d:")) != EOF) {
+		switch (c) {
+		case 'd':
+			device_selector = optarg;
+			continue;
+		default:
+		usage:
+			fprintf(stderr, "usage: %s [-d device-selector]\n",
+				argv[0]);
+			exit(1);
+		}
+	}
+	if (argc != optind)
+		goto usage;
+}
 
 main(argc, argv)
 	char **argv;
@@ -24,11 +48,8 @@
 	struct usb_device *dev;
 	usb_dev_handle *usbh;
 
-	if (argc != 2) {
-		fprintf(stderr, "usage: %s device-selector\n", argv[0]);
-		exit(1);
-	}
-	dev = find_cp2102_device(argv[1]);
+	process_cmdline(argc, argv);
+	dev = find_cp2102_device(device_selector);
 	usbh = usb_open(dev);
 	if (!usbh) {
 		fprintf(stderr, "error: usb_open() failed\n");
--- a/cp2102/read_eeprom_main.c	Thu Sep 28 03:26:11 2023 +0000
+++ b/cp2102/read_eeprom_main.c	Thu Sep 28 04:45:13 2023 +0000
@@ -18,6 +18,32 @@
 extern struct usb_device *find_cp2102_device();
 
 u_char eeprom[SIZE_EEPROM];
+char *device_selector, *outfilename;
+
+process_cmdline(argc, argv)
+	char **argv;
+{
+	int c;
+	extern int optind;
+	extern char *optarg;
+
+	while ((c = getopt(argc, argv, "d:")) != EOF) {
+		switch (c) {
+		case 'd':
+			device_selector = optarg;
+			continue;
+		default:
+		usage:
+			fprintf(stderr,
+			"usage: %s [-d device-selector] output-hex-file\n",
+				argv[0]);
+			exit(1);
+		}
+	}
+	if (argc != optind + 1)
+		goto usage;
+	outfilename = argv[optind];
+}
 
 main(argc, argv)
 	char **argv;
@@ -26,12 +52,8 @@
 	usb_dev_handle *usbh;
 	FILE *outf;
 
-	if (argc != 3) {
-		fprintf(stderr, "usage: %s device-selector output-hex-file\n",
-			argv[0]);
-		exit(1);
-	}
-	dev = find_cp2102_device(argv[1]);
+	process_cmdline(argc, argv);
+	dev = find_cp2102_device(device_selector);
 	usbh = usb_open(dev);
 	if (!usbh) {
 		fprintf(stderr, "error: usb_open() failed\n");
@@ -39,9 +61,9 @@
 	}
 	read_eeprom(usbh);
 	usb_close(usbh);
-	outf = fopen(argv[2], "w");
+	outf = fopen(outfilename, "w");
 	if (!outf) {
-		perror(argv[2]);
+		perror(outfilename);
 		exit(1);
 	}
 	intel_hex_out(eeprom, outf);
--- a/cp2102/read_partno.c	Thu Sep 28 03:26:11 2023 +0000
+++ b/cp2102/read_partno.c	Thu Sep 28 04:45:13 2023 +0000
@@ -15,6 +15,30 @@
 #include "cp210x_defs.h"
 
 extern struct usb_device *find_cp2102_device();
+char *device_selector;
+
+process_cmdline(argc, argv)
+	char **argv;
+{
+	int c;
+	extern int optind;
+	extern char *optarg;
+
+	while ((c = getopt(argc, argv, "d:")) != EOF) {
+		switch (c) {
+		case 'd':
+			device_selector = optarg;
+			continue;
+		default:
+		usage:
+			fprintf(stderr, "usage: %s [-d device-selector]\n",
+				argv[0]);
+			exit(1);
+		}
+	}
+	if (argc != optind)
+		goto usage;
+}
 
 main(argc, argv)
 	char **argv;
@@ -24,11 +48,8 @@
 	u_char partno;
 	int rc;
 
-	if (argc != 2) {
-		fprintf(stderr, "usage: %s device-selector\n", argv[0]);
-		exit(1);
-	}
-	dev = find_cp2102_device(argv[1]);
+	process_cmdline(argc, argv);
+	dev = find_cp2102_device(device_selector);
 	usbh = usb_open(dev);
 	if (!usbh) {
 		fprintf(stderr, "error: usb_open() failed\n");
--- a/cp2102/write_eeprom_main.c	Thu Sep 28 03:26:11 2023 +0000
+++ b/cp2102/write_eeprom_main.c	Thu Sep 28 04:45:13 2023 +0000
@@ -27,24 +27,24 @@
 	extern int optind;
 	extern char *optarg;
 
-	while ((c = getopt(argc, argv, "n")) != EOF) {
+	while ((c = getopt(argc, argv, "d:n")) != EOF) {
 		switch (c) {
+		case 'd':
+			device_selector = optarg;
+			continue;
 		case 'n':
 			no_detach = 1;
 			continue;
 		default:
-			/* error msg already printed */
+		usage:
+			fprintf(stderr, "usage: %s [hw-options] eeprom-image\n",
+				argv[0]);
 			exit(1);
 		}
 	}
-	if (argc != optind + 2) {
-		fprintf(stderr,
-			"usage: %s [options] device-selector eeprom-image\n",
-			argv[0]);
-		exit(1);
-	}
-	device_selector = argv[optind];
-	input_filename = argv[optind + 1];
+	if (argc != optind + 1)
+		goto usage;
+	input_filename = argv[optind];
 }
 
 main(argc, argv)