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