/[zanavi_public1]/navit/navit/vehicle/maemo/vehicle_maemo.c
ZANavi

Contents of /navit/navit/vehicle/maemo/vehicle_maemo.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: 9704 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
21 /*
22 Plugin for new Maemo's liblocation API.
23
24 <vehicle source="maemo://any" retry_interval="1"/>
25 source cound be on of "any","cwp","acwp","gnss","agnss"
26 retry_interval could be one of "1","2","5","10","20","30","60","120" measured in seconds
27 */
28
29 #include <config.h>
30 #include <string.h>
31 #include <glib.h>
32 #include <math.h>
33 #include <location/location-gps-device.h>
34 #include <location/location-gpsd-control.h>
35 #include "debug.h"
36 #include "callback.h"
37 #include "plugin.h"
38 #include "coord.h"
39 #include "item.h"
40 #include "vehicle.h"
41 #include "event.h"
42
43 static struct vehicle_priv {
44 LocationGPSDControl *control;
45 LocationGPSDevice *device;
46 char *source;
47 guint retry_interval;
48 struct callback_list *cbl;
49 struct attr ** attrs;
50 int sats; // satellites_in_view
51 int sats_used; //satellites_in_user
52 int fix_type; //mode
53 struct coord_geo geo; //lattigute&longittude
54 double speed; //speed:)
55 double direction; //track
56 double height; //altitude
57 double hdop; //eph
58 time_t fix_time; //time
59 char fixiso8601[128];
60 };
61
62
63 static void vehicle_maemo_callback(LocationGPSDevice *device, gpointer user_data) {
64 struct vehicle_priv *priv=(struct vehicle_priv*)user_data;
65
66 priv->sats=device->satellites_in_view;
67 priv->sats_used=device->satellites_in_use;
68 callback_list_call_attr_0(priv->cbl, attr_position_sats);
69
70 dbg(1,"Got update with %u/%u satellites\n",priv->sats_used,priv->sats);
71
72 if (device->fix) {
73 switch(device->fix->mode) {
74 case LOCATION_GPS_DEVICE_MODE_NOT_SEEN:
75 case LOCATION_GPS_DEVICE_MODE_NO_FIX:
76 priv->fix_type=0;
77 break;
78 case LOCATION_GPS_DEVICE_MODE_2D:
79 case LOCATION_GPS_DEVICE_MODE_3D:
80 priv->fix_type=1;
81 break;
82 }
83
84 if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET) {
85 priv->geo.lat=device->fix->latitude;
86 priv->geo.lng=device->fix->longitude;
87 priv->hdop=device->fix->eph/100;
88 callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
89 dbg(1,"Position: %f %f with error %f meters\n",priv->geo.lat,priv->geo.lng,priv->hdop);
90 }
91
92 if (device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET) {
93 priv->speed=device->fix->speed;
94 callback_list_call_attr_0(priv->cbl, attr_position_speed);
95 dbg(1,"Speed: %f\n ",priv->speed);
96 }
97
98 if (device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET) {
99 priv->direction=device->fix->track;
100 dbg(1,"Direction: %f\n",priv->direction);
101 }
102
103 if (device->fix->fields & LOCATION_GPS_DEVICE_TIME_SET) {
104 priv->fix_time=device->fix->time;
105 dbg(1,"Time: %f\n",priv->fix_time);
106 }
107
108 if (device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET) {
109 priv->height=device->fix->altitude;
110 dbg(1,"Elevation: %f\n",priv->height);
111 }
112
113 }
114
115 return;
116 }
117
118 static void vehicle_maemo_error(LocationGPSDControl *control, LocationGPSDControlError error, gpointer user_data)
119 {
120 switch (error) {
121 case LOCATION_ERROR_USER_REJECTED_DIALOG:
122 dbg(0,"User didn't enable requested methods\n");
123 break;
124 case LOCATION_ERROR_USER_REJECTED_SETTINGS:
125 dbg(0,"User changed settings, which disabled location\n");
126 break;
127 case LOCATION_ERROR_BT_GPS_NOT_AVAILABLE:
128 dbg(0,"Problems with BT GPS\n");
129 break;
130 case LOCATION_ERROR_METHOD_NOT_ALLOWED_IN_OFFLINE_MODE:
131 dbg(0,"Requested method is not allowed in offline mode\n");
132 break;
133 case LOCATION_ERROR_SYSTEM:
134 dbg(0,"System error\n");
135 break;
136 }
137 }
138
139 /**
140 * Instantiate liblocation objects
141 */
142 static void
143 vehicle_maemo_open(struct vehicle_priv *priv)
144 {
145
146 priv->control = location_gpsd_control_get_default();
147 priv->device = g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
148
149 if (!strcasecmp(priv->source+8,"cwp")) {
150 g_object_set(G_OBJECT(priv->control), "preferred-method", LOCATION_METHOD_CWP, NULL);
151 dbg(1,"Method set: CWP\n");
152 } else if (!strcasecmp(priv->source+8,"acwp")) {
153 g_object_set(G_OBJECT(priv->control), "preferred-method", LOCATION_METHOD_ACWP, NULL);
154 dbg(1,"Method set: ACWP\n");
155 } else if (!strcasecmp(priv->source+8,"gnss")) {
156 g_object_set(G_OBJECT(priv->control), "preferred-method", LOCATION_METHOD_GNSS, NULL);
157 dbg(1,"Method set: GNSS\n");
158 } else if (!strcasecmp(priv->source+8,"agnss")) {
159 g_object_set(G_OBJECT(priv->control), "preferred-method", LOCATION_METHOD_AGNSS, NULL);
160 dbg(1,"Method set: AGNSS\n");
161 } else {
162 g_object_set(G_OBJECT(priv->control), "preferred-method", LOCATION_METHOD_USER_SELECTED, NULL);
163 dbg(1,"Method set: ANY\n");
164 }
165
166 switch (priv->retry_interval) {
167 case 2:
168 g_object_set(G_OBJECT(priv->control), "preferred-interval", LOCATION_INTERVAL_2S, NULL);
169 dbg(1,"Interval set: 2s\n");
170 break;
171 case 5:
172 g_object_set(G_OBJECT(priv->control), "preferred-interval", LOCATION_INTERVAL_5S, NULL);
173 dbg(1,"Interval set: 5s\n");
174 break;
175 case 10:
176 g_object_set(G_OBJECT(priv->control), "preferred-interval", LOCATION_INTERVAL_10S, NULL);
177 dbg(1,"Interval set: 10s\n");
178 break;
179 case 20:
180 g_object_set(G_OBJECT(priv->control), "preferred-interval", LOCATION_INTERVAL_20S, NULL);
181 dbg(1,"Interval set: 20s\n");
182 break;
183 case 30:
184 g_object_set(G_OBJECT(priv->control), "preferred-interval", LOCATION_INTERVAL_30S, NULL);
185 dbg(1,"Interval set: 30s\n");
186 break;
187 case 60:
188 g_object_set(G_OBJECT(priv->control), "preferred-interval", LOCATION_INTERVAL_60S, NULL);
189 dbg(1,"Interval set: 60s\n");
190 break;
191 case 120:
192 g_object_set(G_OBJECT(priv->control), "preferred-interval", LOCATION_INTERVAL_120S, NULL);
193 dbg(1,"Interval set: 120s\n");
194 break;
195 case 1:
196 default:
197 g_object_set(G_OBJECT(priv->control), "preferred-interval", LOCATION_INTERVAL_1S, NULL);
198 dbg(1,"Interval set: 1s\n");
199 break;
200 }
201
202 g_signal_connect(priv->device, "changed", G_CALLBACK(vehicle_maemo_callback), priv);
203 g_signal_connect(priv->control, "error-verbose", G_CALLBACK(vehicle_maemo_error), priv);
204
205 location_gpsd_control_start(priv->control);
206
207 return;
208 }
209
210 static void
211 vehicle_maemo_destroy(struct vehicle_priv *priv)
212 {
213 location_gpsd_control_stop(priv->control);
214
215 g_object_unref(priv->device);
216 g_object_unref(priv->control);
217
218 return;
219 }
220
221 static int
222 vehicle_maemo_position_attr_get(struct vehicle_priv *priv,
223 enum attr_type type, struct attr *attr)
224 {
225 struct attr * active=NULL;
226 switch (type) {
227 case attr_position_fix_type:
228 dbg(1,"Attr requested: position_fix_type\n");
229 attr->u.num = priv->fix_type;
230 break;
231 case attr_position_height:
232 dbg(1,"Attr requested: position_height\n");
233 attr->u.numd = &priv->height;
234 break;
235 case attr_position_speed:
236 dbg(1,"Attr requested: position_speed\n");
237 attr->u.numd = &priv->speed;
238 break;
239 case attr_position_direction:
240 dbg(1,"Attr requested: position_direction\n");
241 attr->u.numd = &priv->direction;
242 break;
243 case attr_position_hdop:
244 dbg(1,"Attr requested: position_hdop\n");
245 attr->u.numd = &priv->hdop;
246 break;
247 case attr_position_sats:
248 dbg(1,"Attr requested: position_sats_signal\n");
249 attr->u.num = priv->sats;
250 break;
251 case attr_position_sats_used:
252 dbg(1,"Attr requested: position_sats_used\n");
253 attr->u.num = priv->sats_used;
254 break;
255 case attr_position_coord_geo:
256 dbg(1,"Attr requested: position_coord_geo\n");
257 attr->u.coord_geo = &priv->geo;
258 break;
259 case attr_position_time_iso8601:
260 {
261 struct tm tm;
262 dbg(1,"Attr requested: position_time_iso8601\n");
263 if (!priv->fix_time)
264 return 0;
265 if (gmtime_r(&priv->fix_time, &tm)) {
266 strftime(priv->fixiso8601, sizeof(priv->fixiso8601),
267 "%Y-%m-%dT%TZ", &tm);
268 attr->u.str=priv->fixiso8601;
269 } else
270 return 0;
271 }
272 break;
273 case attr_active:
274 dbg(1,"Attr requested: position_active\n");
275 active = attr_search(priv->attrs,NULL,attr_active);
276 if(active != NULL) {
277 attr->u.num=active->u.num;
278 return 1;
279 } else
280 return 0;
281 break;
282 default:
283 return 0;
284 }
285 attr->type = type;
286 return 1;
287 }
288
289 struct vehicle_methods vehicle_maemo_methods = {
290 vehicle_maemo_destroy,
291 vehicle_maemo_position_attr_get,
292 };
293
294 static struct vehicle_priv *
295 vehicle_maemo_new_maemo(struct vehicle_methods
296 *meth, struct callback_list
297 *cbl, struct attr **attrs)
298 {
299 struct vehicle_priv *ret;
300 struct attr *source, *retry_int;
301
302 dbg(1, "enter\n");
303 source = attr_search(attrs, NULL, attr_source);
304 ret = g_new0(struct vehicle_priv, 1);
305 ret->source = g_strdup(source->u.str);
306 retry_int = attr_search(attrs, NULL, attr_retry_interval);
307 if (retry_int) {
308 ret->retry_interval = retry_int->u.num;
309 if (ret->retry_interval !=1 && ret->retry_interval !=2 && ret->retry_interval !=5 && ret->retry_interval !=10 && ret->retry_interval !=20 && ret->retry_interval !=30 && ret->retry_interval !=60 && ret->retry_interval !=120 ) {
310 dbg(0, "Retry interval %d invalid, setting to 1\n", ret->retry_interval,1);
311 ret->retry_interval = 1;
312 }
313 } else {
314 ret->retry_interval = 1;
315 }
316 dbg(1,"source: %s, interval: %u\n",ret->source,ret->retry_interval);
317 ret->cbl = cbl;
318 *meth = vehicle_maemo_methods;
319 ret->attrs = attrs;
320 vehicle_maemo_open(ret);
321 return ret;
322 }
323
324 void
325 plugin_init(void)
326 {
327 dbg(1, "enter\n");
328 plugin_register_vehicle_type("maemo", vehicle_maemo_new_maemo);
329 }

   
Visit the ZANavi Wiki