tizen beta release
[framework/location/libslp-location.git] / location / location-wps.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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>
8  *
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "location/location-setting.h"
27 #include "location/location-log.h"
28
29 #include "location/location-module-internal.h"
30
31 #include "location/location-wps.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"
36 /*
37  * forward definitions
38  */
39
40 typedef struct _LocationWpsPrivate {
41         LocationWpsMod* mod;
42         gboolean is_started;
43         gboolean set_noti;
44         gboolean enabled;
45         guint    interval;
46         LocationPosition *pos;
47         LocationVelocity *vel;
48         LocationAccuracy *acc;
49         GList *boundary_list;
50         ZoneStatus zone_status;
51 } LocationWpsPrivate;
52
53 enum {
54         PROP_0,
55         PROP_METHOD_TYPE,
56         PROP_LAST_POSITION,
57         PROP_UPDATE_INTERVAL,
58         PROP_BOUNDARY,
59         PROP_REMOVAL_BOUNDARY,
60         PROP_MAX
61 };
62
63 static guint32 signals[LAST_SIGNAL] = {0, };
64 static GParamSpec *properties[PROP_MAX] = {NULL, };
65
66 #define GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), LOCATION_TYPE_WPS, LocationWpsPrivate))
67
68 static void location_ielement_interface_init (LocationIElementInterface *iface);
69
70 G_DEFINE_TYPE_WITH_CODE (LocationWps, location_wps, G_TYPE_OBJECT,
71                          G_IMPLEMENT_INTERFACE (LOCATION_TYPE_IELEMENT,
72                          location_ielement_interface_init));
73
74 static void
75 wps_status_cb (gboolean enabled,
76         LocationStatus status,
77         gpointer self)
78 {
79         LOCATION_LOGD("wps_status_cb");
80         g_return_if_fail(self);
81         LocationWpsPrivate* priv = GET_PRIVATE(self);
82         enable_signaling(self, signals, &(priv->enabled), enabled, status);
83 }
84
85 static void
86 wps_position_cb (gboolean enabled,
87         LocationPosition *pos,
88         LocationAccuracy *acc,
89         gpointer self)
90 {
91         LOCATION_LOGD("wps_position_cb");
92         g_return_if_fail(self);
93         g_return_if_fail(pos);
94         g_return_if_fail(acc);
95         LocationWpsPrivate* priv = GET_PRIVATE(self);
96         enable_signaling(self, signals, &(priv->enabled), enabled, pos->status);
97         position_signaling(self, signals, &(priv->enabled), priv->interval, &(priv->pos), &(priv->acc), priv->boundary_list, &(priv->zone_status), enabled, pos, acc);
98 }
99
100 static void
101 wps_velocity_cb(gboolean enabled,
102         LocationVelocity *vel,
103         LocationAccuracy *acc,
104         gpointer self)
105 {
106         LOCATION_LOGD("wps_velocity_cb");
107         g_return_if_fail(self);
108         LocationWpsPrivate* priv = GET_PRIVATE(self);
109         velocity_signaling(self, signals, &(priv->enabled), priv->interval, &(priv->vel), enabled, vel, acc);
110 }
111
112 static void
113 location_setting_wps_cb(keynode_t *key,
114         gpointer self)
115 {
116         LOCATION_LOGD("location_setting_wps_cb");
117         g_return_if_fail(key);
118         g_return_if_fail(self);
119         LocationWpsPrivate* priv = GET_PRIVATE(self);
120         g_return_if_fail (priv->mod);
121         g_return_if_fail (priv->mod->handler);
122
123         int ret = LOCATION_ERROR_NONE;
124
125         if (location_setting_get_key_val(key) == 0) {
126                 if (priv->mod->ops.stop && priv->is_started) {
127                         ret = priv->mod->ops.stop(priv->mod->handler);
128                         if (ret == LOCATION_ERROR_NONE) priv->is_started = FALSE;
129                 }
130         }
131         else {
132                 if (1 == location_setting_get_int(NETWORK_ENABLED) && priv->mod->ops.start && !priv->is_started) {
133                         LOCATION_LOGD("location resumed by setting");
134                         ret = priv->mod->ops.start (priv->mod->handler, wps_status_cb, wps_position_cb, wps_velocity_cb, self);
135                         if (ret == LOCATION_ERROR_NONE) priv->is_started = TRUE;
136                 }
137         }
138
139 }
140
141 static int
142 location_wps_start (LocationWps *self)
143 {
144         LOCATION_LOGD("location_wps_start");
145         LocationWpsPrivate* priv = GET_PRIVATE(self);
146         g_return_val_if_fail (priv->mod, LOCATION_ERROR_NOT_AVAILABLE);
147         g_return_val_if_fail (priv->mod->handler, LOCATION_ERROR_NOT_AVAILABLE);
148         g_return_val_if_fail (priv->mod->ops.start, LOCATION_ERROR_NOT_AVAILABLE);
149
150         if (priv->is_started == TRUE) return LOCATION_ERROR_NONE;
151
152         int ret = LOCATION_ERROR_NONE;
153         int noti_err = 0;
154
155         if (!location_setting_get_int(GPS_ENABLED) || !location_setting_get_int(NETWORK_ENABLED)) {
156                 ret = LOCATION_ERROR_NOT_ALLOWED;
157         }
158         else {
159                 ret = priv->mod->ops.start (priv->mod->handler, wps_status_cb, wps_position_cb, wps_velocity_cb, self);
160                 if (ret == LOCATION_ERROR_NONE) {
161                         priv->is_started = TRUE;
162                 }
163                 else {
164                         return ret;
165                 }
166         }
167
168         if (priv->set_noti == FALSE) {
169                 noti_err = location_setting_add_notify (GPS_ENABLED, location_setting_wps_cb, self);
170                 if (noti_err != 0) {
171                         return LOCATION_ERROR_UNKNOWN;
172                 }
173                 noti_err = location_setting_add_notify (NETWORK_ENABLED, location_setting_wps_cb, self);
174                 if (noti_err != 0) {
175                         return LOCATION_ERROR_UNKNOWN;
176                 }
177                 priv->set_noti = TRUE;
178         }
179
180         return ret;
181 }
182
183 static int
184 location_wps_stop (LocationWps *self)
185 {
186         LOCATION_LOGD("location_wps_stop");
187         LocationWpsPrivate* priv = GET_PRIVATE(self);
188         g_return_val_if_fail (priv->mod, LOCATION_ERROR_NOT_AVAILABLE);
189         g_return_val_if_fail (priv->mod->handler, LOCATION_ERROR_NOT_AVAILABLE);
190         g_return_val_if_fail (priv->mod->ops.stop, LOCATION_ERROR_NOT_AVAILABLE);
191
192         int ret = LOCATION_ERROR_NONE;
193         int noti_err = 0;
194
195         if (priv->is_started == TRUE) {
196                 ret = priv->mod->ops.stop (priv->mod->handler);
197                 if (ret == LOCATION_ERROR_NONE) {
198                         priv->is_started = FALSE;
199                 }
200                 else {
201                         return ret;
202                 }
203         }
204
205         if (priv->set_noti == TRUE) {
206                 noti_err = location_setting_ignore_notify (GPS_ENABLED, location_setting_wps_cb);
207                 if (noti_err != 0) {
208                         return LOCATION_ERROR_UNKNOWN;
209                 }
210                 noti_err = location_setting_ignore_notify (NETWORK_ENABLED, location_setting_wps_cb);
211                 if (noti_err != 0) {
212                         return LOCATION_ERROR_UNKNOWN;
213                 }
214                 priv->set_noti = FALSE;
215         }
216
217         return ret;
218 }
219
220 static void
221 location_wps_dispose (GObject *gobject)
222 {
223         LOCATION_LOGD("location_wps_dispose");
224         G_OBJECT_CLASS (location_wps_parent_class)->dispose (gobject);
225 }
226
227 static void
228 location_wps_finalize (GObject *gobject)
229 {
230         LOCATION_LOGD("location_wps_finalize");
231         LocationWpsPrivate* priv = GET_PRIVATE(gobject);
232         module_free(priv->mod, "wps");
233         G_OBJECT_CLASS (location_wps_parent_class)->finalize (gobject);
234 }
235
236 static void
237 location_wps_set_property (GObject *object,
238         guint property_id,
239         const GValue *value,
240         GParamSpec *pspec)
241 {
242         LocationWpsPrivate* priv = GET_PRIVATE(object);
243         int ret = 0;
244
245         switch (property_id){
246                 case PROP_BOUNDARY:{
247                         GList *boundary_list = (GList *)g_list_copy(g_value_get_pointer(value));
248                         ret = set_prop_boundary(&priv->boundary_list, boundary_list);
249                         if(ret != 0) LOCATION_LOGD("Set boundary. Error[%d]", ret);
250                         break;
251                 }
252                 case PROP_REMOVAL_BOUNDARY: {
253                         LocationBoundary *req_boundary = (LocationBoundary*) g_value_dup_boxed(value);
254                         ret = set_prop_removal_boundary(&priv->boundary_list, req_boundary);
255                         if(ret != 0) LOCATION_LOGD("Set removal boundary. Error[%d]", ret);
256                         break;
257                 }
258                 case PROP_UPDATE_INTERVAL: {
259                         guint interval = g_value_get_uint(value);
260                         if(interval > 0) {
261                                 if(interval < LOCATION_UPDATE_INTERVAL_MAX)
262                                         priv->interval = interval;
263                                 else
264                                         priv->interval = (guint)LOCATION_UPDATE_INTERVAL_MAX;
265                         }
266                         else
267                                 priv->interval = (guint)LOCATION_UPDATE_INTERVAL_DEFAULT;
268                         break;
269                 }
270                 default:
271                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
272                         break;
273         }
274 }
275
276 static void
277 location_wps_get_property (GObject *object,
278         guint property_id,
279         GValue *value,
280         GParamSpec *pspec)
281 {
282         LocationWpsPrivate *priv = GET_PRIVATE (object);
283
284         switch (property_id){
285                 case PROP_METHOD_TYPE:
286                         g_value_set_int(value, LOCATION_METHOD_WPS);
287                         break;
288                 case PROP_LAST_POSITION:
289                         g_value_set_boxed (value, priv->pos);
290                         break;
291                 case PROP_BOUNDARY:
292                         g_value_set_pointer(value, g_list_first(priv->boundary_list));
293                         break;
294                 case PROP_UPDATE_INTERVAL:
295                         g_value_set_uint(value, priv->interval);
296                         break;
297                 default:
298                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
299                         break;
300         }
301 }
302
303 static int
304 location_wps_get_position (LocationWps *self,
305         LocationPosition **position,
306         LocationAccuracy **accuracy)
307 {
308         LOCATION_LOGD("location_wps_get_position");
309
310         LocationWpsPrivate *priv = GET_PRIVATE (self);
311         g_return_val_if_fail (priv->mod, LOCATION_ERROR_NOT_AVAILABLE);
312         setting_retval_if_fail(GPS_ENABLED);
313         setting_retval_if_fail(NETWORK_ENABLED);
314
315         LocModWpsOps ops = priv->mod->ops;
316         g_return_val_if_fail (priv->mod->handler, LOCATION_ERROR_NOT_AVAILABLE);
317         g_return_val_if_fail (ops.get_position, LOCATION_ERROR_NOT_AVAILABLE);
318         return ops.get_position(priv->mod->handler, position, accuracy);
319 }
320
321 static int
322 location_wps_get_velocity (LocationWps *self,
323         LocationVelocity **velocity,
324         LocationAccuracy **accuracy)
325 {
326         LOCATION_LOGD("location_wps_get_velocity");
327
328         LocationWpsPrivate *priv = GET_PRIVATE (self);
329         g_return_val_if_fail (priv->mod, LOCATION_ERROR_NOT_AVAILABLE);
330         setting_retval_if_fail(GPS_ENABLED);
331         setting_retval_if_fail(NETWORK_ENABLED);
332
333         LocModWpsOps ops = priv->mod->ops;
334         g_return_val_if_fail (priv->mod->handler, LOCATION_ERROR_NOT_AVAILABLE);
335         g_return_val_if_fail (ops.get_velocity, LOCATION_ERROR_NOT_AVAILABLE);
336         return ops.get_velocity(priv->mod->handler, velocity, accuracy);
337 }
338
339 static void
340 location_ielement_interface_init (LocationIElementInterface *iface)
341 {
342         iface->start = (TYPE_START_FUNC)location_wps_start;
343         iface->stop = (TYPE_STOP_FUNC)location_wps_stop;
344         iface->get_position = (TYPE_GET_POSITION)location_wps_get_position;
345         iface->get_velocity = (TYPE_GET_VELOCITY)location_wps_get_velocity;
346 }
347
348 static void
349 location_wps_init (LocationWps *self)
350 {
351         LOCATION_LOGD("location_wps_init");
352         LocationWpsPrivate* priv = GET_PRIVATE(self);
353
354         priv->mod = (LocationWpsMod*)module_new("wps");
355         if(!priv->mod) LOCATION_LOGW("module loading failed");
356
357         priv->is_started = FALSE;
358         priv->set_noti = FALSE;
359         priv->enabled= FALSE;
360         priv->interval = LOCATION_UPDATE_INTERVAL_DEFAULT;
361
362         priv->pos = NULL;
363         priv->vel = NULL;
364         priv->acc = NULL;
365         priv->zone_status = ZONE_STATUS_NONE;
366         priv->boundary_list = NULL;
367 }
368
369 static void
370 location_wps_class_init (LocationWpsClass *klass)
371 {
372         LOCATION_LOGD("location_wps_class_init");
373         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
374
375         gobject_class->set_property = location_wps_set_property;
376         gobject_class->get_property = location_wps_get_property;
377
378         gobject_class->dispose = location_wps_dispose;
379         gobject_class->finalize = location_wps_finalize;
380
381         g_type_class_add_private (klass, sizeof (LocationWpsPrivate));
382
383         signals[SERVICE_ENABLED] = g_signal_new ("service-enabled",
384                         G_TYPE_FROM_CLASS (klass),
385                         G_SIGNAL_RUN_FIRST |
386                         G_SIGNAL_NO_RECURSE,
387                         G_STRUCT_OFFSET (LocationWpsClass, enabled),
388                         NULL, NULL,
389                         location_VOID__UINT,
390                         G_TYPE_NONE, 1,
391                         G_TYPE_UINT);
392
393         signals[SERVICE_DISABLED] = g_signal_new ("service-disabled",
394                         G_TYPE_FROM_CLASS (klass),
395                         G_SIGNAL_RUN_FIRST |
396                         G_SIGNAL_NO_RECURSE,
397                         G_STRUCT_OFFSET (LocationWpsClass, disabled),
398                         NULL, NULL,
399                         location_VOID__UINT,
400                         G_TYPE_NONE, 1,
401                         G_TYPE_UINT);
402
403         signals[SERVICE_UPDATED] = g_signal_new ("service-updated",
404                         G_TYPE_FROM_CLASS (klass),
405                         G_SIGNAL_RUN_FIRST |
406                         G_SIGNAL_NO_RECURSE,
407                         G_STRUCT_OFFSET (LocationWpsClass, updated),
408                         NULL, NULL,
409                         location_VOID__UINT_POINTER_POINTER,
410                         G_TYPE_NONE, 3,
411                         G_TYPE_UINT,
412                         G_TYPE_POINTER,
413                         G_TYPE_POINTER);
414
415         signals[ZONE_IN] = g_signal_new ("zone-in",
416                         G_TYPE_FROM_CLASS (klass),
417                         G_SIGNAL_RUN_FIRST |
418                         G_SIGNAL_NO_RECURSE,
419                         G_STRUCT_OFFSET (LocationWpsClass, zone_in),
420                         NULL, NULL,
421                         location_VOID__UINT_POINTER_POINTER,
422                         G_TYPE_NONE, 3,
423                         G_TYPE_UINT,
424                         G_TYPE_POINTER,
425                         G_TYPE_POINTER);
426
427         signals[ZONE_OUT] = g_signal_new ("zone-out",
428                         G_TYPE_FROM_CLASS (klass),
429                         G_SIGNAL_RUN_FIRST |
430                         G_SIGNAL_NO_RECURSE,
431                         G_STRUCT_OFFSET (LocationWpsClass, zone_out),
432                         NULL, NULL,
433                         location_VOID__UINT_POINTER_POINTER,
434                         G_TYPE_NONE, 3,
435                         G_TYPE_UINT,
436                         G_TYPE_POINTER,
437                         G_TYPE_POINTER);
438
439         properties[PROP_METHOD_TYPE] = g_param_spec_int ("method",
440                         "method type",
441                         "location method type name",
442                         LOCATION_METHOD_WPS,
443                         LOCATION_METHOD_WPS,
444                         LOCATION_METHOD_WPS,
445                         G_PARAM_READABLE);
446
447         properties[PROP_LAST_POSITION] = g_param_spec_boxed ("last-position",
448                         "wps last position prop",
449                         "wps last position data",
450                         LOCATION_TYPE_POSITION,
451                         G_PARAM_READABLE);
452
453         properties[PROP_UPDATE_INTERVAL] = g_param_spec_uint ("update-interval",
454                         "wps update interval prop",
455                         "wps update interval data",
456                         LOCATION_UPDATE_INTERVAL_MIN,
457                         LOCATION_UPDATE_INTERVAL_MAX,
458                         LOCATION_UPDATE_INTERVAL_DEFAULT,
459                         G_PARAM_READWRITE);
460
461         properties[PROP_BOUNDARY] = g_param_spec_pointer ("boundary",
462                         "wps boundary prop",
463                         "wps boundary data",
464                         G_PARAM_READWRITE);
465
466         properties[PROP_REMOVAL_BOUNDARY] = g_param_spec_boxed("removal-boundary",
467                         "wps removal boundary prop",
468                         "wps removal boundary data",
469                         LOCATION_TYPE_BOUNDARY,
470                         G_PARAM_READWRITE);
471
472         g_object_class_install_properties (gobject_class,
473                         PROP_MAX,
474                         properties);
475 }
476