libgps.adoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. = libgps(3)
  2. :author: Eric S. Raymond
  3. :date: 7 March 2021
  4. :email: <esr@thyrsus.com.>
  5. :keywords: gps, gpsd, libgps
  6. :manmanual: GPSD Documentation
  7. :mansource: GPSD, Version {gpsdver}
  8. :robots: index,follow
  9. :sectlinks:
  10. :toc: macro
  11. :type: manpage
  12. :webfonts!:
  13. include::../www/inc-menu.adoc[]
  14. == NAME
  15. libgps - C service library for communicating with the GPS daemon
  16. == SYNOPSIS
  17. $$C:$$
  18. ----
  19. #include <gps.h>
  20. int gps_open(char * server, char * port, struct gps_data_t * gpsdata)
  21. int gps_send(struct gps_data_t * gpsdata, char * fmt, ...)
  22. int gps_read(struct gps_data_t * gpsdata, char * message,
  23. int message_size)
  24. bool gps_waiting(const struct gps_data_t * gpsdata, int timeout)
  25. char * gps_data(const struct gps_data_t * gpsdata)
  26. int gps_unpack(char * buf, struct gps_data_t * gpsdata)
  27. int gps_close(struct gps_data_t * gpsdata)
  28. int gps_stream(struct gps_data_t * gpsdata, unsigned int flags,
  29. void * data)
  30. int gps_mainloop(struct gps_data_t * gpsdata, int timeout,
  31. void (* hook)(struct gps_data_t *gpsdata))
  32. const char * gps_errstr(int err)
  33. ----
  34. Python:
  35. ----
  36. import gps
  37. session = gps.gps(host="localhost", port="2947")
  38. session.stream(flags=gps.WATCH_JSON)
  39. for report in session:
  40. process(report) del session
  41. ----
  42. == DESCRIPTION
  43. *libgps* is a service library which supports communicating with an
  44. instance of the *gpsd*(8), link it with the linker option *-lgps*.
  45. Some systems may also require *-lm*.
  46. [WARNING]
  47. ====
  48. Take care to conditionalize your code on the major and minor API version
  49. symbols in *gps.h*; ideally, force a compilation failure if
  50. GPSD_API_MAJOR_VERSION is not a version you recognize. See the GPSD
  51. project website for more information on the protocol and API changes.
  52. ====
  53. All the functions described here use the *gps_data_t* structure.
  54. Consult *gps.h* to learn more about *gps_data_t*, its data members,
  55. associated structures. associated timestamps. Note that information will
  56. accumulate in the session structure over time, and the 'valid' field is
  57. not automatically zeroed by each *gps_read()*. It is up to the client
  58. to zero that field when appropriate and to keep an eye on the fix and
  59. sentence timestamps.
  60. [WARNING]
  61. ====
  62. *gps_data_t* sets floating point variables to NaN when the actual
  63. variable value is unknown. Check all floats and doubles with *isfinite()*
  64. before using them. *isnan()* is not sufficient!
  65. ====
  66. *gps_open()*::
  67. Calling *gps_open()* initializes a *gps_data_t* structure to hold the data
  68. collected by the GPS, and sets up access to *gpsd*(8) via either the socket
  69. or shared-memory export. The shared-memory export is faster, but does
  70. not carry information about device activation and deactivation events
  71. and will not allow you to monitor device packet traffic.
  72. +
  73. *gps_open()* returns 0 on success, -1 on errors and is re-entrant.
  74. errno is set depending on the error returned from the socket or
  75. shared-memory interface; see *gps.h* for values and explanations; also
  76. see *gps_errstr()*. The host address may be a DNS name, an IPv4 dotted
  77. quad, an IPV6 address, or the special value *GPSD_SHARED_MEMORY*
  78. referring to the shared-memory export; the library will do the right
  79. thing for any of these.
  80. *gps_close()*::
  81. *gps_close()* ends the session and should only be called after a
  82. successful *gps_open()*. It returns 0 on success, -1 on errors. The
  83. shared-memory interface close always returns 0, whereas a socket close
  84. can result in an error. For a socket close error it will have set an
  85. errno from the call to the system's *close()*.
  86. *gps_send()*::
  87. *gps_send()* writes a command to the *gpsd* daemon. It does nothing when
  88. using the shared-memory export. The second argument must be a format
  89. string containing elements from the command set documented at *gpsd*(8).
  90. It may have % elements as for sprintf(3), which will be filled in
  91. from any following arguments. This function returns a -1 if there was
  92. a Unix-level write error, otherwise 0. Please read the LIMITATIONS
  93. section for additional information and cautions. See *gps_stream()* as a
  94. possible alternative.
  95. *gps_read()*::
  96. *gps_read()* accepts a response, or sequence of responses, from the
  97. *gpsd* daemon and decodes the response into a *gps_data_t*. By default,
  98. this function does either a blocking read for data from the *gpsd*
  99. daemon or a fetch from shared memory; it returns a count of bytes read
  100. for success, -1 with errno set on a Unix-level read error, -1 with
  101. errno not set if the socket to the *gpsd* daemon has closed or if the
  102. shared-memory segment was unavailable, and 0 if no data is available.
  103. +
  104. The second argument to *gps_read()* is usually NULL, and the third
  105. argument is zero. If your application wants to see the raw data from
  106. the *gpsd* daemon then set the second argument to the address of your
  107. message buffer, and the third argument is the size of your buffer. Use
  108. with care; this may not to be a NUL-terminated string if WATCH_RAW is
  109. enabled.
  110. *gps_waiting()*::
  111. *gps_waiting()* can be used to check whether there is new data from the
  112. *gpsd* daemon. The second argument is the maximum amount of time to block
  113. (in microseconds) waiting on input before returning. It returns true if there is
  114. input waiting, false on timeout (no data waiting) or error condition.
  115. When using the socket export, this function is a convenience wrapper
  116. around a *select*(2) call, and zeros *errno* on entry; you can test *errno*
  117. after exit to get more information about error conditions.
  118. +
  119. Warning: under the shared-memory interface there is a tiny race window
  120. between *gps_waiting()* and a following *gps_read()*; in that context,
  121. because the latter does not block, it is probably better to write a
  122. simple read loop.
  123. *gps_mainloop()*::
  124. *gps_mainloop()* enables the provided hook function to be continually
  125. called whenever there is *gpsd* data. The second argument is the maximum
  126. amount of time to wait (in microseconds) on input before exiting the
  127. loop (and return a value of -1). It will also return a negative value on
  128. various errors.
  129. *gps_unpack()*::
  130. *gps_unpack()* parses JSON from the argument buffer into the target of
  131. the session structure pointer argument. Included in case your
  132. application wishes to manage socket I/O itself.
  133. *gps_data()*::
  134. *gps_data()* returns the contents of the client data buffer (it
  135. returns NULL when using the shared-memory export). Use with care; this
  136. may not to be a NUL-terminated string if WATCH_RAW is enabled.
  137. *gps_stream()*::
  138. *gps_stream()* asks *gpsd* to stream the reports it has at you, to be
  139. made available when you poll (not available when using the shared-memory
  140. export). The second argument is a flag mask that sets various policy
  141. bits; see the list below. Calling *gps_stream()* more than once with
  142. different flag masks is allowed.
  143. *WATCH_DEVICE*;;
  144. Restrict watching to a specified device. The device path string is
  145. given as the third argument (data).
  146. *WATCH_DISABLE*;;
  147. Disable the reporting modes specified by the other WATCH_ flags.
  148. *WATCH_ENABLE*;;
  149. Enable the reporting modes specified by the other WATCH_ flags. This
  150. is the default.
  151. *WATCH_JSON*;;
  152. Enable JSON reporting of data. If WATCH_ENABLE is set, and no other
  153. WATCH flags are set, this is the default.
  154. *WATCH_NEWSTYLE*;;
  155. Force issuing a JSON initialization and getting new-style responses.
  156. This is the default.
  157. *WATCH_NMEA*;;
  158. Enable generated pseudo-NMEA reporting on binary devices.
  159. *WATCH_OLDSTYLE*;;
  160. Force issuing a W or R command and getting old-style responses.
  161. Warning: this flag (and the capability) will be removed in a future
  162. release.
  163. *WATCH_RARE*;;
  164. Enable reporting of binary packets in encoded hex.
  165. *WATCH_RAW*;;
  166. Enable literal passthrough of binary packets.
  167. *WATCH_SCALED*;;
  168. When reporting AIS or Subframe data, scale integer quantities to
  169. floats if they have a divisor or rendering formula associated with
  170. them.
  171. *gps_errstr()*::
  172. *gps_errstr()* returns an ASCII string (in English) describing the
  173. error indicated by a nonzero return value from *gps_open()*.
  174. The Python implementation supports the same facilities as the
  175. socket-export calls in the C library; there is no shared-memory
  176. interface. *gps_open()* is replaced by the initialization of a gps
  177. session object; the other calls are methods of that object, and have the
  178. same names as the corresponding C functions. However, it is simpler just
  179. to use the session object as an iterator, as in the example given below.
  180. Resources within the session object will be properly released when it is
  181. garbage-collected.
  182. ----
  183. import gps
  184. session = gps.gps(host="localhost", port="2947")
  185. session.stream(flags=gps.WATCH_JSON)
  186. for report in session:
  187. process(report) del session
  188. ----
  189. == ENVIRONMENT VARIABLES
  190. By setting the environment variable *GPSD_SHM_KEY*, you can control
  191. the key value used to create shared-memory segment used for
  192. communication with *gpsd*. This will be useful mainly when isolating test
  193. instances of *gpsd* from production ones.
  194. == EXAMPLES
  195. The following is a fully functional minimal C client. Check
  196. the C source for the other *gpsd* clients for more ideas.
  197. ----
  198. // example gpsd client
  199. // compile this way:
  200. // gcc example.c -o example -lgps -lm
  201. #include <gps.h>
  202. #include <math.h> // for isfinite()
  203. #include <unistd.h> // for sleep()
  204. #define MODE_STR_NUM 4
  205. static char *mode_str[MODE_STR_NUM] = {
  206. "n/a",
  207. "None",
  208. "2D",
  209. "3D"
  210. };
  211. int main(int argc, char *argv[])
  212. {
  213. struct gps_data_t gps_data;
  214. if (0 != gps_open("localhost", "2947", &gps_data)) {
  215. printf("Open error. Bye, bye\n");
  216. return 1;
  217. }
  218. (void)gps_stream(&gps_data, WATCH_ENABLE | WATCH_JSON, NULL);
  219. // Wait for data available.
  220. while (gps_waiting(&gps_data, 5000000)) {
  221. // will not block because we know data is available.
  222. if (-1 == gps_read(&gps_data, NULL, 0)) {
  223. printf("Read error. Bye, bye\n");
  224. break;
  225. }
  226. if (MODE_SET != (MODE_SET & gps_data.set)) {
  227. // did not even get mode, nothing to see here
  228. continue;
  229. }
  230. if (0 > gps_data.fix.mode ||
  231. MODE_STR_NUM <= gps_data.fix.mode) {
  232. gps_data.fix.mode = 0;
  233. }
  234. printf("Fix mode: %s (%d) Time: ",
  235. mode_str[gps_data.fix.mode],
  236. gps_data.fix.mode);
  237. if (TIME_SET == (TIME_SET & gps_data.set)) {
  238. // not 32 bit safe
  239. printf("%ld.%09ld ", gps_data.fix.time.tv_sec,
  240. gps_data.fix.time.tv_nsec);
  241. } else {
  242. puts("n/a ");
  243. }
  244. if (isfinite(gps_data.fix.latitude) &&
  245. isfinite( gps_data.fix.longitude)) {
  246. // Display data from the GPS receiver if valid.
  247. printf("Lat %.6f Lon %.6f\n",
  248. gps_data.fix.latitude, gps_data.fix.longitude);
  249. }
  250. }
  251. // When you are done...
  252. (void)gps_stream(&gps_data, WATCH_DISABLE, NULL);
  253. (void)gps_close(&gps_data);
  254. return 0;
  255. }
  256. ----
  257. == LIMITATIONS
  258. On some systems (those which do not support implicit linking in
  259. libraries) you may need to add *-lm* to your link line when you link
  260. libgps. It is always safe to do this.
  261. In the C API, incautious use of *gps_send()* may lead to subtle bugs.
  262. In order to not bloat struct *gps_data_t* with space used by responses
  263. that are not expected to be shipped in close sequence with each other,
  264. the storage for fields associated with certain responses are combined in
  265. a union.
  266. The risky set of responses includes VERSION, DEVICELIST, RTCM2, RTCM3,
  267. SUBFRAME, AIS, GST, and ERROR; it may not be limited to that set. The
  268. logic of the *gpsd* daemon's watcher mode is careful to avoid dangerous
  269. sequences, but you should read and understand the layout of struct
  270. *gps_data_t* before using *gps_send()* to request any of these
  271. responses.
  272. == COMPATIBILITY
  273. The *gps_query()* supported in major versions 1 and 2 of this library
  274. has been removed. With the new streaming-oriented wire protocol behind
  275. this library, it is extremely unwise to assume that the first
  276. transmission from the *gpsd* daemon after a command is shipped to it will be
  277. the response to command.
  278. If you must send commands to the *gpsd* daemon explicitly, use *gps_send()*
  279. but beware that this ties your code to the GPSD wire protocol. It is not
  280. recommended.
  281. In some versions of the API *gps_read()* is a blocking call and
  282. there was a POLL_NONBLOCK option to make it nonblocking.
  283. *gps_waiting()* was added to reduce the number of wrong ways to code a
  284. polling loop.
  285. See the comment above the symbol GPSD_API_MAJOR_VERSION in *gps.h* for
  286. recent changes.
  287. == ACKNOWLEDGEMENTS
  288. C sample code by Gary E. Miller <gem@rellim.com> and
  289. Charles Curley <charlescurley@charlescurley.com>
  290. == SEE ALSO
  291. *gpsd*(8), *gps*(1), *gpsd_json*(5), *libgpsmm*(3)
  292. == RESOURCES
  293. * {gpsdweb}gpsd-client-example-code.html[GPSD Client Example Code] An
  294. annotated example client.
  295. * {gpsdweb}client-howto.html[GPSD Client HOWTO] A GPS client HOWTO.
  296. * *Project web site:* {gpsdweb}
  297. == COPYING
  298. This file is Copyright 2013 by the GPSD project +
  299. SPDX-License-Identifier: BSD-2-clause