1. Code synchronization with tizen_2.4
[platform/core/api/location-manager.git] / src / locations.c
1 /*
2  * Copyright (c) 2011-2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <system_info.h>
21 #include "locations.h"
22 #include "location_internal.h"
23
24 static location_setting_changed_s g_location_setting[LOCATIONS_METHOD_WPS + 1];
25
26 static location_method_e __convert_location_method_e(LocationMethod method)
27 {
28         location_method_e _method = LOCATIONS_METHOD_NONE;
29         switch (method) {
30                 case LOCATION_METHOD_HYBRID:
31                         _method = LOCATIONS_METHOD_HYBRID;
32                         break;
33                 case LOCATION_METHOD_GPS:
34                         _method = LOCATIONS_METHOD_GPS;
35                         break;
36                 case LOCATION_METHOD_WPS:
37                         _method = LOCATIONS_METHOD_WPS;
38                         break;
39                 case LOCATION_METHOD_NONE:
40                 default:
41                         break;
42         }
43         return _method;
44 }
45
46 static LocationMethod __convert_LocationMethod(location_method_e method)
47 {
48         LocationMethod _method = LOCATION_METHOD_NONE;
49         switch (method) {
50                 case LOCATIONS_METHOD_HYBRID:
51                         _method = LOCATION_METHOD_HYBRID;
52                         break;
53                 case LOCATIONS_METHOD_GPS:
54                         _method = LOCATION_METHOD_GPS;
55                         break;
56                 case LOCATIONS_METHOD_WPS:
57                         _method = LOCATION_METHOD_WPS;
58                         break;
59                 case LOCATIONS_METHOD_NONE:
60                 default:
61                         _method = LOCATION_METHOD_NONE;
62                         break;
63         }
64         return _method;
65 }
66
67 static void __cb_service_updated(GObject *self, guint type, gpointer data, gpointer velocity, gpointer accuracy, gpointer userdata)
68 {
69         LOCATIONS_LOGD("Callback function has been invoked. ");
70         location_manager_s *handle = (location_manager_s *) userdata;
71
72         if (type == SATELLITE_UPDATED && handle->user_cb[_LOCATIONS_EVENT_TYPE_SATELLITE]) {
73                 LocationSatellite *sat = (LocationSatellite *)data;
74                 LOCATIONS_LOGD("Current satellite information: timestamp : %d, number of active : %d, number of inview : %d",
75                                sat->timestamp, sat->num_of_sat_used, sat->num_of_sat_inview);
76                 ((gps_status_satellite_updated_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_SATELLITE])(sat->num_of_sat_used, sat->num_of_sat_inview,
77                         sat->timestamp, handle->user_data[_LOCATIONS_EVENT_TYPE_SATELLITE]);
78         } else if (type == DISTANCE_UPDATED && handle->user_cb[_LOCATIONS_EVENT_TYPE_DISTANCE]) {
79                 LocationPosition *pos = (LocationPosition *) data;
80                 LocationVelocity *vel = (LocationVelocity *) velocity;  /* current velocity */
81                 ((location_changed_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_DISTANCE])(0, pos->latitude, pos->longitude, pos->altitude,
82                                                                                         vel->speed, vel->direction, pos->timestamp, handle->user_data[_LOCATIONS_EVENT_TYPE_DISTANCE]);
83         } else {
84
85                 if (handle->user_cb[_LOCATIONS_EVENT_TYPE_POSITION] && (type & POSITION_UPDATED) != 0) {
86                         LocationPosition *pos = (LocationPosition *) data;
87                         ((location_position_updated_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_POSITION])(pos->latitude, pos->longitude, pos->altitude, pos->timestamp,
88                                 handle->user_data[_LOCATIONS_EVENT_TYPE_POSITION]);
89                 }
90
91                 if (handle->user_cb[_LOCATIONS_EVENT_TYPE_VELOCITY] && (type & VELOCITY_UPDATED) != 0) {
92                         LocationVelocity *vel = (LocationVelocity *) data;
93                         ((location_velocity_updated_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_VELOCITY])(vel->speed, vel->direction, vel->climb, vel->timestamp,
94                                 handle->user_data[_LOCATIONS_EVENT_TYPE_VELOCITY]);
95                 }
96
97                 if (handle->user_cb[_LOCATIONS_EVENT_TYPE_POS_VEL] && (type & LOCATION_CHANGED) != 0) {
98                         LocationPosition *pos = (LocationPosition *) data;
99                         LocationVelocity *vel = (LocationVelocity *) velocity;
100                         LocationAccuracy *acc = (LocationAccuracy *) accuracy;
101                         ((location_changed_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_POS_VEL])(pos->latitude, pos->longitude, pos->altitude,
102                                                                                                vel->speed, vel->direction, acc->horizontal_accuracy, pos->timestamp, handle->user_data[_LOCATIONS_EVENT_TYPE_POS_VEL]);
103                 }
104         }
105 }
106
107 static void __cb_location_updated(GObject *self, int error, gpointer position, gpointer velocity, gpointer accuracy, gpointer userdata)
108 {
109         LOCATIONS_LOGD("Callback function has been invoked. ");
110         int converted_err = __convert_error_code(error);
111         location_manager_s *handle = (location_manager_s *) userdata;
112         LocationPosition *pos = (LocationPosition *) position;
113         LocationVelocity *vel = (LocationVelocity *) velocity;
114
115         LOCATIONS_LOGD("Current position: timestamp : %d", pos->timestamp);
116         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_LOCATION]) {
117                 ((location_updated_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_LOCATION])(converted_err, pos->latitude, pos->longitude, pos->altitude,
118                                                                                         pos->timestamp, vel->speed, vel->climb, vel->direction, handle->user_data[_LOCATIONS_EVENT_TYPE_LOCATION]);
119         }
120 }
121
122 static void __cb_service_enabled(GObject *self, guint status, gpointer userdata)
123 {
124         LOCATIONS_LOGD("Callback function has been invoked. ");
125         location_manager_s *handle = (location_manager_s *) userdata;
126         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_SERVICE_STATE]) {
127                 ((location_service_state_changed_cb)
128                  handle->user_cb[_LOCATIONS_EVENT_TYPE_SERVICE_STATE])(LOCATIONS_SERVICE_ENABLED,
129                                                                        handle->user_data[_LOCATIONS_EVENT_TYPE_SERVICE_STATE]);
130         }
131 }
132
133 static void __cb_service_disabled(GObject *self, guint status, gpointer userdata)
134 {
135         LOCATIONS_LOGD("Callback function has been invoked. ");
136         location_manager_s *handle = (location_manager_s *) userdata;
137         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_SERVICE_STATE])
138                 ((location_service_state_changed_cb)
139                  handle->user_cb[_LOCATIONS_EVENT_TYPE_SERVICE_STATE])(LOCATIONS_SERVICE_DISABLED,
140                                                                        handle->user_data[_LOCATIONS_EVENT_TYPE_SERVICE_STATE]);
141 }
142
143 static int __compare_position(gconstpointer a, gconstpointer b)
144 {
145         if (location_position_equal((LocationPosition *) a, (LocationPosition *)b) == TRUE) {
146                 return 0;
147         }
148
149         return -1;
150 }
151
152 static int __boundary_compare(LocationBoundary *bound1, LocationBoundary *bound2)
153 {
154         int ret = -1;
155
156         if (bound1->type == bound2->type) {
157                 switch (bound1->type) {
158                         case LOCATION_BOUNDARY_CIRCLE:
159                                 if (location_position_equal(bound1->circle.center, bound2->circle.center) && bound1->circle.radius == bound2->circle.radius) {
160                                         ret = 0;
161                                 }
162                                 break;
163                         case LOCATION_BOUNDARY_RECT:
164                                 if (location_position_equal(bound1->rect.left_top, bound2->rect.left_top) && location_position_equal(bound1->rect.right_bottom, bound2->rect.right_bottom)) {
165                                         ret = 0;
166                                 }
167                                 break;
168                         case LOCATION_BOUNDARY_POLYGON: {
169                                         GList *boundary1_next = NULL;
170                                         GList *boundary2_start = NULL, *boundary2_prev = NULL, *boundary2_next = NULL;
171                                         if (g_list_length(bound1->polygon.position_list) != g_list_length(bound2->polygon.position_list)) {
172                                                 return -1;
173                                         }
174
175                                         boundary2_start = g_list_find_custom(bound2->polygon.position_list, g_list_nth_data(bound1->polygon.position_list, 0), (GCompareFunc) __compare_position);
176                                         if (boundary2_start == NULL) return -1;
177
178                                         boundary2_prev = g_list_previous(boundary2_start);
179                                         boundary2_next = g_list_next(boundary2_start);
180
181                                         if (boundary2_prev == NULL) boundary2_prev = g_list_last(bound2->polygon.position_list);
182                                         if (boundary2_next == NULL) boundary2_next = g_list_first(bound2->polygon.position_list);
183
184                                         boundary1_next = g_list_next(bound1->polygon.position_list);
185                                         if (boundary1_next != NULL && boundary2_prev != NULL &&
186                                             location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *)boundary2_prev->data) == TRUE) {
187                                                 boundary1_next = g_list_next(boundary1_next);
188                                                 while (boundary1_next) {
189                                                         boundary2_prev = g_list_previous(boundary2_prev);
190                                                         if (boundary2_prev == NULL) boundary2_prev = g_list_last(bound2->polygon.position_list);
191                                                         if (location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *) boundary2_prev->data) == FALSE) {
192                                                                 return -1;
193                                                         }
194                                                         boundary1_next = g_list_next(boundary1_next);
195                                                 }
196                                                 ret = 0;
197                                         } else if (boundary1_next != NULL && boundary2_next != NULL &&
198                                                    location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *)boundary2_next->data) == TRUE) {
199                                                 boundary1_next = g_list_next(boundary1_next);
200                                                 while (boundary1_next) {
201                                                         boundary2_next = g_list_next(boundary2_next);
202                                                         if (boundary2_next == NULL) boundary2_next = g_list_first(bound2->polygon.position_list);
203                                                         if (location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *) boundary2_next->data) == FALSE) {
204                                                                 return -1;
205                                                         }
206                                                         boundary1_next = g_list_next(boundary1_next);
207                                                 }
208                                                 ret = 0;
209                                         } else {
210                                                 return -1;
211                                         }
212                                         break;
213                                 }
214                         default:
215                                 break;
216                 }
217         }
218         return ret;
219 }
220
221 static void __cb_zone_in(GObject *self, gpointer boundary, gpointer position, gpointer accuracy, gpointer userdata)
222 {
223         LOCATIONS_LOGD("ENTER >>>");
224         location_manager_s *handle = (location_manager_s *) userdata;
225         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_BOUNDARY]) {
226                 LocationPosition *pos = (LocationPosition *) position;
227                 ((location_zone_changed_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_BOUNDARY])(LOCATIONS_BOUNDARY_IN,
228                                                                                              pos->latitude, pos->longitude,
229                                                                                              pos->altitude, pos->timestamp,
230                                                                                              handle->user_data
231                                                                                              [_LOCATIONS_EVENT_TYPE_BOUNDARY]);
232         }
233
234         location_bounds_s *bounds;
235         GList *bounds_list = g_list_first(handle->bounds_list);
236         while (bounds_list) {
237                 bounds = (location_bounds_s *)bounds_list->data;
238                 if (__boundary_compare(boundary, bounds->boundary) == 0) {
239                         LOCATIONS_LOGD("Find zone in boundary");
240                         if (bounds->user_cb) {
241                                 ((location_bounds_state_changed_cb) bounds->user_cb)(LOCATIONS_BOUNDARY_IN, bounds->user_data);
242                         }
243                         break;
244                 }
245                 bounds_list = g_list_next(bounds_list);
246         }
247         LOCATIONS_LOGD("EXIT <<<");
248 }
249
250 static void __cb_zone_out(GObject *self, gpointer boundary, gpointer position, gpointer accuracy, gpointer userdata)
251 {
252         LOCATIONS_LOGD("ENTER >>>");
253         location_manager_s *handle = (location_manager_s *) userdata;
254         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_BOUNDARY]) {
255                 LocationPosition *pos = (LocationPosition *) position;
256                 ((location_zone_changed_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_BOUNDARY])(LOCATIONS_BOUNDARY_OUT,
257                                                                                              pos->latitude, pos->longitude,
258                                                                                              pos->altitude, pos->timestamp,
259                                                                                              handle->user_data
260                                                                                              [_LOCATIONS_EVENT_TYPE_BOUNDARY]);
261         }
262
263         location_bounds_s *bounds;
264         GList *bounds_list = g_list_first(handle->bounds_list);
265         while (bounds_list) {
266                 bounds = (location_bounds_s *)bounds_list->data;
267                 if (__boundary_compare(boundary, bounds->boundary) == 0) {
268                         LOCATIONS_LOGD("Find zone out boundary");
269                         if (bounds->user_cb) {
270                                 ((location_bounds_state_changed_cb) bounds->user_cb)(LOCATIONS_BOUNDARY_OUT, bounds->user_data);
271                         }
272                         break;
273                 }
274                 bounds_list = g_list_next(bounds_list);
275         }
276         LOCATIONS_LOGD("EXIT <<<");
277 }
278
279 static void __foreach_boundary(LocationBoundary *boundary, void *user_data)
280 {
281         location_manager_s *handle = (location_manager_s *) user_data;
282
283         if (handle != NULL && boundary != NULL) {
284                 int ret = -1;
285                 location_bounds_h bounds;
286                 if (boundary->type == LOCATION_BOUNDARY_CIRCLE) {
287                         location_coords_s center;
288                         center.latitude = boundary->circle.center->latitude;
289                         center.longitude = boundary->circle.center->longitude;
290                         ret = location_bounds_create_circle(center, boundary->circle.radius, &bounds);
291                 } else if (boundary->type == LOCATION_BOUNDARY_RECT) {
292                         location_coords_s left_top;
293                         location_coords_s right_bottom;
294                         left_top.latitude = boundary->rect.left_top->latitude;
295                         left_top.longitude = boundary->rect.left_top->longitude;
296                         right_bottom.latitude = boundary->rect.right_bottom->latitude;
297                         right_bottom.longitude = boundary->rect.right_bottom->longitude;
298                         ret = location_bounds_create_rect(left_top, right_bottom, &bounds);
299                 } else if (boundary->type == LOCATION_BOUNDARY_POLYGON) {
300                         GList *list = boundary->polygon.position_list;
301                         int size = g_list_length(list);
302                         if (size > 0) {
303                                 location_coords_s coords[size];
304                                 int cnt = 0;
305                                 while (list) {
306                                         LocationPosition *pos = list->data;
307                                         coords[cnt].latitude = pos->latitude;
308                                         coords[cnt].longitude = pos->longitude;
309                                         list = g_list_next(list);
310                                         cnt++;
311                                 }
312                                 ret = location_bounds_create_polygon(coords, size, &bounds);
313                         }
314                 } else {
315                         LOCATIONS_LOGI("Invalid boundary type : %d", boundary->type);
316                 }
317
318                 if (ret != LOCATIONS_ERROR_NONE) {
319                         LOCATIONS_LOGI("Failed to create location_bounds : (0x%08x) ", ret);
320                 } else {
321                         if (handle->is_continue_foreach_bounds) {
322                                 handle->is_continue_foreach_bounds =
323                                     ((location_bounds_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS])(bounds,
324                                             handle->
325                                             user_data
326                                             [_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS]);
327                         }
328                         location_bounds_destroy(bounds);
329                 }
330         } else {
331                 LOCATIONS_LOGD("__foreach_boundary() has been failed");
332         }
333 }
334
335 static void __setting_changed_cb(LocationMethod method, gboolean enable, void *user_data)
336 {
337         LOCATIONS_LOGD("__setting_changed_cb method [%d]", method);
338         location_method_e _method = __convert_location_method_e(method);
339         location_setting_changed_s *_setting_changed = (location_setting_changed_s *)user_data;
340         if (_setting_changed == NULL) {
341                 LOCATIONS_LOGE("Invaild userdata\n");
342                 return;
343         }
344
345         if (_setting_changed[_method].callback != NULL) {
346                 _setting_changed[_method].callback(_method, enable, _setting_changed[_method].user_data);
347         }
348 }
349
350 /*/////////////////////////////////////// */
351 /* Location Manager */
352 /*////////////////////////////////////// */
353
354 EXPORT_API bool location_manager_is_supported_method(location_method_e method)
355 {
356         LOCATIONS_LOGD("location_manager_is_supported_method %d", method);
357         if (__is_location_supported() == LOCATIONS_ERROR_NOT_SUPPORTED) {
358                 set_last_result(LOCATIONS_ERROR_NOT_SUPPORTED);
359                 return false;
360         }
361
362         LocationMethod _method = __convert_LocationMethod(method);
363         if (_method == LOCATION_METHOD_NONE) {
364                 LOCATIONS_LOGE("Not supported method [%d]", method);
365                 set_last_result(LOCATIONS_ERROR_INCORRECT_METHOD);
366                 return false;
367         }
368
369         set_last_result(LOCATIONS_ERROR_NONE);
370         return location_is_supported_method(_method);
371 }
372
373 EXPORT_API int location_manager_is_enabled_method(location_method_e method, bool *enable)
374 {
375         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
376
377         if (LOCATIONS_METHOD_HYBRID > method || LOCATIONS_METHOD_WPS < method) {
378                 LOCATIONS_LOGE("Not supported method [%d]", method);
379                 return LOCATIONS_ERROR_INCORRECT_METHOD;
380         }
381
382         LOCATIONS_LOGD("location_manager_is_enabled_method %d", method);
383         LOCATIONS_NULL_ARG_CHECK(enable);
384         int is_enabled_val = -1;
385         LocationMethod _method = __convert_LocationMethod(method);
386         int ret = location_is_enabled_method(_method, &is_enabled_val);
387         if (ret != LOCATION_ERROR_NONE) {
388                 if (ret == LOCATION_ERROR_NOT_SUPPORTED)
389                         return LOCATIONS_ERROR_INCORRECT_METHOD;
390                 return __convert_error_code(ret);
391         }
392         if (is_enabled_val == -1)
393                 return TIZEN_ERROR_PERMISSION_DENIED;
394
395         *enable = (is_enabled_val == 0) ? FALSE : TRUE;
396         return LOCATIONS_ERROR_NONE;
397 }
398
399 EXPORT_API int location_manager_enable_method(const location_method_e method, const bool enable)
400 {
401         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
402
403         if (LOCATIONS_METHOD_HYBRID > method || LOCATIONS_METHOD_WPS < method) {
404                 LOCATIONS_LOGE("Not supported method [%d]", method);
405                 return LOCATIONS_ERROR_INCORRECT_METHOD;
406         }
407
408         LOCATIONS_LOGD("method: %d, enable: %d", method, enable);
409
410         LocationMethod _method = __convert_LocationMethod(method);
411
412         int ret = location_enable_method(_method, enable);
413         return __convert_error_code(ret);
414 }
415
416
417 EXPORT_API int location_manager_create(location_method_e method, location_manager_h *manager)
418 {
419         LOCATIONS_LOGD("location_manager_create (method : %d)", method);
420         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
421
422         LocationMethod _method = __convert_LocationMethod(method);
423         if (_method == LOCATION_METHOD_NONE) {
424                 LOCATIONS_LOGE("LOCATIONS_ERROR_NOT_SUPPORTED(0x%08x) : fail to location_init", LOCATIONS_ERROR_NOT_SUPPORTED);
425                 return LOCATIONS_ERROR_NOT_SUPPORTED;
426         }
427         if (!location_is_supported_method(_method)) {
428                 LOCATIONS_LOGE("LOCATIONS_ERROR_NOT_SUPPORTED(0x%08x) : fail to location_is_supported_method", LOCATIONS_ERROR_NOT_SUPPORTED);
429                 return LOCATIONS_ERROR_NOT_SUPPORTED;
430         }
431
432         /*It is moved here becasue of TCS. */
433         LOCATIONS_NULL_ARG_CHECK(manager);
434
435         if (location_init() != LOCATION_ERROR_NONE) {
436                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : fail to location_init", LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
437                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
438         }
439
440         location_manager_s *handle = (location_manager_s *) malloc(sizeof(location_manager_s));
441         if (handle == NULL) {
442                 LOCATIONS_LOGE("OUT_OF_MEMORY(0x%08x)", LOCATIONS_ERROR_OUT_OF_MEMORY);
443                 return LOCATIONS_ERROR_OUT_OF_MEMORY;
444         }
445
446         memset(handle, 0, sizeof(location_manager_s));
447
448         handle->object = location_new(_method);
449         if (handle->object == NULL) {
450                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : fail to location_new", LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
451                 free(handle);
452                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
453         }
454         handle->method = method;
455         handle->is_continue_foreach_bounds = TRUE;
456         handle->bounds_list = NULL;
457
458         if (!handle->sig_id[_LOCATION_SIGNAL_SERVICE_ENABLED])
459                 handle->sig_id[_LOCATION_SIGNAL_SERVICE_ENABLED] = g_signal_connect(handle->object, "service-enabled", G_CALLBACK(__cb_service_enabled), handle);
460
461         if (!handle->sig_id[_LOCATION_SIGNAL_SERVICE_DISABLED])
462                 handle->sig_id[_LOCATION_SIGNAL_SERVICE_DISABLED] = g_signal_connect(handle->object, "service-disabled", G_CALLBACK(__cb_service_disabled), handle);
463
464         *manager = (location_manager_h) handle;
465         return LOCATIONS_ERROR_NONE;
466 }
467
468 EXPORT_API int location_manager_destroy(location_manager_h manager)
469 {
470         LOCATIONS_LOGD("location_manager_destroy");
471         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
472         LOCATIONS_NULL_ARG_CHECK(manager);
473         location_manager_s *handle = (location_manager_s *) manager;
474
475         if (handle->sig_id[_LOCATION_SIGNAL_SERVICE_ENABLED]) {
476                 g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_SERVICE_ENABLED]);
477                 handle->sig_id[_LOCATION_SIGNAL_SERVICE_ENABLED] = 0;
478         }
479
480         if (handle->sig_id[_LOCATION_SIGNAL_SERVICE_DISABLED]) {
481                 g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_SERVICE_DISABLED]);
482                 handle->sig_id[_LOCATION_SIGNAL_SERVICE_DISABLED] = 0;
483         }
484
485         int ret = location_free(handle->object);
486         if (ret != LOCATIONS_ERROR_NONE) {
487                 return __convert_error_code(ret);
488         }
489         free(handle);
490         return LOCATIONS_ERROR_NONE;
491 }
492
493 EXPORT_API int location_manager_start(location_manager_h manager)
494 {
495         LOCATIONS_LOGD("location_manager_start");
496         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
497         LOCATIONS_NULL_ARG_CHECK(manager);
498         location_manager_s *handle = (location_manager_s *) manager;
499
500         if (!handle->sig_id[_LOCATION_SIGNAL_SERVICE_UPDATED])
501                 handle->sig_id[_LOCATION_SIGNAL_SERVICE_UPDATED] = g_signal_connect(handle->object, "service-updated", G_CALLBACK(__cb_service_updated), handle);
502
503         if (LOCATIONS_METHOD_HYBRID <= handle->method && LOCATIONS_METHOD_WPS >= handle->method) {
504                 if (!handle->sig_id[_LOCATION_SIGNAL_ZONE_IN])
505                         handle->sig_id[_LOCATION_SIGNAL_ZONE_IN] = g_signal_connect(handle->object, "zone-in", G_CALLBACK(__cb_zone_in), handle);
506
507                 if (!handle->sig_id[_LOCATION_SIGNAL_ZONE_OUT])
508                         handle->sig_id[_LOCATION_SIGNAL_ZONE_OUT] = g_signal_connect(handle->object, "zone-out", G_CALLBACK(__cb_zone_out), handle);
509         } else {
510                 LOCATIONS_LOGI("This method [%d] is not supported zone-in, zone-out signal.", handle->method);
511         }
512
513         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_SATELLITE] != NULL) {
514                 LOCATIONS_LOGI("Satellite update_cb is set");
515                 location_set_option(handle->object, "USE_SV");
516         }
517
518         int ret = location_start(handle->object);
519         if (ret != LOCATION_ERROR_NONE) {
520                 return __convert_error_code(ret);
521         }
522         return LOCATIONS_ERROR_NONE;
523 }
524
525 EXPORT_API int location_manager_request_single_location(location_manager_h manager, int timeout, location_updated_cb callback, void *user_data)
526 {
527         LOCATIONS_LOGD("location_manager_request_single_location");
528         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
529         LOCATIONS_NULL_ARG_CHECK(manager);
530         LOCATIONS_NULL_ARG_CHECK(callback);
531         if (timeout <= 0 || timeout > 120) {
532                 LOCATIONS_LOGE("timeout scope is incorrect(1~120) [%d]", timeout);
533                 return LOCATIONS_ERROR_INVALID_PARAMETER;
534         }
535
536         location_manager_s *handle = (location_manager_s *) manager;
537         int ret = LOCATIONS_ERROR_NONE;
538
539         if (!handle->sig_id[_LOCATION_SIGNAL_LOCATION_UPDATED])
540                 handle->sig_id[_LOCATION_SIGNAL_LOCATION_UPDATED] = g_signal_connect(handle->object, "location-updated", G_CALLBACK(__cb_location_updated), handle);
541
542         ret = __set_callback(_LOCATIONS_EVENT_TYPE_LOCATION, manager, callback, user_data);
543         if (ret != LOCATIONS_ERROR_NONE) {
544                 return ret;
545         }
546
547         ret = location_request_single_location(handle->object, timeout);
548         if (ret != LOCATION_ERROR_NONE) {
549                 return __convert_error_code(ret);
550         }
551         return LOCATIONS_ERROR_NONE;
552 }
553
554 EXPORT_API int location_manager_stop(location_manager_h manager)
555 {
556         LOCATIONS_LOGD("location_manager_stop");
557         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
558         LOCATIONS_NULL_ARG_CHECK(manager);
559
560         location_manager_s *handle = (location_manager_s *) manager;
561
562         if (handle->sig_id[_LOCATION_SIGNAL_SERVICE_UPDATED]) {
563                 g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_SERVICE_UPDATED]);
564                 handle->sig_id[_LOCATION_SIGNAL_SERVICE_UPDATED] = 0;
565         }
566
567         if (LOCATIONS_METHOD_HYBRID <= handle->method && LOCATIONS_METHOD_WPS >= handle->method) {
568                 if (handle->sig_id[_LOCATION_SIGNAL_ZONE_IN]) {
569                         g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_ZONE_IN]);
570                         handle->sig_id[_LOCATION_SIGNAL_ZONE_IN] = 0;
571                 }
572
573                 if (handle->sig_id[_LOCATION_SIGNAL_ZONE_OUT]) {
574                         g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_ZONE_OUT]);
575                         handle->sig_id[_LOCATION_SIGNAL_ZONE_OUT] = 0;
576                 }
577         }
578
579         int ret = location_stop(handle->object);
580         if (ret != LOCATION_ERROR_NONE) {
581                 return __convert_error_code(ret);
582         }
583         return LOCATIONS_ERROR_NONE;
584 }
585
586 EXPORT_API int location_manager_add_boundary(location_manager_h manager, location_bounds_h bounds)
587 {
588         LOCATIONS_LOGD("location_manager_add_boundary");
589         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
590         LOCATIONS_NULL_ARG_CHECK(manager);
591         LOCATIONS_NULL_ARG_CHECK(bounds);
592
593         location_manager_s *handle = (location_manager_s *) manager;
594         location_bounds_s *bound_handle = (location_bounds_s *) bounds;
595         int ret = location_boundary_add(handle->object, bound_handle->boundary);
596         if (ret != LOCATION_ERROR_NONE) {
597                 return __convert_error_code(ret);
598         }
599         bound_handle->is_added = TRUE;
600         handle->bounds_list = g_list_append(handle->bounds_list, bound_handle);
601         return LOCATIONS_ERROR_NONE;
602 }
603
604 EXPORT_API int location_manager_remove_boundary(location_manager_h manager, location_bounds_h bounds)
605 {
606         LOCATIONS_LOGD("location_manager_remove_boundary");
607         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
608         LOCATIONS_NULL_ARG_CHECK(manager);
609         LOCATIONS_NULL_ARG_CHECK(bounds);
610
611         location_manager_s *handle = (location_manager_s *) manager;
612         location_bounds_s *bound_handle = (location_bounds_s *) bounds;
613         int ret = location_boundary_remove(handle->object, bound_handle->boundary);
614         if (ret != LOCATION_ERROR_NONE) {
615                 return __convert_error_code(ret);
616         }
617         handle->bounds_list = g_list_remove(handle->bounds_list, bound_handle);
618         bound_handle->is_added = FALSE;
619         return LOCATIONS_ERROR_NONE;
620 }
621
622 EXPORT_API int location_manager_foreach_boundary(location_manager_h manager, location_bounds_cb callback, void *user_data)
623 {
624         LOCATIONS_LOGD("location_manager_foreach_boundary");
625         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
626         LOCATIONS_NULL_ARG_CHECK(manager);
627         LOCATIONS_NULL_ARG_CHECK(callback);
628
629         location_manager_s *handle = (location_manager_s *) manager;
630         handle->user_cb[_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS] = callback;
631         handle->user_data[_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS] = user_data;
632         handle->is_continue_foreach_bounds = TRUE;
633         int ret = location_boundary_foreach(handle->object, __foreach_boundary, handle);
634         if (ret != LOCATION_ERROR_NONE) {
635                 return __convert_error_code(ret);
636         }
637         return LOCATIONS_ERROR_NONE;
638 }
639
640 EXPORT_API int location_manager_get_method(location_manager_h manager, location_method_e *method)
641 {
642         LOCATIONS_LOGD("location_manager_get_method %d", method);
643         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
644         LOCATIONS_NULL_ARG_CHECK(manager);
645         LOCATIONS_NULL_ARG_CHECK(method);
646
647         location_manager_s *handle = (location_manager_s *) manager;
648         LocationMethod _method = LOCATION_METHOD_NONE;
649         g_object_get(handle->object, "method", &_method, NULL);
650         switch (_method) {
651                 case LOCATION_METHOD_NONE:
652                         *method = LOCATIONS_METHOD_NONE;
653                         break;
654                 case LOCATION_METHOD_HYBRID:
655                         *method = LOCATIONS_METHOD_HYBRID;
656                         break;
657                 case LOCATION_METHOD_GPS:
658                         *method = LOCATIONS_METHOD_GPS;
659                         break;
660                 case LOCATION_METHOD_WPS:
661                         *method = LOCATIONS_METHOD_WPS;
662                         break;
663                 default: {
664                                 LOCATIONS_LOGE("LOCATIONS_ERROR_INVALID_PARAMETER(0x%08x) : Out of range (location_method_e) - method : %d ",
665                                                LOCATIONS_ERROR_INVALID_PARAMETER, method);
666                                 return LOCATIONS_ERROR_INVALID_PARAMETER;
667                         }
668         }
669         /**method = handle->method; */
670         return LOCATIONS_ERROR_NONE;
671 }
672
673 EXPORT_API int location_manager_get_position(location_manager_h manager, double *altitude, double *latitude, double *longitude,
674                                              time_t *timestamp)
675 {
676         LOCATIONS_LOGD("location_manager_get_position");
677         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
678         LOCATIONS_NULL_ARG_CHECK(manager);
679         LOCATIONS_NULL_ARG_CHECK(altitude);
680         LOCATIONS_NULL_ARG_CHECK(latitude);
681         LOCATIONS_NULL_ARG_CHECK(longitude);
682         LOCATIONS_NULL_ARG_CHECK(timestamp);
683
684         location_manager_s *handle = (location_manager_s *) manager;
685         int ret;
686         LocationPosition *pos = NULL;
687         LocationAccuracy *acc = NULL;
688         ret = location_get_position(handle->object, &pos, &acc);
689         if (ret != LOCATION_ERROR_NONE) {
690                 return __convert_error_code(ret);
691         }
692
693         if (pos->status == LOCATION_STATUS_NO_FIX) {
694                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
695         } else {
696                 *latitude = pos->latitude;
697                 *longitude = pos->longitude;
698                 *altitude = pos->altitude;
699         }
700         *timestamp = pos->timestamp;
701         location_position_free(pos);
702         location_accuracy_free(acc);
703         return LOCATIONS_ERROR_NONE;
704 }
705
706 EXPORT_API int location_manager_get_location(location_manager_h manager, double *altitude, double *latitude, double *longitude, double *climb, double *direction, double *speed, location_accuracy_level_e *level, double *horizontal, double *vertical, time_t *timestamp)
707 {
708         LOCATIONS_LOGD("location_manager_get_location");
709         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
710         LOCATIONS_NULL_ARG_CHECK(manager);
711         LOCATIONS_NULL_ARG_CHECK(altitude);
712         LOCATIONS_NULL_ARG_CHECK(latitude);
713         LOCATIONS_NULL_ARG_CHECK(longitude);
714         LOCATIONS_NULL_ARG_CHECK(climb);
715         LOCATIONS_NULL_ARG_CHECK(direction);
716         LOCATIONS_NULL_ARG_CHECK(speed);
717         LOCATIONS_NULL_ARG_CHECK(level);
718         LOCATIONS_NULL_ARG_CHECK(horizontal);
719         LOCATIONS_NULL_ARG_CHECK(vertical);
720         LOCATIONS_NULL_ARG_CHECK(timestamp);
721
722         location_manager_s *handle = (location_manager_s *) manager;
723         int ret;
724         LocationPosition *pos = NULL;
725         LocationVelocity *vel = NULL;
726         LocationAccuracy *acc = NULL;
727         ret = location_get_position_ext(handle->object, &pos, &vel, &acc);
728         if (ret != LOCATION_ERROR_NONE) {
729                 return __convert_error_code(ret);
730         }
731
732         if (pos->status == LOCATION_STATUS_NO_FIX) {
733                 return __convert_error_code(LOCATION_ERROR_NOT_AVAILABLE);
734         } else {
735                 *latitude = pos->latitude;
736                 *longitude = pos->longitude;
737                 *altitude = pos->altitude;
738         }
739         *timestamp = pos->timestamp;
740         *climb = vel->climb;
741         *direction = vel->direction;
742         *speed = vel->speed;
743         *level = acc->level;
744         *horizontal = acc->horizontal_accuracy;
745         *vertical = acc->vertical_accuracy;
746
747         location_position_free(pos);
748         location_velocity_free(vel);
749         location_accuracy_free(acc);
750         return LOCATIONS_ERROR_NONE;
751 }
752
753 EXPORT_API int location_manager_get_velocity(location_manager_h manager, double *climb, double *direction, double *speed, time_t *timestamp)
754 {
755         LOCATIONS_LOGD("location_manager_get_velocity");
756         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
757         LOCATIONS_NULL_ARG_CHECK(manager);
758         LOCATIONS_NULL_ARG_CHECK(climb);
759         LOCATIONS_NULL_ARG_CHECK(direction);
760         LOCATIONS_NULL_ARG_CHECK(speed);
761         LOCATIONS_NULL_ARG_CHECK(timestamp);
762
763         location_manager_s *handle = (location_manager_s *) manager;
764         int ret;
765         LocationVelocity *vel = NULL;
766         LocationAccuracy *acc = NULL;
767         ret = location_get_velocity(handle->object, &vel, &acc);
768         if (ret != LOCATION_ERROR_NONE) {
769                 return __convert_error_code(ret);
770         }
771
772         *climb = vel->climb;
773         *direction = vel->direction;
774         *speed = vel->speed;
775         *timestamp = vel->timestamp;
776         location_velocity_free(vel);
777         location_accuracy_free(acc);
778         return LOCATIONS_ERROR_NONE;
779 }
780
781 EXPORT_API int location_manager_get_accuracy(location_manager_h manager, location_accuracy_level_e *level, double *horizontal,
782                                              double *vertical)
783 {
784         LOCATIONS_LOGD("location_manager_get_accuracy");
785         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
786         LOCATIONS_NULL_ARG_CHECK(manager);
787         LOCATIONS_NULL_ARG_CHECK(level);
788         LOCATIONS_NULL_ARG_CHECK(horizontal);
789         LOCATIONS_NULL_ARG_CHECK(vertical);
790         location_manager_s *handle = (location_manager_s *) manager;
791
792         int ret;
793         LocationPosition *pos = NULL;
794         LocationAccuracy *acc = NULL;
795         ret = location_get_position(handle->object, &pos, &acc);
796         if (ret != LOCATION_ERROR_NONE) {
797                 return __convert_error_code(ret);
798         }
799
800         if (acc == NULL) {
801                 return __convert_error_code(LOCATION_ERROR_NOT_AVAILABLE);
802         }
803
804         *level = acc->level;
805         *horizontal = acc->horizontal_accuracy;
806         *vertical = acc->vertical_accuracy;
807         location_position_free(pos);
808         location_accuracy_free(acc);
809         return LOCATIONS_ERROR_NONE;
810 }
811
812 EXPORT_API int location_manager_get_last_position(location_manager_h manager, double *altitude, double *latitude, double *longitude,
813                                                   time_t *timestamp)
814 {
815         LOCATIONS_LOGD("location_manager_get_last_position");
816         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
817         LOCATIONS_NULL_ARG_CHECK(manager);
818         LOCATIONS_NULL_ARG_CHECK(altitude);
819         LOCATIONS_NULL_ARG_CHECK(latitude);
820         LOCATIONS_NULL_ARG_CHECK(longitude);
821         LOCATIONS_NULL_ARG_CHECK(timestamp);
822
823         location_manager_s *handle = (location_manager_s *) manager;
824
825         int ret;
826         LocationPosition *last_pos = NULL;
827         LocationAccuracy *last_acc = NULL;
828         ret = location_get_last_position(handle->object, &last_pos, &last_acc);
829         if (ret != LOCATION_ERROR_NONE) {
830                 return __convert_error_code(ret);
831         }
832
833         if (last_pos->status == LOCATION_STATUS_NO_FIX) {
834                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
835         } else {
836                 *latitude = last_pos->latitude;
837                 *longitude = last_pos->longitude;
838                 *altitude = last_pos->altitude;
839         }
840         *timestamp = last_pos->timestamp;
841         location_position_free(last_pos);
842         location_accuracy_free(last_acc);
843         return LOCATIONS_ERROR_NONE;
844 }
845
846 EXPORT_API int location_manager_get_last_location(location_manager_h manager, double *altitude, double *latitude, double *longitude, double *climb, double *direction, double *speed, location_accuracy_level_e *level, double *horizontal, double *vertical, time_t *timestamp)
847 {
848         LOCATIONS_LOGD("location_manager_get_last_location");
849         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
850         LOCATIONS_NULL_ARG_CHECK(manager);
851         LOCATIONS_NULL_ARG_CHECK(altitude);
852         LOCATIONS_NULL_ARG_CHECK(latitude);
853         LOCATIONS_NULL_ARG_CHECK(longitude);
854         LOCATIONS_NULL_ARG_CHECK(climb);
855         LOCATIONS_NULL_ARG_CHECK(direction);
856         LOCATIONS_NULL_ARG_CHECK(speed);
857         LOCATIONS_NULL_ARG_CHECK(level);
858         LOCATIONS_NULL_ARG_CHECK(horizontal);
859         LOCATIONS_NULL_ARG_CHECK(vertical);
860         LOCATIONS_NULL_ARG_CHECK(timestamp);
861
862         location_manager_s *handle = (location_manager_s *) manager;
863
864         int ret;
865         LocationPosition *last_pos = NULL;
866         LocationVelocity *last_vel = NULL;
867         LocationAccuracy *last_acc = NULL;
868         ret = location_get_last_position_ext(handle->object, &last_pos, &last_vel, &last_acc);
869         if (ret != LOCATION_ERROR_NONE) {
870                 return __convert_error_code(ret);
871         }
872
873         if (last_pos->status == LOCATION_STATUS_NO_FIX) {
874                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
875         } else {
876                 *latitude = last_pos->latitude;
877                 *longitude = last_pos->longitude;
878                 *altitude = last_pos->altitude;
879         }
880         *timestamp = last_pos->timestamp;
881         *climb = last_vel->climb;
882         *direction = last_vel->direction;
883         *speed = last_vel->speed;
884         *level = last_acc->level;
885         *horizontal = last_acc->horizontal_accuracy;
886         *vertical = last_acc->vertical_accuracy;
887         location_position_free(last_pos);
888         location_velocity_free(last_vel);
889         location_accuracy_free(last_acc);
890         return LOCATIONS_ERROR_NONE;
891 }
892
893 EXPORT_API int location_manager_get_last_velocity(location_manager_h manager, double *climb, double *direction, double *speed, time_t *timestamp)
894 {
895         LOCATIONS_LOGD("location_manager_get_last_velocity");
896         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
897         LOCATIONS_NULL_ARG_CHECK(manager);
898         LOCATIONS_NULL_ARG_CHECK(climb);
899         LOCATIONS_NULL_ARG_CHECK(direction);
900         LOCATIONS_NULL_ARG_CHECK(speed);
901         LOCATIONS_NULL_ARG_CHECK(timestamp);
902
903         location_manager_s *handle = (location_manager_s *) manager;
904
905         int ret;
906         LocationVelocity *last_vel = NULL;
907         LocationAccuracy *last_acc = NULL;
908         ret = location_get_last_velocity(handle->object, &last_vel, &last_acc);
909         if (ret != LOCATION_ERROR_NONE) {
910                 return __convert_error_code(ret);
911         }
912
913         *climb = last_vel->climb;
914         *direction = last_vel->direction;
915         *speed = last_vel->speed;
916         *timestamp = last_vel->timestamp;
917         location_velocity_free(last_vel);
918         location_accuracy_free(last_acc);
919         return LOCATIONS_ERROR_NONE;
920 }
921
922 EXPORT_API int location_manager_get_last_accuracy(location_manager_h manager, location_accuracy_level_e *level, double *horizontal,
923                                                   double *vertical)
924 {
925         LOCATIONS_LOGD("location_manager_get_last_accuracy");
926         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
927         LOCATIONS_NULL_ARG_CHECK(manager);
928         LOCATIONS_NULL_ARG_CHECK(level);
929         LOCATIONS_NULL_ARG_CHECK(horizontal);
930         LOCATIONS_NULL_ARG_CHECK(vertical);
931         location_manager_s *handle = (location_manager_s *) manager;
932
933         int ret;
934         LocationPosition *last_pos = NULL;
935         LocationAccuracy *last_acc = NULL;
936         ret = location_get_last_position(handle->object, &last_pos, &last_acc);
937         if (ret != LOCATION_ERROR_NONE) {
938                 return __convert_error_code(ret);
939         }
940
941         *level = last_acc->level;
942         *horizontal = last_acc->horizontal_accuracy;
943         *vertical = last_acc->vertical_accuracy;
944         location_position_free(last_pos);
945         location_accuracy_free(last_acc);
946         return LOCATIONS_ERROR_NONE;
947 }
948
949 EXPORT_API int location_manager_get_accessibility_state(location_accessibility_state_e *state)
950 {
951         LOCATIONS_LOGD("location_manager_get_accessibility_state");
952         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
953         LOCATIONS_NULL_ARG_CHECK(state);
954
955         int ret = LOCATION_ERROR_NONE;
956         LocationAccessState auth = LOCATION_ACCESS_NONE;
957         ret = location_get_accessibility_state(&auth);
958         if (ret != LOCATION_ERROR_NONE) {
959                 *state = LOCATIONS_ACCESS_STATE_NONE;
960                 return __convert_error_code(ret);
961         }
962
963         switch (auth) {
964                 case LOCATION_ACCESS_DENIED:
965                         *state = LOCATIONS_ACCESS_STATE_DENIED;
966                         break;
967                 case LOCATION_ACCESS_ALLOWED:
968                         *state = LOCATIONS_ACCESS_STATE_ALLOWED;
969                         break;
970                 case LOCATION_ACCESS_NONE:
971                 default:
972                         *state = LOCATIONS_ACCESS_STATE_NONE;
973                         break;
974         }
975
976         return LOCATIONS_ERROR_NONE;
977 }
978
979 EXPORT_API int location_manager_set_distance_based_location_changed_cb(location_manager_h manager, location_changed_cb callback, int interval, double distance, void *user_data)
980 {
981         LOCATIONS_LOGD("location_manager_set_distance_updated_cb");
982         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
983         LOCATIONS_CHECK_CONDITION(interval >= 1 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
984         LOCATIONS_CHECK_CONDITION(distance > 0 && distance <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
985         LOCATIONS_NULL_ARG_CHECK(manager);
986         location_manager_s *handle = (location_manager_s *) manager;
987         g_object_set(handle->object, "min-interval", interval, NULL);
988         g_object_set(handle->object, "min-distance", distance, NULL);
989         return __set_callback(_LOCATIONS_EVENT_TYPE_DISTANCE, manager, callback, user_data);
990 }
991
992 EXPORT_API int location_manager_unset_distance_based_location_changed_cb(location_manager_h manager)
993 {
994         LOCATIONS_LOGD("location_manager_unset_distance_updated_cb");
995         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
996         LOCATIONS_NULL_ARG_CHECK(manager);
997         location_manager_s *handle = (location_manager_s *) manager;
998         g_object_set(handle->object, "min-interval", 0, NULL);
999         g_object_set(handle->object, "min-distance", 0, NULL);
1000         return __unset_callback(_LOCATIONS_EVENT_TYPE_DISTANCE, manager);
1001 }
1002
1003 EXPORT_API int location_manager_set_location_changed_cb(location_manager_h manager, location_changed_cb callback, int interval, void *user_data)
1004 {
1005         LOCATIONS_LOGD("location_manager_set_location_updated_cb");
1006         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1007         LOCATIONS_CHECK_CONDITION(interval >= 1 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1008         LOCATIONS_NULL_ARG_CHECK(manager);
1009         location_manager_s *handle = (location_manager_s *) manager;
1010         g_object_set(handle->object, "loc-interval", interval, NULL);
1011         return __set_callback(_LOCATIONS_EVENT_TYPE_POS_VEL, manager, callback, user_data);
1012 }
1013
1014 EXPORT_API int location_manager_unset_location_changed_cb(location_manager_h manager)
1015 {
1016         LOCATIONS_LOGD("location_manager_unset_position_updated_cb");
1017         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1018         return __unset_callback(_LOCATIONS_EVENT_TYPE_POS_VEL, manager);
1019 }
1020
1021 EXPORT_API int location_manager_set_position_updated_cb(location_manager_h manager, location_position_updated_cb callback, int interval, void *user_data)
1022 {
1023         LOCATIONS_LOGD("location_manager_set_position_updated_cb");
1024         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1025         LOCATIONS_CHECK_CONDITION(interval >= 1
1026                                   && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1027         LOCATIONS_NULL_ARG_CHECK(manager);
1028         location_manager_s *handle = (location_manager_s *) manager;
1029         g_object_set(handle->object, "pos-interval", interval, NULL);
1030         return __set_callback(_LOCATIONS_EVENT_TYPE_POSITION, manager, callback, user_data);
1031 }
1032
1033 EXPORT_API int location_manager_unset_position_updated_cb(location_manager_h manager)
1034 {
1035         LOCATIONS_LOGD("location_manager_unset_position_updated_cb");
1036         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1037         return __unset_callback(_LOCATIONS_EVENT_TYPE_POSITION, manager);
1038 }
1039
1040 EXPORT_API int location_manager_set_velocity_updated_cb(location_manager_h manager, location_velocity_updated_cb callback, int interval, void *user_data)
1041 {
1042         LOCATIONS_LOGD("location_manager_set_velocity_updated_cb");
1043         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1044         LOCATIONS_CHECK_CONDITION(interval >= 1 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1045         LOCATIONS_NULL_ARG_CHECK(manager);
1046         location_manager_s *handle = (location_manager_s *) manager;
1047         g_object_set(handle->object, "vel-interval", interval, NULL);
1048         return __set_callback(_LOCATIONS_EVENT_TYPE_VELOCITY, manager, callback, user_data);
1049 }
1050
1051 EXPORT_API int location_manager_unset_velocity_updated_cb(location_manager_h manager)
1052 {
1053         LOCATIONS_LOGD("location_manager_unset_velocity_updated_cb");
1054         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1055         return __unset_callback(_LOCATIONS_EVENT_TYPE_VELOCITY, manager);
1056 }
1057
1058 EXPORT_API int location_manager_set_service_state_changed_cb(location_manager_h manager, location_service_state_changed_cb callback,
1059                                                              void *user_data)
1060 {
1061         LOCATIONS_LOGD("location_manager_set_service_state_changed_cb");
1062         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1063         return __set_callback(_LOCATIONS_EVENT_TYPE_SERVICE_STATE, manager, callback, user_data);
1064 }
1065
1066 EXPORT_API int location_manager_unset_service_state_changed_cb(location_manager_h manager)
1067 {
1068         LOCATIONS_LOGD("location_manager_unset_service_state_changed_cb");
1069         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1070         return __unset_callback(_LOCATIONS_EVENT_TYPE_SERVICE_STATE, manager);
1071 }
1072
1073 EXPORT_API int location_manager_set_zone_changed_cb(location_manager_h manager, location_zone_changed_cb callback, void *user_data)
1074 {
1075         LOCATIONS_LOGD("location_manager_set_zone_changed_cb");
1076         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1077         return __set_callback(_LOCATIONS_EVENT_TYPE_BOUNDARY, manager, callback, user_data);
1078 }
1079
1080 EXPORT_API int location_manager_unset_zone_changed_cb(location_manager_h manager)
1081 {
1082         LOCATIONS_LOGD("location_manager_unset_zone_changed_cb");
1083         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1084         return __unset_callback(_LOCATIONS_EVENT_TYPE_BOUNDARY, manager);
1085 }
1086
1087 EXPORT_API int location_manager_set_setting_changed_cb(location_method_e method, location_setting_changed_cb callback, void *user_data)
1088 {
1089         LOCATIONS_LOGD("location_manager_set_setting_changed_cb");
1090         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1091         LOCATIONS_NULL_ARG_CHECK(callback);
1092
1093         LocationMethod _method = __convert_LocationMethod(method);
1094         int ret = LOCATION_ERROR_NONE;
1095
1096         if (_method == LOCATION_METHOD_NONE) {
1097                 return __convert_error_code(LOCATION_ERROR_PARAMETER);
1098         }
1099
1100         g_location_setting[_method].callback = callback;
1101         g_location_setting[_method].user_data = user_data;
1102
1103         ret = location_add_setting_notify(_method, __setting_changed_cb, &g_location_setting);
1104         if (ret != LOCATION_ERROR_NONE) {
1105                 return __convert_error_code(ret);
1106         }
1107
1108         return LOCATIONS_ERROR_NONE;
1109 }
1110
1111 EXPORT_API int location_manager_unset_setting_changed_cb(location_method_e method)
1112 {
1113         LOCATIONS_LOGD("location_manager_unset_setting_changed_cb");
1114         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1115         LocationMethod _method = __convert_LocationMethod(method);
1116         int ret = LOCATION_ERROR_NONE;
1117
1118         if (_method == LOCATION_METHOD_NONE) {
1119                 return __convert_error_code(LOCATION_ERROR_PARAMETER);
1120         }
1121
1122         ret = location_ignore_setting_notify(_method, __setting_changed_cb);
1123         if (ret != LOCATION_ERROR_NONE) {
1124                 LOCATIONS_LOGE("Fail to ignore notify. Error[%d]", ret);
1125                 ret = __convert_error_code(ret);
1126         }
1127
1128         g_location_setting[method].callback = NULL;
1129         g_location_setting[method].user_data = NULL;
1130
1131         return LOCATIONS_ERROR_NONE;
1132 }
1133
1134 EXPORT_API int location_manager_get_distance(double start_latitude, double start_longitude, double end_latitude, double end_longitude, double *distance)
1135 {
1136         LOCATIONS_LOGD("location_manager_get_distance");
1137         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1138         LOCATIONS_NULL_ARG_CHECK(distance);
1139         LOCATIONS_CHECK_CONDITION(start_latitude >= -90 && start_latitude <= 90, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1140         LOCATIONS_CHECK_CONDITION(start_longitude >= -180 && start_longitude <= 180, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1141         LOCATIONS_CHECK_CONDITION(end_latitude >= -90 && end_latitude <= 90, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1142         LOCATIONS_CHECK_CONDITION(end_longitude >= -180 && end_longitude <= 180, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1143
1144         int ret = LOCATION_ERROR_NONE;
1145         ulong u_distance;
1146
1147         LocationPosition *start = location_position_new(0, start_latitude, start_longitude, 0, LOCATION_STATUS_2D_FIX);
1148         LocationPosition *end = location_position_new(0, end_latitude, end_longitude, 0, LOCATION_STATUS_2D_FIX);
1149
1150         ret = location_get_distance(start, end, &u_distance);
1151         if (ret != LOCATION_ERROR_NONE) {
1152                 return __convert_error_code(ret);
1153         }
1154
1155         *distance = (double)u_distance;
1156
1157         return LOCATIONS_ERROR_NONE;
1158 }
1159
1160 /*/////////////////////////////////////// */
1161 /* GPS Status & Satellites */
1162 /*////////////////////////////////////// */
1163
1164 EXPORT_API int gps_status_get_nmea(location_manager_h manager, char **nmea)
1165 {
1166         LOCATIONS_LOGD("gps_status_get_nmea");
1167         LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_supported());
1168         LOCATIONS_NULL_ARG_CHECK(manager);
1169         LOCATIONS_NULL_ARG_CHECK(nmea);
1170         location_manager_s *handle = (location_manager_s *) manager;
1171         char *nmea_data;
1172
1173         int ret = location_get_nmea(handle->object, &nmea_data);
1174         if (ret != LOCATION_ERROR_NONE || nmea == NULL) {
1175                 if (ret == LOCATION_ERROR_NOT_ALLOWED) {
1176                         LOCATIONS_LOGE("LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED");
1177                         return LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED;
1178                 }
1179
1180                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : NMEA is NULL ",
1181                 LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
1182                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
1183         }
1184
1185         *nmea = g_strdup(nmea_data);
1186         g_free(nmea_data);
1187
1188         return LOCATIONS_ERROR_NONE;
1189 }
1190
1191 EXPORT_API int gps_status_get_satellite(location_manager_h manager, int *num_of_active, int *num_of_inview, time_t *timestamp)
1192 {
1193         LOCATIONS_LOGD("gps_status_get_satellite");
1194         LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_supported());
1195         LOCATIONS_NULL_ARG_CHECK(manager);
1196         LOCATIONS_NULL_ARG_CHECK(num_of_active);
1197         LOCATIONS_NULL_ARG_CHECK(num_of_inview);
1198         LOCATIONS_NULL_ARG_CHECK(timestamp);
1199         location_manager_s *handle = (location_manager_s *) manager;
1200         LocationSatellite *sat = NULL;
1201         int ret = location_get_satellite(handle->object, &sat);
1202         if (ret != LOCATION_ERROR_NONE || sat == NULL) {
1203                 if (ret == LOCATION_ERROR_NOT_ALLOWED) {
1204                         LOCATIONS_LOGE("LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED");
1205                         return LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED;
1206                 }
1207
1208                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ",
1209                                LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
1210                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
1211         }
1212
1213         *num_of_active = sat->num_of_sat_used;
1214         *num_of_inview = sat->num_of_sat_inview;
1215         *timestamp = sat->timestamp;
1216         location_satellite_free(sat);
1217         sat = NULL;
1218         return LOCATIONS_ERROR_NONE;
1219 }
1220
1221 EXPORT_API int gps_status_set_satellite_updated_cb(location_manager_h manager, gps_status_satellite_updated_cb callback, int interval, void *user_data)
1222 {
1223         LOCATIONS_LOGD("gps_status_set_satellite_updated_cb");
1224         LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_supported());
1225         LOCATIONS_CHECK_CONDITION(interval >= 1 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1226         LOCATIONS_NULL_ARG_CHECK(manager);
1227         LOCATIONS_NULL_ARG_CHECK(callback);
1228
1229         location_manager_s *handle = (location_manager_s *) manager;
1230         location_set_option(handle->object, "USE_SV");
1231         g_object_set(handle->object, "sat-interval", interval, NULL);
1232         return __set_callback(_LOCATIONS_EVENT_TYPE_SATELLITE, manager, callback, user_data);
1233 }
1234
1235 EXPORT_API int gps_status_unset_satellite_updated_cb(location_manager_h manager)
1236 {
1237         LOCATIONS_LOGD("gps_status_unset_satellite_updated_cb");
1238         LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_supported());
1239         return __unset_callback(_LOCATIONS_EVENT_TYPE_SATELLITE, manager);
1240 }
1241
1242 EXPORT_API int gps_status_foreach_satellites_in_view(location_manager_h manager, gps_status_get_satellites_cb callback, void *user_data)
1243 {
1244         LOCATIONS_LOGD("gps_status_foreach_satellites_in_view");
1245         LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_supported());
1246         LOCATIONS_NULL_ARG_CHECK(manager);
1247         LOCATIONS_NULL_ARG_CHECK(callback);
1248
1249         location_manager_s *handle = (location_manager_s *) manager;
1250         LocationSatellite *sat = NULL;
1251         int ret = location_get_satellite(handle->object, &sat);
1252         if (ret != LOCATION_ERROR_NONE || sat == NULL) {
1253                 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
1254                         LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
1255                                        LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
1256                         return LOCATIONS_ERROR_INCORRECT_METHOD;
1257                 } else if (ret == LOCATION_ERROR_NOT_ALLOWED) {
1258                         LOCATIONS_LOGE("LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED");
1259                         return LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED;
1260                 }
1261
1262                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ",
1263                                LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
1264                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
1265         }
1266
1267         int i;
1268         for (i = 0; i < sat->num_of_sat_inview; i++) {
1269                 guint prn;
1270                 gboolean used;
1271                 guint elevation;
1272                 guint azimuth;
1273                 gint snr;
1274                 location_satellite_get_satellite_details(sat, i, &prn, &used, &elevation, &azimuth, &snr);
1275                 if (callback(azimuth, elevation, prn, snr, used, user_data) != TRUE)
1276                         break;
1277         }
1278         location_satellite_free(sat);
1279         sat = NULL;
1280         return LOCATIONS_ERROR_NONE;
1281 }
1282
1283 EXPORT_API int gps_status_get_last_satellite(location_manager_h manager, int *num_of_active, int *num_of_inview, time_t *timestamp)
1284 {
1285         LOCATIONS_LOGD("gps_status_get_last_satellite");
1286         LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_supported());
1287         LOCATIONS_NULL_ARG_CHECK(manager);
1288         LOCATIONS_NULL_ARG_CHECK(num_of_active);
1289         LOCATIONS_NULL_ARG_CHECK(num_of_inview);
1290         LOCATIONS_NULL_ARG_CHECK(timestamp);
1291
1292         location_manager_s *handle = (location_manager_s *) manager;
1293         int ret = LOCATION_ERROR_NONE;
1294         LocationSatellite *last_sat = NULL;
1295         ret = location_get_last_satellite(handle->object, &last_sat);
1296         if (ret != LOCATION_ERROR_NONE || last_sat == NULL) {
1297                 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
1298                         LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
1299                                        LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
1300                         return LOCATIONS_ERROR_INCORRECT_METHOD;
1301                 } else if (ret == LOCATION_ERROR_NOT_ALLOWED) {
1302                         LOCATIONS_LOGE("LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED");
1303                         return LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED;
1304                 }
1305
1306                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ",
1307                                LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
1308                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
1309         }
1310
1311         *num_of_active = last_sat->num_of_sat_used;
1312         *num_of_inview = last_sat->num_of_sat_inview;
1313         *timestamp = last_sat->timestamp;
1314         location_satellite_free(last_sat);
1315         return LOCATIONS_ERROR_NONE;
1316 }
1317
1318 EXPORT_API int gps_status_foreach_last_satellites_in_view(location_manager_h manager, gps_status_get_satellites_cb callback,
1319                                                           void *user_data)
1320 {
1321         LOCATIONS_LOGD("gps_status_foreach_last_satellites_in_view");
1322         LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_supported());
1323         LOCATIONS_NULL_ARG_CHECK(manager);
1324         LOCATIONS_NULL_ARG_CHECK(callback);
1325         location_manager_s *handle = (location_manager_s *) manager;
1326         int ret;
1327         LocationSatellite *last_sat = NULL;
1328         ret = location_get_last_satellite(handle->object, &last_sat);
1329         if (ret != LOCATION_ERROR_NONE || last_sat == NULL) {
1330                 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
1331                         LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
1332                                        LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
1333                         return LOCATIONS_ERROR_INCORRECT_METHOD;
1334                 }
1335
1336                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ",
1337                                LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
1338                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
1339         }
1340
1341         int i;
1342         for (i = 0; i < last_sat->num_of_sat_inview; i++) {
1343                 guint prn;
1344                 gboolean used;
1345                 guint elevation;
1346                 guint azimuth;
1347                 gint snr;
1348                 location_satellite_get_satellite_details(last_sat, i, &prn, &used, &elevation, &azimuth, &snr);
1349                 if (callback(azimuth, elevation, prn, snr, used, user_data) != TRUE) {
1350                         break;
1351                 }
1352         }
1353         location_satellite_free(last_sat);
1354         return LOCATIONS_ERROR_NONE;
1355 }