summaryrefslogtreecommitdiff
path: root/libgps.xml
blob: c50ce967e5f4c1059b461446e386a20f2db674f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
                   "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
<!-- lifted from troff+man by doclifter -->
<refentry>
<refentryinfo><date>14 Aug 2004</date></refentryinfo>
<refmeta>
<refentrytitle>3</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo class='source'>Linux</refmiscinfo>
</refmeta>
<refnamediv id='name'>
<refname>libgps</refname>
<refpurpose>C service library for communicating with the GPS daemon</refpurpose>
</refnamediv>
<refsynopsisdiv id='synopsis'>
<funcsynopsis>
<funcsynopsisinfo>

C:

#include &lt;gps.h&gt;

</funcsynopsisinfo>
<funcprototype>
<funcdef>struct gps_data_t *<function>gps_open</function></funcdef>
    <paramdef>char *<parameter>server</parameter></paramdef>
    <paramdef>char * <parameter>port</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>gps_open_r</function></funcdef>
    <paramdef>char *<parameter>server</parameter></paramdef>
    <paramdef>char * <parameter>port</parameter></paramdef>
    <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>gps_send</function></funcdef>
    <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
    <paramdef>char *<parameter>fmt</parameter>...</paramdef>
</funcprototype>
<funcprototype>
<funcdef>void <function>gps_set_raw_hook</function></funcdef>
    <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
    <paramdef>void (*<parameter>hook</parameter>)(struct gps_data_t *,
    char *buf, size_t len)</paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>gps_poll</function></funcdef>
    <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>void <function>gps_close</function></funcdef>
    <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>void <function>gps_set_callback</function></funcdef>
    <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
    <paramdef>void (*<parameter>callback</parameter>)(struct
    gps_data_t *sentence, char *buf, size_t len)</paramdef>
    <paramdef>pthread_t *<parameter>handler</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>void <function>gps_del_callback</function></funcdef>
    <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
    <paramdef>pthread *<parameter>handler</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>void <function>rtcm2_unpack</function></funcdef>
    <paramdef>struct rtcm_t *<parameter>rtcmp</parameter></paramdef>
    <paramdef>char *<parameter>buf</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>gps_stream</function></funcdef>
    <paramdef>struct gps_data_t *<parameter>gpsdata</parameter></paramdef>
    <paramdef>unsigned int<parameter>flags</parameter></paramdef>
    <paramdef>void *<parameter>data</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>char *<function>gps_errstr</function></funcdef>
    <paramdef>int <parameter>err</parameter></paramdef>
</funcprototype>
<funcsynopsisinfo>

Python:

import gps

session = gps.gps(host="127.0.0.1", port="2947")

session.set_raw_hook(raw_hook)

session.stream(WATCH_ENABLE)

for report in session:
    process(report)

del session

</funcsynopsisinfo>
</funcsynopsis>
</refsynopsisdiv>

<refsect1 id='description'><title>DESCRIPTION</title>

<para><emphasis remap='B'>libgps</emphasis> is a service library which
supports querying GPS devices; link it with the linker option
-lgps. There are two interfaces supported in it; one high-level
interface that goes through
<citerefentry><refentrytitle>gpsd</refentrytitle><manvolnum>1</manvolnum></citerefentry>
and is intended for concurrent use by several applications, and one
low-level interface that speaks directly with the serial or USB device
to which the GPS is attached. This page describes the high-level
interface that is safe for multiple applications to use simultaneously; it
is probably the one you want.  The low-level interface is documented
at
<citerefentry><refentrytitle>libgpsd</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>

<warning><para>The API described below may change incompatibly at API
version 4. Take care to conditionalize your code on the major and
minor API version symbols in <filename>gps.h</filename>; ideally,
force a compilation failure if the header is not exporting a version
you recognize.  See the GPSD project website for more information on
the protocol and API changes.</para></warning>

<para>Calling <function>gps_open()</function> initializes a GPS-data
structure to hold the data collected by the GPS, and returns a socket
attached to
<citerefentry><refentrytitle>gpsd</refentrytitle><manvolnum>1</manvolnum></citerefentry>. 
<function>gps_open()</function> returns NULL on errors.  errno is
set depending on the error returned from the the socket layer; see
<filename>gps.h</filename> for values and explanations.</para>

<para><function>gps_open_r()</function> is a reentrent-friendly
version That put the session storage where you wish to allocate it.
It returns 0 on success and -1 on failure, with errno set
appropriately.</para>

<para><function>gps_close()</function> ends the session.
NB: ending the session does not destroy any threads created to handle
callbacks.  You must delete all callbacks prior to
calling <function>gps_close()</function>.  Failure to do so results in
undefined behavior.  Currently it will leak a thread, and in the
future might result in aborting the program.
</para>

<para><function>gps_send()</function> writes a command to the daemon.
The second argument must be a format string containing elements from
the command set documented at
<citerefentry><refentrytitle>gpsd</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
It may have % elements as for
<citerefentry><refentrytitle>sprintf</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
which will be filled in from any following arguments.  This function
returns a -1 if there was a Unix-level write error, otherwise
0. Please read the LIMITATIONS section for additional information and
cautions.</para>

<para><function>gps_poll()</function> accepts a response, or sequence
of responses, from the daemon and interprets it as though it were a
query response (the return value is as for a query).
<function>gps_poll()</function> returns the validity mask of the
received structure.  This function does a blocking read waiting for
data from the daemon; it returns 0 for success, or -1 on a Unix-level
read error. </para>

