2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <locations_private.h>
21 #include <location_bounds.h>
24 #define LOCATIONS_NULL_ARG_CHECK(arg) \
25 LOCATIONS_CHECK_CONDITION((arg != NULL),LOCATIONS_ERROR_INVALID_PARAMETER,"LOCATIONS_ERROR_INVALID_PARAMETER") \
28 * Internal Implementation
31 static int __convert_error_code(int code)
34 char *msg = "LOCATIONS_ERROR_NONE";
36 case LOCATION_ERROR_NONE:
37 ret = LOCATIONS_ERROR_NONE;
38 msg = "LOCATIONS_ERROR_NONE";
40 case LOCATION_ERROR_NETWORK_FAILED:
41 case LOCATION_ERROR_NETWORK_NOT_CONNECTED:
42 ret = LOCATIONS_ERROR_NETWORK_FAILED;
43 msg = "LOCATIONS_ERROR_NETWORK_FAILED";
45 case LOCATION_ERROR_NOT_ALLOWED:
46 ret = LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED;
47 msg = "LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED";
49 case LOCATION_ERROR_SETTING_OFF:
50 ret = LOCATIONS_ERROR_GPS_SETTING_OFF;
51 msg = "LOCATIONS_ERROR_GPS_SETTING_OFF";
53 case LOCATION_ERROR_SECURITY_DENIED:
54 ret = LOCATIONS_ERROR_SECURITY_RESTRICTED;
55 msg = "LOCATIONS_ERROR_SECURITY_RESTRICTED";
57 case LOCATION_ERROR_NOT_AVAILABLE:
58 case LOCATION_ERROR_CONFIGURATION:
59 case LOCATION_ERROR_PARAMETER:
60 case LOCATION_ERROR_UNKNOWN:
62 msg = "LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE";
63 ret = LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
65 LOCATIONS_LOGE("%s(0x%08x) : core fw error(0x%x)", msg, ret, code);
69 static void __cb_service_updated(GObject * self, guint type, gpointer data, gpointer accuracy, gpointer userdata)
71 LOCATIONS_LOGI("Callback function has been invoked. ");
72 location_manager_s *handle = (location_manager_s *) userdata;
73 if (type == VELOCITY_UPDATED && handle->user_cb[_LOCATIONS_EVENT_TYPE_VELOCITY]) {
74 LocationVelocity *vel = (LocationVelocity *) data;
75 LOCATIONS_LOGI("Current velocity: timestamp : %d, speed: %f, direction : %f, climb : %f",
76 vel->timestamp, vel->speed, vel->direction, vel->climb);
77 ((location_velocity_updated_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_VELOCITY]) (vel->speed, vel->direction,
78 vel->climb, vel->timestamp,
80 [_LOCATIONS_EVENT_TYPE_VELOCITY]);
82 else if (type == POSITION_UPDATED && handle->user_cb[_LOCATIONS_EVENT_TYPE_POSITION]) {
83 LocationPosition *pos = (LocationPosition *) data;
84 LOCATIONS_LOGI("Current position: timestamp : %d, latitude : %f, altitude: %f, longitude: %f",
85 pos->timestamp, pos->latitude, pos->altitude, pos->longitude);
86 ((location_position_updated_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_POSITION]) (pos->latitude, pos->longitude,
87 pos->altitude, pos->timestamp,
89 [_LOCATIONS_EVENT_TYPE_POSITION]);
91 else if (type == SATELLITE_UPDATED && handle->user_cb[_LOCATIONS_EVENT_TYPE_SATELLITE]) {
92 LocationSatellite *sat = (LocationSatellite *)data;
93 LOCATIONS_LOGI("Current satellite information: timestamp : %d, number of active : %d, number of inview : %d",
94 sat->timestamp, sat->num_of_sat_used, sat->num_of_sat_inview);
95 ((gps_status_satellite_updated_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_SATELLITE]) (sat->num_of_sat_used, sat->num_of_sat_inview,
96 sat->timestamp, handle->user_data[_LOCATIONS_EVENT_TYPE_SATELLITE]);
100 static void __cb_service_enabled(GObject * self, guint status, gpointer userdata)
102 LOCATIONS_LOGI("Callback function has been invoked. ");
103 location_manager_s *handle = (location_manager_s *) userdata;
104 if (handle->user_cb[_LOCATIONS_EVENT_TYPE_SERVICE_STATE]) {
105 ((location_service_state_changed_cb)
106 handle->user_cb[_LOCATIONS_EVENT_TYPE_SERVICE_STATE]) (LOCATIONS_SERVICE_ENABLED,
107 handle->user_data[_LOCATIONS_EVENT_TYPE_SERVICE_STATE]);
111 static void __cb_service_disabled(GObject * self, guint status, gpointer userdata)
113 LOCATIONS_LOGI("Callback function has been invoked. ");
114 location_manager_s *handle = (location_manager_s *) userdata;
115 if (handle->user_cb[_LOCATIONS_EVENT_TYPE_SERVICE_STATE])
116 ((location_service_state_changed_cb)
117 handle->user_cb[_LOCATIONS_EVENT_TYPE_SERVICE_STATE]) (LOCATIONS_SERVICE_DISABLED,
118 handle->user_data[_LOCATIONS_EVENT_TYPE_SERVICE_STATE]);
121 static int __compare_position (gconstpointer a, gconstpointer b)
123 if(location_position_equal((LocationPosition*) a, (LocationPosition *)b) == TRUE) {
130 static int __boundary_compare(LocationBoundary * bound1, LocationBoundary * bound2)
134 if (bound1->type == bound2->type) {
135 switch (bound1->type) {
136 case LOCATION_BOUNDARY_CIRCLE:
137 if (location_position_equal(bound1->circle.center, bound2->circle.center) && bound1->circle.radius == bound2->circle.radius) {
141 case LOCATION_BOUNDARY_RECT:
142 if (location_position_equal(bound1->rect.left_top, bound2->rect.left_top) && location_position_equal(bound1->rect.right_bottom, bound2->rect.right_bottom)) {
146 case LOCATION_BOUNDARY_POLYGON: {
147 GList *boundary1_next = NULL;
148 GList *boundary2_start = NULL, *boundary2_prev = NULL, *boundary2_next = NULL;
149 if (g_list_length(bound1->polygon.position_list) != g_list_length(bound2->polygon.position_list)) {
153 boundary2_start = g_list_find_custom(bound2->polygon.position_list, g_list_nth_data(bound1->polygon.position_list, 0), (GCompareFunc) __compare_position);
154 if (boundary2_start == NULL) return -1;
156 boundary2_prev = g_list_previous(boundary2_start);
157 boundary2_next = g_list_next(boundary2_start);
159 if (boundary2_prev == NULL) boundary2_prev = g_list_last(bound2->polygon.position_list);
160 if (boundary2_next == NULL) boundary2_next = g_list_first(bound2->polygon.position_list);
162 boundary1_next = g_list_next(bound1->polygon.position_list);
163 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*)boundary2_prev->data) == TRUE) {
164 boundary1_next = g_list_next(boundary1_next);
165 while (boundary1_next) {
166 boundary2_prev = g_list_previous(boundary2_prev);
167 if (boundary2_prev == NULL) boundary2_prev = g_list_last(bound2->polygon.position_list);
168 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*) boundary2_prev->data) == FALSE) {
171 boundary1_next = g_list_next(boundary1_next);
174 } else if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*)boundary2_next->data) == TRUE) {
175 boundary1_next = g_list_next(boundary1_next);
176 while(boundary1_next) {
177 boundary2_next = g_list_next(boundary2_next);
178 if (boundary2_next == NULL) boundary2_next = g_list_first(bound2->polygon.position_list);
179 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*) boundary2_next->data) == FALSE) {
182 boundary1_next = g_list_next(boundary1_next);
197 static void __cb_zone_in(GObject * self, gpointer boundary, gpointer position, gpointer accuracy, gpointer userdata)
199 LOCATIONS_LOGI("Callback function has been invoked.");
200 location_manager_s *handle = (location_manager_s *) userdata;
201 if (handle->user_cb[_LOCATIONS_EVENT_TYPE_BOUNDARY]) {
202 LocationPosition *pos = (LocationPosition *) position;
203 ((location_zone_changed_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_BOUNDARY]) (LOCATIONS_BOUNDARY_IN,
204 pos->latitude, pos->longitude,
205 pos->altitude, pos->timestamp,
207 [_LOCATIONS_EVENT_TYPE_BOUNDARY]);
210 location_bounds_s *bounds;
211 GList *bounds_list = g_list_first(handle->bounds_list);
212 while (bounds_list) {
213 bounds = (location_bounds_s *)bounds_list->data;
214 if (__boundary_compare(boundary, bounds->boundary) == 0) {
215 LOCATIONS_LOGI("Find zone in boundary");
216 ((location_bounds_state_changed_cb) bounds->user_cb) (LOCATIONS_BOUNDARY_IN, bounds->user_data);
219 bounds_list = g_list_next(bounds_list);
223 static void __cb_zone_out(GObject * self, gpointer boundary, gpointer position, gpointer accuracy, gpointer userdata)
225 LOCATIONS_LOGI("Callback function has been invoked.");
226 location_manager_s *handle = (location_manager_s *) userdata;
227 if (handle->user_cb[_LOCATIONS_EVENT_TYPE_BOUNDARY]) {
228 LocationPosition *pos = (LocationPosition *) position;
229 ((location_zone_changed_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_BOUNDARY]) (LOCATIONS_BOUNDARY_OUT,
230 pos->latitude, pos->longitude,
231 pos->altitude, pos->timestamp,
233 [_LOCATIONS_EVENT_TYPE_BOUNDARY]);
236 location_bounds_s *bounds;
237 GList *bounds_list = g_list_first(handle->bounds_list);
238 while (bounds_list) {
239 bounds = (location_bounds_s *)bounds_list->data;
240 if (__boundary_compare(boundary, bounds->boundary) == 0) {
241 LOCATIONS_LOGI("Find zone out boundary");
242 ((location_bounds_state_changed_cb) bounds->user_cb) (LOCATIONS_BOUNDARY_OUT, bounds->user_data);
245 bounds_list = g_list_next(bounds_list);
249 static int __set_callback(_location_event_e type, location_manager_h manager, void *callback, void *user_data)
251 LOCATIONS_NULL_ARG_CHECK(manager);
252 LOCATIONS_NULL_ARG_CHECK(callback);
253 location_manager_s *handle = (location_manager_s *) manager;
254 handle->user_cb[type] = callback;
255 handle->user_data[type] = user_data;
256 LOCATIONS_LOGI("event type : %d. ", type);
257 return LOCATIONS_ERROR_NONE;
260 static int __unset_callback(_location_event_e type, location_manager_h manager)
262 LOCATIONS_NULL_ARG_CHECK(manager);
263 location_manager_s *handle = (location_manager_s *) manager;
264 handle->user_cb[type] = NULL;
265 handle->user_data[type] = NULL;
266 LOCATIONS_LOGI("event type : %d. ", type);
267 return LOCATIONS_ERROR_NONE;
270 static void __foreach_boundary(LocationBoundary * boundary, void *user_data)
272 location_manager_s *handle = (location_manager_s *) user_data;
274 if (handle != NULL && boundary != NULL) {
276 location_bounds_h bounds;
277 if (boundary->type == LOCATION_BOUNDARY_CIRCLE) {
278 location_coords_s center;
279 center.latitude = boundary->circle.center->latitude;
280 center.longitude = boundary->circle.center->longitude;
281 ret = location_bounds_create_circle(center, boundary->circle.radius, &bounds);
282 } else if (boundary->type == LOCATION_BOUNDARY_RECT) {
283 location_coords_s left_top;
284 location_coords_s right_bottom;
285 left_top.latitude = boundary->rect.left_top->latitude;
286 left_top.longitude = boundary->rect.left_top->longitude;
287 right_bottom.latitude = boundary->rect.right_bottom->latitude;
288 right_bottom.longitude = boundary->rect.right_bottom->longitude;
289 ret = location_bounds_create_rect(left_top, right_bottom, &bounds);
290 } else if (boundary->type == LOCATION_BOUNDARY_POLYGON) {
291 GList *list = boundary->polygon.position_list;
292 int size = g_list_length(list);
294 location_coords_s coords[size];
297 LocationPosition *pos = list->data;
298 coords[cnt].latitude = pos->latitude;
299 coords[cnt].longitude = pos->longitude;
300 list = g_list_next(list);
303 ret = location_bounds_create_polygon(coords, size, &bounds);
306 LOCATIONS_LOGI("Invalid boundary type : %d", boundary->type);
309 if (ret != LOCATIONS_ERROR_NONE) {
310 LOCATIONS_LOGI("Failed to create location_bounds : (0x%08x) ", ret);
312 if (handle->is_continue_foreach_bounds) {
313 handle->is_continue_foreach_bounds =
314 ((location_bounds_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS]) (bounds,
317 [_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS]);
319 location_bounds_destroy(bounds);
322 LOCATIONS_LOGI("__foreach_boundary() has been failed");
326 /////////////////////////////////////////
328 ////////////////////////////////////////
331 * Public Implementation
334 bool location_manager_is_supported_method(location_method_e method)
336 LocationMethod _method = LOCATION_METHOD_NONE;
338 case LOCATIONS_METHOD_HYBRID:
339 _method = LOCATION_METHOD_HYBRID;
341 case LOCATIONS_METHOD_GPS:
342 _method = LOCATION_METHOD_GPS;
344 case LOCATIONS_METHOD_WPS:
345 _method = LOCATION_METHOD_WPS;
347 case LOCATIONS_METHOD_CPS:
348 _method = LOCATION_METHOD_CPS;
351 _method = LOCATION_METHOD_NONE;
354 return location_is_supported_method(_method);
357 int location_manager_create(location_method_e method, location_manager_h * manager)
359 LOCATIONS_NULL_ARG_CHECK(manager);
360 if (location_init() != LOCATION_ERROR_NONE)
361 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
363 LocationMethod _method = LOCATION_METHOD_NONE;
365 case LOCATIONS_METHOD_HYBRID:
366 _method = LOCATION_METHOD_HYBRID;
368 case LOCATIONS_METHOD_GPS:
369 _method = LOCATION_METHOD_GPS;
371 case LOCATIONS_METHOD_WPS:
372 _method = LOCATION_METHOD_WPS;
374 case LOCATIONS_METHOD_CPS:
375 _method = LOCATION_METHOD_CPS;
377 case LOCATIONS_METHOD_NONE:
378 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
381 LOCATIONS_LOGE("LOCATIONS_ERROR_INVALID_PARAMETER(0x%08x) : Out of range (location_method_e) - method : %d ",
382 LOCATIONS_ERROR_INVALID_PARAMETER, method);
383 return LOCATIONS_ERROR_INVALID_PARAMETER;
387 location_manager_s *handle = (location_manager_s *) malloc(sizeof(location_manager_s));
388 if (handle == NULL) {
389 LOCATIONS_LOGE("OUT_OF_MEMORY(0x%08x)", LOCATIONS_ERROR_OUT_OF_MEMORY);
390 return LOCATIONS_ERROR_OUT_OF_MEMORY;
393 memset(handle, 0, sizeof(location_manager_s));
395 handle->object = location_new(_method);
396 if (handle->object == NULL) {
397 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : fail to location_new",
398 LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
400 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
402 handle->method = method;
403 handle->is_continue_foreach_bounds = TRUE;
404 handle->bounds_list = NULL;
405 *manager = (location_manager_h) handle;
406 return LOCATIONS_ERROR_NONE;
409 int location_manager_destroy(location_manager_h manager)
411 LOCATIONS_NULL_ARG_CHECK(manager);
412 location_manager_s *handle = (location_manager_s *) manager;
414 int ret = location_free(handle->object);
415 if (ret != LOCATIONS_ERROR_NONE) {
416 return __convert_error_code(ret);
419 return LOCATIONS_ERROR_NONE;
422 int location_manager_start(location_manager_h manager)
424 LOCATIONS_NULL_ARG_CHECK(manager);
425 location_manager_s *handle = (location_manager_s *) manager;
427 g_signal_connect(handle->object, "service-enabled", G_CALLBACK(__cb_service_enabled), handle);
428 g_signal_connect(handle->object, "service-disabled", G_CALLBACK(__cb_service_disabled), handle);
429 g_signal_connect(handle->object, "service-updated", G_CALLBACK(__cb_service_updated), handle);
430 g_signal_connect(handle->object, "zone-in", G_CALLBACK(__cb_zone_in), handle);
431 g_signal_connect(handle->object, "zone-out", G_CALLBACK(__cb_zone_out), handle);
433 int ret = location_start(handle->object);
434 if (ret != LOCATION_ERROR_NONE) {
435 return __convert_error_code(ret);
437 return LOCATIONS_ERROR_NONE;
440 int location_manager_stop(location_manager_h manager)
442 LOCATIONS_NULL_ARG_CHECK(manager);
443 location_manager_s *handle = (location_manager_s *) manager;
445 int ret = location_stop(handle->object);
446 if (ret != LOCATION_ERROR_NONE) {
447 return __convert_error_code(ret);
449 return LOCATIONS_ERROR_NONE;
452 int location_manager_add_boundary(location_manager_h manager, location_bounds_h bounds)
454 LOCATIONS_NULL_ARG_CHECK(manager);
455 LOCATIONS_NULL_ARG_CHECK(bounds);
457 location_manager_s *handle = (location_manager_s *) manager;
458 location_bounds_s *bound_handle = (location_bounds_s *) bounds;
459 int ret = location_boundary_add(handle->object, bound_handle->boundary);
460 if (ret != LOCATION_ERROR_NONE) {
461 return __convert_error_code(ret);
463 bound_handle->is_added = TRUE;
464 handle->bounds_list = g_list_append(handle->bounds_list, bound_handle);
465 return LOCATIONS_ERROR_NONE;
468 int location_manager_remove_boundary(location_manager_h manager, location_bounds_h bounds)
470 LOCATIONS_NULL_ARG_CHECK(manager);
471 LOCATIONS_NULL_ARG_CHECK(bounds);
473 location_manager_s *handle = (location_manager_s *) manager;
474 location_bounds_s *bound_handle = (location_bounds_s *) bounds;
475 int ret = location_boundary_remove(handle->object, bound_handle->boundary);
476 if (ret != LOCATION_ERROR_NONE) {
477 return __convert_error_code(ret);
479 handle->bounds_list = g_list_remove(handle->bounds_list, bound_handle);
480 bound_handle->is_added = FALSE;
481 return LOCATIONS_ERROR_NONE;
484 int location_manager_foreach_boundary(location_manager_h manager, location_bounds_cb callback, void *user_data)
486 LOCATIONS_NULL_ARG_CHECK(manager);
487 LOCATIONS_NULL_ARG_CHECK(callback);
489 location_manager_s *handle = (location_manager_s *) manager;
490 handle->user_cb[_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS] = callback;
491 handle->user_data[_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS] = user_data;
492 handle->is_continue_foreach_bounds = TRUE;
493 int ret = location_boundary_foreach(handle->object, __foreach_boundary, handle);
494 if (ret != LOCATION_ERROR_NONE) {
495 return __convert_error_code(ret);
497 return LOCATIONS_ERROR_NONE;
500 int location_manager_get_method(location_manager_h manager, location_method_e * method)
502 LOCATIONS_NULL_ARG_CHECK(manager);
503 LOCATIONS_NULL_ARG_CHECK(method);
504 location_manager_s *handle = (location_manager_s *) manager;
505 LocationMethod _method = LOCATION_METHOD_NONE;
506 g_object_get(handle->object, "method", &_method, NULL);
508 case LOCATION_METHOD_NONE:
509 *method = LOCATIONS_METHOD_NONE;
511 case LOCATION_METHOD_HYBRID:
512 *method = LOCATIONS_METHOD_HYBRID;
514 case LOCATION_METHOD_GPS:
515 *method = LOCATIONS_METHOD_GPS;
517 case LOCATION_METHOD_WPS:
518 *method = LOCATIONS_METHOD_WPS;
520 case LOCATION_METHOD_CPS:
521 *method = LOCATIONS_METHOD_CPS;
525 LOCATIONS_LOGE("LOCATIONS_ERROR_INVALID_PARAMETER(0x%08x) : Out of range (location_method_e) - method : %d ",
526 LOCATIONS_ERROR_INVALID_PARAMETER, method);
527 return LOCATIONS_ERROR_INVALID_PARAMETER;
530 //*method = handle->method;
531 return LOCATIONS_ERROR_NONE;
534 int location_manager_get_position(location_manager_h manager, double *altitude, double *latitude, double *longitude,
537 LOCATIONS_NULL_ARG_CHECK(manager);
538 LOCATIONS_NULL_ARG_CHECK(altitude);
539 LOCATIONS_NULL_ARG_CHECK(latitude);
540 LOCATIONS_NULL_ARG_CHECK(longitude);
541 LOCATIONS_NULL_ARG_CHECK(timestamp);
543 location_manager_s *handle = (location_manager_s *) manager;
545 LocationPosition *pos = NULL;
546 LocationAccuracy *acc = NULL;
547 ret = location_get_position(handle->object, &pos, &acc);
548 if (ret != LOCATION_ERROR_NONE) {
549 return __convert_error_code(ret);
552 if (pos->status == LOCATION_STATUS_NO_FIX) {
557 if (pos->status == LOCATION_STATUS_3D_FIX) {
558 *altitude = pos->altitude;
562 *latitude = pos->latitude;
563 *longitude = pos->longitude;
565 *timestamp = pos->timestamp;
566 location_position_free(pos);
567 location_accuracy_free(acc);
568 return LOCATIONS_ERROR_NONE;
571 int location_manager_get_velocity(location_manager_h manager, double *climb, double *direction, double *speed, time_t * timestamp)
573 LOCATIONS_NULL_ARG_CHECK(manager);
574 LOCATIONS_NULL_ARG_CHECK(climb);
575 LOCATIONS_NULL_ARG_CHECK(direction);
576 LOCATIONS_NULL_ARG_CHECK(speed);
577 LOCATIONS_NULL_ARG_CHECK(timestamp);
579 location_manager_s *handle = (location_manager_s *) manager;
581 LocationVelocity *vel = NULL;
582 LocationAccuracy *acc = NULL;
583 ret = location_get_velocity(handle->object, &vel, &acc);
584 if (ret != LOCATION_ERROR_NONE) {
585 return __convert_error_code(ret);
589 *direction = vel->direction;
591 *timestamp = vel->timestamp;
592 location_velocity_free(vel);
593 location_accuracy_free(acc);
594 return LOCATIONS_ERROR_NONE;
597 int location_manager_get_accuracy(location_manager_h manager, location_accuracy_level_e * level, double *horizontal,
600 LOCATIONS_NULL_ARG_CHECK(manager);
601 LOCATIONS_NULL_ARG_CHECK(level);
602 LOCATIONS_NULL_ARG_CHECK(horizontal);
603 LOCATIONS_NULL_ARG_CHECK(vertical);
604 location_manager_s *handle = (location_manager_s *) manager;
607 LocationPosition *pos = NULL;
608 LocationAccuracy *acc = NULL;
609 ret = location_get_position(handle->object, &pos, &acc);
610 if (ret != LOCATION_ERROR_NONE) {
611 return __convert_error_code(ret);
615 return __convert_error_code(LOCATION_ERROR_NOT_AVAILABLE);
619 *horizontal = acc->horizontal_accuracy;
620 *vertical = acc->vertical_accuracy;
621 location_position_free(pos);
622 location_accuracy_free(acc);
623 return LOCATIONS_ERROR_NONE;
626 int location_manager_get_last_position(location_manager_h manager, double *altitude, double *latitude, double *longitude,
629 LOCATIONS_NULL_ARG_CHECK(manager);
630 LOCATIONS_NULL_ARG_CHECK(altitude);
631 LOCATIONS_NULL_ARG_CHECK(latitude);
632 LOCATIONS_NULL_ARG_CHECK(longitude);
633 LOCATIONS_NULL_ARG_CHECK(timestamp);
635 location_manager_s *handle = (location_manager_s *) manager;
638 LocationPosition *last_pos = NULL;
639 LocationAccuracy *last_acc = NULL;
640 ret = location_get_last_position(handle->object, &last_pos, &last_acc);
641 if (ret != LOCATION_ERROR_NONE) {
642 return __convert_error_code(ret);
645 if (last_pos->status == LOCATION_STATUS_NO_FIX) {
650 if (last_pos->status == LOCATION_STATUS_3D_FIX) {
651 *altitude = last_pos->altitude;
655 *latitude = last_pos->latitude;
656 *longitude = last_pos->longitude;
658 *timestamp = last_pos->timestamp;
659 location_position_free(last_pos);
660 location_accuracy_free(last_acc);
661 return LOCATIONS_ERROR_NONE;
664 int location_manager_get_last_velocity(location_manager_h manager, double *climb, double *direction, double *speed, time_t * timestamp)
666 LOCATIONS_NULL_ARG_CHECK(manager);
667 LOCATIONS_NULL_ARG_CHECK(climb);
668 LOCATIONS_NULL_ARG_CHECK(direction);
669 LOCATIONS_NULL_ARG_CHECK(speed);
670 LOCATIONS_NULL_ARG_CHECK(timestamp);
672 location_manager_s *handle = (location_manager_s *) manager;
675 LocationVelocity *last_vel = NULL;
676 LocationAccuracy *last_acc = NULL;
677 ret = location_get_last_velocity(handle->object, &last_vel, &last_acc);
678 if (ret != LOCATION_ERROR_NONE) {
679 return __convert_error_code(ret);
682 *climb = last_vel->climb;
683 *direction = last_vel->direction;
684 *speed = last_vel->speed;
685 *timestamp = last_vel->timestamp;
686 location_velocity_free(last_vel);
687 location_accuracy_free(last_acc);
688 return LOCATIONS_ERROR_NONE;
691 int location_manager_get_last_accuracy(location_manager_h manager, location_accuracy_level_e * level, double *horizontal,
694 LOCATIONS_NULL_ARG_CHECK(manager);
695 LOCATIONS_NULL_ARG_CHECK(level);
696 LOCATIONS_NULL_ARG_CHECK(horizontal);
697 LOCATIONS_NULL_ARG_CHECK(vertical);
698 location_manager_s *handle = (location_manager_s *) manager;
701 LocationPosition *last_pos = NULL;
702 LocationAccuracy *last_acc = NULL;
703 ret = location_get_last_position(handle->object, &last_pos, &last_acc);
704 if (ret != LOCATION_ERROR_NONE) {
705 return __convert_error_code(ret);
708 *level = last_acc->level;
709 *horizontal = last_acc->horizontal_accuracy;
710 *vertical = last_acc->vertical_accuracy;
711 location_position_free(last_pos);
712 location_accuracy_free(last_acc);
713 return LOCATIONS_ERROR_NONE;
716 int location_manager_get_accessibility_state(location_accessibility_state_e* state)
718 LOCATIONS_NULL_ARG_CHECK(state);
720 int ret = LOCATION_ERROR_NONE;
721 LocationAccessState auth = LOCATION_ACCESS_NONE;
722 ret = location_get_accessibility_state (&auth);
723 if (ret != LOCATION_ERROR_NONE) {
724 *state = LOCATIONS_ACCESS_STATE_NONE;
725 return __convert_error_code(ret);
729 case LOCATION_ACCESS_DENIED:
730 *state = LOCATIONS_ACCESS_STATE_DENIED;
732 case LOCATION_ACCESS_ALLOWED:
733 *state = LOCATIONS_ACCESS_STATE_ALLOWED;
735 case LOCATION_ACCESS_NONE:
737 *state = LOCATIONS_ACCESS_STATE_NONE;
741 return LOCATIONS_ERROR_NONE;
744 int location_manager_set_position_updated_cb(location_manager_h manager, location_position_updated_cb callback, int interval, void *user_data)
746 LOCATIONS_CHECK_CONDITION(interval >= 1
747 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
748 LOCATIONS_NULL_ARG_CHECK(manager);
749 location_manager_s *handle = (location_manager_s *) manager;
750 g_object_set(handle->object, "pos-interval", interval, NULL);
751 return __set_callback(_LOCATIONS_EVENT_TYPE_POSITION, manager, callback, user_data);
754 int location_manager_unset_position_updated_cb(location_manager_h manager)
756 return __unset_callback(_LOCATIONS_EVENT_TYPE_POSITION, manager);
759 int location_manager_set_velocity_updated_cb(location_manager_h manager, location_velocity_updated_cb callback, int interval, void *user_data)
761 LOCATIONS_CHECK_CONDITION(interval >= 1
762 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
763 LOCATIONS_NULL_ARG_CHECK(manager);
764 location_manager_s *handle = (location_manager_s *) manager;
765 g_object_set(handle->object, "vel-interval", interval, NULL);
766 return __set_callback(_LOCATIONS_EVENT_TYPE_VELOCITY, manager, callback, user_data);
769 int location_manager_unset_velocity_updated_cb(location_manager_h manager)
771 return __unset_callback(_LOCATIONS_EVENT_TYPE_VELOCITY, manager);
774 int location_manager_set_service_state_changed_cb(location_manager_h manager, location_service_state_changed_cb callback,
777 return __set_callback(_LOCATIONS_EVENT_TYPE_SERVICE_STATE, manager, callback, user_data);
780 int location_manager_unset_service_state_changed_cb(location_manager_h manager)
782 return __unset_callback(_LOCATIONS_EVENT_TYPE_SERVICE_STATE, manager);
785 int location_manager_set_zone_changed_cb(location_manager_h manager, location_zone_changed_cb callback, void *user_data)
787 return __set_callback(_LOCATIONS_EVENT_TYPE_BOUNDARY, manager, callback, user_data);
790 int location_manager_unset_zone_changed_cb(location_manager_h manager)
792 return __unset_callback(_LOCATIONS_EVENT_TYPE_BOUNDARY, manager);
795 int location_manager_get_distance(double start_latitude, double start_longitude, double end_latitude, double end_longitude, double *distance)
797 LOCATIONS_NULL_ARG_CHECK(distance);
798 LOCATIONS_CHECK_CONDITION(start_latitude>=-90 && start_latitude<=90,LOCATIONS_ERROR_INVALID_PARAMETER,"LOCATIONS_ERROR_INVALID_PARAMETER");
799 LOCATIONS_CHECK_CONDITION(start_longitude>=-180 && start_longitude<=180,LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
800 LOCATIONS_CHECK_CONDITION(end_latitude>=-90 && end_latitude<=90,LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
801 LOCATIONS_CHECK_CONDITION(end_longitude>=-180 && end_longitude<=180,LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
803 int ret = LOCATION_ERROR_NONE;
806 LocationPosition *start = location_position_new (0, start_latitude, start_longitude, 0, LOCATION_STATUS_2D_FIX);
807 LocationPosition *end = location_position_new (0, end_latitude, end_longitude, 0, LOCATION_STATUS_2D_FIX);
809 ret = location_get_distance (start, end, &u_distance);
810 if (ret != LOCATION_ERROR_NONE) {
811 return __convert_error_code(ret);
814 *distance = (double)u_distance;
816 return LOCATIONS_ERROR_NONE;
819 int location_manager_send_command(const char *cmd)
821 LOCATIONS_NULL_ARG_CHECK(cmd);
824 ret = location_send_command(cmd);
825 if (ret != LOCATION_ERROR_NONE) {
826 return __convert_error_code(ret);
829 return LOCATIONS_ERROR_NONE;
832 /////////////////////////////////////////
833 // GPS Status & Satellites
834 ////////////////////////////////////////
836 int gps_status_get_nmea(location_manager_h manager, char **nmea)
838 LOCATIONS_NULL_ARG_CHECK(manager);
839 LOCATIONS_NULL_ARG_CHECK(nmea);
840 location_manager_s *handle = (location_manager_s *) manager;
842 if (handle->method == LOCATIONS_METHOD_HYBRID) {
843 LocationMethod _method = LOCATION_METHOD_NONE;
844 g_object_get(handle->object, "method", &_method, NULL);
845 if (_method != LOCATION_METHOD_GPS) {
846 LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
847 LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
848 return LOCATIONS_ERROR_INCORRECT_METHOD;
850 } else if (handle->method != LOCATIONS_METHOD_GPS) {
851 LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
852 LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
853 return LOCATIONS_ERROR_INCORRECT_METHOD;
855 gchar *nmea_data = NULL;
856 g_object_get(handle->object, "nmea", &nmea_data, NULL);
857 if (nmea_data == NULL) {
858 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : nmea data is NULL ",
859 LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
860 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
863 *nmea = strdup(nmea_data);
865 LOCATIONS_LOGE("LOCATIONS_ERROR_OUT_OF_MEMORY(0x%08x) : fail to strdup ",
866 LOCATIONS_ERROR_OUT_OF_MEMORY);
867 return LOCATIONS_ERROR_OUT_OF_MEMORY;
870 return LOCATIONS_ERROR_NONE;
873 int gps_status_get_satellite(location_manager_h manager, int *num_of_active, int *num_of_inview, time_t *timestamp)
875 LOCATIONS_NULL_ARG_CHECK(manager);
876 LOCATIONS_NULL_ARG_CHECK(num_of_active);
877 LOCATIONS_NULL_ARG_CHECK(num_of_inview);
878 LOCATIONS_NULL_ARG_CHECK(timestamp);
879 location_manager_s *handle = (location_manager_s *) manager;
880 LocationSatellite *sat = NULL;
881 int ret = location_get_satellite (handle->object, &sat);
882 if (ret != LOCATION_ERROR_NONE || sat == NULL) {
883 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
884 LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
885 LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
886 return LOCATIONS_ERROR_INCORRECT_METHOD;
889 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ",
890 LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
891 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
894 *num_of_active = sat->num_of_sat_used;
895 *num_of_inview = sat->num_of_sat_inview;
896 *timestamp = sat->timestamp;
897 location_satellite_free(sat);
899 return LOCATIONS_ERROR_NONE;
902 int gps_status_set_satellite_updated_cb(location_manager_h manager, gps_status_satellite_updated_cb callback, int interval, void *user_data)
904 LOCATIONS_CHECK_CONDITION(interval >= 1
905 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
906 LOCATIONS_NULL_ARG_CHECK(manager);
907 location_manager_s *handle = (location_manager_s *) manager;
908 g_object_set(handle->object, "sat-interval", interval, NULL);
909 return __set_callback(_LOCATIONS_EVENT_TYPE_SATELLITE, manager, callback, user_data);
912 int gps_status_unset_satellite_updated_cb(location_manager_h manager)
914 return __unset_callback(_LOCATIONS_EVENT_TYPE_SATELLITE, manager);
918 int gps_status_foreach_satellites_in_view(location_manager_h manager, gps_status_get_satellites_cb callback, void *user_data)
920 LOCATIONS_NULL_ARG_CHECK(manager);
921 LOCATIONS_NULL_ARG_CHECK(callback);
922 location_manager_s *handle = (location_manager_s *) manager;
923 LocationSatellite *sat = NULL;
924 int ret = location_get_satellite (handle->object, &sat);
925 if (ret != LOCATION_ERROR_NONE || sat == NULL) {
926 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
927 LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
928 LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
929 return LOCATIONS_ERROR_INCORRECT_METHOD;
932 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ",
933 LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
934 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
938 for (i = 0; i < sat->num_of_sat_inview; i++) {
944 location_satellite_get_satellite_details(sat, i, &prn, &used, &elevation, &azimuth, &snr);
945 if (callback(azimuth, elevation, prn, snr, used, user_data) != TRUE)
948 location_satellite_free(sat);
950 return LOCATIONS_ERROR_NONE;
953 int gps_status_get_last_satellite(location_manager_h manager, int *num_of_active, int *num_of_inview, time_t *timestamp)
955 LOCATIONS_NULL_ARG_CHECK(manager);
956 LOCATIONS_NULL_ARG_CHECK(num_of_active);
957 LOCATIONS_NULL_ARG_CHECK(num_of_inview);
958 LOCATIONS_NULL_ARG_CHECK(timestamp);
959 location_manager_s *handle = (location_manager_s *) manager;
960 int ret = LOCATION_ERROR_NONE;
961 LocationSatellite *last_sat = NULL;
962 ret = location_get_last_satellite(handle->object, &last_sat);
963 if (ret != LOCATION_ERROR_NONE || last_sat == NULL) {
964 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
965 LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
966 LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
967 return LOCATIONS_ERROR_INCORRECT_METHOD;
970 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ",
971 LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
972 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
975 *num_of_active = last_sat->num_of_sat_used;
976 *num_of_inview = last_sat->num_of_sat_inview;
977 *timestamp = last_sat->timestamp;
978 location_satellite_free(last_sat);
979 return LOCATIONS_ERROR_NONE;
982 int gps_status_foreach_last_satellites_in_view(location_manager_h manager, gps_status_get_satellites_cb callback,
985 LOCATIONS_NULL_ARG_CHECK(manager);
986 LOCATIONS_NULL_ARG_CHECK(callback);
987 location_manager_s *handle = (location_manager_s *) manager;
989 LocationSatellite *last_sat = NULL;
990 ret = location_get_last_satellite(handle->object, &last_sat);
991 if (ret != LOCATION_ERROR_NONE || last_sat == NULL) {
992 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
993 LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
994 LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
995 return LOCATIONS_ERROR_INCORRECT_METHOD;
998 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ",
999 LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
1000 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
1004 for (i = 0; i < last_sat->num_of_sat_inview; i++) {
1010 location_satellite_get_satellite_details(last_sat, i, &prn, &used, &elevation, &azimuth, &snr);
1011 if (callback(azimuth, elevation, prn, snr, used, user_data) != TRUE) {
1015 location_satellite_free(last_sat);
1016 return LOCATIONS_ERROR_NONE;