6207d4331d3efc355476ecafffe07e22f8b0af16
[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                                         printf ("state %1d accel %5d pres %5d ",
86                                                 telem.sensor.state, telem.sensor.accel, telem.sensor.pres);
87                                         printf ("accel %6.2f speed %6.2f height %5d ",
88                                                 telem.sensor.acceleration / 16.0,
89                                                 telem.sensor.speed / 16.0,
90                                                 telem.sensor.height);
91                                         printf ("ground_pres %5d ground_accel %5d accel_plus %5d accel_minus %5d\n",
92                                                 telem.sensor.ground_pres,
93                                                 telem.sensor.ground_accel,
94                                                 telem.sensor.accel_plus_g,
95                                                 telem.sensor.accel_minus_g);
96                                         break;
97                                 case AO_TELEMETRY_CONFIGURATION:
98                                         memcpy(call, telem.configuration.callsign, AO_MAX_CALLSIGN);
99                                         memcpy(version, telem.configuration.version, AO_MAX_VERSION);
100                                         call[AO_MAX_CALLSIGN] = '\0';
101                                         version[AO_MAX_CALLSIGN] = '\0';
102                                         printf ("device %3d flight %5d config %3d.%03d delay %2d main %4d",
103                                                 telem.configuration.device,
104                                                 telem.configuration.flight,
105                                                 telem.configuration.config_major,
106                                                 telem.configuration.config_minor,
107                                                 telem.configuration.apogee_delay,
108                                                 telem.configuration.main_deploy,
109                                                 telem.configuration.flight_log_max);
110                                         printf (" call %8s version %8s\n", call, version);
111                                         break;
112                                 case AO_TELEMETRY_LOCATION:
113                                         printf ("sats %d flags %s%s%s%s",
114                                                 telem.location.flags & 0xf,
115                                                 (telem.location.flags & (1 << 4)) ? "valid" : "invalid",
116                                                 (telem.location.flags & (1 << 5)) ? ",running" : "",
117                                                 (telem.location.flags & (1 << 6)) ? ",date" : "",
118                                                 (telem.location.flags & (1 << 7)) ? ",course" : "");
119                                         printf (" alt %5d lat %12.7f lon %12.7f",
120                                                 telem.location.altitude,
121                                                 telem.location.latitude / 1e7,
122                                                 telem.location.longitude / 1e7);
123                                         if ((telem.location.flags & (1 << 6)) != 0) {
124                                                 printf (" year %2d month %2d day %2d",
125                                                         telem.location.year,
126                                                         telem.location.month,
127                                                         telem.location.day);
128                                                 printf (" hour %2d minute %2d second %2d",
129                                                         telem.location.hour,
130                                                         telem.location.minute,
131                                                         telem.location.second);
132                                         }
133                                         printf (" pdop %3.1f hdop %3.1f vdop %3.1f mode %d",
134                                                 telem.location.pdop / 5.0,
135                                                 telem.location.hdop / 5.0,
136                                                 telem.location.vdop / 5.0,
137                                                 telem.location.mode);
138                                         if ((telem.location.flags & (1 << 7)) != 0)
139                                                 printf (" ground_speed %6.2f climb_rate %6.2f course %d",
140                                                         telem.location.ground_speed / 100.0,
141                                                         telem.location.climb_rate / 100.0,
142                                                         telem.location.course * 2);
143                                         printf ("\n");
144                                         break;
145                                 case AO_TELEMETRY_SATELLITE:
146                                         printf ("sats %d", telem.satellite.channels);
147                                         for (c = 0; c < 12 && c < telem.satellite.channels; c++) {
148                                                 printf (" sat %d svid %d c_n_1 %d",
149                                                         c,
150                                                         telem.satellite.sats[c].svid,
151                                                         telem.satellite.sats[c].c_n_1);
152                                         }
153                                         printf ("\n");
154                                         break;
155                                 default:
156                                         printf("\n");
157                                 }
158                         }
159                 }
160                 fclose (file);
161
162         }
163         return ret;
164 }