ccd40ac2cb9db31e15501c57ab8bcfc6bbfaa287
[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; 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 #include "cc.h"
19 #include <string.h>
20 #include <stdlib.h>
21
22 static void
23 cc_parse_string(char *target, int len, char *source)
24 {
25         strncpy(target, source, len-1);
26         target[len-1] = '\0';
27 }
28
29 static void
30 cc_parse_int(int *target, char *source)
31 {
32         *target = strtol(source, NULL, 0);
33 }
34
35 static void
36 cc_parse_hex(int *target, char *source)
37 {
38         *target = strtol(source, NULL, 16);
39 }
40
41 static void
42 cc_parse_pos(double *target, char *source)
43 {
44         int     deg;
45         double  min;
46         char    dir;
47         double  r;
48
49         if (sscanf(source, "%d°%lf'%c", &deg, &min, &dir) != 3) {
50                 *target = 0;
51                 return;
52         }
53         r = deg + min / 60.0;
54         if (dir == 'S' || dir == 'W')
55                 r = -r;
56         *target = r;
57 }
58
59 #define PARSE_MAX_WORDS 512
60
61 int
62 cc_telem_parse(const char *input_line, struct cc_telem *telem)
63 {
64         char *saveptr;
65         char *raw_words[PARSE_MAX_WORDS];
66         char **words;
67         int version = 0;
68         int nword;
69         char line_buf[8192], *line;
70         int     tracking_pos;
71
72         /* avoid smashing our input parameter */
73         strncpy (line_buf, input_line, sizeof (line_buf)-1);
74         line_buf[sizeof(line_buf) - 1] = '\0';
75         line = line_buf;
76         for (nword = 0; nword < PARSE_MAX_WORDS; nword++) {
77                 raw_words[nword] = strtok_r(line, " \t\n", &saveptr);
78                 line = NULL;
79                 if (raw_words[nword] == NULL)
80                         break;
81         }
82         if (nword < 36)
83                 return FALSE;
84         words = raw_words;
85         if (strcmp(words[0], "VERSION") == 0) {
86                 cc_parse_int(&version, words[1]);
87                 words += 2;
88                 nword -= 2;
89         }
90
91         if (strcmp(words[0], "CALL") != 0)
92                 return FALSE;
93         cc_parse_string(telem->callsign, sizeof (telem->callsign), words[1]);
94         cc_parse_int(&telem->serial, words[3]);
95
96         if (version >= 2) {
97                 cc_parse_int(&telem->flight, words[5]);
98                 words += 2;
99                 nword -= 2;
100         } else
101                 telem->flight = 0;
102
103         cc_parse_int(&telem->rssi, words[5]);
104         cc_parse_string(telem->state, sizeof (telem->state), words[9]);
105         cc_parse_int(&telem->tick, words[10]);
106         cc_parse_int(&telem->accel, words[12]);
107         cc_parse_int(&telem->pres, words[14]);
108         cc_parse_int(&telem->temp, words[16]);
109         cc_parse_int(&telem->batt, words[18]);
110         cc_parse_int(&telem->drogue, words[20]);
111         cc_parse_int(&telem->main, words[22]);
112         cc_parse_int(&telem->flight_accel, words[24]);
113         cc_parse_int(&telem->ground_accel, words[26]);
114         cc_parse_int(&telem->flight_vel, words[28]);
115         cc_parse_int(&telem->flight_pres, words[30]);
116         cc_parse_int(&telem->ground_pres, words[32]);
117         if (version >= 1) {
118                 cc_parse_int(&telem->accel_plus_g, words[34]);
119                 cc_parse_int(&telem->accel_minus_g, words[36]);
120                 words += 4;
121                 nword -= 4;
122         } else {
123                 telem->accel_plus_g = telem->ground_accel;
124                 telem->accel_minus_g = telem->ground_accel + 530;
125         }
126         cc_parse_int(&telem->gps.nsat, words[34]);
127         if (strcmp (words[36], "unlocked") == 0) {
128                 telem->gps.gps_connected = 1;
129                 telem->gps.gps_locked = 0;
130                 telem->gps.gps_time.year = telem->gps.gps_time.month = telem->gps.gps_time.day = 0;
131                 telem->gps.gps_time.hour = telem->gps.gps_time.minute = telem->gps.gps_time.second = 0;
132                 telem->gps.lat = telem->gps.lon = 0;
133                 telem->gps.alt = 0;
134                 tracking_pos = 37;
135         } else if (nword >= 40) {
136                 telem->gps.gps_locked = 1;
137                 telem->gps.gps_connected = 1;
138                 if (version >= 2) {
139                         sscanf(words[36], "%d-%d-%d",
140                                &telem->gps.gps_time.year,
141                                &telem->gps.gps_time.month,
142                                &telem->gps.gps_time.day);
143                         words += 1;
144                         nword -= 1;
145                 } else {
146                         telem->gps.gps_time.year = telem->gps.gps_time.month = telem->gps.gps_time.day = 0;
147                 }
148                 sscanf(words[36], "%d:%d:%d", &telem->gps.gps_time.hour, &telem->gps.gps_time.minute, &telem->gps.gps_time.second);
149                 cc_parse_pos(&telem->gps.lat, words[37]);
150                 cc_parse_pos(&telem->gps.lon, words[38]);
151                 sscanf(words[39], "%dm", &telem->gps.alt);
152                 tracking_pos = 46;
153         } else {
154                 telem->gps.gps_connected = 0;
155                 telem->gps.gps_locked = 0;
156                 telem->gps.gps_time.year = telem->gps.gps_time.month = telem->gps.gps_time.day = 0;
157                 telem->gps.gps_time.hour = telem->gps.gps_time.minute = telem->gps.gps_time.second = 0;
158                 telem->gps.lat = telem->gps.lon = 0;
159                 telem->gps.alt = 0;
160                 tracking_pos = -1;
161         }
162         if (nword >= 46) {
163                 telem->gps.gps_extended = 1;
164                 sscanf(words[40], "%lfm/s", &telem->gps.ground_speed);
165                 sscanf(words[41], "%d", &telem->gps.course);
166                 sscanf(words[42], "%lfm/s", &telem->gps.climb_rate);
167                 sscanf(words[43], "%lf", &telem->gps.hdop);
168                 sscanf(words[44], "%d", &telem->gps.h_error);
169                 sscanf(words[45], "%d", &telem->gps.v_error);
170         } else {
171                 telem->gps.gps_extended = 0;
172                 telem->gps.ground_speed = 0;
173                 telem->gps.course = 0;
174                 telem->gps.climb_rate = 0;
175                 telem->gps.hdop = 0;
176                 telem->gps.h_error = 0;
177                 telem->gps.v_error = 0;
178         }
179         if (tracking_pos >= 0 && nword >= tracking_pos + 2 && strcmp(words[tracking_pos], "SAT") == 0) {
180                 int     c, n, pos;
181                 int     per_sat;
182                 int     state;
183
184                 if (version >= 2)
185                         per_sat = 2;
186                 else
187                         per_sat = 3;
188                 cc_parse_int(&n, words[tracking_pos + 1]);
189                 pos = tracking_pos + 2;
190                 if (nword >= pos + n * per_sat) {
191                         telem->gps_tracking.channels = n;
192                         for (c = 0; c < n; c++) {
193                                 cc_parse_int(&telem->gps_tracking.sats[c].svid,
194                                                  words[pos + 0]);
195                                 if (version < 2)
196                                         cc_parse_hex(&state, words[pos + 1]);
197                                 cc_parse_int(&telem->gps_tracking.sats[c].c_n0,
198                                                  words[pos + per_sat - 1]);
199                                 pos += per_sat;
200                         }
201                 } else {
202                         telem->gps_tracking.channels = 0;
203                 }
204         } else {
205                 telem->gps_tracking.channels = 0;
206         }
207         return TRUE;
208 }