upload tizen1.0 source
[framework/api/location-manager.git] / test / location_test.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 <glib.h>
21 #include <locations.h>
22
23 location_manager_h manager;
24
25 void zone_event_cb(location_boundary_state_e state, double latitude, double longitude, double altitude, time_t timestamp,
26                    void *user_data)
27 {
28         if (state == LOCATIONS_BOUNDARY_IN) {
29                 printf("Entering zone\n");
30         } else                  // state == LOCATIONS_BOUNDARY_OUT
31         {
32                 printf("Leaving zone\n");
33         }
34
35         printf("Latitude: %lf, longitude: %lf, altitude: %lf\n", latitude, longitude, altitude);
36
37         printf("Time: %s\n", ctime(&timestamp));
38 }
39
40 static bool satellites_foreach_cb(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_in_use,
41                                   void *user_data)
42 {
43         printf("[Satellite information]  azimuth  : %d, elevation : %d, prn :%d, snr : %d, used: %d\n", azimuth, elevation, prn,
44                snr, is_in_use);
45         return true;
46 }
47
48 static bool last_satellites_foreach_cb(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_in_use,
49                                        void *user_data)
50 {
51         printf("[Last Satellite information]  azimuth  : %d, elevation : %d, prn :%d, snr : %d, used: %d\n", azimuth, elevation,
52                prn, snr, is_in_use);
53         return true;
54 }
55
56 static void _state_change_cb(location_service_state_e state, void *user_data)
57 {
58         fprintf(stderr, "--------------------------state change %d---------\n", state);
59         location_manager_h lm = (location_manager_h) user_data;
60         if (state == LOCATIONS_SERVICE_ENABLED) {
61                 int ret;
62                 double altitude;
63                 double latitude;
64                 double longitude;
65                 time_t timestamp;
66                 ret = location_manager_get_position(lm, &altitude, &latitude, &longitude, &timestamp);
67                 if (ret != LOCATIONS_ERROR_NONE) {
68                         printf(" Fail : location_manager_get_position ---> %d \n", ret);
69                 } else {
70                         printf("[%ld] alt: %g, lat %g, long %g\n", timestamp, altitude, latitude, longitude);
71                 }
72
73                 location_accuracy_level_e level;
74                 double horizontal;
75                 double vertical;
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);
79                 } else {
80                         printf("Level : %d, horizontal: %g, vertical %g\n", level, horizontal, vertical);
81                 }
82
83                 char *nmea;
84                 ret = gps_status_get_nmea(lm, &nmea);
85                 if (ret != LOCATIONS_ERROR_NONE) {
86                         printf(" Fail : gps_status_get_nmea ---> %d \n", ret);
87                 } else {
88                         printf("NMEA : %s\n", nmea);
89                         free(nmea);
90                 }
91
92                 int num_of_view, num_of_active;
93                 ret = gps_status_get_satellite(lm, &num_of_active, &num_of_view, &timestamp);
94                 if (ret != LOCATIONS_ERROR_NONE) {
95                         printf(" Fail : gps_status_get_satellite_count_in_view ---> %d \n", ret);
96                 } else {
97                         printf("[%ld] Satellite number of active : %d, in view : %d\n", timestamp, num_of_active, num_of_view);
98                 }
99
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);
103                 }
104         }
105 }
106
107 static bool __poly_coords_cb(location_coords_s coords, void *user_data)
108 {
109         printf("location_bounds_foreach_rect_coords(latitude : %lf, longitude: %lf) \n", coords.latitude, coords.longitude);
110         return TRUE;
111 }
112
113 static bool __location_bounds_cb(location_bounds_h bounds, void *user_data)
114 {
115         if (bounds == NULL)
116                 printf("bounds ==NULL\n");
117         else {
118                 location_bounds_type_e type;
119                 location_bounds_get_type(bounds, &type);
120                 if (type == LOCATION_BOUNDS_CIRCLE) {
121                         location_coords_s center;
122                         double radius;
123                         location_bounds_get_circle_coords(bounds, &center, &radius);
124                         printf("location_bounds_get_circle_coords(center : %lf, %lf, radius : %lf) \n", center.latitude,
125                                center.longitude, radius);
126
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);
135                 }
136         }
137         return TRUE;
138 }
139
140 void location_bounds_test()
141 {
142         int ret;
143         location_coords_s center;
144         center.latitude = 37.258;
145         center.longitude = 127.056;
146         double radius = 30;
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");
151         } else
152                 printf("Bounds(circle) has been created successfully.\n");
153
154         ret = location_manager_add_boundary(manager, bounds_circle);
155         if (ret != LOCATIONS_ERROR_NONE) {
156                 printf("Setting boundary failed\n");
157         } else
158                 printf("Boundary set\n");
159
160         location_coords_s center2;
161         double radius2;
162         ret = location_bounds_get_circle_coords(bounds_circle, &center2, &radius2);
163         if (ret != LOCATIONS_ERROR_NONE) {
164                 printf("location_bounds_get_circle_coords() failed\n");
165         } else
166                 printf("location_bounds_get_circle_coords(center : %lf, %lf, radius : %lf) \n", center2.latitude,
167                        center2.longitude, radius2);
168
169         //Add the rect bounds
170         location_coords_s left_top;
171         left_top.latitude = 30;
172         left_top.longitude = 30;
173
174         location_coords_s right_bottom;
175         right_bottom.latitude = 10;
176         right_bottom.longitude = 50;
177
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");
182         } else
183                 printf("Bounds(rect) has been created successfully.\n");
184
185         ret = location_manager_add_boundary(manager, bounds_rect);
186         if (ret != LOCATIONS_ERROR_NONE) {
187                 printf("Setting boundary failed\n");
188         } else
189                 printf("Boundary set\n");
190
191         location_coords_s left_top2;
192         location_coords_s right_bottom2;
193
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");
197         } else
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);
200
201         //Add the polygon bounds
202
203         int poly_size = 3;
204         location_coords_s coord_list[poly_size];
205
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;
212
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");
217         } else
218                 printf("Bounds(polygon) has been created successfully.\n");
219
220         ret = location_manager_add_boundary(manager, bounds_poly);
221         if (ret != LOCATIONS_ERROR_NONE) {
222                 printf("Setting boundary failed\n");
223         } else
224                 printf("Boundary set\n");
225
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");
229         }
230
231         location_coords_s test_coords;
232         test_coords.latitude = 12;
233         test_coords.longitude = 12;
234
235         if (location_bounds_is_contains_coordinates(bounds_poly, test_coords))
236                 printf("location_bounds_is_contains_coordinates() retrun TRUE \n");
237         else
238                 printf("location_bounds_is_contains_coordinates() retrun FALSE \n");
239
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");
244         }
245
246 }
247
248 void location_get_last_information_test()
249 {
250         int ret;
251         double altitude, latitude, longitude;
252         int climb, direction, speed;
253         double horizontal, vertical;
254         location_accuracy_level_e level;
255         time_t timestamp;
256         int num_of_inview, num_of_active;
257
258         ret = location_manager_get_last_position(manager, &altitude, &latitude, &longitude, &timestamp);
259         if (ret != LOCATIONS_ERROR_NONE) {
260                 printf(" Fail : location_manager_get_last_position ---> %d \n", ret);
261         } else {
262                 printf("[%ld] alt: %g, lat: %g, long: %g\n", timestamp, altitude, latitude, longitude);
263         }
264
265         ret = location_manager_get_last_velocity(manager, &climb, &direction, &speed, &timestamp);
266         if (ret != LOCATIONS_ERROR_NONE) {
267                 printf(" Fail : location_manager_get_last_velocity ---> %d \n", ret);
268         } else {
269                 printf("climb: %d, direction: %d, speed: %d\n", climb, direction, speed);
270         }
271
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);
275         } else {
276                 printf("Level : %d, horizontal: %g, vertical : %g\n", level, horizontal, vertical);
277         }
278
279         ret = gps_status_get_last_satellite(manager, &num_of_active, &num_of_inview, &timestamp);
280         if (ret != LOCATIONS_ERROR_NONE) {
281                 printf(" Fail : gps_status_get_last_satellite_count_in_view ---> %d \n", ret);
282         } else {
283                 printf("[%ld] Satellite number of active : %d, in view : %d\n", timestamp, num_of_active, num_of_inview);
284         }
285
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);
289         }
290 }
291
292 int location_test()
293 {
294         int ret;
295         ret = location_manager_create(LOCATIONS_METHOD_GPS, &manager);
296         printf("create : %d\n", ret);
297
298         location_bounds_test();
299         location_get_last_information_test();
300
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);
304
305         ret = location_manager_set_service_state_changed_cb(manager, _state_change_cb, (void *)manager);
306         printf("set state callback : %d\n", ret);
307
308         ret = location_manager_start(manager);
309         printf("start :  %d\n", ret);
310         return 1;
311 }
312
313 static GMainLoop *g_mainloop = NULL;
314
315 static gboolean exit_program(gpointer data)
316 {
317         if (manager == NULL) {
318                 printf("manager == NULL \n");
319         } else {
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);
324         }
325         g_main_loop_quit(g_mainloop);
326         printf("Quit g_main_loop\n");
327         return FALSE;
328 }
329
330 int main(int argc, char **argv)
331 {
332         g_setenv("PKG_NAME", "com.samsung.location-test", 1);
333         g_mainloop = g_main_loop_new(NULL, 0);
334         location_test();
335         g_timeout_add_seconds(90, exit_program, NULL);
336         g_main_loop_run(g_mainloop);
337         return 0;
338 }