"Initial commit to Gerrit"
[profile/ivi/gpsd.git] / libgps.xml
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!--
3 This file is Copyright (c) 2010 by the GPSD project
4 BSD terms apply: see the file COPYING in the distribution root for details.
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>14 Aug 2004</date></refentryinfo>
10 <refmeta>
11 <refentrytitle>3</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
24 C:
25
26 #include &lt;gps.h&gt;
27
28 </funcsynopsisinfo>
29 <funcprototype>
30 <funcdef>struct gps_data_t *<function>gps_open</function></funcdef>
31     <paramdef>int<parameter>af</parameter></paramdef>
32     <paramdef>char *<parameter>server</parameter></paramdef>
33     <paramdef>char * <parameter>port</parameter></paramdef>
34 </funcprototype>
35 <funcprototype>
36 <funcdef>int <function>gps_open_r</function></funcdef>
37     <paramdef>char *<parameter>server</parameter></paramdef>
38     <paramdef>char * <parameter>port</parameter></paramdef>
39     <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
40 </funcprototype>
41 <funcprototype>
42 <funcdef>int <function>gps_send</function></funcdef>
43     <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
44     <paramdef>char *<parameter>fmt</parameter>...</paramdef>
45 </funcprototype>
46 <funcprototype>
47 <funcdef>void <function>gps_set_raw_hook</function></funcdef>
48     <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
49     <paramdef>void (*<parameter>hook</parameter>)(struct gps_data_t *,
50     char *buf, size_t len)</paramdef>
51 </funcprototype>
52 <funcprototype>
53 <funcdef>int <function>gps_poll</function></funcdef>
54     <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
55 </funcprototype>
56 <funcprototype>
57 <funcdef>bool <function>gps_waiting</function></funcdef>
58     <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
59 </funcprototype>
60 <funcprototype>
61 <funcdef>void <function>gps_close</function></funcdef>
62     <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
63 </funcprototype>
64 <funcprototype>
65 <funcdef>int <function>gps_stream</function></funcdef>
66     <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
67     <paramdef>unsigned int<parameter>flags</parameter></paramdef>
68     <paramdef>void *<parameter>data</parameter></paramdef>
69 </funcprototype>
70 <funcprototype>
71 <funcdef>char *<function>gps_errstr</function></funcdef>
72     <paramdef>int <parameter>err</parameter></paramdef>
73 </funcprototype>
74 <funcsynopsisinfo>
75
76 Python:
77
78 import gps
79
80 session = gps.gps(host="localhost", port="2947")
81
82 session.set_raw_hook(raw_hook)
83
84 session.stream(flags=WATCH_JSON)
85
86 for report in session:
87     process(report)
88
89 del session
90
91 </funcsynopsisinfo>
92 </funcsynopsis>
93 </refsynopsisdiv>
94
95 <refsect1 id='description'><title>DESCRIPTION</title>
96
97 <para><emphasis remap='B'>libgps</emphasis> is a service library which
98 supports communicating with an instance of the
99 <citerefentry><refentrytitle>gpsd</refentrytitle><manvolnum>8</manvolnum></citerefentry>; link it with the linker option -lgps.</para>
100
101 <warning><para>Take care to conditionalize your code on the major and
102 minor API version symbols in <filename>gps.h</filename>; ideally,
103 force a compilation failure if GPSD_API_MAJOR_VERSION is not a version
104 you recognize.  See the GPSD project website for more information on
105 the protocol and API changes.</para></warning>
106
107 <para>Calling <function>gps_open()</function> initializes a GPS-data
108 structure to hold the data collected by the GPS, and returns a socket
109 attached to
110 <citerefentry><refentrytitle>gpsd</refentrytitle><manvolnum>1</manvolnum></citerefentry>. 
111 <function>gps_open()</function> returns NULL on errors.  errno is
112 set depending on the error returned from the the socket layer; see
113 <filename>gps.h</filename> for values and explanations. The host
114 address may be a DNS name, an IPv4 dotted quad, or an IPV6 address;
115 the library will do the right thing for any of these.</para>
116
117 <para><function>gps_open_r()</function> is a reentrent-friendly
118 version that puts the session storage where you wish to allocate it.
119 It returns 0 on success and -1 on failure, with errno set
120 appropriately.</para>
121
122 <para><function>gps_close()</function> ends the session.</para>
123
124 <para><function>gps_send()</function> writes a command to the daemon.
125 The second argument must be a format string containing elements from
126 the command set documented at
127 <citerefentry><refentrytitle>gpsd</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
128 It may have % elements as for
129 <citerefentry><refentrytitle>sprintf</refentrytitle><manvolnum>3</manvolnum></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.</para>
134
135 <para><function>gps_poll()</function> accepts a response, or sequence
136 of responses, from the daemon and interprets it as though it were a
137 query response (the return value is as for a query).
138 <function>gps_poll()</function> returns the validity mask of the
139 received structure.  This function does a blocking read waiting for
140 data from the daemon; it returns 0 for success, -1 with errno set on a 
141 Unix-level read error, -1 with errno not set if the socket to
142 the daemon has closed. </para>
143
144 <para><function>gps_waiting()</function> can be used to check
145 whether there is data from the daemon. It returns true if there is,
146 false on no data waiting or error condition. It does not block
147 waiting for input.</para>
148
149 <para><function>gps_stream()</function> asks
150 <application>gpsd</application> to stream the reports it has at you,
151 to be made available whenn you poll.  It is preferable to the
152 older-style (pre-2.90) way of doing this,
153 <function>gps_query()</function> with a "w+" argument, because it
154 insulates your code from whether your client library and your
155 <application>gpsd</application> are using old or new protocol.
156 The second argument is a flag mask that sets various policy bits; 
157 see trhe list below.  Calling <function>gps_stream()</function>
158 more than once with different flag masks is allowed.</para>
159
160 <variablelist>
161 <varlistentry>
162 <term>WATCH_DISABLE</term>
163 <listitem>
164 <para>Disable the reporting modes specified by the other WATCH_ flags.
165 Cannot be used to disable POLL_NONBLOCK.</para>
166 </listitem>
167 </varlistentry>
168 <varlistentry>
169 <term>WATCH_ENABLE</term>
170 <listitem>
171 <para>Disable the reporting modes specified by the other WATCH_ flags.
172 This is the default.</para>
173 </listitem>
174 </varlistentry>
175 <varlistentry>
176 <term>WATCH_JSON</term>
177 <listitem>
178 <para>Enable JSON reporting of data. If WATCH_ENABLE is set, and no
179 other WATCH flags are set, this is the default.</para>
180 </listitem>
181 </varlistentry>
182 <varlistentry>
183 <term>WATCH_NMEA</term>
184 <listitem>
185 <para>Enable generated pseudo-NMEA reporting on binary devices.</para>
186 </listitem>
187 </varlistentry>
188 <varlistentry>
189 <term>WATCH_RARE</term>
190 <listitem>
191 <para>Enable reporting of binary packets in encoded hex.</para>
192 </listitem>
193 </varlistentry>
194 <varlistentry>
195 <term>WATCH_RAW</term>
196 <listitem>
197 <para>Enable literal passtrough of binary packets.</para>
198 </listitem>
199 </varlistentry>
200 <varlistentry>
201 <term>WATCH_SCALED</term>
202 <listitem>
203 <para>When reporting AIS data, scale integer quantities to floats if 
204 they have a divisor or rendering formula assosiated with them.</para>
205 </listitem>
206 </varlistentry>
207 <varlistentry>
208 <term>WATCH_NEWSTYLE</term>
209 <listitem>
210 <para>Force issuing a JSON initialization and getting new-style 
211 responses. This will become the default in a future release. </para>
212 </listitem>
213 </varlistentry>
214 <varlistentry>
215 <term>WATCH_OLDSTYLE</term>
216 <listitem>
217 <para>Force issuing a W or R command and getting old-style
218 responses. This is now the default behavior, but will be removed
219 in a future release.</para>
220 </listitem>
221 </varlistentry>
222 <varlistentry>
223 <term>WATCH_DEVICE</term>
224 <listitem>
225 <para>Restrict watching to a speciied device, patch given as second 
226 argument.</para>
227 </listitem>
228 </varlistentry>
229 <varlistentry>
230 <term>POLL_NONBLOCK</term>
231 <listitem>
232 <para>Normally <function>gps_poll()</function> blocks until
233 either there is a read error or some data is received from tha 
234 daemon.  In this mode, <function>gps_poll()</function> returns
235 immediately with a value of 0 if there is no input waiting.</para>
236 </listitem>
237 </varlistentry>
238 </variablelist>
239
240 <para><function>gps_set_raw_hook()</function> takes a function you
241 specify and run it (synchronously) on the raw data pulled by a
242 <function>gps_query()</function> or <function>gps_poll()</function>
243 call.  The arguments passed to this hook will be a pointer to a
244 structure containing parsed data, and a buffer containining the
245 raw <application>gpsd</application> response.</para>
246
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
251 <para>Consult <filename>gps.h</filename> to learn more about the data members 
252 and associated timestamps.  Note that information will accumulate
253 in the session structure over time, and the 'valid' field is not
254 automatically zeroed by each poll.  It is up to the client to
255 zero that field when appropriate and to keep an eye on the fix 
256 and sentence timestamps.</para>
257
258 <para>The Python implementation supports the same facilities as the C
259 library. <function>gps_open()</function> is replaced by the
260 initialization of a gps session object; the other calls are methods of
261 that object, and have the same names as the corresponding C functions.
262 Resources within the session object will be properly released when it
263 is garbage-collected. Note one limitation: POLL_NOBLOCK is not yet 
264 supported in Python; use the waiting() method instead.</para>
265 </refsect1>
266
267 <refsect1 id='example'><title>CODE EXAMPLE</title>
268
269 <para>The following is an excerpted and simplified version of the
270 libgps interface code from
271 <citerefentry><refentrytitle>xgps</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
272 The function <function>handle_input()</function> is a trivial piece of
273 code that calls gps_poll(gpsdata).
274 </para>
275
276 <programlisting>
277     gpsdata = gps_open(server, port);
278
279     build_gui(toplevel);
280
281     gps_set_raw_hook(gpsdata, update_panel);
282         
283     (void)gps_stream(gpsdata, WATCH_ENABLE, NULL);
284
285     (void)XtAppAddInput(app, gpsdata->gps_fd, 
286                   (XtPointer)XtInputReadMask, handle_input, NULL);
287     (void)XtAppMainLoop(app);
288
289     (void)gps_close(gpsdata);
290 </programlisting>
291
292 </refsect1>
293
294 <refsect1 id='limitations'><title>LIMITATIONS</title>
295
296 <para>In the C API, incautious use of <function>gps_send()</function>
297 may lead to subtle bugs. In order to not bloat <structname>struct
298 gps_data_t</structname> with space used by responses that are not
299 expected to be shipped in close sequence with each other, the storage
300 for fields associated with certain responses are combined in a
301 union.</para>
302
303 <para>The risky set of responses includes VERSION, DEVICELIST, RTCM2,
304 RTCM3, and AIS; it may not be limited to that set.  The logic of the
305 daemon's watcher mode is careful to avoid dangerous sequences, but
306 you should read and understand the layout of <structname>struct
307 gps_data_t</structname> before using <function>gps_send()</function>
308 to request any of these responses.</para>
309
310 </refsect1>
311
312 <refsect1 id='compatibility'><title>COMPATIBILITY</title>
313
314 <para>The <function>gps_query()</function> supported in major versions
315 1 and 2 of this library has been removed.  With the new
316 streaming-oriented wire protocol behind this library, it is extremely
317 unwise to assume that the first transmission from the damon after a
318 command is shipped to it will be the reponse to command.</para>
319
320 <para>If you must send commands to the daemon explicity, 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
324 <para>This API has been stable since GPSD 2.90, except that
325 <function>gps_waiting()</function> was added in 2.91.</para>
326 </refsect1>
327
328 <refsect1 id='see_also'><title>SEE ALSO</title>
329 <para>
330 <citerefentry><refentrytitle>gpsd</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
331 <citerefentry><refentrytitle>gps</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
332 <citerefentry><refentrytitle>libgpsd</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
333 <citerefentry><refentrytitle>libgpsmm</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
334 </para>
335 </refsect1>
336
337 <refsect1 id='author'><title>AUTHOR</title>
338 <para>Eric S. Raymond &lt;esr@thyrsus.com&gt;, Thread-callback methods
339 in the C binding added by Alfredo Pironti
340 &lt;alfredo@users.sourceforge.net&gt;.</para>
341 </refsect1>
342 </refentry>
343