13(3) 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
15
16 struct gps_data_t *gps_open (char *server, char * port);
17
18 int gps_query (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));
22
23 int gps_poll (struct gps_data_t *gpsdata);
24
25 void gps_close (struct gps_data_t *gpsdata);
26
27 void gps_set_callback (struct gps_data_t *gpsdata,
28 void (*callback)(struct gps_data_t *sentence, char *buf),
29 pthread_t *handler);
30
31 void gps_del_callback (struct gps_data_t *gpsdata, pthread *handler);
32
33 void rtcm_unpack (struct rtcm_t *rtcmp, char *buf);
34
35
36
37 Python:
38
39 import gps
40
41 session = gps.gps(host="127.0.0.1", port="2947")
42
43 session.set_raw_hook(raw_hook)
44
45 session.query(commands)
46
47 session.poll()
48
49 del session
50
51
52
53
54
56 libgps is a service library which supports querying GPS devices; link
57 it with the linker option -lgps. There are two interfaces supported in
58 it; one high-level interface that goes through gpsd(1) and is intended
59 for concurrent use by several applications, and one low-level interface
60 that speaks directly with the serial or USB device to which the GPS is
61 attached. This page describes the high-level interface that is safe for
62 multiple applications to use simultaneously; it is probably the one you
63 want. The low-level interface is documented at libgpsd(3).
64
65
66 Calling gps_open() initializes a GPS-data structure to hold the data
67 collected by the GPS, and returns a socket attached to gpsd(1).
68 gps_open() returns NULL on errors. errno is set depending on the error
69 returned from the the socket layer; see gps.h for values and explana‐
70 tions.
71
72
73 gps_close() ends the session.
74
75
76 gps_poll() accepts a response, or sequence of responses, from the dae‐
77 mon and interprets it as though it were a query response (the return
78 value is as for a query). gps_poll() returns the validity mask of the
79 received structure. This function does a blocking read waiting for data
80 from the daemon; it returns 0 for success, or -1 on a Unix-level read
81 error.
82
83
84 gps_query() writes a command to the daemon, accepts a one-line re‐
85 sponse, and updates parts of the GPS-data structure that correspond to
86 data changed since the last call. The second argument must be a format
87 string containing letters from the command set documented at gpsd(1).
88 It may have % elements as for sprintf(3), which will be filled in from
89 any following arguments. This function returns a 0 on success, or a -1
90 if there was a Unix-level read error.
91
92
93 gps_set_raw_hook() takes a function you specify and run it (syn‐
94 chronously) on the raw data pulled by a gps_query() or gps_poll() call.
95 The arguments passed to this hook will be a pointer to a structure con‐
96 taining parsed data, and a buffer containining the raw gpsd response.
97
98
99 gps_set_callback() takes a function you specify and runs it asyn‐
100 chronously each time new data arrives from gpsd, using POSIX threads.
101 For example, you can call gps_set_callback(gpsdata, my_function, han‐
102 dler) once in your program, and from there on your gpsdata structure
103 will be parsed by your my_function() each time new data are available.
104 my_function() could change some global variables in your program based
105 on received data; it is your responsibility to ensure that your program
106 uses mutexes or other mechanisms to avoid race conditions.
107
108
109 gps_del_callback() deregisters the callback function previously set
110 with gps_set_callback(). After the invocation of this function no oper‐
111 ation will be done when new data arrives.
112
113
114 Consult gps.h to learn more about the data members and associated time‐
115 stamps. Note that information will accumulate in the session structure
116 over time, and the 'valid' field is not automatically zeroed by each
117 poll. It is up to the client to zero that field when appropriate and to
118 keep an eye on the fix and sentence timestamps.
119
120
121 The rtcm_unpack() will be useful when you are connected to an RTCM-104
122 source in raw mode. Use it as part of a raw hook, calling it with the
123 address of the struct rtcm_t element of your session structure buffer
124 as first argument and the buffer as the second. It will unpack a line
125 of RTCM data into the structure. This function returns 0 when it has
126 read the last line of an RTCM-104 message, a positive int when it ex‐
127 pects more dump lines, and a negative int on parse failure. You must
128 zero out the struct rtcm_t each time before this function is called on
129 a new header (H) line, as it relies on the message type field being
130 initially zero and uses it to track what kind of following line is ex‐
131 pected.
132
133
134 The Python implementation supports the same facilities as the C li‐
135 brary. gps_open() is replaced by the initialization of a gps session
136 object; the other calls are methods of that object, and have the same
137 names as the corresponding C functions. Resources within the session
138 object will be properly released when it is garbage-collected.
139
140
142 The following is an excerpted and simplified version of the libgps in‐
143 terface code from gps(1). The function handle_input() is a trivial pies
144 of code that calls gps_poll(gpsdata).
145
146
147 gpsdata = gps_open(server, port);
148
149 build_gui(toplevel);
150
151 gps_set_raw_hook(gpsdata, update_panel);
152
153 (void)gps_query(gpsdata, "w+x\n");
154
155 (void)XtAppAddInput(app, gpsdata->gps_fd,
156 (XtPointer)XtInputReadMask, handle_input, NULL);
157 (void)XtAppMainLoop(app);
158
159 (void)gps_close(gpsdata);
160
161
162
164 gpsd(8), gps(1), libgps(3). libgpsmm(3).
165
166
168 Eric S. Raymond <esr@thyrsus.com>, Thread-callback methods in the C
169 binding added by Alfredo Pironti <alfredo@users.sourceforge.net>.
170
171
172
173
174 14 Aug 2004 3(3)