Dynamic interval table for multi handle
[platform/core/location/lbs-location.git] / location / manager / location.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
7  *                      Genie Kim <daejins.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 <glib.h>
27 #include <stdio.h>
28 #include <vconf.h>
29
30 #include "location.h"
31 #include "location-log.h"
32 #include "location-setting.h"
33 #include "location-ielement.h"
34 #include "location-hybrid.h"
35 #include "location-gps.h"
36 #include "location-wps.h"
37 #include "location-passive.h"
38 #include "location-position.h"
39 #include "module-internal.h"
40 #include "location-common-util.h"
41 #ifndef TIZEN_PROFILE_TV
42 #include "location-privacy.h"
43 #endif
44
45 #define LOCATION_PRIVILEGE                      "http://tizen.org/privilege/location"
46 #define LOCATION_ENABLE_PRIVILEGE       "http://tizen.org/privilege/location.enable"
47 #define HYBRID  2
48
49 typedef struct _LocationSetting {
50         LocationSettingCb callback;
51         void *user_data;
52 } LocationSetting;
53
54 enum {
55         _METHOD_HYBRID = 0,
56         _METHOD_GPS_1,
57         _METHOD_GPS_2,
58         _METHOD_WPS_1,
59         _METHOD_WPS_2,
60         _METHOD_MAX
61 };
62
63 static LocationSetting g_location_setting;
64 static bool is_method_created[_METHOD_MAX] = {false};
65
66 static char *__convert_setting_key(LocationMethod method)
67 {
68         char *key = NULL;
69         switch (method) {
70         case LOCATION_METHOD_HYBRID:
71                 key = g_strdup(VCONFKEY_LOCATION_USE_MY_LOCATION);
72                 break;
73         case LOCATION_METHOD_GPS:
74                 key = g_strdup(VCONFKEY_LOCATION_ENABLED);
75                 break;
76         case LOCATION_METHOD_WPS:
77                 key = g_strdup(VCONFKEY_LOCATION_NETWORK_ENABLED);
78                 break;
79         case INTERNAL_METHOD_MOCK:
80                 key = g_strdup(VCONFKEY_LOCATION_MOCK_ENABLED);
81                 break;
82         default:
83                 break;
84         }
85
86         return key;
87 }
88
89 static LocationMethod __convert_method_from_key(const char *key)
90 {
91         LocationMethod _method = LOCATION_METHOD_NONE;
92         if (g_strcmp0(key, VCONFKEY_LOCATION_USE_MY_LOCATION) == 0)
93                 _method = LOCATION_METHOD_HYBRID;
94         else if (g_strcmp0(key, VCONFKEY_LOCATION_ENABLED) == 0)
95                 _method = LOCATION_METHOD_GPS;
96         else if (g_strcmp0(key, VCONFKEY_LOCATION_NETWORK_ENABLED) == 0)
97                 _method = LOCATION_METHOD_WPS;
98
99         return _method;
100 }
101
102 static void __location_setting_cb(keynode_t *key, gpointer data)
103 {
104         if (key == NULL || data == NULL)
105                 return;
106
107         LocationSetting *_setting = (LocationSetting *)data;
108         LocationMethod _method = LOCATION_METHOD_NONE;
109
110         if (_setting->callback) {
111                 _method = __convert_method_from_key(vconf_keynode_get_name(key));
112                 _setting->callback(_method, location_setting_get_key_val(key), _setting->user_data);
113         }
114 }
115
116 EXPORT_API
117 int location_init(void)
118 {
119 #if !GLIB_CHECK_VERSION(2, 35, 0)
120         g_type_init();
121 #endif
122
123 #if !GLIB_CHECK_VERSION(2, 31, 0)
124         if (!g_thread_supported()) g_thread_init(NULL);
125 #endif
126         if (FALSE == module_init())
127                 return LOCATION_ERROR_NOT_AVAILABLE;
128
129         return LOCATION_ERROR_NONE;
130 }
131
132 EXPORT_API LocationObject *
133 location_new(LocationMethod method, gboolean is_internal_for_hybrid)
134 {
135         LocationObject *self = NULL;
136         LOCATION_LOGD("method : %d, is_internal_for_hybrid[%d]", method, is_internal_for_hybrid);
137
138         switch (method) {
139         case LOCATION_METHOD_HYBRID:
140                 if (is_method_created[_METHOD_HYBRID]) {
141                         LOCATION_LOGE("Hybrid handle already exist.");
142                 } else {
143                         self = g_object_new(LOCATION_TYPE_HYBRID, NULL);
144                         is_method_created[_METHOD_HYBRID] = true;
145                 }
146                 break;
147         case LOCATION_METHOD_GPS:
148                 if (is_internal_for_hybrid) {
149                         if (is_method_created[_METHOD_GPS_2]) {
150                                 LOCATION_LOGD("Hybrid(GPS) handle already exist.");
151                         } else {
152                                 is_method_created[_METHOD_GPS_2] = true;
153                                 self = g_object_new(LOCATION_TYPE_GPS, NULL);
154                         }
155                 } else {
156                         if (is_method_created[_METHOD_GPS_1]) {
157                                 LOCATION_LOGE("GPS handle already exist.");
158                         } else {
159                                 is_method_created[_METHOD_GPS_1] = true;
160                                 self = g_object_new(LOCATION_TYPE_GPS, NULL);
161                         }
162                 }
163                 break;
164         case LOCATION_METHOD_WPS:
165                 if (is_internal_for_hybrid) {
166                         if (is_method_created[_METHOD_WPS_2]) {
167                                 LOCATION_LOGD("Hybrid(WPS) handle already exist.");
168                         } else {
169                                 is_method_created[_METHOD_WPS_2] = true;
170                                 self = g_object_new(LOCATION_TYPE_WPS, NULL);
171                         }
172                 } else {
173                         if (is_method_created[_METHOD_WPS_1]) {
174                                 LOCATION_LOGE("WPS handle already exist.");
175                         } else {
176                                 is_method_created[_METHOD_WPS_1] = true;
177                                 self = g_object_new(LOCATION_TYPE_WPS, NULL);
178                         }
179                 }
180                 break;
181         case LOCATION_METHOD_PASSIVE:
182                 self = g_object_new(LOCATION_TYPE_PASSIVE, NULL);
183                 break;
184         default:
185                 break;
186         }
187
188         LOC_COND_LOG(!self, _E, "Fail to create location object. [method=%d]", method);
189         return self;
190 }
191
192 EXPORT_API int
193 location_free(LocationObject *obj, gboolean is_internal_for_hybrid)
194 {
195         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
196         if (G_OBJECT_TYPE(obj) == LOCATION_TYPE_HYBRID) {
197                 is_method_created[_METHOD_HYBRID] = false;
198         } else if (G_OBJECT_TYPE(obj) == LOCATION_TYPE_GPS) {
199                 if (is_internal_for_hybrid)
200                         is_method_created[_METHOD_GPS_2] = false;
201                 else
202                         is_method_created[_METHOD_GPS_1] = false;
203         } else if (G_OBJECT_TYPE(obj) == LOCATION_TYPE_WPS) {
204                 if (is_internal_for_hybrid)
205                         is_method_created[_METHOD_WPS_2] = false;
206                 else
207                         is_method_created[_METHOD_WPS_1] = false;
208         }
209         g_object_unref(obj);
210         return LOCATION_ERROR_NONE;
211 }
212
213 EXPORT_API int
214 location_request_single_location(LocationObject *obj, int timeout)
215 {
216         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
217
218         int ret = LOCATION_ERROR_NONE;
219         ret = location_ielement_request_single_location(LOCATION_IELEMENT(obj), timeout);
220         LOC_IF_FAIL(ret, _E, "Fail to request single location [%s]", err_msg(ret));
221
222         return ret;
223 }
224
225 EXPORT_API int
226 location_start(LocationObject *obj)
227 {
228         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
229
230         int ret = LOCATION_ERROR_NONE;
231 #ifndef TIZEN_PROFILE_TV
232         ret = location_check_cynara(LOCATION_PRIVILEGE);
233         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
234 #endif
235
236         ret = location_ielement_start(LOCATION_IELEMENT(obj));
237         LOC_IF_FAIL(ret, _E, "Fail to start [%s]", err_msg(ret));
238
239         return ret;
240 }
241
242 EXPORT_API int
243 location_stop(LocationObject *obj)
244 {
245         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
246
247         int ret = LOCATION_ERROR_NONE;
248         ret = location_ielement_stop(LOCATION_IELEMENT(obj));
249         LOC_IF_FAIL(ret, _E, "Fail to stop [%s]", err_msg(ret));
250
251         return ret;
252 }
253
254 EXPORT_API int
255 location_start_batch(LocationObject *obj)
256 {
257         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
258         int ret = LOCATION_ERROR_NONE;
259
260 #ifndef TIZEN_PROFILE_TV
261         ret = location_check_cynara(LOCATION_PRIVILEGE);
262         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
263 #endif
264
265         ret = location_ielement_start_batch(LOCATION_IELEMENT(obj));
266         LOC_IF_FAIL(ret, _E, "Fail to start_batch [%s]", err_msg(ret));
267
268         return ret;
269 }
270
271 EXPORT_API int
272 location_stop_batch(LocationObject *obj)
273 {
274         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
275         int ret = LOCATION_ERROR_NONE;
276         ret = location_ielement_stop_batch(LOCATION_IELEMENT(obj));
277         LOC_IF_FAIL(ret, _E, "Fail to stop_batch [%d]", err_msg(ret));
278
279         return ret;
280 }
281
282 EXPORT_API gboolean
283 location_is_supported_method(LocationMethod method)
284 {
285         gboolean is_supported = FALSE;
286
287         switch (method) {
288         case LOCATION_METHOD_HYBRID:
289                 if (module_is_supported("gps") || module_is_supported("wps"))
290                         is_supported = TRUE;
291                 break;
292         case LOCATION_METHOD_GPS:
293                 is_supported = module_is_supported("gps");
294                 break;
295         case LOCATION_METHOD_WPS:
296                 is_supported = module_is_supported("wps");
297                 break;
298         case LOCATION_METHOD_PASSIVE:
299                 is_supported = module_is_supported("passive");
300                 break;
301         default:
302                         break;
303         }
304
305         return is_supported;
306 }
307
308 EXPORT_API int
309 location_is_enabled_method(LocationMethod method, int *is_enabled)
310 {
311         g_return_val_if_fail(is_enabled, LOCATION_ERROR_PARAMETER);
312         int vconf_val = 0;
313         int vconf_ret = VCONF_ERROR;
314
315         char *_key = __convert_setting_key(method);
316         LOC_COND_RET(!_key, LOCATION_ERROR_NOT_SUPPORTED, _E, "Invalid method = %d [%s]", method, err_msg(LOCATION_ERROR_NOT_SUPPORTED));
317
318         vconf_ret = vconf_get_int(_key, &vconf_val);
319         if (vconf_ret != VCONF_OK) {
320                 LOCATION_SECLOG("vconf_get failed [%s], error [%d]", _key, vconf_ret);
321                 g_free(_key);
322                 return LOCATION_ERROR_NOT_AVAILABLE;
323         } else {
324                 LOCATION_SECLOG("[%s]:[%d]", _key, vconf_val);
325         }
326
327         *is_enabled = vconf_val;
328         g_free(_key);
329
330         return LOCATION_ERROR_NONE;
331 }
332
333 EXPORT_API int
334 location_enable_method(const LocationMethod method, const int enable)
335 {
336         int ret = 0;
337         char *_key = NULL;
338
339 #ifndef TIZEN_PROFILE_TV
340         ret = location_check_cynara(LOCATION_ENABLE_PRIVILEGE);
341         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
342 #endif
343
344         if (location_setting_get_int(VCONFKEY_LOCATION_RESTRICT) > RESTRICT_OFF) {
345                 LOCATION_SECLOG("Location setting is denied by DPM");
346                 return LOCATION_ERROR_NOT_ALLOWED;
347         }
348
349         /* for itself */
350         _key = __convert_setting_key(method);
351         LOC_COND_RET(!_key, LOCATION_ERROR_NOT_SUPPORTED, _E, "Invalid method = %d [%s]", method, err_msg(LOCATION_ERROR_NOT_SUPPORTED));
352
353         ret = vconf_set_int(_key, enable);
354         if (ret != VCONF_OK) {
355                 LOCATION_SECLOG("vconf_set_int failed [%s], ret=[%d]", _key, ret);
356                 g_free(_key);
357                 return LOCATION_ERROR_NOT_ALLOWED;
358         }
359         g_free(_key);
360
361         /* for hybrid */
362         _key = __convert_setting_key(LOCATION_METHOD_HYBRID);
363         LOC_COND_RET(!_key, LOCATION_ERROR_NOT_SUPPORTED, _E, "Invalid method = %d [%s]", LOCATION_METHOD_HYBRID, err_msg(LOCATION_ERROR_NOT_SUPPORTED));
364
365         if (enable) {
366                 ret = vconf_set_int(_key, enable);
367                 if (ret != VCONF_OK) {
368                         LOCATION_SECLOG("vconf_set_int failed [%s], ret=[%d]", _key, ret);
369                         g_free(_key);
370                         return LOCATION_ERROR_NOT_ALLOWED;
371                 }
372         } else {
373                 int i = 0;
374                 int enabled_state = 0;
375
376                 for (i = LOCATION_METHOD_GPS; i < LOCATION_METHOD_MAX; i++) {
377                         _key = __convert_setting_key(i);
378                         enabled_state |= location_setting_get_int(_key);
379                         g_free(_key);
380                 }
381                 if (!enabled_state) {
382                         _key = __convert_setting_key(LOCATION_METHOD_HYBRID);
383                         ret = vconf_set_int(_key, enable);
384                         if (ret != VCONF_OK) {
385                                 LOCATION_SECLOG("vconf_set_int failed [%s], error [%d]", _key, ret);
386                                 g_free(_key);
387                                 return LOCATION_ERROR_NOT_ALLOWED;
388                         } else {
389                                 LOCATION_SECLOG("[%s]:[%d]", _key, ret);
390                         }
391                         g_free(_key);
392                 }
393         }
394
395         return ret;
396 }
397
398 EXPORT_API int
399 location_add_setting_notify(LocationMethod method, LocationSettingCb callback, void *user_data)
400 {
401         int ret = LOCATION_ERROR_NONE;
402         char *_key = __convert_setting_key(method);
403         LOC_COND_RET(!_key, LOCATION_ERROR_PARAMETER, _E, "Invalid method = %d [%s]", method, err_msg(LOCATION_ERROR_PARAMETER));
404
405         g_location_setting.callback = callback;
406         g_location_setting.user_data = user_data;
407
408         ret = location_setting_add_notify(_key, __location_setting_cb, &g_location_setting);
409         g_free(_key);
410
411         return ret;
412 }
413
414 EXPORT_API int
415 location_ignore_setting_notify(LocationMethod method, LocationSettingCb callback)
416 {
417         int ret = LOCATION_ERROR_NONE;
418         char *_key = __convert_setting_key(method);
419         LOC_COND_RET(!_key, LOCATION_ERROR_PARAMETER, _E, "Invalid method = %d [%s]", method, err_msg(LOCATION_ERROR_PARAMETER));
420
421         g_location_setting.callback = NULL;
422         g_location_setting.user_data = NULL;
423
424         ret = location_setting_ignore_notify(_key, __location_setting_cb);
425         g_free(_key);
426
427         return ret;
428 }
429
430
431 EXPORT_API int
432 location_get_position(LocationObject *obj, LocationPosition **position, LocationAccuracy **accuracy)
433 {
434         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
435         g_return_val_if_fail(position, LOCATION_ERROR_PARAMETER);
436         g_return_val_if_fail(accuracy, LOCATION_ERROR_PARAMETER);
437
438         int ret = LOCATION_ERROR_NONE;
439
440 #ifndef TIZEN_PROFILE_TV
441         ret = location_check_cynara(LOCATION_PRIVILEGE);
442         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
443 #endif
444
445         ret = location_ielement_get_position(LOCATION_IELEMENT(obj), position, accuracy);
446         LOC_COND_RET(ret != LOCATION_ERROR_NONE, ret, _E, "Fail to get_position [%s]", err_msg(ret));
447
448         return ret;
449 }
450
451 EXPORT_API int
452 location_get_position_ext(LocationObject *obj, LocationPosition **position, LocationVelocity **velocity, LocationAccuracy **accuracy)
453 {
454         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
455         g_return_val_if_fail(position, LOCATION_ERROR_PARAMETER);
456         g_return_val_if_fail(velocity, LOCATION_ERROR_PARAMETER);
457         g_return_val_if_fail(accuracy, LOCATION_ERROR_PARAMETER);
458
459         int ret = LOCATION_ERROR_NONE;
460
461 #ifndef TIZEN_PROFILE_TV
462         ret = location_check_cynara(LOCATION_PRIVILEGE);
463         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
464 #endif
465
466         ret = location_ielement_get_position_ext(LOCATION_IELEMENT(obj), position, velocity, accuracy);
467         LOC_IF_FAIL(ret, _E, "Fail to get_position_ext [%s]", err_msg(ret));
468
469         return ret;
470 }
471
472 EXPORT_API int
473 location_get_last_position(LocationObject *obj, LocationPosition **position, LocationAccuracy **accuracy)
474 {
475         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
476         g_return_val_if_fail(position, LOCATION_ERROR_PARAMETER);
477         g_return_val_if_fail(accuracy, LOCATION_ERROR_PARAMETER);
478
479         int ret = LOCATION_ERROR_NONE;
480
481 #ifndef TIZEN_PROFILE_TV
482         ret = location_check_cynara(LOCATION_PRIVILEGE);
483         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
484 #endif
485
486         ret = location_ielement_get_last_position(LOCATION_IELEMENT(obj), position, accuracy);
487         LOC_IF_FAIL(ret, _E, "Fail to get_last_position [%s]", err_msg(ret));
488
489         return ret;
490 }
491
492 EXPORT_API int
493 location_get_last_position_ext(LocationObject *obj, LocationPosition **position, LocationVelocity **velocity, LocationAccuracy **accuracy)
494 {
495         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
496         g_return_val_if_fail(position, LOCATION_ERROR_PARAMETER);
497         g_return_val_if_fail(velocity, LOCATION_ERROR_PARAMETER);
498         g_return_val_if_fail(accuracy, LOCATION_ERROR_PARAMETER);
499
500         int ret = LOCATION_ERROR_NONE;
501
502 #ifndef TIZEN_PROFILE_TV
503         ret = location_check_cynara(LOCATION_PRIVILEGE);
504         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
505 #endif
506
507         ret = location_ielement_get_last_position_ext(LOCATION_IELEMENT(obj), position, velocity, accuracy);
508         LOC_IF_FAIL(ret, _E, "Fail to get_last_position_ext [%s]", err_msg(ret));
509
510         return ret;
511 }
512
513 EXPORT_API int
514 location_get_nmea(LocationObject *obj, char **nmea)
515 {
516         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
517         g_return_val_if_fail(nmea, LOCATION_ERROR_PARAMETER);
518
519         int ret = LOCATION_ERROR_NONE;
520         ret = location_ielement_get_nmea(LOCATION_IELEMENT(obj), nmea);
521         LOC_IF_FAIL(ret, _E, "Fail to get_nmea [%s]", err_msg(ret));
522
523         return ret;
524 }
525
526
527 EXPORT_API int
528 location_get_satellite(LocationObject *obj, LocationSatellite **satellite)
529 {
530         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
531         g_return_val_if_fail(satellite, LOCATION_ERROR_PARAMETER);
532
533         int ret = LOCATION_ERROR_NONE;
534
535 #ifndef TIZEN_PROFILE_TV
536         ret = location_check_cynara(LOCATION_PRIVILEGE);
537         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
538 #endif
539
540         ret = location_ielement_get_satellite(LOCATION_IELEMENT(obj), satellite);
541         LOC_IF_FAIL(ret, _E, "Fail to get_satellite [%s]", err_msg(ret));
542
543         return ret;
544 }
545
546 EXPORT_API int
547 location_get_batch(LocationObject *obj, LocationBatch **batch)
548 {
549         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
550         g_return_val_if_fail(batch, LOCATION_ERROR_PARAMETER);
551
552         int ret = LOCATION_ERROR_NONE;
553
554 #ifndef TIZEN_PROFILE_TV
555         ret = location_check_cynara(LOCATION_PRIVILEGE);
556         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
557 #endif
558
559         ret = location_ielement_get_batch(LOCATION_IELEMENT(obj), batch);
560         LOC_IF_FAIL(ret, _E, "Fail to get_batch [%s]", err_msg(ret));
561
562         return ret;
563 }
564
565 EXPORT_API int
566 location_get_last_satellite(LocationObject *obj, LocationSatellite **satellite)
567 {
568         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
569         g_return_val_if_fail(satellite, LOCATION_ERROR_PARAMETER);
570
571         int ret = LOCATION_ERROR_NONE;
572
573 #ifndef TIZEN_PROFILE_TV
574         ret = location_check_cynara(LOCATION_PRIVILEGE);
575         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
576 #endif
577
578         ret = location_ielement_get_last_satellite(LOCATION_IELEMENT(obj), satellite);
579         LOC_IF_FAIL(ret, _E, "Fail to get_last_satellite [%s]", err_msg(ret));
580
581         return ret;
582 }
583
584 EXPORT_API int
585 location_get_velocity(LocationObject *obj, LocationVelocity **velocity, LocationAccuracy **accuracy)
586 {
587         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
588         g_return_val_if_fail(velocity, LOCATION_ERROR_PARAMETER);
589         g_return_val_if_fail(accuracy, LOCATION_ERROR_PARAMETER);
590
591         int ret = LOCATION_ERROR_NONE;
592
593 #ifndef TIZEN_PROFILE_TV
594         ret = location_check_cynara(LOCATION_PRIVILEGE);
595         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
596 #endif
597
598         ret = location_ielement_get_velocity(LOCATION_IELEMENT(obj), velocity, accuracy);
599         LOC_IF_FAIL(ret, _E, "Fail to get_velocity [%s]", err_msg(ret));
600
601         return ret;
602 }
603
604 EXPORT_API int
605 location_get_last_velocity(LocationObject *obj, LocationVelocity **velocity, LocationAccuracy **accuracy)
606 {
607         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
608         g_return_val_if_fail(velocity, LOCATION_ERROR_PARAMETER);
609         g_return_val_if_fail(accuracy, LOCATION_ERROR_PARAMETER);
610
611         int ret = LOCATION_ERROR_NONE;
612
613 #ifndef TIZEN_PROFILE_TV
614         ret = location_check_cynara(LOCATION_PRIVILEGE);
615         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
616 #endif
617
618         ret = location_ielement_get_last_velocity(LOCATION_IELEMENT(obj), velocity, accuracy);
619         LOC_IF_FAIL(ret, _E, "Fail to get_last_velocity [%s]", err_msg(ret));
620
621         return ret;
622 }
623
624 EXPORT_API int
625 location_get_accessibility_state(LocationAccessState *state)
626 {
627 #ifndef TIZEN_PROFILE_TV
628         int ret = location_check_cynara(LOCATION_PRIVILEGE);
629
630         if (ret == LOCATION_ERROR_NONE) {
631                 *state = LOCATION_ACCESS_ALLOWED;
632         } else {
633                 *state = LOCATION_ACCESS_DENIED;
634                 LOCATION_LOGE("Cannot use location service for privacy[%d]", ret);
635         }
636 #else
637         *state = LOCATION_ACCESS_ALLOWED;
638 #endif
639
640         return LOCATION_ERROR_NONE;
641 }
642
643 EXPORT_API int
644 location_set_accessibility_state(LocationAccessState state)
645 {
646         int auth = LOCATION_APP_NOT_FOUND;
647         int ret = LOCATION_ERROR_NONE;
648
649         switch (state) {
650         case LOCATION_ACCESS_DENIED:
651                 auth = LOCATION_APP_OFF;
652                 break;
653         case LOCATION_ACCESS_ALLOWED:
654                 auth = LOCATION_APP_ON;
655                 break;
656         case LOCATION_ACCESS_NONE:
657         default:
658                 return LOCATION_ERROR_PARAMETER;
659         }
660
661         ret = location_application_set_authority(auth);
662         LOCATION_LOGD("set_accessibility_state [%d], Error[%d]", auth, ret);
663         return ret;
664 }
665
666 EXPORT_API int
667 location_send_command(const char *cmd)
668 {
669         g_return_val_if_fail(cmd, LOCATION_ERROR_PARAMETER);
670         return LOCATION_ERROR_NOT_AVAILABLE;
671 }
672
673 EXPORT_API int
674 location_set_option(LocationObject *obj, const char *option)
675 {
676         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
677         int ret = LOCATION_ERROR_NONE;
678
679 #ifndef TIZEN_PROFILE_TV
680         ret = location_check_cynara(LOCATION_PRIVILEGE);
681         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
682 #endif
683
684         ret = location_ielement_set_option(LOCATION_IELEMENT(obj), option);
685         LOC_IF_FAIL(ret, _E, "Fail to get_velocity [%s]", err_msg(ret));
686         return ret;
687 }
688
689
690 /*
691  * Tizen 3.0
692  */
693 EXPORT_API int
694 location_enable_mock(const int enable)
695 {
696         int ret = LOCATION_ERROR_NONE;
697
698 #ifndef TIZEN_PROFILE_TV
699         ret = location_check_cynara(LOCATION_PRIVILEGE);
700         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
701 #endif
702
703 #if 0 /* Tizen platform didn't turn developer option on */
704         gboolean developer_option = FALSE;
705
706         ret = vconf_get_bool(VCONFKEY_SETAPPL_DEVELOPER_OPTION_STATE, &developer_option);
707         LOC_COND_RET(!developer_option, LOCATION_ERROR_NOT_ALLOWED, _E, "Cannot enable mock location because developer option is not turned on", ret);
708 #endif
709
710         ret = vconf_set_int(VCONFKEY_LOCATION_MOCK_ENABLED, enable);
711         if (ret != VCONF_OK) {
712                 LOCATION_SECLOG("vconf_set_int failed [MOCK_ENABLED], ret=[%d]", ret);
713                 return LOCATION_ERROR_NOT_ALLOWED;
714         }
715
716         return ret;
717 }
718
719 EXPORT_API int
720 location_set_mock_location(LocationObject *obj, const LocationPosition *position, const LocationVelocity *velocity, const LocationAccuracy *accuracy)
721 {
722         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
723         g_return_val_if_fail(position, LOCATION_ERROR_PARAMETER);
724         g_return_val_if_fail(velocity, LOCATION_ERROR_PARAMETER);
725         g_return_val_if_fail(accuracy, LOCATION_ERROR_PARAMETER);
726
727         int ret = LOCATION_ERROR_NONE;
728         ret = location_ielement_set_mock_location(LOCATION_IELEMENT(obj), position, velocity, accuracy);
729         LOC_IF_FAIL(ret, _E, "Fail to set_mock_location [%s]", err_msg(ret));
730
731         return ret;
732 }
733
734 EXPORT_API int
735 location_clear_mock_location(LocationObject *obj)
736 {
737         g_return_val_if_fail(obj, LOCATION_ERROR_PARAMETER);
738
739         int ret = LOCATION_ERROR_NONE;
740         ret = location_ielement_clear_mock_location(LOCATION_IELEMENT(obj));
741         LOC_IF_FAIL(ret, _E, "Fail to clear_mock_location [%s]", err_msg(ret));
742
743         return ret;
744 }
745
746 EXPORT_API int
747 location_enable_restriction(const int enable)
748 {
749         int ret = LOCATION_ERROR_NONE;
750         int restriction = 0;
751
752 #ifndef TIZEN_PROFILE_TV
753         ret = location_check_cynara(LOCATION_ENABLE_PRIVILEGE);
754         LOC_IF_FAIL(ret, _E, "Privilege not allowed [%s]", err_msg(ret));
755 #endif
756         if (enable) {
757                 int value = 0;
758                 ret = vconf_get_int(VCONFKEY_LOCATION_RESTRICT, &restriction);
759                 LOC_COND_RET(ret != VCONF_OK, LOCATION_ERROR_NOT_ALLOWED, _E, "Fail to get restriction status [%s]", err_msg(LOCATION_ERROR_NOT_ALLOWED));
760
761                 if (restriction == RESTRICT_OFF) {
762
763                         if (location_setting_get_int(VCONFKEY_LOCATION_ENABLED)) {
764                                 value |= RESTRICT_GPS;
765                                 ret = vconf_set_int(VCONFKEY_LOCATION_ENABLED, 0);
766                                 LOC_COND_RET(ret != VCONF_OK, LOCATION_ERROR_NOT_ALLOWED, _E, "Fail to set status [%s]", err_msg(LOCATION_ERROR_NOT_ALLOWED));
767                         }
768
769                         if (location_setting_get_int(VCONFKEY_LOCATION_NETWORK_ENABLED)) {
770                                 value |= RESTRICT_WPS;
771                                 ret = vconf_set_int(VCONFKEY_LOCATION_NETWORK_ENABLED, 0);
772                                 LOC_COND_RET(ret != VCONF_OK, LOCATION_ERROR_NOT_ALLOWED, _E, "Fail to set status [%s]", err_msg(LOCATION_ERROR_NOT_ALLOWED));
773                         }
774
775                         if (location_setting_get_int(VCONFKEY_LOCATION_USE_MY_LOCATION)) {
776                                 value |= RESTRICT_HYBRID;
777                                 ret = vconf_set_int(VCONFKEY_LOCATION_USE_MY_LOCATION, 0);
778                                 LOC_COND_RET(ret != VCONF_OK, LOCATION_ERROR_NOT_ALLOWED, _E, "Fail to set status [%s]", err_msg(LOCATION_ERROR_NOT_ALLOWED));
779                         }
780
781                         if (value == 0)
782                                 value = RESTRICT_NONE;
783
784                         ret = vconf_set_int(VCONFKEY_LOCATION_RESTRICT, value);
785                         LOC_COND_RET(ret != VCONF_OK, LOCATION_ERROR_NOT_ALLOWED, _E, "Fail to set value. %d [%s]", value, err_msg(LOCATION_ERROR_NOT_ALLOWED));
786                 }
787
788         } else {
789                 ret = vconf_get_int(VCONFKEY_LOCATION_RESTRICT, &restriction);
790                 LOC_COND_RET(ret != VCONF_OK, LOCATION_ERROR_NOT_ALLOWED, _E, "Fail to get restriction status [%s]", err_msg(LOCATION_ERROR_NOT_ALLOWED));
791
792                 if (restriction > RESTRICT_OFF) {
793
794                         if (restriction & RESTRICT_GPS) {
795                                 ret = vconf_set_int(VCONFKEY_LOCATION_ENABLED, 1);
796                                 LOC_COND_RET(ret != VCONF_OK, LOCATION_ERROR_NOT_ALLOWED, _E, "Fail to set status [%s]", err_msg(LOCATION_ERROR_NOT_ALLOWED));
797                         }
798
799                         if (restriction & RESTRICT_WPS) {
800                                 ret = vconf_set_int(VCONFKEY_LOCATION_NETWORK_ENABLED, 1);
801                                 LOC_COND_RET(ret != VCONF_OK, LOCATION_ERROR_NOT_ALLOWED, _E, "Fail to set status [%s]", err_msg(LOCATION_ERROR_NOT_ALLOWED));
802                         }
803
804                         if (restriction & RESTRICT_HYBRID) {
805                                 ret = vconf_set_int(VCONFKEY_LOCATION_USE_MY_LOCATION, 1);
806                                 LOC_COND_RET(ret != VCONF_OK, LOCATION_ERROR_NOT_ALLOWED, _E, "Fail to set status [%s]", err_msg(LOCATION_ERROR_NOT_ALLOWED));
807                         }
808
809                         ret = vconf_set_int(VCONFKEY_LOCATION_RESTRICT, RESTRICT_OFF);
810                         LOC_COND_RET(ret != VCONF_OK, LOCATION_ERROR_NOT_ALLOWED, _E, "Fail to set value. %d [%s]", RESTRICT_OFF, err_msg(LOCATION_ERROR_NOT_ALLOWED));
811                 }
812         }
813
814         return LOCATION_ERROR_NONE;
815 }