/[zanavi_public1]/navit/navit/vehicle/webos/vehicle_webos.c
ZANavi

Contents of /navit/navit/vehicle/webos/vehicle_webos.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Fri Oct 28 21:19:04 2011 UTC (12 years, 5 months ago) by zoff99
File MIME type: text/plain
File size: 7629 byte(s)
import files
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 <config.h>
21 #include <string.h>
22 #include <glib.h>
23 #include <math.h>
24 #include <errno.h>
25 #include <sys/time.h>
26 #include <PDL.h>
27 #include <SDL.h>
28 #include "debug.h"
29 #include "callback.h"
30 #include "plugin.h"
31 #include "coord.h"
32 #include "item.h"
33 #include "vehicle.h"
34 #include "event.h"
35
36 static char *vehicle_webos_prefix="webos:";
37
38 struct vehicle_priv {
39 char *source;
40 char *address;
41 struct callback_list *cbl;
42 struct callback *event_cb;
43 double track, speed, altitude, radius;
44 time_t fix_time;
45 struct coord_geo geo;
46 struct attr ** attrs;
47 char fixiso8601[128];
48 int pdk_version;
49 };
50
51 static void
52 vehicle_webos_callback(PDL_ServiceParameters *params, void *user)
53 {
54 PDL_Location *location;
55 SDL_Event event;
56 SDL_UserEvent userevent;
57 int err;
58
59 err = PDL_GetParamInt(params, "errorCode");
60 if (err != 0) {
61 dbg(0,"Location Callback errorCode %d\n", err);
62 return /*PDL_EOTHER*/;
63 }
64
65 location = g_new0 (PDL_Location, 1);
66
67 location->altitude = PDL_GetParamDouble(params, "altitude");
68 location->velocity = PDL_GetParamDouble(params, "velocity");
69 location->heading = PDL_GetParamDouble(params, "heading");
70 location->horizontalAccuracy = PDL_GetParamDouble(params, "horizAccuracy");
71 location->latitude = PDL_GetParamDouble(params, "latitude");
72 location->longitude = PDL_GetParamDouble(params, "longitude");
73
74 userevent.type = SDL_USEREVENT;
75 userevent.code = PDL_GPS_UPDATE;
76 userevent.data1 = location;
77 userevent.data2 = NULL;
78
79 event.type = SDL_USEREVENT;
80 event.user = userevent;
81
82 SDL_PushEvent(&event);
83
84 return /*PDL_NOERROR*/;
85 }
86
87 static void
88 vehicle_webos_gps_update(struct vehicle_priv *priv, PDL_Location *location)
89 {
90 struct timeval tv;
91 gettimeofday(&tv,NULL);
92 priv->fix_time = tv.tv_sec;
93
94 priv->geo.lat = location->latitude;
95 priv->geo.lng = location->longitude;
96
97 dbg(2,"Location: %f %f %f %.12g %.12g +-%fm\n",
98 location->altitude,
99 location->velocity,
100 location->heading,
101 priv->geo.lat,
102 priv->geo.lng,
103 location->horizontalAccuracy);
104
105 if (location->altitude != -1)
106 priv->altitude = location->altitude;
107 if (location->velocity != -1)
108 priv->speed = location->velocity * 3.6;
109 if (location->heading != -1)
110 priv->track = location->heading;
111 if (location->horizontalAccuracy != -1)
112 priv->radius = location->horizontalAccuracy;
113
114 if (priv->pdk_version <= 100)
115 g_free(location);
116
117 callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
118
119 return;
120 }
121
122 static void
123 vehicle_webos_close(struct vehicle_priv *priv)
124 {
125 if (priv->pdk_version <= 100)
126 PDL_UnregisterServiceCallback((PDL_ServiceCallbackFunc)vehicle_webos_callback);
127 else
128 PDL_EnableLocationTracking(PDL_FALSE);
129 }
130
131 static int
132 vehicle_webos_open(struct vehicle_priv *priv)
133 {
134 PDL_Err err;
135
136 priv->pdk_version = PDL_GetPDKVersion();
137 dbg(1,"pdk_version(%d)\n", priv->pdk_version);
138
139 if (priv->pdk_version <= 100) {
140 err = PDL_ServiceCallWithCallback("palm://com.palm.location/startTracking",
141 "{subscribe:true}",
142 (PDL_ServiceCallbackFunc)vehicle_webos_callback,
143 priv,
144 PDL_FALSE);
145 if (err != PDL_NOERROR) {
146 dbg(0,"PDL_ServiceCallWithCallback failed with (%d): (%s)\n", err, PDL_GetError());
147 vehicle_webos_close(priv);
148 return 0;
149 }
150 }
151 else {
152 dbg(1,"Calling PDL_EnableLocationTracking(PDL_TRUE)\n");
153 err = PDL_EnableLocationTracking(PDL_TRUE);
154 if (err != PDL_NOERROR) {
155 dbg(0,"PDL_EnableLocationTracking failed with (%d): (%s)\n", err, PDL_GetError());
156 vehicle_webos_close(priv);
157 return 0;
158 }
159 }
160
161 return 1;
162 }
163
164 static void
165 vehicle_webos_destroy(struct vehicle_priv *priv)
166 {
167 vehicle_webos_close(priv);
168 if (priv->source)
169 g_free(priv->source);
170 g_free(priv);
171 }
172
173 static int
174 vehicle_webos_position_attr_get(struct vehicle_priv *priv,
175 enum attr_type type, struct attr *attr)
176 {
177 switch (type) {
178 case attr_position_height:
179 dbg(1,"Altitude: %f\n", priv->altitude);
180 attr->u.numd = &priv->altitude;
181 break;
182 case attr_position_speed:
183 dbg(1,"Speed: %f\n", priv->speed);
184 attr->u.numd = &priv->speed;
185 break;
186 case attr_position_direction:
187 dbg(1,"Direction: %f\n", priv->track);
188 attr->u.numd = &priv->track;
189 break;
190 case attr_position_coord_geo:
191 dbg(1,"Coord: %.12g %.12g\n", priv->geo.lat, priv->geo.lng);
192 attr->u.coord_geo = &priv->geo;
193 break;
194 case attr_position_radius:
195 dbg(1,"Radius: %f\n", priv->radius);
196 attr->u.numd = &priv->radius;
197 break;
198 case attr_position_time_iso8601:
199 if (priv->fix_time) {
200 struct tm tm;
201 if (gmtime_r(&priv->fix_time, &tm)) {
202 strftime(priv->fixiso8601, sizeof(priv->fixiso8601),
203 "%Y-%m-%dT%TZ", &tm);
204 attr->u.str=priv->fixiso8601;
205 }
206 else {
207 priv->fix_time = 0;
208 return 0;
209 }
210 dbg(1,"Fix Time: %d %s\n", priv->fix_time, priv->fixiso8601);
211 }
212 else {
213 dbg(1,"Fix Time: %d\n", priv->fix_time);
214 return 0;
215 }
216
217 break;
218 case attr_position_fix_type:
219 if (priv->radius == 0.0)
220 return 0; // strength = -1
221 else if (priv->radius > 20.0)
222 attr->u.num = 0; // strength = 1
223 else
224 attr->u.num = 1; // strength = 2
225
226 break;
227 case attr_position_sats_used:
228 if (priv->radius <= 6.0 )
229 attr->u.num = 6; // strength = 5
230 else if (priv->radius <= 10.0 )
231 attr->u.num = 5; // strength = 4
232 else if (priv->radius <= 15.0 )
233 attr->u.num = 4; // strength = 3
234 else
235 return 0;
236
237 break;
238 default:
239 return 0;
240 }
241 attr->type = type;
242 return 1;
243 }
244
245 static int
246 vehicle_webos_set_attr_do(struct vehicle_priv *priv, struct attr *attr, int init)
247 {
248 switch (attr->type) {
249 case attr_source:
250 if (strncmp(vehicle_webos_prefix,attr->u.str,strlen(vehicle_webos_prefix))) {
251 dbg(1,"source must start with '%s'\n", vehicle_webos_prefix);
252 return 0;
253 }
254 g_free(priv->source);
255 priv->source=g_strdup(attr->u.str);
256 priv->address=priv->source+strlen(vehicle_webos_prefix);
257 if (!priv->address[0])
258 priv->address=NULL;
259 if (!init) {
260 vehicle_webos_close(priv);
261 vehicle_webos_open(priv);
262 }
263 return 1;
264 case attr_profilename:
265 return 1;
266 case attr_pdl_gps_update:
267 vehicle_webos_gps_update(priv, (PDL_Location *)attr->u.data);
268 return 1;
269 default:
270 return 0;
271 }
272 }
273
274 static int
275 vehicle_webos_set_attr(struct vehicle_priv *priv, struct attr *attr)
276 {
277 return vehicle_webos_set_attr_do(priv, attr, 0);
278 }
279
280 struct vehicle_methods vehicle_webos_methods = {
281 vehicle_webos_destroy,
282 vehicle_webos_position_attr_get,
283 vehicle_webos_set_attr,
284 };
285
286 static struct vehicle_priv *
287 vehicle_webos_new(struct vehicle_methods
288 *meth, struct callback_list
289 *cbl, struct attr **attrs)
290 {
291 struct vehicle_priv *priv;
292
293
294 priv = g_new0(struct vehicle_priv, 1);
295 priv->attrs = attrs;
296 priv->cbl = cbl;
297 *meth = vehicle_webos_methods;
298 while (*attrs) {
299 vehicle_webos_set_attr_do(priv, *attrs, 1);
300 attrs++;
301 }
302
303 vehicle_webos_open(priv);
304
305 return priv;
306 }
307
308 void
309 plugin_init(void)
310 {
311 dbg(1, "enter\n");
312 plugin_register_vehicle_type("webos", vehicle_webos_new);
313 }
314

   
Visit the ZANavi Wiki