2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
23 cc_parse_string(char *target, int len, char *source)
25 strncpy(target, source, len-1);
30 cc_parse_int(int *target, char *source)
32 *target = strtol(source, NULL, 0);
36 cc_parse_hex(int *target, char *source)
38 *target = strtol(source, NULL, 16);
42 cc_parse_pos(double *target, char *source)
49 if (sscanf(source, "%d°%lf'%c", °, &min, &dir) != 3) {
54 if (dir == 'S' || dir == 'W')
59 #define PARSE_MAX_WORDS 512
62 cc_telem_parse(const char *input_line, struct cc_telem *telem)
65 char *words[PARSE_MAX_WORDS];
67 char line_buf[8192], *line;
70 /* avoid smashing our input parameter */
71 strncpy (line_buf, input_line, sizeof (line_buf)-1);
72 line_buf[sizeof(line_buf) - 1] = '\0';
74 for (nword = 0; nword < PARSE_MAX_WORDS; nword++) {
75 words[nword] = strtok_r(line, " \t\n", &saveptr);
77 if (words[nword] == NULL)
82 if (strcmp(words[0], "CALL") != 0)
84 cc_parse_string(telem->callsign, sizeof (telem->callsign), words[1]);
85 cc_parse_int(&telem->serial, words[3]);
87 cc_parse_int(&telem->rssi, words[5]);
88 cc_parse_string(telem->state, sizeof (telem->state), words[9]);
89 cc_parse_int(&telem->tick, words[10]);
90 cc_parse_int(&telem->accel, words[12]);
91 cc_parse_int(&telem->pres, words[14]);
92 cc_parse_int(&telem->temp, words[16]);
93 cc_parse_int(&telem->batt, words[18]);
94 cc_parse_int(&telem->drogue, words[20]);
95 cc_parse_int(&telem->main, words[22]);
96 cc_parse_int(&telem->flight_accel, words[24]);
97 cc_parse_int(&telem->ground_accel, words[26]);
98 cc_parse_int(&telem->flight_vel, words[28]);
99 cc_parse_int(&telem->flight_pres, words[30]);
100 cc_parse_int(&telem->ground_pres, words[32]);
101 cc_parse_int(&telem->gps.nsat, words[34]);
102 if (strcmp (words[36], "unlocked") == 0) {
103 telem->gps.gps_connected = 1;
104 telem->gps.gps_locked = 0;
105 telem->gps.gps_time.hour = telem->gps.gps_time.minute = telem->gps.gps_time.second = 0;
106 telem->gps.lat = telem->gps.lon = 0;
109 } else if (nword >= 40) {
110 telem->gps.gps_locked = 1;
111 telem->gps.gps_connected = 1;
112 sscanf(words[36], "%d:%d:%d", &telem->gps.gps_time.hour, &telem->gps.gps_time.minute, &telem->gps.gps_time.second);
113 cc_parse_pos(&telem->gps.lat, words[37]);
114 cc_parse_pos(&telem->gps.lon, words[38]);
115 sscanf(words[39], "%dm", &telem->gps.alt);
118 telem->gps.gps_connected = 0;
119 telem->gps.gps_locked = 0;
120 telem->gps.gps_time.hour = telem->gps.gps_time.minute = telem->gps.gps_time.second = 0;
121 telem->gps.lat = telem->gps.lon = 0;
126 telem->gps.gps_extended = 1;
127 sscanf(words[40], "%lfm/s", &telem->gps.ground_speed);
128 sscanf(words[41], "%d", &telem->gps.course);
129 sscanf(words[42], "%lfm/s", &telem->gps.climb_rate);
130 sscanf(words[43], "%lf", &telem->gps.hdop);
131 sscanf(words[44], "%d", &telem->gps.h_error);
132 sscanf(words[45], "%d", &telem->gps.v_error);
134 telem->gps.gps_extended = 0;
135 telem->gps.ground_speed = 0;
136 telem->gps.course = 0;
137 telem->gps.climb_rate = 0;
139 telem->gps.h_error = 0;
140 telem->gps.v_error = 0;
142 if (tracking_pos >= 0 && nword >= tracking_pos + 2 && strcmp(words[tracking_pos], "SAT") == 0) {
144 cc_parse_int(&n, words[tracking_pos + 1]);
145 pos = tracking_pos + 2;
146 if (nword >= pos + n * 3) {
147 telem->gps_tracking.channels = n;
148 for (c = 0; c < n; c++) {
149 cc_parse_int(&telem->gps_tracking.sats[c].svid,
151 cc_parse_hex(&telem->gps_tracking.sats[c].state,
153 cc_parse_int(&telem->gps_tracking.sats[c].c_n0,
158 telem->gps_tracking.channels = 0;
161 telem->gps_tracking.channels = 0;