comparison ffstools/newcomp/compile-fc-batt2.c @ 753:cae22bec3cba

compile-fc-batt2 utility written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 05 Nov 2020 22:07:13 +0000
parents
children
comparison
equal deleted inserted replaced
752:c79aaed75bd8 753:cae22bec3cba
1 /*
2 * This utility compiles a table of battery thresholds for the 2020-10 version
3 * of FreeCalypso battery management code from ASCII source into the binary
4 * form suitable for uploading into /etc/batterytab2 on a FreeCalypso device.
5 */
6
7 #include <sys/types.h>
8 #include <ctype.h>
9 #include <string.h>
10 #include <strings.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include "../../rvinterf/include/exitcodes.h"
14
15 #define MIN_PERCENT_THRESH 5
16 #define MAX_PERCENT_THRESH 21 /* allowing 5% steps */
17 #define NB_BARS_THRESH 4
18
19 char *infname;
20 FILE *inf;
21 char linebuf[256];
22 int lineno;
23
24 unsigned main_array_mv[MAX_PERCENT_THRESH];
25 unsigned main_array_percent[MAX_PERCENT_THRESH];
26 unsigned main_array_count;
27
28 u_char bars_thresh_array[NB_BARS_THRESH];
29 unsigned expect_bars = NB_BARS_THRESH, bars_index;
30
31 process_line()
32 {
33 char *cp;
34 unsigned mv, percent, bars;
35 int bars_set;
36
37 for (cp = linebuf; isspace(*cp); cp++)
38 ;
39 if (*cp == '\0' || *cp == '#')
40 return(0);
41 if (!isdigit(*cp)) {
42 inv: fprintf(stderr, "%s line %d: invalid syntax\n", infname,
43 lineno);
44 exit(ERROR_USAGE);
45 }
46 mv = strtoul(cp, 0, 10);
47 while (isdigit(*cp))
48 cp++;
49 if (!isspace(*cp))
50 goto inv;
51 while (isspace(*cp))
52 cp++;
53 if (!isdigit(*cp))
54 goto inv;
55 percent = strtoul(cp, 0, 10);
56 while (isdigit(*cp))
57 cp++;
58 while (isspace(*cp))
59 cp++;
60 if (*cp && *cp != '#') {
61 if (!isdigit(*cp))
62 goto inv;
63 bars = strtoul(cp, 0, 10);
64 while (isdigit(*cp))
65 cp++;
66 while (isspace(*cp))
67 cp++;
68 if (*cp && *cp != '#')
69 goto inv;
70 bars_set = 1;
71 } else
72 bars_set = 0;
73 if (mv > 0xFFFF) {
74 fprintf(stderr, "%s line %d: the millivolt value is invalid\n",
75 infname, lineno);
76 exit(ERROR_USAGE);
77 }
78 if (percent > 100) {
79 fprintf(stderr, "%s line %d: the percent value is invalid\n",
80 infname, lineno);
81 exit(ERROR_USAGE);
82 }
83 if (bars_set && (bars < 1 || bars > NB_BARS_THRESH)) {
84 fprintf(stderr, "%s line %d: the bars number is invalid\n",
85 infname, lineno);
86 exit(ERROR_USAGE);
87 }
88 if (main_array_count >= MAX_PERCENT_THRESH) {
89 fprintf(stderr, "%s line %d: too many table entries\n",
90 infname, lineno);
91 exit(ERROR_USAGE);
92 }
93 if (main_array_count) {
94 if (mv >= main_array_mv[main_array_count-1]) {
95 fprintf(stderr,
96 "%s line %d: millivolt numbers must be decreasing\n",
97 infname, lineno);
98 exit(ERROR_USAGE);
99 }
100 if (percent >= main_array_percent[main_array_count-1]) {
101 fprintf(stderr,
102 "%s line %d: percent numbers must be decreasing\n",
103 infname, lineno);
104 exit(ERROR_USAGE);
105 }
106 }
107 main_array_mv[main_array_count] = mv;
108 main_array_percent[main_array_count] = percent;
109 if (bars_set) {
110 if (bars != expect_bars) {
111 fprintf(stderr,
112 "%s line %d: bars threshold out of order\n",
113 infname, lineno);
114 exit(1);
115 }
116 bars_thresh_array[bars_index++] = main_array_count;
117 expect_bars--;
118 }
119 main_array_count++;
120 return(1);
121 }
122
123 write_output(filename)
124 char *filename;
125 {
126 FILE *of;
127 unsigned n, mv, percent;
128
129 of = fopen(filename, "w");
130 if (!of) {
131 perror(filename);
132 exit(ERROR_UNIX);
133 }
134 fwrite(bars_thresh_array, 1, NB_BARS_THRESH, of);
135 for (n = 0; n < main_array_count; n++) {
136 mv = main_array_mv[n];
137 percent = main_array_percent[n];
138 putc(mv, of);
139 putc(mv >> 8, of);
140 putc(percent, of);
141 putc(0, of);
142 }
143 fclose(of);
144 }
145
146 main(argc, argv)
147 char **argv;
148 {
149 if (argc != 3) {
150 fprintf(stderr, "usage: %s srcfile output-binfile\n", argv[0]);
151 exit(ERROR_USAGE);
152 }
153 infname = argv[1];
154 inf = fopen(infname, "r");
155 if (!inf) {
156 perror(infname);
157 exit(ERROR_UNIX);
158 }
159 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++)
160 process_line();
161 fclose(inf);
162 if (main_array_count < MIN_PERCENT_THRESH) {
163 fprintf(stderr, "error: %s is too short\n", infname);
164 exit(ERROR_USAGE);
165 }
166 if (expect_bars) {
167 fprintf(stderr, "error: %s did not specify bars thresholds\n",
168 infname);
169 exit(ERROR_USAGE);
170 }
171 write_output(argv[2]);
172 exit(0);
173 }