annotate ffstools/newcomp/compile-fc-batt.c @ 329:18c692984549

uptools/libcoding: hex line decoding implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 03 Feb 2018 20:37:14 +0000
parents 182c3ae209f6
children c79aaed75bd8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
318
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This utility compiles a table of battery thresholds for the new FreeCalypso
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * battery management code from ASCII source into the binary form suitable
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * for uploading into /etc/batterytab on a FreeCalypso device.
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <ctype.h>
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <string.h>
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <strings.h>
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdio.h>
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdlib.h>
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include "../../rvinterf/include/exitcodes.h"
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 char *infname;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 FILE *inf, *outf;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 char linebuf[256];
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 int lineno, record_count;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 unsigned last_mv, last_percent;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 process_line()
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 char *cp;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 unsigned mv, percent;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 for (cp = linebuf; isspace(*cp); cp++)
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 ;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 if (*cp == '\0' || *cp == '#')
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 return(0);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (!isdigit(*cp)) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 inv: fprintf(stderr, "%s line %d: invalid syntax\n", infname,
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 lineno);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 exit(ERROR_USAGE);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 mv = strtoul(cp, 0, 10);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 while (isdigit(*cp))
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 cp++;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 if (!isspace(*cp))
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 goto inv;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 while (isspace(*cp))
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 cp++;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 if (!isdigit(*cp))
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 goto inv;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 percent = strtoul(cp, 0, 10);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 while (isdigit(*cp))
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 cp++;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 while (isspace(*cp))
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 cp++;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 if (*cp != '\0' && *cp != '#')
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 goto inv;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (mv > 0xFFFF) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 fprintf(stderr, "%s line %d: the millivolt value is invalid\n",
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 infname, lineno);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 exit(ERROR_USAGE);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 if (percent > 100) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 fprintf(stderr, "%s line %d: the percent value is invalid\n",
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 infname, lineno);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 exit(ERROR_USAGE);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 if (record_count) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 if (mv >= last_mv) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 fprintf(stderr,
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 "%s line %d: millivolt numbers must be decreasing\n",
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 infname, lineno);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 exit(ERROR_USAGE);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 if (percent >= last_percent) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 fprintf(stderr,
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 "%s line %d: percent numbers must be decreasing\n",
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 infname, lineno);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 exit(ERROR_USAGE);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 putc(mv, outf);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 putc(mv >> 8, outf);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 putc(percent, outf);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 putc(0, outf);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 last_mv = mv;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 last_percent = percent;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 record_count++;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 return(1);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 main(argc, argv)
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 char **argv;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 if (argc != 3) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 fprintf(stderr, "usage: %s srcfile output-binfile\n", argv[0]);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 exit(ERROR_USAGE);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 infname = argv[1];
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 inf = fopen(infname, "r");
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 if (!inf) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 perror(infname);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 exit(ERROR_UNIX);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 outf = fopen(argv[2], "w");
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 if (!outf) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 perror(argv[2]);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 exit(ERROR_UNIX);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++)
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 process_line();
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 fclose(inf);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 fclose(outf);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 if (record_count)
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 exit(0);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 fprintf(stderr, "error: %s is empty\n", infname);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 exit(ERROR_USAGE);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 }