ao-telem: Add new program to convert telem data to ascii
[fw/altos] / ao-tools / ao-telem / ao-telem.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #define _GNU_SOURCE
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <getopt.h>
24 #include "cc.h"
25
26 static const struct option options[] = {
27         { 0, 0, 0, 0},
28 };
29
30 static void usage(char *program)
31 {
32         fprintf(stderr, "usage: %s\n"
33                 "\t{flight-log} ...\n", program);
34         exit(1);
35 }
36
37 int
38 main (int argc, char **argv)
39 {
40         char    line[80];
41         int c, i, ret;
42         char *s;
43         FILE *file;
44         int serial;
45         while ((c = getopt_long(argc, argv, "", options, NULL)) != -1) {
46                 switch (c) {
47                 default:
48                         usage(argv[0]);
49                         break;
50                 }
51         }
52         for (i = optind; i < argc; i++) {
53                 file = fopen(argv[i], "r");
54                 if (!file) {
55                         perror(argv[i]);
56                         ret++;
57                         continue;
58                 }
59                 s = strstr(argv[i], "-serial-");
60                 if (s)
61                         serial = atoi(s + 8);
62                 else
63                         serial = 0;
64                 while (fgets(line, sizeof (line), file)) {
65                         union ao_telemetry_all telem;
66                         char call[AO_MAX_CALLSIGN+1];
67                         char version[AO_MAX_VERSION+1];
68
69                         if (cc_telemetry_parse(line, &telem)) {
70                                 printf ("serial %5d tick %5d type %3d ",
71                                         telem.generic.serial, telem.generic.tick, telem.generic.type);
72                                 switch (telem.generic.type) {
73                                 case AO_TELEMETRY_SENSOR_TELEMETRUM:
74                                 case AO_TELEMETRY_SENSOR_TELEMINI:
75                                 case AO_TELEMETRY_SENSOR_TELENANO:
76                                         printf ("state %1d accel %5d pres %5d ",
77                                                 telem.sensor.state, telem.sensor.accel, telem.sensor.pres);
78                                         printf ("accel %5d speed %5d height %5d ",
79                                                 telem.sensor.acceleration,
80                                                 telem.sensor.speed,
81                                                 telem.sensor.height);
82                                         printf ("ground_pres %5d ground_accel %5d accel_plus %5d accel_minus %5d\n",
83                                                 telem.sensor.ground_pres,
84                                                 telem.sensor.ground_accel,
85                                                 telem.sensor.accel_plus_g,
86                                                 telem.sensor.accel_minus_g);
87                                         break;
88                                 case AO_TELEMETRY_CONFIGURATION:
89                                         memcpy(call, telem.configuration.callsign, AO_MAX_CALLSIGN);
90                                         memcpy(version, telem.configuration.version, AO_MAX_VERSION);
91                                         call[AO_MAX_CALLSIGN] = '\0';
92                                         version[AO_MAX_CALLSIGN] = '\0';
93                                         printf ("device %3d flight %5d config %3d.%03d delay %2d main %4d",
94                                                 telem.configuration.device,
95                                                 telem.configuration.flight,
96                                                 telem.configuration.config_major,
97                                                 telem.configuration.config_minor,
98                                                 telem.configuration.apogee_delay,
99                                                 telem.configuration.main_deploy,
100                                                 telem.configuration.flight_log_max);
101                                         printf (" call %8s version %8s\n", call, version);
102                                         break;
103                                 default:
104                                         printf("\n");
105                                 }
106                         }
107                 }
108                 fclose (file);
109
110         }
111         return ret;
112 }