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 location_manager_h manager;
26 void zone_event_cb(location_boundary_state_e state, double latitude, double longitude, double altitude, time_t timestamp,
29 if (state == LOCATIONS_BOUNDARY_IN) {
30 printf("Entering zone\n");
31 } else // state == LOCATIONS_BOUNDARY_OUT
33 printf("Leaving zone\n");
36 printf("Latitude: %lf, longitude: %lf, altitude: %lf\n", latitude, longitude, altitude);
38 printf("Time: %s\n", ctime(×tamp));
41 static bool satellites_foreach_cb(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_in_use,
44 printf("[Satellite information] azimuth : %d, elevation : %d, prn :%d, snr : %d, used: %d\n", azimuth, elevation, prn,
49 static bool last_satellites_foreach_cb(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_in_use,
52 printf("[Last Satellite information] azimuth : %d, elevation : %d, prn :%d, snr : %d, used: %d\n", azimuth, elevation,
57 static void _state_change_cb(location_service_state_e state, void *user_data)
59 fprintf(stderr, "--------------------------state change %d---------\n", state);
60 location_manager_h lm = (location_manager_h) user_data;
61 if (state == LOCATIONS_SERVICE_ENABLED) {
67 ret = location_manager_get_position(lm, &altitude, &latitude, &longitude, ×tamp);
68 if (ret != LOCATIONS_ERROR_NONE) {
69 printf(" Fail : location_manager_get_position ---> %d \n", ret);
71 printf("[%ld] alt: %g, lat %g, long %g\n", timestamp, altitude, latitude, longitude);
74 location_accuracy_level_e level;
77 ret = location_manager_get_accuracy(lm, &level, &horizontal, &vertical);
78 if (ret != LOCATIONS_ERROR_NONE) {
79 printf(" Fail : location_manager_get_accuracy ---> %d \n", ret);
81 printf("Level : %d, horizontal: %g, vertical %g\n", level, horizontal, vertical);
85 ret = gps_status_get_nmea(lm, &nmea);
86 if (ret != LOCATIONS_ERROR_NONE) {
87 printf(" Fail : gps_status_get_nmea ---> %d \n", ret);
89 printf("NMEA : %s\n", nmea);
93 int num_of_view, num_of_active;
94 ret = gps_status_get_satellite(lm, &num_of_active, &num_of_view, ×tamp);
95 if (ret != LOCATIONS_ERROR_NONE) {
96 printf(" Fail : gps_status_get_satellite_count_in_view ---> %d \n", ret);
98 printf("[%ld] Satellite number of active : %d, in view : %d\n", timestamp, num_of_active, num_of_view);
101 ret = gps_status_foreach_satellites_in_view(lm, satellites_foreach_cb, user_data);
102 if (ret != LOCATIONS_ERROR_NONE) {
103 printf(" Fail : gps_status_foreach_satellites_in_view ---> %d \n", ret);
108 static bool __poly_coords_cb(location_coords_s coords, void *user_data)
110 printf("location_bounds_foreach_rect_coords(latitude : %lf, longitude: %lf) \n", coords.latitude, coords.longitude);
114 static bool __location_bounds_cb(location_bounds_h bounds, void *user_data)
117 printf("bounds ==NULL\n");
119 location_bounds_type_e type;
120 location_bounds_get_type(bounds, &type);
121 if (type == LOCATION_BOUNDS_CIRCLE) {
122 location_coords_s center;
124 location_bounds_get_circle_coords(bounds, ¢er, &radius);
125 printf("location_bounds_get_circle_coords(center : %lf, %lf, radius : %lf) \n", center.latitude,
126 center.longitude, radius);
128 } else if (type == LOCATION_BOUNDS_RECT) {
129 location_coords_s left_top;
130 location_coords_s right_bottom;
131 location_bounds_get_rect_coords(bounds, &left_top, &right_bottom);
132 printf("location_bounds_get_rect_coords(left_top : %lf, %lf - right_bottom : %lf, %lf) \n",
133 left_top.latitude, left_top.longitude, right_bottom.latitude, right_bottom.longitude);
134 } else if (type == LOCATION_BOUNDS_POLYGON) {
135 location_bounds_foreach_polygon_coords(bounds, __poly_coords_cb, NULL);
141 void location_bounds_test()
144 location_coords_s center;
145 center.latitude = 37.258;
146 center.longitude = 127.056;
148 location_bounds_h bounds_circle;
149 ret = location_bounds_create_circle(center, radius, &bounds_circle);
150 if (ret != LOCATION_BOUNDS_ERROR_NONE) {
151 printf("location_bounds_create_circle() failed\n");
153 printf("Bounds(circle) has been created successfully.\n");
155 ret = location_manager_add_boundary(manager, bounds_circle);
156 if (ret != LOCATIONS_ERROR_NONE) {
157 printf("Setting boundary failed\n");
159 printf("Boundary set\n");
161 location_coords_s center2;
163 ret = location_bounds_get_circle_coords(bounds_circle, ¢er2, &radius2);
164 if (ret != LOCATIONS_ERROR_NONE) {
165 printf("location_bounds_get_circle_coords() failed\n");
167 printf("location_bounds_get_circle_coords(center : %lf, %lf, radius : %lf) \n", center2.latitude,
168 center2.longitude, radius2);
170 //Add the rect bounds
171 location_coords_s left_top;
172 left_top.latitude = 30;
173 left_top.longitude = 30;
175 location_coords_s right_bottom;
176 right_bottom.latitude = 10;
177 right_bottom.longitude = 50;
179 location_bounds_h bounds_rect;
180 ret = location_bounds_create_rect(left_top, right_bottom, &bounds_rect);
181 if (ret != LOCATION_BOUNDS_ERROR_NONE) {
182 printf("location_bounds_create_rect() failed\n");
184 printf("Bounds(rect) has been created successfully.\n");
186 ret = location_manager_add_boundary(manager, bounds_rect);
187 if (ret != LOCATIONS_ERROR_NONE) {
188 printf("Setting boundary failed\n");
190 printf("Boundary set\n");
192 location_coords_s left_top2;
193 location_coords_s right_bottom2;
195 ret = location_bounds_get_rect_coords(bounds_rect, &left_top2, &right_bottom2);
196 if (ret != LOCATIONS_ERROR_NONE) {
197 printf("location_bounds_get_rect_coords() failed\n");
199 printf("location_bounds_get_rect_coords(left_top : %lf, %lf - right_bottom : %lf, %lf) \n", left_top2.latitude,
200 left_top2.longitude, right_bottom2.latitude, right_bottom2.longitude);
202 //Add the polygon bounds
205 location_coords_s coord_list[poly_size];
207 coord_list[0].latitude = 10;
208 coord_list[0].longitude = 10;
209 coord_list[1].latitude = 20;
210 coord_list[1].longitude = 20;
211 coord_list[2].latitude = 30;
212 coord_list[2].longitude = 30;
214 location_bounds_h bounds_poly;
215 ret = location_bounds_create_polygon(coord_list, poly_size, &bounds_poly);
216 if (ret != LOCATION_BOUNDS_ERROR_NONE) {
217 printf("location_bounds_create_polygon() failed\n");
219 printf("Bounds(polygon) has been created successfully.\n");
221 ret = location_manager_add_boundary(manager, bounds_poly);
222 if (ret != LOCATIONS_ERROR_NONE) {
223 printf("Setting boundary failed\n");
225 printf("Boundary set\n");
227 ret = location_bounds_foreach_polygon_coords(bounds_poly, __poly_coords_cb, NULL);
228 if (ret != LOCATIONS_ERROR_NONE) {
229 printf("location_bounds_get_rect_coords() failed\n");
232 location_coords_s test_coords;
233 test_coords.latitude = 12;
234 test_coords.longitude = 12;
236 if (location_bounds_contains_coordinates(bounds_poly, test_coords))
237 printf("location_bounds_contains_coordinates() retrun TRUE \n");
239 printf("location_bounds_contains_coordinates() retrun FALSE \n");
241 //print current bounds
242 ret = location_manager_foreach_boundary(manager, __location_bounds_cb, (void *)manager);
243 if (ret != LOCATIONS_ERROR_NONE) {
244 printf("location_manager_foreach_boundary() failed\n");
249 void location_get_last_information_test()
252 double altitude, latitude, longitude;
253 double climb, direction, speed;
254 double horizontal, vertical;
255 location_accuracy_level_e level;
257 int num_of_inview, num_of_active;
259 ret = location_manager_get_last_position(manager, &altitude, &latitude, &longitude, ×tamp);
260 if (ret != LOCATIONS_ERROR_NONE) {
261 printf(" Fail : location_manager_get_last_position ---> %d \n", ret);
263 printf("[%ld] alt: %g, lat: %g, long: %g\n", timestamp, altitude, latitude, longitude);
266 ret = location_manager_get_last_velocity(manager, &climb, &direction, &speed, ×tamp);
267 if (ret != LOCATIONS_ERROR_NONE) {
268 printf(" Fail : location_manager_get_last_velocity ---> %d \n", ret);
270 printf("climb: %f, direction: %f, speed: %f\n", climb, direction, speed);
273 ret = location_manager_get_last_accuracy(manager, &level, &horizontal, &vertical);
274 if (ret != LOCATIONS_ERROR_NONE) {
275 printf(" Fail : location_manager_get_last_accuracy ---> %d \n", ret);
277 printf("Level : %d, horizontal: %g, vertical : %g\n", level, horizontal, vertical);
280 ret = gps_status_get_last_satellite(manager, &num_of_active, &num_of_inview, ×tamp);
281 if (ret != LOCATIONS_ERROR_NONE) {
282 printf(" Fail : gps_status_get_last_satellite_count_in_view ---> %d \n", ret);
284 printf("[%ld] Satellite number of active : %d, in view : %d\n", timestamp, num_of_active, num_of_inview);
287 ret = gps_status_foreach_last_satellites_in_view(manager, last_satellites_foreach_cb, NULL);
288 if (ret != LOCATIONS_ERROR_NONE) {
289 printf(" Fail : gps_status_foreach_last_satellites_in_view ---> %d \n", ret);
296 ret = location_manager_create(LOCATIONS_METHOD_GPS, &manager);
297 printf("create : %d\n", ret);
299 location_bounds_test();
300 location_get_last_information_test();
302 //set zone changed callback
303 ret = location_manager_set_zone_changed_cb(manager, zone_event_cb, (void *)manager);
304 printf("set zone callback : %d\n", ret);
306 ret = location_manager_set_service_state_changed_cb(manager, _state_change_cb, (void *)manager);
307 printf("set state callback : %d\n", ret);
309 ret = location_manager_start(manager);
310 printf("start : %d\n", ret);
314 static GMainLoop *g_mainloop = NULL;
316 static gboolean exit_program(gpointer data)
318 if (manager == NULL) {
319 printf("manager == NULL \n");
321 int ret = location_manager_stop(manager);
322 printf("stop : %d\n", ret);
323 ret = location_manager_destroy(manager);
324 printf("destroy : %d\n", ret);
326 g_main_loop_quit(g_mainloop);
327 printf("Quit g_main_loop\n");
331 int main(int argc, char **argv)
333 g_setenv("PKG_NAME", "com.samsung.location-test", 1);
334 g_mainloop = g_main_loop_new(NULL, 0);
336 g_timeout_add_seconds(90, exit_program, NULL);
337 g_main_loop_run(g_mainloop);