Git init
[framework/location/libslp-location.git] / tests / wps-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 <location/location.h>
24
25 static GMainLoop *loop = NULL;
26
27 static void
28 cb_service_updated (GObject *self,
29         guint type,
30         gpointer data,
31         gpointer accuracy,
32         gpointer userdata)
33 {
34         g_debug("cb_service_updated: type(%d) userdata(0x%x)", type, (unsigned int)userdata);
35
36         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
37         switch (type) {
38         case POSITION_UPDATED: {
39                         LocationPosition *pos = (LocationPosition*) data;
40                         g_debug ("ASYNC>> Current position> time: %d, lat: %f, long: %f, alt: %f, status: %d",
41                                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
42                         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
43                                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
44                 }
45                 break;
46         case VELOCITY_UPDATED: {
47                         LocationVelocity *vel = (LocationVelocity*) data;
48                         g_debug ("ASYNC>> Current velocity> time: %d, speed: %f, direction:%f, climb:%f",
49                                 vel->timestamp, vel->speed, vel->direction, vel->climb);
50                         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
51                                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
52                 }
53                 break;
54         default:
55                 g_warning ("ASYNC>> Undefined update type");
56                 break;
57         }
58 }
59
60 static void
61 cb_service_enabled (GObject *self,
62         guint status,
63         gpointer userdata)
64 {
65         g_debug("cb_service_enabled: status(%d) userdata(0x%x)", status, (unsigned int)userdata);
66
67         LocationObject *loc = (LocationObject*)userdata;
68         LocationAccuracy *acc = NULL;
69         LocationPosition *pos = NULL;
70         LocationVelocity *vel = NULL;
71         LocationAddress *addr = NULL;
72
73         if (LOCATION_ERROR_NONE == location_get_position (loc, &pos, &acc)) {
74                 g_debug ("SYNC>> Current position> time: %d, lat: %f, long: %f, alt: %f, status: %d",
75                         pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
76                 g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
77                         acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
78                 location_position_free(pos);
79                 location_accuracy_free(acc);
80         } else g_warning ("SYNC>> Current position> failed");
81         if (LOCATION_ERROR_NONE == location_get_velocity (loc, &vel, &acc)) {
82                 g_debug ("SYNC>> Current velocity> time: %d, speed: %f, direction:%f, climb:%f",
83                         vel->timestamp, vel->speed, vel->direction, vel->climb);
84                 g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
85                         acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
86                 location_velocity_free(vel);
87                 location_accuracy_free(acc);
88         } else g_warning ("SYNC>> Current velocity> failed");
89         if (LOCATION_ERROR_NONE == location_get_address(loc, &addr, &acc)) {
90                 g_debug ("SYNC>> Current address> %s %s %s %s %s %s %s",
91                         addr->building_number, addr->street, addr->district, addr->city, addr->state, addr->postal_code, addr->country_code);
92                 g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)", acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
93                 location_address_free(addr);
94                 location_accuracy_free(acc);
95         } else g_warning ("SYNC>> Current address> failed");
96 }
97
98 static void
99 cb_service_disabled (GObject *self,
100         guint status,
101         gpointer userdata)
102 {
103         g_debug("cb_service_disabled: status(%d) userdata(0x%x)", status, (unsigned int)userdata);
104 }
105
106 static void
107 cb_zone_in (GObject *self,
108         guint type,
109         gpointer position,
110         gpointer accuracy)
111 {
112         g_debug("cb_zone_in: type(%d)", type);
113         LocationPosition *pos = (LocationPosition*) position;
114         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
115
116         g_debug ("ASYNC>> ZoneIn> Current position: time: %d, lat: %f, long: %f, alt: %f",
117                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude);
118         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
119                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
120 }
121
122 static void
123 cb_zone_out (GObject *self,
124         guint type,
125         gpointer position,
126         gpointer accuracy)
127 {
128         g_debug("cb_zone_out: type(%d)", type);
129         LocationPosition *pos = (LocationPosition*) position;
130         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
131
132         g_debug ("ASYNC>> ZoneOut> Current position: time: %d, lat: %f, long: %f, alt: %f",
133                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude);
134         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
135                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
136 }
137
138 int
139 main (int argc, char *argv[])
140 {
141         LocationObject *loc = NULL;
142
143         // If application is executed by AUL, this is not needed.
144         g_setenv("PKG_NAME", "org.tizen.wps-test", 1);
145
146         location_init ();
147
148         loop = g_main_loop_new (NULL, TRUE);
149
150         loc  = location_new (LOCATION_METHOD_WPS);
151         if (!loc) {
152                 g_debug("location_new failed");
153                 return -1;
154         }
155
156         LocationMethod method = LOCATION_METHOD_NONE;
157         g_object_get(loc, "method", &method, NULL);
158         g_debug("Get property>> method:%d", method);
159
160         LocationBoundary *bound = NULL;
161         g_object_get(loc, "boundary", &bound, NULL);
162         if (bound) {
163                 g_debug("Get property>> boundary> type: %d", bound->type);
164                 location_boundary_free (bound);
165         } else g_warning("failed to get property> boundary");
166
167         LocationPosition *rb = location_position_new(0, 37.255, 127.056, 0, LOCATION_STATUS_2D_FIX);
168         LocationPosition *lt = location_position_new(0, 37.260, 127.050, 0, LOCATION_STATUS_2D_FIX);
169         bound = location_boundary_new_for_rect(lt, rb);
170         location_position_free (rb);
171         location_position_free (lt);
172         if (bound) {
173                 g_object_set(loc, "boundary", bound, NULL);
174                 location_boundary_free(bound);
175         } else g_warning("failed to location_boundary_new_for_rect()");
176         g_object_get(loc, "boundary", &bound, NULL);
177         if (bound) {
178                 g_debug("Set property>> boundary> type: %d, (%f,%f),(%f,%f)",
179                         bound->type,
180                         bound->rect.right_bottom->latitude, bound->rect.right_bottom->longitude,
181                         bound->rect.left_top->latitude, bound->rect.left_top->longitude);
182                 location_boundary_free (bound);
183         } else  g_warning("failed to set property> boundary");
184
185         g_signal_connect (loc, "service-enabled", G_CALLBACK(cb_service_enabled), loc);
186         g_signal_connect (loc, "service-disabled", G_CALLBACK(cb_service_disabled), loc);
187         g_signal_connect (loc, "service-updated", G_CALLBACK(cb_service_updated), loc);
188         g_signal_connect (loc, "zone-in", G_CALLBACK(cb_zone_in), loc);
189         g_signal_connect (loc, "zone-out", G_CALLBACK(cb_zone_out), loc);
190
191         if (LOCATION_ERROR_NONE != location_start (loc)) {
192                 g_debug("location_start failed");
193                 return -1;
194         }
195
196         g_main_loop_run (loop);
197
198         location_stop (loc);
199
200         LocationPosition *pos = NULL;
201         g_object_get(loc, "last-position", &pos, NULL);
202         if (pos) {
203                 g_debug ("Get property>> last-position> time: %d, lat: %f, long: %f, alt: %f, status: %d",
204                         pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
205                 location_position_free(pos);
206         } else  g_warning("failed to get property> last-position");
207
208         location_free (loc);
209
210         return 0;
211 }