2 * Copyright (c) 2011 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>
23 location_manager_h manager;
25 void zone_event_cb(location_boundary_state_e state, double latitude, double longitude, double altitude, time_t timestamp,
28 if (state == LOCATIONS_BOUNDARY_IN) {
29 printf("Entering zone\n");
30 } else // state == LOCATIONS_BOUNDARY_OUT
32 printf("Leaving zone\n");
35 printf("Latitude: %lf, longitude: %lf, altitude: %lf\n", latitude, longitude, altitude);
37 printf("Time: %s\n", ctime(×tamp));
40 static bool satellites_foreach_cb(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_in_use,
43 printf("[Satellite information] azimuth : %d, elevation : %d, prn :%d, snr : %d, used: %d\n", azimuth, elevation, prn,
48 static bool last_satellites_foreach_cb(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_in_use,
51 printf("[Last Satellite information] azimuth : %d, elevation : %d, prn :%d, snr : %d, used: %d\n", azimuth, elevation,
56 static void _state_change_cb(location_service_state_e state, void *user_data)
58 fprintf(stderr, "--------------------------state change %d---------\n", state);
59 location_manager_h lm = (location_manager_h) user_data;
60 if (state == LOCATIONS_SERVICE_ENABLED) {
66 ret = location_manager_get_position(lm, &altitude, &latitude, &longitude, ×tamp);
67 if (ret != LOCATIONS_ERROR_NONE) {
68 printf(" Fail : location_manager_get_position ---> %d \n", ret);
70 printf("[%ld] alt: %g, lat %g, long %g\n", timestamp, altitude, latitude, longitude);
73 location_accuracy_level_e level;
76 ret = location_manager_get_accuracy(lm, &level, &horizontal, &vertical);
77 if (ret != LOCATIONS_ERROR_NONE) {
78 printf(" Fail : location_manager_get_accuracy ---> %d \n", ret);
80 printf("Level : %d, horizontal: %g, vertical %g\n", level, horizontal, vertical);
84 ret = gps_status_get_nmea(lm, &nmea);
85 if (ret != LOCATIONS_ERROR_NONE) {
86 printf(" Fail : gps_status_get_nmea ---> %d \n", ret);
88 printf("NMEA : %s\n", nmea);
92 int num_of_view, num_of_active;
93 ret = gps_status_get_satellite(lm, &num_of_active, &num_of_view, ×tamp);
94 if (ret != LOCATIONS_ERROR_NONE) {
95 printf(" Fail : gps_status_get_satellite_count_in_view ---> %d \n", ret);
97 printf("[%ld] Satellite number of active : %d, in view : %d\n", timestamp, num_of_active, num_of_view);
100 ret = gps_status_foreach_satellites_in_view(lm, satellites_foreach_cb, user_data);
101 if (ret != LOCATIONS_ERROR_NONE) {
102 printf(" Fail : gps_status_foreach_satellites_in_view ---> %d \n", ret);
107 static bool __poly_coords_cb(location_coords_s coords, void *user_data)
109 printf("location_bounds_foreach_rect_coords(latitude : %lf, longitude: %lf) \n", coords.latitude, coords.longitude);
113 static bool __location_bounds_cb(location_bounds_h bounds, void *user_data)
116 printf("bounds ==NULL\n");
118 location_bounds_type_e type;
119 location_bounds_get_type(bounds, &type);
120 if (type == LOCATION_BOUNDS_CIRCLE) {
121 location_coords_s center;
123 location_bounds_get_circle_coords(bounds, ¢er, &radius);
124 printf("location_bounds_get_circle_coords(center : %lf, %lf, radius : %lf) \n", center.latitude,
125 center.longitude, radius);
127 } else if (type == LOCATION_BOUNDS_RECT) {
128 location_coords_s left_top;
129 location_coords_s right_bottom;
130 location_bounds_get_rect_coords(bounds, &left_top, &right_bottom);
131 printf("location_bounds_get_rect_coords(left_top : %lf, %lf - right_bottom : %lf, %lf) \n",
132 left_top.latitude, left_top.longitude, right_bottom.latitude, right_bottom.longitude);
133 } else if (type == LOCATION_BOUNDS_POLYGON) {
134 location_bounds_foreach_polygon_coords(bounds, __poly_coords_cb, NULL);
140 void location_bounds_test()
143 location_coords_s center;
144 center.latitude = 37.258;
145 center.longitude = 127.056;
147 location_bounds_h bounds_circle;
148 ret = location_bounds_create_circle(center, radius, &bounds_circle);
149 if (ret != LOCATION_BOUNDS_ERROR_NONE) {
150 printf("location_bounds_create_circle() failed\n");
152 printf("Bounds(circle) has been created successfully.\n");
154 ret = location_manager_add_boundary(manager, bounds_circle);
155 if (ret != LOCATIONS_ERROR_NONE) {
156 printf("Setting boundary failed\n");
158 printf("Boundary set\n");
160 location_coords_s center2;
162 ret = location_bounds_get_circle_coords(bounds_circle, ¢er2, &radius2);
163 if (ret != LOCATIONS_ERROR_NONE) {
164 printf("location_bounds_get_circle_coords() failed\n");
166 printf("location_bounds_get_circle_coords(center : %lf, %lf, radius : %lf) \n", center2.latitude,
167 center2.longitude, radius2);
169 //Add the rect bounds
170 location_coords_s left_top;
171 left_top.latitude = 30;
172 left_top.longitude = 30;
174 location_coords_s right_bottom;
175 right_bottom.latitude = 10;
176 right_bottom.longitude = 50;
178 location_bounds_h bounds_rect;
179 ret = location_bounds_create_rect(left_top, right_bottom, &bounds_rect);
180 if (ret != LOCATION_BOUNDS_ERROR_NONE) {
181 printf("location_bounds_create_rect() failed\n");
183 printf("Bounds(rect) has been created successfully.\n");
185 ret = location_manager_add_boundary(manager, bounds_rect);
186 if (ret != LOCATIONS_ERROR_NONE) {
187 printf("Setting boundary failed\n");
189 printf("Boundary set\n");
191 location_coords_s left_top2;
192 location_coords_s right_bottom2;
194 ret = location_bounds_get_rect_coords(bounds_rect, &left_top2, &right_bottom2);
195 if (ret != LOCATIONS_ERROR_NONE) {
196 printf("location_bounds_get_rect_coords() failed\n");
198 printf("location_bounds_get_rect_coords(left_top : %lf, %lf - right_bottom : %lf, %lf) \n", left_top2.latitude,
199 left_top2.longitude, right_bottom2.latitude, right_bottom2.longitude);
201 //Add the polygon bounds
204 location_coords_s coord_list[poly_size];
206 coord_list[0].latitude = 10;
207 coord_list[0].longitude = 10;
208 coord_list[1].latitude = 20;
209 coord_list[1].longitude = 20;
210 coord_list[2].latitude = 30;
211 coord_list[2].longitude = 30;
213 location_bounds_h bounds_poly;
214 ret = location_bounds_create_polygon(coord_list, poly_size, &bounds_poly);
215 if (ret != LOCATION_BOUNDS_ERROR_NONE) {
216 printf("location_bounds_create_polygon() failed\n");
218 printf("Bounds(polygon) has been created successfully.\n");
220 ret = location_manager_add_boundary(manager, bounds_poly);
221 if (ret != LOCATIONS_ERROR_NONE) {
222 printf("Setting boundary failed\n");
224 printf("Boundary set\n");
226 ret = location_bounds_foreach_polygon_coords(bounds_poly, __poly_coords_cb, NULL);
227 if (ret != LOCATIONS_ERROR_NONE) {
228 printf("location_bounds_get_rect_coords() failed\n");
231 location_coords_s test_coords;
232 test_coords.latitude = 12;
233 test_coords.longitude = 12;
235 if (location_bounds_is_contains_coordinates(bounds_poly, test_coords))
236 printf("location_bounds_is_contains_coordinates() retrun TRUE \n");
238 printf("location_bounds_is_contains_coordinates() retrun FALSE \n");
240 //print current bounds
241 ret = location_manager_foreach_boundary(manager, __location_bounds_cb, (void *)manager);
242 if (ret != LOCATIONS_ERROR_NONE) {
243 printf("location_manager_foreach_boundary() failed\n");
248 void location_get_last_information_test()
251 double altitude, latitude, longitude;
252 int climb, direction, speed;
253 double horizontal, vertical;
254 location_accuracy_level_e level;
256 int num_of_inview, num_of_active;
258 ret = location_manager_get_last_position(manager, &altitude, &latitude, &longitude, ×tamp);
259 if (ret != LOCATIONS_ERROR_NONE) {
260 printf(" Fail : location_manager_get_last_position ---> %d \n", ret);
262 printf("[%ld] alt: %g, lat: %g, long: %g\n", timestamp, altitude, latitude, longitude);
265 ret = location_manager_get_last_velocity(manager, &climb, &direction, &speed, ×tamp);
266 if (ret != LOCATIONS_ERROR_NONE) {
267 printf(" Fail : location_manager_get_last_velocity ---> %d \n", ret);
269 printf("climb: %d, direction: %d, speed: %d\n", climb, direction, speed);
272 ret = location_manager_get_last_accuracy(manager, &level, &horizontal, &vertical);
273 if (ret != LOCATIONS_ERROR_NONE) {
274 printf(" Fail : location_manager_get_last_accuracy ---> %d \n", ret);
276 printf("Level : %d, horizontal: %g, vertical : %g\n", level, horizontal, vertical);
279 ret = gps_status_get_last_satellite(manager, &num_of_active, &num_of_inview, ×tamp);
280 if (ret != LOCATIONS_ERROR_NONE) {
281 printf(" Fail : gps_status_get_last_satellite_count_in_view ---> %d \n", ret);
283 printf("[%ld] Satellite number of active : %d, in view : %d\n", timestamp, num_of_active, num_of_inview);
286 ret = gps_status_foreach_last_satellites_in_view(manager, last_satellites_foreach_cb, NULL);
287 if (ret != LOCATIONS_ERROR_NONE) {
288 printf(" Fail : gps_status_foreach_last_satellites_in_view ---> %d \n", ret);
295 ret = location_manager_create(LOCATIONS_METHOD_GPS, &manager);
296 printf("create : %d\n", ret);
298 location_bounds_test();
299 location_get_last_information_test();
301 //set zone changed callback
302 ret = location_manager_set_zone_changed_cb(manager, zone_event_cb, (void *)manager);
303 printf("set zone callback : %d\n", ret);
305 ret = location_manager_set_service_state_changed_cb(manager, _state_change_cb, (void *)manager);
306 printf("set state callback : %d\n", ret);
308 ret = location_manager_start(manager);
309 printf("start : %d\n", ret);
313 static GMainLoop *g_mainloop = NULL;
315 static gboolean exit_program(gpointer data)
317 if (manager == NULL) {
318 printf("manager == NULL \n");
320 int ret = location_manager_stop(manager);
321 printf("stop : %d\n", ret);
322 ret = location_manager_destroy(manager);
323 printf("destroy : %d\n", ret);
325 g_main_loop_quit(g_mainloop);
326 printf("Quit g_main_loop\n");
330 int main(int argc, char **argv)
332 g_setenv("PKG_NAME", "com.samsung.location-test", 1);
333 g_mainloop = g_main_loop_new(NULL, 0);
335 g_timeout_add_seconds(90, exit_program, NULL);
336 g_main_loop_run(g_mainloop);