/src/gpsd/gpsd-3.27.6~dev/gpsd/libgpsd_core.c
Line | Count | Source |
1 | | /* libgpsd_core.c -- manage access to sensors |
2 | | * |
3 | | * Access to the driver layer goes through the entry points in this file. |
4 | | * The idea is to present a session as an abstraction from which you get |
5 | | * fixes (and possibly other data updates) by calling gpsd_multipoll(). The |
6 | | * rest is setup and teardown. (For backward compatibility the older gpsd_poll() |
7 | | * entry point has been retained.) |
8 | | * |
9 | | * This file is Copyright 2010 by the GPSD project |
10 | | * SPDX-License-Identifier: BSD-2-clause |
11 | | */ |
12 | | |
13 | | #include "../include/gpsd_config.h" // must be before all includes |
14 | | |
15 | | #include <assert.h> |
16 | | #include <ctype.h> |
17 | | #include <errno.h> |
18 | | #include <fcntl.h> |
19 | | #include <libgen.h> |
20 | | #include <math.h> |
21 | | #include <stdarg.h> |
22 | | #include <stdbool.h> |
23 | | #include <stdio.h> |
24 | | #include <stdlib.h> |
25 | | #include <string.h> |
26 | | #include <syslog.h> |
27 | | #include <sys/select.h> // for pselect() per POSIX |
28 | | #include <sys/socket.h> |
29 | | #include <sys/stat.h> |
30 | | #include <sys/types.h> |
31 | | #include <sys/wait.h> |
32 | | #include <time.h> |
33 | | #include <unistd.h> |
34 | | |
35 | | #include "../include/gpsd.h" |
36 | | #include "../include/matrix.h" |
37 | | #include "../include/strfuncs.h" |
38 | | #include "../include/timespec.h" |
39 | | #if defined(NMEA2000_ENABLE) |
40 | | #include "../include/driver_nmea2000.h" |
41 | | #endif // defined(NMEA2000_ENABLE) |
42 | | |
43 | | // pass low-level data to devices straight through |
44 | | ssize_t gpsd_write(struct gps_device_t *session, |
45 | | const char *buf, |
46 | | const size_t len) |
47 | 165k | { |
48 | 165k | return session->context->serial_write(session, buf, len); |
49 | 165k | } |
50 | | |
51 | | static void basic_report(const char *buf) |
52 | 0 | { |
53 | 0 | (void)fputs(buf, stderr); |
54 | 0 | } |
55 | | |
56 | | void errout_reset(struct gpsd_errout_t *errout) |
57 | 4 | { |
58 | 4 | errout->debug = LOG_SHOUT; |
59 | 4 | errout->report = basic_report; |
60 | 4 | } |
61 | | |
62 | | static pthread_mutex_t report_mutex; |
63 | | |
64 | | void gpsd_acquire_reporting_lock(void) |
65 | 25.3k | { |
66 | 25.3k | int err; |
67 | | |
68 | | // pthread_mutex_lock() returns zero, or an error code. |
69 | 25.3k | err = pthread_mutex_lock(&report_mutex); |
70 | 25.3k | if (0 != err) { |
71 | | /* POSIX says pthread_mutex_lock() should only fail if the |
72 | | thread holding the lock has died. Best for gpsd to just die |
73 | | because things are FUBAR. */ |
74 | |
|
75 | 0 | (void)fprintf(stderr,"pthread_mutex_lock() failed: %s(%d)\n", |
76 | 0 | strerror(err), err); |
77 | 0 | exit(EXIT_FAILURE); |
78 | 0 | } |
79 | 25.3k | } |
80 | | |
81 | | void gpsd_release_reporting_lock(void) |
82 | 25.3k | { |
83 | 25.3k | int err; |
84 | | |
85 | | // pthread_mutex_lock() returns zero, or an error code. |
86 | 25.3k | err = pthread_mutex_unlock(&report_mutex); |
87 | 25.3k | if (0 != err) { |
88 | | /* POSIX says pthread_mutex_unlock() should only fail when |
89 | | trying to unlock a lock that does not exist, or is not owned by |
90 | | this thread. This should never happen, so best for gpsd to die |
91 | | because things are FUBAR. */ |
92 | |
|
93 | 0 | (void)fprintf(stderr,"pthread_mutex_unlock() failed: %s(%d)\n", |
94 | 0 | strerror(err), err); |
95 | 0 | exit(EXIT_FAILURE); |
96 | 0 | } |
97 | 25.3k | } |
98 | | |
99 | | // assemble msg in vprintf(3) style, use errout hook or syslog for delivery |
100 | | // FIXME: duplicated in gpsd/libgpsd_core.c |
101 | | static void gpsd_vlog(const int errlevel, |
102 | | const struct gpsd_errout_t *errout, |
103 | | char *outbuf, size_t outlen, |
104 | | const char *fmt, va_list ap) |
105 | 25.3k | { |
106 | | #ifdef SQUELCH_ENABLE |
107 | | (void)errout; |
108 | | (void)errlevel; |
109 | | (void)fmt; |
110 | | #else |
111 | 25.3k | char buf[BUFSIZ]; |
112 | 25.3k | const char *err_str; |
113 | 25.3k | const char *label; |
114 | 25.3k | int level = LOG_ERR; |
115 | | |
116 | 25.3k | gpsd_acquire_reporting_lock(); |
117 | 25.3k | switch (errlevel) { |
118 | 23.3k | case LOG_ERROR: // -1, cannot turn off |
119 | 23.3k | err_str = "ERROR"; |
120 | 23.3k | level = LOG_CRIT; |
121 | 23.3k | break; |
122 | 1.91k | case LOG_SHOUT: // 0, cannot turn off |
123 | 1.91k | err_str = "SHOUT"; |
124 | 1.91k | level = LOG_ERR; |
125 | 1.91k | break; |
126 | 0 | case LOG_WARN: // 1 |
127 | 0 | err_str = "WARN"; |
128 | 0 | level = LOG_WARNING; |
129 | 0 | break; |
130 | 0 | case LOG_CLIENT: // 2, log JSON to clients |
131 | 0 | err_str = "CLIENT"; |
132 | 0 | level = LOG_NOTICE; |
133 | 0 | break; |
134 | 0 | case LOG_INF: // 3, informative info |
135 | 0 | err_str = "INFO"; |
136 | 0 | level = LOG_INFO; |
137 | 0 | break; |
138 | 0 | case LOG_PROG: // 4, program progress messages |
139 | 0 | err_str = "PROG"; |
140 | 0 | level = LOG_DEBUG; |
141 | 0 | break; |
142 | 0 | case LOG_IO: // 5, device IO |
143 | 0 | err_str = "IO"; |
144 | 0 | level = LOG_DEBUG; |
145 | 0 | break; |
146 | 0 | case LOG_DATA: // 6, decoded data |
147 | 0 | err_str = "DATA"; |
148 | 0 | level = LOG_DEBUG; |
149 | 0 | break; |
150 | 0 | case LOG_SPIN: // 7, spin logging |
151 | 0 | err_str = "SPIN"; |
152 | 0 | level = LOG_DEBUG; |
153 | 0 | break; |
154 | 0 | case LOG_RAW: // 8, low level IO |
155 | 0 | err_str = "RAW"; |
156 | 0 | level = LOG_DEBUG; |
157 | 0 | break; |
158 | 0 | case LOG_RAW1: // 9, ridiculous |
159 | 0 | err_str = "RAW1"; |
160 | 0 | level = LOG_DEBUG; |
161 | 0 | break; |
162 | 0 | case LOG_RAW2: // 10, insane |
163 | 0 | err_str = "RAW2"; |
164 | 0 | level = LOG_DEBUG; |
165 | 0 | break; |
166 | 0 | default: // WTF? |
167 | 0 | err_str = "UNK"; |
168 | 0 | level = LOG_CRIT; |
169 | 0 | break; |
170 | 25.3k | } |
171 | | |
172 | 25.3k | if (NULL == errout->label) { |
173 | 0 | label = "MISSING"; |
174 | 25.3k | } else { |
175 | 25.3k | label = errout->label; |
176 | 25.3k | } |
177 | 25.3k | snprintf(buf, sizeof(buf), "%s:%s: %s", label, err_str, fmt); |
178 | 25.3k | vsnprintf(outbuf, outlen, buf, ap); |
179 | | |
180 | | // this was crazy expensive, just fix the bad log calls |
181 | | // gps_visibilize(outbuf, outlen, buf, strlen(buf)); |
182 | | |
183 | 25.3k | if (getpid() == getsid(getpid())) { |
184 | | // I think this calls syslog() only when daemonized |
185 | 0 | syslog(level, "%s", outbuf); |
186 | 25.3k | } else if (NULL != errout->report) { |
187 | | // we are a thread, use report()? |
188 | | // FIXME: is POSIX syslog() thread safe? |
189 | 25.3k | errout->report(outbuf); |
190 | 25.3k | } else { |
191 | | // foreground, use stderr? |
192 | 0 | (void)fputs(outbuf, stderr); |
193 | 0 | } |
194 | 25.3k | gpsd_release_reporting_lock(); |
195 | 25.3k | #endif // !SQUELCH_ENABLE |
196 | 25.3k | } |
197 | | |
198 | | // assemble msg in printf(3) style, use errout hook or syslog for delivery |
199 | | void gpsd_log(const int errlevel, const struct gpsd_errout_t *errout, |
200 | | const char *fmt, ...) |
201 | 25.3k | { |
202 | 25.3k | char buf[BUFSIZ]; |
203 | 25.3k | va_list ap; |
204 | | |
205 | | // errout should never be NULL, but some code analyzers complain anyway |
206 | 25.3k | if (NULL == errout || |
207 | 25.3k | errout->debug < errlevel) { |
208 | 0 | return; |
209 | 0 | } |
210 | | |
211 | 25.3k | buf[0] = '\0'; |
212 | 25.3k | va_start(ap, fmt); |
213 | 25.3k | gpsd_vlog(errlevel, errout, buf, sizeof(buf), fmt, ap); |
214 | 25.3k | va_end(ap); |
215 | 25.3k | } |
216 | | |
217 | | // dump the current packet in a form optimised for eyeballs |
218 | | const char *gpsd_prettydump(struct gps_device_t *session) |
219 | 926 | { |
220 | 926 | return gpsd_packetdump(session->msgbuf, sizeof(session->msgbuf), |
221 | 926 | session->lexer.outbuffer, |
222 | 926 | session->lexer.outbuflen); |
223 | 926 | } |
224 | | |
225 | | // Define the possible hook strings here so we can get the length |
226 | 0 | #define HOOK_ACTIVATE "ACTIVATE" |
227 | 0 | #define HOOK_DEACTIVATE "DEACTIVATE" |
228 | | |
229 | | #define HOOK_CMD_MAX (sizeof(DEVICEHOOKPATH) + GPS_PATH_MAX \ |
230 | | + sizeof(HOOK_DEACTIVATE)) |
231 | | |
232 | | static void gpsd_run_device_hook(struct gpsd_errout_t *errout, |
233 | | char *device_name, char *hook) |
234 | 0 | { |
235 | 0 | struct stat statbuf; |
236 | 0 | int status; |
237 | 0 | char buf[HOOK_CMD_MAX]; |
238 | |
|
239 | 0 | if (-1 == stat(DEVICEHOOKPATH, &statbuf)) { |
240 | 0 | GPSD_LOG(LOG_PROG, errout, |
241 | 0 | "CORE: no %s present, skipped running %s hook. %s(%d)\n", |
242 | 0 | DEVICEHOOKPATH, hook, strerror(errno), errno); |
243 | 0 | return; |
244 | 0 | } |
245 | | |
246 | 0 | (void)snprintf(buf, sizeof(buf), "%s %s %s", |
247 | 0 | DEVICEHOOKPATH, device_name, hook); |
248 | 0 | GPSD_LOG(LOG_INF, errout, "CORE: running %s\n", buf); |
249 | 0 | status = system(buf); |
250 | 0 | if (-1 == status) { |
251 | 0 | GPSD_LOG(LOG_ERROR, errout, "CORE: error %s(%d) running %s\n", |
252 | 0 | strerror(errno), errno, buf); |
253 | 0 | } else { |
254 | 0 | GPSD_LOG(LOG_INF, errout, |
255 | 0 | "CORE: %s returned %d\n", DEVICEHOOKPATH, |
256 | 0 | WEXITSTATUS(status)); |
257 | 0 | } |
258 | 0 | } |
259 | | |
260 | | int gpsd_switch_driver(struct gps_device_t *session, char *type_name) |
261 | 75.1k | { |
262 | 75.1k | const struct gps_type_t **dp; |
263 | 75.1k | bool first_sync = (NULL != session->device_type); |
264 | 75.1k | unsigned int i; |
265 | | |
266 | 75.1k | if (first_sync && |
267 | 27.1k | 0 == strcmp(session->device_type->type_name, type_name)) { |
268 | | // no need to switch driver |
269 | 0 | return 0; |
270 | 0 | } |
271 | | |
272 | 75.1k | GPSD_LOG(LOG_PROG, &session->context->errout, |
273 | 75.1k | "CORE: switch_driver(%s) called...\n", type_name); |
274 | 1.35M | for (dp = gpsd_drivers, i = 0; *dp; dp++, i++) |
275 | 1.35M | if (0 == strcmp((*dp)->type_name, type_name)) { |
276 | 75.1k | GPSD_LOG(LOG_PROG, &session->context->errout, |
277 | 75.1k | "CORE: selecting %s driver...\n", |
278 | 75.1k | (*dp)->type_name); |
279 | 75.1k | gpsd_assert_sync(session); |
280 | 75.1k | session->device_type = *dp; |
281 | 75.1k | session->driver_index = i; |
282 | 75.1k | session->gpsdata.dev.mincycle = session->device_type->min_cycle; |
283 | | // reconfiguration might be required |
284 | 75.1k | if (first_sync && |
285 | 75.1k | NULL != session->device_type->event_hook) { |
286 | 23.0k | session->device_type->event_hook(session, |
287 | 23.0k | EVENT_DRIVER_SWITCH); |
288 | 23.0k | } |
289 | 75.1k | if (STICKY(*dp)) { |
290 | 47.3k | session->last_controller = *dp; |
291 | 47.3k | } |
292 | 75.1k | return 1; |
293 | 75.1k | } |
294 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
295 | 0 | "CORE: invalid GPS type \"%s\".\n", type_name); |
296 | 0 | return 0; |
297 | 75.1k | } |
298 | | |
299 | | void gps_context_init(struct gps_context_t *context, |
300 | | const char *label) |
301 | 4 | { |
302 | 4 | (void)memset(context, '\0', sizeof(struct gps_context_t)); |
303 | | //context.readonly = false; |
304 | 4 | context->leap_notify = LEAP_NOWARNING; |
305 | 4 | context->serial_write = gpsd_serial_write; |
306 | | |
307 | 4 | errout_reset(&context->errout); |
308 | 4 | context->errout.label = label; |
309 | | |
310 | 4 | (void)pthread_mutex_init(&report_mutex, NULL); |
311 | 4 | } |
312 | | |
313 | | // initialize GPS polling |
314 | | void gpsd_init(struct gps_device_t *session, struct gps_context_t *context, |
315 | | const char *device_path) |
316 | 4 | { |
317 | 4 | (void)memset(session, 0, sizeof(struct gps_device_t)); |
318 | | |
319 | 4 | if (NULL != device_path) { |
320 | 4 | (void)strlcpy(session->gpsdata.dev.path, device_path, |
321 | 4 | sizeof(session->gpsdata.dev.path)); |
322 | 4 | obfuscate_uri(session->gpsdata.dev.path_obf, device_path, |
323 | 4 | sizeof(session->gpsdata.dev.path_obf)); |
324 | 4 | } else { |
325 | | // should fail?? |
326 | 0 | session->gpsdata.dev.path[0] = '\0'; |
327 | 0 | session->gpsdata.dev.path_obf[0] = '\0'; |
328 | 0 | } |
329 | | |
330 | | /* with memset(), no need to set NULLs, or zeros |
331 | | * |
332 | | * session->device_type = NULL; // start by hunting packets |
333 | | * session->last_controller = NULL; |
334 | | * session->observed = 0; |
335 | | * memset(session->subtype, 0, sizeof(session->subtype)); |
336 | | * memset(session->subtype1, 0, sizeof(session->subtype1)); |
337 | | * memset(&(session->nmea), 0, sizeof(session->nmea)); |
338 | | * session->gpsdata.set = 0; |
339 | | * session->sor = (timespec_t){0, 0}; |
340 | | * session->ts_startCurrentBaud = (timespec_t){0, 0}; |
341 | | * session->chars = 0; |
342 | | * |
343 | | */ |
344 | 4 | session->context = context; |
345 | 4 | session->gpsdata.dev.cycle =(timespec_t){1, 0}; |
346 | 4 | session->gpsdata.dev.mincycle = (timespec_t){1, 0}; |
347 | 4 | session->gpsdata.dev.parity = ' '; // will be E, N, or O |
348 | 4 | session->servicetype = SERVICE_UNKNOWN; // gpsd_open() sets this |
349 | 4 | session->shm_clock_unit = -1; |
350 | 4 | session->shm_pps_unit = -1; |
351 | 4 | session->sourcetype = SOURCE_UNKNOWN; // gpsd_open() sets this |
352 | 4 | gps_clear_att(&session->gpsdata.attitude); |
353 | 4 | gps_clear_dop(&session->gpsdata.dop); |
354 | 4 | gps_clear_fix(&session->gpsdata.fix); |
355 | 4 | gps_clear_fix(&session->lastfix); |
356 | 4 | gps_clear_fix(&session->newdata); |
357 | 4 | gps_clear_fix(&session->oldfix); |
358 | 4 | gps_clear_gst(&session->gpsdata.gst); |
359 | 4 | gps_clear_log(&session->gpsdata.log); |
360 | | // tty-level initialization |
361 | 4 | gpsd_tty_init(session); |
362 | | // necessary in case we start reading in the middle of a GPGSV sequence |
363 | 4 | gpsd_zero_satellites(&session->gpsdata); |
364 | | |
365 | | // initialize things for the packet parser |
366 | 4 | lexer_init(&session->lexer, &session->context->errout); |
367 | 4 | } |
368 | | |
369 | | // temporarily release the GPS device |
370 | | void gpsd_deactivate(struct gps_device_t *session) |
371 | 0 | { |
372 | 0 | if (!session->context->readonly && |
373 | 0 | NULL != session->device_type && |
374 | 0 | NULL != session->device_type->event_hook) { |
375 | 0 | session->device_type->event_hook(session, EVENT_DEACTIVATE); |
376 | 0 | } |
377 | | // cast for 32-bit ints. |
378 | 0 | GPSD_LOG(LOG_INF, &session->context->errout, |
379 | 0 | "CORE: closing %s, fd %ld\n", |
380 | 0 | session->gpsdata.dev.path, (long)session->gpsdata.gps_fd); |
381 | 0 | if (SERVICE_NTRIP == session->servicetype) { |
382 | 0 | ntrip_close(session); |
383 | 0 | } else |
384 | 0 | #if defined(NMEA2000_ENABLE) |
385 | 0 | if (SOURCE_CAN == session->sourcetype) { |
386 | 0 | (void)nmea2000_close(session); |
387 | 0 | } else |
388 | 0 | #endif // NMEA2000_ENABLE |
389 | 0 | { |
390 | | // could be serial, udp://, tcp://, etc. |
391 | 0 | gpsd_close(session); |
392 | 0 | } |
393 | 0 | if (O_OPTIMIZE == session->mode) { |
394 | 0 | gpsd_run_device_hook(&session->context->errout, |
395 | 0 | session->gpsdata.dev.path, |
396 | 0 | HOOK_DEACTIVATE); |
397 | 0 | } |
398 | | // tell any PPS-watcher thread to die |
399 | 0 | session->pps_thread.report_hook = NULL; |
400 | | // mark it inactivated |
401 | 0 | session->gpsdata.online.tv_sec = 0; |
402 | 0 | session->gpsdata.online.tv_nsec = 0; |
403 | 0 | } |
404 | | |
405 | | // shim function to decouple PPS monitor code from the session structure |
406 | | static void ppsthread_log(volatile struct pps_thread_t *pps_thread, |
407 | | int loglevel, const char *fmt, ...) |
408 | 0 | { |
409 | 0 | struct gps_device_t *device = (struct gps_device_t *)pps_thread->context; |
410 | 0 | char buf[BUFSIZ]; |
411 | 0 | va_list ap; |
412 | |
|
413 | 0 | switch (loglevel) { |
414 | 0 | case THREAD_ERROR: |
415 | 0 | loglevel = LOG_ERROR; |
416 | 0 | break; |
417 | 0 | case THREAD_WARN: |
418 | 0 | loglevel = LOG_WARN; |
419 | 0 | break; |
420 | 0 | case THREAD_INF: |
421 | 0 | loglevel = LOG_INF; |
422 | 0 | break; |
423 | 0 | case THREAD_PROG: |
424 | 0 | loglevel = LOG_PROG; |
425 | 0 | break; |
426 | 0 | case THREAD_RAW: |
427 | 0 | loglevel = LOG_RAW; |
428 | 0 | break; |
429 | 0 | } |
430 | | |
431 | 0 | if (device->context->errout.debug < loglevel) { |
432 | | // skip it |
433 | 0 | return; |
434 | 0 | } |
435 | | |
436 | 0 | buf[0] = '\0'; |
437 | 0 | va_start(ap, fmt); |
438 | 0 | gpsd_vlog(loglevel, &device->context->errout, buf, sizeof(buf), fmt, ap); |
439 | 0 | va_end(ap); |
440 | 0 | } |
441 | | |
442 | | /* gpsd_clear().- set and clear some data storage fields. |
443 | | * device has been opened. |
444 | | * So some things like path and gpsd_fd are already set. |
445 | | * |
446 | | * Return: void |
447 | | */ |
448 | | void gpsd_clear(struct gps_device_t *session) |
449 | 57.0k | { |
450 | 57.0k | (void)clock_gettime(CLOCK_REALTIME, &session->gpsdata.online); |
451 | 57.0k | lexer_init(&session->lexer, &session->context->errout); |
452 | | // session->gpsdata.online = 0; |
453 | 57.0k | gps_clear_att(&session->gpsdata.attitude); |
454 | 57.0k | gps_clear_dop(&session->gpsdata.dop); |
455 | 57.0k | gps_clear_fix(&session->gpsdata.fix); |
456 | 57.0k | gps_clear_gst(&session->gpsdata.gst); |
457 | 57.0k | session->releasetime = (time_t)0; |
458 | 57.0k | session->badcount = 0; |
459 | | |
460 | | // clear the private data union |
461 | 57.0k | memset((void *)&session->driver, '\0', sizeof(session->driver)); |
462 | | // set up the context structure for the PPS thread monitor |
463 | 57.0k | memset_volatile(&session->pps_thread, 0, sizeof(session->pps_thread)); |
464 | 57.0k | session->pps_thread.devicefd = session->gpsdata.gps_fd; |
465 | 57.0k | session->pps_thread.devicename = session->gpsdata.dev.path; |
466 | 57.0k | session->pps_thread.log_hook = ppsthread_log; |
467 | 57.0k | session->pps_thread.context = (void *)session; |
468 | | |
469 | 57.0k | session->opentime = time(NULL); |
470 | 57.0k | } |
471 | | |
472 | | /* |
473 | | * strip "user@example.com:password@" from a uri (device path). |
474 | | * better not be an @ after the host name. |
475 | | * |
476 | | * Needs proper tests. |
477 | | */ |
478 | | const char *obfuscate_uri(char *dest, const char *uri, size_t dest_sz) |
479 | 4 | { |
480 | 4 | const char *at = NULL; |
481 | 4 | const char *last_at = NULL; |
482 | | |
483 | | // Find the protocol separator |
484 | 4 | const char* proto_end = strstr(uri, "://"); |
485 | 4 | if (!proto_end) { |
486 | | // none |
487 | 4 | return stpncpy(dest, uri, dest_sz); |
488 | 4 | } |
489 | | |
490 | 0 | at = proto_end + 3; |
491 | 0 | last_at = NULL; |
492 | 0 | while (at && |
493 | 0 | '\0' != *at) { |
494 | 0 | at = strchr(at, '@'); |
495 | 0 | if (!at) { |
496 | 0 | break; |
497 | 0 | } |
498 | 0 | last_at = at; |
499 | 0 | at++; |
500 | 0 | } |
501 | 0 | if (!last_at) { |
502 | | // No credentials, |
503 | 0 | return stpncpy(dest, uri, dest_sz); |
504 | 0 | } |
505 | | |
506 | 0 | (void)snprintf(dest, dest_sz, "%.*sUSER:PASS@%s", |
507 | 0 | (int)(3 + proto_end - uri), uri, last_at + 1); |
508 | |
|
509 | 0 | return dest; |
510 | 0 | } |
511 | | |
512 | | /* split s into host and service parts |
513 | | * if service is not specified, *service is assigned to NULL |
514 | | * device is currently always assigned to NULL |
515 | | * return: -1 on error, 0 otherwise |
516 | | */ |
517 | | int parse_uri_dest(char *s, char **host, char **service, char **device) |
518 | 45 | { |
519 | 45 | char *search = s; |
520 | | |
521 | 45 | if ('[' == s[0]) { |
522 | | // IPv6 literal |
523 | 2 | char *cb = strchr(s, ']'); |
524 | 2 | if (NULL == cb) { |
525 | | // missing terminating ] |
526 | 1 | return -1; |
527 | 1 | } |
528 | 1 | *cb = '\0'; |
529 | 1 | *host = s + 1; |
530 | 1 | search = cb + 1; |
531 | 43 | } else { |
532 | | // IPv4 literal, or hostname |
533 | 43 | *host = s; |
534 | 43 | } |
535 | 44 | s = strchr(search, ':'); |
536 | 44 | if (NULL != s) { |
537 | | // found a colon, remove it from host |
538 | 24 | *s = '\0'; |
539 | 24 | search = s + 1; |
540 | 24 | if ('\0' != s[1] && ':' != s[1]) { |
541 | | // s[1] start port/service |
542 | 14 | *service = s + 1; |
543 | 14 | } else { |
544 | 10 | *service = NULL; |
545 | 10 | } |
546 | 24 | } else { |
547 | 20 | *service = NULL; |
548 | 20 | } |
549 | 44 | s = strchr(search, ':'); |
550 | 44 | if (NULL != s) { |
551 | | // found a colon, remove it |
552 | 9 | *s = '\0'; |
553 | 9 | if ('\0' != s[1]) { |
554 | 8 | *device = s + 1; |
555 | 8 | } else { |
556 | 1 | *device = NULL; |
557 | 1 | } |
558 | 35 | } else { |
559 | 35 | *device = NULL; |
560 | 35 | } |
561 | | /* Support trailing / in URIs, e.g. tcp://192.168.100.90:1234/ |
562 | | * Assumes / is not valid _inside_ host or service parts of URI |
563 | | * Accepts strange input, e.g. service part with / but no digits |
564 | | */ |
565 | 44 | s = strchr(*host, '/'); |
566 | 44 | if (NULL != s) { |
567 | | // found a backslash, remove it |
568 | 1 | *s = '\0'; |
569 | 1 | } |
570 | | /* Don't enforce that / should not be in both host and service |
571 | | * Or that if host has /, there shouldn't be any service at all */ |
572 | 44 | if (NULL != *service) { |
573 | 14 | s = strchr(*service, '/'); |
574 | 14 | if (NULL != s) { |
575 | | // found a backslash, remove it |
576 | 1 | *s = '\0'; |
577 | 1 | } |
578 | | // nothing in service besides / --> trigger use of default port |
579 | 14 | if ('\0' == *service[0]) { |
580 | 1 | *service = NULL; |
581 | 1 | } |
582 | 14 | } |
583 | 44 | return 0; |
584 | 45 | } |
585 | | |
586 | | /* open a device for access to its data * |
587 | | * return: the opened file descriptor |
588 | | * PLACEHOLDING_FD (-2) - for /dev/ppsX, ntrip waiting reconnect, etc. |
589 | | * UNALLOCATED_FD (-1) - for open failure |
590 | | */ |
591 | | int gpsd_open(struct gps_device_t *session) |
592 | 0 | { |
593 | | // cast for 32-bit ints |
594 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
595 | 0 | "CORE: gpsd_open(%s) fld %ld\n", |
596 | 0 | session->gpsdata.dev.path, |
597 | 0 | (long)session->gpsdata.gps_fd); |
598 | | |
599 | | // special case: source may be a URI to a remote GNSS or DGPS service |
600 | 0 | if (netgnss_uri_check(session->gpsdata.dev.path)) { |
601 | 0 | session->gpsdata.gps_fd = netgnss_uri_open(session, |
602 | 0 | session->gpsdata.dev.path); |
603 | 0 | session->sourcetype = SOURCE_TCP; |
604 | | // cast for 32-bit ints. |
605 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
606 | 0 | "CORE: netgnss_uri_open(%s) returns socket on fd %ld\n", |
607 | 0 | session->gpsdata.dev.path, (long)session->gpsdata.gps_fd); |
608 | 0 | return (int)session->gpsdata.gps_fd; |
609 | | // otherwise, could be an TCP data feed |
610 | 0 | } else if (str_starts_with(session->gpsdata.dev.path, "tcp://")) { |
611 | 0 | char server[GPS_PATH_MAX], *host, *port, *device; |
612 | 0 | socket_t dsock; |
613 | 0 | char addrbuf[50]; // INET6_ADDRSTRLEN |
614 | |
|
615 | 0 | session->sourcetype = SOURCE_TCP; |
616 | 0 | (void)strlcpy(server, session->gpsdata.dev.path + 6, sizeof(server)); |
617 | 0 | INVALIDATE_SOCKET(session->gpsdata.gps_fd); |
618 | 0 | if (-1 == parse_uri_dest(server, &host, &port, &device) || |
619 | 0 | !port) { |
620 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
621 | 0 | "CORE: Missing service in TCP feed spec %s\n", |
622 | 0 | session->gpsdata.dev.path); |
623 | 0 | return UNALLOCATED_FD ; |
624 | 0 | } |
625 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
626 | 0 | "CORE: opening TCP feed at %s, port %s.\n", host, |
627 | 0 | port); |
628 | | // open non-blocking |
629 | 0 | dsock = netlib_connectsock1(AF_UNSPEC, host, port, "tcp", |
630 | 0 | 1, false, addrbuf, sizeof(addrbuf)); |
631 | 0 | if (0 > dsock) { |
632 | | // cast for 32-bit ints. |
633 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
634 | 0 | "CORE: TCP %s IP %s, open error %s(%ld).\n", |
635 | 0 | session->gpsdata.dev.path, addrbuf, |
636 | 0 | netlib_errstr(dsock), (long)dsock); |
637 | 0 | } else { |
638 | | // cast for 32-bit ints. |
639 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
640 | 0 | "CORE: TCP %s IP %s opened on fd %ld\n", |
641 | 0 | session->gpsdata.dev.path, addrbuf, (long)dsock); |
642 | 0 | } |
643 | 0 | session->gpsdata.gps_fd = dsock; |
644 | 0 | return session->gpsdata.gps_fd; |
645 | | // or could be UDP |
646 | 0 | } else if (str_starts_with(session->gpsdata.dev.path, "udp://")) { |
647 | 0 | char server[GPS_PATH_MAX], *host, *port, *device; |
648 | 0 | socket_t dsock; |
649 | |
|
650 | 0 | session->sourcetype = SOURCE_UDP; |
651 | 0 | (void)strlcpy(server, session->gpsdata.dev.path + 6, sizeof(server)); |
652 | 0 | INVALIDATE_SOCKET(session->gpsdata.gps_fd); |
653 | 0 | if (-1 == parse_uri_dest(server, &host, &port, &device) || |
654 | 0 | !port) { |
655 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
656 | 0 | "CORE: Missing service in UDP feed spec.\n"); |
657 | 0 | return -1; |
658 | 0 | } |
659 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
660 | 0 | "CORE: opening UDP feed at %s, port %s.\n", host, |
661 | 0 | port); |
662 | 0 | if (0 > (dsock = netlib_connectsock1(AF_UNSPEC, host, port, "udp", |
663 | 0 | 1, true, NULL, 0))) { |
664 | | // cast for 32-bit ints. |
665 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
666 | 0 | "CORE: UDP device open error %s(%ld).\n", |
667 | 0 | netlib_errstr(dsock), (long)dsock); |
668 | 0 | return -1; |
669 | 0 | } else { |
670 | | // cast for 32-bit ints. |
671 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
672 | 0 | "CORE: UDP device opened on fd %ld\n", (long)dsock); |
673 | 0 | } |
674 | 0 | session->gpsdata.gps_fd = dsock; |
675 | 0 | return session->gpsdata.gps_fd; |
676 | 0 | } |
677 | 0 | if (str_starts_with(session->gpsdata.dev.path, "gpsd://")) { |
678 | | /* could be: |
679 | | * gpsd://[ipv6] |
680 | | * gpsd://ipv4 |
681 | | * gpsd://hostname |
682 | | * gpsd://[ipv6]:port |
683 | | * gpsd://ipv4:port |
684 | | * gpsd://hostname:port |
685 | | * gpsd://[ipv6]:port:/device |
686 | | * gpsd://ipv4:port:/device |
687 | | * gpsd://hostname:port:/device |
688 | | * gpsd://[ipv6]::/device |
689 | | * gpsd://ipv4::/device |
690 | | * gpsd://hostname::/device |
691 | | */ |
692 | 0 | char server[GPS_PATH_MAX], *host, *port, *device; |
693 | 0 | socket_t dsock; |
694 | |
|
695 | 0 | session->sourcetype = SOURCE_GPSD; |
696 | 0 | (void)strlcpy(server, session->gpsdata.dev.path + 7, sizeof(server)); |
697 | 0 | INVALIDATE_SOCKET(session->gpsdata.gps_fd); |
698 | 0 | if (-1 == parse_uri_dest(server, &host, &port, &device)) { |
699 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
700 | 0 | "CORE: Malformed URI specified.\n"); |
701 | 0 | return -1; |
702 | 0 | } |
703 | 0 | if (!port) { |
704 | 0 | port = DEFAULT_GPSD_PORT; |
705 | 0 | } |
706 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
707 | 0 | "CORE: opening remote gpsd feed at %s, port %s.\n", |
708 | 0 | host, port); |
709 | 0 | if (0 > (dsock = netlib_connectsock(AF_UNSPEC, host, port, "tcp"))) { |
710 | | // cast for 32-bit ints. |
711 | 0 | GPSD_LOG(LOG_ERROR, &session->context->errout, |
712 | 0 | "CORE: remote gpsd device open error %s(%ld).\n", |
713 | 0 | netlib_errstr(dsock), (long)dsock); |
714 | 0 | return -1; |
715 | 0 | } // else |
716 | | // cast for 32-bit ints. |
717 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
718 | 0 | "CORE: remote gpsd feed opened on fd %ld\n", (long) dsock); |
719 | | |
720 | | // watch to remote is issued when WATCH is |
721 | 0 | session->gpsdata.gps_fd = dsock; |
722 | 0 | return session->gpsdata.gps_fd; |
723 | 0 | } |
724 | 0 | #if defined(NMEA2000_ENABLE) |
725 | 0 | if (str_starts_with(session->gpsdata.dev.path, "nmea2000://")) { |
726 | 0 | return nmea2000_open(session); |
727 | 0 | } |
728 | 0 | #endif // defined(NMEA2000_ENABLE) |
729 | | /* fall through to plain serial open. |
730 | | * could be a naked /dev/ppsX */ |
731 | 0 | return gpsd_serial_open(session); |
732 | 0 | } |
733 | | |
734 | | /* acquire a connection to the GPS device |
735 | | * could be serial, udp://, tcp://, etc. |
736 | | * |
737 | | * Return: fd on success |
738 | | * less than zero on failure |
739 | | * UNALLOCATED_FD (-1) -- give up |
740 | | * PLACEHOLDING_FD (-2) -- retry possible |
741 | | */ |
742 | | int gpsd_activate(struct gps_device_t *session, const int mode) |
743 | 0 | { |
744 | | // cast for 32-bit ints |
745 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
746 | 0 | "CORE: gpsd_activate(%s, %d) fd %ld\n", |
747 | 0 | session->gpsdata.dev.path, mode, |
748 | 0 | (long)session->gpsdata.gps_fd); |
749 | |
|
750 | 0 | if (O_OPTIMIZE == mode) { |
751 | 0 | gpsd_run_device_hook(&session->context->errout, |
752 | 0 | session->gpsdata.dev.path, HOOK_ACTIVATE); |
753 | 0 | } |
754 | 0 | session->gpsdata.gps_fd = (gps_fd_t)gpsd_open(session); |
755 | 0 | if (O_CONTINUE != mode) { |
756 | 0 | session->mode = mode; |
757 | 0 | } |
758 | |
|
759 | 0 | if (0 > session->gpsdata.gps_fd) { |
760 | | // return could be -1, PLACEHOLDING_FD, of UNALLOCATED_FD |
761 | | // could be ntrip:// reconnect in progress |
762 | 0 | if (PLACEHOLDING_FD == session->gpsdata.gps_fd && |
763 | 0 | SOURCE_PPS == session->sourcetype && |
764 | 0 | NULL == session->pps_thread.report_hook) { |
765 | | /* it is /dev/ppsX, need to set devicename, etc. |
766 | | * check report_hook to ensure not already running |
767 | | * cast for 32-bit ints */ |
768 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
769 | 0 | "CORE: to gpsd_clear() fd %ld\n", |
770 | 0 | (long)session->gpsdata.gps_fd); |
771 | 0 | gpsd_clear(session); |
772 | 0 | } |
773 | 0 | return session->gpsdata.gps_fd; |
774 | 0 | } |
775 | | |
776 | | // if it's a sensor, it must be probed |
777 | 0 | if ((SERVICE_SENSOR == session->servicetype) && |
778 | 0 | (SOURCE_CAN != session->sourcetype)) { |
779 | 0 | const struct gps_type_t **dp; |
780 | |
|
781 | 0 | for (dp = gpsd_drivers; *dp; dp++) { |
782 | 0 | if (NULL != (*dp)->probe_detect) { |
783 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
784 | 0 | "CORE: Probing \"%s\" driver...\n", |
785 | 0 | (*dp)->type_name); |
786 | | // toss stale data |
787 | 0 | (void)tcflush(session->gpsdata.gps_fd, TCIOFLUSH); |
788 | 0 | if (0 != (*dp)->probe_detect(session)) { |
789 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
790 | 0 | "CORE: Probe found \"%s\" driver...\n", |
791 | 0 | (*dp)->type_name); |
792 | 0 | session->device_type = *dp; |
793 | 0 | gpsd_assert_sync(session); |
794 | 0 | goto foundit; |
795 | 0 | } else { |
796 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
797 | 0 | "CORE: Probe not found \"%s\" driver...\n", |
798 | 0 | (*dp)->type_name); |
799 | 0 | } |
800 | 0 | } |
801 | 0 | } |
802 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
803 | 0 | "CORE: no probe matched...\n"); |
804 | 0 | } |
805 | 0 | foundit: |
806 | |
|
807 | 0 | gpsd_clear(session); |
808 | | /* |
809 | | * We might know the device's type, but we shouldn't assume it has |
810 | | * retained its settings. A revert hook might well have undone |
811 | | * them on the previous close. Fire a reactivate event so drivers |
812 | | * can do something about this if they choose. |
813 | | */ |
814 | 0 | if (NULL != session->device_type && |
815 | 0 | NULL != session->device_type->event_hook) { |
816 | 0 | session->device_type->event_hook(session, EVENT_REACTIVATE); |
817 | 0 | } |
818 | | // cast for 32-bit ints |
819 | 0 | GPSD_LOG(LOG_PROG, &session->context->errout, |
820 | 0 | "CORE: activate fd %ld done\n", |
821 | 0 | (long)session->gpsdata.gps_fd); |
822 | |
|
823 | 0 | return session->gpsdata.gps_fd; |
824 | 0 | } |
825 | | |
826 | | |
827 | | /***************************************************************************** |
828 | | |
829 | | Carl Carter of SiRF supplied this algorithm for computing DOPs from |
830 | | a list of visible satellites (some typos corrected)... |
831 | | |
832 | | For satellite n, let az(n) = azimuth angle from North and el(n) be elevation. |
833 | | Let: |
834 | | |
835 | | a(k, 1) = sin az(k) * cos el(k) |
836 | | a(k, 2) = cos az(k) * cos el(k) |
837 | | a(k, 3) = sin el(k) |
838 | | |
839 | | Then form the line-of-sight matrix A for satellites used in the solution: |
840 | | |
841 | | | a(1,1) a(1,2) a(1,3) 1 | |
842 | | | a(2,1) a(2,2) a(2,3) 1 | |
843 | | | : : : : | |
844 | | | a(n,1) a(n,2) a(n,3) 1 | |
845 | | |
846 | | And its transpose A~: |
847 | | |
848 | | |a(1, 1) a(2, 1) . . . a(n, 1) | |
849 | | |a(1, 2) a(2, 2) . . . a(n, 2) | |
850 | | |a(1, 3) a(2, 3) . . . a(n, 3) | |
851 | | | 1 1 . . . 1 | |
852 | | |
853 | | Compute the covariance matrix (A~*A)^-1, which is guaranteed symmetric: |
854 | | |
855 | | | s(x)^2 s(x)*s(y) s(x)*s(z) s(x)*s(t) | |
856 | | | s(y)*s(x) s(y)^2 s(y)*s(z) s(y)*s(t) | |
857 | | | s(z)*s(x) s(z)*s(y) s(z)^2 s(z)*s(t) | |
858 | | | s(t)*s(x) s(t)*s(y) s(t)*s(z) s(t)^2 | |
859 | | |
860 | | Then: |
861 | | |
862 | | GDOP = sqrt(s(x)^2 + s(y)^2 + s(z)^2 + s(t)^2) |
863 | | TDOP = sqrt(s(t)^2) |
864 | | PDOP = sqrt(s(x)^2 + s(y)^2 + s(z)^2) |
865 | | HDOP = sqrt(s(x)^2 + s(y)^2) |
866 | | VDOP = sqrt(s(z)^2) |
867 | | |
868 | | Here's how we implement it... |
869 | | |
870 | | First, each compute element P(i,j) of the 4x4 product A~*A. |
871 | | If S(k=1,k=n): f(...) is the sum of f(...) as k varies from 1 to n, then |
872 | | applying the definition of matrix product tells us: |
873 | | |
874 | | P(i,j) = S(k=1,k=n): B(i, k) * A(k, j) |
875 | | |
876 | | But because B is the transpose of A, this reduces to |
877 | | |
878 | | P(i,j) = S(k=1,k=n): A(k, i) * A(k, j) |
879 | | |
880 | | This is not, however, the entire algorithm that SiRF uses. Carl writes: |
881 | | |
882 | | > As you note, with rounding accounted for, most values agree exactly, and |
883 | | > those that don't agree our number is higher. That is because we |
884 | | > deweight some satellites and account for that in the DOP calculation. |
885 | | > If a satellite is not used in a solution at the same weight as others, |
886 | | > it should not contribute to DOP calculation at the same weight. So our |
887 | | > internal algorithm does a compensation for that which you would have no |
888 | | > way to duplicate on the outside since we don't output the weighting |
889 | | > factors. In fact those are not even available to API users. |
890 | | |
891 | | Queried about the deweighting, Carl says: |
892 | | |
893 | | > In the SiRF tracking engine, each satellite track is assigned a quality |
894 | | > value based on the tracker's estimate of that signal. It includes C/No |
895 | | > estimate, ability to hold onto the phase, stability of the I vs. Q phase |
896 | | > angle, etc. The navigation algorithm then ranks all the tracks into |
897 | | > quality order and selects which ones to use in the solution and what |
898 | | > weight to give those used in the solution. The process is actually a |
899 | | > bit of a "trial and error" method -- we initially use all available |
900 | | > tracks in the solution, then we sequentially remove the lowest quality |
901 | | > ones until the solution stabilizes. The weighting is inherent in the |
902 | | > Kalman filter algorithm. Once the solution is stable, the DOP is |
903 | | > computed from those SVs used, and there is an algorithm that looks at |
904 | | > the quality ratings and determines if we need to deweight any. |
905 | | > Likewise, if we use altitude hold mode for a 3-SV solution, we deweight |
906 | | > the phantom satellite at the center of the Earth. |
907 | | |
908 | | So we cannot exactly duplicate what SiRF does internally. We'll leave |
909 | | HDOP alone and use our computed values for VDOP and PDOP. Note, this |
910 | | may have to change in the future if this code is used by a non-SiRF |
911 | | driver. |
912 | | |
913 | | ******************************************************************************/ |
914 | | |
915 | | |
916 | | static gps_mask_t fill_dop(const struct gpsd_errout_t *errout, |
917 | | const struct gps_data_t * gpsdata, |
918 | | struct dop_t * dop) |
919 | 18.1k | { |
920 | 18.1k | double prod[4][4] = {{0}}; |
921 | 18.1k | double inv[4][4] = {{0}}; |
922 | 18.1k | double satpos[MAXCHANNELS][4] = {{0}}; |
923 | 18.1k | double xdop, ydop, hdop, vdop, pdop, tdop, gdop; |
924 | 18.1k | unsigned i, j, k, n; |
925 | 18.1k | unsigned max_skyview; |
926 | | |
927 | | // guard against malicisous fuzzers deep linking past initialization |
928 | | // https://issues.oss-fuzz.com/issues/480975802 |
929 | 18.1k | if (ROWS(gpsdata->skyview) < gpsdata->satellites_visible) { |
930 | 0 | max_skyview = ROWS(gpsdata->skyview); |
931 | 18.1k | } else { |
932 | 18.1k | max_skyview = gpsdata->satellites_visible; |
933 | 18.1k | } |
934 | | |
935 | 432k | for (n = k = 0; k < max_skyview; k++) { |
936 | | // This double counts single sats where we got 2 signals from them. |
937 | | #ifdef __UNUSED__ |
938 | | GPSD_LOG(LOG_IO, errout, |
939 | | "CORE: PRN %d used %d az %.1f el %.1f\n", |
940 | | gpsdata->skyview[k].PRN, |
941 | | gpsdata->skyview[k].used, |
942 | | gpsdata->skyview[k].azimuth, |
943 | | gpsdata->skyview[k].elevation); |
944 | | #endif // __UNUSED__ |
945 | 414k | if (!gpsdata->skyview[k].used) { |
946 | | // skip unused sats |
947 | 367k | continue; |
948 | 367k | } |
949 | 46.4k | if (1 > gpsdata->skyview[k].PRN) { |
950 | | // skip bad PRN |
951 | 3.16k | continue; |
952 | 3.16k | } |
953 | 43.2k | if (0 == isfinite(gpsdata->skyview[k].azimuth) || |
954 | 27.1k | 0 > gpsdata->skyview[k].azimuth || |
955 | 25.1k | 359 < gpsdata->skyview[k].azimuth) { |
956 | | // skip bad azimuth |
957 | 23.2k | continue; |
958 | 23.2k | } |
959 | 20.0k | if (0 == isfinite(gpsdata->skyview[k].elevation) || |
960 | 16.1k | 90 < fabs(gpsdata->skyview[k].elevation)) { |
961 | | // skip bad elevation |
962 | 6.26k | continue; |
963 | 6.26k | } |
964 | 13.7k | const struct satellite_t *sp = &gpsdata->skyview[k]; |
965 | 13.7k | satpos[n][0] = sin(sp->azimuth * DEG_2_RAD) |
966 | 13.7k | * cos(sp->elevation * DEG_2_RAD); |
967 | 13.7k | satpos[n][1] = cos(sp->azimuth * DEG_2_RAD) |
968 | 13.7k | * cos(sp->elevation * DEG_2_RAD); |
969 | 13.7k | satpos[n][2] = sin(sp->elevation * DEG_2_RAD); |
970 | 13.7k | satpos[n][3] = 1; |
971 | 13.7k | GPSD_LOG(LOG_INF, errout, |
972 | 13.7k | "CORE: PRN %3d az %5.1f el %4.1f (%9.6f, %9.6f, %9.6f)\n", |
973 | 13.7k | gpsdata->skyview[k].PRN, |
974 | 13.7k | gpsdata->skyview[k].azimuth, |
975 | 13.7k | gpsdata->skyview[k].elevation, |
976 | 13.7k | satpos[n][0], satpos[n][1], satpos[n][2]); |
977 | 13.7k | n++; |
978 | 13.7k | } |
979 | | /* can't use gpsdata->satellites_used as that is a counter for xxGSA, |
980 | | * and gets cleared at odd times */ |
981 | 18.1k | GPSD_LOG(LOG_INF, errout, "CORE: Sats used %d visible %d:\n", |
982 | 18.1k | n, gpsdata->satellites_visible); |
983 | | |
984 | | /* If we don't have 4 satellites then we don't have enough |
985 | | * information to calculate DOPS */ |
986 | 18.1k | if (n < 4) { |
987 | | #ifdef __UNUSED__ |
988 | | GPSD_LOG(LOG_RAW, errout, |
989 | | "CORE: Not enough satellites available %d < 4:\n", n); |
990 | | #endif // __UNUSED__ |
991 | | // Is this correct return code here? or should it be ERROR_SET |
992 | 16.8k | return 0; |
993 | 16.8k | } |
994 | | |
995 | | #ifdef __UNUSED__ |
996 | | GPSD_LOG(LOG_INF, errout, "CORE: Line-of-sight matrix:\n"); |
997 | | for (k = 0; k < n; k++) { |
998 | | GPSD_LOG(LOG_INF, errout, "CORE: %f %f %f %f\n", |
999 | | satpos[k][0], satpos[k][1], satpos[k][2], satpos[k][3]); |
1000 | | } |
1001 | | #endif // __UNUSED__ |
1002 | | |
1003 | 6.68k | for (i = 0; i < 4; ++i) { // < rows |
1004 | 26.7k | for (j = 0; j < 4; ++j) { // < cols |
1005 | 21.3k | prod[i][j] = 0.0; |
1006 | 200k | for (k = 0; k < n; ++k) { |
1007 | 178k | prod[i][j] += satpos[k][i] * satpos[k][j]; |
1008 | 178k | } |
1009 | 21.3k | } |
1010 | 5.34k | } |
1011 | | |
1012 | | #ifdef __UNUSED__ |
1013 | | GPSD_LOG(LOG_INF, errout, "CORE: product:\n"); |
1014 | | for (k = 0; k < 4; k++) { |
1015 | | GPSD_LOG(LOG_INF, errout, "CORE: %f %f %f %f\n", |
1016 | | prod[k][0], prod[k][1], prod[k][2], prod[k][3]); |
1017 | | } |
1018 | | #endif // __UNUSED__ |
1019 | | |
1020 | 1.33k | if (matrix_invert(prod, inv)) { |
1021 | | #ifdef __UNUSED__ |
1022 | | /* |
1023 | | * Note: this will print garbage unless all the subdeterminants |
1024 | | * are computed in the invert() function. |
1025 | | */ |
1026 | | GPSD_LOG(LOG_RAW, errout, "CORE: inverse:\n"); |
1027 | | for (k = 0; k < 4; k++) { |
1028 | | GPSD_LOG(LOG_RAW, errout, |
1029 | | "CORE: %f %f %f %f\n", |
1030 | | inv[k][0], inv[k][1], inv[k][2], inv[k][3]); |
1031 | | } |
1032 | | #endif // __UNUSED__ |
1033 | 941 | } else { |
1034 | 396 | GPSD_LOG(LOG_DATA, errout, |
1035 | 396 | "CORE: LOS matrix singular, DOPs fail - source '%s'\n", |
1036 | 396 | gpsdata->dev.path); |
1037 | 396 | return 0; |
1038 | 396 | } |
1039 | | |
1040 | 941 | xdop = sqrt(inv[0][0]); |
1041 | 941 | ydop = sqrt(inv[1][1]); |
1042 | 941 | hdop = sqrt(inv[0][0] + inv[1][1]); |
1043 | 941 | vdop = sqrt(inv[2][2]); |
1044 | 941 | pdop = sqrt(inv[0][0] + inv[1][1] + inv[2][2]); |
1045 | 941 | tdop = sqrt(inv[3][3]); |
1046 | 941 | gdop = sqrt(inv[0][0] + inv[1][1] + inv[2][2] + inv[3][3]); |
1047 | | |
1048 | 941 | GPSD_LOG(LOG_DATA, errout, |
1049 | 941 | "CORE: DOPS computed/reported: X=%f/%f Y=%f/%f H=%f/%f " |
1050 | 941 | "V=%f/%f P=%f/%f T=%f/%f G=%f/%f\n", |
1051 | 941 | xdop, dop->xdop, ydop, dop->ydop, hdop, dop->hdop, vdop, |
1052 | 941 | dop->vdop, pdop, dop->pdop, tdop, dop->tdop, gdop, dop->gdop); |
1053 | | |
1054 | | /* Check to see which DOPs we already have. Save values if no value |
1055 | | * from the GPS. Do not overwrite values which came from the GPS */ |
1056 | 941 | if (0 == isfinite(dop->xdop)) { |
1057 | 246 | dop->xdop = xdop; |
1058 | 246 | } |
1059 | 941 | if (0 == isfinite(dop->ydop)) { |
1060 | 246 | dop->ydop = ydop; |
1061 | 246 | } |
1062 | 941 | if (0 == isfinite(dop->hdop)) { |
1063 | 245 | dop->hdop = hdop; |
1064 | 245 | } |
1065 | 941 | if (0 == isfinite(dop->vdop)) { |
1066 | 245 | dop->vdop = vdop; |
1067 | 245 | } |
1068 | 941 | if (0 == isfinite(dop->pdop)) { |
1069 | 245 | dop->pdop = pdop; |
1070 | 245 | } |
1071 | 941 | if (0 == isfinite(dop->tdop)) { |
1072 | 245 | dop->tdop = tdop; |
1073 | 245 | } |
1074 | 941 | if (0 == isfinite(dop->gdop)) { |
1075 | 245 | dop->gdop = gdop; |
1076 | 245 | } |
1077 | | |
1078 | 941 | return DOP_SET; |
1079 | 1.33k | } |
1080 | | |
1081 | | /* compute errors and derived quantities |
1082 | | * also a handy place to do final sanity checking */ |
1083 | | static void gpsd_error_model(struct gps_device_t *session) |
1084 | 399k | { |
1085 | 399k | struct gps_fix_t *fix; // current fix |
1086 | 399k | struct gps_fix_t *lastfix; // last fix, maybe same time stamp |
1087 | 399k | struct gps_fix_t *oldfix; // old fix, previous time stamp |
1088 | 399k | struct gps_fix_t *newfix; // new fix (just merged) |
1089 | 399k | double deltatime = -1.0; // time span to compute rates |
1090 | | |
1091 | | /* |
1092 | | * Now we compute derived quantities. This is where the tricky error- |
1093 | | * modeling stuff goes. Presently we don't know how to derive |
1094 | | * time error. |
1095 | | * |
1096 | | * Some drivers set the error fields. No NMEA 183 reports climb error. |
1097 | | * $GPXTE and $PSRFEPE can report track error, but are rare. Whenever |
1098 | | * possible, we step aside and allow the GNSS receiver error estimates |
1099 | | * to be used. But even they are only Wild Ass Guesses (WAGs). |
1100 | | * |
1101 | | * The UERE constants are our assumption about the base error of |
1102 | | * GPS fixes in different directions. |
1103 | | * |
1104 | | * UERE is actually a variable sent in the Almanac, so assuming |
1105 | | * a UERE constant is bogus, as is using it this way. |
1106 | | * |
1107 | | * Assuming that DGPS has substantially better accuracy than plain |
1108 | | * GPS is also a fallacy. Extending this to RTK is building false |
1109 | | * conjecture on top of misplaced wishful thinking. |
1110 | | */ |
1111 | 399k | #define H_UERE_NO_DGPS 15.0 // meters, 95% confidence |
1112 | 399k | #define H_UERE_WITH_DGPS 3.75 // meters, 95% confidence |
1113 | 399k | #define V_UERE_NO_DGPS 23.0 // meters, 95% confidence |
1114 | 399k | #define V_UERE_WITH_DGPS 5.75 // meters, 95% confidence |
1115 | 399k | #define P_UERE_NO_DGPS 19.0 // meters, 95% confidence |
1116 | 399k | #define P_UERE_WITH_DGPS 4.75 // meters, 95% confidence |
1117 | 399k | double h_uere, v_uere, p_uere; |
1118 | | |
1119 | 399k | if (NULL == session) { |
1120 | 0 | return; |
1121 | 0 | } |
1122 | | |
1123 | 399k | fix = &session->gpsdata.fix; |
1124 | 399k | lastfix = &session->lastfix; |
1125 | 399k | oldfix = &session->oldfix; |
1126 | 399k | newfix = &session->newdata; // For whether rcvr supplies values |
1127 | | |
1128 | 399k | if (0 < fix->time.tv_sec) { |
1129 | | // we have a time for this merge data |
1130 | | |
1131 | 28.9k | deltatime = TS_SUB_D(&fix->time, &lastfix->time); |
1132 | | |
1133 | 28.9k | if (0.0099 < fabs(deltatime)) { |
1134 | | /* Time just moved, probably forward at least 10 ms. |
1135 | | * Lastfix is now the previous (old) fix. */ |
1136 | 9.60k | *oldfix = *lastfix; |
1137 | 19.3k | } else { |
1138 | | // compute delta from old fix |
1139 | 19.3k | deltatime = TS_SUB_D(&fix->time, &oldfix->time); |
1140 | 19.3k | } |
1141 | 28.9k | } |
1142 | | // Sanity check for negative delta? |
1143 | | |
1144 | | // adjusting UERE for DGPS is dodgy... |
1145 | 399k | h_uere = |
1146 | 399k | (session->gpsdata.fix.status == |
1147 | 399k | STATUS_DGPS ? H_UERE_WITH_DGPS : H_UERE_NO_DGPS); |
1148 | 399k | v_uere = |
1149 | 399k | (session->gpsdata.fix.status == |
1150 | 399k | STATUS_DGPS ? V_UERE_WITH_DGPS : V_UERE_NO_DGPS); |
1151 | 399k | p_uere = |
1152 | 399k | (session->gpsdata.fix.status == |
1153 | 399k | STATUS_DGPS ? P_UERE_WITH_DGPS : P_UERE_NO_DGPS); |
1154 | | |
1155 | 399k | if (0 == isfinite(fix->latitude) || |
1156 | 24.2k | 0 == isfinite(fix->longitude) || // both lat/lon, or none |
1157 | 23.7k | 90.0 < fabs(fix->latitude) || // lat out of range |
1158 | 382k | 180.0 < fabs(fix->longitude)) { // lon out of range |
1159 | 382k | fix->latitude = fix->longitude = NAN; |
1160 | 382k | } |
1161 | | // validate ECEF |
1162 | 399k | if (0 == isfinite(fix->ecef.x) || |
1163 | 9.81k | 0 == isfinite(fix->ecef.y) || |
1164 | 9.25k | 0 == isfinite(fix->ecef.z) || |
1165 | 8.89k | 10.0 >= (fabs(fix->ecef.x) + |
1166 | 8.89k | fabs(fix->ecef.y) + |
1167 | 391k | fabs(fix->ecef.z))) { // all zeros |
1168 | 391k | fix->ecef.x = fix->ecef.y = fix->ecef.z = NAN; |
1169 | 391k | } |
1170 | | |
1171 | | // if we have not lat/lon, but do have ECEF, calculate lat/lon |
1172 | 399k | if ((0 == isfinite(fix->longitude) || |
1173 | 17.2k | 0 == isfinite(fix->latitude)) && |
1174 | 382k | 0 != isfinite(fix->ecef.x)) { |
1175 | 4.42k | session->gpsdata.set |= ecef_to_wgs84fix(fix, |
1176 | 4.42k | fix->ecef.x, fix->ecef.y, |
1177 | 4.42k | fix->ecef.z, fix->ecef.vx, |
1178 | 4.42k | fix->ecef.vy, fix->ecef.vz); |
1179 | 4.42k | } |
1180 | | |
1181 | | /* If you are in a rocket, and your GPS is ITAR unlocked, then |
1182 | | * triple check these sanity checks. |
1183 | | * |
1184 | | * u-blox 8: Max altitude: 50,000m |
1185 | | * Max horizontal speed: 250 m/s |
1186 | | * Max climb: 100 m/s |
1187 | | * |
1188 | | * u-blox ZED-F9P: Max Velocity: 500 m/s |
1189 | | */ |
1190 | | |
1191 | | /* sanity check the speed, 10,000 m/s should be a nice max |
1192 | | * Low Earth Orbit (LEO) is about 7,800 m/s */ |
1193 | 399k | if (9999.9 < fabs(fix->speed)) |
1194 | 2.51k | fix->speed = NAN; |
1195 | | |
1196 | 399k | if (9999.9 < fabs(fix->NED.velN)) |
1197 | 2.07k | fix->NED.velN = NAN; |
1198 | 399k | if (9999.9 < fabs(fix->NED.velE)) |
1199 | 2.56k | fix->NED.velE = NAN; |
1200 | 399k | if (9999.9 < fabs(fix->NED.velD)) |
1201 | 2.36k | fix->NED.velD = NAN; |
1202 | | |
1203 | | // sanity check the climb, 10,000 m/s should be a nice max |
1204 | 399k | if (9999.9 < fabs(fix->climb)) |
1205 | 150 | fix->climb = NAN; |
1206 | 399k | if (0 != isfinite(fix->NED.velD) && |
1207 | 8.63k | 0 == isfinite(fix->climb)) { |
1208 | | // have good velD, use it for climb |
1209 | 3.56k | fix->climb = -fix->NED.velD; |
1210 | 3.56k | } |
1211 | | |
1212 | | // compute speed and track from velN and velE if needed and possible |
1213 | 399k | if (0 != isfinite(fix->NED.velN) && |
1214 | 9.09k | 0 != isfinite(fix->NED.velE)) { |
1215 | 7.50k | if (0 == isfinite(fix->speed)) { |
1216 | 3.37k | fix->speed = hypot(fix->NED.velN, fix->NED.velE); |
1217 | 3.37k | } |
1218 | 7.50k | if (0 == isfinite(fix->track)) { |
1219 | 3.20k | fix->track = atan2(fix->NED.velE, fix->NED.velN) * RAD_2_DEG; |
1220 | | // normalized later |
1221 | 3.20k | } |
1222 | 7.50k | } |
1223 | | |
1224 | | /* |
1225 | | * OK, this is not an error computation, but we're at the right |
1226 | | * place in the architecture for it. Compute geoid separation |
1227 | | * and altHAE and altMSL in the simplest possible way. |
1228 | | */ |
1229 | | |
1230 | | // geoid (ellipsoid) separation and variation |
1231 | 399k | if (0 != isfinite(fix->latitude) && |
1232 | 21.6k | 0 != isfinite(fix->longitude)) { |
1233 | 21.6k | if (0 == isfinite(fix->geoid_sep)) { |
1234 | 7.30k | fix->geoid_sep = wgs84_separation(fix->latitude, |
1235 | 7.30k | fix->longitude); |
1236 | 7.30k | } |
1237 | 21.6k | if (0 == isfinite(fix->magnetic_var) || |
1238 | 13.9k | 0.09 >= fabs(fix->magnetic_var)) { |
1239 | | // some GPS set 0.0,E, or 0,W instead of blank |
1240 | 9.62k | fix->magnetic_var = mag_var(fix->latitude, |
1241 | 9.62k | fix->longitude); |
1242 | 9.62k | } |
1243 | 21.6k | } |
1244 | | |
1245 | 399k | if (0 != isfinite(fix->magnetic_var)) { |
1246 | 22.5k | if (0 == isfinite(fix->magnetic_track) && |
1247 | 16.2k | 0 != isfinite(fix->track)) { |
1248 | | |
1249 | | // calculate mag track, normalized later |
1250 | 3.23k | fix->magnetic_track = fix->track + fix->magnetic_var; |
1251 | 19.2k | } else if (0 != isfinite(fix->magnetic_track) && |
1252 | 6.24k | 0 == isfinite(fix->track)) { |
1253 | | |
1254 | | // calculate true track, normalized later |
1255 | 212 | fix->track = fix->magnetic_track - fix->magnetic_var; |
1256 | 212 | } |
1257 | 22.5k | } |
1258 | 399k | if (0 != isfinite(fix->track)) { |
1259 | | // normalize true track |
1260 | 15.9k | DEG_NORM(fix->track); |
1261 | 15.9k | } |
1262 | | |
1263 | 399k | if (0 != isfinite(fix->magnetic_track)) { |
1264 | | // normalize mag track |
1265 | 9.99k | DEG_NORM(fix->magnetic_track); |
1266 | 9.99k | } |
1267 | | |
1268 | 399k | if (0 != isfinite(fix->geoid_sep)) { |
1269 | 23.0k | if (0 != isfinite(fix->altHAE) && |
1270 | 18.1k | 0 == isfinite(fix->altMSL)) { |
1271 | | // compute missing altMSL |
1272 | 5.48k | fix->altMSL = fix->altHAE - fix->geoid_sep; |
1273 | 17.6k | } else if (0 == isfinite(fix->altHAE) && |
1274 | 4.89k | 0 != isfinite(fix->altMSL)) { |
1275 | | // compute missing altHAE |
1276 | 542 | fix->altHAE = fix->altMSL + fix->geoid_sep; |
1277 | 542 | } |
1278 | 23.0k | } |
1279 | | |
1280 | | /* |
1281 | | * OK, this is not an error computation, but we're at the right |
1282 | | * place in the architecture for it. Compute speed over ground |
1283 | | * and climb/sink in the simplest possible way. |
1284 | | */ |
1285 | | |
1286 | | #ifdef __UNUSED__ |
1287 | | // debug code |
1288 | | { |
1289 | | char tbuf[JSON_DATE_MAX+1]; |
1290 | | GPSD_LOG(LOG_SHOUT, &session->context->errout, |
1291 | | "CORE: time %s deltatime %f\n", |
1292 | | timespec_to_iso8601(fix->time, tbuf, sizeof(tbuf)), |
1293 | | deltatime); |
1294 | | } |
1295 | | #endif // __UNUSED__ |
1296 | | |
1297 | 399k | if (0 < deltatime) { |
1298 | | // have a valid time duration |
1299 | | // FIXME! ignore if large. maybe > 1 hour? |
1300 | | |
1301 | 14.9k | if (MODE_2D <= fix->mode && |
1302 | 6.66k | MODE_2D <= oldfix->mode) { |
1303 | | |
1304 | 4.72k | if (0 == isfinite(fix->speed)) { |
1305 | 2.23k | fix->speed = earth_distance(fix->latitude, |
1306 | 2.23k | fix->longitude, |
1307 | 2.23k | oldfix->latitude, |
1308 | 2.23k | oldfix->longitude) / deltatime; |
1309 | | // sanity check |
1310 | 2.23k | if (9999.9 < fabs(fix->speed)) |
1311 | 445 | fix->speed = NAN; |
1312 | 2.23k | } |
1313 | | |
1314 | 4.72k | if (MODE_3D <= fix->mode && |
1315 | 2.97k | MODE_3D <= oldfix->mode && |
1316 | 2.47k | 0 == isfinite(fix->climb) && |
1317 | 1.41k | 0 != isfinite(fix->altHAE) && |
1318 | 844 | 0 != isfinite(oldfix->altHAE)) { |
1319 | 646 | fix->climb = (fix->altHAE - oldfix->altHAE) / deltatime; |
1320 | | |
1321 | | // sanity check the climb |
1322 | 646 | if (9999.9 < fabs(fix->climb)) |
1323 | 420 | fix->climb = NAN; |
1324 | 646 | } |
1325 | 4.72k | } |
1326 | 14.9k | } |
1327 | | |
1328 | | /* |
1329 | | * Field reports match the theoretical prediction that |
1330 | | * expected time error should be half the resolution of |
1331 | | * the GPS clock, so we put the bound of the error |
1332 | | * in as a constant pending getting it from each driver. |
1333 | | * |
1334 | | * In an ideal world, we'd increase this if no leap-second has |
1335 | | * been seen and it's less than 750s (one almanac load cycle) from |
1336 | | * device powerup. Alas, we have no way to know when device |
1337 | | * powerup occurred - depending on the receiver design it could be |
1338 | | * when the hardware was first powered up or when it was first |
1339 | | * opened. Also, some devices (notably plain NMEA0183 receivers) |
1340 | | * never ship an indication of when they have valid leap second. |
1341 | | */ |
1342 | 399k | if (0 < fix->time.tv_sec && |
1343 | 28.9k | 0 == isfinite(fix->ept)) { |
1344 | | // can we compute ept from tdop? |
1345 | 12.0k | fix->ept = 0.005; |
1346 | 12.0k | } |
1347 | | |
1348 | | // Other error computations depend on having a valid fix |
1349 | 399k | if (MODE_2D <= fix->mode) { |
1350 | 31.1k | if (0 == isfinite(newfix->epx) && |
1351 | 30.8k | 0 != isfinite(session->gpsdata.dop.xdop)) { |
1352 | 845 | fix->epx = session->gpsdata.dop.xdop * h_uere; |
1353 | 845 | } |
1354 | | |
1355 | 31.1k | if (0 == isfinite(newfix->epy) && |
1356 | 30.8k | 0 != isfinite(session->gpsdata.dop.ydop)) { |
1357 | 849 | fix->epy = session->gpsdata.dop.ydop * h_uere; |
1358 | 849 | } |
1359 | | |
1360 | 31.1k | if (MODE_3D <= fix->mode && |
1361 | 19.9k | 0 == isfinite(fix->epv) && |
1362 | 16.8k | 0 != isfinite(session->gpsdata.dop.vdop)) { |
1363 | 1.01k | fix->epv = session->gpsdata.dop.vdop * v_uere; |
1364 | 1.01k | } |
1365 | | |
1366 | | // 2D error |
1367 | 31.1k | if (0 == isfinite(fix->eph) && |
1368 | 26.3k | 0 != isfinite(session->gpsdata.dop.hdop)) { |
1369 | 2.13k | fix->eph = session->gpsdata.dop.hdop * p_uere; |
1370 | 2.13k | } |
1371 | | |
1372 | | // 3D error |
1373 | 31.1k | if (0 == isfinite(fix->sep) && |
1374 | 27.3k | 0 != isfinite(session->gpsdata.dop.pdop)) { |
1375 | 1.96k | fix->sep = session->gpsdata.dop.pdop * p_uere; |
1376 | 1.96k | } |
1377 | | |
1378 | | /* |
1379 | | * If we have a current fix and an old fix, and the packet handler |
1380 | | * didn't set the speed error, climb error or track error members |
1381 | | * itself, try to compute them now. |
1382 | | */ |
1383 | | |
1384 | 31.1k | #define EMAX(x, y) (((x) > (y)) ? (x) : (y)) |
1385 | | |
1386 | 31.1k | if (0 < deltatime && |
1387 | 6.66k | MODE_2D <= oldfix->mode) { |
1388 | | |
1389 | 4.72k | if (0 == isfinite(newfix->eps) && |
1390 | 4.22k | 0 != isfinite(oldfix->epx) && |
1391 | 775 | 0 != isfinite(oldfix->epy)) { |
1392 | 640 | fix->eps = (EMAX(oldfix->epx, oldfix->epy) + |
1393 | 640 | EMAX(fix->epx, fix->epy)) / deltatime; |
1394 | 640 | } |
1395 | | |
1396 | 4.72k | if (0 == isfinite(fix->epd)) { |
1397 | | /* |
1398 | | * We compute a track error estimate solely from the |
1399 | | * position of this fix and the last one. The maximum |
1400 | | * track error, as seen from the position of last fix, is |
1401 | | * the angle subtended by the two most extreme possible |
1402 | | * error positions of the current fix; the expected track |
1403 | | * error is half that. Let the position of the old fix be |
1404 | | * A and of the new fix B. We model the view from A as |
1405 | | * two right triangles ABC and ABD with BC and BD both |
1406 | | * having the length of the new fix's estimated error. |
1407 | | * adj = len(AB), opp = len(BC) = len(BD), hyp = len(AC) = |
1408 | | * len(AD). This leads to spurious uncertainties |
1409 | | * near 180 when we're moving slowly; to avoid reporting |
1410 | | * garbage, throw back NaN if the distance from the previous |
1411 | | * fix is less than the error estimate. |
1412 | | */ |
1413 | 4.46k | double adj = earth_distance(oldfix->latitude, oldfix->longitude, |
1414 | 4.46k | fix->latitude, fix->longitude); |
1415 | 4.46k | double opp = EMAX(fix->epx, fix->epy); |
1416 | 4.46k | if (isfinite(adj) != 0 && adj > opp) { |
1417 | 114 | double hyp = sqrt(adj * adj + opp * opp); |
1418 | 114 | fix->epd = RAD_2_DEG * 2 * asin(opp / hyp); |
1419 | 114 | } |
1420 | 4.46k | } |
1421 | | |
1422 | 4.72k | if (0 == isfinite(newfix->epc) && |
1423 | 4.62k | 0 != isfinite(fix->epv) && |
1424 | 1.30k | 0 != isfinite(oldfix->epv)) { |
1425 | | // Is this really valid? |
1426 | | // if vertical uncertainties are zero this will be too |
1427 | 422 | fix->epc = (oldfix->epv + fix->epv) / deltatime; |
1428 | 422 | } |
1429 | 4.72k | } |
1430 | 31.1k | } |
1431 | | |
1432 | | #ifdef __UNUSED__ |
1433 | | { |
1434 | | // Debug code. |
1435 | | char tbuf[JSON_DATE_MAX+1]; |
1436 | | GPSD_LOG(&session->context->errout, 0, |
1437 | | "CORE: %s deltatime %.3f, speed %0.3f climb %.3f " |
1438 | | "epc %.3f fixHAE %.3f oldHAE %.3f\n", |
1439 | | timespec_to_iso8601(fix->time, tbuf, sizeof(tbuf)), |
1440 | | deltatime, fix->speed, fix->climb, fix->epc, |
1441 | | fix->altHAE, oldfix->altHAE); |
1442 | | } |
1443 | | #endif // __UNUSED__ |
1444 | | |
1445 | 399k | if (0 < fix->time.tv_sec) { |
1446 | | // save lastfix, not yet oldfix, for later error computations |
1447 | 28.9k | *lastfix = *fix; |
1448 | 28.9k | } |
1449 | 399k | } |
1450 | | |
1451 | | /* await data from any socket in the all_fds set |
1452 | | * |
1453 | | * return: AWAIT_ value |
1454 | | */ |
1455 | | int gpsd_await_data(fd_set *rfds, |
1456 | | fd_set *efds, |
1457 | | int maxfd, |
1458 | | fd_set *all_fds, |
1459 | | struct gpsd_errout_t *errout, |
1460 | | timespec_t ts_timeout) |
1461 | 0 | { |
1462 | 0 | int status; |
1463 | |
|
1464 | 0 | FD_ZERO(efds); |
1465 | 0 | *rfds = *all_fds; |
1466 | 0 | GPSD_LOG(LOG_RAW1, errout, "CORE: select waits, maxfd %d\n", maxfd); |
1467 | | /* |
1468 | | * Poll for user commands or GPS data. The timeout doesn't |
1469 | | * actually matter here since select returns whenever one of |
1470 | | * the file descriptors in the set goes ready. The point |
1471 | | * of tracking maxfd is to keep the set of descriptors that |
1472 | | * pselect(2) has to poll here as small as possible (for |
1473 | | * low-clock-rate SBCs and the like). |
1474 | | * |
1475 | | * As used here, there is no difference between pselect() |
1476 | | * or select(). A timeout is used, this adds a bit |
1477 | | * of power consumption, but prevents infinite hang during autobaud, |
1478 | | * or select. pselect() may, or may not, modify ts_timeout. |
1479 | | */ |
1480 | 0 | errno = 0; |
1481 | |
|
1482 | 0 | status = pselect(maxfd + 1, rfds, NULL, NULL, &ts_timeout, NULL); |
1483 | 0 | if (-1 == status) { |
1484 | 0 | if (EINTR == errno) { |
1485 | | // caught a signal |
1486 | 0 | return AWAIT_NOT_READY; |
1487 | 0 | } |
1488 | | |
1489 | 0 | if (EBADF == errno) { |
1490 | | // Invalid file descriptor. |
1491 | 0 | int fd; |
1492 | 0 | for (fd = 0; fd < (int)FD_SETSIZE; fd++) { |
1493 | | /* |
1494 | | * All we care about here is a cheap, fast, uninterruptible |
1495 | | * way to check if a file descriptor is valid. |
1496 | | */ |
1497 | 0 | if (FD_ISSET(fd, all_fds) && -1 == fcntl(fd, F_GETFL, 0)) { |
1498 | 0 | FD_CLR(fd, all_fds); |
1499 | 0 | FD_SET(fd, efds); |
1500 | 0 | } |
1501 | 0 | } |
1502 | 0 | return AWAIT_NOT_READY; |
1503 | 0 | } |
1504 | | // else |
1505 | 0 | GPSD_LOG(LOG_ERROR, errout, "CORE: pselect: %s(%d)\n", |
1506 | 0 | strerror(errno), errno); |
1507 | 0 | return AWAIT_FAILED; |
1508 | 0 | } |
1509 | 0 | if (0 == status) { |
1510 | | // pselect timeout |
1511 | 0 | GPSD_LOG(LOG_PROG, errout, "CORE: pselect: timeout\n"); |
1512 | 0 | return AWAIT_TIMEOUT; |
1513 | 0 | } |
1514 | | |
1515 | 0 | if (LOG_SPIN <= errout->debug) { |
1516 | 0 | int i; |
1517 | 0 | char dbuf[BUFSIZ]; |
1518 | 0 | timespec_t ts_now; |
1519 | 0 | char ts_str[TIMESPEC_LEN]; |
1520 | |
|
1521 | 0 | dbuf[0] = '\0'; |
1522 | 0 | for (i = 0; i < (int)FD_SETSIZE; i++) { |
1523 | 0 | if (FD_ISSET(i, all_fds)) { |
1524 | 0 | str_appendf(dbuf, sizeof(dbuf), "%d ", i); |
1525 | 0 | } |
1526 | 0 | } |
1527 | 0 | str_rstrip_char(dbuf, ' '); |
1528 | 0 | (void)strlcat(dbuf, "} -> {", sizeof(dbuf)); |
1529 | 0 | for (i = 0; i < (int)FD_SETSIZE; i++) { |
1530 | 0 | if (FD_ISSET(i, rfds)) { |
1531 | 0 | str_appendf(dbuf, sizeof(dbuf), " %d ", i); |
1532 | 0 | } |
1533 | 0 | } |
1534 | |
|
1535 | 0 | (void)clock_gettime(CLOCK_REALTIME, &ts_now); |
1536 | 0 | GPSD_LOG(LOG_SPIN, errout, |
1537 | 0 | "CORE: pselect() {%s} at %s, %s(%d)\n", |
1538 | 0 | dbuf, |
1539 | 0 | timespec_str(&ts_now, ts_str, sizeof(ts_str)), |
1540 | 0 | strerror(errno), errno); |
1541 | 0 | } |
1542 | |
|
1543 | 0 | return AWAIT_GOT_INPUT; |
1544 | 0 | } |
1545 | | |
1546 | | /* Return false - don't go to next hunt setting |
1547 | | * true - time to go to next hunt setting |
1548 | | */ |
1549 | | static bool hunt_failure(struct gps_device_t *session) |
1550 | 66.2k | { |
1551 | | /* |
1552 | | * After a bad packet, what should cue us to go to next autobaud setting? |
1553 | | * We have tried three different tests here. |
1554 | | * |
1555 | | * The first was session->badcount++>1. This worked very well on |
1556 | | * ttys for years and years, but caused failure to sync on TCP/IP |
1557 | | * sources, which have I/O boundaries in mid-packet more often |
1558 | | * than RS232 ones. There's a test for this at |
1559 | | * test/daemon/tcp-torture.log. |
1560 | | * |
1561 | | * The second was session->badcount++>1 && session->lexer.state==0. |
1562 | | * Fail hunt only if we get a second consecutive bad packet |
1563 | | * and the lexer is in ground state. We don't want to fail on |
1564 | | * a first bad packet because the source might have a burst of |
1565 | | * leading garbage after open. We don't want to fail if the |
1566 | | * lexer is not in ground state, because that means the read |
1567 | | * might have picked up a valid partial packet - better to go |
1568 | | * back around the loop and pick up more data. |
1569 | | * |
1570 | | * The "&& session->lexer.state==0" guard causes an intermittent |
1571 | | * hang while autobauding on SiRF IIIs (but not on SiRF-IIs, oddly |
1572 | | * enough). Removing this conjunct resurrected the failure |
1573 | | * of test/daemon/tcp-torture.log. |
1574 | | * |
1575 | | * Our third attempt, isatty(session->gpsdata.gps_fd) != 0 |
1576 | | * && session->badcount++ > 1, reverts to the old test that worked |
1577 | | * well on ttys for ttys and prevents non-tty devices from *ever* |
1578 | | * having hunt failures. This has the cost that non-tty devices |
1579 | | * will never get kicked off for presenting bad packets. |
1580 | | * |
1581 | | * Slightly refactored, but equivalent, in 3.23.1 |
1582 | | * |
1583 | | * This test may need further revision. |
1584 | | */ |
1585 | 66.2k | if (0 >= gpsd_serial_isatty(session)) { |
1586 | | // Not a tty, so can't hunt. |
1587 | 66.2k | return false; |
1588 | 66.2k | } |
1589 | | // It is a tty, but don't hunt if speed is fixed. |
1590 | 0 | if (0 != session->context->fixed_port_speed) { |
1591 | 0 | return false; |
1592 | 0 | } |
1593 | 0 | return 1 < session->badcount++; |
1594 | 0 | } |
1595 | | |
1596 | | // update the stuff in the scoreboard structure |
1597 | | // Also used by gpsdecode.c |
1598 | | gps_mask_t gpsd_poll(struct gps_device_t *session) |
1599 | 5.70M | { |
1600 | 5.70M | ssize_t newlen; |
1601 | 5.70M | bool driver_change = false; |
1602 | 5.70M | timespec_t ts_now; |
1603 | 5.70M | timespec_t delta; |
1604 | 5.70M | char ts_buf[TIMESPEC_LEN]; |
1605 | | |
1606 | | // Maybe only clear when we actually get a new packet? How? |
1607 | 5.70M | gps_clear_fix(&session->newdata); |
1608 | | |
1609 | | /* |
1610 | | * Input just became available from a sensor, but no read from the |
1611 | | * device has yet been done. |
1612 | | * |
1613 | | * What we actually do here is trickier. For latency-timing |
1614 | | * purposes, we want to know the time at the start of the current |
1615 | | * recording cycle. We rely on the fact that even at 4800bps |
1616 | | * there's a quiet time perceptible to the human eye in gpsmon |
1617 | | * between when the last character of the last packet in a |
1618 | | * 1-second cycle ships and when the next reporting cycle |
1619 | | * ships. Because the cycle time is fixed, higher baud rates will |
1620 | | * make this gap larger. |
1621 | | * |
1622 | | * Thus, we look for an inter-character delay much larger than an |
1623 | | * average 4800bps sentence time. How should this delay be set? Well, |
1624 | | * counting framing bits and erring on the side of caution, it's |
1625 | | * about 480 characters per second or 2083 microeconds per character; |
1626 | | * that's almost exactly 0.125 seconds per average 60-char sentence. |
1627 | | * Doubling this to avoid false positives, we look for an inter-character |
1628 | | * delay of greater than 0.250s. |
1629 | | * |
1630 | | * The above assumes a cycle time of 1 second. To get the minimum size of |
1631 | | * the quiet period, we multiply by the device cycle time. |
1632 | | * |
1633 | | * We can sanity-check these calculation by watching logs. If we have set |
1634 | | * MINIMUM_QUIET_TIME correctly, the "transmission pause" message below |
1635 | | * will consistently be emitted just before the sentence that shows up |
1636 | | * as start-of-cycle in gpsmon, and never emitted at any other point |
1637 | | * in the cycle. |
1638 | | * |
1639 | | * In practice, it seems that edge detection succeeds at 9600bps but |
1640 | | * fails at 4800bps. This is not surprising, as previous profiling has |
1641 | | * indicated that at 4800bps some devices overrun a 1-second cycle time |
1642 | | * with the data they transmit. |
1643 | | */ |
1644 | 5.70M | #define MINIMUM_QUIET_TIME 0.25 |
1645 | 5.70M | if (0 == session->lexer.outbuflen) { |
1646 | | /* beginning of a new packet, or not... |
1647 | | * 0 == lexer.outbuf just means the last read was not a full packet. |
1648 | | * that works on serial lines that dribble data. |
1649 | | * usb tends to only send complete packets. |
1650 | | * Worse, we do not know if we have a full packet this time. |
1651 | | */ |
1652 | 3.20M | (void)clock_gettime(CLOCK_REALTIME, &ts_now); |
1653 | 3.20M | if (NULL != session->device_type && |
1654 | 2.48M | (0 < session->lexer.start_time.tv_sec || |
1655 | 2.48M | 0 < session->lexer.start_time.tv_nsec)) { |
1656 | 2.48M | const double min_cycle = TSTONS(&session->device_type->min_cycle); |
1657 | 2.48M | double quiet_time = (MINIMUM_QUIET_TIME * min_cycle); |
1658 | 2.48M | double gap; |
1659 | | |
1660 | 2.48M | gap = TS_SUB_D(&ts_now, &session->lexer.start_time); |
1661 | | |
1662 | | // used to compare gap > min_cycle, but min_cycle is now |
1663 | | // so variable as to be not helpful. Some GPS models can |
1664 | | // vary from 20Hz to 1Hz. |
1665 | 2.48M | if (gap > quiet_time) { |
1666 | | // quiet_time is getting less useful as GNSS receivers |
1667 | | // have more data to send. |
1668 | 98.6k | GPSD_LOG(LOG_PROG, &session->context->errout, |
1669 | 98.6k | "CORE: transmission pause. gap %f quiet_time %f\n", |
1670 | 98.6k | gap, quiet_time); |
1671 | 98.6k | session->sor = ts_now; |
1672 | 98.6k | session->lexer.start_char = session->lexer.char_counter; |
1673 | 98.6k | } |
1674 | 2.48M | } |
1675 | 3.20M | session->lexer.start_time = ts_now; |
1676 | 3.20M | } |
1677 | | |
1678 | 5.70M | if (COMMENT_PACKET <= session->lexer.type) { |
1679 | 4.63M | session->observed |= PACKET_TYPEMASK(session->lexer.type); |
1680 | 4.63M | } |
1681 | | |
1682 | | // can we get a full packet from the device/NTRIP/DGPS/tcp/etc.? |
1683 | 5.70M | if (NULL != session->device_type && |
1684 | 5.70M | NULL != session->device_type->get_packet) { |
1685 | 4.74M | newlen = session->device_type->get_packet(session); |
1686 | | // coverity[deref_ptr] |
1687 | 4.74M | GPSD_LOG(LOG_RAW, &session->context->errout, |
1688 | 4.74M | "CORE: %s is known to be %s, packet type %d\n", |
1689 | 4.74M | session->gpsdata.dev.path, |
1690 | 4.74M | session->device_type->type_name, |
1691 | 4.74M | session->lexer.type); |
1692 | 4.74M | } else { |
1693 | 955k | newlen = packet_get1(session); |
1694 | 955k | } |
1695 | | |
1696 | | // update the scoreboard structure from the GPS |
1697 | 5.70M | GPSD_LOG(LOG_RAW1, &session->context->errout, |
1698 | 5.70M | "CORE: %s sent %zd new characters\n", |
1699 | 5.70M | session->gpsdata.dev.path, newlen); |
1700 | | |
1701 | 5.70M | (void)clock_gettime(CLOCK_REALTIME, &ts_now); |
1702 | 5.70M | TS_SUB(&delta, &ts_now, &session->gpsdata.online); |
1703 | 5.70M | if (0 > newlen) { // read error |
1704 | 0 | GPSD_LOG(LOG_INF, &session->context->errout, |
1705 | 0 | "CORE: %s returned error %zd (%s sec since data)\n", |
1706 | 0 | session->gpsdata.dev.path, newlen, |
1707 | 0 | timespec_str(&delta, ts_buf, sizeof(ts_buf))); |
1708 | 0 | session->gpsdata.online.tv_sec = 0; |
1709 | 0 | session->gpsdata.online.tv_nsec = 0; |
1710 | 0 | return ERROR_SET; |
1711 | 0 | } |
1712 | 5.70M | if (0 == newlen) { // zero length read, possible EOF |
1713 | | /* |
1714 | | * Multiplier is 2 to avoid edge effects due to sampling at the exact |
1715 | | * wrong time... |
1716 | | * leave TCP network connection alone, let the TCP link timer expire |
1717 | | * and throw an error. |
1718 | | */ |
1719 | 5.29M | if (0 < session->gpsdata.online.tv_sec && |
1720 | 4.17M | SOURCE_TCP != session->sourcetype && |
1721 | | // FIXME: do this with integer math... |
1722 | 4.17M | TSTONS(&delta) >= (TSTONS(&session->gpsdata.dev.cycle) * 2)) { |
1723 | 11.4k | GPSD_LOG(LOG_INF, &session->context->errout, |
1724 | 11.4k | "CORE: %s is offline (%s sec since data) cycle %lld " |
1725 | 11.4k | "srctype %d\n", |
1726 | 11.4k | session->gpsdata.dev.path, |
1727 | 11.4k | timespec_str(&delta, ts_buf, sizeof(ts_buf)), |
1728 | 11.4k | (long long)session->gpsdata.dev.cycle.tv_sec, |
1729 | 11.4k | session->sourcetype); |
1730 | 11.4k | session->gpsdata.online.tv_sec = 0; |
1731 | 11.4k | session->gpsdata.online.tv_nsec = 0; |
1732 | 11.4k | } |
1733 | 5.29M | return NODATA_IS; |
1734 | 5.29M | } |
1735 | | // else (0 < newlen), got at least something. |
1736 | 405k | session->lexer.pkt_time = ts_now; |
1737 | | |
1738 | 405k | GPSD_LOG(LOG_RAW, &session->context->errout, |
1739 | 405k | "CORE: packet sniff on %s finds type %d\n", |
1740 | 405k | session->gpsdata.dev.path, session->lexer.type); |
1741 | 405k | if (COMMENT_PACKET == session->lexer.type) { |
1742 | | // deal with regression test helper macros |
1743 | 17.9k | const char date_str[] = "# Date: "; |
1744 | | |
1745 | 17.9k | session->badcount = 0; |
1746 | 17.9k | if (0 == strcmp((const char *)session->lexer.outbuffer, |
1747 | 17.9k | "# EOF\n")) { |
1748 | | // undocumented, used by gpsfake to signal EOF |
1749 | 140 | GPSD_LOG(LOG_PROG, &session->context->errout, |
1750 | 140 | "CORE: synthetic EOF\n"); |
1751 | 140 | return EOF_IS; |
1752 | 140 | } |
1753 | 17.8k | if (0 == strncmp((const char *)session->lexer.outbuffer, |
1754 | 17.8k | date_str, sizeof(date_str) - 1)) { |
1755 | | // # Date: yyyy-mm-dd |
1756 | | // used by regression tests to correct |
1757 | | // change start time, gps weeks, etc. |
1758 | 2.04k | gpsd_set_century(session); |
1759 | 2.04k | session->regression = 1; |
1760 | | |
1761 | 2.04k | GPSD_LOG(LOG_PROG, &session->context->errout, |
1762 | 2.04k | "CORE: start_time %lld\n", |
1763 | 2.04k | (long long)session->context->start_time); |
1764 | 2.04k | } |
1765 | 17.8k | GPSD_LOG(LOG_PROG, &session->context->errout, |
1766 | 17.8k | "CORE: comment, sync lock deferred: >%s<\n", |
1767 | 17.8k | session->lexer.outbuffer); |
1768 | 387k | } else if (COMMENT_PACKET < session->lexer.type) { |
1769 | 321k | if (NULL == session->device_type) { |
1770 | 48.0k | driver_change = true; |
1771 | 273k | } else { |
1772 | 273k | int newtype = session->lexer.type; |
1773 | | /* |
1774 | | * Are we seeing a new packet type? Then we probably |
1775 | | * want to change drivers. |
1776 | | */ |
1777 | 273k | bool new_packet_type = |
1778 | 273k | (newtype != session->device_type->packet_type); |
1779 | | /* |
1780 | | * Possibly the old driver has a mode-switcher method, in |
1781 | | * which case we know it can handle NMEA itself and may |
1782 | | * want to do special things (like tracking whether a |
1783 | | * previous mode switch to binary succeeded in suppressing |
1784 | | * NMEA). |
1785 | | */ |
1786 | | // QQQ: use STICKY() instead? |
1787 | 273k | bool dependent_nmea = (NMEA_PACKET == newtype && |
1788 | 273k | NULL != session->device_type->mode_switcher); |
1789 | | |
1790 | | /* |
1791 | | * Compute whether to switch drivers. |
1792 | | * If the previous driver type was sticky and this one |
1793 | | * isn't, we'll revert after processing the packet. |
1794 | | */ |
1795 | 273k | driver_change = new_packet_type && !dependent_nmea; |
1796 | 273k | } |
1797 | 321k | if (driver_change) { |
1798 | 71.3k | const struct gps_type_t **dp; |
1799 | | |
1800 | 1.27M | for (dp = gpsd_drivers; *dp; dp++) { |
1801 | 1.27M | if (session->lexer.type == (*dp)->packet_type) { |
1802 | 71.3k | GPSD_LOG(LOG_PROG, &session->context->errout, |
1803 | 71.3k | "CORE: switching to match packet type %d: %s\n", |
1804 | 71.3k | session->lexer.type, gpsd_prettydump(session)); |
1805 | 71.3k | (void)gpsd_switch_driver(session, (*dp)->type_name); |
1806 | 71.3k | break; |
1807 | 71.3k | } |
1808 | 1.27M | } |
1809 | 71.3k | if (NULL == *dp) { |
1810 | 0 | GPSD_LOG(LOG_WARN, &session->context->errout, |
1811 | 0 | "CORE: no matching packet type %d\n", |
1812 | 0 | session->lexer.type); |
1813 | 0 | } |
1814 | 71.3k | } |
1815 | 321k | session->badcount = 0; |
1816 | 321k | session->gpsdata.dev.driver_mode = |
1817 | 321k | (session->lexer.type > NMEA_PACKET) ? MODE_BINARY : MODE_NMEA; |
1818 | 321k | } else if (hunt_failure(session) && !gpsd_next_hunt_setting(session)) { |
1819 | 0 | (void)clock_gettime(CLOCK_REALTIME, &ts_now); |
1820 | 0 | TS_SUB(&delta, &ts_now, &session->gpsdata.online); |
1821 | 0 | GPSD_LOG(LOG_INF, &session->context->errout, |
1822 | 0 | "CORE: hunt on %s failed (%s sec since data)\n", |
1823 | 0 | session->gpsdata.dev.path, |
1824 | 0 | timespec_str(&delta, ts_buf, sizeof(ts_buf))); |
1825 | 0 | return ERROR_SET; |
1826 | 0 | } |
1827 | | |
1828 | 405k | if (0 == session->lexer.outbuflen) { // got new data, but no packet |
1829 | 5.59k | GPSD_LOG(LOG_RAW1, &session->context->errout, |
1830 | 5.59k | "CORE: New data on %s, not yet a packet\n", |
1831 | 5.59k | session->gpsdata.dev.path); |
1832 | 5.59k | return ONLINE_SET; |
1833 | 5.59k | } |
1834 | | |
1835 | | // we have recognized a packet |
1836 | 399k | session->badcount = 0; |
1837 | 399k | gps_mask_t received = PACKET_SET; |
1838 | 399k | (void)clock_gettime(CLOCK_REALTIME, &session->gpsdata.online); |
1839 | | |
1840 | 399k | GPSD_LOG(LOG_RAW1, &session->context->errout, |
1841 | 399k | "CORE: Accepted packet on %s.\n", |
1842 | 399k | session->gpsdata.dev.path); |
1843 | | |
1844 | | // track the packet count since achieving sync on the device |
1845 | 399k | if (driver_change && |
1846 | 71.3k | 0 == (session->drivers_identified & (1ULL << session->driver_index))) { |
1847 | | |
1848 | | // coverity[var_deref_op] |
1849 | 7.45k | GPSD_LOG(LOG_INF, &session->context->errout, |
1850 | 7.45k | "CORE: %s identified as type %s, %ld sec\n", |
1851 | 7.45k | session->gpsdata.dev.path, |
1852 | 7.45k | session->device_type->type_name, |
1853 | 7.45k | (long)(time(NULL) - session->opentime)); |
1854 | | |
1855 | 7.45k | if (0 < gpsd_serial_isatty(session)) { |
1856 | 0 | GPSD_LOG(LOG_INF, &session->context->errout, |
1857 | 0 | "CORE: %s %ubps\n", |
1858 | 0 | session->gpsdata.dev.path, |
1859 | 0 | (unsigned int)gpsd_get_speed(session)); |
1860 | 0 | } |
1861 | | |
1862 | | // fire the init_query method |
1863 | 7.45k | if (NULL != session->device_type && |
1864 | 7.45k | NULL != session->device_type->init_query) { |
1865 | | /* |
1866 | | * We can force readonly off knowing this method does |
1867 | | * not alter device state. |
1868 | | */ |
1869 | 2.89k | bool saved = session->context->readonly; |
1870 | 2.89k | session->context->readonly = false; |
1871 | 2.89k | session->device_type->init_query(session); |
1872 | 2.89k | session->context->readonly = saved; |
1873 | 2.89k | } |
1874 | | |
1875 | | // fire the identified hook |
1876 | 7.45k | if (NULL != session->device_type && |
1877 | 7.45k | NULL != session->device_type->event_hook) { |
1878 | 5.68k | session->device_type->event_hook(session, EVENT_IDENTIFIED); |
1879 | 5.68k | } |
1880 | 7.45k | session->lexer.counter = 0; |
1881 | | |
1882 | | // let clients know about this. |
1883 | 7.45k | received |= DRIVER_IS; |
1884 | | |
1885 | | // mark the fact that this driver has been seen |
1886 | 7.45k | session->drivers_identified |= (1ULL << session->driver_index); |
1887 | 392k | } else { |
1888 | 392k | session->lexer.counter++; |
1889 | 392k | } |
1890 | | |
1891 | | // fire the configure hook, on every packet. Seems excessive... |
1892 | 399k | if (NULL != session->device_type && |
1893 | 399k | NULL != session->device_type->event_hook) { |
1894 | 287k | session->device_type->event_hook(session, EVENT_CONFIGURE); |
1895 | 287k | } |
1896 | | |
1897 | 399k | GPSD_LOG(LOG_RAW, &session->context->errout, |
1898 | 399k | "CORE: raw packet of type %d, %zd:%s\n", |
1899 | 399k | session->lexer.type, |
1900 | 399k | session->lexer.outbuflen, |
1901 | 399k | gpsd_prettydump(session)); |
1902 | | |
1903 | | // Get data from current packet into the fix structure |
1904 | 399k | if (COMMENT_PACKET != session->lexer.type && |
1905 | 381k | BAD_PACKET != session->lexer.type && |
1906 | 399k | NULL != session->device_type && |
1907 | 399k | NULL != session->device_type->parse_packet) { |
1908 | 321k | received |= session->device_type->parse_packet(session); |
1909 | 321k | GPSD_LOG(LOG_SPIN, &session->context->errout, |
1910 | 321k | "CORE: parse_packet() = %s\n", gps_maskdump(received)); |
1911 | 321k | } |
1912 | | |
1913 | | /* |
1914 | | * We may want to revert to the last driver that was marked |
1915 | | * sticky. What this accomplishes is that if we've just |
1916 | | * processed something like AIVDM, but a driver with control |
1917 | | * methods or an event hook had been active before that, we |
1918 | | * keep the information about those capabilities. |
1919 | | */ |
1920 | 399k | if (!STICKY(session->device_type) && |
1921 | 399k | NULL != session->last_controller && |
1922 | 5.91k | STICKY(session->last_controller)) { |
1923 | 5.91k | session->device_type = session->last_controller; |
1924 | 5.91k | GPSD_LOG(LOG_PROG, &session->context->errout, |
1925 | 5.91k | "CORE: reverted to %s driver...\n", |
1926 | 5.91k | session->device_type->type_name); |
1927 | 5.91k | } |
1928 | | |
1929 | | // are we going to generate a report? if so, count characters |
1930 | 399k | if (0 != (received & REPORT_IS)) { |
1931 | 44.4k | session->chars = session->lexer.char_counter - |
1932 | 44.4k | session->lexer.start_char; |
1933 | 44.4k | } |
1934 | | |
1935 | 399k | session->gpsdata.set = ONLINE_SET | received; |
1936 | | |
1937 | | // copy/merge device data into staging buffers |
1938 | 399k | if (0 != (session->gpsdata.set & CLEAR_IS)) { |
1939 | | // CLEAR_IS should only be set on first sentence of cycle |
1940 | 27.5k | gps_clear_att(&session->gpsdata.attitude); |
1941 | 27.5k | if (0 == (session->gpsdata.set & DOP_SET)) { |
1942 | | // FIXME: put gpsdata.dop in newdata.dop |
1943 | 20.1k | gps_clear_dop(&session->gpsdata.dop); |
1944 | 20.1k | } |
1945 | 27.5k | gps_clear_fix(&session->gpsdata.fix); |
1946 | 27.5k | } |
1947 | | #ifdef __UNUSED |
1948 | | // debug |
1949 | | GPSD_LOG(LOG_SHOUT, &session->context->errout, |
1950 | | "CORE: before alt %f new %f\n", |
1951 | | session->gpsdata.fix.altMSL, |
1952 | | session->newdata.altMSL); |
1953 | | GPSD_LOG(LOG_SHOUT, &session->context->errout, |
1954 | | "CORE: before base S %d E %f N %f U %f new %d %f %f %f\n", |
1955 | | session->gpsdata.fix.base.status, |
1956 | | session->gpsdata.fix.base.east, |
1957 | | session->gpsdata.fix.base.north, |
1958 | | session->gpsdata.fix.base.up, |
1959 | | session->newdata.base.status, |
1960 | | session->newdata.base.east, |
1961 | | session->newdata.base.north, |
1962 | | session->newdata.base.up); |
1963 | | |
1964 | | GPSD_LOG(LOG_SHOUT, &session->context->errout, |
1965 | | "CORE: transfer mask: %s\n", |
1966 | | gps_maskdump(session->gpsdata.set)); |
1967 | | #endif // __UNUSED |
1968 | 399k | gps_merge_fix(&session->gpsdata.fix, |
1969 | 399k | session->gpsdata.set, &session->newdata); |
1970 | | |
1971 | | #ifdef __UNUSED |
1972 | | // debug |
1973 | | GPSD_LOG(LOG_SHOUT, &session->context->errout, |
1974 | | "CORE: after base S %d E %f N %f U %f new %d %f %f %f\n", |
1975 | | session->gpsdata.fix.base.status, |
1976 | | session->gpsdata.fix.base.east, |
1977 | | session->gpsdata.fix.base.north, |
1978 | | session->gpsdata.fix.base.up, |
1979 | | session->newdata.base.status, |
1980 | | session->newdata.base.east, |
1981 | | session->newdata.base.north, |
1982 | | session->newdata.base.up); |
1983 | | #endif // __UNUSED |
1984 | | /* |
1985 | | * Compute fix-quality data from the satellite positions. |
1986 | | * These will not overwrite any DOPs reported from the packet |
1987 | | * we just got. |
1988 | | */ |
1989 | 399k | if (0 != (received & SATELLITE_SET) && |
1990 | 20.8k | 0 < session->gpsdata.satellites_visible) { |
1991 | 18.1k | session->gpsdata.set |= fill_dop(&session->context->errout, |
1992 | 18.1k | &session->gpsdata, |
1993 | 18.1k | &session->gpsdata.dop); |
1994 | 18.1k | } |
1995 | | |
1996 | 399k | gpsd_error_model(session); |
1997 | | |
1998 | | /* |
1999 | | * Count good fixes. We used to check |
2000 | | * session->gpsdata.fix.status > STATUS_UNK |
2001 | | * here, but that wasn't quite right. That tells us whether |
2002 | | * we think we have a valid fix for the current cycle, but remains |
2003 | | * true while following non-fix packets are received. What we |
2004 | | * really want to know is whether the last packet received was a |
2005 | | * fix packet AND held a valid fix. We must ignore non-fix packets |
2006 | | * AND packets which have fix data but are flagged as invalid. Some |
2007 | | * devices output fix packets on a regular basis, even when unable |
2008 | | * to derive a good fix. Such bad packets should set MODE_NO_FIX |
2009 | | */ |
2010 | 399k | if (0 != (session->gpsdata.set & (LATLON_SET|ECEF_SET))) { |
2011 | 22.4k | if (MODE_NO_FIX < session->gpsdata.fix.mode) { |
2012 | 12.3k | session->context->fixcnt++; |
2013 | 12.3k | session->fixcnt++; |
2014 | 12.3k | } else { |
2015 | 10.1k | session->context->fixcnt = 0; |
2016 | 10.1k | session->fixcnt = 0; |
2017 | 10.1k | } |
2018 | 377k | } else if (0 != (session->gpsdata.set & (MODE_SET))) { |
2019 | 23.9k | if (MODE_NO_FIX == session->gpsdata.fix.mode) { |
2020 | 14.2k | session->context->fixcnt = 0; |
2021 | 14.2k | session->fixcnt = 0; |
2022 | 14.2k | } |
2023 | 23.9k | } |
2024 | | /* |
2025 | | * Sanity check. This catches a surprising number of port and |
2026 | | * driver errors, including 32-vs.-64-bit problems. |
2027 | | */ |
2028 | 399k | if (0 != (session->gpsdata.set & TIME_SET)) { |
2029 | 31.1k | if (session->newdata.time.tv_sec > |
2030 | 31.1k | (time(NULL) + (60 * 60 * 24 * 365))) { |
2031 | 13.8k | GPSD_LOG(LOG_WARN, &session->context->errout, |
2032 | 13.8k | "CORE: date (%lld) more than a year in the future!\n", |
2033 | 13.8k | (long long)session->newdata.time.tv_sec); |
2034 | 17.2k | } else if (0 > session->newdata.time.tv_sec) { |
2035 | 9.83k | GPSD_LOG(LOG_ERROR, &session->context->errout, |
2036 | 9.83k | "CORE: date (%lld) is negative!\n", |
2037 | 9.83k | (long long)session->newdata.time.tv_sec); |
2038 | 9.83k | } |
2039 | 31.1k | } |
2040 | | |
2041 | 399k | GPSD_LOG(LOG_DATA, &session->context->errout, |
2042 | 399k | "CORE: gpsd_poll(%s) %s\n", |
2043 | 399k | session->gpsdata.dev.path, |
2044 | 399k | gps_maskdump(session->gpsdata.set)); |
2045 | 399k | return session->gpsdata.set; |
2046 | 405k | } |
2047 | | |
2048 | | // consume and handle packets from a specified device |
2049 | | int gpsd_multipoll(const bool data_ready, |
2050 | | struct gps_device_t *device, |
2051 | | void (*handler)(struct gps_device_t *, gps_mask_t), |
2052 | | float reawake_time) |
2053 | 0 | { |
2054 | 0 | if (data_ready) { |
2055 | 0 | int fragments; |
2056 | | |
2057 | | // cast for 32-bit ints |
2058 | 0 | GPSD_LOG(LOG_RAW1, &device->context->errout, |
2059 | 0 | "CORE: polling %ld\n", (long)device->gpsdata.gps_fd); |
2060 | | |
2061 | | /* |
2062 | | * Strange special case - the opening transaction on an NTRIP |
2063 | | * connection may not yet be completed. |
2064 | | * Try to ratchet things forward. |
2065 | | */ |
2066 | 0 | if (SERVICE_NTRIP == device->servicetype && |
2067 | 0 | NTRIP_CONN_ESTABLISHED != device->ntrip.conn_state) { |
2068 | |
|
2069 | 0 | timespec_t ts_now; |
2070 | 0 | double step; |
2071 | |
|
2072 | 0 | (void)clock_gettime(CLOCK_REALTIME, &ts_now); |
2073 | |
|
2074 | 0 | step = TS_SUB_D(&ts_now, &device->ntrip.stream.stream_time); |
2075 | | // wait 6 seconds between hitting ntrip_open() |
2076 | 0 | if (6 > fabs(step)) { |
2077 | 0 | return DEVICE_UNCHANGED; |
2078 | 0 | } |
2079 | 0 | device->ntrip.stream.stream_time = ts_now; |
2080 | 0 | (void)ntrip_open(device, ""); |
2081 | 0 | if (NTRIP_CONN_ERR == device->ntrip.conn_state) { |
2082 | 0 | GPSD_LOG(LOG_WARN, &device->context->errout, |
2083 | 0 | "CORE: connection to ntrip server failed\n"); |
2084 | | // FIXME: next stat after error should depend on if |
2085 | | // initial connect or reconnect... |
2086 | 0 | device->ntrip.conn_state = NTRIP_CONN_CLOSED; |
2087 | 0 | return DEVICE_ERROR; |
2088 | 0 | } |
2089 | | // else |
2090 | 0 | return DEVICE_READY; |
2091 | 0 | } |
2092 | | |
2093 | 0 | for (fragments = 0; ; fragments++) { |
2094 | 0 | gps_mask_t changed = gpsd_poll(device); |
2095 | |
|
2096 | 0 | if (EOF_IS == changed) { |
2097 | 0 | GPSD_LOG(LOG_WARN, &device->context->errout, |
2098 | 0 | "CORE: device signed off %s\n", |
2099 | 0 | device->gpsdata.dev.path); |
2100 | 0 | return DEVICE_EOF; |
2101 | 0 | } |
2102 | 0 | if (ERROR_SET == changed) { |
2103 | 0 | GPSD_LOG(LOG_WARN, &device->context->errout, |
2104 | 0 | "CORE: device read of %s returned error or " |
2105 | 0 | "packet sniffer failed sync (flags %s)\n", |
2106 | 0 | device->gpsdata.dev.path, |
2107 | 0 | gps_maskdump(changed)); |
2108 | 0 | return DEVICE_ERROR; |
2109 | 0 | } |
2110 | 0 | if (NODATA_IS == changed) { |
2111 | | /* |
2112 | | * No data on the first fragment read means the device |
2113 | | * fd may have been in an end-of-file condition on select. |
2114 | | */ |
2115 | 0 | if (0 == fragments) { |
2116 | 0 | GPSD_LOG(LOG_DATA, &device->context->errout, |
2117 | 0 | "CORE: %s returned zero bytes\n", |
2118 | 0 | device->gpsdata.dev.path); |
2119 | 0 | if (device->zerokill) { |
2120 | | // failed timeout-and-reawake, kill it |
2121 | 0 | gpsd_deactivate(device); |
2122 | 0 | if (device->ntrip.works) { |
2123 | | // reset so we try this once only |
2124 | 0 | device->ntrip.works = false; |
2125 | 0 | if (0 > gpsd_activate(device, O_CONTINUE)) { |
2126 | 0 | GPSD_LOG(LOG_WARN, &device->context->errout, |
2127 | 0 | "CORE: reconnect to ntrip server " |
2128 | 0 | "failed\n"); |
2129 | 0 | return DEVICE_ERROR; |
2130 | 0 | } |
2131 | | // else |
2132 | 0 | GPSD_LOG(LOG_INF, &device->context->errout, |
2133 | 0 | "CORE: reconnecting to ntrip server\n"); |
2134 | 0 | return DEVICE_READY; |
2135 | 0 | } |
2136 | 0 | } else if (0 == reawake_time) { |
2137 | 0 | return DEVICE_ERROR; |
2138 | 0 | } else { |
2139 | | /* |
2140 | | * Disable listening to this fd for long enough |
2141 | | * that the buffer can fill up again. |
2142 | | */ |
2143 | 0 | GPSD_LOG(LOG_DATA, &device->context->errout, |
2144 | 0 | "CORE: %s will be repolled in %f seconds\n", |
2145 | 0 | device->gpsdata.dev.path, reawake_time); |
2146 | 0 | device->reawake = time(NULL) + reawake_time; |
2147 | 0 | return DEVICE_UNREADY; |
2148 | 0 | } |
2149 | 0 | } |
2150 | | /* |
2151 | | * No data on later fragment reads just means the |
2152 | | * input buffer is empty. In this case break out |
2153 | | * of the fragment-processing loop but consider |
2154 | | * the device still good. |
2155 | | */ |
2156 | 0 | break; |
2157 | 0 | } |
2158 | | |
2159 | | // we got actual data, head off the reawake special case |
2160 | 0 | device->zerokill = false; |
2161 | 0 | device->reawake = (time_t)0; |
2162 | | |
2163 | | // must have a full packet to continue |
2164 | 0 | if (0 == (changed & PACKET_SET)) { |
2165 | 0 | break; |
2166 | 0 | } |
2167 | | |
2168 | | // conditional prevents mask dumper from eating CPU |
2169 | 0 | if (LOG_DATA <= device->context->errout.debug) { |
2170 | 0 | if (BAD_PACKET == device->lexer.type) { |
2171 | 0 | GPSD_LOG(LOG_DATA, &device->context->errout, |
2172 | 0 | "CORE: packet with bad checksum from %s\n", |
2173 | 0 | device->gpsdata.dev.path); |
2174 | 0 | } else { |
2175 | 0 | GPSD_LOG(LOG_DATA, &device->context->errout, |
2176 | 0 | "CORE: packet type %d from %s with %s\n", |
2177 | 0 | device->lexer.type, |
2178 | 0 | device->gpsdata.dev.path, |
2179 | 0 | gps_maskdump(device->gpsdata.set)); |
2180 | 0 | } |
2181 | 0 | } |
2182 | | |
2183 | | |
2184 | | // handle data contained in this packet |
2185 | 0 | if (BAD_PACKET != device->lexer.type) { |
2186 | 0 | handler(device, changed); |
2187 | 0 | } |
2188 | |
|
2189 | | #ifdef __future__ |
2190 | | // this breaks: test/daemon/passthrough.log ?? |
2191 | | /* |
2192 | | * Bernd Ocklin suggests: |
2193 | | * Exit when a full packet was received and parsed. |
2194 | | * This allows other devices to be serviced even if |
2195 | | * this device delivers a full packet at every single |
2196 | | * read. |
2197 | | * Otherwise we can sit here for a long time without |
2198 | | * any for-loop exit condition being met. |
2199 | | * It might also reduce the latency from a received packet to |
2200 | | * it being output by gpsd. |
2201 | | */ |
2202 | | if (0 != (changed & PACKET_SET)) { |
2203 | | break; |
2204 | | } |
2205 | | #endif // __future__ |
2206 | 0 | } |
2207 | 0 | } else if (0 < device->reawake && |
2208 | 0 | time(NULL) > device->reawake) { |
2209 | | // FIXME: what if time went backward? |
2210 | | // device may have had a zero-length read |
2211 | 0 | GPSD_LOG(LOG_DATA, &device->context->errout, |
2212 | 0 | "CORE: %s reawakened after zero-length read\n", |
2213 | 0 | device->gpsdata.dev.path); |
2214 | 0 | device->reawake = (time_t)0; |
2215 | 0 | device->zerokill = true; |
2216 | 0 | return DEVICE_READY; |
2217 | 0 | } else if (SERVICE_NTRIP == device->servicetype && |
2218 | 0 | NTRIP_CONN_INPROGRESS == device->ntrip.conn_state) { |
2219 | |
|
2220 | 0 | timespec_t ts_now; |
2221 | 0 | double step; |
2222 | |
|
2223 | 0 | (void)clock_gettime(CLOCK_REALTIME, &ts_now); |
2224 | |
|
2225 | 0 | step = TS_SUB_D(&ts_now, &device->ntrip.stream.stream_time); |
2226 | | // wait 6 seconds between hitting ntrip_open() |
2227 | 0 | if (6 > fabs(step)) { |
2228 | 0 | return DEVICE_UNCHANGED; |
2229 | 0 | } |
2230 | 0 | device->ntrip.stream.stream_time = ts_now; |
2231 | 0 | (void)ntrip_open(device, ""); |
2232 | 0 | if (NTRIP_CONN_ERR == device->ntrip.conn_state) { |
2233 | 0 | GPSD_LOG(LOG_WARN, &device->context->errout, |
2234 | 0 | "CORE: 2 connection to ntrip server failed\n"); |
2235 | | // FIXME: next stat after error should depend on if |
2236 | | // initial connect or reconnect... |
2237 | 0 | device->ntrip.conn_state = NTRIP_CONN_CLOSED; |
2238 | 0 | return DEVICE_ERROR; |
2239 | 0 | } |
2240 | | // else |
2241 | 0 | return DEVICE_READY; |
2242 | 0 | } |
2243 | | |
2244 | | // no change in device descriptor state |
2245 | 0 | return DEVICE_UNCHANGED; |
2246 | 0 | } |
2247 | | |
2248 | | // end-of-session wrapup |
2249 | | void gpsd_wrap(struct gps_device_t *session) |
2250 | 0 | { |
2251 | 0 | if (!BAD_SOCKET(session->gpsdata.gps_fd)) { |
2252 | 0 | gpsd_deactivate(session); |
2253 | 0 | } |
2254 | 0 | } |
2255 | | |
2256 | | /* gpsd_zero_satellites(), initialize the skyview (satellite_t) |
2257 | | * |
2258 | | * return; void |
2259 | | */ |
2260 | | void gpsd_zero_satellites( struct gps_data_t *out) |
2261 | 42.1k | { |
2262 | 42.1k | unsigned long sat; |
2263 | | |
2264 | 42.1k | memset(out->skyview, 0, sizeof(out->skyview)); |
2265 | 42.1k | out->satellites_visible = 0; |
2266 | | // zero is good inbound data for ss, elevation, and azimuth. |
2267 | | // we need to set them to invalid values |
2268 | 9.74M | for (sat = 0; sat < ROWS(out->skyview); sat++) { |
2269 | 9.70M | out->skyview[sat].azimuth = NAN; |
2270 | 9.70M | out->skyview[sat].elevation = NAN; |
2271 | 9.70M | out->skyview[sat].ss = NAN; |
2272 | 9.70M | out->skyview[sat].prRes = NAN; |
2273 | 9.70M | out->skyview[sat].prRate = NAN; |
2274 | 9.70M | out->skyview[sat].pr = NAN; |
2275 | 9.70M | out->skyview[sat].freqid = -1; |
2276 | 9.70M | out->skyview[sat].qualityInd = -1; |
2277 | 9.70M | } |
2278 | | #if 0 |
2279 | | /* |
2280 | | * We used to clear DOPs here, but this causes misbehavior on some |
2281 | | * combined GPS/GLONASS/QZSS receivers like the Telit SL869; the |
2282 | | * symptom is that the "satellites_used" field in a struct gps_data_t |
2283 | | * filled in by gps_read() is always zero. |
2284 | | */ |
2285 | | gps_clear_dop(&out->dop); |
2286 | | #endif |
2287 | 42.1k | } |
2288 | | |
2289 | | /* Latch the fact that we've saved a fix. |
2290 | | * And add in the device fudge */ |
2291 | | void ntp_latch(struct gps_device_t *device, struct timedelta_t *td) |
2292 | 0 | { |
2293 | | // this should be an invariant of the way this function is called |
2294 | 0 | if (0 >= device->newdata.time.tv_sec) { |
2295 | 0 | return; |
2296 | 0 | } |
2297 | | |
2298 | 0 | (void)clock_gettime(CLOCK_REALTIME, &td->clock); |
2299 | | // structure copy of time from GPS |
2300 | 0 | td->real = device->newdata.time; |
2301 | | |
2302 | | // is there an offset method? |
2303 | 0 | if (NULL != device->device_type && |
2304 | 0 | NULL != device->device_type->time_offset) { |
2305 | 0 | double integral; |
2306 | 0 | double offset = device->device_type->time_offset(device); |
2307 | | |
2308 | | // add in offset which is double |
2309 | 0 | td->real.tv_nsec += (long)(modf(offset, &integral) * 1e9); |
2310 | 0 | td->real.tv_sec += (time_t)integral; |
2311 | 0 | TS_NORM(&td->real); |
2312 | 0 | } |
2313 | | |
2314 | | // thread-safe update |
2315 | 0 | pps_thread_fixin(&device->pps_thread, td); |
2316 | 0 | } |
2317 | | |
2318 | | // end |
2319 | | // vim: set expandtab shiftwidth=4 |