2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
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.
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.
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.
34 struct vehicle_priv *priv;
35 struct vehicle_methods meth;
36 struct callback_list *cbl;
37 struct log *nmea_log, *gpx_log;
41 vehicle_log_nmea(struct vehicle *this_, struct log *log)
44 if (!this_->meth.position_attr_get)
46 if (!this_->meth.position_attr_get(this_->priv, attr_position_nmea, &pos_attr))
48 log_write(log, pos_attr.u.str, strlen(pos_attr.u.str));
52 vehicle_log_gpx(struct vehicle *this_, struct log *log)
56 if (!this_->meth.position_attr_get)
58 if (!this_->meth.position_attr_get(this_->priv, attr_position_coord_geo, &pos_attr))
60 sprintf(buffer,"<trkpt lat=\"%f\" lon=\"%f\" />\n", pos_attr.u.coord_geo->lat, pos_attr.u.coord_geo->lng);
61 log_write(log, buffer, strlen(buffer));
66 vehicle_log_textfile(struct vehicle *this_, struct log *log)
70 if (!this_->meth.position_attr_get)
72 if (!this_->meth.position_attr_get(this_->priv, attr_position_coord_geo, &pos_attr))
74 sprintf(buffer,"%f %f type=trackpoint\n", pos_attr.u.coord_geo->lng, pos_attr.u.coord_geo->lat);
75 log_write(log, buffer, strlen(buffer));
79 vehicle_add_log(struct vehicle *this_, struct log *log)
82 struct attr type_attr;
83 if (!log_get_attr(log, attr_type, &type_attr, NULL))
86 if (!strcmp(type_attr.u.str, "nmea")) {
87 cb=callback_new_2(callback_cast(vehicle_log_nmea), this_, log);
88 } else if (!strcmp(type_attr.u.str, "gpx")) {
90 "<?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";
91 char *trailer = "</trkseg>\n</trk>\n</gpx>\n";
92 log_set_header(log, header, strlen(header));
93 log_set_trailer(log, trailer, strlen(trailer));
94 cb=callback_new_2(callback_cast(vehicle_log_gpx), this_, log);
95 } else if (!strcmp(type_attr.u.str, "textfile")) {
96 char *header = "type=track\n";
97 log_set_header(log, header, strlen(header));
98 cb=callback_new_2(callback_cast(vehicle_log_textfile), this_, log);
101 callback_list_add(this_->cbl, cb);
106 vehicle_new(struct attr **attrs)
108 struct vehicle *this_;
110 struct vehicle_priv *(*vehicletype_new) (struct vehicle_methods *
112 struct callback_list *
114 struct attr ** attrs);
119 source = attr_search(attrs, NULL, attr_source);
121 dbg(0, "no source\n");
125 type = g_strdup(source->u.str);
126 colon = index(type, ':');
129 dbg(1, "source='%s' type='%s'\n", source->u.str, type);
131 vehicletype_new = plugin_get_vehicle_type(type);
132 if (!vehicletype_new) {
133 dbg(0, "invalid type '%s'\n", type);
136 this_ = g_new0(struct vehicle, 1);
137 this_->cbl = callback_list_new();
138 this_->priv = vehicletype_new(&this_->meth, this_->cbl, attrs);
140 dbg(0, "vehicletype_new failed\n");
141 callback_list_destroy(this_->cbl);
147 if ((name=attr_search(attrs, NULL, attr_name))) {
148 this_->name=g_strdup(name->u.str);
150 this_->name=g_strdup("Noname");
156 vehicle_get_attr(struct vehicle *this_, enum attr_type type, struct attr *attr)
160 attr->u.str=this_->name;
163 if (this_->meth.position_attr_get) {
164 return this_->meth.position_attr_get(this_->priv, type, attr);
174 vehicle_set_attr(struct vehicle *this_, struct attr *attr,
177 if (this_->meth.set_attr)
178 return this_->meth.set_attr(this_->priv, attr, attrs);
183 vehicle_add_attr(struct vehicle *this_, struct attr *attr)
185 switch (attr->type) {
187 callback_list_add(this_->cbl, attr->u.callback);
190 return vehicle_add_log(this_, attr->u.log);
198 vehicle_remove_attr(struct vehicle *this_, struct attr *attr)
200 switch (attr->type) {
202 callback_list_remove(this_->cbl, attr->u.callback);
211 vehicle_destroy(struct vehicle *this_)
213 callback_list_destroy(this_->cbl);
214 if(this_->name) g_free(this_->name);