summaryrefslogtreecommitdiff
path: root/libgpsd_core.c
blob: f64a2675ec9dbc722a5803641a4057d86339ea9e (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * This provides the interface to the library that supports direct access to
 * GPSes on serial or USB devices.
 */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/ioctl.h>

#include "config.h"
#include "gps.h"
#include "gpsd.h"

static struct gps_type_t *set_device_type(char what)
/* select a device driver by key letter */
{
    struct gps_type_t **dp;
    for (dp = gpsd_drivers; dp < gpsd_drivers + sizeof(gpsd_drivers)/sizeof(gpsd_drivers[0]); dp++)
	if ((*dp)->typekey == what) {
	    gpscli_report(3, "Selecting %s driver...\n", (*dp)->typename);
	    goto foundit;
	}
    return NULL;
 foundit:;
    return *dp;
}

struct gps_session_t *gpsd_init(char devicetype, char *dgpsserver)
/* initialize GPS polling */
{
    time_t now = time(NULL);
    struct gps_type_t *devtype;

    struct gps_session_t *session = (struct gps_session_t *)calloc(sizeof(struct gps_session_t), 1);

    if (!session)
	return NULL;

    session->gpsd_device = "/dev/gps";
    session->device_type = &nmea;
    devtype = set_device_type(devicetype);
    if (!devtype)
	gpscli_report(1, "invalid GPS type \"%s\", using NMEA instead\n", optarg);
    else
    {
	session->device_type = devtype;
	session->baudrate = devtype->baudrate;
    }

    session->dsock = -1;
    if (dgpsserver) {
	char hn[256], buf[BUFSIZE];
	char *colon, *dgpsport = "rtcm-sc104";

	if ((colon = strchr(optarg, ':'))) {
	    dgpsport = colon+1;
	    *colon = '\0';
	}

	if (!getservbyname(dgpsport, "tcp"))
	    dgpsport = "2101";

	session->dsock = netlib_connectsock(dgpsserver, dgpsport, "tcp");
	if (session->dsock < 0)
	    gpscli_report(1, "Can't connect to dgps server, netlib error %d\n", session->dsock);
	else
	{
	    gethostname(hn, sizeof(hn));

	    sprintf(buf, "HELO %s gpsd %s\r\nR\r\n", hn, VERSION);
	    write(session->dsock, buf, strlen(buf));
	}
    }

    /* mark fds closed */
    session->fdin = -1;
    session->fdout = -1;

    INIT(session->gNMEAdata.online_stamp, now);
    INIT(session->gNMEAdata.latlon_stamp, now);
    INIT(session->gNMEAdata.altitude_stamp, now);
    INIT(session->gNMEAdata.track_stamp, now);
    INIT(session->gNMEAdata.speed_stamp, now);
    INIT(session->gNMEAdata.status_stamp, now);
    INIT(session->gNMEAdata.mode_stamp, now);
    INIT(session->gNMEAdata.fix_quality_stamp, now);
    INIT(session->gNMEAdata.satellite_stamp, now);
#ifdef PROCESS_PRWIZCH
    INIT(session->gNMEAdata.signal_quality_stamp, now);
#endif /* PROCESS_PRWIZCH */
    session->gNMEAdata.mode = MODE_NO_FIX;

    return session;
}

void gpsd_deactivate(struct gps_session_t *session)
/* temporarily release the GPS device */
{
    session->gNMEAdata.online = 0;
    REFRESH(session->gNMEAdata.online_stamp);
    session->gNMEAdata.mode = MODE_NO_FIX;
    session->gNMEAdata.status = STATUS_NO_FIX;
    session->fdin = -1;
    session->fdout = -1;
    gpsd_close();
    if (session->device_type->wrapup)
	session->device_type->wrapup(session);
    gpscli_report(1, "closed GPS\n");
}

int gpsd_activate(struct gps_session_t *session)
/* acquire a connection to the GPS device */
{
    int input;

    if ((input = gpsd_open(session->gpsd_device, session->baudrate)) < 0)
	return -1;
    else
    {
	session->gNMEAdata.online = 1;
	REFRESH(session->gNMEAdata.online_stamp);
	session->fdin = input;
	session->fdout = input;
	gpscli_report(1, "gpsd_activate: opened GPS (%d)\n", input);

	/* if there is an initializer and no trigger string, invoke it */
	if (session->device_type->initializer && !session->device_type->trigger)
	    session->device_type->initializer(session);
	return input;
    }
}

static int is_input_waiting(int fd)
{
    int	count;
    if (fd < 0 || ioctl(fd, FIONREAD, &count) < 0)
	return -1;
    return count;
}

int gpsd_poll(struct gps_session_t *session)
/* update the stuff in the scoreboard structure */
{
    int waiting;

    /* accept a DGPS correction if one is pending */
    if (is_input_waiting(session->dsock) > 0)
    {
	char buf[BUFSIZE];
	int rtcmbytes;

	if ((rtcmbytes=read(session->dsock,buf,BUFSIZE))>0 && (session->fdout!=-1))
	{
	    if (session->device_type->rtcm_writer(session, buf, rtcmbytes) <= 0)
		gpscli_report(1, "Write to rtcm sink failed\n");
	    else
		gpscli_report(2, "<= DGPS: %d bytes of RTCM relayed.\n", rtcmbytes);
	}
	else 
	{
	    gpscli_report(1, "Read from rtcm source failed\n");
	}
    }

    /* update the scoreboard structure from the GPS */
    waiting = is_input_waiting(session->fdin);
    gpscli_report(4, "GPS has %d chars waiting\n", waiting);
    if (waiting < 0)
	return waiting;
    else if (!waiting) {
	if (time(NULL) <= session->gNMEAdata.online_stamp.last_refresh + session->device_type->interval+1) {
	    return 0;
	} else {
	    session->gNMEAdata.online = 0;
	    REFRESH(session->gNMEAdata.online_stamp);
	    return -1;
	}
    } else {
	session->gNMEAdata.online = 1;
	REFRESH(session->gNMEAdata.online_stamp);

	/* call the input routine from the device-specific driver */
	session->device_type->handle_input(session);

	/* count the good fixes */
	if (session->gNMEAdata.status > STATUS_NO_FIX) 
	    session->fixcnt++;

	/* may be time to ship a DGPS correction to the GPS */
	if (session->fixcnt > 10) {
	    if (!session->sentdgps) {
		session->sentdgps++;
		if (session->dsock > -1)
		{
		  char buf[BUFSIZE];

		  sprintf(buf, "R %0.8f %0.8f %0.2f\r\n", 
			  session->gNMEAdata.latitude,
			  session->gNMEAdata.longitude, 
			  session->gNMEAdata.altitude);
		  write(session->dsock, buf, strlen(buf));
		  gpscli_report(2, "=> dgps %s", buf);
		}
	    }
	}
    }
    return waiting;
}

void gpsd_wrap(struct gps_session_t *session)
/* end-of-session wrapup */
{
    gpsd_deactivate(session);
    if (session->dsock >= 0)
	close(session->dsock);
}