Core:Patch:Ticket #179 - Patch from RedDog - Add timestamp to gpxlog
[navit-package] / navit / vehicle.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <glib.h>
23 #include "config.h"
24 #include "debug.h"
25 #include "coord.h"
26 #include "item.h"
27 #include "log.h"
28 #include "callback.h"
29 #include "plugin.h"
30 #include "vehicle.h"
31
32 struct vehicle {
33         char *name;
34         struct vehicle_priv *priv;
35         struct vehicle_methods meth;
36         struct callback_list *cbl;
37         struct log *nmea_log, *gpx_log;
38 };
39
40 static void
41 vehicle_log_nmea(struct vehicle *this_, struct log *log)
42 {
43         struct attr pos_attr;
44         if (!this_->meth.position_attr_get)
45                 return;
46         if (!this_->meth.position_attr_get(this_->priv, attr_position_nmea, &pos_attr))
47                 return;
48         log_write(log, pos_attr.u.str, strlen(pos_attr.u.str));
49 }
50
51 static void
52 vehicle_log_gpx(struct vehicle *this_, struct log *log)
53 {
54         struct attr pos_attr;
55         char buffer[256];
56         GTimeVal time; 
57         g_get_current_time(&time); 
58
59         if (!this_->meth.position_attr_get)
60                 return;
61         if (!this_->meth.position_attr_get(this_->priv, attr_position_coord_geo, &pos_attr))
62                 return;
63         sprintf(buffer,"<trkpt lat=\"%f\" lon=\"%f\">\n\t<time>%s</time>\n</trkpt>\n",
64                 pos_attr.u.coord_geo->lat, pos_attr.u.coord_geo->lng, g_time_val_to_iso8601(&time));
65         log_write(log, buffer, strlen(buffer));
66 }
67
68
69 static void
70 vehicle_log_textfile(struct vehicle *this_, struct log *log)
71 {
72         struct attr pos_attr;
73         char buffer[256];
74         if (!this_->meth.position_attr_get)
75                 return;
76         if (!this_->meth.position_attr_get(this_->priv, attr_position_coord_geo, &pos_attr))
77                 return;
78         sprintf(buffer,"%f %f type=trackpoint\n", pos_attr.u.coord_geo->lng, pos_attr.u.coord_geo->lat);
79         log_write(log, buffer, strlen(buffer));
80 }
81
82 static int
83 vehicle_add_log(struct vehicle *this_, struct log *log)
84 {
85         struct callback *cb;
86         struct attr type_attr;
87         if (!log_get_attr(log, attr_type, &type_attr, NULL))
88                 return 1;
89
90         if (!strcmp(type_attr.u.str, "nmea")) {
91                 cb=callback_new_2(callback_cast(vehicle_log_nmea), this_, log);
92         } else if (!strcmp(type_attr.u.str, "gpx")) {
93                 char *header =
94                     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gpx version=\"1.0\" creator=\"Navit http://navit.sourceforge.net\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/0\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n<trk>\n<trkseg>\n";
95                 char *trailer = "</trkseg>\n</trk>\n</gpx>\n";
96                 log_set_header(log, header, strlen(header));
97                 log_set_trailer(log, trailer, strlen(trailer));
98                 cb=callback_new_2(callback_cast(vehicle_log_gpx), this_, log);
99         } else if (!strcmp(type_attr.u.str, "textfile")) {
100                 char *header = "type=track\n";
101                 log_set_header(log, header, strlen(header));
102                 cb=callback_new_2(callback_cast(vehicle_log_textfile), this_, log);
103         } else
104                 return 1;
105         callback_list_add(this_->cbl, cb);
106         return 0;
107 }
108
109 struct vehicle *
110 vehicle_new(struct attr **attrs)
111 {
112         struct vehicle *this_;
113         struct attr *source;
114         struct vehicle_priv *(*vehicletype_new) (struct vehicle_methods *
115                                                  meth,
116                                                  struct callback_list *
117                                                  cbl,
118                                                  struct attr ** attrs);
119         char *type, *colon;
120         struct attr *name;
121
122         dbg(1, "enter\n");
123         source = attr_search(attrs, NULL, attr_source);
124         if (!source) {
125                 dbg(0, "no source\n");
126                 return NULL;
127         }
128
129         type = g_strdup(source->u.str);
130         colon = index(type, ':');
131         if (colon)
132                 *colon = '\0';
133         dbg(1, "source='%s' type='%s'\n", source->u.str, type);
134
135         vehicletype_new = plugin_get_vehicle_type(type);
136         if (!vehicletype_new) {
137                 dbg(0, "invalid type '%s'\n", type);
138                 return NULL;
139         }
140         this_ = g_new0(struct vehicle, 1);
141         this_->cbl = callback_list_new();
142         this_->priv = vehicletype_new(&this_->meth, this_->cbl, attrs);
143         if (!this_->priv) {
144                 dbg(0, "vehicletype_new failed\n");
145                 callback_list_destroy(this_->cbl);
146                 g_free(this_);
147                 return NULL;
148         }
149         dbg(1, "leave\n");
150
151         if ((name=attr_search(attrs, NULL, attr_name))) {
152                 this_->name=g_strdup(name->u.str);
153         } else {
154                 this_->name=g_strdup("Noname");
155         }
156         return this_;
157 }
158
159 int
160 vehicle_get_attr(struct vehicle *this_, enum attr_type type, struct attr *attr)
161 {
162         switch (type) {
163         case attr_name:
164                 attr->u.str=this_->name;
165                 break;
166         default:
167                 if (this_->meth.position_attr_get) {
168                         return this_->meth.position_attr_get(this_->priv, type, attr);
169                 } else {
170                         return 0;
171                 }
172         }
173         attr->type=type;
174         return 1;
175 }
176
177 int
178 vehicle_set_attr(struct vehicle *this_, struct attr *attr,
179                  struct attr **attrs)
180 {
181         if (this_->meth.set_attr)
182                 return this_->meth.set_attr(this_->priv, attr, attrs);
183         return 0;
184 }
185
186 int
187 vehicle_add_attr(struct vehicle *this_, struct attr *attr)
188 {
189         switch (attr->type) {
190         case attr_callback:
191                 callback_list_add(this_->cbl, attr->u.callback);
192                 break;
193         case attr_log:
194                 return vehicle_add_log(this_, attr->u.log);
195         default:
196                 return 0;
197         }
198         return 1;
199 }
200
201 int
202 vehicle_remove_attr(struct vehicle *this_, struct attr *attr)
203 {
204         switch (attr->type) {
205         case attr_callback:
206                 callback_list_remove(this_->cbl, attr->u.callback);
207                 break;
208         default:
209                 return 0;
210         }
211         return 1;
212 }
213
214 void
215 vehicle_destroy(struct vehicle *this_)
216 {
217         callback_list_destroy(this_->cbl);
218         if(this_->name) g_free(this_->name);
219         g_free(this_);
220 }