upload tizen1.0 source
[framework/location/libslp-location.git] / tests / hybrid-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.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         LocationMethod method;
73
74         g_object_get(loc, "method", &method, NULL);
75         g_debug("Get property>> method:%d", method);
76
77         if (LOCATION_ERROR_NONE == location_get_position (loc, &pos, &acc)) {
78                 g_debug ("SYNC>> Current position> time: %d, lat: %f, long: %f, alt: %f, status: %d",
79                         pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
80                 g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
81                         acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
82                 location_position_free(pos);
83                 location_accuracy_free(acc);
84         } else g_warning ("SYNC>> Current position> failed");
85         if (LOCATION_ERROR_NONE == location_get_velocity (loc, &vel, &acc)) {
86                 g_debug ("SYNC>> Current velocity> time: %d, speed: %f, direction:%f, climb:%f",
87                         vel->timestamp, vel->speed, vel->direction, vel->climb);
88                 g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
89                         acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
90                 location_velocity_free(vel);
91                 location_accuracy_free(acc);
92         } else g_warning ("SYNC>> Current velocity> failed");
93         if (LOCATION_ERROR_NONE == location_get_address(loc, &addr, &acc)) {
94                 g_debug ("SYNC>> Current address> %s %s %s %s %s %s %s",
95                         addr->building_number, addr->street, addr->district, addr->city, addr->state, addr->postal_code, addr->country_code);
96                 g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)", acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
97                 location_address_free(addr);
98                 location_accuracy_free(acc);
99         } else g_warning ("SYNC>> Current address> failed");
100         }
101
102 static void
103 cb_service_disabled (GObject *self,
104         guint status,
105         gpointer userdata)
106 {
107         g_debug("cb_service_disabled: status(%d) userdata(0x%x)", status, (unsigned int)userdata);
108 }
109
110 static void
111 cb_zone_in (GObject *self,
112         guint type,
113         gpointer position,
114         gpointer accuracy)
115 {
116         g_debug("cb_zone_in: type(%d)", type);
117         LocationPosition *pos = (LocationPosition*) position;
118         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
119
120         g_debug ("ASYNC>> ZoneIn> Current position: time: %d, lat: %f, long: %f, alt: %f",
121                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude);
122         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
123                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
124 }
125
126 static void
127 cb_zone_out (GObject *self,
128         guint type,
129         gpointer position,
130         gpointer accuracy)
131 {
132         g_debug("cb_zone_out: type(%d)", type);
133         LocationPosition *pos = (LocationPosition*) position;
134         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
135
136         g_debug ("ASYNC>> ZoneOut> Current position: time: %d, lat: %f, long: %f, alt: %f",
137                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude);
138         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
139                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
140 }
141
142 int
143 main (int argc, char *argv[])
144 {
145         LocationObject *loc = NULL;
146
147         // If application is executed by AUL, this is not needed.
148         g_setenv("PKG_NAME", "com.samsung.hybrid-test", 1);
149
150         location_init ();
151
152         loop = g_main_loop_new (NULL, TRUE);
153
154         loc  = location_new (LOCATION_METHOD_HYBRID);
155         if (!loc) {
156                 g_debug("location_new failed");
157                 return -1;
158         }
159
160         LocationMethod method = LOCATION_METHOD_NONE;
161         g_object_get(loc, "method", &method, NULL);
162         g_debug("Get property>> method:%d", method);
163
164         LocationBoundary *bound = NULL;
165         g_object_get(loc, "boundary", &bound, NULL);
166         if (bound) {
167                 g_debug("Get property>> boundary> type: %d", bound->type);
168                 location_boundary_free (bound);
169         } else g_warning("failed to get property> boundary");
170
171         LocationPosition *rb = location_position_new(0, 37.258, 127.056, 0, LOCATION_STATUS_2D_FIX);
172         LocationPosition *lt = location_position_new(0, 37.260, 127.054, 0, LOCATION_STATUS_2D_FIX);
173         bound = location_boundary_new_for_rect(lt, rb);
174         location_position_free (rb);
175         location_position_free (lt);
176         if (bound) {
177                 g_object_set(loc, "boundary", bound, NULL);
178                 location_boundary_free(bound);
179         } else g_warning("failed to location_boundary_new_for_rect()");
180         g_object_get(loc, "boundary", &bound, NULL);
181         if (bound) {
182                 g_debug("Set property>> boundary> type: %d, (%f,%f),(%f,%f)",
183                         bound->type,
184                         bound->rect.right_bottom->latitude, bound->rect.right_bottom->longitude,
185                         bound->rect.left_top->latitude, bound->rect.left_top->longitude);
186                 location_boundary_free (bound);
187         } else  g_warning("failed to set property> boundary");
188
189         g_signal_connect (loc, "service-enabled", G_CALLBACK(cb_service_enabled), loc);
190         g_signal_connect (loc, "service-disabled", G_CALLBACK(cb_service_disabled), loc);
191         g_signal_connect (loc, "service-updated", G_CALLBACK(cb_service_updated), loc);
192         g_signal_connect (loc, "zone-in", G_CALLBACK(cb_zone_in), loc);
193         g_signal_connect (loc, "zone-out", G_CALLBACK(cb_zone_out), loc);
194
195         if (LOCATION_ERROR_NONE != location_start (loc)) {
196                 g_debug("location_start failed");
197                 return -1;
198         }
199
200         g_main_loop_run (loop);
201
202         location_stop (loc);
203
204         LocationPosition *pos = NULL;
205         g_object_get(loc, "last-position", &pos, NULL);
206         if (pos) {
207                 g_debug ("Get property>> last-position> time: %d, lat: %f, long: %f, alt: %f, status: %d",
208                         pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
209                 location_position_free(pos);
210         } else  g_warning("failed to get property> last-position");
211
212         location_free (loc);
213
214         return 0;
215 }