sync PRIVATE and RSA for tizen2.1
[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 <dbus/dbus-glib.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-position.h"
39 #include "map-service.h"
40 #include "module-internal.h"
41 #include "location-common-util.h"
42
43 EXPORT_API
44 int location_init (void)
45 {
46         LOCATION_LOGD("location_init");
47         g_type_init ();
48
49 #if !GLIB_CHECK_VERSION (2, 31, 0)
50         if (!g_thread_supported()) g_thread_init (NULL);
51 #endif
52         dbus_g_thread_init ();
53         if( FALSE == module_init() )
54                 return LOCATION_ERROR_NOT_AVAILABLE;
55
56         return LOCATION_ERROR_NONE;
57 }
58
59 EXPORT_API LocationObject*
60 location_new (LocationMethod method)
61 {
62         LocationObject *self = NULL;
63
64         switch (method) {
65                 case LOCATION_METHOD_HYBRID:
66                         self = g_object_new (LOCATION_TYPE_HYBRID, NULL);
67                         break;
68                 case LOCATION_METHOD_GPS:
69                         self = g_object_new (LOCATION_TYPE_GPS, NULL);
70                         break;
71                 case LOCATION_METHOD_WPS:
72                         self = g_object_new (LOCATION_TYPE_WPS, NULL);
73                         break;
74                 case LOCATION_METHOD_CPS:
75                 default:
76                         break;
77         }
78
79         if (!self) LOCATION_LOGD("Fail to create location object. Method [%d]", method);
80         return self;
81 }
82
83 EXPORT_API int
84 location_free (LocationObject *obj)
85 {
86         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
87
88         g_object_unref (obj);
89         return LOCATION_ERROR_NONE;
90 }
91
92 EXPORT_API int
93 location_start (LocationObject *obj)
94 {
95         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
96         g_return_val_if_fail (G_OBJECT_TYPE(obj) != MAP_TYPE_SERVICE, LOCATION_ERROR_PARAMETER);
97
98         int ret = LOCATION_ERROR_NONE;
99         ret = location_ielement_start (LOCATION_IELEMENT(obj));
100         if (ret != LOCATION_ERROR_NONE) LOCATION_LOGD("Fail to start. Error [%d]", ret);
101
102         return ret;
103 }
104
105 EXPORT_API int
106 location_stop (LocationObject *obj)
107 {
108         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
109         g_return_val_if_fail (G_OBJECT_TYPE(obj) != MAP_TYPE_SERVICE, LOCATION_ERROR_PARAMETER);
110         int ret = LOCATION_ERROR_NONE;
111         ret = location_ielement_stop (LOCATION_IELEMENT(obj));
112         if (ret != LOCATION_ERROR_NONE) LOCATION_LOGD("Fail to stop. Error [%d]", ret);
113
114         return ret;
115 }
116
117 EXPORT_API gboolean
118 location_is_supported_method(LocationMethod method)
119 {
120         gboolean is_supported = FALSE;
121
122         switch(method) {
123         case LOCATION_METHOD_HYBRID:
124                 if(module_is_supported("gps") || module_is_supported("wps"))
125                         is_supported = TRUE;
126                 break;
127         case LOCATION_METHOD_GPS:
128                 is_supported = module_is_supported("gps");
129                 break;
130         case LOCATION_METHOD_WPS:
131                 is_supported = module_is_supported("wps");
132                 break;
133         case LOCATION_METHOD_CPS:
134         default:
135                 break;
136         }
137
138         return is_supported;
139 }
140
141 EXPORT_API gboolean
142 location_is_enabled_gps(LocationObject *obj)
143 {
144         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
145         g_return_val_if_fail (G_OBJECT_TYPE(obj) != MAP_TYPE_SERVICE, LOCATION_ERROR_PARAMETER);
146
147         return (gboolean) location_setting_get_int(VCONFKEY_LOCATION_ENABLED);
148 }
149
150 EXPORT_API int
151 location_get_position (LocationObject *obj,
152         LocationPosition **position,
153         LocationAccuracy **accuracy)
154 {
155         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
156         g_return_val_if_fail (G_OBJECT_TYPE(obj) != MAP_TYPE_SERVICE, LOCATION_ERROR_PARAMETER);
157         g_return_val_if_fail (position, LOCATION_ERROR_PARAMETER);
158         g_return_val_if_fail (accuracy, LOCATION_ERROR_PARAMETER);
159
160         int ret = LOCATION_ERROR_NONE;
161         ret = location_ielement_get_position (LOCATION_IELEMENT(obj), position, accuracy);
162         if (ret != LOCATION_ERROR_NONE) LOCATION_LOGD("Fail to get_position. Error [%d]", ret);
163
164         return ret;
165 }
166
167 EXPORT_API int
168 location_get_position_ext (LocationObject *obj,
169         LocationPosition **position,
170         LocationVelocity **velocity,
171         LocationAccuracy **accuracy)
172 {
173         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
174         g_return_val_if_fail (G_OBJECT_TYPE(obj) != MAP_TYPE_SERVICE, LOCATION_ERROR_PARAMETER);
175         g_return_val_if_fail (position, LOCATION_ERROR_PARAMETER);
176         g_return_val_if_fail (velocity, LOCATION_ERROR_PARAMETER);
177         g_return_val_if_fail (accuracy, LOCATION_ERROR_PARAMETER);
178
179         int ret = LOCATION_ERROR_NONE;
180         ret = location_ielement_get_position_ext (LOCATION_IELEMENT(obj), position, velocity, accuracy);
181         if (ret != LOCATION_ERROR_NONE) LOCATION_LOGD("Fail to get_position_ext. Error [%d]", ret);
182
183         return ret;
184 }
185
186 EXPORT_API int
187 location_get_last_position (LocationObject *obj,
188         LocationPosition **position,
189         LocationAccuracy **accuracy)
190 {
191         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
192         g_return_val_if_fail (G_OBJECT_TYPE(obj) != MAP_TYPE_SERVICE, LOCATION_ERROR_PARAMETER);
193         g_return_val_if_fail (position, LOCATION_ERROR_PARAMETER);
194         g_return_val_if_fail (accuracy, LOCATION_ERROR_PARAMETER);
195
196         int ret = LOCATION_ERROR_NONE;
197         ret = location_ielement_get_last_position (LOCATION_IELEMENT(obj), position, accuracy);
198         if (ret != LOCATION_ERROR_NONE) LOCATION_LOGD("Fail to get_last_position. Error [%d]", ret);
199
200         return ret;
201 }
202
203 EXPORT_API int
204 location_get_last_position_ext (LocationObject *obj,
205         LocationPosition **position,
206         LocationVelocity **velocity,
207         LocationAccuracy **accuracy)
208 {
209         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
210         g_return_val_if_fail (G_OBJECT_TYPE(obj) != MAP_TYPE_SERVICE, LOCATION_ERROR_PARAMETER);
211         g_return_val_if_fail (position, LOCATION_ERROR_PARAMETER);
212         g_return_val_if_fail (velocity, LOCATION_ERROR_PARAMETER);
213         g_return_val_if_fail (accuracy, LOCATION_ERROR_PARAMETER);
214
215         int ret = LOCATION_ERROR_NONE;
216         ret = location_ielement_get_last_position_ext (LOCATION_IELEMENT(obj), position, accuracy);
217         if (ret != LOCATION_ERROR_NONE) LOCATION_LOGD("Fail to get_last_position_ext. Error [%d]", ret);
218
219         return ret;
220 }
221
222 EXPORT_API int
223 location_get_satellite (LocationObject *obj, LocationSatellite **satellite)
224 {
225         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
226         g_return_val_if_fail (G_OBJECT_TYPE(obj) != MAP_TYPE_SERVICE, LOCATION_ERROR_PARAMETER);
227         g_return_val_if_fail (satellite, LOCATION_ERROR_PARAMETER);
228
229         int ret = LOCATION_ERROR_NONE;
230         ret = location_ielement_get_satellite (LOCATION_IELEMENT(obj), satellite);
231         if (ret != LOCATION_ERROR_NONE) LOCATION_LOGD("Fail to get_satellite. Error [%d]", ret);
232
233         return ret;
234 }
235
236 EXPORT_API int
237 location_get_last_satellite (LocationObject *obj, LocationSatellite **satellite)
238 {
239         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
240         g_return_val_if_fail (G_OBJECT_TYPE(obj) != MAP_TYPE_SERVICE, LOCATION_ERROR_PARAMETER);
241         g_return_val_if_fail (satellite, LOCATION_ERROR_PARAMETER);
242
243         int ret = LOCATION_ERROR_NONE;
244         ret = location_ielement_get_last_satellite (LOCATION_IELEMENT(obj), satellite);
245         if (ret != LOCATION_ERROR_NONE) LOCATION_LOGD("Fail to get_last_satellite. Error [%d]", ret);
246
247         return ret;
248 }
249
250 EXPORT_API int
251 location_get_velocity (LocationObject *obj,
252         LocationVelocity **velocity,
253         LocationAccuracy **accuracy)
254 {
255         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
256         g_return_val_if_fail (G_OBJECT_TYPE(obj) != MAP_TYPE_SERVICE, LOCATION_ERROR_PARAMETER);
257         g_return_val_if_fail (velocity, LOCATION_ERROR_PARAMETER);
258         g_return_val_if_fail (accuracy, LOCATION_ERROR_PARAMETER);
259
260         int ret = LOCATION_ERROR_NONE;
261         ret = location_ielement_get_velocity (LOCATION_IELEMENT(obj), velocity, accuracy);
262         if (ret != LOCATION_ERROR_NONE) LOCATION_LOGD("Fail to get_velocity. Error [%d]", ret);
263
264         return ret;
265 }
266
267 EXPORT_API int
268 location_get_last_velocity (LocationObject *obj,
269         LocationVelocity **velocity,
270         LocationAccuracy **accuracy)
271 {
272         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
273         g_return_val_if_fail (G_OBJECT_TYPE(obj) != MAP_TYPE_SERVICE, LOCATION_ERROR_PARAMETER);
274         g_return_val_if_fail (velocity, LOCATION_ERROR_PARAMETER);
275         g_return_val_if_fail (accuracy, LOCATION_ERROR_PARAMETER);
276
277         int ret = LOCATION_ERROR_NONE;
278         ret = location_ielement_get_last_velocity (LOCATION_IELEMENT(obj), velocity, accuracy);
279         if (ret != LOCATION_ERROR_NONE) LOCATION_LOGD("Fail to get_last_velocity. Error [%d]", ret);
280
281         return ret;
282 }
283
284 EXPORT_API int
285 location_get_accessibility_state (LocationAccessState *state)
286 {
287         int auth = location_application_get_authority ();
288         switch (auth) {
289                 case LOCATION_APP_OFF:
290                         *state = LOCATION_ACCESS_DENIED;
291                         break;
292                 case LOCATION_APP_ON:
293                         *state = LOCATION_ACCESS_ALLOWED;
294                         break;
295                 case LOCATION_APP_NOT_FOUND:
296                         *state = LOCATION_ACCESS_NONE;
297                         break;
298                 default:
299                         return LOCATION_ERROR_UNKNOWN;
300         }
301
302         LOCATION_LOGD("get_accessibility_state [%d]", auth);
303         return LOCATION_ERROR_NONE;
304 }
305
306 EXPORT_API int
307 location_set_accessibility_state (LocationAccessState state)
308 {
309         int auth = LOCATION_APP_NOT_FOUND;
310         int ret = LOCATION_ERROR_NONE;
311
312         switch (state) {
313                 case LOCATION_ACCESS_DENIED:
314                         auth = LOCATION_APP_OFF;
315                         break;
316                 case LOCATION_ACCESS_ALLOWED:
317                         auth = LOCATION_APP_ON;
318                         break;
319                 case LOCATION_ACCESS_NONE:
320                 default:
321                         return LOCATION_ERROR_PARAMETER;
322         }
323
324         ret = location_application_set_authority(auth);
325         LOCATION_LOGD("set_accessibility_state [%d], Error[%d]", auth, ret);
326         return ret;
327 }
328
329 EXPORT_API int
330 location_send_command(const char *cmd)
331 {
332         g_return_val_if_fail (cmd, LOCATION_ERROR_PARAMETER);
333         return LOCATION_ACCESS_NONE;
334 }
335
336 EXPORT_API int
337 location_set_option (LocationObject *obj, const char *option)
338 {
339         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
340         int ret = LOCATION_ERROR_NONE;
341         return ret;
342 }