upload tizen1.0 source
[framework/location/libslp-location.git] / tests / position-sample-gps.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 <location.h>
24
25 static GMainLoop *loop = NULL;
26
27 static gboolean
28 exit_program (gpointer data)
29 {
30         g_main_loop_quit (loop);
31         g_debug ("Quit g_main_loop");
32         return FALSE;
33 }
34
35 static void
36 cb_service_updated (GObject *self,
37         guint type,
38         gpointer data,
39         gpointer accuracy,
40         gpointer userdata)
41 {
42         g_debug("cb_service_updated: type(%d) userdata(0x%x)", type, (unsigned int)userdata);
43
44         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
45         switch (type) {
46         case POSITION_UPDATED: {
47                         LocationPosition *pos = (LocationPosition*) data;
48                         g_debug ("ASYNC>> Current position> time: %d, lat: %f, long: %f, alt: %f, status: %d",
49                                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
50                         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
51                                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
52                 }
53         }
54 }
55
56
57 static void
58 cb_service_enabled (GObject *self,
59         guint status,
60         gpointer userdata)
61 {
62         g_debug("cb_service_enabled: status(%d) userdata(0x%x)", status, (unsigned int)userdata);
63
64         LocationObject *loc = (LocationObject*)userdata;
65         LocationAccuracy *acc = NULL;
66         LocationPosition *pos = NULL;
67
68         if (LOCATION_ERROR_NONE == location_get_position (loc, &pos, &acc)) {
69                 g_debug ("SYNC>> Current position> time: %d, lat: %f, long: %f, alt: %f, status: %d",
70                         pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
71                 g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
72                         acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
73                 location_position_free(pos);
74                 location_accuracy_free(acc);
75         } else g_warning ("SYNC>> Current position> failed");
76 }
77
78 static void
79 cb_service_disabled (GObject *self,
80         guint status,
81         gpointer userdata)
82 {
83         g_debug("cb_service_disabled: status(%d) userdata(0x%x)", status, (unsigned int)userdata);
84 }
85
86 int
87 main (int argc, char *argv[])
88 {
89         LocationObject *loc = NULL;
90
91         location_init ();
92
93         loop = g_main_loop_new (NULL, TRUE);
94
95         loc  = location_new (LOCATION_METHOD_GPS);
96         if (!loc) {
97                 g_debug("location_new failed");
98                 return -1;
99         }
100
101         g_signal_connect (loc, "service-enabled", G_CALLBACK(cb_service_enabled), loc);
102         g_signal_connect (loc, "service-disabled", G_CALLBACK(cb_service_disabled), loc);
103         g_signal_connect (loc, "service-updated", G_CALLBACK(cb_service_updated), loc);
104
105         if( LOCATION_ERROR_NONE != location_start (loc) ){
106                 g_debug("location_start failed");
107                 return -1;
108         }
109
110         g_timeout_add_seconds(60, exit_program, NULL);
111         g_main_loop_run (loop);
112
113         location_stop (loc);
114
115         LocationPosition *pos = NULL;
116         g_object_get(loc, "last-position", &pos, NULL);
117         if (pos) {
118                 g_debug ("Get property>> last-position> time: %d, lat: %f, long: %f, alt: %f, status: %d",
119                         pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
120                 location_position_free(pos);
121         } else  g_warning("failed to get property> last-position");
122
123         location_free (loc);
124
125         return 0;
126 }