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