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>
24 static location_manager_h manager;
25 static GMainLoop *g_mainloop = NULL;
28 static void location_cleanup();
29 static int location_test();
31 static gboolean exit_program(gpointer data)
33 g_main_loop_quit(g_mainloop);
38 static gboolean wait_test()
47 void zone_event_cb(location_boundary_state_e state, double latitude, double longitude, double altitude, time_t timestamp,
50 if (state == LOCATIONS_BOUNDARY_IN) {
51 printf("Entering zone\n");
52 } else { /* state == LOCATIONS_BOUNDARY_OUT */
53 printf("Leaving zone\n");
56 printf("Latitude: %lf, longitude: %lf, altitude: %lf\n", latitude, longitude, altitude);
58 printf("Time: %s\n", ctime(×tamp));
61 static bool last_satellites_foreach_cb(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_in_use,
64 printf("[Last Satellite information] azimuth : %d, elevation: %d, prn: %d, snr: %d, used: %d\n", azimuth, elevation,
70 static bool satellites_foreach_cb(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_in_use,
73 printf("[Satellite information] azimuth : %d, elevation: %d, prn: %d, snr: %d, used: %d\n", azimuth, elevation, prn,
78 static void _state_change_cb(location_service_state_e state, void *user_data)
80 fprintf(stderr, "--------------------------state change %d---------\n", state);
81 location_manager_h lm = (location_manager_h) user_data;
82 if (state == LOCATIONS_SERVICE_ENABLED) {
88 ret = location_manager_get_position(lm, &altitude, &latitude, &longitude, ×tamp);
89 if (ret != LOCATIONS_ERROR_NONE) {
90 printf(" Fail: location_manager_get_position ---> %d \n", ret);
92 printf("[%ld] alt: %g, lat %g, long %g\n", timestamp, altitude, latitude, longitude);
95 location_accuracy_level_e level;
98 ret = location_manager_get_accuracy(lm, &level, &horizontal, &vertical);
99 if (ret != LOCATIONS_ERROR_NONE) {
100 printf(" Fail: location_manager_get_accuracy ---> %d \n", ret);
102 printf("Level: %d, horizontal: %g, vertical %g\n", level, horizontal, vertical);
106 ret = gps_status_get_nmea(lm, &nmea);
107 if (ret != LOCATIONS_ERROR_NONE) {
108 printf(" Fail: gps_status_get_nmea ---> %d \n", ret);
110 printf("NMEA: %s\n", nmea);
114 int num_of_view = 0, num_of_active = 0;
115 ret = gps_status_get_satellite(lm, &num_of_active, &num_of_view, ×tamp);
116 if (ret != LOCATIONS_ERROR_NONE) {
117 printf(" Fail: gps_status_get_satellite_count_in_view ---> %d \n", ret);
119 printf("[%ld] Satellite number of active: %d, in view: %d\n", timestamp, num_of_active, num_of_view);
123 ret = gps_status_foreach_satellites_in_view(lm, satellites_foreach_cb, user_data);
124 if (ret != LOCATIONS_ERROR_NONE) {
125 printf(" Fail: gps_status_foreach_satellites_in_view ---> %d \n", ret);
128 g_timeout_add_seconds(1, wait_test, NULL);
133 static bool __poly_coords_cb(location_coords_s coords, void *user_data)
135 printf("location_bounds_foreach_rect_coords(latitude: %lf, longitude: %lf) \n", coords.latitude, coords.longitude);
139 static bool __location_bounds_cb(location_bounds_h bounds, void *user_data)
142 printf("bounds ==NULL\n");
144 location_bounds_type_e type;
145 location_bounds_get_type(bounds, &type);
146 if (type == LOCATION_BOUNDS_CIRCLE) {
147 location_coords_s center;
149 location_bounds_get_circle_coords(bounds, ¢er, &radius);
150 printf("location_bounds_get_circle_coords(center: %lf, %lf, radius: %lf) \n", center.latitude,
151 center.longitude, radius);
153 } else if (type == LOCATION_BOUNDS_RECT) {
154 location_coords_s left_top;
155 location_coords_s right_bottom;
156 location_bounds_get_rect_coords(bounds, &left_top, &right_bottom);
157 printf("location_bounds_get_rect_coords(left_top: %lf, %lf - right_bottom: %lf, %lf) \n",
158 left_top.latitude, left_top.longitude, right_bottom.latitude, right_bottom.longitude);
159 } else if (type == LOCATION_BOUNDS_POLYGON) {
160 location_bounds_foreach_polygon_coords(bounds, __poly_coords_cb, NULL);
166 void location_bounds_test()
169 location_coords_s center;
170 center.latitude = 37.258;
171 center.longitude = 127.056;
173 location_bounds_h bounds_circle;
174 ret = location_bounds_create_circle(center, radius, &bounds_circle);
175 if (ret != LOCATION_BOUNDS_ERROR_NONE) {
176 printf("location_bounds_create_circle() failed\n");
178 printf("Bounds(circle) has been created successfully.\n");
180 ret = location_manager_add_boundary(manager, bounds_circle);
181 if (ret != LOCATIONS_ERROR_NONE) {
182 printf("Setting boundary failed\n");
184 printf("Boundary set\n");
186 location_coords_s center2;
188 ret = location_bounds_get_circle_coords(bounds_circle, ¢er2, &radius2);
189 if (ret != LOCATIONS_ERROR_NONE) {
190 printf("location_bounds_get_circle_coords() failed\n");
192 printf("location_bounds_get_circle_coords(center: %lf, %lf, radius: %lf) \n", center2.latitude,
193 center2.longitude, radius2);
195 /*Add the rect bounds */
196 location_coords_s left_top;
197 left_top.latitude = 30;
198 left_top.longitude = 30;
200 location_coords_s right_bottom;
201 right_bottom.latitude = 10;
202 right_bottom.longitude = 50;
204 location_bounds_h bounds_rect;
205 ret = location_bounds_create_rect(left_top, right_bottom, &bounds_rect);
206 if (ret != LOCATION_BOUNDS_ERROR_NONE) {
207 printf("location_bounds_create_rect() failed\n");
209 printf("Bounds(rect) has been created successfully.\n");
211 ret = location_manager_add_boundary(manager, bounds_rect);
212 if (ret != LOCATIONS_ERROR_NONE) {
213 printf("Setting boundary failed\n");
215 printf("Boundary set\n");
217 location_coords_s left_top2;
218 location_coords_s right_bottom2;
220 ret = location_bounds_get_rect_coords(bounds_rect, &left_top2, &right_bottom2);
221 if (ret != LOCATIONS_ERROR_NONE) {
222 printf("location_bounds_get_rect_coords() failed\n");
224 printf("location_bounds_get_rect_coords(left_top: %lf, %lf - right_bottom: %lf, %lf) \n", left_top2.latitude,
225 left_top2.longitude, right_bottom2.latitude, right_bottom2.longitude);
227 /*Add the polygon bounds */
230 location_coords_s coord_list[poly_size];
232 coord_list[0].latitude = 10;
233 coord_list[0].longitude = 10;
234 coord_list[1].latitude = 20;
235 coord_list[1].longitude = 20;
236 coord_list[2].latitude = 30;
237 coord_list[2].longitude = 30;
239 location_bounds_h bounds_poly;
240 ret = location_bounds_create_polygon(coord_list, poly_size, &bounds_poly);
241 if (ret != LOCATION_BOUNDS_ERROR_NONE) {
242 printf("location_bounds_create_polygon() failed\n");
244 printf("Bounds(polygon) has been created successfully.\n");
246 ret = location_manager_add_boundary(manager, bounds_poly);
247 if (ret != LOCATIONS_ERROR_NONE) {
248 printf("Setting boundary failed\n");
250 printf("Boundary set\n");
252 ret = location_bounds_foreach_polygon_coords(bounds_poly, __poly_coords_cb, NULL);
253 if (ret != LOCATIONS_ERROR_NONE) {
254 printf("location_bounds_get_rect_coords() failed\n");
257 location_coords_s test_coords;
258 test_coords.latitude = 12;
259 test_coords.longitude = 12;
261 if (location_bounds_contains_coordinates(bounds_poly, test_coords))
262 printf("location_bounds_contains_coordinates() retrun TRUE \n");
264 printf("location_bounds_contains_coordinates() retrun FALSE \n");
266 /*print current bounds */
267 ret = location_manager_foreach_boundary(manager, __location_bounds_cb, (void *)manager);
268 if (ret != LOCATIONS_ERROR_NONE) {
269 printf("location_manager_foreach_boundary() failed\n");
274 void location_get_last_information_test()
277 double altitude, latitude, longitude;
278 double climb, direction, speed;
279 double horizontal, vertical;
280 location_accuracy_level_e level;
282 int num_of_inview, num_of_active;
284 ret = location_manager_get_last_position(manager, &altitude, &latitude, &longitude, ×tamp);
285 if (ret != LOCATIONS_ERROR_NONE) {
286 printf(" Fail: location_manager_get_last_position ---> %d \n", ret);
288 printf("[%ld] alt: %g, lat: %g, long: %g\n", timestamp, altitude, latitude, longitude);
291 ret = location_manager_get_last_velocity(manager, &climb, &direction, &speed, ×tamp);
292 if (ret != LOCATIONS_ERROR_NONE) {
293 printf(" Fail: location_manager_get_last_velocity ---> %d \n", ret);
295 printf("climb: %f, direction: %f, speed: %f\n", climb, direction, speed);
298 ret = location_manager_get_last_accuracy(manager, &level, &horizontal, &vertical);
299 if (ret != LOCATIONS_ERROR_NONE) {
300 printf(" Fail: location_manager_get_last_accuracy ---> %d \n", ret);
302 printf("Level: %d, horizontal: %g, vertical: %g\n", level, horizontal, vertical);
305 ret = gps_status_get_last_satellite(manager, &num_of_active, &num_of_inview, ×tamp);
306 if (ret != LOCATIONS_ERROR_NONE) {
307 printf(" Fail: gps_status_get_last_satellite_count_in_view ---> %d \n", ret);
309 printf("[%ld] Satellite number of active: %d, in view: %d\n", timestamp, num_of_active, num_of_inview);
312 ret = gps_status_foreach_last_satellites_in_view(manager, last_satellites_foreach_cb, NULL);
313 if (ret != LOCATIONS_ERROR_NONE) {
314 printf(" Fail: gps_status_foreach_last_satellites_in_view ---> %d \n", ret);
319 void location_cb(int error, double latitude, double longitude, double altitude, time_t timestamp, double speed, double climb, double direction, void *user_data)
321 printf("error[%d]\n", error);
322 printf("location_cb: lat[%f] lon[%f] alt[%f]\n", latitude, longitude, altitude);
323 printf("speed[%f] climb[%f] direction[%f]\n", speed, climb, direction);
325 g_timeout_add_seconds(1, wait_test, NULL);
328 static void __setting_cb(location_method_e method, bool enable, void *user_data)
330 printf("method[%d], enable[%d]\n", method, enable);
333 static void select_menu()
339 str = fgets(buf, 255, stdin);
341 printf("fgets return NULL. \n");
343 len = g_utf8_strlen(buf, -1);
347 menu = g_strdup(buf);
350 static void print_menu()
352 printf("==== LOCATION TEST ======\n");
353 printf("[1] LOCATIONS_METHOD_HYBRID: Get location\n");
354 printf("[2] LOCATIONS_METHOD_GPS: Get location\n");
355 printf("[3] LOCATIONS_METHOD_WPS: Get location\n");
356 printf("[4] LOCATIONS_METHOD_HYBRID: Single location\n");
357 printf("[5] LOCATIONS_METHOD_GPS: Single location\n");
358 printf("[x] Exit!!!\n\n");
359 printf("Select menu: ");
362 static int location_test()
364 int ret = LOCATIONS_ERROR_NONE;
366 printf("==== LOCATION STATUS ======\n");
367 bool is_enabled = FALSE;
368 location_manager_is_enabled_method(LOCATIONS_METHOD_HYBRID, &is_enabled);
369 printf("hybrid: %d, ", is_enabled);
371 location_manager_is_enabled_method(LOCATIONS_METHOD_GPS, &is_enabled);
372 printf("gps: %d, ", is_enabled);
374 location_manager_is_enabled_method(LOCATIONS_METHOD_WPS, &is_enabled);
375 printf("wps: %d\n", is_enabled);
377 location_manager_set_setting_changed_cb(LOCATIONS_METHOD_GPS, __setting_cb, NULL);
378 location_manager_set_setting_changed_cb(LOCATIONS_METHOD_WPS, __setting_cb, NULL);
383 if (strcmp(menu, "1") == 0) {
384 ret = location_manager_create(LOCATIONS_METHOD_HYBRID, &manager);
385 printf("LOCATIONS_METHOD_HYBRID create: %d\n", ret);
386 ret = location_manager_start(manager);
387 printf("start: %d\n", ret);
388 } else if (strcmp(menu, "2") == 0) {
389 ret = location_manager_create(LOCATIONS_METHOD_GPS, &manager);
390 printf("LOCATIONS_METHOD_GPS create: %d\n", ret);
391 ret = location_manager_start(manager);
392 printf("start: %d\n", ret);
393 } else if (strcmp(menu, "3") == 0) {
394 ret = location_manager_create(LOCATIONS_METHOD_WPS, &manager);
395 printf("LOCATIONS_METHOD_WPS create: %d\n", ret);
396 } else if (strcmp(menu, "4") == 0) {
397 ret = location_manager_create(LOCATIONS_METHOD_HYBRID, &manager);
398 printf("LOCATIONS_METHOD_HYBRID create: %d\n", ret);
399 ret = location_manager_request_single_location(manager, 60, location_cb, manager);
400 printf("single_location create: %d\n", ret);
401 } else if (strcmp(menu, "5") == 0) {
402 ret = location_manager_create(LOCATIONS_METHOD_GPS, &manager);
403 printf("LOCATIONS_METHOD_HYBRID create: %d\n", ret);
404 ret = location_manager_request_single_location(manager, 60, location_cb, manager);
405 printf("single_location create: %d\n", ret);
406 } else if (strcmp(menu, "x") == 0) {
407 g_timeout_add_seconds(0.1, exit_program, NULL);
410 printf("Exit!!! Input: %s\n", menu);
411 g_timeout_add_seconds(0.1, exit_program, NULL);
415 if (ret != LOCATIONS_ERROR_NONE) {
416 printf("Test Failed!!! [%d]\n", ret);
417 g_timeout_add_seconds(0.1, exit_program, NULL);
420 ret = location_manager_set_service_state_changed_cb(manager, _state_change_cb, (void *)manager);
421 printf("set state callback: %d\n", ret);
423 /* location_bounds_test(); */
424 /* set zone changed callback */
426 ret = location_manager_set_zone_changed_cb(manager, zone_event_cb, (void *)manager);
427 printf("set zone callback: %d\n", ret);
432 static void location_cleanup()
434 if (manager != NULL) {
435 int ret = location_manager_stop(manager);
436 printf("stop: %d\n", ret);
437 ret = location_manager_destroy(manager);
438 printf("destroy: %d\n", ret);
441 location_manager_unset_setting_changed_cb(LOCATIONS_METHOD_GPS);
442 location_manager_unset_setting_changed_cb(LOCATIONS_METHOD_WPS);
445 int main(int argc, char **argv)
447 g_mainloop = g_main_loop_new(NULL, 0);
449 g_main_loop_run(g_mainloop);