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