5afe973ff92c360c2a57db292eab772427d7a4ae
[framework/location/libslp-location.git] / location / location.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7  *          Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.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 <stdio.h>
27
28 #include "location/location.h"
29 #include "location/location-log.h"
30 #include "location/location-setting.h"
31 #include "location/location-module-internal.h"
32 #include "location/location-ielement.h"
33 #include "location/location-hybrid.h"
34 #include "location/location-gps.h"
35 #include "location/location-wps.h"
36 #include "location/location-ips.h"
37 #include "location/location-cps.h"
38 #include "location/location-sps.h"
39 #include "location/location-geocode.h"
40 #include "location/location-poi.h"
41 #include "location/location-position.h"
42
43 static LocationObject *geocode = NULL;
44 static LocationObject *poi = NULL;
45
46 static gboolean
47 is_connected_network()
48 {
49         gboolean is_connected = TRUE;
50         int net_state = 0;
51
52         net_state = location_setting_get_int(VCONFKEY_NETWORK_STATUS);
53
54         LOCATION_LOGW("net_state[%d]", net_state);
55         if(net_state == VCONFKEY_NETWORK_OFF) {
56                 is_connected = FALSE;
57         }
58
59         return is_connected;
60
61 }
62
63 EXPORT_API int location_init (void)
64 {
65         LOCATION_LOGD("location_init");
66         g_type_init ();
67         if( FALSE == module_init() )
68                 return LOCATION_ERROR_NOT_AVAILABLE;
69
70         return LOCATION_ERROR_NONE;
71 }
72
73 EXPORT_API LocationObject*
74 location_new (LocationMethod method)
75 {
76         LocationObject *self = NULL;
77
78         if (!geocode)
79                 geocode = g_object_new (LOCATION_TYPE_GEOCODE, NULL);
80
81         /*if (!poi)
82                 poi = g_object_new (LOCATION_TYPE_POI, NULL);*/
83
84         switch (method) {
85         case LOCATION_METHOD_HYBRID:
86                 self = g_object_new (LOCATION_TYPE_HYBRID, NULL);
87                 break;
88         case LOCATION_METHOD_GPS:
89                 self = g_object_new (LOCATION_TYPE_GPS, NULL);
90                 break;
91         case LOCATION_METHOD_WPS:
92                 self = g_object_new (LOCATION_TYPE_WPS, NULL);
93                 break;
94         case LOCATION_METHOD_CPS:
95                 self = g_object_new (LOCATION_TYPE_CPS, NULL);
96                 break;
97         case LOCATION_METHOD_IPS:
98                 self = g_object_new (LOCATION_TYPE_IPS, NULL);
99                 break;
100         case LOCATION_METHOD_SPS:
101                 self = g_object_new (LOCATION_TYPE_SPS, NULL);
102                 break;
103         default:
104                 break;
105         }
106         return self;
107 }
108
109 EXPORT_API int
110 location_free (LocationObject *obj)
111 {
112         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
113
114         //location_ielement_stop(LOCATION_IELEMENT(obj));
115         if (geocode) {
116                 g_object_unref (geocode);
117                 geocode = NULL;
118         }
119         /*if (poi) {
120                 g_object_unref (poi);
121                 poi = NULL;
122         }*/
123         g_object_unref (obj);
124         return LOCATION_ERROR_NONE;
125 }
126
127 EXPORT_API int
128 location_start (LocationObject *obj)
129 {
130         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
131         return location_ielement_start (LOCATION_IELEMENT(obj));
132 }
133
134 EXPORT_API int
135 location_stop (LocationObject *obj)
136 {
137         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
138         return location_ielement_stop (LOCATION_IELEMENT(obj));
139 }
140
141 EXPORT_API gboolean
142 location_is_supported_method(LocationMethod method)
143 {
144         gboolean is_supported = FALSE;
145
146         switch(method) {
147         case LOCATION_METHOD_HYBRID:
148                 if(module_is_supported("gps") || module_is_supported("wps") || module_is_supported("sps"))
149                         is_supported = TRUE;
150                 break;
151         case LOCATION_METHOD_GPS:
152                 is_supported = module_is_supported("gps");
153                 break;
154         case LOCATION_METHOD_WPS:
155                 is_supported = module_is_supported("wps");
156                 break;
157         case LOCATION_METHOD_SPS:
158                 is_supported = module_is_supported("sps");
159                 break;
160         case LOCATION_METHOD_CPS:
161         case LOCATION_METHOD_IPS:
162         default:
163                 break;
164         }
165
166         return is_supported;
167 }
168
169 EXPORT_API gboolean
170 location_is_enabled_gps(LocationObject *obj)
171 {
172         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
173
174         return (gboolean) location_setting_get_int(GPS_ENABLED);
175 }
176
177 EXPORT_API int
178 location_get_position (LocationObject *obj,
179         LocationPosition **position,
180         LocationAccuracy **accuracy)
181 {
182         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
183         return location_ielement_get_position (LOCATION_IELEMENT(obj), position, accuracy);
184 }
185
186 EXPORT_API int
187 location_get_last_known_position (LocationObject *obj,
188         LocationLastPosition *last_position)
189 {
190         g_return_val_if_fail (last_position, LOCATION_ERROR_PARAMETER);
191
192         int ret = -1;
193         int lat, lon, acc;
194         char position[MAX_KEY_LENGTH + 1] = {0,};
195
196         snprintf(position, MAX_KEY_LENGTH + 1, "%s", location_setting_get_string(LAST_POSITION));
197         if (position[0] != '\0') {
198                 location_last_position_a2i(position, &lat, &lon);
199                 LOCATION_LOGD("location_last_position_a2i [%s][%d/%d]", position, lat, lon);
200                 acc = location_setting_get_int(LAST_ACCURACY);
201
202                 last_position->latitude = lat * 0.0001;
203                 last_position->longitude = lon * 0.0001;
204                 last_position->accuracy = acc * 0.0001;
205                 LOCATION_LOGD("last_position [%f/%f]", last_position->latitude, last_position->longitude);
206                 ret = LOCATION_ERROR_NONE;
207         } else {
208                 LOCATION_LOGD("location_last_position is empty");
209                 ret = LOCATION_ERROR_UNKNOWN;
210         }
211
212         return ret;
213 }
214
215 EXPORT_API int
216 location_get_position_from_address (LocationObject *obj,
217         const LocationAddress *address,
218         LocationPosition **position,
219         LocationAccuracy **accuracy)
220 {
221         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
222         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
223         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
224         return location_ielement_get_geocode (LOCATION_IELEMENT(geocode), address, position, accuracy);
225 }
226
227 EXPORT_API int
228 location_get_position_from_freeformed_address (LocationObject *obj,
229         const gchar *address,
230         LocationPosition **position,
231         LocationAccuracy **accuracy)
232 {
233         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
234         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
235         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
236         return location_ielement_get_geocode_freeform (LOCATION_IELEMENT(geocode), address, position, accuracy);
237 }
238
239 EXPORT_API int
240 location_get_velocity (LocationObject *obj,
241         LocationVelocity **velocity,
242         LocationAccuracy **accuracy)
243 {
244         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
245         return location_ielement_get_velocity (LOCATION_IELEMENT(obj), velocity, accuracy);
246 }
247
248 EXPORT_API int
249 location_get_address (LocationObject *obj,
250         LocationAddress **address,
251         LocationAccuracy **accuracy)
252 {
253         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
254         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
255         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
256
257         LocationPosition *position = NULL;
258         int ret = location_ielement_get_position(LOCATION_IELEMENT (obj), &position, accuracy);
259         if (LOCATION_ERROR_NONE != ret) return ret;
260         ret = location_ielement_get_reversegeocode (LOCATION_IELEMENT(geocode), position, address, accuracy);
261         location_position_free (position);
262         return ret;
263 }
264
265 EXPORT_API int
266 location_get_address_from_position (LocationObject *obj,
267         const LocationPosition *position,
268         LocationAddress **address,
269         LocationAccuracy **accuracy)
270 {
271         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
272         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
273         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
274         return location_ielement_get_reversegeocode (LOCATION_IELEMENT(geocode), position, address, accuracy);
275 }
276
277 EXPORT_API int
278 location_get_position_from_address_async (LocationObject *obj,
279         const LocationAddress *address,
280         LocationPositionCB callback,
281         gpointer userdata)
282 {
283         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
284         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
285         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
286         return location_ielement_get_geocode_async (LOCATION_IELEMENT(geocode), address, callback, userdata);
287 }
288
289
290 EXPORT_API int
291 location_get_position_from_freeformed_address_async (LocationObject *obj,
292         const gchar *address,
293         LocationPositionCB callback,
294         gpointer userdata)
295 {
296         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
297         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
298         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
299         return location_ielement_get_geocode_freeform_async (LOCATION_IELEMENT(geocode), address, callback, userdata);
300 }
301
302 EXPORT_API int
303 location_get_address_async (LocationObject *obj,
304         LocationAddressCB callback,
305         gpointer userdata)
306 {
307         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
308         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
309         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
310
311         LocationPosition *position = NULL;
312         LocationAccuracy *acc = NULL;
313         int ret = location_ielement_get_position(LOCATION_IELEMENT(obj), &position, &acc);
314         if (LOCATION_ERROR_NONE != ret) return ret;
315         ret = location_ielement_get_reversegeocode_async (LOCATION_IELEMENT(geocode), position, callback, userdata);
316         location_position_free (position);
317         return ret;
318 }
319
320 EXPORT_API int
321 location_get_address_from_position_async (LocationObject *obj,
322         const LocationPosition *position,
323         LocationAddressCB callback,
324         gpointer userdata)
325 {
326         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
327         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
328         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
329         return location_ielement_get_reversegeocode_async (LOCATION_IELEMENT(geocode), position, callback, userdata);
330 }
331
332 EXPORT_API int
333 location_get_poi (LocationObject *obj,
334         gdouble radius,
335         const gchar *keyword,
336         LocationPOIInfo **poi_info)
337 {
338         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
339         g_return_val_if_fail (poi, LOCATION_ERROR_NOT_AVAILABLE);
340         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
341         return location_ielement_get_poi(LOCATION_IELEMENT(poi), radius, keyword, poi_info);
342 }
343
344 EXPORT_API int
345 location_get_poi_from_address(LocationObject *obj,
346         const LocationAddress *address,
347         gdouble radius,
348         const gchar *keyword,
349         LocationPOIInfo **poi_info)
350 {
351         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
352         g_return_val_if_fail (poi, LOCATION_ERROR_NOT_AVAILABLE);
353         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
354         return location_ielement_get_poi_from_address(LOCATION_IELEMENT(poi), address, radius, keyword, poi_info);
355 }
356
357 EXPORT_API int
358 location_get_poi_from_position(LocationObject *obj,
359         const LocationPosition *position,
360         gdouble radius,
361         const gchar *keyword,
362         LocationPOIInfo **poi_info)
363 {
364         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
365         g_return_val_if_fail (poi, LOCATION_ERROR_NOT_AVAILABLE);
366         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
367         return location_ielement_get_poi_from_position(LOCATION_IELEMENT(poi), position, radius, keyword, poi_info);
368 }
369
370 EXPORT_API int
371 location_get_poi_async (LocationObject *obj,
372         gdouble radius,
373         const gchar *keyword,
374         LocationPOICB callback,
375         gpointer userdata)
376 {
377         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
378         g_return_val_if_fail (poi, LOCATION_ERROR_NOT_AVAILABLE);
379         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
380         return location_ielement_get_poi_async(LOCATION_IELEMENT(poi), radius, keyword, callback, userdata);
381 }
382
383 EXPORT_API int
384 location_get_poi_from_address_async (LocationObject *obj,
385         const LocationAddress *address,
386         gdouble radius,
387         const gchar *keyword,
388         LocationPOICB callback,
389         gpointer userdata)
390 {
391         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
392         g_return_val_if_fail (poi, LOCATION_ERROR_NOT_AVAILABLE);
393         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
394         return location_ielement_get_poi_from_address_async(LOCATION_IELEMENT(poi), address, radius, keyword, callback, userdata);
395 }
396
397 EXPORT_API int
398 location_get_poi_from_position_async (LocationObject *obj,
399         const LocationPosition *position,
400         gdouble radius,
401         const gchar*keyword,
402         LocationPOICB callback,
403         gpointer userdata)
404 {
405         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
406         g_return_val_if_fail (poi, LOCATION_ERROR_NOT_AVAILABLE);
407         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
408         return location_ielement_get_poi_from_position_async(LOCATION_IELEMENT(poi), position, radius, keyword, callback, userdata);
409 }
410
411 EXPORT_API int
412 location_send_command(const char *cmd)
413 {
414         g_return_val_if_fail (cmd, LOCATION_ERROR_PARAMETER);
415
416         return LOCATION_ERROR_NONE;
417 }
418