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