libgps.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!--
  3. This file is Copyright (c) 2010 by the GPSD project
  4. SPDX-License-Identifier: BSD-2-clause
  5. -->
  6. <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
  7. "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
  8. <refentry>
  9. <refentryinfo><date>4 Feb 2019</date></refentryinfo>
  10. <refmeta>
  11. <refentrytitle>libgps</refentrytitle>
  12. <manvolnum>3</manvolnum>
  13. <refmiscinfo class="source">The GPSD Project</refmiscinfo>
  14. <refmiscinfo class="manual">GPSD Documentation</refmiscinfo>
  15. </refmeta>
  16. <refnamediv id='name'>
  17. <refname>libgps</refname>
  18. <refpurpose>C service library for communicating with the GPS daemon</refpurpose>
  19. </refnamediv>
  20. <refsynopsisdiv id='synopsis'>
  21. <funcsynopsis>
  22. <funcsynopsisinfo>
  23. C:
  24. #include &lt;gps.h&gt;
  25. </funcsynopsisinfo>
  26. <funcprototype>
  27. <funcdef>int <function>gps_open</function></funcdef>
  28. <paramdef>char *<parameter>server</parameter></paramdef>
  29. <paramdef>char *<parameter>port</parameter></paramdef>
  30. <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
  31. </funcprototype>
  32. <funcprototype>
  33. <funcdef>int <function>gps_send</function></funcdef>
  34. <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
  35. <paramdef>char *<parameter>fmt</parameter>...</paramdef>
  36. </funcprototype>
  37. <funcprototype>
  38. <funcdef>int <function>gps_read</function></funcdef>
  39. <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
  40. <paramdef>char *<parameter>message</parameter></paramdef>
  41. <paramdef>int <parameter>message_size</parameter></paramdef>
  42. </funcprototype>
  43. <funcprototype>
  44. <funcdef>bool <function>gps_waiting</function></funcdef>
  45. <paramdef>const struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
  46. <paramdef>int <parameter>timeout</parameter></paramdef>
  47. </funcprototype>
  48. <funcprototype>
  49. <funcdef>char *<function>gps_data</function></funcdef>
  50. <paramdef>const struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
  51. </funcprototype>
  52. <funcprototype>
  53. <funcdef>int <function>gps_unpack</function></funcdef>
  54. <paramdef>char *<parameter>buf</parameter></paramdef>
  55. <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
  56. </funcprototype>
  57. <funcprototype>
  58. <funcdef>int <function>gps_close</function></funcdef>
  59. <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
  60. </funcprototype>
  61. <funcprototype>
  62. <funcdef>int <function>gps_stream</function></funcdef>
  63. <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
  64. <paramdef>unsigned int<parameter>flags</parameter></paramdef>
  65. <paramdef>void *<parameter>data</parameter></paramdef>
  66. </funcprototype>
  67. <funcprototype>
  68. <funcdef>int <function>gps_mainloop</function></funcdef>
  69. <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
  70. <paramdef>int <parameter>timeout</parameter></paramdef>
  71. <paramdef>void (*<parameter>hook</parameter>)(struct gps_data_t *gpsdata)</paramdef>
  72. </funcprototype>
  73. <funcprototype>
  74. <funcdef>const char *<function>gps_errstr</function></funcdef>
  75. <paramdef>int <parameter>err</parameter></paramdef>
  76. </funcprototype>
  77. <funcsynopsisinfo>
  78. Python:
  79. import gps
  80. session = gps.gps(host="localhost", port="2947")
  81. session.stream(flags=gps.WATCH_JSON)
  82. for report in session:
  83. process(report)
  84. del session
  85. </funcsynopsisinfo>
  86. </funcsynopsis>
  87. </refsynopsisdiv>
  88. <refsect1 id='description'><title>DESCRIPTION</title>
  89. <para><emphasis remap='B'>libgps</emphasis> is a service library which
  90. supports communicating with an instance of the
  91. <citerefentry><refentrytitle>gpsd</refentrytitle><manvolnum>8</manvolnum>
  92. </citerefentry>; link it with the linker option -lgps.</para>
  93. <warning><para>Take care to conditionalize your code on the major and
  94. minor API version symbols in <filename>gps.h</filename>; ideally,
  95. force a compilation failure if GPSD_API_MAJOR_VERSION is not a version
  96. you recognize. See the GPSD project website for more information on
  97. the protocol and API changes.</para></warning>
  98. <para>Calling <function>gps_open()</function> initializes a GPS-data
  99. structure to hold the data collected by the GPS, and sets up access to
  100. <citerefentry><refentrytitle>gpsd</refentrytitle><manvolnum>1</manvolnum>
  101. </citerefentry>
  102. via either the socket or shared-memory export. The shared-memory
  103. export is faster, but does not carry information about device
  104. activation and deactivation events and will not allow you to monitor
  105. device packet traffic.</para>
  106. <para><function>gps_open()</function> returns 0 on success, -1 on
  107. errors and is re-entrant. errno is set depending on the error
  108. returned from the socket or shared-memory interface; see
  109. <filename>gps.h</filename> for values and explanations; also see
  110. <function>gps_errstr()</function>. The host address may be a DNS name,
  111. an IPv4 dotted quad, an IPV6 address, or the special value
  112. <constant>GPSD_SHARED_MEMORY</constant> referring to the
  113. shared-memory export; the library will do the right thing for any of
  114. these.</para>
  115. <para><function>gps_close()</function> ends the session and should only be
  116. called after a successful <function>gps_open()</function>.
  117. It returns 0 on success, -1 on errors. The shared-memory interface
  118. close always returns 0, whereas a socket close can result in an error.
  119. For a socket close error it will have set an errno from the call to the
  120. system's <function>close()</function>. </para>
  121. <para><function>gps_send()</function> writes a command to the daemon.
  122. It does nothing when using the shared-memory export.
  123. The second argument must be a format string containing elements from
  124. the command set documented at
  125. <citerefentry><refentrytitle>gpsd</refentrytitle><manvolnum>1</manvolnum>
  126. </citerefentry>.
  127. It may have % elements as for
  128. <citerefentry><refentrytitle>sprintf</refentrytitle><manvolnum>3</manvolnum>
  129. </citerefentry>,
  130. which will be filled in from any following arguments. This function
  131. returns a -1 if there was a Unix-level write error, otherwise
  132. 0. Please read the LIMITATIONS section for additional information and
  133. cautions. See <function>gps_stream()</function> as a possible
  134. alternative.</para>
  135. <para><function>gps_read()</function> accepts a response, or sequence
  136. of responses, from the daemon and interprets. This function does
  137. either a nonblocking read for data from the daemon or a fetch from
  138. shared memory; it returns a count of bytes read for success, -1 with
  139. errno set on a Unix-level read error, -1 with errno not set if the
  140. socket to the daemon has closed or if the shared-memory segment was
  141. unavailable, and 0 if no data is available.</para>
  142. <para><function>gps_waiting()</function> can be used to check whether
  143. there is new data from the daemon. The second argument is the maximum
  144. amount of time to wait (in microseconds) on input before returning.
  145. It returns true if there is input waiting, false on timeout (no data
  146. waiting) or error condition. When using the socket export, this
  147. function is a convenience wrapper around a
  148. <citerefentry><refentrytitle>select</refentrytitle><manvolnum>2</manvolnum>
  149. </citerefentry>
  150. call, and zeros <varname>errno</varname> on entry; you can test
  151. <varname>errno</varname> after exit to get more information about
  152. error conditions. Warning: under the shared-memory interface there is
  153. a tiny race window between <function>gps_waiting()</function> and a
  154. following <function>gps_read()</function>; in that context, because the
  155. latter does not block, it is probably better to write a simple read
  156. loop.</para>
  157. <para><function>gps_mainloop()</function> enables the provided hook
  158. function to be continually called whenever there is gpsd data.
  159. The second argument is the maximum amount of time to wait (in microseconds)
  160. on input before exiting the loop (and return a value of -1).
  161. It will also return a negative value on various errors.
  162. </para>
  163. <para><function>gps_unpack()</function> parses JSON from the argument
  164. buffer into the target of the session structure pointer argument.
  165. Included in case your application wishes to manage socket I/O
  166. itself.</para>
  167. <para><function>gps_data()</function> returns the contents of the
  168. client data buffer (it returns NULL when using the shared-memory
  169. export). Use with care; this may fail to be a NUL-terminated string if
  170. WATCH_RAW is enabled.</para>
  171. <para><function>gps_stream()</function> asks
  172. <application>gpsd</application> to stream the reports it has at you,
  173. to be made available when you poll (not available when using the
  174. shared-memory export). The second argument is a flag mask that sets
  175. various policy bits; see the list below. Calling
  176. <function>gps_stream()</function> more than once with different flag
  177. masks is allowed.</para>
  178. <variablelist>
  179. <varlistentry>
  180. <term>WATCH_DISABLE</term>
  181. <listitem>
  182. <para>Disable the reporting modes specified by the other WATCH_ flags.</para>
  183. </listitem>
  184. </varlistentry>
  185. <varlistentry>
  186. <term>WATCH_ENABLE</term>
  187. <listitem>
  188. <para>Disable the reporting modes specified by the other WATCH_ flags.
  189. This is the default.</para>
  190. </listitem>
  191. </varlistentry>
  192. <varlistentry>
  193. <term>WATCH_JSON</term>
  194. <listitem>
  195. <para>Enable JSON reporting of data. If WATCH_ENABLE is set, and no
  196. other WATCH flags are set, this is the default.</para>
  197. </listitem>
  198. </varlistentry>
  199. <varlistentry>
  200. <term>WATCH_NMEA</term>
  201. <listitem>
  202. <para>Enable generated pseudo-NMEA reporting on binary devices.</para>
  203. </listitem>
  204. </varlistentry>
  205. <varlistentry>
  206. <term>WATCH_RARE</term>
  207. <listitem>
  208. <para>Enable reporting of binary packets in encoded hex.</para>
  209. </listitem>
  210. </varlistentry>
  211. <varlistentry>
  212. <term>WATCH_RAW</term>
  213. <listitem>
  214. <para>Enable literal passthrough of binary packets.</para>
  215. </listitem>
  216. </varlistentry>
  217. <varlistentry>
  218. <term>WATCH_SCALED</term>
  219. <listitem>
  220. <para>When reporting AIS or Subframe data, scale integer quantities to
  221. floats if they have a divisor or rendering formula associated with them.</para>
  222. </listitem>
  223. </varlistentry>
  224. <varlistentry>
  225. <term>WATCH_NEWSTYLE</term>
  226. <listitem>
  227. <para>Force issuing a JSON initialization and getting new-style
  228. responses. This is the default. </para>
  229. </listitem>
  230. </varlistentry>
  231. <varlistentry>
  232. <term>WATCH_OLDSTYLE</term>
  233. <listitem>
  234. <para>Force issuing a W or R command and getting old-style
  235. responses. Warning: this flag (and the capability) will be removed
  236. in a future release.</para>
  237. </listitem>
  238. </varlistentry>
  239. <varlistentry>
  240. <term>WATCH_DEVICE</term>
  241. <listitem>
  242. <para>Restrict watching to a specified device, path given as second
  243. argument.</para>
  244. </listitem>
  245. </varlistentry>
  246. </variablelist>
  247. <para><function>gps_errstr()</function> returns an ASCII string (in
  248. English) describing the error indicated by a nonzero return value from
  249. <function>gps_open()</function>.</para>
  250. <para>Consult <filename>gps.h</filename> to learn more about the data
  251. members and associated timestamps. Note that information will
  252. accumulate in the session structure over time, and the 'valid' field
  253. is not automatically zeroed by each <function>gps_read()</function>.
  254. It is up to the client to zero that field when appropriate and to keep
  255. an eye on the fix and sentence timestamps.</para>
  256. <para>The Python implementation supports the same facilities as the
  257. socket-export calls in the C library; there is no shared-memory
  258. interface. <function>gps_open()</function> is replaced by the
  259. initialization of a gps session object; the other calls are methods of
  260. that object, and have the same names as the corresponding C functions.
  261. However, it is simpler just to use the session object as an iterator,
  262. as in the example given below. Resources within the session object
  263. will be properly released when it is garbage-collected.</para>
  264. </refsect1>
  265. <refsect1 id='environment'><title>ENVIRONMENT VARIABLES</title>
  266. <para>By setting the environment variable <envar>GPSD_SHM_KEY</envar>,
  267. you can control the key value used to create shared-memory segment
  268. used for communication with <application>gpsd</application>. This
  269. will be useful mainly when isolating test instances of
  270. <application>gpsd</application> from production ones.</para>
  271. </refsect1>
  272. <refsect1 id='example'><title>CODE EXAMPLE</title>
  273. <para>The following is an excerpted and simplified version of the
  274. libgps interface code from
  275. <citerefentry><refentrytitle>cgps</refentrytitle><manvolnum>1</manvolnum>
  276. </citerefentry>.</para>
  277. <programlisting>
  278. struct gps_data_t gps_data;
  279. ret = gps_open(hostName, hostPort, &amp;gps_data);
  280. (void) gps_stream(&amp;gps_data, WATCH_ENABLE | WATCH_JSON, NULL);
  281. /* Put this in a loop with a call to a high resolution sleep () in it. */
  282. if (gps_waiting(&amp;gps_data, 500)) {
  283. errno = 0;
  284. if (gps_read(&amp;gps_data, NULL, 0) == -1) {
  285. ...
  286. } else {
  287. /* Display data from the GPS receiver. */
  288. if (gps_data.set &amp; ...
  289. }
  290. }
  291. /* When you are done... */
  292. (void) gps_stream(&amp;gps_data, WATCH_DISABLE, NULL);
  293. (void) gps_close (&amp;gps_data);
  294. </programlisting>
  295. </refsect1>
  296. <refsect1 id='limitations'><title>LIMITATIONS</title>
  297. <para>On some systems (those which do not support implicit linking in
  298. libraries) you may need to add -lm to your link line when you link libgps.
  299. It is always safe to do this.</para>
  300. <para>In the C API, incautious use of <function>gps_send()</function>
  301. may lead to subtle bugs. In order to not bloat <structname>struct
  302. gps_data_t</structname> with space used by responses that are not
  303. expected to be shipped in close sequence with each other, the storage
  304. for fields associated with certain responses are combined in a
  305. union.</para>
  306. <para>The risky set of responses includes VERSION, DEVICELIST, RTCM2,
  307. RTCM3, SUBFRAME, AIS, GST, and ERROR; it may not be limited to that
  308. set. The logic of the daemon's watcher mode is careful to avoid
  309. dangerous sequences, but you should read and understand the layout of
  310. <structname>struct gps_data_t</structname> before using
  311. <function>gps_send()</function> to request any of these
  312. responses.</para>
  313. </refsect1>
  314. <refsect1 id='compatibility'><title>COMPATIBILITY</title>
  315. <para>The <function>gps_query()</function> supported in major versions
  316. 1 and 2 of this library has been removed. With the new
  317. streaming-oriented wire protocol behind this library, it is extremely
  318. unwise to assume that the first transmission from the daemon after a
  319. command is shipped to it will be the response to command.</para>
  320. <para>If you must send commands to the daemon explicitly, use
  321. <function>gps_send()</function> but beware that this ties your code to
  322. the GPSD wire protocol. It is not recommended.</para>
  323. <para>In earlier versions of the API <function>gps_read()</function> was
  324. a blocking call and there was a POLL_NONBLOCK option to make it nonblocking.
  325. <function>gps_waiting()</function> was added to reduce the number of
  326. wrong ways to code a polling loop.</para>
  327. <para>See the comment above the symbol GPSD_API_MAJOR_VERSION
  328. in <filename>gps.h</filename> for recent changes.</para>
  329. </refsect1>
  330. <refsect1 id='see_also'><title>SEE ALSO</title>
  331. <para>
  332. <citerefentry><refentrytitle>gpsd</refentrytitle><manvolnum>8</manvolnum>
  333. </citerefentry>,
  334. <citerefentry><refentrytitle>gps</refentrytitle><manvolnum>1</manvolnum>
  335. </citerefentry>,
  336. <citerefentry><refentrytitle>libgpsmm</refentrytitle><manvolnum>3</manvolnum>
  337. </citerefentry>.
  338. </para>
  339. </refsect1>
  340. <refsect1 id='author'><title>AUTHOR</title>
  341. <para>Eric S. Raymond &lt;esr@thyrsus.com&gt;,
  342. C sample code Charles Curley &lt;charlescurley@charlescurley.com&gt;</para>
  343. </refsect1>
  344. </refentry>