annotate ffstools/newcomp/compile-fc-batt.c @ 816:b3724fe6c581

fc-tmsh bsim commands implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 14 May 2021 17:16:43 +0000
parents c79aaed75bd8
children
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 /*
752
c79aaed75bd8 compile-fc-batt: allow possible third field in source lines
Mychaela Falconia <falcon@freecalypso.org>
parents: 318
diff changeset
2 * This utility compiles a table of battery thresholds for the 2017-12 version
c79aaed75bd8 compile-fc-batt: allow possible third field in source lines
Mychaela Falconia <falcon@freecalypso.org>
parents: 318
diff changeset
3 * of FreeCalypso battery management code from ASCII source into the binary
c79aaed75bd8 compile-fc-batt: allow possible third field in source lines
Mychaela Falconia <falcon@freecalypso.org>
parents: 318
diff changeset
4 * form suitable for uploading into /etc/batterytab on a FreeCalypso device.
318
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++;
752
c79aaed75bd8 compile-fc-batt: allow possible third field in source lines
Mychaela Falconia <falcon@freecalypso.org>
parents: 318
diff changeset
46 if (*cp && !isspace(*cp))
318
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 goto inv;
752
c79aaed75bd8 compile-fc-batt: allow possible third field in source lines
Mychaela Falconia <falcon@freecalypso.org>
parents: 318
diff changeset
48 /* ignore possible third field added in 2020-11 for bars threshold */
318
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 if (mv > 0xFFFF) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 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
51 infname, lineno);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 exit(ERROR_USAGE);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (percent > 100) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 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
56 infname, lineno);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 exit(ERROR_USAGE);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 if (record_count) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 if (mv >= last_mv) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 fprintf(stderr,
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 "%s line %d: millivolt numbers must be decreasing\n",
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 infname, lineno);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 exit(ERROR_USAGE);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 if (percent >= last_percent) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 fprintf(stderr,
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 "%s line %d: percent numbers must be decreasing\n",
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 infname, lineno);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 exit(ERROR_USAGE);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 }
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 putc(mv, outf);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 putc(mv >> 8, outf);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 putc(percent, outf);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 putc(0, outf);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 last_mv = mv;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 last_percent = percent;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 record_count++;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 return(1);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 }
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 main(argc, argv)
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 char **argv;
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 if (argc != 3) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 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
88 exit(ERROR_USAGE);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 infname = argv[1];
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 inf = fopen(infname, "r");
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 if (!inf) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 perror(infname);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 exit(ERROR_UNIX);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 outf = fopen(argv[2], "w");
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 if (!outf) {
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 perror(argv[2]);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 exit(ERROR_UNIX);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 }
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++)
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 process_line();
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 fclose(inf);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 fclose(outf);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 if (record_count)
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 exit(0);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 fprintf(stderr, "error: %s is empty\n", infname);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 exit(ERROR_USAGE);
182c3ae209f6 compile-fc-batt tool written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }