13(3) GPSD Documentation 3(3)
2
3
4
6 libgps - C service library for communicating with the GPS daemon
7
9 C:
10
11 #include <gps.h>
12
13
14 struct gps_data_t *gps_open(intaf, char *server, char * port);
15
16 int gps_open_r(char *server, char * port, struct gps_data_t *gpsdata);
17
18 int gps_send(struct gps_data_t *gpsdata, char *fmt...);
19
20 void gps_set_raw_hook(struct gps_data_t *gpsdata,
21 void (*hook)(struct gps_data_t *, char *buf, size_t len));
22
23 int gps_poll(struct gps_data_t *gpsdata);
24
25 bool gps_waiting(struct gps_data_t *gpsdata);
26
27 void gps_close(struct gps_data_t *gpsdata);
28
29 int gps_stream(struct gps_data_t *gpsdata, unsigned intflags,
30 void *data);
31
32 char *gps_errstr(int err);
33
34
35 Python:
36
37 import gps
38
39 session = gps.gps(host="localhost", port="2947")
40
41 session.set_raw_hook(raw_hook)
42
43 session.stream(flags=WATCH_JSON)
44
45 for report in session:
46 process(report)
47
48 del session
49
50
52 libgps is a service library which supports communicating with an
53 instance of the gpsd(8); link it with the linker option -lgps.
54
55 Warning
56 Take care to conditionalize your code on the major and minor API
57 version symbols in gps.h; ideally, force a compilation failure if
58 GPSD_API_MAJOR_VERSION is not a version you recognize. See the GPSD
59 project website for more information on the protocol and API
60 changes.
61
62 Calling gps_open() initializes a GPS-data structure to hold the data
63 collected by the GPS, and returns a socket attached to gpsd(1).
64 gps_open() returns NULL on errors. errno is set depending on the error
65 returned from the the socket layer; see gps.h for values and
66 explanations. The host address may be a DNS name, an IPv4 dotted quad,
67 or an IPV6 address; the library will do the right thing for any of
68 these.
69
70 gps_open_r() is a reentrent-friendly version that puts the session
71 storage where you wish to allocate it. It returns 0 on success and -1
72 on failure, with errno set appropriately.
73
74 gps_close() ends the session.
75
76 gps_send() writes a command to the daemon. The second argument must be
77 a format string containing elements from the command set documented at
78 gpsd(1). It may have % elements as for sprintf(3), which will be filled
79 in from any following arguments. This function returns a -1 if there
80 was a Unix-level write error, otherwise 0. Please read the LIMITATIONS
81 section for additional information and cautions.
82
83 gps_poll() accepts a response, or sequence of responses, from the
84 daemon and interprets it as though it were a query response (the return
85 value is as for a query). gps_poll() returns the validity mask of the
86 received structure. This function does a blocking read waiting for data
87 from the daemon; it returns 0 for success, -1 with errno set on a
88 Unix-level read error, -1 with errno not set if the socket to the
89 daemon has closed.
90
91 gps_waiting() can be used to check whether there is data from the
92 daemon. It returns true if there is, false on no data waiting or error
93 condition. It does not block waiting for input.
94
95 gps_stream() asks gpsd to stream the reports it has at you, to be made
96 available whenn you poll. It is preferable to the older-style
97 (pre-2.90) way of doing this, gps_query() with a "w+" argument, because
98 it insulates your code from whether your client library and your gpsd
99 are using old or new protocol. The second argument is a flag mask that
100 sets various policy bits; see trhe list below. Calling gps_stream()
101 more than once with different flag masks is allowed.
102
103 WATCH_DISABLE
104 Disable the reporting modes specified by the other WATCH_ flags.
105 Cannot be used to disable POLL_NONBLOCK.
106
107 WATCH_ENABLE
108 Disable the reporting modes specified by the other WATCH_ flags.
109 This is the default.
110
111 WATCH_JSON
112 Enable JSON reporting of data. If WATCH_ENABLE is set, and no other
113 WATCH flags are set, this is the default.
114
115 WATCH_NMEA
116 Enable generated pseudo-NMEA reporting on binary devices.
117
118 WATCH_RARE
119 Enable reporting of binary packets in encoded hex.
120
121 WATCH_RAW
122 Enable literal passtrough of binary packets.
123
124 WATCH_SCALED
125 When reporting AIS data, scale integer quantities to floats if they
126 have a divisor or rendering formula assosiated with them.
127
128 WATCH_NEWSTYLE
129 Force issuing a JSON initialization and getting new-style
130 responses. This will become the default in a future release.
131
132 WATCH_OLDSTYLE
133 Force issuing a W or R command and getting old-style responses.
134 This is now the default behavior, but will be removed in a future
135 release.
136
137 WATCH_DEVICE
138 Restrict watching to a speciied device, patch given as second
139 argument.
140
141 POLL_NONBLOCK
142 Normally gps_poll() blocks until either there is a read error or
143 some data is received from tha daemon. In this mode, gps_poll()
144 returns immediately with a value of 0 if there is no input waiting.
145
146 gps_set_raw_hook() takes a function you specify and run it
147 (synchronously) on the raw data pulled by a gps_query() or gps_poll()
148 call. The arguments passed to this hook will be a pointer to a
149 structure containing parsed data, and a buffer containining the raw
150 gpsd response.
151
152 gps_errstr() returns an ASCII string (in English) describing the error
153 indicated by a nonzero return value from gps_open().
154
155 Consult gps.h to learn more about the data members and associated
156 timestamps. Note that information will accumulate in the session
157 structure over time, and the 'valid' field is not automatically zeroed
158 by each poll. It is up to the client to zero that field when
159 appropriate and to keep an eye on the fix and sentence timestamps.
160
161 The Python implementation supports the same facilities as the C
162 library. gps_open() is replaced by the initialization of a gps session
163 object; the other calls are methods of that object, and have the same
164 names as the corresponding C functions. Resources within the session
165 object will be properly released when it is garbage-collected. Note one
166 limitation: POLL_NOBLOCK is not yet supported in Python; use the
167 waiting() method instead.
168
170 The following is an excerpted and simplified version of the libgps
171 interface code from xgps(1). The function handle_input() is a trivial
172 piece of code that calls gps_poll(gpsdata).
173
174 gpsdata = gps_open(server, port);
175
176 build_gui(toplevel);
177
178 gps_set_raw_hook(gpsdata, update_panel);
179
180 (void)gps_stream(gpsdata, WATCH_ENABLE, NULL);
181
182 (void)XtAppAddInput(app, gpsdata->gps_fd,
183 (XtPointer)XtInputReadMask, handle_input, NULL);
184 (void)XtAppMainLoop(app);
185
186 (void)gps_close(gpsdata);
187
189 In the C API, incautious use of gps_send() may lead to subtle bugs. In
190 order to not bloat struct gps_data_t with space used by responses that
191 are not expected to be shipped in close sequence with each other, the
192 storage for fields associated with certain responses are combined in a
193 union.
194
195 The risky set of responses includes VERSION, DEVICELIST, RTCM2, RTCM3,
196 and AIS; it may not be limited to that set. The logic of the daemon's
197 watcher mode is careful to avoid dangerous sequences, but you should
198 read and understand the layout of struct gps_data_t before using
199 gps_send() to request any of these responses.
200
202 The gps_query() supported in major versions 1 and 2 of this library has
203 been removed. With the new streaming-oriented wire protocol behind this
204 library, it is extremely unwise to assume that the first transmission
205 from the damon after a command is shipped to it will be the reponse to
206 command.
207
208 If you must send commands to the daemon explicity, use gps_send() but
209 beware that this ties your code to the GPSD wire protocol. It is not
210 recommended.
211
212 This API has been stable since GPSD 2.90, except that gps_waiting() was
213 added in 2.91.
214
216 gpsd(8), gps(1), libgpsd(3). libgpsmm(3).
217
219 Eric S. Raymond <esr@thyrsus.com>, Thread-callback methods in the C
220 binding added by Alfredo Pironti <alfredo@users.sourceforge.net>.
221
222
223
224The GPSD Project 14 Aug 2004 3(3)