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