libgps_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* libgps_core.c -- client interface library for the gpsd daemon
  2. *
  3. * Core portion of client library. Cals helpers to handle different eports.
  4. *
  5. * This file is Copyright (c) 2010-2018 by the GPSD project
  6. * SPDX-License-Identifier: BSD-2-clause
  7. */
  8. #include "gpsd_config.h" /* must be before all includes */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdarg.h>
  12. #include <stdbool.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15. #include <errno.h>
  16. #include <stdarg.h>
  17. #include "gpsd.h"
  18. #include "libgps.h"
  19. #include "gps_json.h"
  20. #include "strfuncs.h"
  21. #ifdef LIBGPS_DEBUG
  22. int libgps_debuglevel = 0;
  23. static FILE *debugfp;
  24. void gps_enable_debug(int level, FILE * fp)
  25. /* control the level and destination of debug trace messages */
  26. {
  27. libgps_debuglevel = level;
  28. debugfp = fp;
  29. #if defined(CLIENTDEBUG_ENABLE) && defined(SOCKET_EXPORT_ENABLE)
  30. json_enable_debug(level - DEBUG_JSON, fp);
  31. #endif
  32. }
  33. void libgps_trace(int errlevel, const char *fmt, ...)
  34. /* assemble command in printf(3) style */
  35. {
  36. if (errlevel <= libgps_debuglevel) {
  37. char buf[BUFSIZ];
  38. va_list ap;
  39. (void)strlcpy(buf, "libgps: ", sizeof(buf));
  40. va_start(ap, fmt);
  41. str_vappendf(buf, sizeof(buf), fmt, ap);
  42. va_end(ap);
  43. (void)fputs(buf, debugfp);
  44. }
  45. }
  46. #else
  47. // Functions defined as so to furfil the API but otherwise do nothing when built with debug capability turned off
  48. void gps_enable_debug(int level UNUSED, FILE * fp UNUSED) {}
  49. void libgps_trace(int errlevel UNUSED, const char *fmt UNUSED, ...){}
  50. #endif /* LIBGPS_DEBUG */
  51. #ifdef SOCKET_EXPORT_ENABLE
  52. #define CONDITIONALLY_UNUSED
  53. #else
  54. #define CONDITIONALLY_UNUSED UNUSED
  55. #endif /* SOCKET_EXPORT_ENABLE */
  56. int gps_open(const char *host,
  57. const char *port CONDITIONALLY_UNUSED,
  58. struct gps_data_t *gpsdata)
  59. {
  60. int status = -1;
  61. if (!gpsdata)
  62. return -1;
  63. #ifdef SHM_EXPORT_ENABLE
  64. if (host != NULL && strcmp(host, GPSD_SHARED_MEMORY) == 0) {
  65. status = gps_shm_open(gpsdata);
  66. if (status == -1)
  67. status = SHM_NOSHARED;
  68. else if (status == -2)
  69. status = SHM_NOATTACH;
  70. }
  71. #define USES_HOST
  72. #endif /* SHM_EXPORT_ENABLE */
  73. #ifdef DBUS_EXPORT_ENABLE
  74. if (host != NULL && strcmp(host, GPSD_DBUS_EXPORT) == 0) {
  75. status = gps_dbus_open(gpsdata);
  76. if (status != 0)
  77. status = DBUS_FAILURE;
  78. }
  79. #define USES_HOST
  80. #endif /* DBUS_EXPORT_ENABLE */
  81. #ifdef SOCKET_EXPORT_ENABLE
  82. if (status == -1) {
  83. status = gps_sock_open(host, port, gpsdata);
  84. }
  85. #define USES_HOST
  86. #endif /* SOCKET_EXPORT_ENABLE */
  87. #ifndef USES_HOST
  88. (void)fprintf(stderr,
  89. "No methods available for connnecting to %s!\n",
  90. host);
  91. #endif /* USES_HOST */
  92. #undef USES_HOST
  93. gpsdata->set = 0;
  94. gpsdata->status = STATUS_NO_FIX;
  95. gpsdata->satellites_used = 0;
  96. gps_clear_att(&(gpsdata->attitude));
  97. gps_clear_dop(&(gpsdata->dop));
  98. gps_clear_fix(&(gpsdata->fix));
  99. return status;
  100. }
  101. #if defined(SHM_EXPORT_ENABLE) || defined(SOCKET_EXPORT_ENABLE)
  102. #define CONDITIONALLY_UNUSED
  103. #else
  104. #define CONDITIONALLY_UNUSED UNUSED
  105. #endif
  106. int gps_close(struct gps_data_t *gpsdata CONDITIONALLY_UNUSED)
  107. /* close a gpsd connection */
  108. {
  109. int status = -1;
  110. libgps_debug_trace((DEBUG_CALLS, "gps_close()\n"));
  111. #ifdef SHM_EXPORT_ENABLE
  112. if (BAD_SOCKET((intptr_t)(gpsdata->gps_fd))) {
  113. gps_shm_close(gpsdata);
  114. status = 0;
  115. }
  116. #endif /* SHM_EXPORT_ENABLE */
  117. #ifdef SOCKET_EXPORT_ENABLE
  118. if (status == -1) {
  119. status = gps_sock_close(gpsdata);
  120. }
  121. #endif /* SOCKET_EXPORT_ENABLE */
  122. return status;
  123. }
  124. /* read from a gpsd connection
  125. *
  126. * parameters:
  127. * gps_data_t *gpsdata -- structure for GPS data
  128. * char *message -- NULL, or optional buffer for received JSON
  129. * int message_len -- zero, or sizeof(message)
  130. */
  131. int gps_read(struct gps_data_t *gpsdata CONDITIONALLY_UNUSED,
  132. char *message, int message_len)
  133. {
  134. int status = -1;
  135. libgps_debug_trace((DEBUG_CALLS, "gps_read() begins\n"));
  136. if ((NULL != message) && (0 < message_len)) {
  137. /* be sure message is zero length */
  138. /* we do not memset() as this is time critical input path */
  139. *message = '\0';
  140. }
  141. #ifdef SHM_EXPORT_ENABLE
  142. if (BAD_SOCKET((intptr_t)(gpsdata->gps_fd))) {
  143. status = gps_shm_read(gpsdata);
  144. }
  145. #endif /* SHM_EXPORT_ENABLE */
  146. #ifdef SOCKET_EXPORT_ENABLE
  147. if (status == -1 && !BAD_SOCKET((intptr_t)(gpsdata->gps_fd))) {
  148. status = gps_sock_read(gpsdata, message, message_len);
  149. }
  150. #endif /* SOCKET_EXPORT_ENABLE */
  151. libgps_debug_trace((DEBUG_CALLS, "gps_read() -> %d (%s)\n",
  152. status, gps_maskdump(gpsdata->set)));
  153. return status;
  154. }
  155. int gps_send(struct gps_data_t *gpsdata CONDITIONALLY_UNUSED, const char *fmt CONDITIONALLY_UNUSED, ...)
  156. /* send a command to the gpsd instance */
  157. {
  158. int status = -1;
  159. char buf[BUFSIZ];
  160. va_list ap;
  161. va_start(ap, fmt);
  162. (void)vsnprintf(buf, sizeof(buf) - 2, fmt, ap);
  163. va_end(ap);
  164. if (buf[strlen(buf) - 1] != '\n')
  165. (void)strlcat(buf, "\n", sizeof(buf));
  166. #ifdef SOCKET_EXPORT_ENABLE
  167. status = gps_sock_send(gpsdata, buf);
  168. #endif /* SOCKET_EXPORT_ENABLE */
  169. return status;
  170. }
  171. int gps_stream(struct gps_data_t *gpsdata CONDITIONALLY_UNUSED,
  172. unsigned int flags CONDITIONALLY_UNUSED,
  173. void *d CONDITIONALLY_UNUSED)
  174. {
  175. int status = -1;
  176. #ifdef SOCKET_EXPORT_ENABLE
  177. /* cppcheck-suppress redundantAssignment */
  178. status = gps_sock_stream(gpsdata, flags, d);
  179. #endif /* SOCKET_EXPORT_ENABLE */
  180. return status;
  181. }
  182. const char *gps_data(const struct gps_data_t *gpsdata CONDITIONALLY_UNUSED)
  183. /* return the contents of the client data buffer */
  184. {
  185. const char *bufp = NULL;
  186. #ifdef SOCKET_EXPORT_ENABLE
  187. bufp = gps_sock_data(gpsdata);
  188. #endif /* SOCKET_EXPORT_ENABLE */
  189. return bufp;
  190. }
  191. bool gps_waiting(const struct gps_data_t *gpsdata CONDITIONALLY_UNUSED, int timeout CONDITIONALLY_UNUSED)
  192. /* is there input waiting from the GPS? */
  193. /* timeout is in uSec */
  194. {
  195. /* this is bogus, but I can't think of a better solution yet */
  196. bool waiting = true;
  197. #ifdef SHM_EXPORT_ENABLE
  198. if ((intptr_t)(gpsdata->gps_fd) == SHM_PSEUDO_FD)
  199. waiting = gps_shm_waiting(gpsdata, timeout);
  200. #endif /* SHM_EXPORT_ENABLE */
  201. #ifdef SOCKET_EXPORT_ENABLE
  202. // cppcheck-suppress pointerPositive
  203. if ((intptr_t)(gpsdata->gps_fd) >= 0)
  204. waiting = gps_sock_waiting(gpsdata, timeout);
  205. #endif /* SOCKET_EXPORT_ENABLE */
  206. return waiting;
  207. }
  208. int gps_mainloop(struct gps_data_t *gpsdata CONDITIONALLY_UNUSED,
  209. int timeout CONDITIONALLY_UNUSED,
  210. void (*hook)(struct gps_data_t *gpsdata) CONDITIONALLY_UNUSED)
  211. {
  212. int status = -1;
  213. libgps_debug_trace((DEBUG_CALLS, "gps_mainloop() begins\n"));
  214. #ifdef SHM_EXPORT_ENABLE
  215. if ((intptr_t)(gpsdata->gps_fd) == SHM_PSEUDO_FD)
  216. status = gps_shm_mainloop(gpsdata, timeout, hook);
  217. #endif /* SHM_EXPORT_ENABLE */
  218. #ifdef DBUS_EXPORT_ENABLE
  219. if ((intptr_t)(gpsdata->gps_fd) == DBUS_PSEUDO_FD)
  220. status = gps_dbus_mainloop(gpsdata, timeout, hook);
  221. #endif /* DBUS_EXPORT_ENABLE */
  222. #ifdef SOCKET_EXPORT_ENABLE
  223. if ((intptr_t)(gpsdata->gps_fd) >= 0)
  224. status = gps_sock_mainloop(gpsdata, timeout, hook);
  225. #endif /* SOCKET_EXPORT_ENABLE */
  226. libgps_debug_trace((DEBUG_CALLS, "gps_mainloop() -> %d (%s)\n",
  227. status, gps_maskdump(gpsdata->set)));
  228. return status;
  229. }
  230. extern const char *gps_errstr(const int err)
  231. {
  232. /*
  233. * We might add our own error codes in the future, e.g for
  234. * protocol compatibility checks
  235. */
  236. #ifndef USE_QT
  237. #ifdef SHM_EXPORT_ENABLE
  238. if (err == SHM_NOSHARED)
  239. return "no shared-memory segment or daemon not running";
  240. else if (err == SHM_NOATTACH)
  241. return "attach failed for unknown reason";
  242. #endif /* SHM_EXPORT_ENABLE */
  243. #ifdef DBUS_EXPORT_ENABLE
  244. if (err == DBUS_FAILURE)
  245. return "DBUS initialization failure";
  246. #endif /* DBUS_EXPORT_ENABLE */
  247. return netlib_errstr(err);
  248. #else
  249. static char buf[32];
  250. (void)snprintf(buf, sizeof(buf), "Qt error %d", err);
  251. return buf;
  252. #endif
  253. }
  254. #ifdef LIBGPS_DEBUG
  255. void libgps_dump_state(struct gps_data_t *collect)
  256. {
  257. char ts_buf[TIMESPEC_LEN];
  258. /* no need to dump the entire state, this is a sanity check */
  259. #ifndef USE_QT
  260. (void)fprintf(debugfp, "flags: (0x%04x) %s\n",
  261. (unsigned int)collect->set, gps_maskdump(collect->set));
  262. #endif
  263. if (collect->set & ONLINE_SET)
  264. (void)fprintf(debugfp, "ONLINE: %s\n",
  265. timespec_str(&collect->online, ts_buf, sizeof(ts_buf)));
  266. if (collect->set & TIME_SET)
  267. (void)fprintf(debugfp, "TIME: %s\n",
  268. timespec_str(&collect->fix.time, ts_buf, sizeof(ts_buf)));
  269. /* NOTE: %.7f needed for cm level accurate GPS */
  270. if (collect->set & LATLON_SET)
  271. (void)fprintf(debugfp, "LATLON: lat/lon: %.7lf %.7lf\n",
  272. collect->fix.latitude, collect->fix.longitude);
  273. if (collect->set & ALTITUDE_SET)
  274. (void)fprintf(debugfp, "ALTITUDE: altHAE: %lf U: climb: %lf\n",
  275. collect->fix.altHAE, collect->fix.climb);
  276. if (collect->set & SPEED_SET)
  277. (void)fprintf(debugfp, "SPEED: %lf\n", collect->fix.speed);
  278. if (collect->set & TRACK_SET)
  279. (void)fprintf(debugfp, "TRACK: track: %lf\n", collect->fix.track);
  280. if (collect->set & MAGNETIC_TRACK_SET)
  281. (void)fprintf(debugfp, "MAGNETIC_TRACK: magtrack: %lf\n",
  282. collect->fix.magnetic_track);
  283. if (collect->set & CLIMB_SET)
  284. (void)fprintf(debugfp, "CLIMB: climb: %lf\n", collect->fix.climb);
  285. if (collect->set & STATUS_SET) {
  286. const char *status_values[] = { "NO_FIX", "FIX", "DGPS_FIX" };
  287. (void)fprintf(debugfp, "STATUS: status: %d (%s)\n",
  288. collect->status, status_values[collect->status]);
  289. }
  290. if (collect->set & MODE_SET) {
  291. const char *mode_values[] = { "", "NO_FIX", "MODE_2D", "MODE_3D" };
  292. (void)fprintf(debugfp, "MODE: mode: %d (%s)\n",
  293. collect->fix.mode, mode_values[collect->fix.mode]);
  294. }
  295. if (collect->set & DOP_SET)
  296. (void)fprintf(debugfp,
  297. "DOP: satellites %d, pdop=%lf, hdop=%lf, vdop=%lf\n",
  298. collect->satellites_used, collect->dop.pdop,
  299. collect->dop.hdop, collect->dop.vdop);
  300. if (collect->set & VERSION_SET)
  301. (void)fprintf(debugfp, "VERSION: release=%s rev=%s proto=%d.%d\n",
  302. collect->version.release,
  303. collect->version.rev,
  304. collect->version.proto_major,
  305. collect->version.proto_minor);
  306. if (collect->set & POLICY_SET)
  307. (void)fprintf(debugfp,
  308. "POLICY: watcher=%s nmea=%s raw=%d scaled=%s timing=%s, split24=%s pps=%s, devpath=%s\n",
  309. collect->policy.watcher ? "true" : "false",
  310. collect->policy.nmea ? "true" : "false",
  311. collect->policy.raw,
  312. collect->policy.scaled ? "true" : "false",
  313. collect->policy.timing ? "true" : "false",
  314. collect->policy.split24 ? "true" : "false",
  315. collect->policy.pps ? "true" : "false",
  316. collect->policy.devpath);
  317. if (collect->set & SATELLITE_SET) {
  318. struct satellite_t *sp;
  319. (void)fprintf(debugfp, "SKY: satellites in view: %d\n",
  320. collect->satellites_visible);
  321. for (sp = collect->skyview;
  322. sp < collect->skyview + collect->satellites_visible;
  323. sp++) {
  324. (void)fprintf(debugfp, " %2.2d: %4.1f %5.1f %3.0f %c\n",
  325. sp->PRN, sp->elevation,
  326. sp->azimuth, sp->ss,
  327. sp->used ? 'Y' : 'N');
  328. }
  329. }
  330. if (collect->set & RAW_SET)
  331. (void)fprintf(debugfp, "RAW: got raw data\n");
  332. if (collect->set & DEVICE_SET)
  333. (void)fprintf(debugfp, "DEVICE: Device is '%s', driver is '%s'\n",
  334. collect->dev.path, collect->dev.driver);
  335. if (collect->set & DEVICELIST_SET) {
  336. int i;
  337. (void)fprintf(debugfp, "DEVICELIST:%d devices:\n",
  338. collect->devices.ndevices);
  339. for (i = 0; i < collect->devices.ndevices; i++) {
  340. (void)fprintf(debugfp, "%d: path='%s' driver='%s'\n",
  341. collect->devices.ndevices,
  342. collect->devices.list[i].path,
  343. collect->devices.list[i].driver);
  344. }
  345. }
  346. }
  347. #endif /* LIBGPS_DEBUG */
  348. // end