summaryrefslogtreecommitdiff
path: root/aoview/aoview_monitor.c
blob: 3b3245e268866a77c1941bd26e242eecf75b68ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright © 2009 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include "aoview.h"

static struct aoview_serial *monitor_serial;

#define MONITOR_LEN	1024

static char	monitor_line[MONITOR_LEN + 1];
static int	monitor_pos;

void
aoview_monitor_disconnect(void)
{
	if (monitor_serial) {
		aoview_serial_close(monitor_serial);
		monitor_serial = NULL;
	}
	aoview_table_clear();
	aoview_log_new();
}

static void
aoview_parse_string(char *target, int len, char *source)
{
	strncpy(target, source, len-1);
	target[len-1] = '\0';
}

static void
aoview_parse_int(int *target, char *source)
{
	*target = strtol(source, NULL, 0);
}

static void
aoview_parse_pos(double *target, char *source)
{
	int	deg;
	double	min;
	char	dir;
	double	r;

	if (sscanf(source, "%d°%lf'%c", &deg, &min, &dir) != 3) {
		*target = 0;
		return;
	}
	r = deg + min / 60.0;
	if (dir == 'S' || dir == 'W')
		r = -r;
	*target = r;
}

static void
aoview_monitor_parse(char *line)
{
	char *saveptr;
	char *words[64];
	int nword;
	struct aostate	state;

	if (aoview_log_get_serial())
		aoview_log_printf ("%s\n", line);
	for (nword = 0; nword < 64; nword++) {
		words[nword] = strtok_r(line, " \t\n", &saveptr);
		line = NULL;
		if (words[nword] == NULL)
			break;
	}
	if (nword < 26)
		return;
	if (strcmp(words[0], "CALL") != 0)
		return;
	aoview_parse_string(state.callsign, sizeof (state.callsign), words[1]);
	aoview_parse_int(&state.serial, words[3]);
	if (!aoview_log_get_serial())
		aoview_log_set_serial(state.serial);

	aoview_parse_int(&state.rssi, words[5]);
	aoview_parse_string(state.state, sizeof (state.state), words[9]);
	aoview_parse_int(&state.tick, words[10]);
	aoview_parse_int(&state.accel, words[12]);
	aoview_parse_int(&state.pres, words[14]);
	aoview_parse_int(&state.temp, words[16]);
	aoview_parse_int(&state.batt, words[18]);
	aoview_parse_int(&state.drogue, words[20]);
	aoview_parse_int(&state.main, words[22]);
	aoview_parse_int(&state.nsat, words[24]);
	if (strcmp (words[26], "unlocked") != 0 && nword >= 29) {
		state.locked = 1;
		sscanf(words[26], "%d:%d:%d", &state.gps_time.hour, &state.gps_time.minute, &state.gps_time.second);
		aoview_parse_pos(&state.lat, words[27]);
		aoview_parse_pos(&state.lon, words[28]);
		sscanf(words[29], "%dm", &state.alt);
	} else {
		state.locked = 0;
		state.gps_time.hour = state.gps_time.minute = state.gps_time.second = 0;
		state.lat = state.lon = 0;
		state.alt = 0;
	}
	aoview_state_notify(&state);
}

static void
aoview_monitor_callback(gpointer user_data,
			struct aoview_serial *serial,
			gint revents)
{
	int	c;

	if (revents & (G_IO_HUP|G_IO_ERR)) {
		aoview_monitor_disconnect();
		return;
	}
	if (revents & G_IO_IN) {
		for (;;) {
			c = aoview_serial_getc(serial);
			if (c == -1)
				break;
			if (c == '\r')
				continue;
			if (c == '\n') {
				monitor_line[monitor_pos] = '\0';
				if (monitor_pos)
				aoview_monitor_parse(monitor_line);
				monitor_pos = 0;
			} else if (monitor_pos < MONITOR_LEN)
				monitor_line[monitor_pos++] = c;
		}
	}
}

gboolean
aoview_monitor_connect(char *tty)
{
	aoview_monitor_disconnect();
	monitor_serial = aoview_serial_open(tty);
	if (!monitor_serial)
		return FALSE;
	aoview_serial_set_callback(monitor_serial,
				   aoview_monitor_callback,
				   monitor_serial,
				   NULL);
	return TRUE;
}