view ffstools/newcomp/compile-fc-batt.c @ 752:c79aaed75bd8

compile-fc-batt: allow possible third field in source lines Battery tables maintained in the fc-battery-conf repository will now have a third field added, defining thresholds for the battery bars icon, and there will be a new utility to compile them into the new /etc/batterytab2 file read by the FC Tourmaline version of our FCHG driver. For backward compatibility with the original Magnetite version of FCHG, compile-fc-batt remains the tool for compiling the original /etc/batterytab file format, and it needs to ignore the newly added third field in battery table sources.
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 05 Nov 2020 20:37:55 +0000
parents 182c3ae209f6
children
line wrap: on
line source

/*
 * This utility compiles a table of battery thresholds for the 2017-12 version
 * of FreeCalypso battery management code from ASCII source into the binary
 * form suitable for uploading into /etc/batterytab on a FreeCalypso device.
 */

#include <ctype.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../rvinterf/include/exitcodes.h"

char *infname;
FILE *inf, *outf;
char linebuf[256];
int lineno, record_count;
unsigned last_mv, last_percent;

process_line()
{
	char *cp;
	unsigned mv, percent;

	for (cp = linebuf; isspace(*cp); cp++)
		;
	if (*cp == '\0' || *cp == '#')
		return(0);
	if (!isdigit(*cp)) {
inv:		fprintf(stderr, "%s line %d: invalid syntax\n", infname,
			lineno);
		exit(ERROR_USAGE);
	}
	mv = strtoul(cp, 0, 10);
	while (isdigit(*cp))
		cp++;
	if (!isspace(*cp))
		goto inv;
	while (isspace(*cp))
		cp++;
	if (!isdigit(*cp))
		goto inv;
	percent = strtoul(cp, 0, 10);
	while (isdigit(*cp))
		cp++;
	if (*cp && !isspace(*cp))
		goto inv;
	/* ignore possible third field added in 2020-11 for bars threshold */
	if (mv > 0xFFFF) {
		fprintf(stderr, "%s line %d: the millivolt value is invalid\n",
			infname, lineno);
		exit(ERROR_USAGE);
	}
	if (percent > 100) {
		fprintf(stderr, "%s line %d: the percent value is invalid\n",
			infname, lineno);
		exit(ERROR_USAGE);
	}
	if (record_count) {
		if (mv >= last_mv) {
			fprintf(stderr,
			"%s line %d: millivolt numbers must be decreasing\n",
				infname, lineno);
			exit(ERROR_USAGE);
		}
		if (percent >= last_percent) {
			fprintf(stderr,
			"%s line %d: percent numbers must be decreasing\n",
				infname, lineno);
			exit(ERROR_USAGE);
		}
	}
	putc(mv, outf);
	putc(mv >> 8, outf);
	putc(percent, outf);
	putc(0, outf);
	last_mv = mv;
	last_percent = percent;
	record_count++;
	return(1);
}

main(argc, argv)
	char **argv;
{
	if (argc != 3) {
		fprintf(stderr, "usage: %s srcfile output-binfile\n", argv[0]);
		exit(ERROR_USAGE);
	}
	infname = argv[1];
	inf = fopen(infname, "r");
	if (!inf) {
		perror(infname);
		exit(ERROR_UNIX);
	}
	outf = fopen(argv[2], "w");
	if (!outf) {
		perror(argv[2]);
		exit(ERROR_UNIX);
	}
	for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++)
		process_line();
	fclose(inf);
	fclose(outf);
	if (record_count)
		exit(0);
	fprintf(stderr, "error: %s is empty\n", infname);
	exit(ERROR_USAGE);
}