Git init
[framework/location/libslp-location.git] / tests / sps-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 update_gps(LocationObject* loc)
29 {
30         g_debug("update gps");
31         double ran = g_random_double()/200;
32         LocationPosition* pos = location_position_new (time(NULL), 37.257809 + ran, 127.056383 + ran, 150.199997 + ran, LOCATION_STATUS_3D_FIX);
33         g_object_set(loc, "position-base", pos, NULL);
34         location_position_free (pos);
35
36         LocationVelocity* vel = location_velocity_new (pos->timestamp, 0, 0, 0);
37         g_object_set(loc, "velocity-base", vel, NULL);
38         location_velocity_free (vel);
39
40         LocationAccuracy* acc = location_accuracy_new (LOCATION_ACCURACY_LEVEL_DETAILED, 0, 0);
41         g_object_set(loc, "accuracy-info", acc, NULL);
42         location_accuracy_free (acc);
43
44         LocationSatellite* sat = location_satellite_new (9);
45         location_satellite_set_satellite_details(sat, 0, 5, 1, 0, 0, 41);
46         location_satellite_set_satellite_details(sat, 1, 9, 1, 0, 0, 46);
47         location_satellite_set_satellite_details(sat, 2, 15, 1, 0, 0, 45);
48         location_satellite_set_satellite_details(sat, 3, 18, 1, 0, 0, 41);
49         location_satellite_set_satellite_details(sat, 4, 21, 1, 0, 0, 34);
50         location_satellite_set_satellite_details(sat, 5, 22, 0, 0, 0, 41);
51         location_satellite_set_satellite_details(sat, 6, 26, 0, 0, 0, 46);
52         location_satellite_set_satellite_details(sat, 7, 27, 0, 0, 0, 31);
53         location_satellite_set_satellite_details(sat, 8, 28, 0, 0, 0, 33);
54         g_object_set(loc, "satellite-info", sat, NULL);
55         location_satellite_free (sat);
56 }
57
58 static gboolean
59 update_timer(gpointer data)
60 {
61         update_gps(data);
62         return TRUE;
63 }
64
65 static void
66 cb_service_updated (GObject *self,
67         guint type,
68         gpointer data,
69         gpointer accuracy,
70         gpointer userdata)
71 {
72         g_debug("cb_service_updated: type(%d) userdata(0x%x)", type, (unsigned int)userdata);
73
74         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
75         switch (type) {
76         case POSITION_UPDATED: {
77                         LocationPosition *pos = (LocationPosition*) data;
78                         g_debug ("ASYNC>> 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                 }
83                 break;
84         case VELOCITY_UPDATED: {
85                         LocationVelocity *vel = (LocationVelocity*) data;
86                         g_debug ("ASYNC>> 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                 }
91                 break;
92         default:
93                 g_warning ("ASYNC>> Undefined update type");
94                 break;
95         }
96 }
97
98 static void
99 cb_service_enabled (GObject *self,
100         guint status,
101         gpointer userdata)
102 {
103         g_debug("cb_service_enabled: status(%d) userdata(0x%x)", status, (unsigned int)userdata);
104
105         LocationObject *loc = (LocationObject*)userdata;
106         LocationAccuracy *acc = NULL;
107         LocationPosition *pos = NULL;
108         LocationVelocity *vel = NULL;
109         LocationAddress *addr = NULL;
110
111         if (LOCATION_ERROR_NONE == location_get_position (loc, &pos, &acc)) {
112                 g_debug ("SYNC>> Current position> time: %d, lat: %f, long: %f, alt: %f, status: %d",
113                         pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
114                 g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
115                         acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
116                 location_position_free(pos);
117                 location_accuracy_free(acc);
118         } else g_warning ("SYNC>> Current position> failed");
119         if (LOCATION_ERROR_NONE == location_get_velocity (loc, &vel, &acc)) {
120                 g_debug ("SYNC>> Current velocity> time: %d, speed: %f, direction:%f, climb:%f",
121                         vel->timestamp, vel->speed, vel->direction, vel->climb);
122                 g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
123                         acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
124                 location_velocity_free(vel);
125                 location_accuracy_free(acc);
126         } else g_warning ("SYNC>> Current velocity> failed");
127         if (LOCATION_ERROR_NONE == location_get_address(loc, &addr, &acc)) {
128                 g_debug ("SYNC>> Current address> %s %s %s %s %s %s %s",
129                         addr->building_number, addr->street, addr->district, addr->city, addr->state, addr->postal_code, addr->country_code);
130                 g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)", acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
131                 location_address_free(addr);
132                 location_accuracy_free(acc);
133         } else g_warning ("SYNC>> Current address> failed");
134 }
135
136 static void
137 cb_service_disabled (GObject *self,
138         guint status,
139         gpointer userdata)
140 {
141         g_debug("cb_service_disabled: status(%d) userdata(0x%x)", status, (unsigned int)userdata);
142 }
143
144 static void
145 cb_zone_in (GObject *self,
146         guint type,
147         gpointer position,
148         gpointer accuracy)
149 {
150         g_debug("cb_zone_in: type(%d)", type);
151         LocationPosition *pos = (LocationPosition*) position;
152         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
153
154         g_debug ("ASYNC>> ZoneIn> Current position: time: %d, lat: %f, long: %f, alt: %f",
155                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude);
156         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
157                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
158 }
159
160 static void
161 cb_zone_out (GObject *self,
162         guint type,
163         gpointer position,
164         gpointer accuracy)
165 {
166         g_debug("cb_zone_out: type(%d)", type);
167         LocationPosition *pos = (LocationPosition*) position;
168         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
169
170         g_debug ("ASYNC>> ZoneOut> Current position: time: %d, lat: %f, long: %f, alt: %f",
171                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude);
172         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
173                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
174 }
175
176
177 int
178 main (int argc, char *argv[])
179 {
180         LocationObject *loc = NULL;
181
182         // If application is executed by AUL, this is not needed.
183         g_setenv("PKG_NAME", "org.tizen.sps-test", 1);
184
185         location_init ();
186
187         loop = g_main_loop_new (NULL, TRUE);
188
189         loc  = location_new (LOCATION_METHOD_SPS);
190         if (!loc) {
191                 g_debug("location_new failed");
192                 return -1;
193         }
194
195         LocationMethod method = LOCATION_METHOD_NONE;
196         g_object_get(loc, "method", &method, NULL);
197         g_debug("Get property>> method:%d", method);
198
199         LocationBoundary *bound = NULL;
200         g_object_get(loc, "boundary", &bound, NULL);
201         if (bound) {
202                 g_debug("Get property>> boundary> type: %d", bound->type);
203                 location_boundary_free (bound);
204         } else g_warning("failed to get property> boundary");
205
206         LocationPosition *rb = location_position_new(0, 37.255, 127.060, 0, LOCATION_STATUS_2D_FIX);
207         LocationPosition *lt = location_position_new(0, 37.260, 127.055, 0, LOCATION_STATUS_2D_FIX);
208         bound = location_boundary_new_for_rect(lt, rb);
209         location_position_free (rb);
210         location_position_free (lt);
211         if (bound) {
212                 g_object_set(loc, "boundary", bound, NULL);
213                 location_boundary_free(bound);
214         } else g_warning("failed to location_boundary_new_for_rect()");
215         g_object_get(loc, "boundary", &bound, NULL);
216         if (bound) {
217                 g_debug("Set property>> boundary> type: %d, (%f,%f),(%f,%f)",
218                         bound->type,
219                         bound->rect.right_bottom->latitude, bound->rect.right_bottom->longitude,
220                         bound->rect.left_top->latitude, bound->rect.left_top->longitude);
221                 location_boundary_free (bound);
222         } else  g_warning("failed to set property> boundary");
223
224         update_gps(loc);
225         g_signal_connect (loc, "service-enabled", G_CALLBACK(cb_service_enabled), loc);
226         g_signal_connect (loc, "service-disabled", G_CALLBACK(cb_service_disabled), loc);
227         g_signal_connect (loc, "service-updated", G_CALLBACK(cb_service_updated), loc);
228         g_signal_connect (loc, "zone-in", G_CALLBACK(cb_zone_in), loc);
229         g_signal_connect (loc, "zone-out", G_CALLBACK(cb_zone_out), loc);
230
231         if( LOCATION_ERROR_NONE != location_start (loc) ){
232                 g_debug("location_start failed");
233                 return -1;
234         }
235
236         g_timeout_add_seconds(1, update_timer, loc);
237         g_main_loop_run (loop);
238
239         location_stop (loc);
240
241         LocationPosition *pos = NULL;
242         g_object_get(loc, "last-position", &pos, NULL);
243         if (pos) {
244                 g_debug ("Get property>> last-position> time: %d, lat: %f, long: %f, alt: %f, status: %d",
245                         pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
246                 location_position_free(pos);
247         } else  g_warning("failed to get property> last-position");
248
249         location_free (loc);
250
251         return 0;
252 }