altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / ao-tools / lib / cc-telem.c
1 /*
2  * Copyright © 2009 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "cc.h"
20 #include <string.h>
21 #include <stdlib.h>
22
23 static void
24 cc_parse_string(char *target, int len, char *source)
25 {
26         strncpy(target, source, len-1);
27         target[len-1] = '\0';
28 }
29
30 static void
31 cc_parse_int(int *target, char *source)
32 {
33         *target = strtol(source, NULL, 0);
34 }
35
36 static void
37 cc_parse_hex(int *target, char *source)
38 {
39         *target = strtol(source, NULL, 16);
40 }
41
42 static void
43 cc_parse_pos(double *target, char *source)
44 {
45         int     deg;
46         double  min;
47         char    dir;
48         double  r;
49
50         if (sscanf(source, "%d°%lf'%c", &deg, &min, &dir) != 3) {
51                 *target = 0;
52                 return;
53         }
54         r = deg + min / 60.0;
55         if (dir == 'S' || dir == 'W')
56                 r = -r;
57         *target = r;
58 }
59
60 #define PARSE_MAX_WORDS 512
61
62 int
63 cc_telem_parse(const char *input_line, struct cc_telem *telem)
64 {
65         char *saveptr;
66         char *raw_words[PARSE_MAX_WORDS];
67         char **words;
68         int version = 0;
69         int nword;
70         char line_buf[8192], *line;
71         int     tracking_pos;
72
73         /* avoid smashing our input parameter */
74         strncpy (line_buf, input_line, sizeof (line_buf)-1);
75         line_buf[sizeof(line_buf) - 1] = '\0';
76         line = line_buf;
77         for (nword = 0; nword < PARSE_MAX_WORDS; nword++) {
78                 raw_words[nword] = strtok_r(line, " \t\n", &saveptr);
79                 line = NULL;
80                 if (raw_words[nword] == NULL)
81                         break;
82         }
83         if (nword < 36)
84                 return FALSE;
85         words = raw_words;
86         if (strcmp(words[0], "VERSION") == 0) {
87                 cc_parse_int(&version, words[1]);
88                 words += 2;
89                 nword -= 2;
90         }
91
92         if (strcmp(words[0], "CALL") != 0)
93                 return FALSE;
94         cc_parse_string(telem->callsign, sizeof (telem->callsign), words[1]);
95         cc_parse_int(&telem->serial, words[3]);
96
97         if (version >= 2) {
98                 cc_parse_int(&telem->flight, words[5]);
99                 words += 2;
100                 nword -= 2;
101         } else
102                 telem->flight = 0;
103
104         cc_parse_int(&telem->rssi, words[5]);
105         if (version <= 2) {
106                 /* Older telemetry versions mis-computed the rssi value */
107                 telem->rssi = (telem->rssi + 74) / 2 - 74;
108         }
109         cc_parse_string(telem->state, sizeof (telem->state), words[9]);
110         cc_parse_int(&telem->tick, words[10]);
111         cc_parse_int(&telem->accel, words[12]);
112         cc_parse_int(&telem->pres, words[14]);
113         cc_parse_int(&telem->temp, words[16]);
114         cc_parse_int(&telem->batt, words[18]);
115         cc_parse_int(&telem->drogue, words[20]);
116         cc_parse_int(&telem->main, words[22]);
117         cc_parse_int(&telem->flight_accel, words[24]);
118         cc_parse_int(&telem->ground_accel, words[26]);
119         cc_parse_int(&telem->flight_vel, words[28]);
120         cc_parse_int(&telem->flight_pres, words[30]);
121         cc_parse_int(&telem->ground_pres, words[32]);
122         if (version >= 1) {
123                 cc_parse_int(&telem->accel_plus_g, words[34]);
124                 cc_parse_int(&telem->accel_minus_g, words[36]);
125                 words += 4;
126                 nword -= 4;
127         } else {
128                 telem->accel_plus_g = telem->ground_accel;
129                 telem->accel_minus_g = telem->ground_accel + 530;
130         }
131         cc_parse_int(&telem->gps.nsat, words[34]);
132         if (strcmp (words[36], "unlocked") == 0) {
133                 telem->gps.gps_connected = 1;
134                 telem->gps.gps_locked = 0;
135                 telem->gps.gps_time.year = telem->gps.gps_time.month = telem->gps.gps_time.day = 0;
136                 telem->gps.gps_time.hour = telem->gps.gps_time.minute = telem->gps.gps_time.second = 0;
137                 telem->gps.lat = telem->gps.lon = 0;
138                 telem->gps.alt = 0;
139                 tracking_pos = 37;
140         } else if (nword >= 40) {
141                 telem->gps.gps_locked = 1;
142                 telem->gps.gps_connected = 1;
143                 if (version >= 2) {
144                         sscanf(words[36], "%d-%d-%d",
145                                &telem->gps.gps_time.year,
146                                &telem->gps.gps_time.month,
147                                &telem->gps.gps_time.day);
148                         words += 1;
149                         nword -= 1;
150                 } else {
151                         telem->gps.gps_time.year = telem->gps.gps_time.month = telem->gps.gps_time.day = 0;
152                 }
153                 sscanf(words[36], "%d:%d:%d", &telem->gps.gps_time.hour, &telem->gps.gps_time.minute, &telem->gps.gps_time.second);
154                 cc_parse_pos(&telem->gps.lat, words[37]);
155                 cc_parse_pos(&telem->gps.lon, words[38]);
156                 sscanf(words[39], "%dm", &telem->gps.alt);
157                 tracking_pos = 46;
158         } else {
159                 telem->gps.gps_connected = 0;
160                 telem->gps.gps_locked = 0;
161                 telem->gps.gps_time.year = telem->gps.gps_time.month = telem->gps.gps_time.day = 0;
162                 telem->gps.gps_time.hour = telem->gps.gps_time.minute = telem->gps.gps_time.second = 0;
163                 telem->gps.lat = telem->gps.lon = 0;
164                 telem->gps.alt = 0;
165                 tracking_pos = -1;
166         }
167         if (nword >= 46) {
168                 telem->gps.gps_extended = 1;
169                 sscanf(words[40], "%lfm/s", &telem->gps.ground_speed);
170                 sscanf(words[41], "%d", &telem->gps.course);
171                 sscanf(words[42], "%lfm/s", &telem->gps.climb_rate);
172                 sscanf(words[43], "%lf", &telem->gps.hdop);
173                 sscanf(words[44], "%d", &telem->gps.h_error);
174                 sscanf(words[45], "%d", &telem->gps.v_error);
175         } else {
176                 telem->gps.gps_extended = 0;
177                 telem->gps.ground_speed = 0;
178                 telem->gps.course = 0;
179                 telem->gps.climb_rate = 0;
180                 telem->gps.hdop = 0;
181                 telem->gps.h_error = 0;
182                 telem->gps.v_error = 0;
183         }
184         if (tracking_pos >= 0 && nword >= tracking_pos + 2 && strcmp(words[tracking_pos], "SAT") == 0) {
185                 int     c, n, pos;
186                 int     per_sat;
187                 int     state;
188
189                 if (version >= 2)
190                         per_sat = 2;
191                 else
192                         per_sat = 3;
193                 cc_parse_int(&n, words[tracking_pos + 1]);
194                 pos = tracking_pos + 2;
195                 if (nword >= pos + n * per_sat) {
196                         telem->gps_tracking.channels = n;
197                         for (c = 0; c < n; c++) {
198                                 cc_parse_int(&telem->gps_tracking.sats[c].svid,
199                                                  words[pos + 0]);
200                                 if (version < 2)
201                                         cc_parse_hex(&state, words[pos + 1]);
202                                 cc_parse_int(&telem->gps_tracking.sats[c].c_n0,
203                                                  words[pos + per_sat - 1]);
204                                 pos += per_sat;
205                         }
206                 } else {
207                         telem->gps_tracking.channels = 0;
208                 }
209         } else {
210                 telem->gps_tracking.channels = 0;
211         }
212         return TRUE;
213 }