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