637aef5c1f00176bb44ade4218cf4746d09970c7
[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         g_return_val_if_fail (position, LOCATION_ERROR_PARAMETER);
184         g_return_val_if_fail (accuracy, LOCATION_ERROR_PARAMETER);
185         return location_ielement_get_position (LOCATION_IELEMENT(obj), position, accuracy);
186 }
187
188 EXPORT_API int
189 location_get_last_position (LocationObject *obj, LocationMethod method,
190         LocationPosition **position,
191         LocationAccuracy **accuracy)
192 {
193         g_return_val_if_fail (position, LOCATION_ERROR_PARAMETER);
194         g_return_val_if_fail (accuracy, LOCATION_ERROR_PARAMETER);
195
196         return LOCATION_ERROR_NONE;
197 }
198
199 EXPORT_API int
200 location_get_last_known_position (LocationObject *obj, LocationMethod method,
201         LocationLastPosition *last_position)
202 {
203         g_return_val_if_fail (last_position, LOCATION_ERROR_PARAMETER);
204
205         LocationMethod redefined_method = method;
206
207         if (!obj) redefined_method = LOCATION_METHOD_HYBRID;
208
209         if (redefined_method == LOCATION_METHOD_HYBRID) {
210                 int gps_timestamp = 0, wps_timestamp = 0, sps_timestamp = 0;
211                 int latest_timestamp = 0;
212
213                 redefined_method = LOCATION_METHOD_SPS;
214                 vconf_get_int(GPS_LAST_TIMESTAMP, &gps_timestamp);
215                 vconf_get_int(WPS_LAST_TIMESTAMP, &wps_timestamp);
216                 vconf_get_int(SPS_LAST_TIMESTAMP, &sps_timestamp);
217
218                 redefined_method = LOCATION_METHOD_SPS;
219                 latest_timestamp = sps_timestamp;
220                 if (gps_timestamp > sps_timestamp) {
221                         redefined_method = LOCATION_METHOD_GPS;
222                         latest_timestamp = gps_timestamp;
223                 }
224
225                 if (wps_timestamp > latest_timestamp) {
226                         redefined_method = LOCATION_METHOD_WPS;
227                 }
228                 
229         }
230
231         if (get_last_known_position (redefined_method, last_position)) {
232                 return LOCATION_ERROR_UNKNOWN;
233         }
234
235         return LOCATION_ERROR_NONE;
236 }
237
238 EXPORT_API int
239 location_get_satellite (LocationObject *obj, LocationSatellite **satellite)
240 {
241         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
242         g_return_val_if_fail (satellite, LOCATION_ERROR_PARAMETER);
243
244         return LOCATION_ERROR_NONE;;
245 }
246
247 EXPORT_API int
248 location_get_last_satellite (LocationObject *obj, LocationSatellite **satellite)
249 {
250         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
251         g_return_val_if_fail (satellite, LOCATION_ERROR_PARAMETER);
252
253         return LOCATION_ERROR_NONE;;
254 }
255
256 EXPORT_API int
257 location_get_position_from_address (LocationObject *obj,
258         const LocationAddress *address,
259         GList **position_list,
260         GList **accuracy_list)
261 {
262         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
263         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
264         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
265         return location_ielement_get_geocode (LOCATION_IELEMENT(geocode), address, position_list, accuracy_list);
266 }
267
268 EXPORT_API int
269 location_get_position_from_freeformed_address (LocationObject *obj,
270         const gchar *address,
271         GList **position_list,
272         GList **accuracy_list)
273 {
274         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
275         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
276         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
277         return location_ielement_get_geocode_freeform (LOCATION_IELEMENT(geocode), address, position_list, accuracy_list);
278 }
279
280 EXPORT_API int
281 location_get_velocity (LocationObject *obj,
282         LocationVelocity **velocity,
283         LocationAccuracy **accuracy)
284 {
285         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
286         g_return_val_if_fail (velocity, LOCATION_ERROR_PARAMETER);
287         g_return_val_if_fail (accuracy, LOCATION_ERROR_PARAMETER);
288
289         return location_ielement_get_velocity (LOCATION_IELEMENT(obj), velocity, accuracy);
290 }
291
292 EXPORT_API int
293 location_get_last_velocity (LocationObject *obj,
294         LocationVelocity **velocity,
295         LocationAccuracy **accuracy)
296 {
297         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
298         g_return_val_if_fail (velocity, LOCATION_ERROR_PARAMETER);
299         g_return_val_if_fail (accuracy, LOCATION_ERROR_PARAMETER);
300         return LOCATION_ERROR_NONE;
301 }
302
303 EXPORT_API int
304 location_get_address (LocationObject *obj,
305         LocationAddress **address,
306         LocationAccuracy **accuracy)
307 {
308         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
309         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
310         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
311
312         LocationPosition *position = NULL;
313         int ret = location_ielement_get_position(LOCATION_IELEMENT (obj), &position, accuracy);
314         if (LOCATION_ERROR_NONE != ret) return ret;
315         ret = location_ielement_get_reversegeocode (LOCATION_IELEMENT(geocode), position, address, accuracy);
316         location_position_free (position);
317         return ret;
318 }
319
320 EXPORT_API int
321 location_get_address_from_position (LocationObject *obj,
322         const LocationPosition *position,
323         LocationAddress **address,
324         LocationAccuracy **accuracy)
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 (LOCATION_IELEMENT(geocode), position, address, accuracy);
330 }
331
332 EXPORT_API int
333 location_get_position_from_address_async (LocationObject *obj,
334         const LocationAddress *address,
335         LocationPositionCB callback,
336         gpointer userdata)
337 {
338         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
339         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
340         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
341         return location_ielement_get_geocode_async (LOCATION_IELEMENT(geocode), address, callback, userdata);
342 }
343
344
345 EXPORT_API int
346 location_get_position_from_freeformed_address_async (LocationObject *obj,
347         const gchar *address,
348         LocationPositionCB callback,
349         gpointer userdata)
350 {
351         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
352         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
353         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
354         return location_ielement_get_geocode_freeform_async (LOCATION_IELEMENT(geocode), address, callback, userdata);
355 }
356
357 EXPORT_API int
358 location_get_address_async (LocationObject *obj,
359         LocationAddressCB callback,
360         gpointer userdata)
361 {
362         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
363         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
364         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
365
366         LocationPosition *position = NULL;
367         LocationAccuracy *acc = NULL;
368         int ret = location_ielement_get_position(LOCATION_IELEMENT(obj), &position, &acc);
369         if (LOCATION_ERROR_NONE != ret) return ret;
370         ret = location_ielement_get_reversegeocode_async (LOCATION_IELEMENT(geocode), position, callback, userdata);
371         location_position_free (position);
372         return ret;
373 }
374
375 EXPORT_API int
376 location_get_address_from_position_async (LocationObject *obj,
377         const LocationPosition *position,
378         LocationAddressCB callback,
379         gpointer userdata)
380 {
381         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
382         g_return_val_if_fail (geocode, LOCATION_ERROR_NOT_AVAILABLE);
383         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
384         return location_ielement_get_reversegeocode_async (LOCATION_IELEMENT(geocode), position, callback, userdata);
385 }
386
387 EXPORT_API int
388 location_get_poi (LocationObject *obj,
389         gdouble radius,
390         const gchar *keyword,
391         LocationPOIInfo **poi_info)
392 {
393         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
394         g_return_val_if_fail (poi, LOCATION_ERROR_NOT_AVAILABLE);
395         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
396         return location_ielement_get_poi(LOCATION_IELEMENT(poi), radius, keyword, poi_info);
397 }
398
399 EXPORT_API int
400 location_get_poi_from_address(LocationObject *obj,
401         const LocationAddress *address,
402         gdouble radius,
403         const gchar *keyword,
404         LocationPOIInfo **poi_info)
405 {
406         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
407         g_return_val_if_fail (poi, LOCATION_ERROR_NOT_AVAILABLE);
408         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
409         return location_ielement_get_poi_from_address(LOCATION_IELEMENT(poi), address, radius, keyword, poi_info);
410 }
411
412 EXPORT_API int
413 location_get_poi_from_position(LocationObject *obj,
414         const LocationPosition *position,
415         gdouble radius,
416         const gchar *keyword,
417         LocationPOIInfo **poi_info)
418 {
419         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
420         g_return_val_if_fail (poi, LOCATION_ERROR_NOT_AVAILABLE);
421         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
422         return location_ielement_get_poi_from_position(LOCATION_IELEMENT(poi), position, radius, keyword, poi_info);
423 }
424
425 EXPORT_API int
426 location_get_poi_async (LocationObject *obj,
427         gdouble radius,
428         const gchar *keyword,
429         LocationPOICB callback,
430         gpointer userdata)
431 {
432         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
433         g_return_val_if_fail (poi, LOCATION_ERROR_NOT_AVAILABLE);
434         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
435         return location_ielement_get_poi_async(LOCATION_IELEMENT(poi), radius, keyword, callback, userdata);
436 }
437
438 EXPORT_API int
439 location_get_poi_from_address_async (LocationObject *obj,
440         const LocationAddress *address,
441         gdouble radius,
442         const gchar *keyword,
443         LocationPOICB callback,
444         gpointer userdata)
445 {
446         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
447         g_return_val_if_fail (poi, LOCATION_ERROR_NOT_AVAILABLE);
448         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
449         return location_ielement_get_poi_from_address_async(LOCATION_IELEMENT(poi), address, radius, keyword, callback, userdata);
450 }
451
452 EXPORT_API int
453 location_get_poi_from_position_async (LocationObject *obj,
454         const LocationPosition *position,
455         gdouble radius,
456         const gchar*keyword,
457         LocationPOICB callback,
458         gpointer userdata)
459 {
460         g_return_val_if_fail (obj, LOCATION_ERROR_PARAMETER);
461         g_return_val_if_fail (poi, LOCATION_ERROR_NOT_AVAILABLE);
462         g_return_val_if_fail (is_connected_network(), LOCATION_ERROR_NETWORK_NOT_CONNECTED);
463         return location_ielement_get_poi_from_position_async(LOCATION_IELEMENT(poi), position, radius, keyword, callback, userdata);
464 }
465
466 EXPORT_API int
467 location_send_command(const char *cmd)
468 {
469         g_return_val_if_fail (cmd, LOCATION_ERROR_PARAMETER);
470
471         return LOCATION_ERROR_NONE;
472 }
473