Fix a crash after changes that velocity is updated before position
[framework/location/libslp-location.git] / tests / location-api-test.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7  *          Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #include <glib.h>
23 #include <string.h>
24 #include <glib/gprintf.h>
25 #include <location.h>
26 #include "location-api-test-util.h"
27
28 #define STR_MAX 128
29 LocationObject* location_obj = NULL;
30 static GMainLoop *g_mainloop = NULL;
31
32 #define LOCATION_API_TEST_JSON_FILE "/opt/data/location-api-test.json"
33
34 static gpointer GmainThread(gpointer data)
35 {
36         g_mainloop = g_main_loop_new (NULL, FALSE);
37         g_printf("\n...Entering GMain Loop to Receive Notifications....\n");
38         g_main_loop_run (g_mainloop);
39         g_main_loop_unref (g_mainloop);
40         g_mainloop = NULL;
41         return NULL;
42 }
43
44
45 static void GetLocationError(char str[STR_MAX], int ret)
46 {
47         switch(ret)
48         {
49                 case LOCATION_ERROR_NONE:
50                         g_utf8_strncpy(str, "LOCATION_ERROR_NONE", STR_MAX);
51                         break;
52                 case LOCATION_ERROR_NOT_ALLOWED:
53                         g_utf8_strncpy(str, "LOCATION_ERROR_NOT_ALLOWED", STR_MAX);
54                         break;
55                 case LOCATION_ERROR_NOT_AVAILABLE:
56                         g_utf8_strncpy(str, "LOCATION_ERROR_NOT_AVAILABLE", STR_MAX);
57                         break;
58                 case LOCATION_ERROR_NETWORK_FAILED:
59                         g_utf8_strncpy(str, "LOCATION_ERROR_NETWORK_FAILED", STR_MAX);
60                         break;
61                 case LOCATION_ERROR_NETWORK_NOT_CONNECTED:
62                         g_utf8_strncpy(str, "LOCATION_ERROR_NETWORK_NOT_CONNECTED", STR_MAX);
63                         break;
64                 case LOCATION_ERROR_CONFIGURATION:
65                         g_utf8_strncpy(str, "LOCATION_ERROR_CONFIGURATION", STR_MAX);
66                         break;
67                 case LOCATION_ERROR_PARAMETER:
68                         g_utf8_strncpy(str, "LOCATION_ERROR_PARAMETER", STR_MAX);
69                         break;
70                 case LOCATION_ERROR_UNKNOWN:
71                         g_utf8_strncpy(str, "LOCATION_ERROR_UNKNOWN", STR_MAX);
72                         break;
73                 default:
74                         g_utf8_strncpy(str, "Error: undefined error code", STR_MAX);
75         }
76 }
77
78 static void GetStatus(char str[STR_MAX], LocationStatus acc_level)
79 {
80         switch(acc_level)
81         {
82                 case LOCATION_STATUS_NO_FIX:
83                         g_utf8_strncpy(str, "LOCATION_STATUS_NO_FIX", STR_MAX);
84                         break;
85                 case LOCATION_STATUS_2D_FIX:
86                         g_utf8_strncpy(str, "LOCATION_STATUS_2D_FIX", STR_MAX);
87                         break;
88                 case LOCATION_STATUS_3D_FIX:
89                         g_utf8_strncpy(str, "LOCATION_STATUS_3D_FIX", STR_MAX);
90                         break;
91                 default:
92                         g_utf8_strncpy(str, "Error: undefined status code", STR_MAX);
93         }
94 }
95
96 static void GetMethod(char str[STR_MAX], LocationMethod method)
97 {
98         switch(method)
99         {
100                 case LOCATION_METHOD_HYBRID:
101                         g_utf8_strncpy(str, "LOCATION_METHOD_HYBRID", STR_MAX);
102                         break;
103                 case LOCATION_METHOD_GPS:
104                         g_utf8_strncpy(str, "LOCATION_METHOD_GPS", STR_MAX);
105                         break;
106                 case LOCATION_METHOD_CPS:
107                         g_utf8_strncpy(str, "LOCATION_METHOD_CPS", STR_MAX);
108                         break;
109                 case LOCATION_METHOD_WPS:
110                         g_utf8_strncpy(str, "LOCATION_METHOD_WPS", STR_MAX);
111                         break;
112                 default:
113                         g_utf8_strncpy(str, "Error: undefined method", STR_MAX);
114         }
115 }
116
117 static void GetAccuracyLevel(char str[STR_MAX], LocationAccuracyLevel acc_level)
118 {
119         switch(acc_level)
120         {
121                 case LOCATION_ACCURACY_LEVEL_NONE:
122                         g_utf8_strncpy(str, "LOCATION_ACCURACY_LEVEL_NONE", STR_MAX);
123                         break;
124                 case LOCATION_ACCURACY_LEVEL_COUNTRY:
125                         g_utf8_strncpy(str, "LOCATION_ACCURACY_LEVEL_COUNTRY", STR_MAX);
126                         break;
127                 case LOCATION_ACCURACY_LEVEL_REGION:
128                         g_utf8_strncpy(str, "LOCATION_ACCURACY_LEVEL_REGION", STR_MAX);
129                         break;
130                 case LOCATION_ACCURACY_LEVEL_LOCALITY:
131                         g_utf8_strncpy(str, "LOCATION_ACCURACY_LEVEL_LOCALITY", STR_MAX);
132                         break;
133                 case LOCATION_ACCURACY_LEVEL_POSTALCODE:
134                         g_utf8_strncpy(str, "LOCATION_ACCURACY_LEVEL_POSTALCODE", STR_MAX);
135                         break;
136                 case LOCATION_ACCURACY_LEVEL_STREET:
137                         g_utf8_strncpy(str, "LOCATION_ACCURACY_LEVEL_STREET", STR_MAX);
138                         break;
139                 case LOCATION_ACCURACY_LEVEL_DETAILED:
140                         g_utf8_strncpy(str, "LOCATION_ACCURACY_LEVEL_DETAILED", STR_MAX);
141                         break;
142                 default:
143                         g_utf8_strncpy(str, "Error: undefined accuracy level", STR_MAX);
144         }
145 }
146
147
148 static void SelectOpt(char* buf)
149 {
150         int iLen = 0;
151         fgets(buf, 255, stdin);
152         iLen = g_utf8_strlen(buf, -1);
153         buf[iLen-1] = '\0';
154 }
155
156 static int PromptInt()
157 {
158         char buf[255];
159         int ret;
160         fgets(buf, 255, stdin);
161         buf[strlen(buf)-1]='\0';
162         ret = g_ascii_strtoll(buf, NULL, 10);
163         return ret;
164 }
165
166 gulong g_sig_enable = 0;
167 gulong g_sig_disable = 0;
168 gulong g_sig_update = 0;
169 gulong g_sig_zonein = 0;
170 gulong g_sig_zoneout = 0;
171
172 static void PrintPolygonPosition(gpointer data, gpointer user_data)
173 {
174         LocationPosition *position = (LocationPosition*) data;
175         g_printf("[%lf %lf %lf] ", position->latitude, position->longitude, position->altitude);
176 }
177
178 static void PrintBoundary(LocationBoundary *boundary, void *user_data)
179 {
180         if(boundary == NULL) return;
181         if (boundary->type == LOCATION_BOUNDARY_CIRCLE) {
182                 g_printf("\n\tCIRCLE: center[%lf %lf %lf] radius[%lf]",
183                                 boundary->circle.center->latitude, boundary->circle.center->longitude, boundary->circle.center->altitude, boundary->circle.radius);
184         } else if (boundary->type == LOCATION_BOUNDARY_RECT) {
185                 g_printf("\n\tRECT: left_top[%lf %lf %lf] right_bottom[%lf %lf %lf]",
186                                 boundary->rect.left_top->latitude, boundary->rect.left_top->longitude, boundary->rect.left_top->altitude,
187                                 boundary->rect.right_bottom->latitude, boundary->rect.right_bottom->longitude, boundary->rect.right_bottom->altitude);
188         } else if (boundary->type == LOCATION_BOUNDARY_POLYGON) {
189                 g_printf("\n\tPOLYGON: ");
190
191                 GList *list = boundary->polygon.position_list;
192
193                 g_list_foreach(list, PrintPolygonPosition, NULL);
194         }
195 }
196
197 static void RemoveBoundary(LocationBoundary *boundary, void *user_data)
198 {
199         LocationObject * obj = (LocationObject *)user_data;
200         if(obj == NULL || boundary == NULL) return;
201
202         location_boundary_remove(obj, boundary);
203 }
204
205 static void PrintProperty (LocationObject* loc)
206 {
207         if (!loc) return;
208         LocationMethod method = LOCATION_METHOD_NONE;
209         LocationPosition *pos = NULL;
210         LocationAccuracy *acc = NULL;
211         guint pos_interval = 0;
212         guint vel_interval = 0;
213         guint sat_interval = 0;
214         gchar method_str[STR_MAX] = {0, };
215         gchar status_str[STR_MAX] = {0, };
216
217         gchar* devname = NULL;
218
219         g_object_get(loc, "method", &method, NULL);
220         GetMethod(method_str, method);
221         g_printf("method[%s] ", method_str);
222
223         if (LOCATION_METHOD_GPS == method) {
224                 g_object_get(loc, "dev-name", &devname, NULL);
225                 if (devname) {
226                         g_printf("dev-name[%s] ", devname);
227                         g_free(devname);
228                 }
229         }
230
231         int ret = location_get_last_position (loc, &pos, &acc);
232         if (ret == LOCATION_ERROR_NONE) {
233                 GetStatus(status_str, pos->status);
234                 g_printf("\nLast position [time(%d) lat(%f) long(%f) alt(%f) status(%s)]",
235                                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude, status_str);
236                 location_position_free (pos);
237                 location_accuracy_free (acc);
238         }
239         
240         if (method == LOCATION_METHOD_HYBRID || method == LOCATION_METHOD_GPS) {
241                 g_object_get(loc, "pos-interval", &pos_interval, NULL);
242                 g_object_get(loc, "vel-interval", &vel_interval, NULL);
243                 g_object_get(loc, "sat-interval", &sat_interval, NULL);
244         }
245         else if (method == LOCATION_METHOD_WPS) {
246                 g_object_get(loc, "pos-interval", &pos_interval, NULL);
247                 g_object_get(loc, "vel-interval", &vel_interval, NULL);
248         }
249         else if (method == LOCATION_METHOD_CPS) {
250                 g_object_get(loc, "pos-interval", &pos_interval, NULL);
251         }
252         g_printf("Position interval : [%u], Velocity interval [%u], Satellite interval [%u]\n", pos_interval, vel_interval, sat_interval);
253
254         g_printf("\nSignals: ");
255         if (g_sig_enable)  g_printf("[service-enabled], ");
256         if (g_sig_disable) g_printf("[service-disabled], ");
257         if (g_sig_update)  g_printf("[service-updated], ");
258         if (g_sig_zonein)  g_printf("[zone-in], ");
259         if (g_sig_zoneout) g_printf("[zone-out]");
260 }
261
262 static void cb_service_enabled (GObject *self,  guint status, gpointer userdata)
263 {
264         g_printf("cb_service_enabled: status(%d) userdata(0x%x)", status, (unsigned int)userdata);
265
266         LocationObject *loc = (LocationObject*)userdata;
267         LocationAccuracy *acc = NULL;
268         LocationPosition *pos = NULL;
269         LocationVelocity *vel = NULL;
270         LocationMethod method;
271
272         g_object_get(loc, "method", &method, NULL);
273         g_printf("Get property>> method:%d", method);
274
275         if (LOCATION_ERROR_NONE == location_get_position (loc, &pos, &acc)) {
276                 g_printf ("SYNC>> Current position> time: %d, lat: %f, long: %f, alt: %f, status: %d\n",
277                         pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
278                 g_printf ("\tAccuracy level %d (%.0f meters %.0f meters)\n",
279                         acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
280                 location_position_free(pos);
281                 location_accuracy_free(acc);
282         } else g_warning ("SYNC>> Current position> failed");
283         if (LOCATION_ERROR_NONE == location_get_velocity (loc, &vel, &acc)) {
284                 g_printf ("SYNC>> Current velocity> time: %d, speed: %f, direction:%f, climb:%f\n",
285                         vel->timestamp, vel->speed, vel->direction, vel->climb);
286                 g_printf ("\tAccuracy level %d (%.0f meters %.0f meters)\n",
287                         acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
288                 location_velocity_free(vel);
289                 location_accuracy_free(acc);
290         } else g_warning ("SYNC>> Current velocity> failed\n");
291 }
292
293 static void
294 cb_service_disabled (GObject *self,
295         guint status,
296         gpointer userdata)
297 {
298         g_printf("cb_service_disabled: status(%d) userdata(0x%x)\n", status, (unsigned int)userdata);
299 }
300
301 static void
302 cb_zone_in (GObject *self,
303         guint type,
304         gpointer position,
305         gpointer accuracy)
306 {
307         g_printf("cb_zone_in: type(%d)\n", type);
308         LocationPosition *pos = (LocationPosition*) position;
309         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
310
311         g_printf ("ASYNC>> ZoneIn> Current position: time: %d, lat: %f, long: %f, alt: %f\n",
312                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude);
313         g_printf ("\tAccuracy level %d (%.0f meters %.0f meters)\n",
314                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
315 }
316
317 static void
318 cb_zone_out (GObject *self,
319         guint type,
320         gpointer position,
321         gpointer accuracy)
322 {
323         g_printf("cb_zone_out: type(%d)\n", type);
324         LocationPosition *pos = (LocationPosition*) position;
325         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
326
327         g_printf ("ASYNC>> ZoneOut> Current position: time: %d, lat: %f, long: %f, alt: %f\n",
328                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude);
329         g_printf ("\tAccuracy level %d (%.0f meters %.0f meters)\n",
330                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
331 }
332
333 static void
334 cb_service_updated (GObject *self,
335         guint type,
336         gpointer data,
337         gpointer accuracy,
338         gpointer userdata)
339 {
340         g_printf("cb_service_updated: type(%d) userdata(0x%x)", type, (unsigned int)userdata);
341
342         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
343         switch (type) {
344         case POSITION_UPDATED: {
345                 LocationPosition *pos = (LocationPosition*) data;
346                 g_printf ("ASYNC>> Current position> time: %d, lat: %f, long: %f, alt: %f, status: %d\n",
347                                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
348                 g_printf ("\tAccuracy level %d (%.0f meters %.0f meters)\n",
349                                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
350         }
351                 break;
352         case VELOCITY_UPDATED: {
353                 LocationVelocity *vel = (LocationVelocity*) data;
354                 g_printf ("ASYNC>> Current velocity> time: %d, speed: %f, direction:%f, climb:%f\n",
355                         vel->timestamp, vel->speed, vel->direction, vel->climb);
356                 g_printf ("\tAccuracy level %d (%.0f meters %.0f meters)\n",
357                         acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
358         }
359                 break;
360         case SATELLITE_UPDATED: {
361                 int idx = 0;
362                 guint prn;
363                 gboolean used;
364                 guint elevation;
365                 guint azimuth;
366                 gint snr;
367
368                 LocationSatellite *sat = (LocationSatellite *)data;
369                 g_printf ("SYNC>> Current Satellite> time: %d, satellite in view = %d, satellite in used = %d\n", sat->timestamp, sat->num_of_sat_inview, sat->num_of_sat_used);
370                 g_printf ("\tinview satellite information = ");
371                 for (idx=0; idx<sat->num_of_sat_inview; idx++) {
372                         location_satellite_get_satellite_details(sat, idx, &prn, &used, &elevation, &azimuth, &snr);
373                         g_printf ("\t\t[%02d] used: %d, prn: %d, elevation: %d, azimuth: %d, snr: %d\n", idx, used, prn, elevation, azimuth, snr);
374                 }
375         }
376                 break;
377         default:
378                 g_warning ("ASYNC>> Undefined update type");
379                 break;
380         }
381 }
382
383 static void print_menu()
384 {
385         g_printf("\n================================= Location API Test =================================\n");
386         g_printf("q.   Exit\n");
387         g_printf("1.   location_init\n");
388         g_printf("2.   location_new\n");
389         g_printf("3.   location_free\n");
390         g_printf("4.   location_start\n");
391         g_printf("5.   location_stop\n");
392         g_printf("6.   location_get_position\n");
393         g_printf("6a.  location_get_last_position\n");
394         g_printf("7.   location_get_velocity\n");
395         g_printf("7a.  location_get_last_velocity\n");
396         g_printf("8.   location_get_satellite\n");
397         g_printf("8a.  location_get_last_satellite\n");
398         g_printf("9.   location_get_distance\n");
399         g_printf("10.  location_is_supported_method\n");
400         g_printf("11.  location_is_enabled_gps\n");
401         g_printf("99.  location_send_command\n");
402         g_printf("a?.  signals:(1)'service-enabled',(2)'service-disabled',(3)'service-updated',(4)'zone-in',(5)'zone-out'\n");
403         g_printf("b?.  disconnect signals:(1)'service-enabled',(2)'service-disabled',(3)'service-updated',(4)'zone-in',(5)'zone-out'\n");
404         g_printf("c?. (1)Set boundary, (2)Get boundary, (3) Remove boundary, (4) Remove all boundaries, (5)Set device name, \n");
405         g_printf("    (6)Set position interval (7) Set velocity interval (8) Set satellite interval\n");
406         g_printf("==================================== Property ====================================\n");
407         PrintProperty(location_obj);
408         g_printf("\n==================================================================================\n");
409 }
410
411 int main(int argc, char** argv)
412 {
413         char strOpt[255];
414         int ret;
415         char str[STR_MAX];
416         GError *gerr = NULL;
417
418         // If application is executed by AUL, this is not needed.
419         g_setenv("PKG_NAME", "com.samsung.location-api-test", 1);
420
421         g_type_init();
422
423 #if !GLIB_CHECK_VERSION (2, 31, 0)
424         if( !g_thread_supported() )
425         {
426                 g_thread_init(NULL);
427         }
428 #endif
429
430         GThread *g_main;
431         g_main = g_thread_create(GmainThread, NULL, TRUE, &gerr);
432         if (!g_main) {
433                 g_printf("Error create gmain thread: Err domain[%d] Err code[%d] Err msg[%s]",
434                         gerr->domain, gerr->code, gerr->message);
435                 g_error_free(gerr);
436                 return 0;
437         }
438
439         json_util_init(LOCATION_API_TEST_JSON_FILE);
440         g_printf("--- Start LBS Test App ---\n");
441         while(1)
442         {
443                 print_menu();
444                 g_printf("Select option: ");
445                 SelectOpt(strOpt);
446                 g_printf("======================================================================================\n");
447                 if (0 == g_strcmp0 ("x", strOpt)) {
448
449                 }
450                 else if (0 == g_strcmp0("1",strOpt)) {
451                         ret = location_init();
452                         GetLocationError(str, ret);
453                         g_printf("location_init: returned value [%s]\n", str);
454                 } else if (0 == g_strcmp0("2",strOpt)) {
455                         if (location_obj) {
456                                 g_printf("Location object already existed: [0x%x]", (unsigned int)location_obj);
457                                 continue;
458                         }
459                         g_printf("LOCATION_METHOD_HYBRID[0] LOCATION_METHOD_GPS[1] LOCATION_METHOD_WPS[2] LOCATION_METHOD_CPS[3]\n");
460                         g_printf("Select Location Method: ");
461                         LocationMethod method = PromptInt();
462                         location_obj = location_new(method);
463                         if(location_obj) g_printf("Success\n");
464                         else             g_printf("Failed\n");
465                 } else if (0 == g_strcmp0("3",strOpt)) {
466                         ret = location_free (location_obj);
467                         location_obj = NULL;
468
469                         g_sig_enable = 0;
470                         g_sig_disable = 0;
471                         g_sig_update = 0;
472                         g_sig_zonein = 0;
473                         g_sig_zoneout = 0;
474                         GetLocationError(str, ret);
475                         g_printf("location_free: returned value [%s]\n", str);
476                 } else if (0 == g_strcmp0("4",strOpt)) {
477                         ret = location_start(location_obj);
478                         GetLocationError(str, ret);
479                         g_printf("location_start: returned value [%s]\n", str);
480                 } else if (0 == g_strcmp0("5",strOpt)) {
481                         ret = location_stop(location_obj);
482                         GetLocationError(str, ret);
483                         g_printf("location_stop: returned value [%s]\n", str);
484                 } else if (0 == g_strcmp0("6",strOpt)) {
485                         LocationPosition *pos = NULL;
486                         LocationAccuracy *acc = NULL;
487                         ret = location_get_position(location_obj, &pos, &acc);
488                         GetLocationError(str, ret);
489                         g_printf("location_get_position: returned value [%s]\n", str);
490                         if (ret == LOCATION_ERROR_NONE) {
491                                 g_printf("time: [%d], latitude: [%f], longitude: [%f], altitude: [%f], status: [%d]\n", pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
492                                 GetAccuracyLevel(str, acc->level);
493                                 g_printf("level: [%s], horizontal_accuracy: [%f], vertical_accuracy: [%f]\n", str, acc->horizontal_accuracy, acc->vertical_accuracy);
494                         }
495                         if(pos) location_position_free(pos);
496                         if(acc) location_accuracy_free(acc);
497                 } else if (0 == g_strcmp0("6a",strOpt)) {
498                         LocationPosition *last_pos;
499                         LocationAccuracy *last_acc;
500                         int ret = 0;
501
502                         ret = location_get_last_position (location_obj, &last_pos, &last_acc);
503                         GetLocationError(str, ret);
504                         if (ret == LOCATION_ERROR_NONE) {
505                                 g_printf ("SYNC>> Last position> time: %d, lat: %f, long: %f, alt: %f, status: %d",
506                                         last_pos->timestamp, last_pos->latitude, last_pos->longitude, last_pos->altitude, last_pos->status);
507                                 g_printf ("\tAccuracy level %d (%.0f meters %.0f meters)",
508                                         last_acc->level, last_acc->horizontal_accuracy, last_acc->vertical_accuracy);
509                                 location_position_free(last_pos);
510                                 location_accuracy_free(last_acc);
511                         } else g_warning ("SYNC>> Last position> failed. Error[%s]",str);
512
513                 }else if(0 == g_strcmp0("7",strOpt) ){
514                         LocationVelocity *vel = NULL;
515                         LocationAccuracy *acc = NULL;
516                         ret = location_get_velocity(location_obj, &vel, &acc);
517                         g_printf("location_get_velocity: returned value [%s]\n", str);
518                         if (ret == LOCATION_ERROR_NONE) {
519                                 g_printf("time: [%d], speed: [%f], direction: [%f], climb: [%f]\n", vel->timestamp, vel->speed, vel->direction, vel->climb);
520                                 GetAccuracyLevel(str, acc->level);
521                                 g_printf("level: [%s], horizontal_accuracy: [%f], vertical_accuracy: [%f]\n", str, acc->horizontal_accuracy, acc->vertical_accuracy);
522                         }
523                         else {
524                                 GetLocationError(str, ret);
525                                 g_warning ("SYNC>> velocity> failed. Error[%s]", str);
526                         }
527                         if(vel) location_velocity_free(vel);
528                         if(acc) location_accuracy_free(acc);
529                 }else if(0 == g_strcmp0("7a",strOpt) ){
530                         LocationVelocity *last_vel = NULL;
531                         LocationAccuracy *last_acc = NULL;
532                         ret = location_get_last_velocity (location_obj, &last_vel, &last_acc);
533                         GetLocationError(str, ret);
534                         if (ret == LOCATION_ERROR_NONE) {
535                                 g_printf ("SYNC>> Last velocity> time: %d, speed: %f, direction:%f, climb:%f",
536                                         last_vel->timestamp, last_vel->speed, last_vel->direction, last_vel->climb);
537                                 g_printf ("\tAccuracy level %d (%.0f meters %.0f meters)",
538                                         last_acc->level, last_acc->horizontal_accuracy, last_acc->vertical_accuracy);
539                                 location_velocity_free(last_vel);
540                                 location_accuracy_free(last_acc);
541                         } else g_warning ("SYNC>> Last velocity> failed. Error[%s]", str);
542                 }else if(0 == g_strcmp0("8",strOpt) ){
543                         int ret = 0, idx = 0;
544                         LocationSatellite *sat = NULL;
545                         guint prn;
546                         gboolean used;
547                         guint elevation;
548                         guint azimuth;
549                         gint snr;
550
551                         ret =  location_get_satellite (location_obj, &sat);
552                         GetLocationError(str, ret);
553                         if (ret == LOCATION_ERROR_NONE) {
554                                 g_printf ("SYNC>> Current Sattelite> time = %d, satellite in view = %d, satellite in used = %d", sat->timestamp, sat->num_of_sat_inview, sat->num_of_sat_used);
555                                 g_printf ("\tinview satellite information = ");
556                                 for (idx=0; idx<sat->num_of_sat_inview; idx++) {
557                                         location_satellite_get_satellite_details(sat, idx, &prn, &used, &elevation, &azimuth, &snr);
558                                         g_printf ("\t\t[%02d] used: %d, prn: %d, elevation: %d, azimuth: %d, snr: %d", idx, used, prn, elevation, azimuth, snr);
559                                 }
560                                 location_satellite_free (sat);
561                         } else g_warning ("SYNC>> Current satellite> failed. Error[%s]", str);
562                 }else if(0 == g_strcmp0("8a",strOpt) ){
563                         int ret = 0, idx = 0;
564                         LocationSatellite *last_sat = NULL;
565                         guint prn;
566                         gboolean used;
567                         guint elevation;
568                         guint azimuth;
569                         gint snr;
570
571                         ret = location_get_last_satellite (location_obj, &last_sat);
572                         GetLocationError(str, ret);
573                         if (ret == LOCATION_ERROR_NONE) {
574                                 g_printf ("SYNC>> Last Sattelite> time = %d, satellite in view = %d, satellite in used = %d", last_sat->timestamp, last_sat->num_of_sat_inview, last_sat->num_of_sat_used);
575                                 g_printf ("\tinview satellite information = ");
576                                 for (idx=0; idx<last_sat->num_of_sat_inview; idx++) {
577                                         location_satellite_get_satellite_details(last_sat, idx, &prn, &used, &elevation, &azimuth, &snr);
578                                         g_printf ("\t\t[%02d] used: %d, prn: %d, elevation: %d, azimuth: %d, snr: %d", idx, used, prn, elevation, azimuth, snr);
579                                 }
580                                 location_satellite_free (last_sat);
581                         } else g_warning ("SYNC>> Last satellite> failed. Error[%s]", str);
582                 }else if(0 == g_strcmp0("9",strOpt) ) {
583
584                         gulong distance;
585                         int ret = 0;
586                         char str[STR_MAX];
587                         LocationPosition pos1, pos2;
588
589                         pos1.latitude = 50.0663222;
590                         pos1.longitude = -5.71475;
591
592                         pos2.latitude = 58.6441;
593                         pos2.longitude = -3.070094;
594
595                         ret = location_get_distance(&pos1, &pos2, &distance);
596                         GetLocationError(str, ret);
597                         if(ret != LOCATION_ERROR_NONE) {
598                                         g_printf("Fail to get position. Error[%s]\n", str);
599                         }
600                         else {
601                                         g_printf("The approximate distance is [%lu]\n", distance);
602                                         g_printf("cf.) It is approximately 969954.114 meter\n");
603                         }
604                 }else if(0 == g_strcmp0("10", strOpt)) {
605                         int method;
606                         char method_str[STR_MAX] = {0, };
607                         char input[8] = {0, };
608                         gboolean is_supported = FALSE;
609
610                         g_printf("0.Hybrid 1.GPS 2.WPS 3.CPS\n");
611                         g_printf("Select Method :");
612                         fgets(input, 8, stdin);
613                         method = atoi(input);
614                         switch(method) {
615                                 case LOCATION_METHOD_HYBRID:
616                                         is_supported = location_is_supported_method(LOCATION_METHOD_HYBRID);
617                                         break;
618
619                                 case LOCATION_METHOD_GPS:
620                                         is_supported = location_is_supported_method(LOCATION_METHOD_GPS);
621                                         break;
622
623                                 case LOCATION_METHOD_WPS:
624                                         is_supported = location_is_supported_method(LOCATION_METHOD_WPS);
625                                         break;
626
627                                 case LOCATION_METHOD_CPS:
628                                         is_supported = location_is_supported_method(LOCATION_METHOD_CPS);
629                                         break;
630
631                                 default:
632                                         break;
633
634                         }
635                         GetMethod(method_str, method);
636
637                         g_printf("Method[%s] is %s.", method_str, is_supported ? "supported" : "not supported");
638
639                 }else if(0 == g_strcmp0("11", strOpt)) {
640                         gboolean is_enabled = FALSE;
641                         is_enabled = location_is_enabled_gps(location_obj);
642                         if(is_enabled == TRUE) g_printf("GPS is turned on");
643                         else g_printf("GPS is turned off");
644                 }else if(0 == g_strcmp0("99", strOpt)) {
645                         int ret = 0;
646                         const *str = "command";
647                         ret = location_send_command(str);
648                         if(ret == 0)
649                                 g_printf("Success to send command[%s]", str);
650                 }else if(0 == g_strcmp0("a1",strOpt)){
651                         if(location_obj && !g_sig_enable) {
652                                 g_sig_enable = g_signal_connect (location_obj, "service-enabled", G_CALLBACK(cb_service_enabled), location_obj);
653                         }
654                 }else if(0 == g_strcmp0("a2",strOpt)){
655                         if(location_obj && !g_sig_disable){
656                                 g_sig_disable = g_signal_connect (location_obj, "service-disabled", G_CALLBACK(cb_service_disabled), location_obj);
657                         }
658                 }else if(0 == g_strcmp0("a3",strOpt)){
659                         if(location_obj && !g_sig_update){
660                                 g_sig_update = g_signal_connect (location_obj, "service-updated", G_CALLBACK(cb_service_updated), location_obj);
661                         }
662                 }else if(0 == g_strcmp0("a4",strOpt)){
663                         if(location_obj && !g_sig_zonein){
664                                 g_sig_zonein = g_signal_connect (location_obj, "zone-in", G_CALLBACK(cb_zone_in), location_obj);
665                         }
666                 }else if(0 == g_strcmp0("a5",strOpt)){
667                         if(location_obj && !g_sig_zoneout){
668                                 g_sig_zoneout = g_signal_connect (location_obj, "zone-out", G_CALLBACK(cb_zone_out), location_obj);
669                         }
670                 }else if(0 == g_strcmp0("b1",strOpt)){
671                         if(location_obj && g_sig_enable) {
672                                 g_signal_handler_disconnect (location_obj, g_sig_enable);
673                                 g_sig_enable = 0;
674                         }
675                 }else if(0 == g_strcmp0("b2",strOpt)){
676                         if(location_obj && g_sig_disable){
677                                 g_signal_handler_disconnect (location_obj, g_sig_disable);
678                                 g_sig_disable = 0;
679                         }
680                 }else if(0 == g_strcmp0("b3",strOpt)){
681                         if(location_obj && g_sig_update){
682                                 g_signal_handler_disconnect (location_obj, g_sig_update);
683                                 g_sig_update = 0;
684                         }
685                 }else if(0 == g_strcmp0("b4",strOpt)){
686                         if(location_obj && g_sig_zonein){
687                                 g_signal_handler_disconnect (location_obj, g_sig_zonein);
688                                 g_sig_zonein = 0;
689                         }
690                 }else if(0 == g_strcmp0("b5",strOpt)){
691                         if(location_obj && g_sig_zoneout){
692                                 g_signal_handler_disconnect (location_obj, g_sig_zoneout);
693                                 g_sig_zoneout = 0;
694                         }
695                 }else if(0 == g_strcmp0("c1",strOpt)){
696                         LocationBoundary* bound = NULL;
697                         int i = 0;
698                         int polygon_count = json_util_get_polygon_count();
699
700                         g_printf("[0].San jose(Rect) [1].Suwon HQ(Rect) [2].Seoul City(circle) ");
701                         for(i = 0; i < polygon_count; i++)
702                                 g_printf("[%d].%s ", i + 3, json_util_get_polygon_name(i));
703
704                         g_printf("\nSelect Boundary: ");
705                         int opt = PromptInt();
706                         if (opt == 0) {
707                                 LocationPosition* rb = location_position_new (0, 37.300, -121.86, 0, LOCATION_STATUS_2D_FIX);
708                                 LocationPosition* lt = location_position_new (0, 37.360, -121.92, 0, LOCATION_STATUS_2D_FIX);
709                                 bound = location_boundary_new_for_rect (lt, rb);
710                                 location_position_free(rb);
711                                 location_position_free(lt);
712
713                                 location_boundary_add(location_obj, bound);
714
715                         } else if(opt == 1) {
716                                 LocationPosition* rb = location_position_new (0, 37.255, 127.058, 0, LOCATION_STATUS_2D_FIX);
717                                 LocationPosition* lt = location_position_new (0, 37.260, 127.045, 0, LOCATION_STATUS_2D_FIX);
718                                 bound = location_boundary_new_for_rect (lt, rb);
719                                 location_position_free(rb);
720                                 location_position_free(lt);
721
722                                 location_boundary_add(location_obj, bound);
723                         } else if(opt == 2) {
724                                 LocationPosition *center = location_position_new(0, 37.566535, 126.977969, 0.0, LOCATION_STATUS_2D_FIX);
725                                 double radius = 10.0;
726                                 bound = location_boundary_new_for_circle(center, radius);
727
728                                 location_boundary_add(location_obj, bound);
729                         } else if (opt > 2 && opt < 3 + polygon_count) {
730                                 int index;
731                                 int polygon_index = opt - 3;
732                                 bound = json_util_get_polygon_boundary(polygon_index);
733                                 location_boundary_add(location_obj, bound);
734
735                                 LocationPosition * check_pos = NULL;
736                                 gboolean ret = FALSE;
737                                 int count = json_util_get_marker_position_count(polygon_index);
738                                 for(index = 0; index < count; index++) {
739                                         check_pos = json_util_get_marker_position(polygon_index, index);
740
741                                         ret = location_boundary_if_inside(bound, check_pos);
742                                         g_printf("[%s] is [%s] and the test result is [%s]\n", json_util_get_marker_name(polygon_index, index), json_util_result_zone_test(polygon_index, index), ret == TRUE? "inside": "outside");
743                                         location_position_free(check_pos);
744                                 }
745                         } else {
746                                 g_printf("boundary is not set\n");
747                         }
748                         location_boundary_free(bound);
749
750                 }else if(0 == g_strcmp0("c2",strOpt)){
751                         g_printf("Get Boundary\n");
752                         location_boundary_foreach(location_obj, PrintBoundary, NULL);
753
754                 }else if(0 == g_strcmp0("c3",strOpt)){
755                         g_printf("Remove Boundary\n");
756                         LocationBoundary* bound = NULL;
757
758                         int i = 0;
759                         int polygon_count = json_util_get_polygon_count();
760
761                         g_printf("[0].San jose(Rect) [1].Suwon HQ(Rect) [2].Seoul City(circle) ");
762                         for(i = 0; i < polygon_count; i++)
763                                 g_printf("[%d].%s ", i + 3, json_util_get_polygon_name(i));
764
765                         g_printf("\nSelect Boundary: ");
766                         int opt = PromptInt();
767                         if (opt == 0) {
768                                 LocationPosition* rb = location_position_new (0, 37.300, -121.86, 0, LOCATION_STATUS_2D_FIX);
769                                 LocationPosition* lt = location_position_new (0, 37.360, -121.92, 0, LOCATION_STATUS_2D_FIX);
770                                 bound = location_boundary_new_for_rect (lt, rb);
771                                 location_position_free(rb);
772                                 location_position_free(lt);
773
774                         } else if(opt == 1) {
775                                 LocationPosition* rb = location_position_new (0, 37.255, 127.058, 0, LOCATION_STATUS_2D_FIX);
776                                 LocationPosition* lt = location_position_new (0, 37.260, 127.045, 0, LOCATION_STATUS_2D_FIX);
777                                 bound = location_boundary_new_for_rect (lt, rb);
778                                 location_position_free(rb);
779                                 location_position_free(lt);
780
781                         } else if(opt == 2) {
782                                 LocationPosition *center = location_position_new(0, 37.566535, 126.977969, 0.0, LOCATION_STATUS_2D_FIX);
783                                 double radius = 10.0;
784                                 bound = location_boundary_new_for_circle(center, radius);
785                                 location_position_free(center);
786
787                         } else if (opt > 2 && opt < 3 + polygon_count) {
788                                 int polygon_index = opt - 3;
789                                 bound = json_util_get_polygon_boundary(polygon_index);
790                         } else {
791                                 g_printf("Invalid value\n");
792                         }
793
794                         if(bound != NULL) location_boundary_remove(location_obj, bound);
795
796                 }else if(0 == g_strcmp0("c4",strOpt)){
797                         location_boundary_foreach(location_obj, RemoveBoundary, location_obj);
798                 }else if(0 == g_strcmp0("c5",strOpt)){
799                         char buf[255];
800                         g_printf("Input device name: ");
801                         fgets(buf, 255, stdin);
802                         buf[strlen(buf)-1]='\0';
803                         g_object_set(location_obj, "dev-name", buf, NULL);
804                 } else if (0 == g_strcmp0("c6", strOpt)) {
805                         guint interval = 1;
806                         g_printf("Input interval[1~120]:");
807                         scanf("%u", &interval);
808                         g_printf("changed interval to [%u]\n", interval);
809                         g_object_set(location_obj, "pos-interval", interval, NULL);
810                 } else if (0 == g_strcmp0("c7", strOpt)) {
811                         guint interval = 1;
812                         g_printf("Input interval[1~120]:");
813                         scanf("%u", &interval);
814                         g_printf("changed interval to [%u]\n", interval);
815                         g_object_set(location_obj, "vel-interval", interval, NULL);
816                 } else if (0 == g_strcmp0("c8", strOpt)) {
817                         guint interval = 1;
818                         g_printf("Input interval[1~120]:");
819                         scanf("%u", &interval);
820                         g_printf("changed interval to [%u]\n", interval);
821                         g_object_set(location_obj, "sat-interval", interval, NULL);
822                 }else if(0 == g_strcmp0("q",strOpt) ){
823                         g_main_loop_quit(g_mainloop);
824                         break;
825                 }
826         }
827         g_thread_join(g_main);
828         g_printf("\n--- Exit LBS Test App ---\n");
829         return 1;
830 }