ao-tools: add rudimentary support for MM telemetry to ao-telem
[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 #define bool(b) ((b) ? "true" : "false")
38
39 int
40 main (int argc, char **argv)
41 {
42         char    line[80];
43         int c, i, ret;
44         char *s;
45         FILE *file;
46         int serial;
47         while ((c = getopt_long(argc, argv, "", options, NULL)) != -1) {
48                 switch (c) {
49                 default:
50                         usage(argv[0]);
51                         break;
52                 }
53         }
54         for (i = optind; i < argc; i++) {
55                 file = fopen(argv[i], "r");
56                 if (!file) {
57                         perror(argv[i]);
58                         ret++;
59                         continue;
60                 }
61                 s = strstr(argv[i], "-serial-");
62                 if (s)
63                         serial = atoi(s + 8);
64                 else
65                         serial = 0;
66                 while (fgets(line, sizeof (line), file)) {
67                         union ao_telemetry_all telem;
68                         char call[AO_MAX_CALLSIGN+1];
69                         char version[AO_MAX_VERSION+1];
70
71                         if (cc_telemetry_parse(line, &telem)) {
72                                 int rssi = (int8_t) telem.generic.rssi / 2 - 74;
73
74                                 printf ("serial %5d rssi %d status %02x tick %5d type %3d ",
75                                         telem.generic.serial, rssi, telem.generic.status,
76                                         telem.generic.tick, telem.generic.type);
77                                 if ((telem.generic.status & (1 << 7)) == 0) {
78                                         printf ("CRC error\n");
79                                         continue;
80                                 }
81                                 switch (telem.generic.type) {
82                                 case AO_TELEMETRY_SENSOR_TELEMETRUM:
83                                 case AO_TELEMETRY_SENSOR_TELEMINI:
84                                 case AO_TELEMETRY_SENSOR_TELENANO:
85                                 case AO_TELEMETRY_SENSOR_MEGAMETRUM:
86                                         printf ("state %1d accel %5d pres %5d ",
87                                                 telem.sensor.state, telem.sensor.accel, telem.sensor.pres);
88                                         printf ("accel %6.2f speed %6.2f height %5d ",
89                                                 telem.sensor.acceleration / 16.0,
90                                                 telem.sensor.speed / 16.0,
91                                                 telem.sensor.height);
92                                         printf ("ground_pres %5d ground_accel %5d accel_plus %5d accel_minus %5d\n",
93                                                 telem.sensor.ground_pres,
94                                                 telem.sensor.ground_accel,
95                                                 telem.sensor.accel_plus_g,
96                                                 telem.sensor.accel_minus_g);
97                                         break;
98                                 case AO_TELEMETRY_CONFIGURATION:
99                                         memcpy(call, telem.configuration.callsign, AO_MAX_CALLSIGN);
100                                         memcpy(version, telem.configuration.version, AO_MAX_VERSION);
101                                         call[AO_MAX_CALLSIGN] = '\0';
102                                         version[AO_MAX_CALLSIGN] = '\0';
103                                         printf ("device %3d flight %5d config %3d.%03d delay %2d main %4d",
104                                                 telem.configuration.device,
105                                                 telem.configuration.flight,
106                                                 telem.configuration.config_major,
107                                                 telem.configuration.config_minor,
108                                                 telem.configuration.apogee_delay,
109                                                 telem.configuration.main_deploy,
110                                                 telem.configuration.flight_log_max);
111                                         printf (" call %8s version %8s\n", call, version);
112                                         break;
113                                 case AO_TELEMETRY_LOCATION:
114                                         printf ("sats %d flags %s%s%s%s",
115                                                 telem.location.flags & 0xf,
116                                                 (telem.location.flags & (1 << 4)) ? "valid" : "invalid",
117                                                 (telem.location.flags & (1 << 5)) ? ",running" : "",
118                                                 (telem.location.flags & (1 << 6)) ? ",date" : "",
119                                                 (telem.location.flags & (1 << 7)) ? ",course" : "");
120                                         printf (" alt %5d lat %12.7f lon %12.7f",
121                                                 telem.location.altitude,
122                                                 telem.location.latitude / 1e7,
123                                                 telem.location.longitude / 1e7);
124                                         if ((telem.location.flags & (1 << 6)) != 0) {
125                                                 printf (" year %2d month %2d day %2d",
126                                                         telem.location.year,
127                                                         telem.location.month,
128                                                         telem.location.day);
129                                                 printf (" hour %2d minute %2d second %2d",
130                                                         telem.location.hour,
131                                                         telem.location.minute,
132                                                         telem.location.second);
133                                         }
134                                         printf (" pdop %3.1f hdop %3.1f vdop %3.1f mode %d",
135                                                 telem.location.pdop / 5.0,
136                                                 telem.location.hdop / 5.0,
137                                                 telem.location.vdop / 5.0,
138                                                 telem.location.mode);
139                                         if ((telem.location.flags & (1 << 7)) != 0)
140                                                 printf (" ground_speed %6.2f climb_rate %6.2f course %d",
141                                                         telem.location.ground_speed / 100.0,
142                                                         telem.location.climb_rate / 100.0,
143                                                         telem.location.course * 2);
144                                         printf ("\n");
145                                         break;
146                                 case AO_TELEMETRY_SATELLITE:
147                                         printf ("sats %d", telem.satellite.channels);
148                                         for (c = 0; c < 12 && c < telem.satellite.channels; c++) {
149                                                 printf (" sat %d svid %d c_n_1 %d",
150                                                         c,
151                                                         telem.satellite.sats[c].svid,
152                                                         telem.satellite.sats[c].c_n_1);
153                                         }
154                                         printf ("\n");
155                                         break;
156                                 default:
157                                         printf("\n");
158                                 }
159                         }
160                 }
161                 fclose (file);
162
163         }
164         return ret;
165 }