Tizen 2.1 base
[platform/core/api/location-manager.git] / src / locations.c
1 /*
2 * Copyright (c) 2011 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 <locations_private.h>
21 #include <location_bounds.h>
22
23
24 #define LOCATIONS_NULL_ARG_CHECK(arg)   \
25         LOCATIONS_CHECK_CONDITION((arg != NULL),LOCATIONS_ERROR_INVALID_PARAMETER,"LOCATIONS_ERROR_INVALID_PARAMETER") \
26
27 /*
28 * Internal Implementation
29 */
30
31 static int __convert_error_code(int code)
32 {
33         int ret;
34         char *msg = "LOCATIONS_ERROR_NONE";
35         switch (code) {
36         case LOCATION_ERROR_NONE:
37                 ret = LOCATIONS_ERROR_NONE;
38                 msg = "LOCATIONS_ERROR_NONE";
39                 break;
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";
44                 break;
45         case LOCATION_ERROR_NOT_ALLOWED:
46                 ret = LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED;
47                 msg = "LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED";
48                 break;
49         case LOCATION_ERROR_SETTING_OFF:
50                 ret = LOCATIONS_ERROR_GPS_SETTING_OFF;
51                 msg = "LOCATIONS_ERROR_GPS_SETTING_OFF";
52                 break;
53         case LOCATION_ERROR_SECURITY_DENIED:
54                 ret = LOCATIONS_ERROR_SECURITY_RESTRICTED;
55                 msg = "LOCATIONS_ERROR_SECURITY_RESTRICTED";
56                 break;
57         case LOCATION_ERROR_NOT_AVAILABLE:
58         case LOCATION_ERROR_CONFIGURATION:
59         case LOCATION_ERROR_PARAMETER:
60         case LOCATION_ERROR_UNKNOWN:
61         default:
62                 msg = "LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE";
63                 ret = LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
64         }
65         LOCATIONS_LOGE("%s(0x%08x) : core fw error(0x%x)", msg, ret, code);
66         return ret;
67 }
68
69 static void __cb_service_updated(GObject * self, guint type, gpointer data, gpointer accuracy, gpointer userdata)
70 {
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,
79                                                                                                   handle->user_data
80                                                                                                   [_LOCATIONS_EVENT_TYPE_VELOCITY]);
81         }
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,
88                                                                                                   handle->user_data
89                                                                                                   [_LOCATIONS_EVENT_TYPE_POSITION]);
90         }
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]);
97         }
98 }
99
100 static void __cb_service_enabled(GObject * self, guint status, gpointer userdata)
101 {
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]);
108         }
109 }
110
111 static void __cb_service_disabled(GObject * self, guint status, gpointer userdata)
112 {
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]);
119 }
120
121 static int __compare_position (gconstpointer a, gconstpointer b)
122 {
123         if(location_position_equal((LocationPosition*) a, (LocationPosition *)b) == TRUE) {
124                 return 0;
125         }
126
127         return -1;
128 }
129
130 static int __boundary_compare(LocationBoundary * bound1, LocationBoundary * bound2)
131 {
132         int ret = -1;
133
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) {
138                                 ret = 0;
139                         }
140                         break;
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)) {
143                                 ret = 0;
144                         }
145                         break;
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)) {
150                                 return -1;
151                         }
152
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;
155
156                         boundary2_prev = g_list_previous(boundary2_start);
157                         boundary2_next = g_list_next(boundary2_start);
158
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);
161
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) {
169                                                 return -1;
170                                         }
171                                         boundary1_next = g_list_next(boundary1_next);
172                                 }
173                                 ret = 0;
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) {
180                                                 return -1;
181                                         }
182                                         boundary1_next = g_list_next(boundary1_next);
183                                 }
184                                 ret = 0;
185                         } else {
186                                 return -1;
187                         }
188                         break;
189                 }
190                 default:
191                         break;
192                 }
193         }
194         return ret;
195 }
196
197 static void __cb_zone_in(GObject * self, gpointer boundary, gpointer position, gpointer accuracy, gpointer userdata)
198 {
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,
206                                                                                               handle->user_data
207                                                                                               [_LOCATIONS_EVENT_TYPE_BOUNDARY]);
208         }
209
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);
217                         break;
218                 }
219                 bounds_list = g_list_next(bounds_list);
220         }
221 }
222
223 static void __cb_zone_out(GObject * self, gpointer boundary, gpointer position, gpointer accuracy, gpointer userdata)
224 {
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,
232                                                                                               handle->user_data
233                                                                                               [_LOCATIONS_EVENT_TYPE_BOUNDARY]);
234         }
235
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);
243                         break;
244                 }
245                 bounds_list = g_list_next(bounds_list);
246         }
247 }
248
249 static int __set_callback(_location_event_e type, location_manager_h manager, void *callback, void *user_data)
250 {
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;
258 }
259
260 static int __unset_callback(_location_event_e type, location_manager_h manager)
261 {
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;
268 }
269
270 static void __foreach_boundary(LocationBoundary * boundary, void *user_data)
271 {
272         location_manager_s *handle = (location_manager_s *) user_data;
273
274         if (handle != NULL && boundary != NULL) {
275                 int ret = -1;
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);
293                         if (size > 0) {
294                                 location_coords_s coords[size];
295                                 int cnt = 0;
296                                 while (list) {
297                                         LocationPosition *pos = list->data;
298                                         coords[cnt].latitude = pos->latitude;
299                                         coords[cnt].longitude = pos->longitude;
300                                         list = g_list_next(list);
301                                         cnt++;
302                                 }
303                                 ret = location_bounds_create_polygon(coords, size, &bounds);
304                         }
305                 } else {
306                         LOCATIONS_LOGI("Invalid boundary type : %d", boundary->type);
307                 }
308
309                 if (ret != LOCATIONS_ERROR_NONE) {
310                         LOCATIONS_LOGI("Failed to create location_bounds : (0x%08x) ", ret);
311                 } else {
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,
315                                                                                                                   handle->
316                                                                                                                   user_data
317                                                                                                                   [_LOCATIONS_EVENT_TYPE_FOREACH_BOUNDS]);
318                         }
319                         location_bounds_destroy(bounds);
320                 }
321         } else {
322                 LOCATIONS_LOGI("__foreach_boundary() has been failed");
323         }
324 }
325
326 /////////////////////////////////////////
327 // Location Manager
328 ////////////////////////////////////////
329
330 /*
331 * Public Implementation
332 */
333
334 bool location_manager_is_supported_method(location_method_e method)
335 {
336         LocationMethod _method = LOCATION_METHOD_NONE;
337         switch (method) {
338         case LOCATIONS_METHOD_HYBRID:
339                 _method = LOCATION_METHOD_HYBRID;
340                 break;
341         case LOCATIONS_METHOD_GPS:
342                 _method = LOCATION_METHOD_GPS;
343                 break;
344         case LOCATIONS_METHOD_WPS:
345                 _method = LOCATION_METHOD_WPS;
346                 break;
347         case LOCATIONS_METHOD_CPS:
348                 _method = LOCATION_METHOD_CPS;
349                 break;
350         default:
351                 _method = LOCATION_METHOD_NONE;
352                 break;
353         }
354         return location_is_supported_method(_method);
355 }
356
357 int location_manager_create(location_method_e method, location_manager_h * manager)
358 {
359         LOCATIONS_NULL_ARG_CHECK(manager);
360         if (location_init() != LOCATION_ERROR_NONE)
361                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
362
363         LocationMethod _method = LOCATION_METHOD_NONE;
364         switch (method) {
365         case LOCATIONS_METHOD_HYBRID:
366                 _method = LOCATION_METHOD_HYBRID;
367                 break;
368         case LOCATIONS_METHOD_GPS:
369                 _method = LOCATION_METHOD_GPS;
370                 break;
371         case LOCATIONS_METHOD_WPS:
372                 _method = LOCATION_METHOD_WPS;
373                 break;
374         case LOCATIONS_METHOD_CPS:
375                 _method = LOCATION_METHOD_CPS;
376                 break;
377         case LOCATIONS_METHOD_NONE:
378                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
379         default:
380                 {
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;
384                 }
385         }
386
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;
391         }
392
393         memset(handle, 0, sizeof(location_manager_s));
394
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);
399                 free(handle);
400                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
401         }
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;
407 }
408
409 int location_manager_destroy(location_manager_h manager)
410 {
411         LOCATIONS_NULL_ARG_CHECK(manager);
412         location_manager_s *handle = (location_manager_s *) manager;
413
414         int ret = location_free(handle->object);
415         if (ret != LOCATIONS_ERROR_NONE) {
416                 return __convert_error_code(ret);
417         }
418         free(handle);
419         return LOCATIONS_ERROR_NONE;
420 }
421
422 int location_manager_start(location_manager_h manager)
423 {
424         LOCATIONS_NULL_ARG_CHECK(manager);
425         location_manager_s *handle = (location_manager_s *) manager;
426
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);
432
433         int ret = location_start(handle->object);
434         if (ret != LOCATION_ERROR_NONE) {
435                 return __convert_error_code(ret);
436         }
437         return LOCATIONS_ERROR_NONE;
438 }
439
440 int location_manager_stop(location_manager_h manager)
441 {
442         LOCATIONS_NULL_ARG_CHECK(manager);
443         location_manager_s *handle = (location_manager_s *) manager;
444
445         int ret = location_stop(handle->object);
446         if (ret != LOCATION_ERROR_NONE) {
447                 return __convert_error_code(ret);
448         }
449         return LOCATIONS_ERROR_NONE;
450 }
451
452 int location_manager_add_boundary(location_manager_h manager, location_bounds_h bounds)
453 {
454         LOCATIONS_NULL_ARG_CHECK(manager);
455         LOCATIONS_NULL_ARG_CHECK(bounds);
456
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);
462         }
463         bound_handle->is_added = TRUE;
464         handle->bounds_list = g_list_append(handle->bounds_list, bound_handle);
465         return LOCATIONS_ERROR_NONE;
466 }
467
468 int location_manager_remove_boundary(location_manager_h manager, location_bounds_h bounds)
469 {
470         LOCATIONS_NULL_ARG_CHECK(manager);
471         LOCATIONS_NULL_ARG_CHECK(bounds);
472
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);
478         }
479         handle->bounds_list = g_list_remove(handle->bounds_list, bound_handle);
480         bound_handle->is_added = FALSE;
481         return LOCATIONS_ERROR_NONE;
482 }
483
484 int location_manager_foreach_boundary(location_manager_h manager, location_bounds_cb callback, void *user_data)
485 {
486         LOCATIONS_NULL_ARG_CHECK(manager);
487         LOCATIONS_NULL_ARG_CHECK(callback);
488
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);
496         }
497         return LOCATIONS_ERROR_NONE;
498 }
499
500 int location_manager_get_method(location_manager_h manager, location_method_e * method)
501 {
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);
507         switch (_method) {
508         case LOCATION_METHOD_NONE:
509                 *method = LOCATIONS_METHOD_NONE;
510                 break;
511         case LOCATION_METHOD_HYBRID:
512                 *method = LOCATIONS_METHOD_HYBRID;
513                 break;
514         case LOCATION_METHOD_GPS:
515                 *method = LOCATIONS_METHOD_GPS;
516                 break;
517         case LOCATION_METHOD_WPS:
518                 *method = LOCATIONS_METHOD_WPS;
519                 break;
520         case LOCATION_METHOD_CPS:
521                 *method = LOCATIONS_METHOD_CPS;
522                 break;
523         default:
524                 {
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;
528                 }
529         }
530         //*method = handle->method;
531         return LOCATIONS_ERROR_NONE;
532 }
533
534 int location_manager_get_position(location_manager_h manager, double *altitude, double *latitude, double *longitude,
535                                   time_t * timestamp)
536 {
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);
542
543         location_manager_s *handle = (location_manager_s *) manager;
544         int ret;
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);
550         }
551
552         if (pos->status == LOCATION_STATUS_NO_FIX) {
553                 *altitude = -1;
554                 *latitude = -1;
555                 *longitude = -1;
556         } else {
557                 if (pos->status == LOCATION_STATUS_3D_FIX) {
558                         *altitude = pos->altitude;
559                 } else {
560                         *altitude = -1;
561                 }
562                 *latitude = pos->latitude;
563                 *longitude = pos->longitude;
564         }
565         *timestamp = pos->timestamp;
566         location_position_free(pos);
567         location_accuracy_free(acc);
568         return LOCATIONS_ERROR_NONE;
569 }
570
571 int location_manager_get_velocity(location_manager_h manager, double *climb, double *direction, double *speed, time_t * timestamp)
572 {
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);
578
579         location_manager_s *handle = (location_manager_s *) manager;
580         int ret;
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);
586         }
587
588         *climb = vel->climb;
589         *direction = vel->direction;
590         *speed = vel->speed;
591         *timestamp = vel->timestamp;
592         location_velocity_free(vel);
593         location_accuracy_free(acc);
594         return LOCATIONS_ERROR_NONE;
595 }
596
597 int location_manager_get_accuracy(location_manager_h manager, location_accuracy_level_e * level, double *horizontal,
598                                   double *vertical)
599 {
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;
605
606         int ret;
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);
612         }
613
614         if (acc == NULL) {
615                 return __convert_error_code(LOCATION_ERROR_NOT_AVAILABLE);
616         }
617
618         *level = acc->level;
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;
624 }
625
626 int location_manager_get_last_position(location_manager_h manager, double *altitude, double *latitude, double *longitude,
627                                        time_t * timestamp)
628 {
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);
634
635         location_manager_s *handle = (location_manager_s *) manager;
636
637         int ret;
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);
643         }
644
645         if (last_pos->status == LOCATION_STATUS_NO_FIX) {
646                 *altitude = -1;
647                 *latitude = -1;
648                 *longitude = -1;
649         } else {
650                 if (last_pos->status == LOCATION_STATUS_3D_FIX) {
651                         *altitude = last_pos->altitude;
652                 } else {
653                         *altitude = -1;
654                 }
655                 *latitude = last_pos->latitude;
656                 *longitude = last_pos->longitude;
657         }
658         *timestamp = last_pos->timestamp;
659         location_position_free(last_pos);
660         location_accuracy_free(last_acc);
661         return LOCATIONS_ERROR_NONE;
662 }
663
664 int location_manager_get_last_velocity(location_manager_h manager, double *climb, double *direction, double *speed, time_t * timestamp)
665 {
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);
671
672         location_manager_s *handle = (location_manager_s *) manager;
673
674         int ret;
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);
680         }
681
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;
689 }
690
691 int location_manager_get_last_accuracy(location_manager_h manager, location_accuracy_level_e * level, double *horizontal,
692                                        double *vertical)
693 {
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;
699
700         int ret;
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);
706         }
707
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;
714 }
715
716 int location_manager_get_accessibility_state(location_accessibility_state_e* state)
717 {
718         LOCATIONS_NULL_ARG_CHECK(state);
719
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);
726         }
727
728         switch (auth) {
729                 case LOCATION_ACCESS_DENIED:
730                         *state = LOCATIONS_ACCESS_STATE_DENIED;
731                         break;
732                 case LOCATION_ACCESS_ALLOWED:
733                         *state = LOCATIONS_ACCESS_STATE_ALLOWED;
734                         break;
735                 case LOCATION_ACCESS_NONE:
736                 default:
737                         *state = LOCATIONS_ACCESS_STATE_NONE;
738                         break;
739         }
740
741         return LOCATIONS_ERROR_NONE;
742 }
743
744 int location_manager_set_position_updated_cb(location_manager_h manager, location_position_updated_cb callback, int interval, void *user_data)
745 {
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);
752 }
753
754 int location_manager_unset_position_updated_cb(location_manager_h manager)
755 {
756         return __unset_callback(_LOCATIONS_EVENT_TYPE_POSITION, manager);
757 }
758
759 int location_manager_set_velocity_updated_cb(location_manager_h manager, location_velocity_updated_cb callback, int interval, void *user_data)
760 {
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);
767 }
768
769 int location_manager_unset_velocity_updated_cb(location_manager_h manager)
770 {
771         return __unset_callback(_LOCATIONS_EVENT_TYPE_VELOCITY, manager);
772 }
773
774 int location_manager_set_service_state_changed_cb(location_manager_h manager, location_service_state_changed_cb callback,
775                                                   void *user_data)
776 {
777         return __set_callback(_LOCATIONS_EVENT_TYPE_SERVICE_STATE, manager, callback, user_data);
778 }
779
780 int location_manager_unset_service_state_changed_cb(location_manager_h manager)
781 {
782         return __unset_callback(_LOCATIONS_EVENT_TYPE_SERVICE_STATE, manager);
783 }
784
785 int location_manager_set_zone_changed_cb(location_manager_h manager, location_zone_changed_cb callback, void *user_data)
786 {
787         return __set_callback(_LOCATIONS_EVENT_TYPE_BOUNDARY, manager, callback, user_data);
788 }
789
790 int location_manager_unset_zone_changed_cb(location_manager_h manager)
791 {
792         return __unset_callback(_LOCATIONS_EVENT_TYPE_BOUNDARY, manager);
793 }
794
795 int location_manager_get_distance(double start_latitude, double start_longitude, double end_latitude, double end_longitude, double *distance)
796 {
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");
802
803         int ret = LOCATION_ERROR_NONE;
804         ulong u_distance;
805
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);
808
809         ret = location_get_distance (start, end, &u_distance);
810         if (ret != LOCATION_ERROR_NONE) {
811                 return __convert_error_code(ret);
812         }
813
814         *distance = (double)u_distance;
815
816         return LOCATIONS_ERROR_NONE;
817 }
818
819 int location_manager_send_command(const char *cmd)
820 {
821         LOCATIONS_NULL_ARG_CHECK(cmd);
822
823         int ret;
824         ret = location_send_command(cmd);
825         if (ret != LOCATION_ERROR_NONE) {
826                 return __convert_error_code(ret);
827         }
828
829         return LOCATIONS_ERROR_NONE;
830 }
831
832 /////////////////////////////////////////
833 // GPS Status & Satellites
834 ////////////////////////////////////////
835
836 int gps_status_get_nmea(location_manager_h manager, char **nmea)
837 {
838         LOCATIONS_NULL_ARG_CHECK(manager);
839         LOCATIONS_NULL_ARG_CHECK(nmea);
840         location_manager_s *handle = (location_manager_s *) manager;
841
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;
849                 }
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;
854         }
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;
861         }
862         *nmea = NULL;
863         *nmea = strdup(nmea_data);
864         if (*nmea == NULL) {
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;
868         }
869         g_free(nmea_data);
870         return LOCATIONS_ERROR_NONE;
871 }
872
873 int gps_status_get_satellite(location_manager_h manager, int *num_of_active, int *num_of_inview, time_t *timestamp)
874 {
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;
887                 }
888
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;
892         }
893
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);
898         sat = NULL;
899         return LOCATIONS_ERROR_NONE;
900 }
901
902 int gps_status_set_satellite_updated_cb(location_manager_h manager, gps_status_satellite_updated_cb callback, int interval, void *user_data)
903 {
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);
910 }
911
912 int gps_status_unset_satellite_updated_cb(location_manager_h manager)
913 {
914         return __unset_callback(_LOCATIONS_EVENT_TYPE_SATELLITE, manager);
915 }
916
917
918 int gps_status_foreach_satellites_in_view(location_manager_h manager, gps_status_get_satellites_cb callback, void *user_data)
919 {
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;
930                 }
931
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;
935         }
936
937         int i;
938         for (i = 0; i < sat->num_of_sat_inview; i++) {
939                 guint prn;
940                 gboolean used;
941                 guint elevation;
942                 guint azimuth;
943                 gint snr;
944                 location_satellite_get_satellite_details(sat, i, &prn, &used, &elevation, &azimuth, &snr);
945                 if (callback(azimuth, elevation, prn, snr, used, user_data) != TRUE)
946                         break;
947         }
948         location_satellite_free(sat);
949         sat = NULL;
950         return LOCATIONS_ERROR_NONE;
951 }
952
953 int gps_status_get_last_satellite(location_manager_h manager, int *num_of_active, int *num_of_inview, time_t *timestamp)
954 {
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;
968                 }
969
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;
973         }
974
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;
980 }
981
982 int gps_status_foreach_last_satellites_in_view(location_manager_h manager, gps_status_get_satellites_cb callback,
983                                                void *user_data)
984 {
985         LOCATIONS_NULL_ARG_CHECK(manager);
986         LOCATIONS_NULL_ARG_CHECK(callback);
987         location_manager_s *handle = (location_manager_s *) manager;
988         int ret;
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;
996                 }
997
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;
1001         }
1002
1003         int i;
1004         for (i = 0; i < last_sat->num_of_sat_inview; i++) {
1005                 guint prn;
1006                 gboolean used;
1007                 guint elevation;
1008                 guint azimuth;
1009                 gint snr;
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) {
1012                         break;
1013                 }
1014         }
1015         location_satellite_free(last_sat);
1016         return LOCATIONS_ERROR_NONE;
1017 }