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