500a73cdbd15eb2fe4f4c46ea14761988e356d9f
[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 static location_manager_h manager;
25 static GMainLoop *g_mainloop = NULL;
26 static char *menu;
27
28 static void location_cleanup();
29 static int location_test();
30
31 static gboolean exit_program(gpointer data)
32 {
33         g_main_loop_quit(g_mainloop);
34
35         return FALSE;
36 }
37
38 static gboolean wait_test()
39 {
40         location_cleanup();
41         location_test();
42
43         return FALSE;
44 }
45
46 #if 0
47 void zone_event_cb(location_boundary_state_e state, double latitude, double longitude, double altitude, time_t timestamp,
48                    void *user_data)
49 {
50         if (state == LOCATIONS_BOUNDARY_IN) {
51                 printf("Entering zone\n");
52         } else {                /* state == LOCATIONS_BOUNDARY_OUT */
53                 printf("Leaving zone\n");
54         }
55
56         printf("Latitude: %lf, longitude: %lf, altitude: %lf\n", latitude, longitude, altitude);
57
58         printf("Time: %s\n", ctime(&timestamp));
59 }
60
61 static bool last_satellites_foreach_cb(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_in_use,
62                                        void *user_data)
63 {
64         printf("[Last Satellite information]    azimuth : %d, elevation: %d, prn: %d, snr: %d, used: %d\n", azimuth, elevation,
65                prn, snr, is_in_use);
66         return true;
67 }
68 #endif
69
70 static bool satellites_foreach_cb(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_in_use,
71                                   void *user_data)
72 {
73         printf("[Satellite information] azimuth : %d, elevation: %d, prn: %d, snr: %d, used: %d\n", azimuth, elevation, prn,
74                snr, is_in_use);
75         return true;
76 }
77
78 static void _state_change_cb(location_service_state_e state, void *user_data)
79 {
80         fprintf(stderr, "--------------------------state change %d---------\n", state);
81         location_manager_h lm = (location_manager_h) user_data;
82         if (state == LOCATIONS_SERVICE_ENABLED) {
83                 int ret;
84                 double altitude;
85                 double latitude;
86                 double longitude;
87                 time_t timestamp;
88                 ret = location_manager_get_position(lm, &altitude, &latitude, &longitude, &timestamp);
89                 if (ret != LOCATIONS_ERROR_NONE) {
90                         printf(" Fail: location_manager_get_position ---> %d \n", ret);
91                 } else {
92                         printf("[%ld] alt: %g, lat %g, long %g\n", timestamp, altitude, latitude, longitude);
93                 }
94
95                 location_accuracy_level_e level;
96                 double horizontal;
97                 double vertical;
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);
101                 } else {
102                         printf("Level: %d, horizontal: %g, vertical %g\n", level, horizontal, vertical);
103                 }
104
105                 char *nmea;
106                 ret = gps_status_get_nmea(lm, &nmea);
107                 if (ret != LOCATIONS_ERROR_NONE) {
108                         printf(" Fail: gps_status_get_nmea ---> %d \n", ret);
109                 } else {
110                         printf("NMEA: %s\n", nmea);
111                         free(nmea);
112                 }
113
114                 int num_of_view = 0, num_of_active = 0;
115                 ret = gps_status_get_satellite(lm, &num_of_active, &num_of_view, &timestamp);
116                 if (ret != LOCATIONS_ERROR_NONE) {
117                         printf(" Fail: gps_status_get_satellite_count_in_view ---> %d \n", ret);
118                 } else {
119                         printf("[%ld] Satellite number of active: %d, in view: %d\n", timestamp, num_of_active, num_of_view);
120                 }
121
122                 if (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);
126                         }
127                 }
128                 g_timeout_add_seconds(1, wait_test, NULL);
129         }
130 }
131
132 #if 0
133 static bool __poly_coords_cb(location_coords_s coords, void *user_data)
134 {
135         printf("location_bounds_foreach_rect_coords(latitude: %lf, longitude: %lf) \n", coords.latitude, coords.longitude);
136         return TRUE;
137 }
138
139 static bool __location_bounds_cb(location_bounds_h bounds, void *user_data)
140 {
141         if (bounds == NULL)
142                 printf("bounds ==NULL\n");
143         else {
144                 location_bounds_type_e type;
145                 location_bounds_get_type(bounds, &type);
146                 if (type == LOCATION_BOUNDS_CIRCLE) {
147                         location_coords_s center;
148                         double radius;
149                         location_bounds_get_circle_coords(bounds, &center, &radius);
150                         printf("location_bounds_get_circle_coords(center: %lf, %lf, radius: %lf) \n", center.latitude,
151                                center.longitude, radius);
152
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);
161                 }
162         }
163         return TRUE;
164 }
165
166 void location_bounds_test()
167 {
168         int ret;
169         location_coords_s center;
170         center.latitude = 37.258;
171         center.longitude = 127.056;
172         double radius = 30;
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");
177         } else
178                 printf("Bounds(circle) has been created successfully.\n");
179
180         ret = location_manager_add_boundary(manager, bounds_circle);
181         if (ret != LOCATIONS_ERROR_NONE) {
182                 printf("Setting boundary failed\n");
183         } else
184                 printf("Boundary set\n");
185
186         location_coords_s center2;
187         double radius2;
188         ret = location_bounds_get_circle_coords(bounds_circle, &center2, &radius2);
189         if (ret != LOCATIONS_ERROR_NONE) {
190                 printf("location_bounds_get_circle_coords() failed\n");
191         } else
192                 printf("location_bounds_get_circle_coords(center: %lf, %lf, radius: %lf) \n", center2.latitude,
193                        center2.longitude, radius2);
194
195         /*Add the rect bounds */
196         location_coords_s left_top;
197         left_top.latitude = 30;
198         left_top.longitude = 30;
199
200         location_coords_s right_bottom;
201         right_bottom.latitude = 10;
202         right_bottom.longitude = 50;
203
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");
208         } else
209                 printf("Bounds(rect) has been created successfully.\n");
210
211         ret = location_manager_add_boundary(manager, bounds_rect);
212         if (ret != LOCATIONS_ERROR_NONE) {
213                 printf("Setting boundary failed\n");
214         } else
215                 printf("Boundary set\n");
216
217         location_coords_s left_top2;
218         location_coords_s right_bottom2;
219
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");
223         } else
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);
226
227         /*Add the polygon bounds */
228
229         int poly_size = 3;
230         location_coords_s coord_list[poly_size];
231
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;
238
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");
243         } else
244                 printf("Bounds(polygon) has been created successfully.\n");
245
246         ret = location_manager_add_boundary(manager, bounds_poly);
247         if (ret != LOCATIONS_ERROR_NONE) {
248                 printf("Setting boundary failed\n");
249         } else
250                 printf("Boundary set\n");
251
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");
255         }
256
257         location_coords_s test_coords;
258         test_coords.latitude = 12;
259         test_coords.longitude = 12;
260
261         if (location_bounds_contains_coordinates(bounds_poly, test_coords))
262                 printf("location_bounds_contains_coordinates() retrun TRUE \n");
263         else
264                 printf("location_bounds_contains_coordinates() retrun FALSE \n");
265
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");
270         }
271
272 }
273
274 void location_get_last_information_test()
275 {
276         int ret;
277         double altitude, latitude, longitude;
278         double climb, direction, speed;
279         double horizontal, vertical;
280         location_accuracy_level_e level;
281         time_t timestamp;
282         int num_of_inview, num_of_active;
283
284         ret = location_manager_get_last_position(manager, &altitude, &latitude, &longitude, &timestamp);
285         if (ret != LOCATIONS_ERROR_NONE) {
286                 printf(" Fail: location_manager_get_last_position ---> %d \n", ret);
287         } else {
288                 printf("[%ld] alt: %g, lat: %g, long: %g\n", timestamp, altitude, latitude, longitude);
289         }
290
291         ret = location_manager_get_last_velocity(manager, &climb, &direction, &speed, &timestamp);
292         if (ret != LOCATIONS_ERROR_NONE) {
293                 printf(" Fail: location_manager_get_last_velocity ---> %d \n", ret);
294         } else {
295                 printf("climb: %f, direction: %f, speed: %f\n", climb, direction, speed);
296         }
297
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);
301         } else {
302                 printf("Level: %d, horizontal: %g, vertical: %g\n", level, horizontal, vertical);
303         }
304
305         ret = gps_status_get_last_satellite(manager, &num_of_active, &num_of_inview, &timestamp);
306         if (ret != LOCATIONS_ERROR_NONE) {
307                 printf(" Fail: gps_status_get_last_satellite_count_in_view ---> %d \n", ret);
308         } else {
309                 printf("[%ld] Satellite number of active: %d, in view: %d\n", timestamp, num_of_active, num_of_inview);
310         }
311
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);
315         }
316 }
317 #endif
318
319 void location_cb(int error, double latitude, double longitude, double altitude, time_t timestamp, double speed, double climb, double direction, void *user_data)
320 {
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);
324
325         g_timeout_add_seconds(1, wait_test, NULL);
326 }
327
328 static void __setting_cb(location_method_e method, bool enable, void *user_data)
329 {
330         printf("method[%d], enable[%d]\n", method, enable);
331 }
332
333 static void select_menu()
334 {
335         char buf[256];
336         int len = 0;
337         char *str = NULL;
338
339         str = fgets(buf, 255, stdin);
340         if (NULL == str) {
341                 printf("fgets return NULL. \n");
342         }
343         len = g_utf8_strlen(buf, -1);
344         buf[len - 1] = '\0';
345
346         g_free(menu);
347         menu = g_strdup(buf);
348 }
349
350 static void print_menu()
351 {
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: ");
360 }
361
362 static int location_test()
363 {
364         int ret = LOCATIONS_ERROR_NONE;
365
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);
370
371         location_manager_is_enabled_method(LOCATIONS_METHOD_GPS, &is_enabled);
372         printf("gps: %d, ", is_enabled);
373
374         location_manager_is_enabled_method(LOCATIONS_METHOD_WPS, &is_enabled);
375         printf("wps: %d\n", is_enabled);
376
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);
379
380         print_menu();
381         select_menu();
382
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);
408                 return 0;
409         } else {
410                 printf("Exit!!! Input: %s\n", menu);
411                 g_timeout_add_seconds(0.1, exit_program, NULL);
412                 return 0;
413         }
414
415         if (ret != LOCATIONS_ERROR_NONE) {
416                 printf("Test Failed!!! [%d]\n", ret);
417                 g_timeout_add_seconds(0.1, exit_program, NULL);
418                 return 0;
419         }
420         ret = location_manager_set_service_state_changed_cb(manager, _state_change_cb, (void *)manager);
421         printf("set state callback: %d\n", ret);
422
423         /* location_bounds_test(); */
424         /* set zone changed callback */
425         /*
426         ret = location_manager_set_zone_changed_cb(manager, zone_event_cb, (void *)manager);
427         printf("set zone callback: %d\n", ret);
428         */
429         return 0;
430 }
431
432 static void location_cleanup()
433 {
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);
439                 manager = NULL;
440         }
441         location_manager_unset_setting_changed_cb(LOCATIONS_METHOD_GPS);
442         location_manager_unset_setting_changed_cb(LOCATIONS_METHOD_WPS);
443 }
444
445 int main(int argc, char **argv)
446 {
447         g_mainloop = g_main_loop_new(NULL, 0);
448         location_test();
449         g_main_loop_run(g_mainloop);
450         location_cleanup();
451         return 0;
452 }