<filename>libgps.c</filename> Functions:-The client interface library for the gpsd daemon. char *deg_to_str( enum deg_str_type type, double f) Convert double degrees to a static string and return a pointer to it.Makes a simple check on invalid degree values (less than 0 or more than 360) and returns "nan" if found.For valid values, it generates the appropriate string according to the string type enumeration, defaulting to DD MM SS.sss enum unit gpsd_units(void) Simple check of the environment to determine what units are required. If all else fails, use compiled in units. struct gps_data_t *gps_open(const char *host, const char *port) Open a connection to a gps daemon.Try to get a gps_data_t structure. If it fails, return a NULL.Test for a specified host and/or port number, using defaults if nothing is specified in the command line invocation.Try to connect to the now defined socket; on error, release the resources and return NULL. On success, initialise the data structure and return the pointer to it. int gps_close(struct gps_data_t *gpsdata) Close the fd associated with the gps_data_t structure and stash the result.If there is a device id stored, NULL it and the associated path.If the device list has any data in it, free this and mark the number of available devices as invalid.Free the gps_data_t structure itself and return the stashed close() result. void gps_set_raw_hook(struct gps_data_t *gpsdata, void (*hook)(struct gps_data_t *, char *, size_t len, int level)) Fill in the hook to the data for use in raw mode. static void gps_unpack(char *buf, struct gps_data_t *gpsdata) Keep looping through the data in buf finding an occurrence of GPSD.Each time one is found, read data in, discarding the initial GPSD.The data is parsed by scanning to either a , or a CR or a LF. This will break the response into gpsd tuples. These are analysed by a big switch statement and the relevant fields and flags are set in the gps_data_t structure.Any empty fields (? in them) are set to a safe value such as NaN.If either of the raw data or thread data hooks is valid, hook the new data in at level 1 (raw mode). int gps_poll(struct gps_data_t *gpsdata) Poll the daemon and if there is no data or an error, return -1.If there is something to read, clear the buffer, note the time as the received data time and the online time, then unpack the data.If profiling is active, use the received data time, the fix time and the present time to calcluate the decode time and the client receipt time. int gps_query(struct gps_data_t *gpsdata, const char *fmt, ... ) Gather up the instructions to a gpsd instance for information to return and write them to the device.If the write fails, return -1; if it succeeds, call gps_poll() to get any response and return its result. static void *poll_gpsd(void *args) Set the thread parameters the way we need them (asynchronously cancellable) and then call gps_poll() continuously until we get a failure. int gps_set_callback(struct gps_data_t *gpsdata, void (*callback)(struct gps_data_t *sentence, char *buf, size_t len, int level), pthread_t *handler) Set gpsd into watcher mode, so it will be generating data.If the thread hook is already active, set it to hook to our data and return a 0, otherwise set it to hook to our data and make a call to pthread_create() and return its value (0 if it succeeded). int gps_del_callback(struct gps_data_t *gpsdata, pthread_t *handler) Cancel the thread and stash the return value.Set the hook to NULL now it is not needed.If the stashed result was good, switch gpsd out of watcher mode.In all cases, return the stashed result. The following three functions are guarded by an #ifdef so they only compile if it is required to be able to test the library and set up a command line exerciser interface. static void data_dump(struct gps_data_t *collect, time_t now) A data dumper used when debugging. It outputs data according to the command line input data. static void dumpline(struct gps_data_t *ud UNUSED, char *buf, size_t ulen UNUSED, int level UNUSED) A simple call to UNIX puts(). int main(int argc, char *argv[]) A simple command line parser and endless loop to exercise the daemon when debugging. Notes based on $Id: libgps.c 4377 2007-06-02 14:52:38Z esr $