2 * Copyright (c) 2011-2013 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;
406 handle->sig_id[_LOCATION_SIGNAL_SERVICE_ENABLED] = g_signal_connect(handle->object, "service-enabled", G_CALLBACK(__cb_service_enabled), handle);
407 handle->sig_id[_LOCATION_SIGNAL_SERVICE_DISABLED] = g_signal_connect(handle->object, "service-disabled", G_CALLBACK(__cb_service_disabled), handle);
409 *manager = (location_manager_h) handle;
410 return LOCATIONS_ERROR_NONE;
413 int location_manager_destroy(location_manager_h manager)
415 LOCATIONS_NULL_ARG_CHECK(manager);
416 location_manager_s *handle = (location_manager_s *) manager;
418 g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_SERVICE_ENABLED]);
419 g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_SERVICE_DISABLED]);
421 int ret = location_free(handle->object);
422 if (ret != LOCATIONS_ERROR_NONE) {
423 return __convert_error_code(ret);
426 return LOCATIONS_ERROR_NONE;
429 int location_manager_start(location_manager_h manager)
431 LOCATIONS_NULL_ARG_CHECK(manager);
432 location_manager_s *handle = (location_manager_s *) manager;
434 handle->sig_id[_LOCATION_SIGNAL_SERVICE_UPDATED] = g_signal_connect(handle->object, "service-updated", G_CALLBACK(__cb_service_updated), handle);
435 handle->sig_id[_LOCATION_SIGNAL_ZONE_IN] = g_signal_connect(handle->object, "zone-in", G_CALLBACK(__cb_zone_in), handle);
436 handle->sig_id[_LOCATION_SIGNAL_ZONE_OUT] = g_signal_connect(handle->object, "zone-out", G_CALLBACK(__cb_zone_out), handle);
438 int ret = location_start(handle->object);
439 if (ret != LOCATION_ERROR_NONE) {
440 return __convert_error_code(ret);
442 return LOCATIONS_ERROR_NONE;
445 int location_manager_stop(location_manager_h manager)
447 LOCATIONS_NULL_ARG_CHECK(manager);
448 location_manager_s *handle = (location_manager_s *) manager;
450 g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_SERVICE_UPDATED]);
451 g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_ZONE_IN]);
452 g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_ZONE_OUT]);
454 int ret = location_stop(handle->object);
455 if (ret != LOCATION_ERROR_NONE) {
456 return __convert_error_code(ret);
458 return LOCATIONS_ERROR_NONE;
461 int location_manager_add_boundary(location_manager_h manager, location_bounds_h bounds)
463 LOCATIONS_NULL_ARG_CHECK(manager);
464 LOCATIONS_NULL_ARG_CHECK(bounds);
466 location_manager_s *handle = (location_manager_s *) manager;
467 location_bounds_s *bound_handle = (location_bounds_s *) bounds;
468 int ret = location_boundary_add(handle->object, bound_handle->boundary);
469 if (ret != LOCATION_ERROR_NONE) {
470 return __convert_error_code(ret);
472 bound_handle->is_added = TRUE;
473 handle->bounds_list = g_list_append(handle->bounds_list, bound_handle);
474 return LOCATIONS_ERROR_NONE;
477 int location_manager_remove_boundary(location_manager_h manager, location_bounds_h bounds)
479 LOCATIONS_NULL_ARG_CHECK(manager);
480 LOCATIONS_NULL_ARG_CHECK(bounds);
482 location_manager_s *handle = (location_manager_s *) manager;
483 location_bounds_s *bound_handle = (location_bounds_s *) bounds;
484 int ret = location_boundary_remove(handle->object, bound_handle->boundary);
485 if (ret != LOCATION_ERROR_NONE) {
486 return __convert_error_code(ret);
488 handle->bounds_list = g_list_remove(handle->bounds_list, bound_handle);
489 bound_handle->is_added = FALSE;
490 return LOCATIONS_ERROR_NONE;
493 int location_manager_foreach_boundary(location_manager_h manager, location_bounds_cb callback, void *user_data)
495 LOCATIONS_NULL_ARG_CHECK(manager);
496 LOCATIONS_NULL_ARG_CHECK(callback);
498 location_manager_s *handle = (location_manager_s *) manager;
499 handle->user_cb[_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS] = callback;
500 handle->user_data[_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS] = user_data;
501 handle->is_continue_foreach_bounds = TRUE;
502 int ret = location_boundary_foreach(handle->object, __foreach_boundary, handle);
503 if (ret != LOCATION_ERROR_NONE) {
504 return __convert_error_code(ret);
506 return LOCATIONS_ERROR_NONE;
509 int location_manager_get_method(location_manager_h manager, location_method_e * method)
511 LOCATIONS_NULL_ARG_CHECK(manager);
512 LOCATIONS_NULL_ARG_CHECK(method);
513 location_manager_s *handle = (location_manager_s *) manager;
514 LocationMethod _method = LOCATION_METHOD_NONE;
515 g_object_get(handle->object, "method", &_method, NULL);
517 case LOCATION_METHOD_NONE:
518 *method = LOCATIONS_METHOD_NONE;
520 case LOCATION_METHOD_HYBRID:
521 *method = LOCATIONS_METHOD_HYBRID;
523 case LOCATION_METHOD_GPS:
524 *method = LOCATIONS_METHOD_GPS;
526 case LOCATION_METHOD_WPS:
527 *method = LOCATIONS_METHOD_WPS;
529 case LOCATION_METHOD_CPS:
530 *method = LOCATIONS_METHOD_CPS;
534 LOCATIONS_LOGE("LOCATIONS_ERROR_INVALID_PARAMETER(0x%08x) : Out of range (location_method_e) - method : %d ",
535 LOCATIONS_ERROR_INVALID_PARAMETER, method);
536 return LOCATIONS_ERROR_INVALID_PARAMETER;
539 //*method = handle->method;
540 return LOCATIONS_ERROR_NONE;
543 int location_manager_get_position(location_manager_h manager, double *altitude, double *latitude, double *longitude,
546 LOCATIONS_NULL_ARG_CHECK(manager);
547 LOCATIONS_NULL_ARG_CHECK(altitude);
548 LOCATIONS_NULL_ARG_CHECK(latitude);
549 LOCATIONS_NULL_ARG_CHECK(longitude);
550 LOCATIONS_NULL_ARG_CHECK(timestamp);
552 location_manager_s *handle = (location_manager_s *) manager;
554 LocationPosition *pos = NULL;
555 LocationAccuracy *acc = NULL;
556 ret = location_get_position(handle->object, &pos, &acc);
557 if (ret != LOCATION_ERROR_NONE) {
558 return __convert_error_code(ret);
561 if (pos->status == LOCATION_STATUS_NO_FIX) {
566 if (pos->status == LOCATION_STATUS_3D_FIX) {
567 *altitude = pos->altitude;
571 *latitude = pos->latitude;
572 *longitude = pos->longitude;
574 *timestamp = pos->timestamp;
575 location_position_free(pos);
576 location_accuracy_free(acc);
577 return LOCATIONS_ERROR_NONE;
580 int location_manager_get_velocity(location_manager_h manager, double *climb, double *direction, double *speed, time_t * timestamp)
582 LOCATIONS_NULL_ARG_CHECK(manager);
583 LOCATIONS_NULL_ARG_CHECK(climb);
584 LOCATIONS_NULL_ARG_CHECK(direction);
585 LOCATIONS_NULL_ARG_CHECK(speed);
586 LOCATIONS_NULL_ARG_CHECK(timestamp);
588 location_manager_s *handle = (location_manager_s *) manager;
590 LocationVelocity *vel = NULL;
591 LocationAccuracy *acc = NULL;
592 ret = location_get_velocity(handle->object, &vel, &acc);
593 if (ret != LOCATION_ERROR_NONE) {
594 return __convert_error_code(ret);
598 *direction = vel->direction;
600 *timestamp = vel->timestamp;
601 location_velocity_free(vel);
602 location_accuracy_free(acc);
603 return LOCATIONS_ERROR_NONE;
606 int location_manager_get_accuracy(location_manager_h manager, location_accuracy_level_e * level, double *horizontal,
609 LOCATIONS_NULL_ARG_CHECK(manager);
610 LOCATIONS_NULL_ARG_CHECK(level);
611 LOCATIONS_NULL_ARG_CHECK(horizontal);
612 LOCATIONS_NULL_ARG_CHECK(vertical);
613 location_manager_s *handle = (location_manager_s *) manager;
616 LocationPosition *pos = NULL;
617 LocationAccuracy *acc = NULL;
618 ret = location_get_position(handle->object, &pos, &acc);
619 if (ret != LOCATION_ERROR_NONE) {
620 return __convert_error_code(ret);
624 return __convert_error_code(LOCATION_ERROR_NOT_AVAILABLE);
628 *horizontal = acc->horizontal_accuracy;
629 *vertical = acc->vertical_accuracy;
630 location_position_free(pos);
631 location_accuracy_free(acc);
632 return LOCATIONS_ERROR_NONE;
635 int location_manager_get_last_position(location_manager_h manager, double *altitude, double *latitude, double *longitude,
638 LOCATIONS_NULL_ARG_CHECK(manager);
639 LOCATIONS_NULL_ARG_CHECK(altitude);
640 LOCATIONS_NULL_ARG_CHECK(latitude);
641 LOCATIONS_NULL_ARG_CHECK(longitude);
642 LOCATIONS_NULL_ARG_CHECK(timestamp);
644 location_manager_s *handle = (location_manager_s *) manager;
647 LocationPosition *last_pos = NULL;
648 LocationAccuracy *last_acc = NULL;
649 ret = location_get_last_position(handle->object, &last_pos, &last_acc);
650 if (ret != LOCATION_ERROR_NONE) {
651 return __convert_error_code(ret);
654 if (last_pos->status == LOCATION_STATUS_NO_FIX) {
659 if (last_pos->status == LOCATION_STATUS_3D_FIX) {
660 *altitude = last_pos->altitude;
664 *latitude = last_pos->latitude;
665 *longitude = last_pos->longitude;
667 *timestamp = last_pos->timestamp;
668 location_position_free(last_pos);
669 location_accuracy_free(last_acc);
670 return LOCATIONS_ERROR_NONE;
673 int location_manager_get_last_velocity(location_manager_h manager, double *climb, double *direction, double *speed, time_t * timestamp)
675 LOCATIONS_NULL_ARG_CHECK(manager);
676 LOCATIONS_NULL_ARG_CHECK(climb);
677 LOCATIONS_NULL_ARG_CHECK(direction);
678 LOCATIONS_NULL_ARG_CHECK(speed);
679 LOCATIONS_NULL_ARG_CHECK(timestamp);
681 location_manager_s *handle = (location_manager_s *) manager;
684 LocationVelocity *last_vel = NULL;
685 LocationAccuracy *last_acc = NULL;
686 ret = location_get_last_velocity(handle->object, &last_vel, &last_acc);
687 if (ret != LOCATION_ERROR_NONE) {
688 return __convert_error_code(ret);
691 *climb = last_vel->climb;
692 *direction = last_vel->direction;
693 *speed = last_vel->speed;
694 *timestamp = last_vel->timestamp;
695 location_velocity_free(last_vel);
696 location_accuracy_free(last_acc);
697 return LOCATIONS_ERROR_NONE;
700 int location_manager_get_last_accuracy(location_manager_h manager, location_accuracy_level_e * level, double *horizontal,
703 LOCATIONS_NULL_ARG_CHECK(manager);
704 LOCATIONS_NULL_ARG_CHECK(level);
705 LOCATIONS_NULL_ARG_CHECK(horizontal);
706 LOCATIONS_NULL_ARG_CHECK(vertical);
707 location_manager_s *handle = (location_manager_s *) manager;
710 LocationPosition *last_pos = NULL;
711 LocationAccuracy *last_acc = NULL;
712 ret = location_get_last_position(handle->object, &last_pos, &last_acc);
713 if (ret != LOCATION_ERROR_NONE) {
714 return __convert_error_code(ret);
717 *level = last_acc->level;
718 *horizontal = last_acc->horizontal_accuracy;
719 *vertical = last_acc->vertical_accuracy;
720 location_position_free(last_pos);
721 location_accuracy_free(last_acc);
722 return LOCATIONS_ERROR_NONE;
725 int location_manager_get_accessibility_state(location_accessibility_state_e* state)
727 LOCATIONS_NULL_ARG_CHECK(state);
729 int ret = LOCATION_ERROR_NONE;
730 LocationAccessState auth = LOCATION_ACCESS_NONE;
731 ret = location_get_accessibility_state (&auth);
732 if (ret != LOCATION_ERROR_NONE) {
733 *state = LOCATIONS_ACCESS_STATE_NONE;
734 return __convert_error_code(ret);
738 case LOCATION_ACCESS_DENIED:
739 *state = LOCATIONS_ACCESS_STATE_DENIED;
741 case LOCATION_ACCESS_ALLOWED:
742 *state = LOCATIONS_ACCESS_STATE_ALLOWED;
744 case LOCATION_ACCESS_NONE:
746 *state = LOCATIONS_ACCESS_STATE_NONE;
750 return LOCATIONS_ERROR_NONE;
753 int location_manager_set_position_updated_cb(location_manager_h manager, location_position_updated_cb callback, int interval, void *user_data)
755 LOCATIONS_CHECK_CONDITION(interval >= 1
756 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
757 LOCATIONS_NULL_ARG_CHECK(manager);
758 location_manager_s *handle = (location_manager_s *) manager;
759 g_object_set(handle->object, "pos-interval", interval, NULL);
760 return __set_callback(_LOCATIONS_EVENT_TYPE_POSITION, manager, callback, user_data);
763 int location_manager_unset_position_updated_cb(location_manager_h manager)
765 return __unset_callback(_LOCATIONS_EVENT_TYPE_POSITION, manager);
768 int location_manager_set_velocity_updated_cb(location_manager_h manager, location_velocity_updated_cb callback, int interval, void *user_data)
770 LOCATIONS_CHECK_CONDITION(interval >= 1
771 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
772 LOCATIONS_NULL_ARG_CHECK(manager);
773 location_manager_s *handle = (location_manager_s *) manager;
774 g_object_set(handle->object, "vel-interval", interval, NULL);
775 return __set_callback(_LOCATIONS_EVENT_TYPE_VELOCITY, manager, callback, user_data);
778 int location_manager_unset_velocity_updated_cb(location_manager_h manager)
780 return __unset_callback(_LOCATIONS_EVENT_TYPE_VELOCITY, manager);
783 int location_manager_set_service_state_changed_cb(location_manager_h manager, location_service_state_changed_cb callback,
786 return __set_callback(_LOCATIONS_EVENT_TYPE_SERVICE_STATE, manager, callback, user_data);
789 int location_manager_unset_service_state_changed_cb(location_manager_h manager)
791 return __unset_callback(_LOCATIONS_EVENT_TYPE_SERVICE_STATE, manager);
794 int location_manager_set_zone_changed_cb(location_manager_h manager, location_zone_changed_cb callback, void *user_data)
796 return __set_callback(_LOCATIONS_EVENT_TYPE_BOUNDARY, manager, callback, user_data);
799 int location_manager_unset_zone_changed_cb(location_manager_h manager)
801 return __unset_callback(_LOCATIONS_EVENT_TYPE_BOUNDARY, manager);
804 int location_manager_get_distance(double start_latitude, double start_longitude, double end_latitude, double end_longitude, double *distance)
806 LOCATIONS_NULL_ARG_CHECK(distance);
807 LOCATIONS_CHECK_CONDITION(start_latitude>=-90 && start_latitude<=90,LOCATIONS_ERROR_INVALID_PARAMETER,"LOCATIONS_ERROR_INVALID_PARAMETER");
808 LOCATIONS_CHECK_CONDITION(start_longitude>=-180 && start_longitude<=180,LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
809 LOCATIONS_CHECK_CONDITION(end_latitude>=-90 && end_latitude<=90,LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
810 LOCATIONS_CHECK_CONDITION(end_longitude>=-180 && end_longitude<=180,LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
812 int ret = LOCATION_ERROR_NONE;
815 LocationPosition *start = location_position_new (0, start_latitude, start_longitude, 0, LOCATION_STATUS_2D_FIX);
816 LocationPosition *end = location_position_new (0, end_latitude, end_longitude, 0, LOCATION_STATUS_2D_FIX);
818 ret = location_get_distance (start, end, &u_distance);
819 if (ret != LOCATION_ERROR_NONE) {
820 return __convert_error_code(ret);
823 *distance = (double)u_distance;
825 return LOCATIONS_ERROR_NONE;
828 int location_manager_send_command(const char *cmd)
830 LOCATIONS_NULL_ARG_CHECK(cmd);
833 ret = location_send_command(cmd);
834 if (ret != LOCATION_ERROR_NONE) {
835 return __convert_error_code(ret);
838 return LOCATIONS_ERROR_NONE;
841 /////////////////////////////////////////
842 // GPS Status & Satellites
843 ////////////////////////////////////////
845 int gps_status_get_nmea(location_manager_h manager, char **nmea)
847 LOCATIONS_NULL_ARG_CHECK(manager);
848 LOCATIONS_NULL_ARG_CHECK(nmea);
849 location_manager_s *handle = (location_manager_s *) manager;
851 if (handle->method == LOCATIONS_METHOD_HYBRID) {
852 LocationMethod _method = LOCATION_METHOD_NONE;
853 g_object_get(handle->object, "method", &_method, NULL);
854 if (_method != LOCATION_METHOD_GPS) {
855 LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
856 LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
857 return LOCATIONS_ERROR_INCORRECT_METHOD;
859 } else if (handle->method != LOCATIONS_METHOD_GPS) {
860 LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
861 LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
862 return LOCATIONS_ERROR_INCORRECT_METHOD;
864 gchar *nmea_data = NULL;
865 g_object_get(handle->object, "nmea", &nmea_data, NULL);
866 if (nmea_data == NULL) {
867 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : nmea data is NULL ",
868 LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
869 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
872 *nmea = strdup(nmea_data);
874 LOCATIONS_LOGE("LOCATIONS_ERROR_OUT_OF_MEMORY(0x%08x) : fail to strdup ",
875 LOCATIONS_ERROR_OUT_OF_MEMORY);
876 return LOCATIONS_ERROR_OUT_OF_MEMORY;
879 return LOCATIONS_ERROR_NONE;
882 int gps_status_get_satellite(location_manager_h manager, int *num_of_active, int *num_of_inview, time_t *timestamp)
884 LOCATIONS_NULL_ARG_CHECK(manager);
885 LOCATIONS_NULL_ARG_CHECK(num_of_active);
886 LOCATIONS_NULL_ARG_CHECK(num_of_inview);
887 LOCATIONS_NULL_ARG_CHECK(timestamp);
888 location_manager_s *handle = (location_manager_s *) manager;
889 LocationSatellite *sat = NULL;
890 int ret = location_get_satellite (handle->object, &sat);
891 if (ret != LOCATION_ERROR_NONE || sat == NULL) {
892 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
893 LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
894 LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
895 return LOCATIONS_ERROR_INCORRECT_METHOD;
898 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ",
899 LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
900 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
903 *num_of_active = sat->num_of_sat_used;
904 *num_of_inview = sat->num_of_sat_inview;
905 *timestamp = sat->timestamp;
906 location_satellite_free(sat);
908 return LOCATIONS_ERROR_NONE;
911 int gps_status_set_satellite_updated_cb(location_manager_h manager, gps_status_satellite_updated_cb callback, int interval, void *user_data)
913 LOCATIONS_CHECK_CONDITION(interval >= 1
914 && interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
915 LOCATIONS_NULL_ARG_CHECK(manager);
916 location_manager_s *handle = (location_manager_s *) manager;
917 g_object_set(handle->object, "sat-interval", interval, NULL);
918 return __set_callback(_LOCATIONS_EVENT_TYPE_SATELLITE, manager, callback, user_data);
921 int gps_status_unset_satellite_updated_cb(location_manager_h manager)
923 return __unset_callback(_LOCATIONS_EVENT_TYPE_SATELLITE, manager);
927 int gps_status_foreach_satellites_in_view(location_manager_h manager, gps_status_get_satellites_cb callback, void *user_data)
929 LOCATIONS_NULL_ARG_CHECK(manager);
930 LOCATIONS_NULL_ARG_CHECK(callback);
931 location_manager_s *handle = (location_manager_s *) manager;
932 LocationSatellite *sat = NULL;
933 int ret = location_get_satellite (handle->object, &sat);
934 if (ret != LOCATION_ERROR_NONE || sat == NULL) {
935 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
936 LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
937 LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
938 return LOCATIONS_ERROR_INCORRECT_METHOD;
941 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ",
942 LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
943 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
947 for (i = 0; i < sat->num_of_sat_inview; i++) {
953 location_satellite_get_satellite_details(sat, i, &prn, &used, &elevation, &azimuth, &snr);
954 if (callback(azimuth, elevation, prn, snr, used, user_data) != TRUE)
957 location_satellite_free(sat);
959 return LOCATIONS_ERROR_NONE;
962 int gps_status_get_last_satellite(location_manager_h manager, int *num_of_active, int *num_of_inview, time_t *timestamp)
964 LOCATIONS_NULL_ARG_CHECK(manager);
965 LOCATIONS_NULL_ARG_CHECK(num_of_active);
966 LOCATIONS_NULL_ARG_CHECK(num_of_inview);
967 LOCATIONS_NULL_ARG_CHECK(timestamp);
968 location_manager_s *handle = (location_manager_s *) manager;
969 int ret = LOCATION_ERROR_NONE;
970 LocationSatellite *last_sat = NULL;
971 ret = location_get_last_satellite(handle->object, &last_sat);
972 if (ret != LOCATION_ERROR_NONE || last_sat == NULL) {
973 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
974 LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
975 LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
976 return LOCATIONS_ERROR_INCORRECT_METHOD;
979 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ",
980 LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
981 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
984 *num_of_active = last_sat->num_of_sat_used;
985 *num_of_inview = last_sat->num_of_sat_inview;
986 *timestamp = last_sat->timestamp;
987 location_satellite_free(last_sat);
988 return LOCATIONS_ERROR_NONE;
991 int gps_status_foreach_last_satellites_in_view(location_manager_h manager, gps_status_get_satellites_cb callback,
994 LOCATIONS_NULL_ARG_CHECK(manager);
995 LOCATIONS_NULL_ARG_CHECK(callback);
996 location_manager_s *handle = (location_manager_s *) manager;
998 LocationSatellite *last_sat = NULL;
999 ret = location_get_last_satellite(handle->object, &last_sat);
1000 if (ret != LOCATION_ERROR_NONE || last_sat == NULL) {
1001 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
1002 LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d",
1003 LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
1004 return LOCATIONS_ERROR_INCORRECT_METHOD;
1007 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : satellite is NULL ",
1008 LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
1009 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
1013 for (i = 0; i < last_sat->num_of_sat_inview; i++) {
1019 location_satellite_get_satellite_details(last_sat, i, &prn, &used, &elevation, &azimuth, &snr);
1020 if (callback(azimuth, elevation, prn, snr, used, user_data) != TRUE) {
1024 location_satellite_free(last_sat);
1025 return LOCATIONS_ERROR_NONE;