libgpsd_core.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  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 (c) 2010-2018 by the GPSD project
  10. * SPDX-License-Identifier: BSD-2-clause
  11. */
  12. #include "gpsd_config.h" /* must be before all includes */
  13. #include <assert.h>
  14. #include <ctype.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <libgen.h>
  18. #include <math.h>
  19. #include <stdarg.h>
  20. #include <stdbool.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <syslog.h>
  25. #include <sys/select.h>
  26. #include <sys/socket.h>
  27. #include <sys/stat.h>
  28. #include <sys/types.h>
  29. #include <sys/wait.h>
  30. #include <time.h>
  31. #include <unistd.h>
  32. #include "gpsd.h"
  33. #include "matrix.h"
  34. #include "strfuncs.h"
  35. #include "timespec.h"
  36. #if defined(NMEA2000_ENABLE)
  37. #include "driver_nmea2000.h"
  38. #endif /* defined(NMEA2000_ENABLE) */
  39. ssize_t gpsd_write(struct gps_device_t *session,
  40. const char *buf,
  41. const size_t len)
  42. /* pass low-level data to devices straight through */
  43. {
  44. return session->context->serial_write(session, buf, len);
  45. }
  46. static void basic_report(const char *buf)
  47. {
  48. (void)fputs(buf, stderr);
  49. }
  50. void errout_reset(struct gpsd_errout_t *errout)
  51. {
  52. errout->debug = LOG_SHOUT;
  53. errout->report = basic_report;
  54. }
  55. static pthread_mutex_t report_mutex;
  56. void gpsd_acquire_reporting_lock(void)
  57. {
  58. int err;
  59. err = pthread_mutex_lock(&report_mutex);
  60. if ( 0 != err ) {
  61. /* POSIX says pthread_mutex_lock() should only fail if the
  62. thread holding the lock has died. Best for gppsd to just die
  63. because things are FUBAR. */
  64. (void) fprintf(stderr,"pthread_mutex_lock() failed: %s\n",
  65. strerror(errno));
  66. exit(EXIT_FAILURE);
  67. }
  68. }
  69. void gpsd_release_reporting_lock(void)
  70. {
  71. int err;
  72. err = pthread_mutex_unlock(&report_mutex);
  73. if ( 0 != err ) {
  74. /* POSIX says pthread_mutex_unlock() should only fail when
  75. trying to unlock a lock that does not exist, or is not owned by
  76. this thread. This should never happen, so best for gpsd to die
  77. because things are FUBAR. */
  78. (void) fprintf(stderr,"pthread_mutex_unlock() failed: %s\n",
  79. strerror(errno));
  80. exit(EXIT_FAILURE);
  81. }
  82. }
  83. #ifndef SQUELCH_ENABLE
  84. static void visibilize(char *outbuf, size_t outlen,
  85. const char *inbuf, size_t inlen)
  86. {
  87. const char *sp;
  88. outbuf[0] = '\0';
  89. for (sp = inbuf; sp < inbuf + inlen && strlen(outbuf)+6 < outlen; sp++)
  90. if (isprint((unsigned char) *sp) || (sp[0] == '\n' && sp[1] == '\0')
  91. || (sp[0] == '\r' && sp[2] == '\0'))
  92. (void)snprintf(outbuf + strlen(outbuf), 2, "%c", *sp);
  93. else
  94. (void)snprintf(outbuf + strlen(outbuf), 6, "\\x%02x",
  95. 0x00ff & (unsigned)*sp);
  96. }
  97. #endif /* !SQUELCH_ENABLE */
  98. static void gpsd_vlog(const int errlevel,
  99. const struct gpsd_errout_t *errout,
  100. char *outbuf, size_t outlen,
  101. const char *fmt, va_list ap)
  102. /* assemble msg in vprintf(3) style, use errout hook or syslog for delivery */
  103. {
  104. #ifdef SQUELCH_ENABLE
  105. (void)errout;
  106. (void)errlevel;
  107. (void)fmt;
  108. #else
  109. char buf[BUFSIZ];
  110. char *err_str;
  111. // errout should never be NULL, but some code analyzers complain anyway
  112. if (NULL == errout ||
  113. errout->debug < errlevel) {
  114. return;
  115. }
  116. gpsd_acquire_reporting_lock();
  117. switch ( errlevel ) {
  118. case LOG_ERROR:
  119. err_str = "ERROR: ";
  120. break;
  121. case LOG_SHOUT:
  122. err_str = "SHOUT: ";
  123. break;
  124. case LOG_WARN:
  125. err_str = "WARN: ";
  126. break;
  127. case LOG_CLIENT:
  128. err_str = "CLIENT: ";
  129. break;
  130. case LOG_INF:
  131. err_str = "INFO: ";
  132. break;
  133. case LOG_DATA:
  134. err_str = "DATA: ";
  135. break;
  136. case LOG_PROG:
  137. err_str = "PROG: ";
  138. break;
  139. case LOG_IO:
  140. err_str = "IO: ";
  141. break;
  142. case LOG_SPIN:
  143. err_str = "SPIN: ";
  144. break;
  145. case LOG_RAW:
  146. err_str = "RAW: ";
  147. break;
  148. default:
  149. err_str = "UNK: ";
  150. }
  151. assert(errout->label != NULL);
  152. (void)strlcpy(buf, errout->label, sizeof(buf));
  153. (void)strlcat(buf, ":", sizeof(buf));
  154. (void)strlcat(buf, err_str, sizeof(buf));
  155. str_vappendf(buf, sizeof(buf), fmt, ap);
  156. visibilize(outbuf, outlen, buf, strlen(buf));
  157. if (getpid() == getsid(getpid()))
  158. syslog((errlevel <= LOG_SHOUT) ? LOG_ERR : LOG_NOTICE, "%s", outbuf);
  159. else if (errout->report != NULL)
  160. errout->report(outbuf);
  161. else
  162. (void)fputs(outbuf, stderr);
  163. gpsd_release_reporting_lock();
  164. #endif /* !SQUELCH_ENABLE */
  165. }
  166. /* assemble msg in printf(3) style, use errout hook or syslog for delivery */
  167. void gpsd_log(const int errlevel, const struct gpsd_errout_t *errout,
  168. const char *fmt, ...)
  169. {
  170. char buf[BUFSIZ];
  171. va_list ap;
  172. buf[0] = '\0';
  173. va_start(ap, fmt);
  174. gpsd_vlog(errlevel, errout, buf, sizeof(buf), fmt, ap);
  175. va_end(ap);
  176. }
  177. const char *gpsd_prettydump(struct gps_device_t *session)
  178. /* dump the current packet in a form optimised for eyeballs */
  179. {
  180. return gpsd_packetdump(session->msgbuf, sizeof(session->msgbuf),
  181. (char *)session->lexer.outbuffer,
  182. session->lexer.outbuflen);
  183. }
  184. /* Define the possible hook strings here so we can get the length */
  185. #define HOOK_ACTIVATE "ACTIVATE"
  186. #define HOOK_DEACTIVATE "DEACTIVATE"
  187. #define HOOK_CMD_MAX (sizeof(DEVICEHOOKPATH) + GPS_PATH_MAX \
  188. + sizeof(HOOK_DEACTIVATE))
  189. static void gpsd_run_device_hook(struct gpsd_errout_t *errout,
  190. char *device_name, char *hook)
  191. {
  192. struct stat statbuf;
  193. if (stat(DEVICEHOOKPATH, &statbuf) == -1)
  194. GPSD_LOG(LOG_PROG, errout,
  195. "no %s present, skipped running %s hook\n",
  196. DEVICEHOOKPATH, hook);
  197. else {
  198. int status;
  199. char buf[HOOK_CMD_MAX];
  200. (void)snprintf(buf, sizeof(buf), "%s %s %s",
  201. DEVICEHOOKPATH, device_name, hook);
  202. GPSD_LOG(LOG_INF, errout, "running %s\n", buf);
  203. status = system(buf);
  204. if (status == -1)
  205. GPSD_LOG(LOG_ERROR, errout, "error running %s\n", buf);
  206. else
  207. GPSD_LOG(LOG_INF, errout,
  208. "%s returned %d\n", DEVICEHOOKPATH,
  209. WEXITSTATUS(status));
  210. }
  211. }
  212. int gpsd_switch_driver(struct gps_device_t *session, char *type_name)
  213. {
  214. const struct gps_type_t **dp;
  215. bool first_sync = (session->device_type != NULL);
  216. unsigned int i;
  217. if (first_sync && strcmp(session->device_type->type_name, type_name) == 0)
  218. return 0;
  219. GPSD_LOG(LOG_PROG, &session->context->errout,
  220. "switch_driver(%s) called...\n", type_name);
  221. for (dp = gpsd_drivers, i = 0; *dp; dp++, i++)
  222. if (strcmp((*dp)->type_name, type_name) == 0) {
  223. GPSD_LOG(LOG_PROG, &session->context->errout,
  224. "selecting %s driver...\n",
  225. (*dp)->type_name);
  226. gpsd_assert_sync(session);
  227. session->device_type = *dp;
  228. session->driver_index = i;
  229. #ifdef RECONFIGURE_ENABLE
  230. session->gpsdata.dev.mincycle = session->device_type->min_cycle;
  231. #endif /* RECONFIGURE_ENABLE */
  232. /* reconfiguration might be required */
  233. if (first_sync && session->device_type->event_hook != NULL)
  234. session->device_type->event_hook(session,
  235. event_driver_switch);
  236. #ifdef RECONFIGURE_ENABLE
  237. if (STICKY(*dp))
  238. session->last_controller = *dp;
  239. #endif /* RECONFIGURE_ENABLE */
  240. return 1;
  241. }
  242. GPSD_LOG(LOG_ERROR, &session->context->errout,
  243. "invalid GPS type \"%s\".\n", type_name);
  244. return 0;
  245. }
  246. void gps_context_init(struct gps_context_t *context,
  247. const char *label)
  248. {
  249. (void)memset(context, '\0', sizeof(struct gps_context_t));
  250. //context.readonly = false;
  251. context->leap_notify = LEAP_NOWARNING;
  252. context->serial_write = gpsd_serial_write;
  253. errout_reset(&context->errout);
  254. context->errout.label = (char *)label;
  255. (void)pthread_mutex_init(&report_mutex, NULL);
  256. }
  257. void gpsd_init(struct gps_device_t *session, struct gps_context_t *context,
  258. const char *device)
  259. /* initialize GPS polling */
  260. {
  261. if (device != NULL)
  262. (void)strlcpy(session->gpsdata.dev.path, device,
  263. sizeof(session->gpsdata.dev.path));
  264. session->device_type = NULL; /* start by hunting packets */
  265. #ifdef RECONFIGURE_ENABLE
  266. session->last_controller = NULL;
  267. #endif /* RECONFIGURE_ENABLE */
  268. session->observed = 0;
  269. session->sourcetype = source_unknown; /* gpsd_open() sets this */
  270. session->servicetype = service_unknown; /* gpsd_open() sets this */
  271. session->context = context;
  272. memset(session->subtype, 0, sizeof(session->subtype));
  273. memset(session->subtype1, 0, sizeof(session->subtype1));
  274. #ifdef NMEA0183_ENABLE
  275. memset(&(session->nmea), 0, sizeof(session->nmea));
  276. #endif /* NMEA0183_ENABLE */
  277. gps_clear_fix(&session->gpsdata.fix);
  278. gps_clear_fix(&session->newdata);
  279. gps_clear_fix(&session->lastfix);
  280. gps_clear_fix(&session->oldfix);
  281. session->gpsdata.set = 0;
  282. gps_clear_att(&session->gpsdata.attitude);
  283. gps_clear_dop(&session->gpsdata.dop);
  284. session->gpsdata.dev.mincycle.tv_sec = 1;
  285. session->gpsdata.dev.mincycle.tv_nsec = 0;
  286. session->gpsdata.dev.cycle.tv_sec = 1;
  287. session->gpsdata.dev.cycle.tv_nsec = 0;
  288. session->sor.tv_sec = 0;
  289. session->sor.tv_nsec = 0;
  290. session->chars = 0;
  291. /* tty-level initialization */
  292. gpsd_tty_init(session);
  293. /* necessary in case we start reading in the middle of a GPGSV sequence */
  294. gpsd_zero_satellites(&session->gpsdata);
  295. /* initialize things for the packet parser */
  296. packet_reset(&session->lexer);
  297. }
  298. /* temporarily release the GPS device */
  299. void gpsd_deactivate(struct gps_device_t *session)
  300. {
  301. #ifdef RECONFIGURE_ENABLE
  302. if (!session->context->readonly
  303. && session->device_type != NULL
  304. && session->device_type->event_hook != NULL) {
  305. session->device_type->event_hook(session, event_deactivate);
  306. }
  307. #endif /* RECONFIGURE_ENABLE */
  308. GPSD_LOG(LOG_INF, &session->context->errout,
  309. "closing GPS=%s (%d)\n",
  310. session->gpsdata.dev.path, session->gpsdata.gps_fd);
  311. #if defined(NMEA2000_ENABLE)
  312. if (session->sourcetype == source_can)
  313. (void)nmea2000_close(session);
  314. else
  315. #endif /* of defined(NMEA2000_ENABLE) */
  316. (void)gpsd_close(session);
  317. if (session->mode == O_OPTIMIZE)
  318. gpsd_run_device_hook(&session->context->errout,
  319. session->gpsdata.dev.path,
  320. HOOK_DEACTIVATE);
  321. /* tell any PPS-watcher thread to die */
  322. session->pps_thread.report_hook = NULL;
  323. /* mark it inactivated */
  324. session->gpsdata.online.tv_sec = 0;
  325. session->gpsdata.online.tv_nsec = 0;
  326. }
  327. static void ppsthread_log(volatile struct pps_thread_t *pps_thread,
  328. int loglevel, const char *fmt, ...)
  329. /* shim function to decouple PPS monitor code from the session structure */
  330. {
  331. struct gps_device_t *device = (struct gps_device_t *)pps_thread->context;
  332. char buf[BUFSIZ];
  333. va_list ap;
  334. switch (loglevel) {
  335. case THREAD_ERROR:
  336. loglevel = LOG_ERROR;
  337. break;
  338. case THREAD_WARN:
  339. loglevel = LOG_WARN;
  340. break;
  341. case THREAD_INF:
  342. loglevel = LOG_INF;
  343. break;
  344. case THREAD_PROG:
  345. loglevel = LOG_PROG;
  346. break;
  347. case THREAD_RAW:
  348. loglevel = LOG_RAW;
  349. break;
  350. }
  351. buf[0] = '\0';
  352. va_start(ap, fmt);
  353. gpsd_vlog(loglevel, &device->context->errout, buf, sizeof(buf), fmt, ap);
  354. va_end(ap);
  355. }
  356. void gpsd_clear(struct gps_device_t *session)
  357. /* device has been opened - clear its storage for use */
  358. {
  359. (void)clock_gettime(CLOCK_REALTIME, &session->gpsdata.online);
  360. lexer_init(&session->lexer);
  361. session->lexer.errout = session->context->errout;
  362. // session->gpsdata.online = 0;
  363. gps_clear_att(&session->gpsdata.attitude);
  364. gps_clear_dop(&session->gpsdata.dop);
  365. gps_clear_fix(&session->gpsdata.fix);
  366. session->gpsdata.status = STATUS_NO_FIX;
  367. session->releasetime = (time_t)0;
  368. session->badcount = 0;
  369. /* clear the private data union */
  370. memset( (void *)&session->driver, '\0', sizeof(session->driver));
  371. /* set up the context structure for the PPS thread monitor */
  372. memset((void *)&session->pps_thread, 0, sizeof(session->pps_thread));
  373. session->pps_thread.devicefd = session->gpsdata.gps_fd;
  374. session->pps_thread.devicename = session->gpsdata.dev.path;
  375. session->pps_thread.log_hook = ppsthread_log;
  376. session->pps_thread.context = (void *)session;
  377. session->opentime = time(NULL);
  378. }
  379. int gpsd_open(struct gps_device_t *session)
  380. /* open a device for access to its data *
  381. * return: the opened file descriptor
  382. * PLACEHOLDING_FD - for /dev/ppsX
  383. * UNALLOCATED_FD - for open failure
  384. * -1 - for open failure
  385. */
  386. {
  387. #ifdef NETFEED_ENABLE
  388. /* special case: source may be a URI to a remote GNSS or DGPS service */
  389. if (netgnss_uri_check(session->gpsdata.dev.path)) {
  390. session->gpsdata.gps_fd = netgnss_uri_open(session,
  391. session->gpsdata.dev.path);
  392. session->sourcetype = source_tcp;
  393. GPSD_LOG(LOG_SPIN, &session->context->errout,
  394. "netgnss_uri_open(%s) returns socket on fd %d\n",
  395. session->gpsdata.dev.path, session->gpsdata.gps_fd);
  396. return session->gpsdata.gps_fd;
  397. /* otherwise, could be an TCP data feed */
  398. } else if (str_starts_with(session->gpsdata.dev.path, "tcp://")) {
  399. char server[GPS_PATH_MAX], *port;
  400. socket_t dsock;
  401. (void)strlcpy(server, session->gpsdata.dev.path + 6, sizeof(server));
  402. INVALIDATE_SOCKET(session->gpsdata.gps_fd);
  403. port = strchr(server, ':');
  404. if (port == NULL) {
  405. GPSD_LOG(LOG_ERROR, &session->context->errout,
  406. "Missing colon in TCP feed spec.\n");
  407. return -1;
  408. }
  409. *port++ = '\0';
  410. GPSD_LOG(LOG_INF, &session->context->errout,
  411. "opening TCP feed at %s, port %s.\n", server,
  412. port);
  413. if ((dsock = netlib_connectsock(AF_UNSPEC, server, port, "tcp")) < 0) {
  414. GPSD_LOG(LOG_ERROR, &session->context->errout,
  415. "TCP device open error %s.\n",
  416. netlib_errstr(dsock));
  417. return -1;
  418. } else
  419. GPSD_LOG(LOG_SPIN, &session->context->errout,
  420. "TCP device opened on fd %d\n", dsock);
  421. session->gpsdata.gps_fd = dsock;
  422. session->sourcetype = source_tcp;
  423. return session->gpsdata.gps_fd;
  424. /* or could be UDP */
  425. } else if (str_starts_with(session->gpsdata.dev.path, "udp://")) {
  426. char server[GPS_PATH_MAX], *port;
  427. socket_t dsock;
  428. (void)strlcpy(server, session->gpsdata.dev.path + 6, sizeof(server));
  429. INVALIDATE_SOCKET(session->gpsdata.gps_fd);
  430. port = strchr(server, ':');
  431. if (port == NULL) {
  432. GPSD_LOG(LOG_ERROR, &session->context->errout,
  433. "Missing colon in UDP feed spec.\n");
  434. return -1;
  435. }
  436. *port++ = '\0';
  437. GPSD_LOG(LOG_INF, &session->context->errout,
  438. "opening UDP feed at %s, port %s.\n", server,
  439. port);
  440. if ((dsock = netlib_connectsock(AF_UNSPEC, server, port, "udp")) < 0) {
  441. GPSD_LOG(LOG_ERROR, &session->context->errout,
  442. "UDP device open error %s.\n",
  443. netlib_errstr(dsock));
  444. return -1;
  445. } else
  446. GPSD_LOG(LOG_SPIN, &session->context->errout,
  447. "UDP device opened on fd %d\n", dsock);
  448. session->gpsdata.gps_fd = dsock;
  449. session->sourcetype = source_udp;
  450. return session->gpsdata.gps_fd;
  451. }
  452. #endif /* NETFEED_ENABLE */
  453. #ifdef PASSTHROUGH_ENABLE
  454. if (str_starts_with(session->gpsdata.dev.path, "gpsd://")) {
  455. char server[GPS_PATH_MAX], *port;
  456. socket_t dsock;
  457. (void)strlcpy(server, session->gpsdata.dev.path + 7, sizeof(server));
  458. INVALIDATE_SOCKET(session->gpsdata.gps_fd);
  459. if ((port = strchr(server, ':')) == NULL) {
  460. port = DEFAULT_GPSD_PORT;
  461. } else
  462. *port++ = '\0';
  463. GPSD_LOG(LOG_INF, &session->context->errout,
  464. "opening remote gpsd feed at %s, port %s.\n",
  465. server, port);
  466. if ((dsock = netlib_connectsock(AF_UNSPEC, server, port, "tcp")) < 0) {
  467. GPSD_LOG(LOG_ERROR, &session->context->errout,
  468. "remote gpsd device open error %s.\n",
  469. netlib_errstr(dsock));
  470. return -1;
  471. } else
  472. GPSD_LOG(LOG_SPIN, &session->context->errout,
  473. "remote gpsd feed opened on fd %d\n", dsock);
  474. /* watch to remote is issued when WATCH is */
  475. session->gpsdata.gps_fd = dsock;
  476. session->sourcetype = source_gpsd;
  477. return session->gpsdata.gps_fd;
  478. }
  479. #endif /* PASSTHROUGH_ENABLE */
  480. #if defined(NMEA2000_ENABLE)
  481. if (str_starts_with(session->gpsdata.dev.path, "nmea2000://")) {
  482. return nmea2000_open(session);
  483. }
  484. #endif /* defined(NMEA2000_ENABLE) */
  485. /* fall through to plain serial open */
  486. /* could be a naked /dev/ppsX */
  487. return gpsd_serial_open(session);
  488. }
  489. int gpsd_activate(struct gps_device_t *session, const int mode)
  490. /* acquire a connection to the GPS device */
  491. {
  492. if (mode == O_OPTIMIZE)
  493. gpsd_run_device_hook(&session->context->errout,
  494. session->gpsdata.dev.path, HOOK_ACTIVATE);
  495. session->gpsdata.gps_fd = gpsd_open(session);
  496. if (mode != O_CONTINUE)
  497. session->mode = mode;
  498. // cppcheck-suppress pointerLessThanZero
  499. if (session->gpsdata.gps_fd < 0) {
  500. /* return could be -1, PLACEHOLDING_FD, of UNALLOCATED_FD */
  501. if ( PLACEHOLDING_FD == session->gpsdata.gps_fd ) {
  502. /* it is /dev/ppsX, need to set devicename, etc. */
  503. gpsd_clear(session);
  504. }
  505. return session->gpsdata.gps_fd;
  506. }
  507. #ifdef NON_NMEA0183_ENABLE
  508. /* if it's a sensor, it must be probed */
  509. if ((session->servicetype == service_sensor) &&
  510. (session->sourcetype != source_can)) {
  511. const struct gps_type_t **dp;
  512. for (dp = gpsd_drivers; *dp; dp++) {
  513. if ((*dp)->probe_detect != NULL) {
  514. GPSD_LOG(LOG_PROG, &session->context->errout,
  515. "Probing \"%s\" driver...\n",
  516. (*dp)->type_name);
  517. /* toss stale data */
  518. (void)tcflush(session->gpsdata.gps_fd, TCIOFLUSH);
  519. if ((*dp)->probe_detect(session) != 0) {
  520. GPSD_LOG(LOG_PROG, &session->context->errout,
  521. "Probe found \"%s\" driver...\n",
  522. (*dp)->type_name);
  523. session->device_type = *dp;
  524. gpsd_assert_sync(session);
  525. goto foundit;
  526. } else
  527. GPSD_LOG(LOG_PROG, &session->context->errout,
  528. "Probe not found \"%s\" driver...\n",
  529. (*dp)->type_name);
  530. }
  531. }
  532. GPSD_LOG(LOG_PROG, &session->context->errout,
  533. "no probe matched...\n");
  534. }
  535. foundit:
  536. #endif /* NON_NMEA0183_ENABLE */
  537. gpsd_clear(session);
  538. GPSD_LOG(LOG_INF, &session->context->errout,
  539. "gpsd_activate(%d): activated GPS (fd %d)\n",
  540. session->mode, session->gpsdata.gps_fd);
  541. /*
  542. * We might know the device's type, but we shouldn't assume it has
  543. * retained its settings. A revert hook might well have undone
  544. * them on the previous close. Fire a reactivate event so drivers
  545. * can do something about this if they choose.
  546. */
  547. if (session->device_type != NULL
  548. && session->device_type->event_hook != NULL)
  549. session->device_type->event_hook(session, event_reactivate);
  550. return session->gpsdata.gps_fd;
  551. }
  552. /*****************************************************************************
  553. Carl Carter of SiRF supplied this algorithm for computing DOPs from
  554. a list of visible satellites (some typos corrected)...
  555. For satellite n, let az(n) = azimuth angle from North and el(n) be elevation.
  556. Let:
  557. a(k, 1) = sin az(k) * cos el(k)
  558. a(k, 2) = cos az(k) * cos el(k)
  559. a(k, 3) = sin el(k)
  560. Then form the line-of-sight matrix A for satellites used in the solution:
  561. | a(1,1) a(1,2) a(1,3) 1 |
  562. | a(2,1) a(2,2) a(2,3) 1 |
  563. | : : : : |
  564. | a(n,1) a(n,2) a(n,3) 1 |
  565. And its transpose A~:
  566. |a(1, 1) a(2, 1) . . . a(n, 1) |
  567. |a(1, 2) a(2, 2) . . . a(n, 2) |
  568. |a(1, 3) a(2, 3) . . . a(n, 3) |
  569. | 1 1 . . . 1 |
  570. Compute the covariance matrix (A~*A)^-1, which is guaranteed symmetric:
  571. | s(x)^2 s(x)*s(y) s(x)*s(z) s(x)*s(t) |
  572. | s(y)*s(x) s(y)^2 s(y)*s(z) s(y)*s(t) |
  573. | s(z)*s(x) s(z)*s(y) s(z)^2 s(z)*s(t) |
  574. | s(t)*s(x) s(t)*s(y) s(t)*s(z) s(t)^2 |
  575. Then:
  576. GDOP = sqrt(s(x)^2 + s(y)^2 + s(z)^2 + s(t)^2)
  577. TDOP = sqrt(s(t)^2)
  578. PDOP = sqrt(s(x)^2 + s(y)^2 + s(z)^2)
  579. HDOP = sqrt(s(x)^2 + s(y)^2)
  580. VDOP = sqrt(s(z)^2)
  581. Here's how we implement it...
  582. First, each compute element P(i,j) of the 4x4 product A~*A.
  583. If S(k=1,k=n): f(...) is the sum of f(...) as k varies from 1 to n, then
  584. applying the definition of matrix product tells us:
  585. P(i,j) = S(k=1,k=n): B(i, k) * A(k, j)
  586. But because B is the transpose of A, this reduces to
  587. P(i,j) = S(k=1,k=n): A(k, i) * A(k, j)
  588. This is not, however, the entire algorithm that SiRF uses. Carl writes:
  589. > As you note, with rounding accounted for, most values agree exactly, and
  590. > those that don't agree our number is higher. That is because we
  591. > deweight some satellites and account for that in the DOP calculation.
  592. > If a satellite is not used in a solution at the same weight as others,
  593. > it should not contribute to DOP calculation at the same weight. So our
  594. > internal algorithm does a compensation for that which you would have no
  595. > way to duplicate on the outside since we don't output the weighting
  596. > factors. In fact those are not even available to API users.
  597. Queried about the deweighting, Carl says:
  598. > In the SiRF tracking engine, each satellite track is assigned a quality
  599. > value based on the tracker's estimate of that signal. It includes C/No
  600. > estimate, ability to hold onto the phase, stability of the I vs. Q phase
  601. > angle, etc. The navigation algorithm then ranks all the tracks into
  602. > quality order and selects which ones to use in the solution and what
  603. > weight to give those used in the solution. The process is actually a
  604. > bit of a "trial and error" method -- we initially use all available
  605. > tracks in the solution, then we sequentially remove the lowest quality
  606. > ones until the solution stabilizes. The weighting is inherent in the
  607. > Kalman filter algorithm. Once the solution is stable, the DOP is
  608. > computed from those SVs used, and there is an algorithm that looks at
  609. > the quality ratings and determines if we need to deweight any.
  610. > Likewise, if we use altitude hold mode for a 3-SV solution, we deweight
  611. > the phantom satellite at the center of the Earth.
  612. So we cannot exactly duplicate what SiRF does internally. We'll leave
  613. HDOP alone and use our computed values for VDOP and PDOP. Note, this
  614. may have to change in the future if this code is used by a non-SiRF
  615. driver.
  616. ******************************************************************************/
  617. static gps_mask_t fill_dop(const struct gpsd_errout_t *errout,
  618. const struct gps_data_t * gpsdata,
  619. struct dop_t * dop)
  620. {
  621. double prod[4][4];
  622. double inv[4][4];
  623. double satpos[MAXCHANNELS][4];
  624. double xdop, ydop, hdop, vdop, pdop, tdop, gdop;
  625. int i, j, k, n;
  626. memset(satpos, 0, sizeof(satpos));
  627. for (n = k = 0; k < gpsdata->satellites_visible; k++) {
  628. if (!gpsdata->skyview[k].used) {
  629. /* skip unused sats */
  630. continue;
  631. }
  632. if (1 > gpsdata->skyview[k].PRN) {
  633. /* skip bad PRN */
  634. continue;
  635. }
  636. if (0 == isfinite(gpsdata->skyview[k].azimuth) ||
  637. 0 > gpsdata->skyview[k].azimuth ||
  638. 359 < gpsdata->skyview[k].azimuth) {
  639. /* skip bad azimuth */
  640. continue;
  641. }
  642. if (0 == isfinite(gpsdata->skyview[k].elevation) ||
  643. 90 < fabs(gpsdata->skyview[k].elevation)) {
  644. /* skip bad elevation */
  645. continue;
  646. }
  647. const struct satellite_t *sp = &gpsdata->skyview[k];
  648. satpos[n][0] = sin(sp->azimuth * DEG_2_RAD)
  649. * cos(sp->elevation * DEG_2_RAD);
  650. satpos[n][1] = cos(sp->azimuth * DEG_2_RAD)
  651. * cos(sp->elevation * DEG_2_RAD);
  652. satpos[n][2] = sin(sp->elevation * DEG_2_RAD);
  653. satpos[n][3] = 1;
  654. GPSD_LOG(LOG_INF, errout, "PRN=%3d az=%.1f ael%.1f (%f, %f, %f)\n",
  655. gpsdata->skyview[k].PRN,
  656. gpsdata->skyview[k].azimuth,
  657. gpsdata->skyview[k].elevation,
  658. satpos[n][0], satpos[n][1], satpos[n][2]);
  659. n++;
  660. }
  661. /* can't use gpsdata->satellites_used as that is a counter for xxGSA,
  662. * and gets cleared at odd times */
  663. GPSD_LOG(LOG_INF, errout, "Sats used (%d):\n", n);
  664. /* If we don't have 4 satellites then we don't have enough information to calculate DOPS */
  665. if (n < 4) {
  666. #ifdef __UNUSED__
  667. GPSD_LOG(LOG_RAW, errout,
  668. "Not enough satellites available %d < 4:\n",
  669. n);
  670. #endif /* __UNUSED__ */
  671. return 0; /* Is this correct return code here? or should it be ERROR_SET */
  672. }
  673. memset(prod, 0, sizeof(prod));
  674. memset(inv, 0, sizeof(inv));
  675. #ifdef __UNUSED__
  676. GPSD_LOG(LOG_INF, errout, "Line-of-sight matrix:\n");
  677. for (k = 0; k < n; k++) {
  678. GPSD_LOG(LOG_INF, errout, "%f %f %f %f\n",
  679. satpos[k][0], satpos[k][1], satpos[k][2], satpos[k][3]);
  680. }
  681. #endif /* __UNUSED__ */
  682. for (i = 0; i < 4; ++i) { //< rows
  683. for (j = 0; j < 4; ++j) { //< cols
  684. prod[i][j] = 0.0;
  685. for (k = 0; k < n; ++k) {
  686. prod[i][j] += satpos[k][i] * satpos[k][j];
  687. }
  688. }
  689. }
  690. #ifdef __UNUSED__
  691. GPSD_LOG(LOG_INF, errout, "product:\n");
  692. for (k = 0; k < 4; k++) {
  693. GPSD_LOG(LOG_INF, errout, "%f %f %f %f\n",
  694. prod[k][0], prod[k][1], prod[k][2], prod[k][3]);
  695. }
  696. #endif /* __UNUSED__ */
  697. if (matrix_invert(prod, inv)) {
  698. #ifdef __UNUSED__
  699. /*
  700. * Note: this will print garbage unless all the subdeterminants
  701. * are computed in the invert() function.
  702. */
  703. GPSD_LOG(LOG_RAW, errout, "inverse:\n");
  704. for (k = 0; k < 4; k++) {
  705. GPSD_LOG(LOG_RAW, errout,
  706. "%f %f %f %f\n",
  707. inv[k][0], inv[k][1], inv[k][2], inv[k][3]);
  708. }
  709. #endif /* __UNUSED__ */
  710. } else {
  711. #ifndef USE_QT
  712. GPSD_LOG(LOG_DATA, errout,
  713. "LOS matrix is singular, can't calculate DOPs - source '%s'\n",
  714. gpsdata->dev.path);
  715. #endif
  716. return 0;
  717. }
  718. xdop = sqrt(inv[0][0]);
  719. ydop = sqrt(inv[1][1]);
  720. hdop = sqrt(inv[0][0] + inv[1][1]);
  721. vdop = sqrt(inv[2][2]);
  722. pdop = sqrt(inv[0][0] + inv[1][1] + inv[2][2]);
  723. tdop = sqrt(inv[3][3]);
  724. gdop = sqrt(inv[0][0] + inv[1][1] + inv[2][2] + inv[3][3]);
  725. #ifndef USE_QT
  726. GPSD_LOG(LOG_DATA, errout,
  727. "DOPS computed/reported: X=%f/%f, Y=%f/%f, H=%f/%f, V=%f/%f, "
  728. "P=%f/%f, T=%f/%f, G=%f/%f\n",
  729. xdop, dop->xdop, ydop, dop->ydop, hdop, dop->hdop, vdop,
  730. dop->vdop, pdop, dop->pdop, tdop, dop->tdop, gdop, dop->gdop);
  731. #endif
  732. /* Check to see which DOPs we already have. Save values if no value
  733. * from the GPS. Do not overwrite values which came from the GPS */
  734. if (isfinite(dop->xdop) == 0) {
  735. dop->xdop = xdop;
  736. }
  737. if (isfinite(dop->ydop) == 0) {
  738. dop->ydop = ydop;
  739. }
  740. if (isfinite(dop->hdop) == 0) {
  741. dop->hdop = hdop;
  742. }
  743. if (isfinite(dop->vdop) == 0) {
  744. dop->vdop = vdop;
  745. }
  746. if (isfinite(dop->pdop) == 0) {
  747. dop->pdop = pdop;
  748. }
  749. if (isfinite(dop->tdop) == 0) {
  750. dop->tdop = tdop;
  751. }
  752. if (isfinite(dop->gdop) == 0) {
  753. dop->gdop = gdop;
  754. }
  755. return DOP_SET;
  756. }
  757. /* compute errors and derived quantities
  758. * also a handy place to do final sanity checking */
  759. static void gpsd_error_model(struct gps_device_t *session)
  760. {
  761. struct gps_fix_t *fix; /* current fix */
  762. struct gps_fix_t *lastfix; /* last fix, maybe same time stamp */
  763. struct gps_fix_t *oldfix; /* old fix, previsou time stamp */
  764. double deltatime = -1.0; /* time span to compute rates */
  765. /*
  766. * Now we compute derived quantities. This is where the tricky error-
  767. * modeling stuff goes. Presently we don't know how to derive
  768. * time error.
  769. *
  770. * Some drivers set the position-error fields. Only the Zodiacs
  771. * report speed error. No NMEA 183 reports climb error. GPXTE
  772. * and PSRFEPE can report track error, but are rare.
  773. *
  774. * The UERE constants are our assumption about the base error of
  775. * GPS fixes in different directions.
  776. */
  777. #define H_UERE_NO_DGPS 15.0 /* meters, 95% confidence */
  778. #define H_UERE_WITH_DGPS 3.75 /* meters, 95% confidence */
  779. #define V_UERE_NO_DGPS 23.0 /* meters, 95% confidence */
  780. #define V_UERE_WITH_DGPS 5.75 /* meters, 95% confidence */
  781. #define P_UERE_NO_DGPS 19.0 /* meters, 95% confidence */
  782. #define P_UERE_WITH_DGPS 4.75 /* meters, 95% confidence */
  783. double h_uere, v_uere, p_uere;
  784. if (NULL == session)
  785. return;
  786. fix = &session->gpsdata.fix;
  787. lastfix = &session->lastfix;
  788. oldfix = &session->oldfix;
  789. if (0 < fix->time.tv_sec) {
  790. /* we have a time for this merge data */
  791. deltatime = TS_SUB_D(&fix->time, &lastfix->time);
  792. if (0.0099 < fabs(deltatime)) {
  793. /* Time just moved, probably forward at least 10 ms.
  794. * Lastfix is now the previous (old) fix. */
  795. *oldfix = *lastfix;
  796. } else {
  797. // compute delta from old fix
  798. deltatime = TS_SUB_D(&fix->time, &oldfix->time);
  799. }
  800. }
  801. /* Sanity check for negative delta? */
  802. h_uere =
  803. (session->gpsdata.status ==
  804. STATUS_DGPS_FIX ? H_UERE_WITH_DGPS : H_UERE_NO_DGPS);
  805. v_uere =
  806. (session->gpsdata.status ==
  807. STATUS_DGPS_FIX ? V_UERE_WITH_DGPS : V_UERE_NO_DGPS);
  808. p_uere =
  809. (session->gpsdata.status ==
  810. STATUS_DGPS_FIX ? P_UERE_WITH_DGPS : P_UERE_NO_DGPS);
  811. if (0 == isfinite(fix->latitude) ||
  812. 0 == isfinite(fix->longitude) || /* both lat/lon, or none */
  813. 90.0 < fabs(fix->latitude) || /* lat out of range */
  814. 180.0 < fabs(fix->longitude)) { /* lon out of range */
  815. fix->latitude = fix->longitude = NAN;
  816. }
  817. /* validate ECEF */
  818. if (0 == isfinite(fix->ecef.x) ||
  819. 0 == isfinite(fix->ecef.y) ||
  820. 0 == isfinite(fix->ecef.z) ||
  821. 10.0 >= (fabs(fix->ecef.x) +
  822. fabs(fix->ecef.y) +
  823. fabs(fix->ecef.z))) { /* all zeros */
  824. fix->ecef.x = fix->ecef.y = fix->ecef.z = NAN;
  825. }
  826. /* if we have not lat/lon, but do have ECEF, calculate lat/lon */
  827. if ((0 == isfinite(fix->longitude) ||
  828. 0 == isfinite(fix->latitude)) &&
  829. 0 != isfinite(fix->ecef.x)) {
  830. session->gpsdata.set |= ecef_to_wgs84fix(fix,
  831. fix->ecef.x, fix->ecef.y,
  832. fix->ecef.z, fix->ecef.vx,
  833. fix->ecef.vy, fix->ecef.vz);
  834. }
  835. /* If you are in a rocket, and your GPS is ITAR unlocked, then
  836. * triple check these sanity checks.
  837. *
  838. * u-blox 8: Max altitude: 50,000m
  839. * Max horizontal speed: 250 m/s
  840. * Max climb: 100 m/s
  841. *
  842. * u-blox ZED-F9P: Max Velocity: 500 m/s
  843. */
  844. /* sanity check the speed, 10,000 m/s should be a nice max
  845. * Low Earth Orbit (LEO) is about 7,800 m/s */
  846. if (9999.9 < fabs(fix->speed))
  847. fix->speed = NAN;
  848. if (9999.9 < fabs(fix->NED.velN))
  849. fix->NED.velN = NAN;
  850. if (9999.9 < fabs(fix->NED.velE))
  851. fix->NED.velE = NAN;
  852. if (9999.9 < fabs(fix->NED.velD))
  853. fix->NED.velD = NAN;
  854. /* sanity check the climb, 10,000 m/s should be a nice max */
  855. if (9999.9 < fabs(fix->climb))
  856. fix->climb = NAN;
  857. if (0 != isfinite(fix->NED.velD) &&
  858. 0 == isfinite(fix->climb)) {
  859. /* have good velD, use it for climb */
  860. fix->climb = -fix->NED.velD;
  861. }
  862. /* compute speed and track from velN and velE if needed and possible */
  863. if (0 != isfinite(fix->NED.velN) &&
  864. 0 != isfinite(fix->NED.velE)) {
  865. if (0 == isfinite(fix->speed)) {
  866. fix->speed = hypot(fix->NED.velN, fix->NED.velE);
  867. }
  868. if (0 == isfinite(fix->track)) {
  869. fix->track = atan2(fix->NED.velE, fix->NED.velN) * RAD_2_DEG;
  870. // normalized later
  871. }
  872. }
  873. /*
  874. * OK, this is not an error computation, but we're at the right
  875. * place in the architecture for it. Compute geoid separation
  876. * and altHAE and altMSL in the simplest possible way.
  877. */
  878. /* geoid (ellipsoid) separation and variation */
  879. if (0 != isfinite(fix->latitude) &&
  880. 0 != isfinite(fix->longitude)) {
  881. if (0 == isfinite(fix->geoid_sep)) {
  882. fix->geoid_sep = wgs84_separation(fix->latitude,
  883. fix->longitude);
  884. }
  885. if (0 == isfinite(fix->magnetic_var) ||
  886. 0.09 >= fabs(fix->magnetic_var)) {
  887. /* some GPS set 0.0,E, or 0,W instead of blank */
  888. fix->magnetic_var = mag_var(fix->latitude,
  889. fix->longitude);
  890. }
  891. }
  892. if (0 != isfinite(fix->magnetic_var)) {
  893. if (0 == isfinite(fix->magnetic_track) &&
  894. 0 != isfinite(fix->track)) {
  895. // calculate mag track, normalized later
  896. fix->magnetic_track = fix->track + fix->magnetic_var;
  897. } else if (0 != isfinite(fix->magnetic_track) &&
  898. 0 == isfinite(fix->track)) {
  899. // calculate true track, normalized later
  900. fix->track = fix->magnetic_track - fix->magnetic_var;
  901. }
  902. }
  903. if (0 != isfinite(fix->track)) {
  904. // normalize true track
  905. DEG_NORM(fix->track);
  906. }
  907. if (0 != isfinite(fix->magnetic_track)) {
  908. // normalize mag track
  909. DEG_NORM(fix->magnetic_track);
  910. }
  911. if (0 != isfinite(fix->geoid_sep)) {
  912. if (0 != isfinite(fix->altHAE) &&
  913. 0 == isfinite(fix->altMSL)) {
  914. /* compute missing altMSL */
  915. fix->altMSL = fix->altHAE - fix->geoid_sep;
  916. } else if (0 == isfinite(fix->altHAE) &&
  917. 0 != isfinite(fix->altMSL)) {
  918. /* compute missing altHAE */
  919. fix->altHAE = fix->altMSL + fix->geoid_sep;
  920. }
  921. }
  922. /*
  923. * OK, this is not an error computation, but we're at the right
  924. * place in the architecture for it. Compute speed over ground
  925. * and climb/sink in the simplest possible way.
  926. */
  927. #ifdef __UNUSED__
  928. // debug code
  929. {
  930. char tbuf[JSON_DATE_MAX+1];
  931. GPSD_LOG(LOG_SHOUT, &session->context->errout,
  932. "time %s deltatime %f\n",
  933. timespec_to_iso8601(fix->time, tbuf, sizeof(tbuf)),
  934. deltatime);
  935. }
  936. #endif // __UNUSED__
  937. if (0 < deltatime) {
  938. /* have a valid time duration */
  939. /* FIXME! ignore if large. maybe > 1 hour? */
  940. if (MODE_2D <= fix->mode &&
  941. MODE_2D <= oldfix->mode) {
  942. if (0 == isfinite(fix->speed)) {
  943. fix->speed = earth_distance(fix->latitude, fix->longitude,
  944. oldfix->latitude, oldfix->longitude)
  945. / deltatime;
  946. /* sanity check */
  947. if (9999.9 < fabs(fix->speed))
  948. fix->speed = NAN;
  949. }
  950. if (MODE_3D <= fix->mode &&
  951. MODE_3D <= oldfix->mode &&
  952. 0 == isfinite(fix->climb) &&
  953. 0 != isfinite(fix->altHAE) &&
  954. 0 != isfinite(oldfix->altHAE)) {
  955. fix->climb = (fix->altHAE - oldfix->altHAE) / deltatime;
  956. /* sanity check the climb */
  957. if (9999.9 < fabs(fix->climb))
  958. fix->climb = NAN;
  959. }
  960. }
  961. }
  962. /*
  963. * Field reports match the theoretical prediction that
  964. * expected time error should be half the resolution of
  965. * the GPS clock, so we put the bound of the error
  966. * in as a constant pending getting it from each driver.
  967. *
  968. * In an ideal world, we'd increase this if no leap-second has
  969. * been seen and it's less than 750s (one almanac load cycle) from
  970. * device powerup. Alas, we have no way to know when device
  971. * powerup occurred - depending on the receiver design it could be
  972. * when the hardware was first powered up or when it was first
  973. * opened. Also, some devices (notably plain NMEA0183 receivers)
  974. * never ship an indication of when they have valid leap second.
  975. */
  976. if (0 < fix->time.tv_sec &&
  977. 0 == isfinite(fix->ept)) {
  978. /* can we compute ept from tdop? */
  979. fix->ept = 0.005;
  980. }
  981. /* Other error computations depend on having a valid fix */
  982. if (MODE_2D <= fix->mode) {
  983. if (0 == isfinite(fix->epx) &&
  984. 0 != isfinite(session->gpsdata.dop.hdop)) {
  985. fix->epx = session->gpsdata.dop.xdop * h_uere;
  986. }
  987. if (0 == isfinite(fix->epy) &&
  988. 0 != isfinite(session->gpsdata.dop.hdop)) {
  989. fix->epy = session->gpsdata.dop.ydop * h_uere;
  990. }
  991. if (MODE_3D <= fix->mode &&
  992. 0 == isfinite(fix->epv) &&
  993. 0 != isfinite(session->gpsdata.dop.vdop)) {
  994. fix->epv = session->gpsdata.dop.vdop * v_uere;
  995. }
  996. /* 2D error */
  997. if (0 == isfinite(fix->eph) &&
  998. 0 != isfinite(session->gpsdata.dop.hdop)) {
  999. fix->eph = session->gpsdata.dop.hdop * p_uere;
  1000. }
  1001. /* 3D error */
  1002. if (0 == isfinite(fix->sep) &&
  1003. 0 != isfinite(session->gpsdata.dop.pdop)) {
  1004. fix->sep = session->gpsdata.dop.pdop * p_uere;
  1005. }
  1006. /*
  1007. * If we have a current fix and an old fix, and the packet handler
  1008. * didn't set the speed error, climb error or track error members
  1009. * itself, try to compute them now.
  1010. */
  1011. #define EMAX(x, y) (((x) > (y)) ? (x) : (y))
  1012. if (0 < deltatime &&
  1013. MODE_2D <= oldfix->mode) {
  1014. if (0 == isfinite(fix->eps) &&
  1015. 0 != isfinite(oldfix->epx) &&
  1016. 0 != isfinite(oldfix->epy)) {
  1017. fix->eps = (EMAX(oldfix->epx, oldfix->epy) +
  1018. EMAX(fix->epx, fix->epy)) / deltatime;
  1019. }
  1020. if (0 == isfinite(fix->epd)) {
  1021. /*
  1022. * We compute a track error estimate solely from the
  1023. * position of this fix and the last one. The maximum
  1024. * track error, as seen from the position of last fix, is
  1025. * the angle subtended by the two most extreme possible
  1026. * error positions of the current fix; the expected track
  1027. * error is half that. Let the position of the old fix be
  1028. * A and of the new fix B. We model the view from A as
  1029. * two right triangles ABC and ABD with BC and BD both
  1030. * having the length of the new fix's estimated error.
  1031. * adj = len(AB), opp = len(BC) = len(BD), hyp = len(AC) =
  1032. * len(AD). This leads to spurious uncertainties
  1033. * near 180 when we're moving slowly; to avoid reporting
  1034. * garbage, throw back NaN if the distance from the previous
  1035. * fix is less than the error estimate.
  1036. */
  1037. double adj = earth_distance(oldfix->latitude, oldfix->longitude,
  1038. fix->latitude, fix->longitude);
  1039. double opp = EMAX(fix->epx, fix->epy);
  1040. if (isfinite(adj) != 0 && adj > opp) {
  1041. double hyp = sqrt(adj * adj + opp * opp);
  1042. fix->epd = RAD_2_DEG * 2 * asin(opp / hyp);
  1043. }
  1044. }
  1045. if (0 == isfinite(fix->epc) &&
  1046. 0 != isfinite(fix->epv) &&
  1047. 0 != isfinite(oldfix->epv)) {
  1048. /* Is this really valid? */
  1049. /* if vertical uncertainties are zero this will be too */
  1050. fix->epc = (oldfix->epv + fix->epv) / deltatime;
  1051. }
  1052. }
  1053. }
  1054. #ifdef __UNUSED__
  1055. {
  1056. // Debug code.
  1057. char tbuf[JSON_DATE_MAX+1];
  1058. GPSD_LOG(&session->context->errout, 0,
  1059. "DEBUG: %s deltatime %.3f, speed %0.3f climb %.3f "
  1060. "epc %.3f fixHAE %.3f oldHAE %.3f\n",
  1061. timespec_to_iso8601(fix->time, tbuf, sizeof(tbuf)),
  1062. deltatime, fix->speed, fix->climb, fix->epc,
  1063. fix->altHAE, oldfix->altHAE);
  1064. }
  1065. #endif // __UNUSED__
  1066. if (0 < fix->time.tv_sec) {
  1067. /* save lastfix, not yet oldfix, for later error computations */
  1068. *lastfix = *fix;
  1069. }
  1070. }
  1071. int gpsd_await_data(fd_set *rfds,
  1072. fd_set *efds,
  1073. const int maxfd,
  1074. fd_set *all_fds,
  1075. struct gpsd_errout_t *errout)
  1076. /* await data from any socket in the all_fds set */
  1077. {
  1078. int status;
  1079. FD_ZERO(efds);
  1080. *rfds = *all_fds;
  1081. GPSD_LOG(LOG_RAW + 1, errout, "select waits\n");
  1082. /*
  1083. * Poll for user commands or GPS data. The timeout doesn't
  1084. * actually matter here since select returns whenever one of
  1085. * the file descriptors in the set goes ready. The point
  1086. * of tracking maxfd is to keep the set of descriptors that
  1087. * pselect(2) has to poll here as small as possible (for
  1088. * low-clock-rate SBCs and the like).
  1089. *
  1090. * pselect(2) is preferable to vanilla select, to eliminate
  1091. * the once-per-second wakeup when no sensors are attached.
  1092. * This cuts power consumption.
  1093. */
  1094. errno = 0;
  1095. status = pselect(maxfd + 1, rfds, NULL, NULL, NULL, NULL);
  1096. if (status == -1) {
  1097. if (errno == EINTR)
  1098. return AWAIT_NOT_READY;
  1099. else if (errno == EBADF) {
  1100. int fd;
  1101. for (fd = 0; fd < (int)FD_SETSIZE; fd++)
  1102. /*
  1103. * All we care about here is a cheap, fast, uninterruptible
  1104. * way to check if a file descriptor is valid.
  1105. */
  1106. if (FD_ISSET(fd, all_fds) && fcntl(fd, F_GETFL, 0) == -1) {
  1107. FD_CLR(fd, all_fds);
  1108. FD_SET(fd, efds);
  1109. }
  1110. return AWAIT_NOT_READY;
  1111. } else {
  1112. GPSD_LOG(LOG_ERROR, errout, "select: %s\n", strerror(errno));
  1113. return AWAIT_FAILED;
  1114. }
  1115. }
  1116. if (errout->debug >= LOG_SPIN) {
  1117. int i;
  1118. char dbuf[BUFSIZ];
  1119. timespec_t ts_now;
  1120. char ts_str[TIMESPEC_LEN];
  1121. dbuf[0] = '\0';
  1122. for (i = 0; i < (int)FD_SETSIZE; i++)
  1123. if (FD_ISSET(i, all_fds))
  1124. str_appendf(dbuf, sizeof(dbuf), "%d ", i);
  1125. str_rstrip_char(dbuf, ' ');
  1126. (void)strlcat(dbuf, "} -> {", sizeof(dbuf));
  1127. for (i = 0; i < (int)FD_SETSIZE; i++)
  1128. if (FD_ISSET(i, rfds))
  1129. str_appendf(dbuf, sizeof(dbuf), " %d ", i);
  1130. (void)clock_gettime(CLOCK_REALTIME, &ts_now);
  1131. GPSD_LOG(LOG_SPIN, errout,
  1132. "pselect() {%s} at %s (errno %d)\n",
  1133. dbuf,
  1134. timespec_str(&ts_now, ts_str, sizeof(ts_str)),
  1135. errno);
  1136. }
  1137. return AWAIT_GOT_INPUT;
  1138. }
  1139. static bool hunt_failure(struct gps_device_t *session)
  1140. /* after a bad packet, what should cue us to go to next autobaud setting? */
  1141. {
  1142. /*
  1143. * We have tried three different tests here.
  1144. *
  1145. * The first was session->badcount++>1. This worked very well on
  1146. * ttys for years and years, but caused failure to sync on TCP/IP
  1147. * sources, which have I/O boundaries in mid-packet more often
  1148. * than RS232 ones. There's a test for this at
  1149. * test/daemon/tcp-torture.log.
  1150. *
  1151. * The second was session->badcount++>1 && session->lexer.state==0.
  1152. * Fail hunt only if we get a second consecutive bad packet
  1153. * and the lexer is in ground state. We don't want to fail on
  1154. * a first bad packet because the source might have a burst of
  1155. * leading garbage after open. We don't want to fail if the
  1156. * lexer is not in ground state, because that means the read
  1157. * might have picked up a valid partial packet - better to go
  1158. * back around the loop and pick up more data.
  1159. *
  1160. * The "&& session->lexer.state==0" guard causes an intermittent
  1161. * hang while autobauding on SiRF IIIs (but not on SiRF-IIs, oddly
  1162. * enough). Removing this conjunct resurrected the failure
  1163. * of test/daemon/tcp-torture.log.
  1164. *
  1165. * Our third attempt, isatty(session->gpsdata.gps_fd) != 0
  1166. * && session->badcount++>1, reverts to the old test that worked
  1167. * well on ttys for ttys and prevents non-tty devices from *ever*
  1168. * having hunt failures. This has the cost that non-tty devices
  1169. * will never get kicked off for presenting bad packets.
  1170. *
  1171. * This test may need further revision.
  1172. */
  1173. return isatty(session->gpsdata.gps_fd) != 0 && session->badcount++>1;
  1174. }
  1175. gps_mask_t gpsd_poll(struct gps_device_t *session)
  1176. /* update the stuff in the scoreboard structure */
  1177. {
  1178. ssize_t newlen;
  1179. bool driver_change = false;
  1180. timespec_t ts_now;
  1181. timespec_t delta;
  1182. char ts_buf[TIMESPEC_LEN];
  1183. gps_clear_fix(&session->newdata);
  1184. /*
  1185. * Input just became available from a sensor, but no read from the
  1186. * device has yet been done.
  1187. *
  1188. * What we actually do here is trickier. For latency-timing
  1189. * purposes, we want to know the time at the start of the current
  1190. * recording cycle. We rely on the fact that even at 4800bps
  1191. * there's a quiet time perceptible to the human eye in gpsmon
  1192. * between when the last character of the last packet in a
  1193. * 1-second cycle ships and when the next reporting cycle
  1194. * ships. Because the cycle time is fixed, higher baud rates will
  1195. * make this gap larger.
  1196. *
  1197. * Thus, we look for an inter-character delay much larger than an
  1198. * average 4800bps sentence time. How should this delay be set? Well,
  1199. * counting framing bits and erring on the side of caution, it's
  1200. * about 480 characters per second or 2083 microeconds per character;
  1201. * that's almost exactly 0.125 seconds per average 60-char sentence.
  1202. * Doubling this to avoid false positives, we look for an inter-character
  1203. * delay of greater than 0.250s.
  1204. *
  1205. * The above assumes a cycle time of 1 second. To get the minimum size of
  1206. * the quiet period, we multiply by the device cycle time.
  1207. *
  1208. * We can sanity-check these calculation by watching logs. If we have set
  1209. * MINIMUM_QUIET_TIME correctly, the "transmission pause" message below
  1210. * will consistently be emitted just before the sentence that shows up
  1211. * as start-of-cycle in gpsmon, and never emitted at any other point
  1212. * in the cycle.
  1213. *
  1214. * In practice, it seems that edge detection succeeds at 9600bps but
  1215. * fails at 4800bps. This is not surprising, as previous profiling has
  1216. * indicated that at 4800bps some devices overrun a 1-second cycle time
  1217. * with the data they transmit.
  1218. */
  1219. #define MINIMUM_QUIET_TIME 0.25
  1220. if (session->lexer.outbuflen == 0) {
  1221. /* beginning of a new packet */
  1222. (void)clock_gettime(CLOCK_REALTIME, &ts_now);
  1223. if (NULL != session->device_type &&
  1224. (0 < session->lexer.start_time.tv_sec ||
  1225. 0 < session->lexer.start_time.tv_nsec)) {
  1226. #ifdef RECONFIGURE_ENABLE
  1227. const double min_cycle = TSTONS(&session->device_type->min_cycle);
  1228. #else
  1229. // Assume that all GNSS receivers are 1Hz
  1230. const double min_cycle = 1;
  1231. #endif /* RECONFIGURE_ENABLE */
  1232. double quiet_time = (MINIMUM_QUIET_TIME * min_cycle);
  1233. double gap;
  1234. gap = TS_SUB_D(&ts_now, &session->lexer.start_time);
  1235. if (gap > min_cycle)
  1236. GPSD_LOG(LOG_WARN, &session->context->errout,
  1237. "cycle-start detector failed.\n");
  1238. else if (gap > quiet_time) {
  1239. GPSD_LOG(LOG_PROG, &session->context->errout,
  1240. "transmission pause of %f\n", gap);
  1241. session->sor = ts_now;
  1242. session->lexer.start_char = session->lexer.char_counter;
  1243. }
  1244. }
  1245. session->lexer.start_time = ts_now;
  1246. }
  1247. if (session->lexer.type >= COMMENT_PACKET) {
  1248. session->observed |= PACKET_TYPEMASK(session->lexer.type);
  1249. }
  1250. /* can we get a full packet from the device? */
  1251. if (session->device_type != NULL) {
  1252. newlen = session->device_type->get_packet(session);
  1253. /* coverity[deref_ptr] */
  1254. GPSD_LOG(LOG_RAW, &session->context->errout,
  1255. "%s is known to be %s\n",
  1256. session->gpsdata.dev.path,
  1257. session->device_type->type_name);
  1258. } else {
  1259. newlen = generic_get(session);
  1260. }
  1261. /* update the scoreboard structure from the GPS */
  1262. GPSD_LOG(LOG_RAW + 1, &session->context->errout,
  1263. "%s sent %zd new characters\n",
  1264. session->gpsdata.dev.path, newlen);
  1265. (void)clock_gettime(CLOCK_REALTIME, &ts_now);
  1266. TS_SUB(&delta, &ts_now, &session->gpsdata.online);
  1267. if (newlen < 0) { /* read error */
  1268. GPSD_LOG(LOG_INF, &session->context->errout,
  1269. "GPS on %s returned error %zd (%s sec since data)\n",
  1270. session->gpsdata.dev.path, newlen,
  1271. timespec_str(&delta, ts_buf, sizeof(ts_buf)));
  1272. session->gpsdata.online.tv_sec = 0;
  1273. session->gpsdata.online.tv_nsec = 0;
  1274. return ERROR_SET;
  1275. } else if (newlen == 0) { /* zero length read, possible EOF */
  1276. /*
  1277. * Multiplier is 2 to avoid edge effects due to sampling at the exact
  1278. * wrong time...
  1279. */
  1280. if (0 < session->gpsdata.online.tv_sec &&
  1281. // FIXME: do this with integer math...
  1282. TSTONS(&delta) >= (TSTONS(&session->gpsdata.dev.cycle) * 2)) {
  1283. GPSD_LOG(LOG_INF, &session->context->errout,
  1284. "GPS on %s is offline (%s sec since data)\n",
  1285. session->gpsdata.dev.path,
  1286. timespec_str(&delta, ts_buf, sizeof(ts_buf)));
  1287. session->gpsdata.online.tv_sec = 0;
  1288. session->gpsdata.online.tv_nsec = 0;
  1289. }
  1290. return NODATA_IS;
  1291. } else /* (newlen > 0) */ {
  1292. GPSD_LOG(LOG_RAW, &session->context->errout,
  1293. "packet sniff on %s finds type %d\n",
  1294. session->gpsdata.dev.path, session->lexer.type);
  1295. if (session->lexer.type == COMMENT_PACKET) {
  1296. if (strcmp((const char *)session->lexer.outbuffer, "# EOF\n") == 0) {
  1297. GPSD_LOG(LOG_PROG, &session->context->errout,
  1298. "synthetic EOF\n");
  1299. return EOF_IS;
  1300. }
  1301. else
  1302. GPSD_LOG(LOG_PROG, &session->context->errout,
  1303. "comment, sync lock deferred\n");
  1304. /* FALL THROUGH */
  1305. } else if (session->lexer.type > COMMENT_PACKET) {
  1306. if (session->device_type == NULL)
  1307. driver_change = true;
  1308. else {
  1309. int newtype = session->lexer.type;
  1310. /*
  1311. * Are we seeing a new packet type? Then we probably
  1312. * want to change drivers.
  1313. */
  1314. bool new_packet_type =
  1315. (newtype != session->device_type->packet_type);
  1316. /*
  1317. * Possibly the old driver has a mode-switcher method, in
  1318. * which case we know it can handle NMEA itself and may
  1319. * want to do special things (like tracking whether a
  1320. * previous mode switch to binary succeeded in suppressing
  1321. * NMEA).
  1322. */
  1323. #ifdef RECONFIGURE_ENABLE
  1324. bool dependent_nmea = (newtype == NMEA_PACKET &&
  1325. session->device_type->mode_switcher != NULL);
  1326. #else
  1327. bool dependent_nmea = false;
  1328. #endif /* RECONFIGURE_ENABLE */
  1329. /*
  1330. * Compute whether to switch drivers.
  1331. * If the previous driver type was sticky and this one
  1332. * isn't, we'll revert after processing the packet.
  1333. */
  1334. driver_change = new_packet_type && !dependent_nmea;
  1335. }
  1336. if (driver_change) {
  1337. const struct gps_type_t **dp;
  1338. for (dp = gpsd_drivers; *dp; dp++)
  1339. if (session->lexer.type == (*dp)->packet_type) {
  1340. GPSD_LOG(LOG_PROG, &session->context->errout,
  1341. "switching to match packet type %d: %s\n",
  1342. session->lexer.type, gpsd_prettydump(session));
  1343. (void)gpsd_switch_driver(session, (*dp)->type_name);
  1344. break;
  1345. }
  1346. }
  1347. session->badcount = 0;
  1348. session->gpsdata.dev.driver_mode =
  1349. (session->lexer.type > NMEA_PACKET) ? MODE_BINARY : MODE_NMEA;
  1350. /* FALL THROUGH */
  1351. } else if (hunt_failure(session) && !gpsd_next_hunt_setting(session)) {
  1352. (void)clock_gettime(CLOCK_REALTIME, &ts_now);
  1353. TS_SUB(&delta, &ts_now, &session->gpsdata.online);
  1354. GPSD_LOG(LOG_INF, &session->context->errout,
  1355. "hunt on %s failed (%s sec since data)\n",
  1356. session->gpsdata.dev.path,
  1357. timespec_str(&delta, ts_buf, sizeof(ts_buf)));
  1358. return ERROR_SET;
  1359. }
  1360. }
  1361. if (session->lexer.outbuflen == 0) { /* got new data, but no packet */
  1362. GPSD_LOG(LOG_RAW + 1, &session->context->errout,
  1363. "New data on %s, not yet a packet\n",
  1364. session->gpsdata.dev.path);
  1365. return ONLINE_SET;
  1366. } else { /* we have recognized a packet */
  1367. gps_mask_t received = PACKET_SET;
  1368. (void)clock_gettime(CLOCK_REALTIME, &session->gpsdata.online);
  1369. GPSD_LOG(LOG_RAW + 1, &session->context->errout,
  1370. "Accepted packet on %s.\n",
  1371. session->gpsdata.dev.path);
  1372. /* track the packet count since achieving sync on the device */
  1373. if (driver_change &&
  1374. (session->drivers_identified & (1 << session->driver_index)) == 0) {
  1375. speed_t speed = gpsd_get_speed(session);
  1376. /* coverity[var_deref_op] */
  1377. GPSD_LOG(LOG_INF, &session->context->errout,
  1378. "%s identified as type %s, %ld sec @ %ubps\n",
  1379. session->gpsdata.dev.path,
  1380. session->device_type->type_name,
  1381. (long)(time(NULL) - session->opentime),
  1382. (unsigned int)speed);
  1383. /* fire the init_query method */
  1384. if (session->device_type != NULL
  1385. && session->device_type->init_query != NULL) {
  1386. /*
  1387. * We can force readonly off knowing this method does
  1388. * not alter device state.
  1389. */
  1390. bool saved = session->context->readonly;
  1391. session->context->readonly = false;
  1392. session->device_type->init_query(session);
  1393. session->context->readonly = saved;
  1394. }
  1395. /* fire the identified hook */
  1396. if (session->device_type != NULL
  1397. && session->device_type->event_hook != NULL)
  1398. session->device_type->event_hook(session, event_identified);
  1399. session->lexer.counter = 0;
  1400. /* let clients know about this. */
  1401. received |= DRIVER_IS;
  1402. /* mark the fact that this driver has been seen */
  1403. session->drivers_identified |= (1 << session->driver_index);
  1404. } else
  1405. session->lexer.counter++;
  1406. /* fire the configure hook, on every packet. Seems excessive... */
  1407. if (session->device_type != NULL
  1408. && session->device_type->event_hook != NULL)
  1409. session->device_type->event_hook(session, event_configure);
  1410. /*
  1411. * The guard looks superfluous, but it keeps the rather expensive
  1412. * gpsd_packetdump() function from being called even when the debug
  1413. * level does not actually require it.
  1414. */
  1415. if (session->context->errout.debug >= LOG_RAW)
  1416. GPSD_LOG(LOG_RAW, &session->context->errout,
  1417. "raw packet of type %d, %zd:%s\n",
  1418. session->lexer.type,
  1419. session->lexer.outbuflen,
  1420. gpsd_prettydump(session));
  1421. /* Get data from current packet into the fix structure */
  1422. if (session->lexer.type != COMMENT_PACKET)
  1423. if (session->device_type != NULL
  1424. && session->device_type->parse_packet != NULL)
  1425. received |= session->device_type->parse_packet(session);
  1426. #ifdef RECONFIGURE_ENABLE
  1427. /*
  1428. * We may want to revert to the last driver that was marked
  1429. * sticky. What this accomplishes is that if we've just
  1430. * processed something like AIVDM, but a driver with control
  1431. * methods or an event hook had been active before that, we
  1432. * keep the information about those capabilities.
  1433. */
  1434. if (!STICKY(session->device_type)
  1435. && session->last_controller != NULL
  1436. && STICKY(session->last_controller))
  1437. {
  1438. session->device_type = session->last_controller;
  1439. GPSD_LOG(LOG_PROG, &session->context->errout,
  1440. "reverted to %s driver...\n",
  1441. session->device_type->type_name);
  1442. }
  1443. #endif /* RECONFIGURE_ENABLE */
  1444. /* are we going to generate a report? if so, count characters */
  1445. if ((received & REPORT_IS) != 0) {
  1446. session->chars = session->lexer.char_counter - session->lexer.start_char;
  1447. }
  1448. session->gpsdata.set = ONLINE_SET | received;
  1449. /*
  1450. * Compute fix-quality data from the satellite positions.
  1451. * These will not overwrite any DOPs reported from the packet
  1452. * we just got.
  1453. */
  1454. if ((received & SATELLITE_SET) != 0
  1455. && session->gpsdata.satellites_visible > 0) {
  1456. session->gpsdata.set |= fill_dop(&session->context->errout,
  1457. &session->gpsdata,
  1458. &session->gpsdata.dop);
  1459. }
  1460. /* copy/merge device data into staging buffers */
  1461. if ((session->gpsdata.set & CLEAR_IS) != 0) {
  1462. /* CLEAR_IS should only be set on first sentence of cycle */
  1463. gps_clear_fix(&session->gpsdata.fix);
  1464. gps_clear_att(&session->gpsdata.attitude);
  1465. }
  1466. /* GPSD_LOG(LOG_PROG, &session->context->errout,
  1467. "transfer mask: %s\n",
  1468. gps_maskdump(session->gpsdata.set)); */
  1469. gps_merge_fix(&session->gpsdata.fix,
  1470. session->gpsdata.set, &session->newdata);
  1471. gpsd_error_model(session);
  1472. /*
  1473. * Count good fixes. We used to check
  1474. * session->gpsdata.status > STATUS_NO_FIX
  1475. * here, but that wasn't quite right. That tells us whether
  1476. * we think we have a valid fix for the current cycle, but remains
  1477. * true while following non-fix packets are received. What we
  1478. * really want to know is whether the last packet received was a
  1479. * fix packet AND held a valid fix. We must ignore non-fix packets
  1480. * AND packets which have fix data but are flagged as invalid. Some
  1481. * devices output fix packets on a regular basis, even when unable
  1482. * to derive a good fix. Such packets should set STATUS_NO_FIX.
  1483. */
  1484. if (0 != (session->gpsdata.set & (LATLON_SET|ECEF_SET))) {
  1485. if ( session->gpsdata.status > STATUS_NO_FIX) {
  1486. session->context->fixcnt++;
  1487. session->fixcnt++;
  1488. } else {
  1489. session->context->fixcnt = 0;
  1490. session->fixcnt = 0;
  1491. }
  1492. }
  1493. /*
  1494. * Sanity check. This catches a surprising number of port and
  1495. * driver errors, including 32-vs.-64-bit problems.
  1496. */
  1497. if ((session->gpsdata.set & TIME_SET) != 0) {
  1498. if (session->newdata.time.tv_sec >
  1499. (time(NULL) + (60 * 60 * 24 * 365))) {
  1500. GPSD_LOG(LOG_WARN, &session->context->errout,
  1501. "date (%lld) more than a year in the future!\n",
  1502. (long long)session->newdata.time.tv_sec);
  1503. } else if (session->newdata.time.tv_sec < 0) {
  1504. GPSD_LOG(LOG_ERROR, &session->context->errout,
  1505. "date (%lld) is negative!\n",
  1506. (long long)session->newdata.time.tv_sec);
  1507. }
  1508. }
  1509. return session->gpsdata.set;
  1510. }
  1511. /* Should never get here */
  1512. GPSD_LOG(LOG_EMERG, &session->context->errout,
  1513. "fell out of gps_poll()!\n");
  1514. return 0;
  1515. }
  1516. int gpsd_multipoll(const bool data_ready,
  1517. struct gps_device_t *device,
  1518. void (*handler)(struct gps_device_t *, gps_mask_t),
  1519. float reawake_time)
  1520. /* consume and handle packets from a specified device */
  1521. {
  1522. if (data_ready)
  1523. {
  1524. int fragments;
  1525. GPSD_LOG(LOG_RAW + 1, &device->context->errout,
  1526. "polling %d\n", device->gpsdata.gps_fd);
  1527. #ifdef NETFEED_ENABLE
  1528. /*
  1529. * Strange special case - the opening transaction on an NTRIP connection
  1530. * may not yet be completed. Try to ratchet things forward.
  1531. */
  1532. if (device->servicetype == service_ntrip
  1533. && device->ntrip.conn_state != ntrip_conn_established) {
  1534. (void)ntrip_open(device, "");
  1535. if (device->ntrip.conn_state == ntrip_conn_err) {
  1536. GPSD_LOG(LOG_WARN, &device->context->errout,
  1537. "connection to ntrip server failed\n");
  1538. device->ntrip.conn_state = ntrip_conn_init;
  1539. return DEVICE_ERROR;
  1540. } else {
  1541. return DEVICE_READY;
  1542. }
  1543. }
  1544. #endif /* NETFEED_ENABLE */
  1545. for (fragments = 0; ; fragments++) {
  1546. gps_mask_t changed = gpsd_poll(device);
  1547. if (changed == EOF_IS) {
  1548. GPSD_LOG(LOG_WARN, &device->context->errout,
  1549. "device signed off %s\n",
  1550. device->gpsdata.dev.path);
  1551. return DEVICE_EOF;
  1552. } else if (changed == ERROR_SET) {
  1553. GPSD_LOG(LOG_WARN, &device->context->errout,
  1554. "device read of %s returned error or "
  1555. "packet sniffer failed sync (flags %s)\n",
  1556. device->gpsdata.dev.path,
  1557. gps_maskdump(changed));
  1558. return DEVICE_ERROR;
  1559. } else if (changed == NODATA_IS) {
  1560. /*
  1561. * No data on the first fragment read means the device
  1562. * fd may have been in an end-of-file condition on select.
  1563. */
  1564. if (fragments == 0) {
  1565. GPSD_LOG(LOG_DATA, &device->context->errout,
  1566. "%s returned zero bytes\n",
  1567. device->gpsdata.dev.path);
  1568. if (device->zerokill) {
  1569. /* failed timeout-and-reawake, kill it */
  1570. gpsd_deactivate(device);
  1571. if (device->ntrip.works) {
  1572. device->ntrip.works = false; // reset so we try this once only
  1573. if (gpsd_activate(device, O_CONTINUE) < 0) {
  1574. GPSD_LOG(LOG_WARN, &device->context->errout,
  1575. "reconnect to ntrip server failed\n");
  1576. return DEVICE_ERROR;
  1577. } else {
  1578. GPSD_LOG(LOG_INF, &device->context->errout,
  1579. "reconnecting to ntrip server\n");
  1580. return DEVICE_READY;
  1581. }
  1582. }
  1583. } else if (reawake_time == 0) {
  1584. return DEVICE_ERROR;
  1585. } else {
  1586. /*
  1587. * Disable listening to this fd for long enough
  1588. * that the buffer can fill up again.
  1589. */
  1590. GPSD_LOG(LOG_DATA, &device->context->errout,
  1591. "%s will be repolled in %f seconds\n",
  1592. device->gpsdata.dev.path, reawake_time);
  1593. device->reawake = time(NULL) + reawake_time;
  1594. return DEVICE_UNREADY;
  1595. }
  1596. }
  1597. /*
  1598. * No data on later fragment reads just means the
  1599. * input buffer is empty. In this case break out
  1600. * of the fragment-processing loop but consider
  1601. * the device still good.
  1602. */
  1603. break;
  1604. }
  1605. /* we got actual data, head off the reawake special case */
  1606. device->zerokill = false;
  1607. device->reawake = (time_t)0;
  1608. /* must have a full packet to continue */
  1609. if ((changed & PACKET_SET) == 0)
  1610. break;
  1611. /* conditional prevents mask dumper from eating CPU */
  1612. if (device->context->errout.debug >= LOG_DATA) {
  1613. if (device->lexer.type == BAD_PACKET)
  1614. GPSD_LOG(LOG_DATA, &device->context->errout,
  1615. "packet with bad checksum from %s\n",
  1616. device->gpsdata.dev.path);
  1617. else
  1618. GPSD_LOG(LOG_DATA, &device->context->errout,
  1619. "packet type %d from %s with %s\n",
  1620. device->lexer.type,
  1621. device->gpsdata.dev.path,
  1622. gps_maskdump(device->gpsdata.set));
  1623. }
  1624. /* handle data contained in this packet */
  1625. if (device->lexer.type != BAD_PACKET)
  1626. handler(device, changed);
  1627. #ifdef __future__
  1628. // this breaks: test/daemon/passthrough.log ??
  1629. /*
  1630. * Bernd Ocklin suggests:
  1631. * Exit when a full packet was received and parsed.
  1632. * This allows other devices to be serviced even if
  1633. * this device delivers a full packet at every single
  1634. * read.
  1635. * Otherwise we can sit here for a long time without
  1636. * any for-loop exit condition being met.
  1637. * It might also reduce the latency from a received packet to
  1638. * it being output by gpsd.
  1639. */
  1640. if ((changed & PACKET_SET) != 0)
  1641. break;
  1642. #endif /* __future__ */
  1643. }
  1644. }
  1645. else if (device->reawake>0 && time(NULL) >device->reawake) {
  1646. /* device may have had a zero-length read */
  1647. GPSD_LOG(LOG_DATA, &device->context->errout,
  1648. "%s reawakened after zero-length read\n",
  1649. device->gpsdata.dev.path);
  1650. device->reawake = (time_t)0;
  1651. device->zerokill = true;
  1652. return DEVICE_READY;
  1653. }
  1654. /* no change in device descriptor state */
  1655. return DEVICE_UNCHANGED;
  1656. }
  1657. void gpsd_wrap(struct gps_device_t *session)
  1658. /* end-of-session wrapup */
  1659. {
  1660. if (!BAD_SOCKET(session->gpsdata.gps_fd))
  1661. gpsd_deactivate(session);
  1662. }
  1663. void gpsd_zero_satellites( struct gps_data_t *out)
  1664. {
  1665. int sat;
  1666. (void)memset(out->skyview, '\0', sizeof(out->skyview));
  1667. out->satellites_visible = 0;
  1668. /* zero is good inbound data for ss, elevation, and azimuth. */
  1669. /* we need to set them to invalid values */
  1670. for ( sat = 0; sat < MAXCHANNELS; sat++ ) {
  1671. out->skyview[sat].azimuth = NAN;
  1672. out->skyview[sat].elevation = NAN;
  1673. out->skyview[sat].ss = NAN;
  1674. out->skyview[sat].freqid = -1;
  1675. }
  1676. #if 0
  1677. /*
  1678. * We used to clear DOPs here, but this causes misbehavior on some
  1679. * combined GPS/GLONASS/QZSS receivers like the Telit SL869; the
  1680. * symptom is that the "satellites_used" field in a struct gps_data_t
  1681. * filled in by gps_read() is always zero.
  1682. */
  1683. gps_clear_dop(&out->dop);
  1684. #endif
  1685. }
  1686. /* Latch the fact that we've saved a fix.
  1687. * And add in the device fudge */
  1688. void ntp_latch(struct gps_device_t *device, struct timedelta_t *td)
  1689. {
  1690. /* this should be an invariant of the way this function is called */
  1691. if (0 >= device->newdata.time.tv_sec) {
  1692. return;
  1693. }
  1694. (void)clock_gettime(CLOCK_REALTIME, &td->clock);
  1695. /* structure copy of time from GPS */
  1696. td->real = device->newdata.time;
  1697. /* is there an offset method? */
  1698. if (NULL != device->device_type &&
  1699. NULL != device->device_type->time_offset) {
  1700. double integral;
  1701. double offset = device->device_type->time_offset(device);
  1702. /* add in offset which is double */
  1703. td->real.tv_nsec += (long)(modf(offset, &integral) * 1e9);
  1704. td->real.tv_sec += (time_t)integral;
  1705. TS_NORM(&td->real);
  1706. }
  1707. /* thread-safe update */
  1708. pps_thread_fixin(&device->pps_thread, td);
  1709. }
  1710. /* end */