view duart28/main.c @ 33:df284688d0c8

fc-duart28-conf check-eeprom: same change as in fteeprom-read
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 09 Sep 2023 20:04:18 +0000
parents 530ec3792de8
children b9ecfa54fe2b
line wrap: on
line source

/*
 * Main module for fc-duart28-conf utility.
 */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <usb.h>
#include "../libuwrap/open_close.h"

extern struct usb_device *find_duart28_usbdev();

oper_find()
{
	struct usb_device *dev;

	dev = find_duart28_usbdev();
	if (!dev)
		printf("No DUART28 adapter found\n");
	return 0;
}

oper_check_eeprom()
{
	struct usb_device *dev;
	usb_dev_handle *usbh;

	dev = find_duart28_usbdev();
	if (!dev) {
		fprintf(stderr, "error: no DUART28 adapter found\n");
		exit(1);
	}
	usbh = usb_open(dev);
	if (!usbh) {
		fprintf(stderr, "error: usb_open() failed\n");
		exit(1);
	}
	read_eeprom(usbh);
	usb_close(usbh);
	analyze_eeprom();
	return 0;
}

oper_program(newconf)
{
	struct usb_device *dev;
	usb_dev_handle *usbh;
	int prev;

	dev = find_duart28_usbdev();
	if (!dev) {
		fprintf(stderr, "error: no DUART28 adapter found\n");
		exit(1);
	}
	usbh = usbwrap_open_dev(dev, 1);
	read_eeprom(usbh);
	prev = analyze_eeprom();
	if (prev == newconf) {
		printf("EEPROM is already in the right state, nothing to do\n");
		usbwrap_close_dev(usbh);
		return 0;
	}
	printf("Reprogramming to DUART28%c configuration\n", newconf);
	update_eeprom(usbh, newconf);
	usbwrap_close_dev(usbh);
	printf("EEPROM changed, replug the USB device to take effect\n");
	return 0;
}

main(argc, argv)
	char **argv;
{
	if (argc < 2)
		return oper_find();
	if (!strcmp(argv[1], "find")) {
		if (argc != 2) {
			fprintf(stderr,
				"error: %s find command takes no arguments\n",
				argv[0]);
			exit(1);
		}
		return oper_find();
	}
	if (!strcmp(argv[1], "check-eeprom")) {
		if (argc != 2) {
			fprintf(stderr,
			"error: %s check-eeprom command takes no arguments\n",
				argv[0]);
			exit(1);
		}
		return oper_check_eeprom();
	}
	if (!strcmp(argv[1], "set")) {
		if (argc != 3) {
			fprintf(stderr,
				"error: %s set command takes 1 argument\n",
				argv[0]);
			exit(1);
		}
		if (!strcmp(argv[2], "C") || !strcmp(argv[2], "S"))
			return oper_program(argv[2][0]);
		if (!strcmp(argv[2], "c") || !strcmp(argv[2], "s"))
			return oper_program(toupper(argv[2][0]));
		fprintf(stderr, "error: %s set argument must be C or S\n",
			argv[0]);
		exit(1);
	}
	fprintf(stderr, "%s error: non-understood command \"%s\"\n", argv[0],
		argv[1]);
	exit(1);
}