<para><function>gps_stream()</function> asks
<application>gpsd</application> to stream the reports it has at you,
to be made available whenn you poll.  It is preferable to the
older-style (pre-2.39) way of doing this,
<function>gps_query()</function> with a "w+" argument, because it
insulates your code from whether your client library and your
<application>gpsd</application> are using old or new protocol.
The second argument is a flag mask that sets various policy bits; 
see gps.h for description.  Calling <function>gps_stream()</function>
more than once with different flag masks is allowed.The third
argument is not presently used but reserved for expansion.</para>

<para><function>gps_set_raw_hook()</function> takes a function you
specify and run it (synchronously) on the raw data pulled by a
<function>gps_query()</function> or <function>gps_poll()</function>
call.  The arguments passed to this hook will be a pointer to a
structure containing parsed data, and a buffer containining the
raw <application>gpsd</application> response.</para>

<para><function>gps_set_callback()</function> takes a function you
specify and runs it asynchronously each time new data arrives from
<application>gpsd</application>, using POSIX threads.  For example,
you can call gps_set_callback(gpsdata, my_function, handler) once in
your program, and from there on your gpsdata structure will be parsed
by your <function>my_function()</function> each time new data are
available.  <function>my_function()</function> could change some
global variables in your program based on received data; it is your
responsibility to ensure that your program uses mutexes or other
mechanisms to avoid race conditions.
NB: <function>gps_set_callback()</function> will create a new thread,
and contrary to what you might expect <function>gps_close()</function>
will not destroy that thread.  You must therefore explicitly remove
the callback with <function>gps_del_callback()</function> before
calling <function>gps_close()</function>.
</para>

<para><function>gps_del_callback()</function> deregisters the callback
function previously set with <function>gps_set_callback()</function>.
After the invocation of this function no operation will be done when
new data arrives.</para>

<para><function>gps_errstr()</function> returns an ASCII string (in
English) describing the error indicated by a nonzero return value from
<function>gps_open()</function>.</para>

<para>Consult <filename>gps.h</filename> to learn more about the data members 
and associated timestamps.  Note that information will accumulate
in the session structure over time, and the 'valid' field is not
automatically zeroed by each poll.  It is up to the client to
zero that field when appropriate and to keep an eye on the fix 
and sentence timestamps.</para>

<para>The <function>rtcm_unpack()</function> will be useful when you
are connected to an RTCM-104 source in raw mode.  Use it as part of a
raw hook, calling it with the address of the <structname>struct
rtcm_t</structname> element of your session structure buffer as first
argument and the buffer as the second.  It will unpack a line of RTCM
data into the structure.  This function returns 0 when it has read the
last line of an RTCM-104 message, a positive int when it expects more
dump lines, and a negative int on parse failure. You must zero out the
<structname>struct rtcm_t</structname> each time before this function
is called on a new header (H) line, as it relies on the message type
field being initially zero and uses it to track what kind of 
following line is expected.</para>

<para>The Python implementation supports the same facilities as the C
library. <function>gps_open()</function> is replaced by the
initialization of a gps session object; the other calls are methods of
that object, and have the same names as the corresponding C functions.
Resources within the session object will be properly released when it
is garbage-collected.</para>
</refsect1>

<refsect1 id='example'><title>CODE EXAMPLE</title>

<para>The following is an excerpted and simplified version of the
libgps interface code from
<citerefentry><refentrytitle>xgps</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
The function <function>handle_input()</function> is a trivial piece of
code that calls gps_poll(gpsdata).
</para>

<programlisting>
    gpsdata = gps_open(server, port);

    build_gui(toplevel);

    gps_set_raw_hook(gpsdata, update_panel);
	
    (void)gps_stream(gpsdata, WATCH_ENABLE, NULL);

    (void)XtAppAddInput(app, gpsdata->gps_fd, 
		  (XtPointer)XtInputReadMask, handle_input, NULL);
    (void)XtAppMainLoop(app);

    (void)gps_close(gpsdata);
</programlisting>

</refsect1>

<refsect1 id='limitations'><title>LIMITATIONS</title>

<para>Incautious use of <function>gps_send()</function> may lead to
subtle bugs. In order to not bloat <structname>struct
gps_data_t</structname> with space used by responses that are not
expected to be shipped in close sequence with each other, the storage
for fields associated with certain responses are combined in a union.</para>

<para>Sends that might invoke this set of responses need to be
sequenced carefully so the unpacking of multiple responses arriving in
the same socket read cannot result in later updates to the union
obliterating data from earlier ones before it becomes visible to the
client application.</para>

<para>The risky set of responses includes VERSION, DEVICELIST, RTCM2,
RTCM3, and AIS; it may not be limited to that set.  The logic of the
daemon's watcher mode is careful to avoid dangerous sequences, but
you should read and understand the layout of <structname>struct
gps_data_t</structname> before using <function>gps_send()</function>
to request any of these responses.</para>

</refsect1>

<refsect1 id='compatibility'><title>COMPATIBILITY</title>

<para>The <function>gps_query()</function> supported in major versions
1 and 2 of this library has been removed.  With the new
streaming-oriented wire protocol behind this library, it is extremely
unwise to assume that the first transmission from the damon after a
command is shipped to it will be the reponse to command.</para>

<para>If you must send commands to the daemon explicity, use
<function>gps_send()</function> but beware that this ties your code to
the GPSD wire protocol. It is not recommended.</para>
</refsect1>

<refsect1 id='see_also'><title>SEE ALSO</title>
<para>
<citerefentry><refentrytitle>gpsd</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>gps</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>libgps</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
<citerefentry><refentrytitle>libgpsmm</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
</para>
</refsect1>

<refsect1 id='author'><title>AUTHOR</title>
<para>Eric S. Raymond &lt;esr@thyrsus.com&gt;, Thread-callback methods
in the C binding added by Alfredo Pironti
&lt;alfredo@users.sourceforge.net&gt;.</para>
</refsect1>
</refentry>