upload tizen1.0 source
[framework/location/libslp-location.git] / tests / zone-sample.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 gboolean
28 exit_program (gpointer data)
29 {
30         g_main_loop_quit (loop);
31         g_debug ("Quit g_main_loop");
32         return FALSE;
33 }
34
35 static void
36 cb_service_enabled (GObject *self,
37         guint status,
38         gpointer userdata)
39 {
40         g_debug("cb_service_enabled: status(%d) userdata(0x%x)", status, (unsigned int)userdata);
41 }
42
43 static void
44 cb_service_disabled (GObject *self,
45         guint status,
46         gpointer userdata)
47 {
48         g_debug("cb_service_disabled: status(%d) userdata(0x%x)", status, (unsigned int)userdata);      
49 }
50
51 static void
52 cb_zone_in (GObject *self,
53         guint type,
54         gpointer position,
55         gpointer accuracy)
56 {       
57         g_debug("cb_zone_in: type(%d)", type);
58         LocationPosition *pos = (LocationPosition*) position;
59         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
60
61         g_debug ("ASYNC>> ZoneIn> Current position: time: %d, lat: %f, long: %f, alt: %f", 
62                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude);
63         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)", 
64                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
65 }
66
67 static void
68 cb_zone_out (GObject *self,
69         guint type,
70         gpointer position,
71         gpointer accuracy)
72 {
73         g_debug("cb_zone_out: type(%d)", type);
74         LocationPosition *pos = (LocationPosition*) position;
75         LocationAccuracy *acc = (LocationAccuracy*) accuracy;
76
77         g_debug ("ASYNC>> ZoneOut> Current position: time: %d, lat: %f, long: %f, alt: %f", 
78                 pos->timestamp, pos->latitude, pos->longitude, pos->altitude);
79         g_debug ("\tAccuracy level %d (%.0f meters %.0f meters)", 
80                 acc->level, acc->horizontal_accuracy, acc->vertical_accuracy);
81 }
82
83 int
84 main (int argc, char *argv[])
85 {
86         LocationObject *loc = NULL;
87         
88         location_init ();
89
90         loop = g_main_loop_new (NULL, TRUE);
91
92         loc  = location_new (LOCATION_METHOD_GPS);
93         if (!loc) {
94                 g_debug("location_new failed");
95                 return -1;
96         }
97
98         LocationPosition *rb = location_position_new(0, 37.258, 127.056, 0, LOCATION_STATUS_2D_FIX);
99         LocationPosition *lt = location_position_new(0, 37.260, 127.054, 0, LOCATION_STATUS_2D_FIX);
100         LocationBoundary *bound = location_boundary_new_for_rect(lt, rb);
101         location_position_free (rb);
102         location_position_free (lt);
103         if (bound) {
104                 g_object_set(loc, "boundary", bound, NULL);
105                 location_boundary_free(bound);
106         } else g_warning("failed to location_boundary_new_for_rect()"); 
107         g_object_get(loc, "boundary", &bound, NULL);
108         if (bound) {
109                 g_debug("Set property>> boundary> type: %d, (%f,%f),(%f,%f)", 
110                         bound->type,
111                         bound->rect.right_bottom->latitude, bound->rect.right_bottom->longitude,
112                         bound->rect.left_top->latitude, bound->rect.left_top->longitude);
113                 location_boundary_free (bound);
114         } else  g_warning("failed to set property> boundary");
115
116         g_signal_connect (loc, "service-enabled", G_CALLBACK(cb_service_enabled), loc);
117         g_signal_connect (loc, "service-disabled", G_CALLBACK(cb_service_disabled), loc);
118         g_signal_connect (loc, "zone-in", G_CALLBACK(cb_zone_in), loc);
119         g_signal_connect (loc, "zone-out", G_CALLBACK(cb_zone_out), loc);
120         
121         if( LOCATION_ERROR_NONE != location_start (loc) ){
122                 g_debug("location_start failed");
123                 return -1;
124         }
125
126         g_timeout_add_seconds (60, exit_program, NULL);
127         g_main_loop_run (loop);
128
129         location_stop (loc);
130         location_free (loc);
131
132         return 0;
133 }