FreeCalypso > hg > freecalypso-tools
comparison rvinterf/tmsh/rftablerd.c @ 121:4070847293a9
fc-tmsh: RF table reading code implemented
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 19 Feb 2017 08:32:37 +0000 |
parents | |
children | db7fc16bac1e |
comparison
equal
deleted
inserted
replaced
120:12dbb47ad3f7 | 121:4070847293a9 |
---|---|
1 /* | |
2 * Reading RF tables from formatted ASCII files for the rftw command | |
3 */ | |
4 | |
5 #include <sys/types.h> | |
6 #include <ctype.h> | |
7 #include <stdio.h> | |
8 #include <string.h> | |
9 #include <strings.h> | |
10 #include <stdlib.h> | |
11 #include "localtypes.h" | |
12 #include "l1tm.h" | |
13 #include "exitcodes.h" | |
14 | |
15 #define MAX_FIELDS_PER_LINE 64 | |
16 | |
17 static char *filename; | |
18 static FILE *rdfile; | |
19 static unsigned lineno; | |
20 static char linebuf[256], *line_fields[MAX_FIELDS_PER_LINE]; | |
21 static unsigned line_nfields, line_field_ptr; | |
22 static char *format; | |
23 static u_char *writeptr; | |
24 static unsigned written_size; | |
25 | |
26 static int | |
27 read_line() | |
28 { | |
29 char *cp; | |
30 | |
31 if (!fgets(linebuf, sizeof linebuf, rdfile)) | |
32 return(0); | |
33 lineno++; | |
34 cp = linebuf; | |
35 for (line_nfields = 0; ; ) { | |
36 while (isspace(*cp)) | |
37 cp++; | |
38 if (*cp == '\0' || *cp == '#') | |
39 break; | |
40 if (line_nfields >= MAX_FIELDS_PER_LINE) { | |
41 printf("%s line %d: too many fields on one line\n", | |
42 filename, lineno); | |
43 return(-1); | |
44 } | |
45 line_fields[line_nfields++] = cp; | |
46 while (*cp && !isspace(*cp)) | |
47 cp++; | |
48 if (*cp) | |
49 *cp++ = '\0'; | |
50 } | |
51 return(1); | |
52 } | |
53 | |
54 static | |
55 get_field(retp) | |
56 char **retp; | |
57 { | |
58 int rc; | |
59 | |
60 if (line_field_ptr < line_nfields) { | |
61 *retp = line_fields[line_field_ptr++]; | |
62 return(1); | |
63 } | |
64 do { | |
65 rc = read_line(); | |
66 if (rc <= 0) | |
67 return(rc); | |
68 } while (!line_nfields); | |
69 *retp = line_fields[0]; | |
70 line_field_ptr = 1; | |
71 return(1); | |
72 } | |
73 | |
74 static | |
75 process_number(size) | |
76 { | |
77 char *field; | |
78 int rc; | |
79 u_long number; | |
80 | |
81 rc = get_field(&field); | |
82 if (rc < 0) | |
83 return(ERROR_USAGE); | |
84 if (!rc) { | |
85 printf("error: %s is too short for table format %s\n", | |
86 filename, format); | |
87 return(ERROR_USAGE); | |
88 } | |
89 if (field[0] == '-') | |
90 number = strtol(field, 0, 0); | |
91 else | |
92 number = strtoul(field, 0, 0); | |
93 switch (size) { | |
94 case 1: | |
95 *writeptr++ = number; | |
96 break; | |
97 case 2: | |
98 *writeptr++ = number; | |
99 *writeptr++ = number >> 8; | |
100 break; | |
101 case 4: | |
102 *writeptr++ = number; | |
103 *writeptr++ = number >> 8; | |
104 *writeptr++ = number >> 16; | |
105 *writeptr++ = number >> 24; | |
106 break; | |
107 default: | |
108 abort(); | |
109 } | |
110 written_size += size; | |
111 return(0); | |
112 } | |
113 | |
114 static | |
115 ensure_eof() | |
116 { | |
117 char *field; | |
118 int rc; | |
119 | |
120 rc = get_field(&field); | |
121 if (rc < 0) | |
122 return(ERROR_USAGE); | |
123 if (rc) { | |
124 printf("error: %s is too long for table format %s\n", | |
125 filename, format); | |
126 return(ERROR_USAGE); | |
127 } | |
128 return(0); | |
129 } | |
130 | |
131 static | |
132 read_agc_table() | |
133 { | |
134 int i, rc; | |
135 | |
136 for (i = 0; i < 20; i++) { | |
137 rc = process_number(2); | |
138 if (rc) | |
139 return(rc); | |
140 } | |
141 return ensure_eof(); | |
142 } | |
143 | |
144 static | |
145 read_afcparams() | |
146 { | |
147 int i, rc; | |
148 | |
149 for (i = 0; i < 4; i++) { | |
150 rc = process_number(4); | |
151 if (rc) | |
152 return(rc); | |
153 } | |
154 for (i = 0; i < 4; i++) { | |
155 rc = process_number(2); | |
156 if (rc) | |
157 return(rc); | |
158 } | |
159 return ensure_eof(); | |
160 } | |
161 | |
162 static | |
163 read_agc_global_params() | |
164 { | |
165 int i, rc; | |
166 | |
167 for (i = 0; i < 4; i++) { | |
168 rc = process_number(2); | |
169 if (rc) | |
170 return(rc); | |
171 } | |
172 return ensure_eof(); | |
173 } | |
174 | |
175 static | |
176 read_il2agc() | |
177 { | |
178 int i, rc; | |
179 | |
180 for (i = 0; i < 121; i++) { | |
181 rc = process_number(1); | |
182 if (rc) | |
183 return(rc); | |
184 } | |
185 return ensure_eof(); | |
186 } | |
187 | |
188 static | |
189 read_tx_levels() | |
190 { | |
191 int i, rc; | |
192 | |
193 for (i = 0; i < 32; i++) { | |
194 rc = process_number(2); | |
195 if (rc) | |
196 return(rc); | |
197 rc = process_number(1); | |
198 if (rc) | |
199 return(rc); | |
200 rc = process_number(1); | |
201 if (rc) | |
202 return(rc); | |
203 } | |
204 return ensure_eof(); | |
205 } | |
206 | |
207 static | |
208 read_tx_calchan() | |
209 { | |
210 int i, rc; | |
211 | |
212 for (i = 0; i < 64; i++) { | |
213 rc = process_number(2); | |
214 if (rc) | |
215 return(rc); | |
216 } | |
217 return ensure_eof(); | |
218 } | |
219 | |
220 static | |
221 read_tx_caltemp() | |
222 { | |
223 int i, rc; | |
224 | |
225 for (i = 0; i < 20; i++) { | |
226 rc = process_number(2); | |
227 if (rc) | |
228 return(rc); | |
229 } | |
230 return ensure_eof(); | |
231 } | |
232 | |
233 static | |
234 read_rx_calchan() | |
235 { | |
236 int i, rc; | |
237 | |
238 for (i = 0; i < 20; i++) { | |
239 rc = process_number(2); | |
240 if (rc) | |
241 return(rc); | |
242 } | |
243 return ensure_eof(); | |
244 } | |
245 | |
246 static | |
247 read_rx_caltemp() | |
248 { | |
249 int i, rc; | |
250 | |
251 for (i = 0; i < 22; i++) { | |
252 rc = process_number(2); | |
253 if (rc) | |
254 return(rc); | |
255 } | |
256 return ensure_eof(); | |
257 } | |
258 | |
259 static | |
260 read_rx_agc_params() | |
261 { | |
262 int i, rc; | |
263 | |
264 for (i = 0; i < 4; i++) { | |
265 rc = process_number(2); | |
266 if (rc) | |
267 return(rc); | |
268 } | |
269 return ensure_eof(); | |
270 } | |
271 | |
272 static | |
273 read_raw_table() | |
274 { | |
275 int rc; | |
276 char *field; | |
277 u_long number; | |
278 | |
279 for (;;) { | |
280 rc = get_field(&field); | |
281 if (rc < 0) | |
282 return(ERROR_USAGE); | |
283 if (!rc) | |
284 break; | |
285 number = strtoul(field, 0, 16); | |
286 if (written_size >= MAX_RF_TABLE_SIZE) { | |
287 printf("error: raw table %s exceeds maximum size\n", | |
288 filename); | |
289 return(ERROR_USAGE); | |
290 } | |
291 *writeptr++ = number; | |
292 written_size++; | |
293 } | |
294 if (!written_size) { | |
295 printf("error: raw table %s is empty\n", filename); | |
296 return(ERROR_USAGE); | |
297 } | |
298 return(0); | |
299 } | |
300 | |
301 static struct table_format { | |
302 char *kw; | |
303 int (*handler)(); | |
304 } table_formats[] = { | |
305 {"agc-table", read_agc_table}, | |
306 {"afcparams", read_afcparams}, | |
307 {"agc-global-params", read_agc_global_params}, | |
308 {"il2agc", read_il2agc}, | |
309 {"tx-levels", read_tx_levels}, | |
310 {"tx-calchan", read_tx_calchan}, | |
311 {"tx-caltemp", read_tx_caltemp}, | |
312 {"rx-calchan", read_rx_calchan}, | |
313 {"rx-caltemp", read_rx_caltemp}, | |
314 {"rx-agc-params", read_rx_agc_params}, | |
315 {"raw", read_raw_table}, | |
316 {0, 0} | |
317 }; | |
318 | |
319 read_rf_table(filename_arg, rdbuf, format_ret, size_ret) | |
320 char *filename_arg; | |
321 u_char *rdbuf; | |
322 char **format_ret; | |
323 unsigned *size_ret; | |
324 { | |
325 char *field; | |
326 int rc; | |
327 struct table_format *tp; | |
328 | |
329 filename = filename_arg; | |
330 rdfile = fopen(filename, "r"); | |
331 if (!rdfile) { | |
332 perror(filename); | |
333 return(ERROR_UNIX); | |
334 } | |
335 lineno = 0; | |
336 line_nfields = 0; | |
337 rc = get_field(&field); | |
338 if (rc <= 0) { | |
339 not_valid_rftable: | |
340 printf("error: %s is not a valid RF table file\n", filename); | |
341 fclose(rdfile); | |
342 return(ERROR_USAGE); | |
343 } | |
344 if (strcmp(field, "rf_table")) | |
345 goto not_valid_rftable; | |
346 rc = get_field(&field); | |
347 if (rc <= 0) | |
348 goto not_valid_rftable; | |
349 for (tp = table_formats; tp->kw; tp++) | |
350 if (!strcmp(tp->kw, field)) | |
351 break; | |
352 if (!tp->kw) { | |
353 printf("error: table format \"%s\" not recognized\n", field); | |
354 fclose(rdfile); | |
355 return(ERROR_USAGE); | |
356 } | |
357 format = tp->kw; | |
358 if (format_ret) | |
359 *format_ret = format; | |
360 writeptr = rdbuf; | |
361 written_size = 0; | |
362 rc = tp->handler(); | |
363 fclose(rdfile); | |
364 if (size_ret) | |
365 *size_ret = written_size; | |
366 return(rc); | |
367 } |