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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
/* libgps_sock.c -- client interface library for the gpsd daemon
*
* This file is Copyright (c) 2010 by the GPSD project
* BSD terms apply: see the file COPYING in the distribution root for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <locale.h>
#include <assert.h>
#include <sys/time.h> /* expected to have a select(2) prototype a la SuS */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#ifndef S_SPLINT_S
#include <sys/socket.h>
#include <unistd.h>
#endif /* S_SPLINT_S */
#ifndef USE_QT
#ifndef S_SPLINT_S
#include <sys/socket.h>
#endif /* S_SPLINT_S */
#else
#include <QTcpSocket>
#endif /* USE_QT */
#include "gps.h"
#include "gpsd.h"
#include "libgps.h"
#ifdef SOCKET_EXPORT_ENABLE
#include "gps_json.h"
#ifdef S_SPLINT_S
extern char *strtok_r(char *, const char *, char **);
#endif /* S_SPLINT_S */
/*@-matchfields@*/
struct privdata_t
{
bool newstyle;
/* data buffered from the last read */
ssize_t waiting;
char buffer[GPS_JSON_RESPONSE_MAX * 2];
#ifdef LIBGPS_DEBUG
int waitcount;
#endif /* LIBGPS_DEBUG */
};
/*@+matchfields@*/
/*@-branchstate@*/
int gps_sock_open(/*@null@*/const char *host, /*@null@*/const char *port,
/*@out@*/ struct gps_data_t *gpsdata)
{
if (!host)
host = "localhost";
if (!port)
port = DEFAULT_GPSD_PORT;
libgps_debug_trace((DEBUG_CALLS, "gps_sock_open(%s, %s)\n", host, port));
#ifndef USE_QT
if ((gpsdata->gps_fd =
netlib_connectsock(AF_UNSPEC, host, port, "tcp")) < 0) {
errno = gpsdata->gps_fd;
libgps_debug_trace((DEBUG_CALLS, "netlib_connectsock() returns error %d\n", errno));
return -1;
}
else
libgps_debug_trace((DEBUG_CALLS, "netlib_connectsock() returns socket on fd %d\n", gpsdata->gps_fd));
#else
QTcpSocket *sock = new QTcpSocket();
gpsdata->gps_fd = sock;
sock->connectToHost(host, QString(port).toInt());
if (!sock->waitForConnected())
qDebug() << "libgps::connect error: " << sock->errorString();
else
qDebug() << "libgps::connected!";
#endif /* USE_QT */
/* set up for line-buffered I/O over the daemon socket */
gpsdata->privdata = (void *)malloc(sizeof(struct privdata_t));
if (gpsdata->privdata == NULL)
return -1;
PRIVATE(gpsdata)->newstyle = false;
PRIVATE(gpsdata)->waiting = 0;
#ifdef LIBGPS_DEBUG
PRIVATE(gpsdata)->waitcount = 0;
#endif /* LIBGPS_DEBUG */
return 0;
}
/*@+branchstate@*/
bool gps_sock_waiting(const struct gps_data_t *gpsdata, int timeout)
/* is there input waiting from the GPS? */
{
#ifndef USE_QT
fd_set rfds;
struct timeval tv;
libgps_debug_trace((DEBUG_CALLS, "gps_waiting(%d): %d\n", timeout, PRIVATE(gpsdata)->waitcount++));
if (PRIVATE(gpsdata)->waiting > 0)
return true;
/* we might want to check for EINTR if this returns false */
errno = 0;
FD_ZERO(&rfds);
FD_SET(gpsdata->gps_fd, &rfds);
tv.tv_sec = timeout / 1000000;
tv.tv_usec = timeout % 1000000;
/* all error conditions return "not waiting" -- crude but effective */
return (select(gpsdata->gps_fd + 1, &rfds, NULL, NULL, &tv) == 1);
#else
return ((QTcpSocket *) (gpsdata->gps_fd))->waitForReadyRead(timeout / 1000);
#endif
}
/*@-usereleased -compdef@*/
int gps_sock_close(struct gps_data_t *gpsdata)
/* close a gpsd connection */
{
#ifndef USE_QT
int status;
free(PRIVATE(gpsdata));
status = close(gpsdata->gps_fd);
gpsdata->gps_fd = -1;
return status;
#else
QTcpSocket *sock = (QTcpSocket *) gpsdata->gps_fd;
sock->disconnectFromHost();
delete sock;
gpsdata->gps_fd = NULL;
return 0;
#endif
}
/*@+usereleased +compdef@*/
/*@-compdef -usedef -uniondef@*/
int gps_sock_read(/*@out@*/struct gps_data_t *gpsdata)
/* wait for and read data being streamed from the daemon */
{
char *eol;
ssize_t response_length;
int status = -1;
gpsdata->set &= ~PACKET_SET;
for (eol = PRIVATE(gpsdata)->buffer;
*eol != '\n' && eol < PRIVATE(gpsdata)->buffer + PRIVATE(gpsdata)->waiting; eol++)
continue;
if (*eol != '\n')
eol = NULL;
errno = 0;
if (eol == NULL) {
#ifndef USE_QT
/* read data: return -1 if no data waiting or buffered, 0 otherwise */
status = (int)recv(gpsdata->gps_fd,
PRIVATE(gpsdata)->buffer + PRIVATE(gpsdata)->waiting,
sizeof(PRIVATE(gpsdata)->buffer) - PRIVATE(gpsdata)->waiting, 0);
#else
status =
((QTcpSocket *) (gpsdata->gps_fd))->read(PRIVATE(gpsdata)->buffer +
PRIVATE(gpsdata)->waiting,
sizeof(PRIVATE(gpsdata)->buffer) -
PRIVATE(gpsdata)->waiting);
#endif
/* if we just received data from the socket, it's in the buffer */
if (status > -1)
PRIVATE(gpsdata)->waiting += status;
/* buffer is empty - implies no data was read */
if (PRIVATE(gpsdata)->waiting == 0) {
/*
* If we received 0 bytes, other side of socket is closing.
* Return -1 as end-of-data indication.
*/
// cppcheck-suppress duplicateBranch
if (status == 0)
return -1;
#ifndef USE_QT
/* count transient errors as success, we'll retry later */
else if (errno == EINTR || errno == EAGAIN
|| errno == EWOULDBLOCK)
return 0;
#endif
/* hard error return of -1, pass it along */
else
return -1;
}
/* there's buffered data waiting to be returned */
for (eol = PRIVATE(gpsdata)->buffer;
*eol != '\n' && eol < PRIVATE(gpsdata)->buffer + PRIVATE(gpsdata)->waiting; eol++)
continue;
if (*eol != '\n')
eol = NULL;
if (eol == NULL)
return 0;
}
assert(eol != NULL);
*eol = '\0';
response_length = eol - PRIVATE(gpsdata)->buffer + 1;
gpsdata->online = timestamp();
status = gps_unpack(PRIVATE(gpsdata)->buffer, gpsdata);
/*@+matchanyintegral@*/
memmove(PRIVATE(gpsdata)->buffer,
PRIVATE(gpsdata)->buffer + response_length, PRIVATE(gpsdata)->waiting - response_length);
/*@-matchanyintegral@*/
PRIVATE(gpsdata)->waiting -= response_length;
gpsdata->set |= PACKET_SET;
return (status == 0) ? (int)response_length : status;
}
/*@+compdef -usedef +uniondef@*/
/*@ -branchstate -usereleased -mustfreefresh -nullstate -usedef @*/
int gps_unpack(char *buf, struct gps_data_t *gpsdata)
/* unpack a gpsd response into a status structure, buf must be writeable.
* gps_unpack() currently returns 0 in all cases, but should it ever need to
* return an error status, it must be < 0.
*/
{
libgps_debug_trace((DEBUG_CALLS, "gps_unpack(%s)\n", buf));
/* detect and process a JSON response */
if (buf[0] == '{') {
const char *jp = buf, **next = &jp;
while (next != NULL && *next != NULL && next[0][0] != '\0') {
libgps_debug_trace((DEBUG_CALLS,"gps_unpack() segment parse '%s'\n", *next));
if (libgps_json_unpack(*next, gpsdata, next) == -1)
break;
#ifdef LIBGPS_DEBUG
if (libgps_debuglevel >= 1)
libgps_dump_state(gpsdata);
#endif /* LIBGPS_DEBUG */
}
#ifdef OLDSTYLE_ENABLE
if (PRIVATE(gpsdata) != NULL)
PRIVATE(gpsdata)->newstyle = true;
#endif /* OLDSTYLE_ENABLE */
}
#ifdef OLDSTYLE_ENABLE
else {
/*
* Get the decimal separator for the current application locale.
* This looks thread-unsafe, but it's not. The key is that
* character assignment is atomic.
*/
char *ns, *sp, *tp;
#ifdef __UNUSED__
static char decimal_point = '\0';
if (decimal_point == '\0') {
struct lconv *locale_data = localeconv();
if (locale_data != NULL && locale_data->decimal_point[0] != '.')
decimal_point = locale_data->decimal_point[0];
}
#endif /* __UNUSED__ */
for (ns = buf; ns; ns = strstr(ns + 1, "GPSD")) {
if ( /*@i1@*/ strncmp(ns, "GPSD", 4) == 0) {
/* the following should execute each time we have a good next sp */
for (sp = ns + 5; *sp != '\0'; sp = tp + 1) {
bool eol;
tp = sp + strcspn(sp, ",\r\n");
eol = *tp == '\r' || *tp == '\n';
if (*tp == '\0')
tp--;
else
*tp = '\0';
#ifdef __UNUSED__
/*
* The daemon always emits the Anglo-American and SI
* decimal point. Hack these into whatever the
* application locale requires if it's not the same.
* This has to happen *after* we grab the next
* comma-delimited response, or we'll lose horribly
* in locales where the decimal separator is comma.
*/
if (decimal_point != '\0') {
char *cp;
for (cp = sp; cp < tp; cp++)
if (*cp == '.')
*cp = decimal_point;
}
#endif /* __UNUSED__ */
/* note, there's a bit of skip logic after the switch */
switch (*sp) {
case 'F': /*@ -mustfreeonly */
if (sp[2] == '?')
gpsdata->dev.path[0] = '\0';
else {
/*@ -mayaliasunique @*/
(void)strlcpy(gpsdata->dev.path, sp + 2,
sizeof(gpsdata->dev.path));
/*@ +mayaliasunique @*/
gpsdata->set |= DEVICE_SET;
}
/*@ +mustfreeonly */
break;
case 'I':
/*@ -mustfreeonly */
if (sp[2] == '?')
gpsdata->dev.subtype[0] = '\0';
else {
(void)strlcpy(gpsdata->dev.subtype, sp + 2,
sizeof(gpsdata->dev.subtype));
gpsdata->set |= DEVICEID_SET;
}
/*@ +mustfreeonly */
break;
case 'O':
if (sp[2] == '?') {
gpsdata->set = MODE_SET | STATUS_SET;
gpsdata->status = STATUS_NO_FIX;
gps_clear_fix(&gpsdata->fix);
} else {
struct gps_fix_t nf;
char alt[20];
char eph[20], epv[20], track[20], speed[20],
climb[20];
char epd[20], eps[20], epc[20], mode[2];
char timestr[20], ept[20], lat[20], lon[20];
int st = sscanf(sp + 2,
"%*s %19s %19s %19s %19s %19s %19s %19s %19s %19s %19s %19s %19s %19s %1s",
timestr, ept, lat, lon,
alt, eph, epv, track, speed,
climb,
epd, eps, epc, mode);
if (st >= 14) {
#define DEFAULT(val) (val[0] == '?') ? NAN : safe_atof(val)
/*@ +floatdouble @*/
nf.time = DEFAULT(timestr);
nf.latitude = DEFAULT(lat);
nf.longitude = DEFAULT(lon);
nf.ept = DEFAULT(ept);
nf.altitude = DEFAULT(alt);
/* designed before we split eph into epx+epy */
nf.epx = nf.epy = DEFAULT(eph) / sqrt(2);
nf.epv = DEFAULT(epv);
nf.track = DEFAULT(track);
nf.speed = DEFAULT(speed);
nf.climb = DEFAULT(climb);
nf.epd = DEFAULT(epd);
nf.eps = DEFAULT(eps);
nf.epc = DEFAULT(epc);
/*@ -floatdouble @*/
#undef DEFAULT
if (st >= 15)
nf.mode =
(mode[0] ==
'?') ? MODE_NOT_SEEN : atoi(mode);
else
nf.mode =
(alt[0] == '?') ? MODE_2D : MODE_3D;
if (alt[0] != '?')
gpsdata->set |= ALTITUDE_SET | CLIMB_SET;
if (isnan(nf.epx) == 0 && isnan(nf.epy) == 0)
gpsdata->set |= HERR_SET;
if (isnan(nf.epv) == 0)
gpsdata->set |= VERR_SET;
if (isnan(nf.track) == 0)
gpsdata->set |= TRACK_SET | SPEED_SET;
if (isnan(nf.eps) == 0)
gpsdata->set |= SPEEDERR_SET;
if (isnan(nf.epc) == 0)
gpsdata->set |= CLIMBERR_SET;
gpsdata->fix = nf;
gpsdata->set |=
TIME_SET | TIMERR_SET | LATLON_SET |
MODE_SET;
gpsdata->status = STATUS_FIX;
gpsdata->set |= STATUS_SET;
}
}
break;
case 'X':
if (sp[2] == '?')
gpsdata->online = (timestamp_t)-1;
else {
// cppcheck-suppress invalidscanf
(void)sscanf(sp, "X=%lf", &gpsdata->online);
gpsdata->set |= ONLINE_SET;
}
break;
case 'Y':
if (sp[2] == '?') {
gpsdata->satellites_visible = 0;
} else {
int j, i1, i2, i3, i5;
double f4;
char timestamp[21];
// cppcheck-suppress invalidscanf
(void)sscanf(sp, "Y=%*s %20s %d ",
timestamp,
&gpsdata->satellites_visible);
if (timestamp[0] != '?') {
gpsdata->set |= TIME_SET;
}
for (j = 0; j < MAXCHANNELS; j++) {
gpsdata->PRN[j] = gpsdata->elevation[j] = gpsdata->azimuth[j] = gpsdata->used[j] = 0;
gpsdata->ss[j] = 0.0;
}
for (j = 0, gpsdata->satellites_used = 0;
j < gpsdata->satellites_visible; j++) {
if ((sp != NULL)
&& ((sp = strchr(sp, ':')) != NULL)) {
sp++;
// cppcheck-suppress invalidscanf
(void)sscanf(sp, "%d %d %d %lf %d", &i1,
&i2, &i3, &f4, &i5);
gpsdata->PRN[j] = i1;
gpsdata->elevation[j] = i2;
gpsdata->azimuth[j] = i3;
gpsdata->ss[j] = f4;
gpsdata->used[j] = i5;
if (i5 == 1)
gpsdata->satellites_used++;
}
}
}
gpsdata->set |= SATELLITE_SET;
break;
}
#ifdef LIBGPS_DEBUG
if (libgps_debuglevel >= 1)
libgps_dump_state(gpsdata);
#endif /* LIBGPS_DEBUG */
/*
* Skip to next GPSD when we see \r or \n;
* we don't want to try interpreting stuff
* in between that might be raw mode data.
*/
if (eol)
break;
}
}
}
}
#endif /* OLDSTYLE_ENABLE */
#ifndef USE_QT
libgps_debug_trace((DEBUG_CALLS, "final flags: (0x%04x) %s\n", gpsdata->set,gps_maskdump(gpsdata->set)));
#endif
return 0;
}
/*@ +compdef @*/
const char /*@observer@*/ *gps_sock_data(const struct gps_data_t *gpsdata)
/* return the contents of the client data buffer */
{
return PRIVATE(gpsdata)->buffer;
}
int gps_sock_send(struct gps_data_t *gpsdata, const char *buf)
/* send a command to the gpsd instance */
{
#ifndef USE_QT
if (write(gpsdata->gps_fd, buf, strlen(buf)) == (ssize_t) strlen(buf))
return 0;
else
return -1;
#else
QTcpSocket *sock = (QTcpSocket *) gpsdata->gps_fd;
sock->write(buf, strlen(buf));
if (sock->waitForBytesWritten())
return 0;
else {
qDebug() << "libgps::send error: " << sock->errorString();
return -1;
}
#endif
}
int gps_sock_stream(struct gps_data_t *gpsdata, unsigned int flags,
/*@null@*/ void *d)
/* ask gpsd to stream reports at you, hiding the command details */
{
char buf[GPS_JSON_COMMAND_MAX];
if ((flags & (WATCH_JSON | WATCH_OLDSTYLE | WATCH_NMEA | WATCH_RAW)) == 0) {
flags |= WATCH_JSON;
}
if ((flags & WATCH_DISABLE) != 0) {
if ((flags & WATCH_OLDSTYLE) != 0) {
(void)strlcpy(buf, "w-", sizeof(buf));
if ((flags & WATCH_NMEA) != 0)
(void)strlcat(buf, "r-", sizeof(buf));
} else {
(void)strlcpy(buf, "?WATCH={\"enable\":false,", sizeof(buf));
if (flags & WATCH_JSON)
(void)strlcat(buf, "\"json\":false,", sizeof(buf));
if (flags & WATCH_NMEA)
(void)strlcat(buf, "\"nmea\":false,", sizeof(buf));
if (flags & WATCH_RAW)
(void)strlcat(buf, "\"raw\":1,", sizeof(buf));
if (flags & WATCH_RARE)
(void)strlcat(buf, "\"raw\":0,", sizeof(buf));
if (flags & WATCH_SCALED)
(void)strlcat(buf, "\"scaled\":false,", sizeof(buf));
if (flags & WATCH_TIMING)
(void)strlcat(buf, "\"timing\":false,", sizeof(buf));
if (flags & WATCH_SPLIT24)
(void)strlcat(buf, "\"split24\":false,", sizeof(buf));
if (flags & WATCH_PPS)
(void)strlcat(buf, "\"pps\":false,", sizeof(buf));
if (buf[strlen(buf) - 1] == ',')
buf[strlen(buf) - 1] = '\0';
(void)strlcat(buf, "};", sizeof(buf));
}
libgps_debug_trace((DEBUG_CALLS, "gps_stream() disable command: %s\n", buf));
return gps_send(gpsdata, buf);
} else { /* if ((flags & WATCH_ENABLE) != 0) */
if ((flags & WATCH_OLDSTYLE) != 0) {
(void)strlcpy(buf, "w+x", sizeof(buf));
if ((flags & WATCH_NMEA) != 0)
(void)strlcat(buf, "r+", sizeof(buf));
} else {
(void)strlcpy(buf, "?WATCH={\"enable\":true,", sizeof(buf));
if (flags & WATCH_JSON)
(void)strlcat(buf, "\"json\":true,", sizeof(buf));
if (flags & WATCH_NMEA)
(void)strlcat(buf, "\"nmea\":true,", sizeof(buf));
if (flags & WATCH_RARE)
(void)strlcat(buf, "\"raw\":1,", sizeof(buf));
if (flags & WATCH_RAW)
(void)strlcat(buf, "\"raw\":2,", sizeof(buf));
if (flags & WATCH_SCALED)
(void)strlcat(buf, "\"scaled\":true,", sizeof(buf));
if (flags & WATCH_TIMING)
(void)strlcat(buf, "\"timing\":true,", sizeof(buf));
if (flags & WATCH_SPLIT24)
(void)strlcat(buf, "\"split24\":true,", sizeof(buf));
if (flags & WATCH_PPS)
(void)strlcat(buf, "\"pps\":true,", sizeof(buf));
/*@-nullpass@*//* shouldn't be needed, splint has a bug */
if (flags & WATCH_DEVICE)
(void)snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
"\"device\":\"%s\",", (char *)d);
/*@+nullpass@*/
if (buf[strlen(buf) - 1] == ',')
buf[strlen(buf) - 1] = '\0';
(void)strlcat(buf, "};", sizeof(buf));
}
libgps_debug_trace((DEBUG_CALLS, "gps_stream() enable command: %s\n", buf));
return gps_send(gpsdata, buf);
}
}
int gps_sock_mainloop(struct gps_data_t *gpsdata, int timeout,
void (*hook)(struct gps_data_t *gpsdata))
/* run a socket main loop with a specified handler */
{
for (;;) {
if (!gps_waiting(gpsdata, timeout)) {
return -1;
} else {
(void)gps_read(gpsdata);
(*hook)(gpsdata);
}
}
//return 0;
}
#endif /* SOCKET_EXPORT_ENABLE */
/* end */
|