FreeCalypso > hg > fc-rfcal-tools
comparison cmu200/cableconf.c @ 68:3ec82dc1dbda
fc-cmu200d: implemented reading and parsing of cable config files (-c arg)
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 14 Jul 2017 04:34:15 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
67:3f92d88fbb1c | 68:3ec82dc1dbda |
---|---|
1 /* | |
2 * The code that reads and parses cable config files lives here. | |
3 */ | |
4 | |
5 #include <sys/param.h> | |
6 #include <ctype.h> | |
7 #include <stdio.h> | |
8 #include <stdlib.h> | |
9 #include <string.h> | |
10 #include <strings.h> | |
11 #include "band.h" | |
12 | |
13 char cableconf_directory[] = "/opt/freecalypso/rfcal/cableconf/"; | |
14 | |
15 extern struct band supported_bands[]; | |
16 extern int cmu200_rf_port; | |
17 | |
18 static void | |
19 cable_loss_line(cp, filename_for_errs, lineno) | |
20 char *cp, *filename_for_errs; | |
21 { | |
22 char *np; | |
23 struct band *band; | |
24 int dir; | |
25 unsigned num; | |
26 | |
27 while (isspace(*cp)) | |
28 cp++; | |
29 if (!isdigit(*cp)) { | |
30 inv: fprintf(stderr, "%s line %d: invalid syntax for cable-loss\n", | |
31 filename_for_errs, lineno); | |
32 exit(1); | |
33 } | |
34 for (np = cp; isdigit(*cp); cp++) | |
35 ; | |
36 if (cp[0] != 'd' && cp[0] != 'u' || cp[1] != 'l') | |
37 goto inv; | |
38 dir = *cp; | |
39 *cp = '\0'; | |
40 cp += 2; | |
41 for (band = supported_bands; band->name; band++) | |
42 if (!strcmp(band->name, np)) | |
43 break; | |
44 if (!band->name) { | |
45 fprintf(stderr, "%s line %d: frequency band \"%s\" not known\n", | |
46 filename_for_errs, lineno, np); | |
47 exit(1); | |
48 } | |
49 while (isspace(*cp)) | |
50 cp++; | |
51 if (!isdigit(*cp)) | |
52 goto inv; | |
53 for (np = cp; isdigit(*cp); cp++) | |
54 ; | |
55 if (cp[0] != '.' || !isdigit(cp[1])) | |
56 goto inv; | |
57 num = atoi(np) * 10 + (cp[1] - '0'); | |
58 cp += 2; | |
59 while (isspace(*cp)) | |
60 cp++; | |
61 if (*cp != '\0' && *cp != '#') | |
62 goto inv; | |
63 switch (dir) { | |
64 case 'd': | |
65 band->dl_cable_loss = num; | |
66 break; | |
67 case 'u': | |
68 band->ul_cable_loss = num; | |
69 break; | |
70 } | |
71 } | |
72 | |
73 static void | |
74 rf_port_line(cp, filename_for_errs, lineno) | |
75 char *cp, *filename_for_errs; | |
76 { | |
77 while (isspace(*cp)) | |
78 cp++; | |
79 if (!isdigit(*cp)) { | |
80 inv: fprintf(stderr, "%s line %d: invalid syntax for rf-port\n", | |
81 filename_for_errs, lineno); | |
82 exit(1); | |
83 } | |
84 if (cp[0] != '1' && cp[0] != '2' || isdigit(cp[1])) { | |
85 fprintf(stderr, "%s line %d: invalid RF port number\n", | |
86 filename_for_errs, lineno); | |
87 exit(1); | |
88 } | |
89 cmu200_rf_port = *cp++ - '0'; | |
90 while (isspace(*cp)) | |
91 cp++; | |
92 if (*cp != '\0' && *cp != '#') | |
93 goto inv; | |
94 } | |
95 | |
96 static void | |
97 process_line(linebuf, filename_for_errs, lineno) | |
98 char *linebuf, *filename_for_errs; | |
99 { | |
100 char *cp, *np; | |
101 | |
102 for (cp = linebuf; isspace(*cp); cp++) | |
103 ; | |
104 if (*cp == '\0' || *cp == '#') | |
105 return; | |
106 for (np = cp; *cp && !isspace(*cp); cp++) | |
107 ; | |
108 if (*cp) | |
109 *cp++ = '\0'; | |
110 if (!strcmp(np, "cable-loss")) | |
111 cable_loss_line(cp, filename_for_errs, lineno); | |
112 else if (!strcmp(np, "rf-port")) | |
113 rf_port_line(cp, filename_for_errs, lineno); | |
114 else { | |
115 fprintf(stderr, "%s line %d: unknown keyword \"%s\"\n", | |
116 filename_for_errs, lineno, np); | |
117 exit(1); | |
118 } | |
119 } | |
120 | |
121 read_cable_conf_file(cable_conf_name) | |
122 char *cable_conf_name; | |
123 { | |
124 char pathname[MAXPATHLEN]; | |
125 FILE *inf; | |
126 int lineno; | |
127 char linebuf[512]; | |
128 | |
129 strcpy(pathname, cableconf_directory); | |
130 strcat(pathname, cable_conf_name); | |
131 inf = fopen(pathname, "r"); | |
132 if (!inf) { | |
133 perror(pathname); | |
134 exit(1); | |
135 } | |
136 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) | |
137 process_line(linebuf, pathname, lineno); | |
138 fclose(inf); | |
139 } |