C: #include <gps.h>
struct gps_data_t *gps_open( | af, | |
| server, | ||
port); |
intaf;char *server;char * port;int gps_open_r( | server, | |
| port, | ||
gpsdata); |
char *server;char * port;struct gps_data_t *gpsdata;int gps_send( | gpsdata, | |
fmt); |
struct gps_data_t *gpsdata;char *fmt...;void gps_set_raw_hook( | gpsdata, | |
hook); |
struct gps_data_t *gpsdata;void (*hook)(struct gps_data_t *,
char *buf, size_t len);int gps_poll( | gpsdata); |
struct gps_data_t *gpsdata;bool gps_waiting( | gpsdata); |
struct gps_data_t *gpsdata;void gps_close( | gpsdata); |
struct gps_data_t *gpsdata;int gps_stream( | gpsdata, | |
| flags, | ||
data); |
struct gps_data_t *gpsdata;unsigned intflags;void *data;char *gps_errstr( | err); |
int err;
Python:
import gps
session = gps.gps(host="localhost", port="2947")
session.set_raw_hook(raw_hook)
session.stream(flags=WATCH_JSON)
for report in session:
process(report)
del session
libgps 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 gpsd(1) 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 libgpsd(3).
Take care to conditionalize your code on the major and
minor API version symbols in gps.h; ideally,
force a compilation failure if GPSD_API_MAJOR_VERSION is not a version
you recognize. See the GPSD project website for more information on
the protocol and API changes.
Calling gps_open() initializes a GPS-data
structure to hold the data collected by the GPS, and returns a socket
attached to
gpsd(1).
gps_open() returns NULL on errors. errno is
set depending on the error returned from the the socket layer; see
gps.h for values and explanations. The host
address may be a DNS name, an IPv4 dotted quad, or an IPV6 address;
the library will do the roight thing for any of these.
gps_open_r() is a reentrent-friendly
version that puts the session storage where you wish to allocate it.
It returns 0 on success and -1 on failure, with errno set
appropriately.
gps_close() ends the session.
gps_send() writes a command to the daemon.
The second argument must be a format string containing elements from
the command set documented at
gpsd(1).
It may have % elements as for
sprintf(3),
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.
gps_poll() 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).
gps_poll() 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.
gps_waiting() can be used to check
whether there is data from the daemon. It returns true if there is,
false on no data waiting or error condition. It does not block
waiting for input.
gps_stream() asks
gpsd to stream the reports it has at you,
to be made available whenn you poll. It is preferable to the
older-style (pre-2.90) way of doing this,
gps_query() with a "w+" argument, because it
insulates your code from whether your client library and your
gpsd are using old or new protocol.
The second argument is a flag mask that sets various policy bits;
see trhe list below. Calling gps_stream()
more than once with different flag masks is allowed.
Disable the reporting modes specified by the other WATCH_ flags. Cannot be used to disable POLL_NONBLOCK.
Disable the reporting modes specified by the other WATCH_ flags. This is the default.
Enable JSON reporting of data. If WATCH_ENABLE is set, and nmo other WATCH flags are set, this ids the default.
Enable generated pseudo-NMEA reporting on binary devices.
Enable reporting of binary packets in encoded hex.
Enable literal passtrough of binary packets.
When reporting AIS data, scale integer quantities to floats if they have a divisor or rendering formula assosiated with them.
Force issuing a JSON initialization and getting new-style responses. This will become the default in a future release.
Force issuing a W or R command and getting old-style responses. This is now the default behavior, but will be removed in a future release.
Restrict watching to a speciied device, patch given as second argument.
Normally gps_poll() blocks until
either there is a read error or some data is received from tha
daemon. In this mode, gps_poll() returns
immediately with a value of 0 if there is no input waiting.
gps_set_raw_hook() takes a function you
specify and run it (synchronously) on the raw data pulled by a
gps_query() or gps_poll()
call. The arguments passed to this hook will be a pointer to a
structure containing parsed data, and a buffer containining the
raw gpsd response.
gps_errstr() returns an ASCII string (in
English) describing the error indicated by a nonzero return value from
gps_open().
Consult gps.h 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.
The Python implementation supports the same facilities as the C
library. gps_open() 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. Note one limitation: POLL_NOBLOCK is not yet
supported in Python; use the waiting() method instead.
The following is an excerpted and simplified version of the
libgps interface code from
xgps(1).
The function handle_input() is a trivial piece of
code that calls gps_poll(gpsdata).
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);
In the C API, incautious use of gps_send()
may lead to subtle bugs. In order to not bloat struct
gps_data_t 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.
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 struct
gps_data_t before using gps_send()
to request any of these responses.
The gps_query() 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.
If you must send commands to the daemon explicity, use
gps_send() but beware that this ties your code to
the GPSD wire protocol. It is not recommended.
This API has been stable since GPSD 2.90, except that
gps_waiting() was added in 2.91.