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.
21 #include <locations.h>
22 #include <location_bounds.h>
23 #include <location_batch.h>
25 static location_manager_h manager;
26 static GMainLoop *g_mainloop = NULL;
29 static int repeat_count;
30 static void location_cleanup();
31 static int location_test();
32 static guint test_timer;
34 static gboolean exit_program(gpointer data)
36 g_main_loop_quit(g_mainloop);
41 static gboolean wait_test()
44 g_source_remove(test_timer);
55 void zone_event_cb(location_boundary_state_e state, double latitude, double longitude, double altitude, time_t timestamp,
58 if (state == LOCATIONS_BOUNDARY_IN) {
59 printf("Entering zone\n");
60 } else { /* state == LOCATIONS_BOUNDARY_OUT */
61 printf("Leaving zone\n");
64 printf("Latitude: %lf, longitude: %lf, altitude: %lf\n", latitude, longitude, altitude);
66 printf("Time: %s\n", ctime(×tamp));
69 static bool last_satellites_foreach_cb(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_in_use,
72 printf("[Last Satellite information] azimuth : %d, elevation: %d, prn: %d, snr: %d, used: %d\n", azimuth, elevation,
77 static bool __poly_coords_cb(location_coords_s coords, void *user_data)
79 printf("location_bounds_foreach_rect_coords(latitude: %lf, longitude: %lf) \n", coords.latitude, coords.longitude);
83 static bool __location_bounds_cb(location_bounds_h bounds, void *user_data)
86 printf("bounds ==NULL\n");
88 location_bounds_type_e type;
89 location_bounds_get_type(bounds, &type);
90 if (type == LOCATION_BOUNDS_CIRCLE) {
91 location_coords_s center;
93 location_bounds_get_circle_coords(bounds, ¢er, &radius);
94 printf("location_bounds_get_circle_coords(center: %lf, %lf, radius: %lf) \n", center.latitude,
95 center.longitude, radius);
97 } else if (type == LOCATION_BOUNDS_RECT) {
98 location_coords_s left_top;
99 location_coords_s right_bottom;
100 location_bounds_get_rect_coords(bounds, &left_top, &right_bottom);
101 printf("location_bounds_get_rect_coords(left_top: %lf, %lf - right_bottom: %lf, %lf) \n",
102 left_top.latitude, left_top.longitude, right_bottom.latitude, right_bottom.longitude);
103 } else if (type == LOCATION_BOUNDS_POLYGON) {
104 location_bounds_foreach_polygon_coords(bounds, __poly_coords_cb, NULL);
110 void location_bounds_test()
113 location_coords_s center;
114 center.latitude = 37.258;
115 center.longitude = 127.056;
117 location_bounds_h bounds_circle;
118 ret = location_bounds_create_circle(center, radius, &bounds_circle);
119 if (ret != LOCATION_BOUNDS_ERROR_NONE) {
120 printf("location_bounds_create_circle() failed\n");
122 printf("Bounds(circle) has been created successfully.\n");
124 ret = location_manager_add_boundary(manager, bounds_circle);
125 if (ret != LOCATIONS_ERROR_NONE) {
126 printf("Setting boundary failed\n");
128 printf("Boundary set\n");
130 location_coords_s center2;
132 ret = location_bounds_get_circle_coords(bounds_circle, ¢er2, &radius2);
133 if (ret != LOCATIONS_ERROR_NONE) {
134 printf("location_bounds_get_circle_coords() failed\n");
136 printf("location_bounds_get_circle_coords(center: %lf, %lf, radius: %lf) \n", center2.latitude,
137 center2.longitude, radius2);
139 /*Add the rect bounds */
140 location_coords_s left_top;
141 left_top.latitude = 30;
142 left_top.longitude = 30;
144 location_coords_s right_bottom;
145 right_bottom.latitude = 10;
146 right_bottom.longitude = 50;
148 location_bounds_h bounds_rect;
149 ret = location_bounds_create_rect(left_top, right_bottom, &bounds_rect);
150 if (ret != LOCATION_BOUNDS_ERROR_NONE) {
151 printf("location_bounds_create_rect() failed\n");
153 printf("Bounds(rect) has been created successfully.\n");
155 ret = location_manager_add_boundary(manager, bounds_rect);
156 if (ret != LOCATIONS_ERROR_NONE) {
157 printf("Setting boundary failed\n");
159 printf("Boundary set\n");
161 location_coords_s left_top2;
162 location_coords_s right_bottom2;
164 ret = location_bounds_get_rect_coords(bounds_rect, &left_top2, &right_bottom2);
165 if (ret != LOCATIONS_ERROR_NONE) {
166 printf("location_bounds_get_rect_coords() failed\n");
168 printf("location_bounds_get_rect_coords(left_top: %lf, %lf - right_bottom: %lf, %lf) \n", left_top2.latitude,
169 left_top2.longitude, right_bottom2.latitude, right_bottom2.longitude);
171 /*Add the polygon bounds */
174 location_coords_s coord_list[poly_size];
176 coord_list[0].latitude = 10;
177 coord_list[0].longitude = 10;
178 coord_list[1].latitude = 20;
179 coord_list[1].longitude = 20;
180 coord_list[2].latitude = 30;
181 coord_list[2].longitude = 30;
183 location_bounds_h bounds_poly;
184 ret = location_bounds_create_polygon(coord_list, poly_size, &bounds_poly);
185 if (ret != LOCATION_BOUNDS_ERROR_NONE) {
186 printf("location_bounds_create_polygon() failed\n");
188 printf("Bounds(polygon) has been created successfully.\n");
190 ret = location_manager_add_boundary(manager, bounds_poly);
191 if (ret != LOCATIONS_ERROR_NONE) {
192 printf("Setting boundary failed\n");
194 printf("Boundary set\n");
196 ret = location_bounds_foreach_polygon_coords(bounds_poly, __poly_coords_cb, NULL);
197 if (ret != LOCATIONS_ERROR_NONE) {
198 printf("location_bounds_get_rect_coords() failed\n");
201 location_coords_s test_coords;
202 test_coords.latitude = 12;
203 test_coords.longitude = 12;
205 if (location_bounds_contains_coordinates(bounds_poly, test_coords))
206 printf("location_bounds_contains_coordinates() retrun TRUE \n");
208 printf("location_bounds_contains_coordinates() retrun FALSE \n");
210 /*print current bounds */
211 ret = location_manager_foreach_boundary(manager, __location_bounds_cb, (void *)manager);
212 if (ret != LOCATIONS_ERROR_NONE) {
213 printf("location_manager_foreach_boundary() failed\n");
218 void location_get_last_information_test()
221 double altitude, latitude, longitude;
222 double climb, direction, speed;
223 double horizontal, vertical;
224 location_accuracy_level_e level;
226 int num_of_inview, num_of_active;
228 ret = location_manager_get_last_position(manager, &altitude, &latitude, &longitude, ×tamp);
229 if (ret != LOCATIONS_ERROR_NONE) {
230 printf(" Fail: location_manager_get_last_position ---> %d \n", ret);
232 printf("[%ld] alt: %g, lat: %g, long: %g\n", timestamp, altitude, latitude, longitude);
235 ret = location_manager_get_last_velocity(manager, &climb, &direction, &speed, ×tamp);
236 if (ret != LOCATIONS_ERROR_NONE) {
237 printf(" Fail: location_manager_get_last_velocity ---> %d \n", ret);
239 printf("climb: %f, direction: %f, speed: %f\n", climb, direction, speed);
242 ret = location_manager_get_last_accuracy(manager, &level, &horizontal, &vertical);
243 if (ret != LOCATIONS_ERROR_NONE) {
244 printf(" Fail: location_manager_get_last_accuracy ---> %d \n", ret);
246 printf("Level: %d, horizontal: %g, vertical: %g\n", level, horizontal, vertical);
249 ret = gps_status_get_last_satellite(manager, &num_of_active, &num_of_inview, ×tamp);
250 if (ret != LOCATIONS_ERROR_NONE) {
251 printf(" Fail: gps_status_get_last_satellite_count_in_view ---> %d \n", ret);
253 printf("[%ld] Satellite number of active: %d, in view: %d\n", timestamp, num_of_active, num_of_inview);
256 ret = gps_status_foreach_last_satellites_in_view(manager, last_satellites_foreach_cb, NULL);
257 if (ret != LOCATIONS_ERROR_NONE) {
258 printf(" Fail: gps_status_foreach_last_satellites_in_view ---> %d \n", ret);
263 static void _state_change_cb(location_service_state_e state, void *user_data)
265 fprintf(stderr, "--------------------------state change: %d---------\n", state);
266 location_manager_h lm = (location_manager_h) user_data;
267 if (state == LOCATIONS_SERVICE_ENABLED) {
273 ret = location_manager_get_position(lm, &altitude, &latitude, &longitude, ×tamp);
274 if (ret != LOCATIONS_ERROR_NONE) {
275 printf(" Fail: location_manager_get_position ---> %d \n", ret);
277 printf("[%ld] alt: %g, lat %g, long %g\n", timestamp, altitude, latitude, longitude);
280 location_accuracy_level_e level;
283 ret = location_manager_get_accuracy(lm, &level, &horizontal, &vertical);
284 if (ret != LOCATIONS_ERROR_NONE) {
285 printf(" Fail: location_manager_get_accuracy ---> %d \n", ret);
287 printf("Level: %d, horizontal: %g, vertical %g\n", level, horizontal, vertical);
291 ret = gps_status_get_nmea(lm, &nmea);
292 if (ret != LOCATIONS_ERROR_NONE) {
293 printf(" Fail: gps_status_get_nmea ---> %d \n", ret);
295 printf("NMEA: %s\n", nmea);
303 void _position_updated_cb(double latitude, double longitude, double altitude, time_t timestamp, void *user_data)
305 fprintf(stderr, "-------------------------- position updated --------------------------\n");
306 printf("[%ld] lat[%f] lon[%f] alt[%f]\n", timestamp, latitude, longitude, altitude);
310 if (repeat_count > 2) {
311 test_timer = g_timeout_add_seconds(1, wait_test, NULL);
315 void _velocity_updated_cb(double speed, double direction, double climb, time_t timestamp, void *user_data)
317 fprintf(stderr, "-------------------------- velocity updated --------------------------\n");
318 printf("[%ld] speed[%f] direction[%f] climb[%f]\n", timestamp, speed, direction, climb);
321 void _location_cb(int error, double latitude, double longitude, double altitude, time_t timestamp, double speed, double climb, double direction, void *user_data)
323 printf("error[%d]\n", error);
324 printf("location_cb: lat[%f] lon[%f] alt[%f]\n", latitude, longitude, altitude);
325 printf("speed[%f] climb[%f] direction[%f]\n", speed, climb, direction);
327 test_timer = g_timeout_add_seconds(1, wait_test, NULL);
330 void _location_changed_cb(double latitude, double longitude, double altitude, double speed, double direction, double horizontal_accuracy, time_t timestamp, void *user_data)
332 fprintf(stderr, "-------------------------- location changed --------------------------\n");
333 printf("[%ld] lat[%f] lon[%f] alt[%f] speed[%lf] direction[%lf], horizontal_accuracy[%lf]\n", timestamp, latitude, longitude, altitude, speed, direction, horizontal_accuracy);
337 if (repeat_count > 2) {
338 test_timer = g_timeout_add_seconds(1, wait_test, NULL);
342 bool _get_location_cb(double latitude, double longitude, double altitude, double speed, double direction, double horizontal, double vertical, time_t timestamp, void *user_data)
344 fprintf(stderr, "-------------------------- batch: get location --------------------------\n");
345 printf("[%ld] lat[%f] lon[%f] alt[%f] speed[%lf] direction[%lf], horizontal_accuracy[%lf]\n", timestamp, latitude, longitude, altitude, speed, direction, horizontal);
349 void _location_batch_cb(int num_of_location, void *user_data)
351 fprintf(stderr, "-------------------------- location batch --------------------------\n");
352 printf("num_of_location: [%d]\n", num_of_location);
354 location_manager_h manager = user_data;
356 if (num_of_location > 0) {
357 location_manager_foreach_location_batch(manager, _get_location_cb, user_data);
361 if (repeat_count > 1) {
362 test_timer = g_timeout_add_seconds(1, wait_test, NULL);
366 static void __setting_cb(location_method_e method, bool enable, void *user_data)
368 printf("method[%d], enable[%d]\n", method, enable);
371 static void print_location_status()
373 printf("==== LOCATION Setting state =====\n");
374 bool is_enabled = FALSE;
375 location_manager_is_enabled_method(LOCATIONS_METHOD_HYBRID, &is_enabled);
376 printf("hybrid: %d, ", is_enabled);
378 location_manager_is_enabled_method(LOCATIONS_METHOD_GPS, &is_enabled);
379 printf("gps: %d, ", is_enabled);
381 location_manager_is_enabled_method(LOCATIONS_METHOD_WPS, &is_enabled);
382 printf("wps: %d\n", is_enabled);
385 static int enable_method(location_method_e method, bool enable)
388 printf("==== LOCATION Setting changed =====\n");
390 location_manager_set_setting_changed_cb(LOCATIONS_METHOD_HYBRID, __setting_cb, NULL);
392 printf("method[%d], enable[%d]\n", method, enable);
393 ret = location_manager_enable_method(method, enable);
395 location_manager_unset_setting_changed_cb(LOCATIONS_METHOD_HYBRID);
399 static void print_menu()
401 printf("============= LOCATION TEST =============\n");
402 printf("[1] Get location: LOCATIONS_METHOD_HYBRID\n");
403 printf("[2] Get location: LOCATIONS_METHOD_GPS\n");
404 printf("[3] Get location: LOCATIONS_METHOD_WPS\n");
405 printf("[4] Single location: LOCATIONS_METHOD_HYBRID\n");
406 printf("[5] Single location: LOCATIONS_METHOD_GPS\n");
407 printf("[6] Single location: LOCATIONS_METHOD_WPS\n\n");
408 printf("[11] Change update interval: LOCATIONS_METHOD_HYBRID\n");
409 printf("[12] Change update interval: LOCATIONS_METHOD_GPS\n\n");
410 printf("[21] Distance based location update: LOCATIONS_METHOD_HYBRID\n");
411 printf("[22] Distance based location update: LOCATIONS_METHOD_GPS\n");
412 printf("[23] Distance based location update: LOCATIONS_METHOD_WPS\n\n");
413 printf("[31] Location batch update: LOCATIONS_METHOD_GPS\n\n");
414 printf("[41] Turn on/off method: LOCATIONS_METHOD_HYBRID\n");
415 printf("[42] Turn on/off method: LOCATIONS_METHOD_GPS\n");
416 printf("[43] Turn on/off method: LOCATIONS_METHOD_WPS\n\n");
417 printf("[51] Boundary Test\n\n");
418 printf("[0] Exit!!!\n\n");
419 printf("Select menu: ");
421 if (scanf("%d", &menu) < 0) {
422 printf("Can't read menu !!!\n");
426 static int location_test()
428 int ret = LOCATIONS_ERROR_NONE;
433 print_location_status();
442 int method = menu - 1;
443 ret = location_manager_create(method, &manager);
444 printf("location_manager_create (method: %d): %d\n", method, ret);
445 ret = location_manager_start(manager);
446 printf("start: %d\n", ret);
454 printf("\n Input timeout ==> ");
455 ret = scanf("%d", &timeout);
457 int method = menu - 4;
458 ret = location_manager_create(method, &manager);
459 ret = location_manager_request_single_location(manager, timeout, _location_cb, manager);
460 printf("request single_location (method: %d): %d\n", method, ret);
468 printf("\n Input position interval ==> ");
469 ret = scanf("%d", &interval);
471 int method = menu - 11;
472 ret = location_manager_create(method, &manager);
473 printf("location_manager_create (method: %d): %d\n", method, ret);
475 ret = location_manager_set_position_updated_cb(manager, _position_updated_cb, interval, (void *)manager);
476 printf("set_position_updated_cb: %d\n", ret);
479 ret = location_manager_set_velocity_updated_cb(manager, _velocity_updated_cb, interval*2, (void *)manager);
480 printf("set_velocity_updated_cb: %d\n", ret);
483 ret = location_manager_set_location_changed_cb(manager, _location_changed_cb, interval * 2, (void *)manager);
484 printf("set_location_changed_cb: %d\n", ret);
486 ret = location_manager_start(manager);
487 printf("start: %d\n", ret);
494 int method = menu - 21;
496 printf("\n Input position interval ==> ");
497 ret = scanf("%d", &interval);
499 ret = location_manager_create(method, &manager);
500 printf("location_manager_create (method : %d)", method);
502 /*ret = location_manager_set_position_updated_cb(manager, _position_updated_cb, interval, (void *)manager); */
503 /*printf("set position changed callback: %d\n", ret); */
505 ret = location_manager_set_distance_based_location_changed_cb(manager, _location_changed_cb, interval, 30, (void *)manager);
506 printf("set_distance_based_location_changed_cb: %d\n", ret);
508 ret = location_manager_start(manager);
509 printf("start: %d\n", ret);
514 printf("\n Input batch interval ==> ");
515 ret = scanf("%d", &interval);
518 printf(" Input batch period ==> ");
519 ret = scanf("%d", &period);
522 ret = location_manager_create(LOCATIONS_METHOD_GPS, &manager);
523 printf("location_manager_create (method : %d)\n", LOCATIONS_METHOD_GPS);
525 ret = location_manager_set_location_batch_cb(manager, _location_batch_cb, interval, period, (void *)manager);
526 printf("set_location_batch_cb: %d\n", ret);
528 ret = location_manager_start_batch(manager);
529 printf("start_batch: %d\n", ret);
535 int method = menu - 41;
538 printf("\n Input ON: 1 or OFF: 0 ==> ");
539 ret = scanf("%d", &onoff);
541 ret = enable_method(method, onoff);
542 printf("Enabling method: [%d], ret=%d\n", method, ret);
547 location_bounds_h hPolyLocationBound = NULL;
548 bool bIsContained = false;
550 location_coords_s location_coord_list[nPolySize];
551 location_coord_list[0].latitude = 10;
552 location_coord_list[0].longitude = 10;
553 location_coord_list[1].latitude = 20;
554 location_coord_list[1].longitude = 20;
555 location_coord_list[2].latitude = 30;
556 location_coord_list[2].longitude = 10;
558 //Checking coordinates in location boundary
559 location_coords_s testLocationCoordinates;
560 testLocationCoordinates.latitude = 20;
561 testLocationCoordinates.longitude = 12;
562 location_bound_error_e nRet = location_bounds_create_polygon(location_coord_list, nPolySize, &hPolyLocationBound);
564 printf("location_bounds_create_polygon: %d\n", nRet);
566 bIsContained = location_bounds_contains_coordinates(hPolyLocationBound, testLocationCoordinates);// Target API
567 printf("bIsContained: %d\n", bIsContained);
569 location_bounds_destroy(hPolyLocationBound);
573 g_timeout_add_seconds(1, exit_program, NULL);
576 printf("Exit!!! Input: %d\n", menu);
577 g_timeout_add_seconds(1, exit_program, NULL);
581 if (ret != LOCATIONS_ERROR_NONE) {
582 printf("Test Failed!!! [%d]\n", ret);
583 g_timeout_add_seconds(1, exit_program, NULL);
588 ret = location_manager_set_service_state_changed_cb(manager, _state_change_cb, (void *)manager);
589 printf("set_service_state_changed_cb: %d\n", ret);
592 ret = location_manager_set_position_updated_cb(manager, _position_updated_cb, interval, (void *)manager);
593 printf("set_position_updated_cb: %d\n", ret);
597 g_timeout_add_seconds(1, wait_test, NULL);
603 static void location_cleanup()
606 if (manager != NULL) {
607 ret = location_manager_stop(manager);
608 printf("stop: %d\n", ret);
610 ret = location_manager_unset_service_state_changed_cb(manager);
611 printf("unset_service_state_changed_cb: %d\n", ret);
613 ret = location_manager_unset_position_updated_cb(manager);
614 printf("unset_position_updated_cb: %d\n", ret);
616 ret = location_manager_destroy(manager);
617 printf("destroy: %d\n", ret);
622 int main(int argc, char **argv)
624 g_mainloop = g_main_loop_new(NULL, 0);
626 g_main_loop_run(g_mainloop);