2 * gps - gps resource for zavai
4 * Copyright (C) 2009--2010 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 // For a list of dbus services, look in /etc/dbus-1/system.d/
27 public class GPS: zavai.Service
29 protected libgps.data_t data;
30 protected IOChannel gpsfd = null;
31 protected uint gpsfd_watch = 0;
33 protected int old_fix_status = libgps.STATUS_NO_FIX;
35 public signal void fix_status_changed(int status);
40 data = libgps.data_t();
43 public int fix_status() { return old_fix_status; }
45 protected bool on_input_data(IOChannel source, IOCondition condition)
47 while (libgps.waiting(ref data))
49 int res = libgps.poll(ref data);
51 zavai.log.error(libgps.errstr(res));
53 if (data.status != old_fix_status)
55 fix_status_changed(data.status);
56 old_fix_status = data.status;
59 stderr.printf("GPSMSG %d %d\n", (int)data.set, data.status);
60 stderr.printf("SATUSED %d\n", data.satellites_used);
61 stderr.printf("SWT %f\n", data.skyview_time);
62 stderr.printf("SATVIS %d\n", data.satellites_visible);
63 for (int i = 0; i < data.satellites_visible; ++i)
65 stderr.printf("PRN %d ELE %d AZI %d SS %f\n",
66 data.PRN[i], data.elevation[i], data.azimuth[i], data.ss[i]);
73 /// Request GPS resource
74 public override void start()
79 // Then run our own script
80 zavai.app.run_script(zavai.config.homedir + "/gps start");
82 zavai.log.error("Running " + zavai.config.homedir + "/gps start: " + e.message);
86 int res = libgps.open_r("ciapino", "gpsd", ref data);
89 zavai.log.error(libgps.errstr(res));
93 res = libgps.stream(ref data, libgps.WATCH_ENABLE, null);
96 zavai.log.error(libgps.errstr(res));
100 res = libgps.send(ref data, "?SKY;");
101 //res = libgps.send(ref data, "?WATCH;");
102 //if (res != 0) zavai.log.error(libgps.errstr(res));
104 gpsfd = new IOChannel.unix_new(data.gps_fd);
106 gpsfd.set_encoding(null);
108 zavai.log.error("Setting encoding to null on gpsd io channel: " + e.message);
110 //gpsfd.set_buffered(false);
111 gpsfd_watch = gpsfd.add_watch(IOCondition.IN, on_input_data);
113 zavai.log.info("GPS turned on");
117 // Release usage of GPS
118 public override void stop()
120 if (!started) return;
122 Source.remove(gpsfd_watch);
124 int res = libgps.close(ref data);
126 zavai.log.error(libgps.errstr(res));
129 zavai.app.run_script(zavai.config.homedir + "/gps stop");
131 zavai.log.error("Running device stop gps: " + e.message);
134 if (old_fix_status != libgps.STATUS_NO_FIX)
136 old_fix_status = libgps.STATUS_NO_FIX;
137 fix_status_changed(old_fix_status);
144 public class Position : zavai.Service
146 dynamic DBus.Object position;
148 public signal void position_changed(int fields, int tstamp, double lat, double lon, double alt);
152 Object(name: "gps.position");
153 position = zavai.registry.sbus.get_object(
154 "org.freesmartphone.ogpsd",
155 "/org/freedesktop/Gypsy",
156 "org.freedesktop.Gypsy.Position");
159 public void on_position_changed(dynamic DBus.Object pos, int fields, int tstamp, double lat, double lon, double alt)
161 zavai.log.info("gps position: position changed");
162 position_changed(fields, tstamp, lat, lon, alt);
165 public override void start()
168 zavai.log.info("Starting GPS position tracking");
169 gps.request("gps.position");
170 position.PositionChanged += on_position_changed;
174 public override void stop()
176 if (!started) return;
177 zavai.log.info("Stopping GPS position tracking");
178 position.PositionChanged -= on_position_changed;
179 gps.release("gps.position");
184 // # def wait_for_fix(self, callback):
185 // # status = self.gps.GetFixStatus()
186 // # if status in [2, 3]:
187 // # zavai.info("We already have a fix, good.")
191 // # zavai.info("Waiting for a fix...")
192 // # self.waiting_for_fix = callback
193 // # self.bus.add_signal_receiver(
194 // # self.on_fix_status_changed, 'FixStatusChanged', 'org.freedesktop.Gypsy.Device',
195 // # 'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
198 // # def start_recording(self):
199 // # if self.gps_monitor:
200 // # self.gps_monitor.stop()
201 // # self.gps_monitor = None
203 // # if not self.audio:
206 // # # Sync system time
207 // # gpstime = self.gps.gps_time.GetTime()
208 // # subprocess.call(["date", "-s", "@%d" % gpstime])
209 // # subprocess.call(["hwclock", "--systohc"])
211 // # # Compute basename for output files
212 // # self.basename = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(gpstime))
213 // # self.basename = os.path.join(AUDIODIR, self.basename)
215 // # # Start recording the GPX track
216 // # self.gpx = GPX(self.basename)
217 // # self.gps.track_position(self.on_position_changed)
219 // # # Start recording in background forking arecord
220 // # self.audio.set_basename(self.basename)
221 // # self.audio.start_recording()
224 // Write GPX track and waypoint files
225 public class GPX : Service
227 public bool tracking {
228 get { return trk != null; }
231 public signal void tracking_changed(bool tracking);
233 FileStream trk = null;
234 FileStream wpt = null;
236 bool last_valid = false;
245 Object(name: "gps.gpx");
248 public override void start()
252 log.info("Starting GPX trace subsystem");
253 position.request("gps.gpx");
254 position.position_changed += on_position_changed;
259 public override void stop()
263 log.info("Stopping GPX trace subsystem");
264 position.release("gps.gpx");
265 position.position_changed -= on_position_changed;
271 public void on_position_changed(Position pos, int fields, int tstamp, double lat, double lon, double alt)
273 last_fields = fields;
274 last_tstamp = tstamp;
282 public void start_track(time_t tstamp = 0, string? basename = null)
285 if (basename != null)
289 time_t now = tstamp == 0 ? time_t() : tstamp;
291 // Compute basename for output files
292 var t = Time.local(now);
293 char[] res = new char[25];
294 t.strftime(res, "%Y-%m-%d-%H-%M-%S");
295 fname = zavai.config.homedir + "/" + (string)res;
298 trk = FileStream.open(fname + "-trk.gpx", "wt");
299 trk.puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
301 trk.puts(" version=\"1.0\"");
302 trk.printf(" creator=\"zavai %s\"\n", zavai.config.version);
303 trk.puts(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
304 trk.puts(" xmlns=\"http://www.topografix.com/GPX/1/0\"");
305 trk.puts(" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">");
307 trk.puts(" <trkseg>");
309 wpt = FileStream.open(fname + "-wpt.gpx", "wt");
310 wpt.puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
312 wpt.puts(" version=\"1.0\"");
313 wpt.printf(" creator=\"zavai %s\"", zavai.config.version);
314 wpt.puts(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
315 wpt.puts(" xmlns=\"http://www.topografix.com/GPX/1/0\"");
316 wpt.puts(" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">");
319 tracking_changed(true);
322 public void stop_track()
326 trk.puts("</trkseg></trk></gpx>");
337 tracking_changed(false);
340 // Mark a track point
341 public void trackpoint()
343 if (!last_valid) return;
345 start_track(last_tstamp);
347 trk.printf("<trkpt lat=\"%f\" lon=\"%f\">\n", last_lat, last_lon);
348 var t = Time.local(last_tstamp);
349 char[] ts = new char[25];
350 t.strftime(ts, "%Y-%m-%dT%H:%M:%SZ");
351 trk.printf(" <time>%s</time>\n", (string)ts);
352 trk.printf(" <ele>%f</ele>\n", last_alt);
353 // if course is not None: print >>self.trk, " <course>%f</course>" % course
354 // if speed is not None: print >>self.trk, " <speed>%f</speed>" % speed
355 // if fix is not None: print >>self.trk, " <fix>%f</fix>" % fix
356 // if hdop is not None: print >>self.trk, " <hdop>%f</hdop>" % hdop
357 trk.puts("</trkpt>");
361 public void waypoint(string? name = null)
363 if (!last_valid) return;
365 start_track(last_tstamp);
370 wptname = "wpt_%d".printf(wpt_seq);
376 wpt.printf("<wpt lat=\"%f\" lon=\"%f\">\n", last_lat, last_lon);
377 wpt.printf(" <name>%s</name>\n", wptname);
378 var t = Time.local(last_tstamp);
379 char[] ts = new char[25];
380 t.strftime(ts, "%Y-%m-%dT%H:%M:%SZ");
381 wpt.printf(" <time>%s</time>\n", (string)ts);
382 wpt.printf(" <ele>%f</ele>\n", last_alt);
387 public zavai.gps.GPS gps = null;
388 public zavai.gps.Position position = null;
389 public zavai.gps.GPX gpx = null;
394 position = new Position();