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