c36dbf7d39b4d4bc9ff90a619770b6d767ab6a16
[framework/location/libslp-location.git] / tests / gps-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
61 static void
62 cb_service_enabled (GObject *self,
63         guint status,
64         gpointer userdata)
65 {
66         g_debug("cb_service_enabled: status(%d) userdata(0x%x)", status, (unsigned int)userdata);
67
68         LocationObject *loc = (LocationObject*)userdata;
69         LocationAccuracy *acc = NULL;
70         LocationPosition *pos = NULL;
71         LocationVelocity *vel = NULL;
72         LocationAddress *addr = NULL;
73         LocationSatellite *sat = NULL;
74         gchar *nmea_data = NULL;
75         int idx = 0;
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         g_object_get (loc, "satellite", &sat, NULL);
102         if (sat) {
103                 g_debug ("SYNC>> Current Sattelite> satellite in view = %d, satellite in used = %d", sat->num_of_sat_inview, sat->num_of_sat_used);
104                 g_debug ("\tinview satellite information = ");
105                 for (idx=0; idx<sat->num_of_sat_inview; idx++) {
106                         guint prn;
107                         gboolean used;
108                         guint elevation;
109                         guint azimuth;
110                         gint snr;
111                         location_satellite_get_satellite_details(sat, idx, &prn, &used, &elevation, &azimuth, &snr);
112                         g_debug ("\t\t[%02d] used: %d, prn: %d, elevation: %d, azimuth: %d, snr: %d",
113                                 idx, used, prn, elevation, azimuth, snr);
114                 }
115                 location_satellite_free (sat);
116         } else g_warning ("SYNC>> Current Sattelite> failed");
117
118         g_object_get(loc, "nmea", &nmea_data, NULL);
119         if (nmea_data) {
120                 g_debug("SYNC>> Currnet NMEA> nmea_data:\n%s\n", nmea_data);
121                 g_free(nmea_data);
122         } else g_warning("SYNC>> Current NMEA> failed");
123 }
124
125 static void
126 cb_service_disabled (GObject *self,
127         guint status,
128         gpointer userdata)
129 {
130         g_debug("cb_service_disabled: status(%d) userdata(0x%x)", status, (unsigned int)userdata);
131 }
132
133 static void
134 cb_zone_in (GObject *self,
135         guint type,
136         gpointer position,
137         gpointer accuracy)
138 {
139         g_debug("cb_zone_in: type(%d)", type);
140         LocationPosition *pos = (LocationPosition*) position;
141         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
142
143         g_debug ("ASYNC>> ZoneIn> Current position: time: %d, lat: %f, long: %f, alt: %f",
144                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude);
145         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
146                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
147 }
148
149 static void
150 cb_zone_out (GObject *self,
151         guint type,
152         gpointer position,
153         gpointer accuracy)
154 {
155         g_debug("cb_zone_out: type(%d)", type);
156         LocationPosition *pos = (LocationPosition*) position;
157         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
158
159         g_debug ("ASYNC>> ZoneOut> Current position: time: %d, lat: %f, long: %f, alt: %f",
160                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude);
161         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)",
162                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
163 }
164
165 int
166 main (int argc, char *argv[])
167 {
168         LocationObject *loc = NULL;
169
170         // If application is executed by AUL, this is not needed.
171         g_setenv("PKG_NAME", "com.samsung.gps-test", 1);
172
173         location_init ();
174
175         loop = g_main_loop_new (NULL, TRUE);
176
177         loc  = location_new (LOCATION_METHOD_GPS);
178         if (!loc) {
179                 g_debug("location_new failed");
180                 return -1;
181         }
182
183         LocationMethod method = LOCATION_METHOD_NONE;
184         g_object_get(loc, "method", &method, NULL);
185         g_debug("Get property>> method:%d", method);
186
187         char* devname = NULL;
188         g_object_get(loc, "dev-name", &devname, NULL);
189         if (devname) {
190                 g_debug("Get property>> dev-name: %s", devname);
191         } else g_warning("failed to get property> dev-name");
192
193         devname = NULL;
194         g_object_set(loc, "dev-name", "/dev/test", NULL);
195         g_object_get(loc, "dev-name", &devname, NULL);
196         if (devname) {
197                 g_debug("Get property>> dev-name: %s", devname);
198                 g_free(devname);
199         } else g_warning("failed to set property> dev-name");
200
201         LocationBoundary *bound = NULL;
202         g_object_get(loc, "boundary", &bound, NULL);
203         if (bound) {
204                 g_debug("Get property>> boundary> type: %d", bound->type);
205         } else g_warning("failed to get property> boundary");
206
207         bound = NULL;
208         LocationPosition *rb = location_position_new(0, 37.255, 127.056, 0, LOCATION_STATUS_2D_FIX);
209         LocationPosition *lt = location_position_new(0, 37.260, 127.050, 0, LOCATION_STATUS_2D_FIX);
210         bound = location_boundary_new_for_rect(lt, rb);
211         location_position_free (rb);
212         location_position_free (lt);
213         if (bound) {
214                 g_object_set(loc, "boundary", bound, NULL);
215         } else g_warning("failed to location_boundary_new_for_rect()");
216
217         bound = NULL;
218         g_object_get(loc, "boundary", &bound, NULL);
219         if (bound) {
220                 g_debug("Set property>> boundary> type: %d, (%f,%f),(%f,%f)",
221                         bound->type,
222                         bound->rect.right_bottom->latitude, bound->rect.right_bottom->longitude,
223                         bound->rect.left_top->latitude, bound->rect.left_top->longitude);
224                 location_boundary_free (bound);
225         } else  g_warning("failed to set property> boundary");
226
227         g_signal_connect (loc, "service-enabled", G_CALLBACK(cb_service_enabled), loc);
228         g_signal_connect (loc, "service-disabled", G_CALLBACK(cb_service_disabled), loc);
229         g_signal_connect (loc, "service-updated", G_CALLBACK(cb_service_updated), loc);
230         g_signal_connect (loc, "zone-in", G_CALLBACK(cb_zone_in), loc);
231         g_signal_connect (loc, "zone-out", G_CALLBACK(cb_zone_out), loc);
232
233         if( LOCATION_ERROR_NONE != location_start (loc) ){
234                 g_debug("location_start failed");
235                 return -1;
236         }
237
238         g_main_loop_run (loop);
239
240         location_stop (loc);
241
242         LocationPosition *pos = NULL;
243         g_object_get(loc, "last-position", &pos, NULL);
244         if (pos) {
245                 g_debug ("Get property>> last-position> time: %d, lat: %f, long: %f, alt: %f, status: %d",
246                         pos->timestamp, pos->latitude, pos->longitude, pos->altitude, pos->status);
247                 location_position_free(pos);
248         } else  g_warning("failed to get property> last-position");
249
250         location_free (loc);
251
252         return 0;
253 }