sync with latest 3.0
[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_PASSIVE + 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_PASSIVE:
60                 _method = LOCATION_METHOD_PASSIVE;
61                 break;
62         case LOCATIONS_METHOD_NONE:
63         default:
64                 _method = LOCATION_METHOD_NONE;
65                 break;
66         }
67         return _method;
68 }
69
70 static void __cb_service_updated(GObject *self, guint type, gpointer data, gpointer velocity, gpointer accuracy, gpointer userdata)
71 {
72         location_manager_s *handle = (location_manager_s *) userdata;
73
74         if (type == SATELLITE_UPDATED && handle->user_cb[_LOCATIONS_EVENT_TYPE_SATELLITE]) {
75                 LocationSatellite *sat = (LocationSatellite *)data;
76                 LOCATIONS_LOGD("callback invoked[TYPE_SATELLITE] timestamp : %d, number of active : %d, number of inview : %d",
77                                                 sat->timestamp, sat->num_of_sat_used, sat->num_of_sat_inview);
78                 ((gps_status_satellite_updated_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_SATELLITE])(sat->num_of_sat_used, sat->num_of_sat_inview,
79                                                                                         sat->timestamp, handle->user_data[_LOCATIONS_EVENT_TYPE_SATELLITE]);
80         } else if (type == DISTANCE_UPDATED && handle->user_cb[_LOCATIONS_EVENT_TYPE_DISTANCE]) {
81                 LocationPosition *pos = (LocationPosition *) data;
82                 LocationVelocity *vel = (LocationVelocity *) velocity;
83                 LocationAccuracy *acc = (LocationAccuracy *) accuracy;
84                 ((location_changed_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_DISTANCE])(pos->latitude, pos->longitude, pos->altitude,
85                                                                 vel->speed, vel->direction, acc->horizontal_accuracy, pos->timestamp, handle->user_data[_LOCATIONS_EVENT_TYPE_DISTANCE]);
86                 LOCATIONS_LOGD("callback invoked[TYPE_DISTANCE] timestamp : %d", pos->timestamp);
87         } else {
88
89                 if (handle->user_cb[_LOCATIONS_EVENT_TYPE_POSITION] && (type & POSITION_UPDATED) != 0) {
90                         LocationPosition *pos = (LocationPosition *) data;
91                         ((location_position_updated_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_POSITION])(pos->latitude, pos->longitude, pos->altitude,
92                                                                                         pos->timestamp, handle->user_data[_LOCATIONS_EVENT_TYPE_POSITION]);
93                         LOCATIONS_LOGD("callback invoked[TYPE_POSITION] timestamp : %d", pos->timestamp);
94                 }
95
96                 if (handle->user_cb[_LOCATIONS_EVENT_TYPE_VELOCITY] && (type & VELOCITY_UPDATED) != 0) {
97                         LocationVelocity *vel = (LocationVelocity *) velocity;
98                         ((location_velocity_updated_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_VELOCITY])(vel->speed, vel->direction, vel->climb,
99                                                                                         vel->timestamp, handle->user_data[_LOCATIONS_EVENT_TYPE_VELOCITY]);
100                 }
101
102                 if (handle->user_cb[_LOCATIONS_EVENT_TYPE_POS_VEL] && (type & LOCATION_CHANGED) != 0) {
103                         LocationPosition *pos = (LocationPosition *) data;
104                         LocationVelocity *vel = (LocationVelocity *) velocity;
105                         LocationAccuracy *acc = (LocationAccuracy *) accuracy;
106                         ((location_changed_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_POS_VEL])(pos->latitude, pos->longitude, pos->altitude,
107                                                                         vel->speed, vel->direction, acc->horizontal_accuracy, pos->timestamp, handle->user_data[_LOCATIONS_EVENT_TYPE_POS_VEL]);
108                         LOCATIONS_LOGD("callback invoked[TYPE_POS_VEL] timestamp : %d", pos->timestamp);
109                 }
110         }
111 }
112
113 static void __cb_location_updated(GObject *self, int error, gpointer position, gpointer velocity, gpointer accuracy, gpointer userdata)
114 {
115         LOCATIONS_LOGD("Callback function has been invoked. ");
116         int converted_err = __convert_error_code(error);
117         location_manager_s *handle = (location_manager_s *) userdata;
118         LocationPosition *pos = (LocationPosition *) position;
119         LocationVelocity *vel = (LocationVelocity *) velocity;
120
121         LOCATIONS_LOGD("Current position: timestamp : %d", pos->timestamp);
122         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_LOCATION]) {
123                 ((location_updated_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_LOCATION])(converted_err, pos->latitude, pos->longitude, pos->altitude,
124                                                                                                                                                                 pos->timestamp, vel->speed, vel->climb, vel->direction, handle->user_data[_LOCATIONS_EVENT_TYPE_LOCATION]);
125         }
126 }
127
128 static void __cb_batch_updated(GObject *self, guint num_of_location, gpointer userdata)
129 {
130         LOCATIONS_LOGD("Batch callback function has been invoked.");
131         location_manager_s *handle = (location_manager_s *) userdata;
132
133         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_BATCH])
134                 ((location_batch_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_BATCH])(num_of_location, handle->user_data[_LOCATIONS_EVENT_TYPE_BATCH]);
135 }
136
137 static void __cb_service_enabled(GObject *self, guint status, gpointer userdata)
138 {
139         LOCATIONS_LOGD("Invoked[service_enabled]. status = %d", status);
140         location_manager_s *handle = (location_manager_s *) userdata;
141         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_SERVICE_STATE]) {
142                 ((location_service_state_changed_cb)
143                 handle->user_cb[_LOCATIONS_EVENT_TYPE_SERVICE_STATE])(LOCATIONS_SERVICE_ENABLED, handle->user_data[_LOCATIONS_EVENT_TYPE_SERVICE_STATE]);
144         }
145 }
146
147 static void __cb_service_disabled(GObject *self, guint status, gpointer userdata)
148 {
149         LOCATIONS_LOGD("Invoked[service_disabled]. status = %d", status);
150         location_manager_s *handle = (location_manager_s *) userdata;
151         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_SERVICE_STATE]) {
152                 ((location_service_state_changed_cb)
153                  handle->user_cb[_LOCATIONS_EVENT_TYPE_SERVICE_STATE])(LOCATIONS_SERVICE_DISABLED, handle->user_data[_LOCATIONS_EVENT_TYPE_SERVICE_STATE]);
154         }
155 }
156
157 static int __compare_position(gconstpointer a, gconstpointer b)
158 {
159         if (location_position_equal((LocationPosition *) a, (LocationPosition *)b) == TRUE)
160                 return 0;
161
162         return -1;
163 }
164
165 static int __boundary_compare(LocationBoundary *bound1, LocationBoundary *bound2)
166 {
167         int ret = -1;
168
169         if (bound1->type == bound2->type) {
170                 switch (bound1->type) {
171                 case LOCATION_BOUNDARY_CIRCLE:
172                         if (location_position_equal(bound1->circle.center, bound2->circle.center) && bound1->circle.radius == bound2->circle.radius)
173                                 ret = 0;
174
175                         break;
176                 case LOCATION_BOUNDARY_RECT:
177                         if (location_position_equal(bound1->rect.left_top, bound2->rect.left_top) && location_position_equal(bound1->rect.right_bottom, bound2->rect.right_bottom))
178                                 ret = 0;
179
180                         break;
181                 case LOCATION_BOUNDARY_POLYGON: {
182                                 GList *boundary1_next = NULL;
183                                 GList *boundary2_start = NULL, *boundary2_prev = NULL, *boundary2_next = NULL;
184                                 if (g_list_length(bound1->polygon.position_list) != g_list_length(bound2->polygon.position_list))
185                                         return -1;
186
187                                 boundary2_start = g_list_find_custom(bound2->polygon.position_list, g_list_nth_data(bound1->polygon.position_list, 0), (GCompareFunc) __compare_position);
188                                 if (boundary2_start == NULL) return -1;
189
190                                 boundary2_prev = g_list_previous(boundary2_start);
191                                 boundary2_next = g_list_next(boundary2_start);
192
193                                 if (boundary2_prev == NULL) boundary2_prev = g_list_last(bound2->polygon.position_list);
194                                 if (boundary2_next == NULL) boundary2_next = g_list_first(bound2->polygon.position_list);
195
196                                 boundary1_next = g_list_next(bound1->polygon.position_list);
197                                 if (boundary1_next != NULL && boundary2_prev != NULL &&
198                                         location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *)boundary2_prev->data) == TRUE) {
199                                         boundary1_next = g_list_next(boundary1_next);
200                                         while (boundary1_next) {
201                                                 boundary2_prev = g_list_previous(boundary2_prev);
202                                                 if (boundary2_prev == NULL) boundary2_prev = g_list_last(bound2->polygon.position_list);
203                                                 if (location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *) boundary2_prev->data) == FALSE)
204                                                         return -1;
205
206                                                 boundary1_next = g_list_next(boundary1_next);
207                                         }
208                                         ret = 0;
209                                 } else if (boundary1_next != NULL && boundary2_next != NULL &&
210                                                         location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *)boundary2_next->data) == TRUE) {
211                                         boundary1_next = g_list_next(boundary1_next);
212                                         while (boundary1_next) {
213                                                 boundary2_next = g_list_next(boundary2_next);
214                                                 if (boundary2_next == NULL) boundary2_next = g_list_first(bound2->polygon.position_list);
215                                                 if (location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *) boundary2_next->data) == FALSE)
216                                                         return -1;
217
218                                                 boundary1_next = g_list_next(boundary1_next);
219                                         }
220                                         ret = 0;
221                                 } else {
222                                         return -1;
223                                 }
224                                 break;
225                         }
226                 default:
227                         break;
228                 }
229         }
230         return ret;
231 }
232
233 static void __cb_zone_in(GObject *self, gpointer boundary, gpointer position, gpointer accuracy, gpointer userdata)
234 {
235         LOCATIONS_LOGD("ENTER >>>");
236         location_manager_s *handle = (location_manager_s *) userdata;
237         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_BOUNDARY]) {
238                 LocationPosition *pos = (LocationPosition *) position;
239                 ((location_zone_changed_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_BOUNDARY])(LOCATIONS_BOUNDARY_IN,
240                                                                                                                                                                         pos->latitude, pos->longitude,
241                                                                                                                                                                         pos->altitude, pos->timestamp,
242                                                                                                                                                                         handle->user_data
243                                                                                                                                                                         [_LOCATIONS_EVENT_TYPE_BOUNDARY]);
244         }
245
246         location_bounds_s *bounds;
247         GList *bounds_list = g_list_first(handle->bounds_list);
248         while (bounds_list) {
249                 bounds = (location_bounds_s *)bounds_list->data;
250                 if (__boundary_compare(boundary, bounds->boundary) == 0) {
251                         LOCATIONS_LOGD("Find zone in boundary");
252                         if (bounds->user_cb)
253                                 ((location_bounds_state_changed_cb) bounds->user_cb)(LOCATIONS_BOUNDARY_IN, bounds->user_data);
254
255                         break;
256                 }
257                 bounds_list = g_list_next(bounds_list);
258         }
259         LOCATIONS_LOGD("EXIT <<<");
260 }
261
262 static void __cb_zone_out(GObject *self, gpointer boundary, gpointer position, gpointer accuracy, gpointer userdata)
263 {
264         LOCATIONS_LOGD("ENTER >>>");
265         location_manager_s *handle = (location_manager_s *) userdata;
266         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_BOUNDARY]) {
267                 LocationPosition *pos = (LocationPosition *) position;
268                 ((location_zone_changed_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_BOUNDARY])(LOCATIONS_BOUNDARY_OUT,
269                                                                                                                                                                         pos->latitude, pos->longitude,
270                                                                                                                                                                         pos->altitude, pos->timestamp,
271                                                                                                                                                                         handle->user_data
272                                                                                                                                                                         [_LOCATIONS_EVENT_TYPE_BOUNDARY]);
273         }
274
275         location_bounds_s *bounds;
276         GList *bounds_list = g_list_first(handle->bounds_list);
277         while (bounds_list) {
278                 bounds = (location_bounds_s *)bounds_list->data;
279                 if (__boundary_compare(boundary, bounds->boundary) == 0) {
280                         LOCATIONS_LOGD("Find zone out boundary");
281                         if (bounds->user_cb)
282                                 ((location_bounds_state_changed_cb) bounds->user_cb)(LOCATIONS_BOUNDARY_OUT, bounds->user_data);
283
284                         break;
285                 }
286                 bounds_list = g_list_next(bounds_list);
287         }
288         LOCATIONS_LOGD("EXIT <<<");
289 }
290
291 static void __foreach_boundary(LocationBoundary *boundary, void *user_data)
292 {
293         location_manager_s *handle = (location_manager_s *) user_data;
294
295         if (handle != NULL && boundary != NULL) {
296                 int ret = -1;
297                 location_bounds_h bounds;
298                 if (boundary->type == LOCATION_BOUNDARY_CIRCLE) {
299                         location_coords_s center;
300                         center.latitude = boundary->circle.center->latitude;
301                         center.longitude = boundary->circle.center->longitude;
302                         ret = location_bounds_create_circle(center, boundary->circle.radius, &bounds);
303                 } else if (boundary->type == LOCATION_BOUNDARY_RECT) {
304                         location_coords_s left_top;
305                         location_coords_s right_bottom;
306                         left_top.latitude = boundary->rect.left_top->latitude;
307                         left_top.longitude = boundary->rect.left_top->longitude;
308                         right_bottom.latitude = boundary->rect.right_bottom->latitude;
309                         right_bottom.longitude = boundary->rect.right_bottom->longitude;
310                         ret = location_bounds_create_rect(left_top, right_bottom, &bounds);
311                 } else if (boundary->type == LOCATION_BOUNDARY_POLYGON) {
312                         GList *list = boundary->polygon.position_list;
313                         int size = g_list_length(list);
314                         if (size > 0) {
315                                 location_coords_s coords[size];
316                                 int cnt = 0;
317                                 while (list) {
318                                         LocationPosition *pos = list->data;
319                                         coords[cnt].latitude = pos->latitude;
320                                         coords[cnt].longitude = pos->longitude;
321                                         list = g_list_next(list);
322                                         cnt++;
323                                 }
324                                 ret = location_bounds_create_polygon(coords, size, &bounds);
325                         }
326                 } else {
327                         LOCATIONS_LOGI("Invalid boundary type : %d", boundary->type);
328                 }
329
330                 if (ret != LOCATIONS_ERROR_NONE) {
331                         LOCATIONS_LOGI("Failed to create location_bounds : (0x%08x) ", ret);
332                 } else {
333                         if (handle->is_continue_foreach_bounds) {
334                                 handle->is_continue_foreach_bounds =
335                                         ((location_bounds_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS])(bounds,
336                                                         handle->
337                                                         user_data
338                                                         [_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS]);
339                         }
340                         location_bounds_destroy(bounds);
341                 }
342         } else {
343                 LOCATIONS_LOGD("__foreach_boundary() has been failed");
344         }
345 }
346
347 static void __setting_changed_cb(LocationMethod method, gboolean enable, void *user_data)
348 {
349         LOCATIONS_LOGD("method: [%d]", method);
350         location_method_e _method = __convert_location_method_e(method);
351         location_setting_changed_s *_setting_changed = (location_setting_changed_s *)user_data;
352         if (_setting_changed == NULL) {
353                 LOCATIONS_LOGE("Invaild userdata\n");
354                 return;
355         }
356
357         if (_setting_changed[_method].callback != NULL)
358                 _setting_changed[_method].callback(_method, enable, _setting_changed[_method].user_data);
359 }
360
361 /*/////////////////////////////////////// */
362 /* Location Manager */
363 /*////////////////////////////////////// */
364
365 EXPORT_API bool location_manager_is_supported_method(location_method_e method)
366 {
367         LOCATIONS_LOGD("location_manager_is_supported_method %d", method);
368         if (__is_location_supported() == LOCATIONS_ERROR_NOT_SUPPORTED) {
369                 set_last_result(LOCATIONS_ERROR_NOT_SUPPORTED);
370                 return false;
371         }
372
373         if (method == LOCATIONS_METHOD_GPS) {
374                 if (__is_gps_supported() == LOCATIONS_ERROR_NOT_SUPPORTED) {
375                         set_last_result(LOCATIONS_ERROR_NOT_SUPPORTED);
376                         return false;
377                 }
378         } else if (method == LOCATIONS_METHOD_WPS) {
379                 if (__is_wps_supported() == LOCATIONS_ERROR_NOT_SUPPORTED) {
380                         set_last_result(LOCATIONS_ERROR_NOT_SUPPORTED);
381                         return false;
382                 }
383         }
384
385         LocationMethod _method = __convert_LocationMethod(method);
386         if (_method == LOCATION_METHOD_NONE) {
387                 LOCATIONS_LOGE("Not supported method [%d]", method);
388                 set_last_result(LOCATIONS_ERROR_INCORRECT_METHOD);
389                 return false;
390         }
391
392         gboolean ret =  location_is_supported_method(_method);
393         if (ret) {
394                 set_last_result(LOCATIONS_ERROR_NONE);
395                 return true;
396         } else {
397                 set_last_result(LOCATIONS_ERROR_NOT_SUPPORTED);
398                 return false;
399         }
400 }
401
402 EXPORT_API int location_manager_is_enabled_method(location_method_e method, bool *enable)
403 {
404         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
405
406         if (method < LOCATIONS_METHOD_HYBRID || method > LOCATIONS_METHOD_WPS) {
407                 LOCATIONS_LOGE("Not supported method [%d]", method);
408                 return LOCATIONS_ERROR_INCORRECT_METHOD;
409         }
410
411         LOCATIONS_LOGD("location_manager_is_enabled_method %d", method);
412         LOCATIONS_NULL_ARG_CHECK(enable);
413         int is_enabled_val = -1;
414         LocationMethod _method = __convert_LocationMethod(method);
415         int ret = location_is_enabled_method(_method, &is_enabled_val);
416         if (ret != LOCATION_ERROR_NONE) {
417                 if (ret == LOCATION_ERROR_NOT_SUPPORTED)
418                         return LOCATIONS_ERROR_INCORRECT_METHOD;
419                 return __convert_error_code(ret);
420         }
421
422         if (is_enabled_val == -1)
423                 return TIZEN_ERROR_PERMISSION_DENIED;
424
425         *enable = (is_enabled_val == 0) ? FALSE : TRUE;
426         return LOCATIONS_ERROR_NONE;
427 }
428
429 EXPORT_API int location_manager_enable_method(const location_method_e method, const bool enable)
430 {
431         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
432
433         if (method < LOCATIONS_METHOD_HYBRID || method > LOCATIONS_METHOD_WPS) {
434                 LOCATIONS_LOGE("Not supported method [%d]", method);
435                 return LOCATIONS_ERROR_INCORRECT_METHOD;
436         }
437
438         LOCATIONS_LOGD("method: %d, enable: %d", method, enable);
439
440         int ret = 0;
441         if (LOCATIONS_METHOD_HYBRID == method) {
442                 if (__is_gps_supported() == LOCATIONS_ERROR_NONE) {
443                         ret = location_enable_method(LOCATION_METHOD_GPS, enable);
444                         if (ret != LOCATIONS_ERROR_NONE) return __convert_error_code(ret);
445                 }
446                 if (__is_wps_supported() == LOCATIONS_ERROR_NONE) {
447                         ret = location_enable_method(LOCATION_METHOD_WPS, enable);
448                         return __convert_error_code(ret);
449                 }
450                 return LOCATIONS_ERROR_NONE;
451
452         } else  {
453                 if ((LOCATIONS_METHOD_GPS == method) && (__is_gps_supported() == LOCATIONS_ERROR_NOT_SUPPORTED)) {
454                         LOCATIONS_LOGE("LOCATIONS_ERROR_NOT_SUPPORTED(0x%08x)", LOCATIONS_ERROR_NOT_SUPPORTED);
455                         return LOCATIONS_ERROR_NOT_SUPPORTED;
456                 } else if ((LOCATIONS_METHOD_WPS == method) && (__is_wps_supported() == LOCATIONS_ERROR_NOT_SUPPORTED)) {
457                         LOCATIONS_LOGE("LOCATIONS_ERROR_NOT_SUPPORTED(0x%08x)", LOCATIONS_ERROR_NOT_SUPPORTED);
458                         return LOCATIONS_ERROR_NOT_SUPPORTED;
459                 }
460
461                 LocationMethod _method = __convert_LocationMethod(method);
462
463                 int ret = location_enable_method(_method, enable);
464
465                 return __convert_error_code(ret);
466         }
467 }
468
469 EXPORT_API int location_manager_create(location_method_e method, location_manager_h *manager)
470 {
471         LOCATIONS_LOGD("location_manager_create (method : %d)", method);
472
473         if (method == LOCATIONS_METHOD_HYBRID)
474                 LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
475         else if (method == LOCATIONS_METHOD_GPS)
476                 LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_supported());
477         else if (method == LOCATIONS_METHOD_WPS)
478                 LOCATIONS_NOT_SUPPORTED_CHECK(__is_wps_supported());
479         else if (method == LOCATIONS_METHOD_PASSIVE)
480                 LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
481
482         LocationMethod _method = __convert_LocationMethod(method);
483         if (_method == LOCATION_METHOD_NONE) {
484                 LOCATIONS_LOGE("LOCATIONS_ERROR_NOT_SUPPORTED(0x%08x) : fail to location_init", LOCATIONS_ERROR_NOT_SUPPORTED);
485                 return LOCATIONS_ERROR_NOT_SUPPORTED;
486         }
487
488         if (!location_is_supported_method(_method)) {
489                 LOCATIONS_LOGE("LOCATIONS_ERROR_NOT_SUPPORTED(0x%08x) : fail to location_is_supported_method", LOCATIONS_ERROR_NOT_SUPPORTED);
490                 return LOCATIONS_ERROR_NOT_SUPPORTED;
491         }
492
493         /*It is moved here becasue of TCS. */
494         LOCATIONS_NULL_ARG_CHECK(manager);
495
496         if (location_init() != LOCATION_ERROR_NONE) {
497                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : fail to location_init", LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
498                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
499         }
500
501         location_manager_s *handle = (location_manager_s *) malloc(sizeof(location_manager_s));
502         if (handle == NULL) {
503                 LOCATIONS_LOGE("OUT_OF_MEMORY(0x%08x)", LOCATIONS_ERROR_OUT_OF_MEMORY);
504                 return LOCATIONS_ERROR_OUT_OF_MEMORY;
505         }
506
507         memset(handle, 0, sizeof(location_manager_s));
508
509         handle->object = location_new(_method, FALSE);
510         if (handle->object == NULL) {
511                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : fail to location_new", LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
512                 free(handle);
513                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
514         }
515         handle->method = method;
516         handle->is_continue_foreach_bounds = TRUE;
517         handle->bounds_list = NULL;
518
519         if (!handle->sig_id[_LOCATION_SIGNAL_SERVICE_ENABLED])
520                 handle->sig_id[_LOCATION_SIGNAL_SERVICE_ENABLED] = g_signal_connect(handle->object, "service-enabled", G_CALLBACK(__cb_service_enabled), handle);
521
522         if (!handle->sig_id[_LOCATION_SIGNAL_SERVICE_DISABLED])
523                 handle->sig_id[_LOCATION_SIGNAL_SERVICE_DISABLED] = g_signal_connect(handle->object, "service-disabled", G_CALLBACK(__cb_service_disabled), handle);
524
525         *manager = (location_manager_h) handle;
526         return LOCATIONS_ERROR_NONE;
527 }
528
529 EXPORT_API int location_manager_destroy(location_manager_h manager)
530 {
531         LOCATIONS_LOGD("location_manager_destroy");
532         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
533         LOCATIONS_NULL_ARG_CHECK(manager);
534         location_manager_s *handle = (location_manager_s *) manager;
535
536         if (handle->sig_id[_LOCATION_SIGNAL_SERVICE_ENABLED]) {
537                 g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_SERVICE_ENABLED]);
538                 handle->sig_id[_LOCATION_SIGNAL_SERVICE_ENABLED] = 0;
539         }
540
541         if (handle->sig_id[_LOCATION_SIGNAL_SERVICE_DISABLED]) {
542                 g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_SERVICE_DISABLED]);
543                 handle->sig_id[_LOCATION_SIGNAL_SERVICE_DISABLED] = 0;
544         }
545
546         if (handle->sig_id[_LOCATION_SIGNAL_ERROR_EMITTED]) {
547                 g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_ERROR_EMITTED]);
548                 handle->sig_id[_LOCATION_SIGNAL_ERROR_EMITTED] = 0;
549         }
550
551         int ret = location_free(handle->object, FALSE);
552         if (ret != LOCATIONS_ERROR_NONE)
553                 return __convert_error_code(ret);
554
555         free(handle);
556         return LOCATIONS_ERROR_NONE;
557 }
558
559 EXPORT_API int location_manager_start(location_manager_h manager)
560 {
561         LOCATIONS_LOGD("location_manager_start");
562         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
563         LOCATIONS_NULL_ARG_CHECK(manager);
564         location_manager_s *handle = (location_manager_s *) manager;
565
566         if (!handle->sig_id[_LOCATION_SIGNAL_SERVICE_UPDATED])
567                 handle->sig_id[_LOCATION_SIGNAL_SERVICE_UPDATED] = g_signal_connect(handle->object, "service-updated", G_CALLBACK(__cb_service_updated), handle);
568
569         if (handle->method >= LOCATIONS_METHOD_HYBRID && handle->method <= LOCATIONS_METHOD_PASSIVE) {
570                 if (!handle->sig_id[_LOCATION_SIGNAL_ZONE_IN])
571                         handle->sig_id[_LOCATION_SIGNAL_ZONE_IN] = g_signal_connect(handle->object, "zone-in", G_CALLBACK(__cb_zone_in), handle);
572
573                 if (!handle->sig_id[_LOCATION_SIGNAL_ZONE_OUT])
574                         handle->sig_id[_LOCATION_SIGNAL_ZONE_OUT] = g_signal_connect(handle->object, "zone-out", G_CALLBACK(__cb_zone_out), handle);
575         } else {
576                 LOCATIONS_LOGI("This method [%d] is not supported zone-in, zone-out signal.", handle->method);
577         }
578
579         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_SATELLITE] != NULL) {
580                 LOCATIONS_LOGI("Satellite update_cb is set");
581                 location_set_option(handle->object, "USE_SV");
582         }
583
584         int ret = location_start(handle->object);
585         if (ret != LOCATION_ERROR_NONE)
586                 return __convert_error_code(ret);
587
588         return LOCATIONS_ERROR_NONE;
589 }
590
591 EXPORT_API int location_manager_request_single_location(location_manager_h manager, int timeout, location_updated_cb callback, void *user_data)
592 {
593         LOCATIONS_LOGD("location_manager_request_single_location");
594         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
595         LOCATIONS_NULL_ARG_CHECK(manager);
596         LOCATIONS_NULL_ARG_CHECK(callback);
597         if (timeout <= 0 || timeout > 120) {
598                 LOCATIONS_LOGE("timeout scope is incorrect(1~120) [%d]", timeout);
599                 return LOCATIONS_ERROR_INVALID_PARAMETER;
600         }
601
602         location_manager_s *handle = (location_manager_s *) manager;
603         int ret = LOCATIONS_ERROR_NONE;
604
605         if (!handle->sig_id[_LOCATION_SIGNAL_LOCATION_UPDATED])
606                 handle->sig_id[_LOCATION_SIGNAL_LOCATION_UPDATED] = g_signal_connect(handle->object, "location-updated", G_CALLBACK(__cb_location_updated), handle);
607
608         ret = __set_callback(_LOCATIONS_EVENT_TYPE_LOCATION, manager, callback, user_data);
609         if (ret != LOCATIONS_ERROR_NONE)
610                 return ret;
611
612         ret = location_request_single_location(handle->object, timeout);
613         if (ret != LOCATION_ERROR_NONE) {
614                 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
615                         LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD : method - %d", handle->method);
616                         return LOCATIONS_ERROR_INCORRECT_METHOD;
617                 }
618                 return __convert_error_code(ret);
619         }
620
621         return LOCATIONS_ERROR_NONE;
622 }
623
624 EXPORT_API int location_manager_stop(location_manager_h manager)
625 {
626         LOCATIONS_LOGD("location_manager_stop");
627         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
628         LOCATIONS_NULL_ARG_CHECK(manager);
629
630         location_manager_s *handle = (location_manager_s *) manager;
631
632         if (handle->sig_id[_LOCATION_SIGNAL_SERVICE_UPDATED]) {
633                 g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_SERVICE_UPDATED]);
634                 handle->sig_id[_LOCATION_SIGNAL_SERVICE_UPDATED] = 0;
635         }
636
637         if (handle->method >= LOCATIONS_METHOD_HYBRID && handle->method <= LOCATIONS_METHOD_PASSIVE) {
638                 if (handle->sig_id[_LOCATION_SIGNAL_ZONE_IN]) {
639                         g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_ZONE_IN]);
640                         handle->sig_id[_LOCATION_SIGNAL_ZONE_IN] = 0;
641                 }
642
643                 if (handle->sig_id[_LOCATION_SIGNAL_ZONE_OUT]) {
644                         g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_ZONE_OUT]);
645                         handle->sig_id[_LOCATION_SIGNAL_ZONE_OUT] = 0;
646                 }
647         }
648
649         int ret = location_stop(handle->object);
650         if (ret != LOCATION_ERROR_NONE)
651                 return __convert_error_code(ret);
652
653         return LOCATIONS_ERROR_NONE;
654 }
655
656 EXPORT_API int location_manager_add_boundary(location_manager_h manager, location_bounds_h bounds)
657 {
658         LOCATIONS_LOGD("location_manager_add_boundary");
659         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
660         LOCATIONS_NULL_ARG_CHECK(manager);
661         LOCATIONS_NULL_ARG_CHECK(bounds);
662
663         location_manager_s *handle = (location_manager_s *) manager;
664         location_bounds_s *bound_handle = (location_bounds_s *) bounds;
665         int ret = location_boundary_add(handle->object, bound_handle->boundary);
666         if (ret != LOCATION_ERROR_NONE)
667                 return __convert_error_code(ret);
668
669         bound_handle->is_added = TRUE;
670         handle->bounds_list = g_list_append(handle->bounds_list, bound_handle);
671         return LOCATIONS_ERROR_NONE;
672 }
673
674 EXPORT_API int location_manager_remove_boundary(location_manager_h manager, location_bounds_h bounds)
675 {
676         LOCATIONS_LOGD("location_manager_remove_boundary");
677         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
678         LOCATIONS_NULL_ARG_CHECK(manager);
679         LOCATIONS_NULL_ARG_CHECK(bounds);
680
681         location_manager_s *handle = (location_manager_s *) manager;
682         location_bounds_s *bound_handle = (location_bounds_s *) bounds;
683         int ret = location_boundary_remove(handle->object, bound_handle->boundary);
684         if (ret != LOCATION_ERROR_NONE)
685                 return __convert_error_code(ret);
686
687         handle->bounds_list = g_list_remove(handle->bounds_list, bound_handle);
688         bound_handle->is_added = FALSE;
689         return LOCATIONS_ERROR_NONE;
690 }
691
692 EXPORT_API int location_manager_foreach_boundary(location_manager_h manager, location_bounds_cb callback, void *user_data)
693 {
694         LOCATIONS_LOGD("location_manager_foreach_boundary");
695         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
696         LOCATIONS_NULL_ARG_CHECK(manager);
697         LOCATIONS_NULL_ARG_CHECK(callback);
698
699         location_manager_s *handle = (location_manager_s *) manager;
700         handle->user_cb[_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS] = callback;
701         handle->user_data[_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS] = user_data;
702         handle->is_continue_foreach_bounds = TRUE;
703         int ret = location_boundary_foreach(handle->object, __foreach_boundary, handle);
704         if (ret != LOCATION_ERROR_NONE)
705                 return __convert_error_code(ret);
706
707         return LOCATIONS_ERROR_NONE;
708 }
709
710 EXPORT_API int location_manager_get_method(location_manager_h manager, location_method_e *method)
711 {
712         LOCATIONS_LOGD("location_manager_get_method %d", method);
713         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
714         LOCATIONS_NULL_ARG_CHECK(manager);
715         LOCATIONS_NULL_ARG_CHECK(method);
716
717         location_manager_s *handle = (location_manager_s *) manager;
718         LocationMethod _method = LOCATION_METHOD_NONE;
719         g_object_get(handle->object, "method", &_method, NULL);
720         switch (_method) {
721         case LOCATION_METHOD_NONE:
722                 *method = LOCATIONS_METHOD_NONE;
723                 break;
724         case LOCATION_METHOD_HYBRID:
725                 *method = LOCATIONS_METHOD_HYBRID;
726                 break;
727         case LOCATION_METHOD_GPS:
728                 *method = LOCATIONS_METHOD_GPS;
729                 break;
730         case LOCATION_METHOD_WPS:
731                 *method = LOCATIONS_METHOD_WPS;
732                 break;
733         case LOCATION_METHOD_PASSIVE:
734                 *method = LOCATIONS_METHOD_PASSIVE;
735                 break;
736         default: {
737                 LOCATIONS_LOGE("[LOCATIONS_ERROR_INVALID_PARAMETER] method : %d ", method);
738                         return LOCATIONS_ERROR_INVALID_PARAMETER;
739                 }
740         }
741         return LOCATIONS_ERROR_NONE;
742 }
743
744 EXPORT_API int location_manager_get_position(location_manager_h manager, double *altitude, double *latitude, double *longitude,
745                                                                                          time_t *timestamp)
746 {
747         LOCATIONS_LOGD("location_manager_get_position");
748         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
749         LOCATIONS_NULL_ARG_CHECK(manager);
750         LOCATIONS_NULL_ARG_CHECK(altitude);
751         LOCATIONS_NULL_ARG_CHECK(latitude);
752         LOCATIONS_NULL_ARG_CHECK(longitude);
753         LOCATIONS_NULL_ARG_CHECK(timestamp);
754
755         location_manager_s *handle = (location_manager_s *) manager;
756         LocationPosition *pos = NULL;
757         LocationAccuracy *acc = NULL;
758         int ret = location_get_position(handle->object, &pos, &acc);
759         if (ret != LOCATION_ERROR_NONE)
760                 return __convert_error_code(ret);
761
762         if (pos->status == LOCATION_STATUS_NO_FIX) {
763                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
764         } else {
765                 *latitude = pos->latitude;
766                 *longitude = pos->longitude;
767                 *altitude = pos->altitude;
768         }
769         *timestamp = pos->timestamp;
770         location_position_free(pos);
771         location_accuracy_free(acc);
772         return LOCATIONS_ERROR_NONE;
773 }
774
775 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)
776 {
777         LOCATIONS_LOGD("location_manager_get_location");
778         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
779         LOCATIONS_NULL_ARG_CHECK(manager);
780         LOCATIONS_NULL_ARG_CHECK(altitude);
781         LOCATIONS_NULL_ARG_CHECK(latitude);
782         LOCATIONS_NULL_ARG_CHECK(longitude);
783         LOCATIONS_NULL_ARG_CHECK(climb);
784         LOCATIONS_NULL_ARG_CHECK(direction);
785         LOCATIONS_NULL_ARG_CHECK(speed);
786         LOCATIONS_NULL_ARG_CHECK(level);
787         LOCATIONS_NULL_ARG_CHECK(horizontal);
788         LOCATIONS_NULL_ARG_CHECK(vertical);
789         LOCATIONS_NULL_ARG_CHECK(timestamp);
790
791         location_manager_s *handle = (location_manager_s *) manager;
792         LocationPosition *pos = NULL;
793         LocationVelocity *vel = NULL;
794         LocationAccuracy *acc = NULL;
795         int ret = location_get_position_ext(handle->object, &pos, &vel, &acc);
796         if (ret != LOCATION_ERROR_NONE)
797                 return __convert_error_code(ret);
798
799         if (pos->status == LOCATION_STATUS_NO_FIX) {
800                 return __convert_error_code(LOCATION_ERROR_NOT_AVAILABLE);
801         } else {
802                 *latitude = pos->latitude;
803                 *longitude = pos->longitude;
804                 *altitude = pos->altitude;
805         }
806         *timestamp = pos->timestamp;
807         *climb = vel->climb;
808         *direction = vel->direction;
809         *speed = vel->speed;
810         *level = acc->level;
811         *horizontal = acc->horizontal_accuracy;
812         *vertical = acc->vertical_accuracy;
813
814         location_position_free(pos);
815         location_velocity_free(vel);
816         location_accuracy_free(acc);
817         return LOCATIONS_ERROR_NONE;
818 }
819
820 EXPORT_API int location_manager_get_velocity(location_manager_h manager, double *climb, double *direction, double *speed, time_t *timestamp)
821 {
822         LOCATIONS_LOGD("location_manager_get_velocity");
823         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
824         LOCATIONS_NULL_ARG_CHECK(manager);
825         LOCATIONS_NULL_ARG_CHECK(climb);
826         LOCATIONS_NULL_ARG_CHECK(direction);
827         LOCATIONS_NULL_ARG_CHECK(speed);
828         LOCATIONS_NULL_ARG_CHECK(timestamp);
829
830         location_manager_s *handle = (location_manager_s *) manager;
831         LocationVelocity *vel = NULL;
832         LocationAccuracy *acc = NULL;
833
834         int ret = location_get_velocity(handle->object, &vel, &acc);
835         if (ret != LOCATION_ERROR_NONE)
836                 return __convert_error_code(ret);
837
838         *climb = vel->climb;
839         *direction = vel->direction;
840         *speed = vel->speed;
841         *timestamp = vel->timestamp;
842         location_velocity_free(vel);
843         location_accuracy_free(acc);
844         return LOCATIONS_ERROR_NONE;
845 }
846
847 EXPORT_API int location_manager_get_accuracy(location_manager_h manager, location_accuracy_level_e *level, double *horizontal,
848                                                                                          double *vertical)
849 {
850         LOCATIONS_LOGD("location_manager_get_accuracy");
851         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
852         LOCATIONS_NULL_ARG_CHECK(manager);
853         LOCATIONS_NULL_ARG_CHECK(level);
854         LOCATIONS_NULL_ARG_CHECK(horizontal);
855         LOCATIONS_NULL_ARG_CHECK(vertical);
856         location_manager_s *handle = (location_manager_s *) manager;
857
858         int ret;
859         LocationPosition *pos = NULL;
860         LocationAccuracy *acc = NULL;
861         ret = location_get_position(handle->object, &pos, &acc);
862         if (ret != LOCATION_ERROR_NONE)
863                 return __convert_error_code(ret);
864
865         if (acc == NULL)
866                 return __convert_error_code(LOCATION_ERROR_NOT_AVAILABLE);
867
868         *level = acc->level;
869         *horizontal = acc->horizontal_accuracy;
870         *vertical = acc->vertical_accuracy;
871         location_position_free(pos);
872         location_accuracy_free(acc);
873         return LOCATIONS_ERROR_NONE;
874 }
875
876 EXPORT_API int location_manager_get_last_position(location_manager_h manager, double *altitude, double *latitude, double *longitude, time_t *timestamp)
877 {
878         LOCATIONS_LOGD("location_manager_get_last_position");
879         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
880         LOCATIONS_NULL_ARG_CHECK(manager);
881         LOCATIONS_NULL_ARG_CHECK(altitude);
882         LOCATIONS_NULL_ARG_CHECK(latitude);
883         LOCATIONS_NULL_ARG_CHECK(longitude);
884         LOCATIONS_NULL_ARG_CHECK(timestamp);
885
886         location_manager_s *handle = (location_manager_s *) manager;
887
888         int ret;
889         LocationPosition *last_pos = NULL;
890         LocationAccuracy *last_acc = NULL;
891         ret = location_get_last_position(handle->object, &last_pos, &last_acc);
892         if (ret != LOCATION_ERROR_NONE)
893                 return __convert_error_code(ret);
894
895         if (last_pos->status == LOCATION_STATUS_NO_FIX) {
896                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
897         } else {
898                 *latitude = last_pos->latitude;
899                 *longitude = last_pos->longitude;
900                 *altitude = last_pos->altitude;
901         }
902         *timestamp = last_pos->timestamp;
903         location_position_free(last_pos);
904         location_accuracy_free(last_acc);
905         return LOCATIONS_ERROR_NONE;
906 }
907
908 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)
909 {
910         LOCATIONS_LOGD("location_manager_get_last_location");
911         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
912         LOCATIONS_NULL_ARG_CHECK(manager);
913         LOCATIONS_NULL_ARG_CHECK(altitude);
914         LOCATIONS_NULL_ARG_CHECK(latitude);
915         LOCATIONS_NULL_ARG_CHECK(longitude);
916         LOCATIONS_NULL_ARG_CHECK(climb);
917         LOCATIONS_NULL_ARG_CHECK(direction);
918         LOCATIONS_NULL_ARG_CHECK(speed);
919         LOCATIONS_NULL_ARG_CHECK(level);
920         LOCATIONS_NULL_ARG_CHECK(horizontal);
921         LOCATIONS_NULL_ARG_CHECK(vertical);
922         LOCATIONS_NULL_ARG_CHECK(timestamp);
923
924         location_manager_s *handle = (location_manager_s *) manager;
925
926         int ret;
927         LocationPosition *last_pos = NULL;
928         LocationVelocity *last_vel = NULL;
929         LocationAccuracy *last_acc = NULL;
930         ret = location_get_last_position_ext(handle->object, &last_pos, &last_vel, &last_acc);
931         if (ret != LOCATION_ERROR_NONE)
932                 return __convert_error_code(ret);
933
934         if (last_pos->status == LOCATION_STATUS_NO_FIX) {
935                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
936         } else {
937                 *latitude = last_pos->latitude;
938                 *longitude = last_pos->longitude;
939                 *altitude = last_pos->altitude;
940         }
941         *timestamp = last_pos->timestamp;
942         *climb = last_vel->climb;
943         *direction = last_vel->direction;
944         *speed = last_vel->speed;
945         *level = last_acc->level;
946         *horizontal = last_acc->horizontal_accuracy;
947         *vertical = last_acc->vertical_accuracy;
948         location_position_free(last_pos);
949         location_velocity_free(last_vel);
950         location_accuracy_free(last_acc);
951         return LOCATIONS_ERROR_NONE;
952 }
953
954 EXPORT_API int location_manager_get_last_velocity(location_manager_h manager, double *climb, double *direction, double *speed, time_t *timestamp)
955 {
956         LOCATIONS_LOGD("location_manager_get_last_velocity");
957         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
958         LOCATIONS_NULL_ARG_CHECK(manager);
959         LOCATIONS_NULL_ARG_CHECK(climb);
960         LOCATIONS_NULL_ARG_CHECK(direction);
961         LOCATIONS_NULL_ARG_CHECK(speed);
962         LOCATIONS_NULL_ARG_CHECK(timestamp);
963
964         location_manager_s *handle = (location_manager_s *) manager;
965
966         int ret;
967         LocationVelocity *last_vel = NULL;
968         LocationAccuracy *last_acc = NULL;
969         ret = location_get_last_velocity(handle->object, &last_vel, &last_acc);
970         if (ret != LOCATION_ERROR_NONE)
971                 return __convert_error_code(ret);
972
973         *climb = last_vel->climb;
974         *direction = last_vel->direction;
975         *speed = last_vel->speed;
976         *timestamp = last_vel->timestamp;
977         location_velocity_free(last_vel);
978         location_accuracy_free(last_acc);
979         return LOCATIONS_ERROR_NONE;
980 }
981
982 EXPORT_API int location_manager_get_last_accuracy(location_manager_h manager, location_accuracy_level_e *level, double *horizontal, double *vertical)
983 {
984         LOCATIONS_LOGD("location_manager_get_last_accuracy");
985         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
986         LOCATIONS_NULL_ARG_CHECK(manager);
987         LOCATIONS_NULL_ARG_CHECK(level);
988         LOCATIONS_NULL_ARG_CHECK(horizontal);
989         LOCATIONS_NULL_ARG_CHECK(vertical);
990         location_manager_s *handle = (location_manager_s *) manager;
991
992         int ret;
993         LocationPosition *last_pos = NULL;
994         LocationAccuracy *last_acc = NULL;
995         ret = location_get_last_position(handle->object, &last_pos, &last_acc);
996         if (ret != LOCATION_ERROR_NONE)
997                 return __convert_error_code(ret);
998
999         *level = last_acc->level;
1000         *horizontal = last_acc->horizontal_accuracy;
1001         *vertical = last_acc->vertical_accuracy;
1002         location_position_free(last_pos);
1003         location_accuracy_free(last_acc);
1004         return LOCATIONS_ERROR_NONE;
1005 }
1006
1007 EXPORT_API int location_manager_get_accessibility_state(location_accessibility_state_e *state)
1008 {
1009         dlog_print(DLOG_WARN, LOG_TAG, "DEPRECATION WARNING: location_manager_get_accessibility_state() is deprecated and will be removed from next release.");
1010         LOCATIONS_LOGD("location_manager_get_accessibility_state");
1011         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1012         LOCATIONS_NULL_ARG_CHECK(state);
1013
1014         int ret = LOCATION_ERROR_NONE;
1015         LocationAccessState auth = LOCATION_ACCESS_NONE;
1016         ret = location_get_accessibility_state(&auth);
1017         if (ret != LOCATION_ERROR_NONE) {
1018                 *state = LOCATIONS_ACCESS_STATE_NONE;
1019                 return __convert_error_code(ret);
1020         }
1021
1022         switch (auth) {
1023         case LOCATION_ACCESS_DENIED:
1024                 *state = LOCATIONS_ACCESS_STATE_DENIED;
1025                 break;
1026         case LOCATION_ACCESS_ALLOWED:
1027                 *state = LOCATIONS_ACCESS_STATE_ALLOWED;
1028                 break;
1029         case LOCATION_ACCESS_NONE:
1030         default:
1031                 *state = LOCATIONS_ACCESS_STATE_NONE;
1032                 break;
1033         }
1034
1035         return LOCATIONS_ERROR_NONE;
1036 }
1037
1038 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)
1039 {
1040         LOCATIONS_LOGD("location_manager_set_distance_updated_cb");
1041         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1042         LOCATIONS_CHECK_CONDITION(interval >= 1 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1043         LOCATIONS_CHECK_CONDITION(distance > 0 && distance <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1044         LOCATIONS_NULL_ARG_CHECK(manager);
1045         location_manager_s *handle = (location_manager_s *) manager;
1046         g_object_set(handle->object, "min-interval", interval, NULL);
1047         g_object_set(handle->object, "min-distance", distance, NULL);
1048         return __set_callback(_LOCATIONS_EVENT_TYPE_DISTANCE, manager, callback, user_data);
1049 }
1050
1051 EXPORT_API int location_manager_unset_distance_based_location_changed_cb(location_manager_h manager)
1052 {
1053         LOCATIONS_LOGD("location_manager_unset_distance_updated_cb");
1054         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1055         LOCATIONS_NULL_ARG_CHECK(manager);
1056         return __unset_callback(_LOCATIONS_EVENT_TYPE_DISTANCE, manager);
1057 }
1058
1059 EXPORT_API int location_manager_set_location_changed_cb(location_manager_h manager, location_changed_cb callback, int interval, void *user_data)
1060 {
1061         LOCATIONS_LOGD("location_manager_set_location_updated_cb");
1062         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1063         LOCATIONS_CHECK_CONDITION(interval >= 1 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1064         LOCATIONS_NULL_ARG_CHECK(manager);
1065         location_manager_s *handle = (location_manager_s *) manager;
1066         g_object_set(handle->object, "loc-interval", interval, NULL);
1067         return __set_callback(_LOCATIONS_EVENT_TYPE_POS_VEL, manager, callback, user_data);
1068 }
1069
1070 EXPORT_API int location_manager_unset_location_changed_cb(location_manager_h manager)
1071 {
1072         LOCATIONS_LOGD("location_manager_unset_position_updated_cb");
1073         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1074         return __unset_callback(_LOCATIONS_EVENT_TYPE_POS_VEL, manager);
1075 }
1076
1077 EXPORT_API int location_manager_set_position_updated_cb(location_manager_h manager, location_position_updated_cb callback, int interval, void *user_data)
1078 {
1079         LOCATIONS_LOGD("location_manager_set_position_updated_cb");
1080         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1081         LOCATIONS_CHECK_CONDITION(interval >= 1 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1082         LOCATIONS_NULL_ARG_CHECK(manager);
1083         location_manager_s *handle = (location_manager_s *) manager;
1084         g_object_set(handle->object, "pos-interval", interval, NULL);
1085         return __set_callback(_LOCATIONS_EVENT_TYPE_POSITION, manager, callback, user_data);
1086 }
1087
1088 EXPORT_API int location_manager_unset_position_updated_cb(location_manager_h manager)
1089 {
1090         LOCATIONS_LOGD("location_manager_unset_position_updated_cb");
1091         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1092         return __unset_callback(_LOCATIONS_EVENT_TYPE_POSITION, manager);
1093 }
1094
1095 EXPORT_API int location_manager_set_velocity_updated_cb(location_manager_h manager, location_velocity_updated_cb callback, int interval, void *user_data)
1096 {
1097         LOCATIONS_LOGD("location_manager_set_velocity_updated_cb");
1098         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1099         LOCATIONS_CHECK_CONDITION(interval >= 1 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1100         LOCATIONS_NULL_ARG_CHECK(manager);
1101         location_manager_s *handle = (location_manager_s *) manager;
1102         g_object_set(handle->object, "vel-interval", interval, NULL);
1103         return __set_callback(_LOCATIONS_EVENT_TYPE_VELOCITY, manager, callback, user_data);
1104 }
1105
1106 EXPORT_API int location_manager_unset_velocity_updated_cb(location_manager_h manager)
1107 {
1108         LOCATIONS_LOGD("location_manager_unset_velocity_updated_cb");
1109         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1110         return __unset_callback(_LOCATIONS_EVENT_TYPE_VELOCITY, manager);
1111 }
1112
1113 EXPORT_API int location_manager_set_service_state_changed_cb(location_manager_h manager, location_service_state_changed_cb callback,
1114                                                                                                                          void *user_data)
1115 {
1116         LOCATIONS_LOGD("location_manager_set_service_state_changed_cb");
1117         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1118         return __set_callback(_LOCATIONS_EVENT_TYPE_SERVICE_STATE, manager, callback, user_data);
1119 }
1120
1121 EXPORT_API int location_manager_unset_service_state_changed_cb(location_manager_h manager)
1122 {
1123         LOCATIONS_LOGD("location_manager_unset_service_state_changed_cb");
1124         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1125         return __unset_callback(_LOCATIONS_EVENT_TYPE_SERVICE_STATE, manager);
1126 }
1127
1128 EXPORT_API int location_manager_set_zone_changed_cb(location_manager_h manager, location_zone_changed_cb callback, void *user_data)
1129 {
1130         LOCATIONS_LOGD("location_manager_set_zone_changed_cb");
1131         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1132         return __set_callback(_LOCATIONS_EVENT_TYPE_BOUNDARY, manager, callback, user_data);
1133 }
1134
1135 EXPORT_API int location_manager_unset_zone_changed_cb(location_manager_h manager)
1136 {
1137         LOCATIONS_LOGD("location_manager_unset_zone_changed_cb");
1138         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1139         return __unset_callback(_LOCATIONS_EVENT_TYPE_BOUNDARY, manager);
1140 }
1141
1142 EXPORT_API int location_manager_set_setting_changed_cb(location_method_e method, location_setting_changed_cb callback, void *user_data)
1143 {
1144         LOCATIONS_LOGD("location_manager_set_setting_changed_cb");
1145         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1146         LOCATIONS_NULL_ARG_CHECK(callback);
1147
1148         LocationMethod _method = __convert_LocationMethod(method);
1149         int ret = LOCATION_ERROR_NONE;
1150
1151         if (_method == LOCATION_METHOD_NONE) {
1152                 return __convert_error_code(LOCATION_ERROR_PARAMETER);
1153         } else if (_method == LOCATION_METHOD_PASSIVE) {
1154                 LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD");
1155                 return LOCATIONS_ERROR_INCORRECT_METHOD;
1156         }
1157
1158         g_location_setting[_method].callback = callback;
1159         g_location_setting[_method].user_data = user_data;
1160
1161         ret = location_add_setting_notify(_method, __setting_changed_cb, &g_location_setting);
1162         if (ret != LOCATION_ERROR_NONE)
1163                 return __convert_error_code(ret);
1164
1165         return LOCATIONS_ERROR_NONE;
1166 }
1167
1168 EXPORT_API int location_manager_unset_setting_changed_cb(location_method_e method)
1169 {
1170         LOCATIONS_LOGD("location_manager_unset_setting_changed_cb");
1171         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1172         LocationMethod _method = __convert_LocationMethod(method);
1173         int ret = LOCATION_ERROR_NONE;
1174
1175         if (_method == LOCATION_METHOD_NONE) {
1176                 return __convert_error_code(LOCATION_ERROR_PARAMETER);
1177         } else if (_method == LOCATION_METHOD_PASSIVE) {
1178                 LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD");
1179                 return LOCATIONS_ERROR_INCORRECT_METHOD;
1180         }
1181
1182         ret = location_ignore_setting_notify(_method, __setting_changed_cb);
1183         if (ret != LOCATION_ERROR_NONE) {
1184                 LOCATIONS_LOGE("Fail to ignore notify. Error[%d]", ret);
1185                 ret = __convert_error_code(ret);
1186         }
1187
1188         g_location_setting[method].callback = NULL;
1189         g_location_setting[method].user_data = NULL;
1190
1191         return LOCATIONS_ERROR_NONE;
1192 }
1193
1194 EXPORT_API int location_manager_get_distance(double start_latitude, double start_longitude, double end_latitude, double end_longitude, double *distance)
1195 {
1196         LOCATIONS_LOGD("location_manager_get_distance");
1197         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1198         LOCATIONS_NULL_ARG_CHECK(distance);
1199         LOCATIONS_CHECK_CONDITION(start_latitude >= -90 && start_latitude <= 90, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1200         LOCATIONS_CHECK_CONDITION(start_longitude >= -180 && start_longitude <= 180, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1201         LOCATIONS_CHECK_CONDITION(end_latitude >= -90 && end_latitude <= 90, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1202         LOCATIONS_CHECK_CONDITION(end_longitude >= -180 && end_longitude <= 180, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1203
1204         int ret = LOCATION_ERROR_NONE;
1205         ulong u_distance;
1206
1207         LocationPosition *start = location_position_new(0, start_latitude, start_longitude, 0, LOCATION_STATUS_2D_FIX);
1208         LocationPosition *end = location_position_new(0, end_latitude, end_longitude, 0, LOCATION_STATUS_2D_FIX);
1209
1210         ret = location_get_distance(start, end, &u_distance);
1211         if (ret != LOCATION_ERROR_NONE)
1212                 return __convert_error_code(ret);
1213
1214         *distance = (double)u_distance;
1215
1216         return LOCATIONS_ERROR_NONE;
1217 }
1218
1219 /*/////////////////////////////////////// */
1220 /* GPS Status & Satellites */
1221 /*////////////////////////////////////// */
1222
1223 EXPORT_API int gps_status_get_nmea(location_manager_h manager, char **nmea)
1224 {
1225         LOCATIONS_LOGD("gps_status_get_nmea");
1226         LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_satellite_supported());
1227         LOCATIONS_NULL_ARG_CHECK(manager);
1228         LOCATIONS_NULL_ARG_CHECK(nmea);
1229         location_manager_s *handle = (location_manager_s *) manager;
1230         char *nmea_data;
1231
1232         int ret = location_get_nmea(handle->object, &nmea_data);
1233         if (ret != LOCATION_ERROR_NONE || nmea == NULL) {
1234                 if (ret == LOCATION_ERROR_NOT_ALLOWED) {
1235                         LOCATIONS_LOGE("LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED");
1236                         return LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED;
1237                 }
1238
1239                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : NMEA is NULL ", LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
1240                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
1241         }
1242
1243         *nmea = g_strdup(nmea_data);
1244         g_free(nmea_data);
1245
1246         return LOCATIONS_ERROR_NONE;
1247 }
1248
1249 EXPORT_API int gps_status_get_satellite(location_manager_h manager, int *num_of_active, int *num_of_inview, time_t *timestamp)
1250 {
1251         LOCATIONS_LOGD("gps_status_get_satellite");
1252         LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_satellite_supported());
1253         LOCATIONS_NULL_ARG_CHECK(manager);
1254         LOCATIONS_NULL_ARG_CHECK(num_of_active);
1255         LOCATIONS_NULL_ARG_CHECK(num_of_inview);
1256         LOCATIONS_NULL_ARG_CHECK(timestamp);
1257         location_manager_s *handle = (location_manager_s *) manager;
1258         LocationSatellite *sat = NULL;
1259         int ret = location_get_satellite(handle->object, &sat);
1260         if (ret != LOCATION_ERROR_NONE || sat == NULL) {
1261                 if (ret == LOCATION_ERROR_NOT_ALLOWED) {
1262                         LOCATIONS_LOGE("LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED");
1263                         return LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED;
1264                 }
1265
1266                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ", LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
1267                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
1268         }
1269
1270         *num_of_active = sat->num_of_sat_used;
1271         *num_of_inview = sat->num_of_sat_inview;
1272         *timestamp = sat->timestamp;
1273         location_satellite_free(sat);
1274         sat = NULL;
1275         return LOCATIONS_ERROR_NONE;
1276 }
1277
1278 EXPORT_API int gps_status_set_satellite_updated_cb(location_manager_h manager, gps_status_satellite_updated_cb callback, int interval, void *user_data)
1279 {
1280         LOCATIONS_LOGD("gps_status_set_satellite_updated_cb");
1281         LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_satellite_supported());
1282         LOCATIONS_CHECK_CONDITION(interval >= 1 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1283         LOCATIONS_NULL_ARG_CHECK(manager);
1284         LOCATIONS_NULL_ARG_CHECK(callback);
1285
1286         location_manager_s *handle = (location_manager_s *) manager;
1287         int ret = location_set_option(handle->object, "USE_SV");
1288         if (ret != LOCATION_ERROR_NONE) {
1289                 if (ret == LOCATION_ERROR_NOT_ALLOWED) {
1290                         LOCATIONS_LOGE("LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED");
1291                         return LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED;
1292                 }
1293                 return ret;
1294         }
1295         g_object_set(handle->object, "sat-interval", interval, NULL);
1296         return __set_callback(_LOCATIONS_EVENT_TYPE_SATELLITE, manager, callback, user_data);
1297 }
1298
1299 EXPORT_API int gps_status_unset_satellite_updated_cb(location_manager_h manager)
1300 {
1301         LOCATIONS_LOGD("gps_status_unset_satellite_updated_cb");
1302         LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_satellite_supported());
1303         return __unset_callback(_LOCATIONS_EVENT_TYPE_SATELLITE, manager);
1304 }
1305
1306 EXPORT_API int gps_status_foreach_satellites_in_view(location_manager_h manager, gps_status_get_satellites_cb callback, void *user_data)
1307 {
1308         LOCATIONS_LOGD("gps_status_foreach_satellites_in_view");
1309         LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_satellite_supported());
1310         LOCATIONS_NULL_ARG_CHECK(manager);
1311         LOCATIONS_NULL_ARG_CHECK(callback);
1312
1313         location_manager_s *handle = (location_manager_s *) manager;
1314         LocationSatellite *sat = NULL;
1315         int ret = location_get_satellite(handle->object, &sat);
1316         if (ret != LOCATION_ERROR_NONE || sat == NULL) {
1317                 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
1318                         LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d", LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
1319                         return LOCATIONS_ERROR_INCORRECT_METHOD;
1320                 } else if (ret == LOCATION_ERROR_NOT_ALLOWED) {
1321                         LOCATIONS_LOGE("LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED");
1322                         return LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED;
1323                 }
1324
1325                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ", LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
1326                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
1327         }
1328
1329         int i;
1330         for (i = 0; i < sat->num_of_sat_inview; i++) {
1331                 guint prn;
1332                 gboolean used;
1333                 guint elevation;
1334                 guint azimuth;
1335                 gint snr;
1336                 location_satellite_get_satellite_details(sat, i, &prn, &used, &elevation, &azimuth, &snr);
1337                 if (callback(azimuth, elevation, prn, snr, used, user_data) != TRUE)
1338                         break;
1339         }
1340         location_satellite_free(sat);
1341         sat = NULL;
1342         return LOCATIONS_ERROR_NONE;
1343 }
1344
1345 EXPORT_API int gps_status_get_last_satellite(location_manager_h manager, int *num_of_active, int *num_of_inview, time_t *timestamp)
1346 {
1347         LOCATIONS_LOGD("gps_status_get_last_satellite");
1348         LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_satellite_supported());
1349         LOCATIONS_NULL_ARG_CHECK(manager);
1350         LOCATIONS_NULL_ARG_CHECK(num_of_active);
1351         LOCATIONS_NULL_ARG_CHECK(num_of_inview);
1352         LOCATIONS_NULL_ARG_CHECK(timestamp);
1353
1354         location_manager_s *handle = (location_manager_s *) manager;
1355         int ret = LOCATION_ERROR_NONE;
1356         LocationSatellite *last_sat = NULL;
1357         ret = location_get_last_satellite(handle->object, &last_sat);
1358         if (ret != LOCATION_ERROR_NONE || last_sat == NULL) {
1359                 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
1360                         LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d", LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
1361                         return LOCATIONS_ERROR_INCORRECT_METHOD;
1362                 } else if (ret == LOCATION_ERROR_NOT_ALLOWED) {
1363                         LOCATIONS_LOGE("LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED");
1364                         return LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED;
1365                 }
1366
1367                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ", LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
1368                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
1369         }
1370
1371         *num_of_active = last_sat->num_of_sat_used;
1372         *num_of_inview = last_sat->num_of_sat_inview;
1373         *timestamp = last_sat->timestamp;
1374         location_satellite_free(last_sat);
1375         return LOCATIONS_ERROR_NONE;
1376 }
1377
1378 EXPORT_API int gps_status_foreach_last_satellites_in_view(location_manager_h manager, gps_status_get_satellites_cb callback, void *user_data)
1379 {
1380         LOCATIONS_LOGD("gps_status_foreach_last_satellites_in_view");
1381         LOCATIONS_NOT_SUPPORTED_CHECK(__is_gps_satellite_supported());
1382         LOCATIONS_NULL_ARG_CHECK(manager);
1383         LOCATIONS_NULL_ARG_CHECK(callback);
1384         location_manager_s *handle = (location_manager_s *) manager;
1385         int ret = LOCATION_ERROR_NONE;
1386         LocationSatellite *last_sat = NULL;
1387
1388         ret = location_get_last_satellite(handle->object, &last_sat);
1389         if (ret != LOCATION_ERROR_NONE || last_sat == NULL) {
1390                 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
1391                         LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d", LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
1392                         return LOCATIONS_ERROR_INCORRECT_METHOD;
1393                 }
1394
1395                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ", LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
1396                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
1397         }
1398
1399         int i;
1400         for (i = 0; i < last_sat->num_of_sat_inview; i++) {
1401                 guint prn;
1402                 gboolean used;
1403                 guint elevation;
1404                 guint azimuth;
1405                 gint snr;
1406                 location_satellite_get_satellite_details(last_sat, i, &prn, &used, &elevation, &azimuth, &snr);
1407                 if (callback(azimuth, elevation, prn, snr, used, user_data) != TRUE)
1408                         break;
1409         }
1410         location_satellite_free(last_sat);
1411         return LOCATIONS_ERROR_NONE;
1412 }
1413
1414
1415 /**
1416  * Tizen 3.0
1417  */
1418 EXPORT_API int location_manager_set_location_batch_cb(location_manager_h manager, location_batch_cb callback, int batch_interval, int batch_period, void *user_data)
1419 {
1420         LOCATIONS_LOGD("location_manager_set_location_batch_cb");
1421         LOCATIONS_NOT_SUPPORTED_CHECK(__is_batch_supported());
1422
1423         LOCATIONS_CHECK_CONDITION(batch_interval >= 1 && batch_interval <= 255, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1424         LOCATIONS_CHECK_CONDITION(batch_period >= 1 && batch_period <= 60000, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1425         LOCATIONS_CHECK_CONDITION(batch_interval <= batch_period, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1426         LOCATIONS_NULL_ARG_CHECK(manager);
1427         LOCATIONS_NULL_ARG_CHECK(callback);
1428         location_manager_s *handle = (location_manager_s *) manager;
1429         g_object_set(handle->object, "batch-period", batch_period, NULL);
1430         g_object_set(handle->object, "batch-interval", batch_interval, NULL);
1431         return __set_callback(_LOCATIONS_EVENT_TYPE_BATCH, manager, callback, user_data);
1432 }
1433
1434 EXPORT_API int location_manager_unset_location_batch_cb(location_manager_h manager)
1435 {
1436         LOCATIONS_LOGD("location_manager_unset_location_batch_cb");
1437         LOCATIONS_NOT_SUPPORTED_CHECK(__is_batch_supported());
1438         return __unset_callback(_LOCATIONS_EVENT_TYPE_BATCH, manager);
1439 }
1440
1441 EXPORT_API int location_manager_start_batch(location_manager_h manager)
1442 {
1443         LOCATIONS_LOGD("location_manager_start_batch");
1444         LOCATIONS_NOT_SUPPORTED_CHECK(__is_batch_supported());
1445         LOCATIONS_NULL_ARG_CHECK(manager);
1446         location_manager_s *handle = (location_manager_s *) manager;
1447
1448         if (LOCATIONS_METHOD_GPS == handle->method) {
1449                 if (!handle->sig_id[_LOCATION_SIGNAL_BATCH_UPDATED])
1450                         handle->sig_id[_LOCATION_SIGNAL_BATCH_UPDATED] = g_signal_connect(handle->object, "batch-updated", G_CALLBACK(__cb_batch_updated), handle);
1451         } else {
1452                 LOCATIONS_LOGE("method is not GPS [LOCATIONS_ERROR_INCORRECT_METHOD]");
1453                 return LOCATIONS_ERROR_INCORRECT_METHOD;
1454         }
1455
1456         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_BATCH] != NULL)
1457                 LOCATIONS_LOGD("batch status set : Start");
1458
1459         int ret = location_start_batch(handle->object);
1460         if (ret != LOCATION_ERROR_NONE)
1461                 return __convert_error_code(ret);
1462
1463         return LOCATIONS_ERROR_NONE;
1464 }
1465
1466 EXPORT_API int location_manager_stop_batch(location_manager_h manager)
1467 {
1468         LOCATIONS_LOGD("location_manager_stop_batch");
1469         LOCATIONS_NOT_SUPPORTED_CHECK(__is_batch_supported());
1470         LOCATIONS_NULL_ARG_CHECK(manager);
1471         location_manager_s *handle = (location_manager_s *) manager;
1472
1473         if (LOCATIONS_METHOD_GPS == handle->method) {
1474                 if (handle->sig_id[_LOCATION_SIGNAL_BATCH_UPDATED]) {
1475                         g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_BATCH_UPDATED]);
1476                         handle->sig_id[_LOCATION_SIGNAL_BATCH_UPDATED] = 0;
1477                 }
1478         }
1479
1480         int ret = location_stop_batch(handle->object);
1481         if (ret != LOCATION_ERROR_NONE)
1482                 return __convert_error_code(ret);
1483
1484         return LOCATIONS_ERROR_NONE;
1485 }
1486
1487 EXPORT_API int location_manager_foreach_location_batch(location_manager_h manager, location_batch_get_location_cb callback, void *user_data)
1488 {
1489         LOCATIONS_LOGD("location_manager_foreach_location_batch");
1490         LOCATIONS_NOT_SUPPORTED_CHECK(__is_batch_supported());
1491         LOCATIONS_NULL_ARG_CHECK(manager);
1492         LOCATIONS_NULL_ARG_CHECK(callback);
1493         location_manager_s *handle = (location_manager_s *) manager;
1494         LocationBatch *batch = NULL;
1495
1496         int ret = location_get_batch(handle->object, &batch);
1497         if (ret != LOCATION_ERROR_NONE || batch == NULL) {
1498                 if (ret == LOCATION_ERROR_NOT_ALLOWED) {
1499                         LOCATIONS_LOGE("LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED");
1500                         return LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED;
1501                 }
1502
1503                 LOCATIONS_LOGE("Batch is NULL [LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE]");
1504                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
1505         }
1506
1507         int i;
1508         for (i = 0; i < batch->num_of_location; i++) {
1509                 gdouble latitude;
1510                 gdouble longitude;
1511                 gdouble altitude;
1512                 gdouble speed;
1513                 gdouble direction;
1514                 gdouble h_accuracy;
1515                 gdouble v_accuracy;
1516                 guint timestamp;
1517
1518                 location_get_batch_details(batch, i, &latitude, &longitude, &altitude, &speed, &direction, &h_accuracy, &v_accuracy, &timestamp);
1519                 if (callback(latitude, longitude, altitude, speed, direction, h_accuracy, v_accuracy, timestamp, user_data) != TRUE)
1520                         break;
1521         }
1522         location_batch_free(batch);
1523         batch = NULL;
1524         return LOCATIONS_ERROR_NONE;
1525 }
1526
1527 EXPORT_API int location_manager_is_enabled_mock_location(bool *enabled)
1528 {
1529         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1530         LOCATIONS_NULL_ARG_CHECK(enabled);
1531         int is_enabled_val = -1;
1532         int ret = location_is_enabled_method(INTERNAL_METHOD_MOCK, &is_enabled_val);
1533         if (ret != LOCATION_ERROR_NONE)
1534                 return __convert_error_code(ret);
1535
1536         if (is_enabled_val == -1)
1537                 return TIZEN_ERROR_PERMISSION_DENIED;
1538
1539         *enabled = (is_enabled_val == 0) ? FALSE : TRUE;
1540         return LOCATIONS_ERROR_NONE;
1541 }
1542
1543 EXPORT_API int location_manager_enable_mock_location(const bool enable)
1544 {
1545         LOCATIONS_LOGD("enable: %d", enable);
1546         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1547         int ret = LOCATION_ERROR_NONE;
1548
1549         ret = location_enable_mock(enable);
1550         return __convert_error_code(ret);
1551 }
1552
1553 EXPORT_API int location_manager_set_mock_location(location_manager_h manager, const double latitude, const double longitude, const double altitude,
1554         const double speed, const double direction, const double accuracy)
1555 {
1556         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1557         LOCATIONS_NULL_ARG_CHECK(manager);
1558
1559         LOCATIONS_CHECK_CONDITION(latitude >= -90 && latitude <= 90, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1560         LOCATIONS_CHECK_CONDITION(longitude >= -180 && longitude <= 180, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1561         LOCATIONS_CHECK_CONDITION(direction >= 0 && direction <= 360, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
1562
1563         location_manager_s *handle = (location_manager_s *) manager;
1564         int ret = LOCATION_ERROR_NONE;
1565         int enabled;
1566         LocationPosition *pos = NULL;
1567         LocationVelocity *vel = NULL;
1568         LocationAccuracy *acc = NULL;
1569
1570         ret = location_is_enabled_method(INTERNAL_METHOD_MOCK, &enabled);
1571         LOCATIONS_LOGD("enable: %d, ret: %d", enabled, ret);
1572         if (ret == LOCATIONS_ERROR_NONE) {
1573                 if (enabled == 0)
1574                         return __convert_error_code(LOCATION_ERROR_SETTING_OFF);
1575         } else {
1576                 return __convert_error_code(ret);
1577         }
1578
1579         pos = location_position_new(0, latitude, longitude, 0, LOCATION_STATUS_3D_FIX);
1580         if (!pos) {
1581                 LOCATIONS_LOGE("Failed to create position");
1582                 return LOCATIONS_ERROR_OUT_OF_MEMORY;
1583         }
1584         vel = location_velocity_new(0, speed, direction, 0);
1585         if (!vel) {
1586                 LOCATIONS_LOGE("Failed to create velocity");
1587                 location_position_free(pos);
1588                 return LOCATIONS_ERROR_OUT_OF_MEMORY;
1589         }
1590
1591         acc = location_accuracy_new(LOCATION_ACCURACY_LEVEL_DETAILED, accuracy, -1);
1592         if (!acc) {
1593                 LOCATIONS_LOGE("Failed to create accuracy");
1594                 location_position_free(pos);
1595                 location_velocity_free(vel);
1596                 return LOCATIONS_ERROR_OUT_OF_MEMORY;
1597         }
1598
1599         ret = location_set_mock_location(handle->object, pos, vel, acc);
1600
1601         location_position_free(pos);
1602         location_velocity_free(vel);
1603         location_accuracy_free(acc);
1604
1605         return __convert_error_code(ret);
1606 }
1607
1608 EXPORT_API int location_manager_clear_mock_location(location_manager_h manager)
1609 {
1610         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
1611         LOCATIONS_NULL_ARG_CHECK(manager);
1612
1613         location_manager_s *handle = (location_manager_s *) manager;
1614         int enabled;
1615
1616         int ret = location_is_enabled_method(INTERNAL_METHOD_MOCK, &enabled);
1617         LOCATIONS_LOGD("enable: %d, ret: %d", enabled, ret);
1618         if (ret == LOCATIONS_ERROR_NONE) {
1619                 if (enabled == 0)
1620                         return __convert_error_code(LOCATION_ERROR_SETTING_OFF);
1621         } else {
1622                 return __convert_error_code(ret);
1623         }
1624
1625         ret = location_clear_mock_location(handle->object);
1626         return __convert_error_code(ret);
1627 }