4 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7 * Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
26 #include "location/location-setting.h"
27 #include "location/location-log.h"
29 #include "location/location-module-internal.h"
31 #include "location/location-gps.h"
32 #include "location/location-marshal.h"
33 #include "location/location-ielement.h"
34 #include "location/location-signaling-util.h"
35 #include "location/location-common-util.h"
38 typedef struct _LocationGpsPrivate {
44 LocationPosition* pos;
45 LocationVelocity* vel;
46 LocationAccuracy* acc;
48 ZoneStatus zone_status;
58 PROP_REMOVAL_BOUNDARY,
64 static guint32 signals[LAST_SIGNAL] = {0, };
65 static GParamSpec *properties[PROP_MAX] = {NULL, };
67 #define GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), LOCATION_TYPE_GPS, LocationGpsPrivate))
69 static void location_ielement_interface_init (LocationIElementInterface *iface);
71 G_DEFINE_TYPE_WITH_CODE (LocationGps, location_gps, G_TYPE_OBJECT,
72 G_IMPLEMENT_INTERFACE (LOCATION_TYPE_IELEMENT,
73 location_ielement_interface_init));
76 gps_status_cb (gboolean enabled,
77 LocationStatus status,
80 LOCATION_LOGD("gps_status_cb");
81 g_return_if_fail(self);
82 LocationGpsPrivate* priv = GET_PRIVATE(self);
83 enable_signaling(self, signals, &(priv->enabled), enabled, status);
87 gps_position_cb (gboolean enabled,
88 LocationPosition *pos,
89 LocationAccuracy *acc,
92 LOCATION_LOGD("gps_position_cb");
93 g_return_if_fail(self);
94 g_return_if_fail(pos);
95 g_return_if_fail(acc);
96 LocationGpsPrivate* priv = GET_PRIVATE(self);
97 enable_signaling(self, signals, &(priv->enabled), enabled, pos->status);
98 position_signaling(self, signals, &(priv->enabled), priv->interval, &(priv->pos), &(priv->acc), priv->boundary_list, &(priv->zone_status), enabled, pos, acc);
102 gps_velocity_cb (gboolean enabled,
103 LocationVelocity *vel,
104 LocationAccuracy *acc,
107 LOCATION_LOGD("gps_velocity_cb");
108 g_return_if_fail(self);
109 LocationGpsPrivate* priv = GET_PRIVATE(self);
110 velocity_signaling(self, signals, &(priv->enabled), priv->interval, &(priv->vel), enabled, vel, acc);
114 location_setting_gps_cb (keynode_t *key,
117 LOCATION_LOGD("location_setting_gps_cb");
118 g_return_if_fail(key);
119 g_return_if_fail(self);
120 LocationGpsPrivate* priv = GET_PRIVATE(self);
121 g_return_if_fail (priv->mod);
122 g_return_if_fail (priv->mod->handler);
124 int ret = LOCATION_ERROR_NONE;
126 if (0 == location_setting_get_key_val(key) && priv->mod->ops.stop && priv->is_started) {
127 LOCATION_LOGD("location stopped by setting");
128 ret = priv->mod->ops.stop(priv->mod->handler);
129 if (ret == LOCATION_ERROR_NONE) priv->is_started = FALSE;
131 else if (1 == location_setting_get_key_val(key) && priv->mod->ops.start && !priv->is_started) {
132 LOCATION_LOGD("location resumed by setting");
133 ret = priv->mod->ops.start (priv->mod->handler, gps_status_cb, gps_position_cb, gps_velocity_cb, self);
134 if (ret == LOCATION_ERROR_NONE) priv->is_started = TRUE;
139 location_gps_start (LocationGps *self)
141 LOCATION_LOGD("location_gps_start");
142 LocationGpsPrivate* priv = GET_PRIVATE(self);
143 g_return_val_if_fail (priv->mod, LOCATION_ERROR_NOT_AVAILABLE);
144 g_return_val_if_fail (priv->mod->handler, LOCATION_ERROR_NOT_AVAILABLE);
145 g_return_val_if_fail (priv->mod->ops.start, LOCATION_ERROR_NOT_AVAILABLE);
147 if (priv->is_started == TRUE) return LOCATION_ERROR_NONE;
149 int ret = LOCATION_ERROR_NONE;
152 if (!location_setting_get_int(GPS_ENABLED)) {
153 ret = LOCATION_ERROR_NOT_ALLOWED;
156 ret = priv->mod->ops.start (priv->mod->handler, gps_status_cb, gps_position_cb, gps_velocity_cb, self);
157 if (ret == LOCATION_ERROR_NONE) {
158 priv->is_started = TRUE;
165 if(priv->set_noti == FALSE) {
166 noti_err = location_setting_add_notify (GPS_ENABLED, location_setting_gps_cb, self);
168 return LOCATION_ERROR_UNKNOWN;
170 priv->set_noti = TRUE;
177 location_gps_stop (LocationGps *self)
179 LOCATION_LOGD("location_gps_stop");
180 LocationGpsPrivate* priv = GET_PRIVATE(self);
181 g_return_val_if_fail (priv->mod, LOCATION_ERROR_NOT_AVAILABLE);
182 g_return_val_if_fail (priv->mod->handler, LOCATION_ERROR_NOT_AVAILABLE);
183 g_return_val_if_fail (priv->mod->ops.stop, LOCATION_ERROR_NOT_AVAILABLE);
185 int ret = LOCATION_ERROR_NONE;
188 if ( priv->is_started == TRUE) {
189 ret = priv->mod->ops.stop (priv->mod->handler);
190 if (ret == LOCATION_ERROR_NONE) {
191 priv->is_started = FALSE;
198 if(priv->set_noti == TRUE) {
199 noti_err = location_setting_ignore_notify (GPS_ENABLED, location_setting_gps_cb);
201 return LOCATION_ERROR_UNKNOWN;
203 priv->set_noti = FALSE;
210 location_gps_dispose (GObject *gobject)
212 LOCATION_LOGD("location_gps_dispose");
213 G_OBJECT_CLASS (location_gps_parent_class)->dispose (gobject);
217 location_gps_finalize (GObject *gobject)
219 LOCATION_LOGD("location_gps_finalize");
220 LocationGpsPrivate* priv = GET_PRIVATE(gobject);
221 module_free(priv->mod, "gps");
223 G_OBJECT_CLASS (location_gps_parent_class)->finalize (gobject);
227 location_gps_set_property (GObject *object,
232 LocationGpsPrivate* priv = GET_PRIVATE(object);
234 g_return_if_fail (priv->mod);
235 g_return_if_fail (priv->mod->handler);
236 LocModGpsOps ops = priv->mod->ops;
239 switch (property_id){
240 case PROP_DEV_NAME: {
241 char* devname = g_value_dup_string(value);
242 LOCATION_LOGD("Set prop>> device_name: %s", devname);
244 ops.set_devname(priv->mod->handler, devname);
248 case PROP_BOUNDARY: {
249 GList *boundary_list = g_list_copy(g_value_get_pointer(value));
250 ret = set_prop_boundary(&priv->boundary_list, boundary_list);
251 if(ret != 0) LOCATION_LOGD("Set boundary. Error[%d]", ret);
254 case PROP_REMOVAL_BOUNDARY: {
255 LocationBoundary *req_boundary = (LocationBoundary*) g_value_dup_boxed(value);
256 ret = set_prop_removal_boundary(&priv->boundary_list, req_boundary);
257 if(ret != 0) LOCATION_LOGD("Removal boundary. Error[%d]", ret);
260 case PROP_UPDATE_INTERVAL: {
261 guint interval = g_value_get_uint(value);
262 LOCATION_LOGD("Set prop>> update-interval: %u", interval);
264 if(interval < LOCATION_UPDATE_INTERVAL_MAX)
265 priv->interval = interval;
267 priv->interval = (guint)LOCATION_UPDATE_INTERVAL_MAX;
270 priv->interval = (guint)LOCATION_UPDATE_INTERVAL_DEFAULT;
274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
280 location_gps_get_property (GObject *object,
285 LocationGpsPrivate *priv = GET_PRIVATE (object);
287 g_return_if_fail (priv->mod);
288 g_return_if_fail (priv->mod->handler);
289 LocModGpsOps ops = priv->mod->ops;
290 switch (property_id) {
291 case PROP_DEV_NAME: {
292 char* devname = NULL;
294 ops.get_devname(priv->mod->handler, &devname);
295 LOCATION_LOGD ("Get prop>> device_name: %s", devname);
296 g_value_set_string (value, devname);
300 case PROP_METHOD_TYPE:
301 g_value_set_int(value, LOCATION_METHOD_GPS);
303 case PROP_LAST_POSITION:
304 g_value_set_boxed (value, priv->pos);
306 case PROP_UPDATE_INTERVAL:
307 g_value_set_uint(value, priv->interval);
310 g_value_set_pointer(value, g_list_first(priv->boundary_list));
313 char *nmea_data = NULL;
314 if (ops.get_nmea && LOCATION_ERROR_NONE == ops.get_nmea(priv->mod->handler, &nmea_data) && nmea_data) {
315 LOCATION_LOGD("Get prop>> Lastest nmea: \n%s", nmea_data);
316 g_value_set_string(value, nmea_data);
319 LOCATION_LOGW("Get prop>> Lastest nmea: failed");
320 g_value_set_string(value, NULL);
324 case PROP_SATELLITE: {
325 LocationSatellite *satellite = NULL;
326 if (ops.get_satellite && priv->mod->handler && LOCATION_ERROR_NONE == ops.get_satellite(priv->mod->handler, &satellite) && satellite){
327 LOCATION_LOGD("Get prop>> Last sat: num_used(%d) num_view(%d)", satellite->num_of_sat_used, satellite->num_of_sat_inview);
328 g_value_set_boxed (value, satellite);
329 location_satellite_free(satellite);
331 LOCATION_LOGW("Get prop>> Last sat: failed");
332 g_value_set_boxed (value, NULL);
337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
343 location_gps_get_position (LocationGps *self,
344 LocationPosition **position,
345 LocationAccuracy **accuracy)
347 LOCATION_LOGD("location_gps_get_position");
349 LocationGpsPrivate *priv = GET_PRIVATE (self);
350 g_return_val_if_fail (priv->mod, LOCATION_ERROR_NOT_AVAILABLE);
351 setting_retval_if_fail(GPS_ENABLED);
353 LocModGpsOps ops = priv->mod->ops;
354 g_return_val_if_fail (priv->mod->handler, LOCATION_ERROR_NOT_AVAILABLE);
355 g_return_val_if_fail (ops.get_position, LOCATION_ERROR_NOT_AVAILABLE);
356 return ops.get_position(priv->mod->handler, position, accuracy);
360 location_gps_get_velocity (LocationGps *self,
361 LocationVelocity **velocity,
362 LocationAccuracy **accuracy)
364 LOCATION_LOGD("location_gps_get_velocity");
366 LocationGpsPrivate *priv = GET_PRIVATE (self);
367 g_return_val_if_fail (priv->mod, LOCATION_ERROR_NOT_AVAILABLE);
368 setting_retval_if_fail(GPS_ENABLED);
370 LocModGpsOps ops = priv->mod->ops;
371 g_return_val_if_fail (priv->mod->handler, LOCATION_ERROR_NOT_AVAILABLE);
372 g_return_val_if_fail (ops.get_velocity, LOCATION_ERROR_NOT_AVAILABLE);
373 return ops.get_velocity(priv->mod->handler, velocity, accuracy);
377 location_ielement_interface_init (LocationIElementInterface *iface)
379 iface->start = (TYPE_START_FUNC)location_gps_start;
380 iface->stop = (TYPE_STOP_FUNC)location_gps_stop;
381 iface->get_position = (TYPE_GET_POSITION)location_gps_get_position;
382 iface->get_velocity = (TYPE_GET_VELOCITY)location_gps_get_velocity;
386 location_gps_init (LocationGps *self)
388 LOCATION_LOGD("location_gps_init");
389 LocationGpsPrivate* priv = GET_PRIVATE(self);
391 priv->mod = (LocationGpsMod*)module_new("gps");
392 if(!priv->mod) LOCATION_LOGW("module loading failed");
394 priv->is_started = FALSE;
395 priv->set_noti = FALSE;
396 priv->enabled= FALSE;
397 priv->interval = LOCATION_UPDATE_INTERVAL_DEFAULT;
402 priv->zone_status = ZONE_STATUS_NONE;
403 priv->boundary_list = NULL;
407 location_gps_class_init (LocationGpsClass *klass)
409 LOCATION_LOGD("location_gps_class_init");
410 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
412 gobject_class->set_property = location_gps_set_property;
413 gobject_class->get_property = location_gps_get_property;
415 gobject_class->dispose = location_gps_dispose;
416 gobject_class->finalize = location_gps_finalize;
418 g_type_class_add_private (klass, sizeof (LocationGpsPrivate));
420 signals[SERVICE_ENABLED] = g_signal_new ("service-enabled",
421 G_TYPE_FROM_CLASS (klass),
424 G_STRUCT_OFFSET (LocationGpsClass, enabled),
430 signals[SERVICE_DISABLED] = g_signal_new ("service-disabled",
431 G_TYPE_FROM_CLASS (klass),
434 G_STRUCT_OFFSET (LocationGpsClass, disabled),
440 signals[SERVICE_UPDATED] = g_signal_new ("service-updated",
441 G_TYPE_FROM_CLASS (klass),
444 G_STRUCT_OFFSET (LocationGpsClass, updated),
446 location_VOID__UINT_POINTER_POINTER,
452 signals[ZONE_IN] = g_signal_new ("zone-in",
453 G_TYPE_FROM_CLASS (klass),
456 G_STRUCT_OFFSET (LocationGpsClass, zone_in),
458 location_VOID__UINT_POINTER_POINTER,
464 signals[ZONE_OUT] = g_signal_new ("zone-out",
465 G_TYPE_FROM_CLASS (klass),
468 G_STRUCT_OFFSET (LocationGpsClass, zone_out),
470 location_VOID__UINT_POINTER_POINTER,
476 properties[PROP_DEV_NAME] = g_param_spec_string ("dev-name",
477 "gps device name prop",
482 properties[PROP_METHOD_TYPE] = g_param_spec_int ("method",
484 "location method type name",
490 properties[PROP_LAST_POSITION] = g_param_spec_boxed ("last-position",
491 "gps last position prop",
492 "gps last position data",
493 LOCATION_TYPE_POSITION,
496 properties[PROP_UPDATE_INTERVAL] = g_param_spec_uint ("update-interval",
497 "gps update interval prop",
498 "gps update interval data",
499 LOCATION_UPDATE_INTERVAL_MIN,
500 LOCATION_UPDATE_INTERVAL_MAX,
501 LOCATION_UPDATE_INTERVAL_DEFAULT,
504 properties[PROP_BOUNDARY] = g_param_spec_pointer ("boundary",
509 properties[PROP_REMOVAL_BOUNDARY] = g_param_spec_boxed("removal-boundary",
510 "gps removal boundary prop",
511 "gps removal boundary data",
512 LOCATION_TYPE_BOUNDARY,
516 properties[PROP_NMEA] = g_param_spec_string ("nmea",
517 "gps NMEA name prop",
522 properties[PROP_SATELLITE] = g_param_spec_boxed ("satellite",
523 "gps satellite prop",
524 "gps satellite data",
525 LOCATION_TYPE_SATELLITE,
528 g_object_class_install_properties (gobject_class,