14 Aug 2004
3
3
The GPSD Project
GPSD Documentation
libgps
C service library for communicating with the GPS daemon
C:
#include <gps.h>
int gps_open
char *server
char * port
struct gps_data_t *gpsdata
int gps_send
struct gps_data_t *gpsdata
char *fmt...
int gps_read
struct gps_data_t *gpsdata
bool gps_waiting
struct gps_data_t *gpsdata
void gps_close
struct gps_data_t *gpsdata
int gps_stream
struct gps_data_t *gpsdata
unsigned intflags
void *data
char *gps_errstr
int err
Python:
import gps
session = gps.gps(host="localhost", port="2947")
session.stream(flags=WATCH_JSON)
for report in session:
process(report)
del session
DESCRIPTION
libgps is a service library which
supports communicating with an instance of the
gpsd8; link it with the linker option -lgps.
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
gpsd1.
gps_open() returns NULL on errors and is
re-entrant. errno is set depending on the error returned from the the
socket layer; see gps.h for values and
explanations; also see gps_errstr(). The host
address may be a DNS name, an IPv4 dotted quad, or an IPV6 address;
the library will do the right thing for any of these.
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
gpsd1.
It may have % elements as for
sprintf3,
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. See gps_stream() as a possible
alternative.
gps_read() 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). This function
does a blocking read waiting for data from the daemon (but see
POLL_NONBLOCK below); it returns a count of bytes read for success,
-1 with errno set on a Unix-level read error, -1 with errno not set
if the socket to the daemon has closed, and 0 if POLL_NONBLOCK was
passed to gps_stream() and no data is yet available.
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 when 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 the list below. Calling gps_stream()
more than once with different flag masks is allowed.
WATCH_DISABLE
Disable the reporting modes specified by the other WATCH_ flags.
Cannot be used to disable POLL_NONBLOCK.
WATCH_ENABLE
Disable the reporting modes specified by the other WATCH_ flags.
This is the default.
WATCH_JSON
Enable JSON reporting of data. If WATCH_ENABLE is set, and no
other WATCH flags are set, this is the default.
WATCH_NMEA
Enable generated pseudo-NMEA reporting on binary devices.
WATCH_RARE
Enable reporting of binary packets in encoded hex.
WATCH_RAW
Enable literal passthrough of binary packets.
WATCH_SCALED
When reporting AIS data, scale integer quantities to floats if
they have a divisor or rendering formula associated with them.
WATCH_NEWSTYLE
Force issuing a JSON initialization and getting new-style
responses. This is the default.
WATCH_OLDSTYLE
Force issuing a W or R command and getting old-style
responses. Warning: this flag (and the capability) will be removed
in a future release.
WATCH_DEVICE
Restrict watching to a specified device, patch given as second
argument.
POLL_NONBLOCK
Normally gps_read() blocks until
either there is a read error or some data is received from the
daemon. In this mode, gps_read() returns
immediately with a value of 0 if there is no input waiting.
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 gps_read().
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.
However, it is simpler just to use the sessiion object as an iterator,
as in the example given below. Resources within the session object
will be properly released when it is garbage-collected. Note one
limitation: POLL_NONBLOCK is not yet supported in Python; use the
waiting() method instead.
CODE EXAMPLE
The following is an excerpted and simplified version of the
libgps interface code from
cgps1.
struct gps_data_t gps_data;
ret = gps_open(hostName, hostPort, &gps_data);
(void) gps_stream(&gps_data, WATCH_ENABLE | WATCH_JSON, NULL);
/* Put this in a loop with a call to a high resolution sleep () in it. */
if (gps_waiting (&gps_data)) {
errno = 0;
if (gps_read (&gps_data) == -1) {
...
} else {
/* Display data from the GPS receiver. */
if (gps_data.set & ...
}
}
/* When you are done... */
(void) gps_stream(&gps_data, WATCH_DISABLE, NULL);
gps_close (&gps_data);
LIMITATIONS
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.
COMPATIBILITY
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 daemon after a
command is shipped to it will be the response to command.
If you must send commands to the daemon explicitly, use
gps_send() but beware that this ties your code to
the GPSD wire protocol. It is not recommended.
See the comment above the symbol GPSD_API_MAJOR_VERSION
in gps.h for recent changes.
SEE ALSO
gpsd8,
gps1,
libgpsd3.
libgpsmm3.
AUTHOR
Eric S. Raymond <esr@thyrsus.com>, C sample code Charles Curley <charlescurley@charlescurley.com>