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