upload tizen1.0 source
[framework/location/libslp-location.git] / location / manager / location-signaling-util.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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "location-signaling-util.h"
27 #include "location-log.h"
28
29 #define POS_EXPIRATION_TIME             9       /* sec */
30
31 void
32 enable_signaling (LocationObject *obj,
33         guint32 signals[LAST_SIGNAL],
34         gboolean *prev_enabled,
35         gboolean enabled,
36         LocationStatus status)
37 {
38         g_return_if_fail(obj);
39         g_return_if_fail(signals);
40         g_return_if_fail(prev_enabled);
41         if (*prev_enabled == TRUE && enabled == FALSE) {
42                 *prev_enabled = FALSE;
43                 LOCATION_LOGD("Signal emit: SERVICE_DISABLED");
44                 g_signal_emit (obj, signals[SERVICE_DISABLED], 0, LOCATION_STATUS_NO_FIX);
45         } else if (*prev_enabled == FALSE && enabled == TRUE){
46                 *prev_enabled = TRUE;
47                 LOCATION_LOGD("Signal emit: SERVICE_ENABLED");
48                 g_signal_emit (obj, signals[SERVICE_ENABLED], 0, status);
49         }
50 }
51
52 void
53 position_signaling (LocationObject *obj,
54         guint32 signals[LAST_SIGNAL],
55         gboolean *prev_enabled,
56         guint interval,
57         LocationPosition **prev_pos,
58         LocationAccuracy **prev_acc,
59         GList *prev_bound,
60         ZoneStatus *zone_status,
61         gboolean enabled,
62         const LocationPosition *pos,
63         const LocationAccuracy *acc)
64 {
65         g_return_if_fail(zone_status);
66         g_return_if_fail(pos);
67         g_return_if_fail(acc);
68         g_return_if_fail(obj);
69         g_return_if_fail(signals);
70
71         int index = 0;
72         gboolean is_inside = FALSE;
73         guint updated_timestamp = 0;
74         GList *boundary_list = prev_bound;
75         LocationBoundary *boundary = NULL;
76
77         if(!*prev_pos || !*prev_acc || location_accuracy_level_compare(*prev_acc, acc) != -1 ||
78                         (pos->timestamp - (*prev_pos)->timestamp) > POS_EXPIRATION_TIME) {
79
80                 if(*prev_pos) {
81                         updated_timestamp = (*prev_pos)->updated_timestamp;
82                         location_position_free(*prev_pos);
83                 }
84                 if(*prev_acc) location_accuracy_free(*prev_acc);
85
86                 if(!updated_timestamp || pos->timestamp - updated_timestamp >= interval) {
87                         LOCATION_LOGD("Signal emit: POSITION SERVICE_UPDATED");
88                         g_signal_emit(obj, signals[SERVICE_UPDATED], 0, POSITION_UPDATED, pos, acc);
89                         updated_timestamp = pos->timestamp;
90                 }
91
92                 if(boundary_list) {
93                         while((boundary = (LocationBoundary *)g_list_nth_data(boundary_list, index))!= NULL) {
94
95                                 is_inside = location_boundary_if_inside(boundary, pos);
96                                 if(is_inside) {
97                                         break;
98                                 }
99                                 index++;
100                         }
101
102                         if(is_inside) {
103                                 if(*zone_status != ZONE_STATUS_IN) {
104                                         LOCATION_LOGD("Signal emit: ZONE IN");
105                                         g_signal_emit(obj, signals[ZONE_IN], 0, NULL, pos, acc);
106                                         *zone_status = ZONE_STATUS_IN;
107                                 }
108                         }
109                         else {
110                                 if (*zone_status != ZONE_STATUS_OUT) {
111                                         LOCATION_LOGD("Signal emit : ZONE_OUT");
112                                         g_signal_emit(obj, signals[ZONE_OUT], 0, NULL, pos, acc);
113                                         *zone_status = ZONE_STATUS_OUT;
114                                 }
115                         }
116
117                 }
118         }
119
120         *prev_pos = location_position_copy(pos);
121         *prev_acc = location_accuracy_copy(acc);
122         (*prev_pos)->updated_timestamp = updated_timestamp;
123 }
124
125 void
126 velocity_signaling (LocationObject *obj,
127         guint32 signals[LAST_SIGNAL],
128         gboolean *prev_enabled,
129         guint interval,
130         LocationVelocity **prev_vel,
131         gboolean enabled,
132         const LocationVelocity *vel,
133         const LocationAccuracy *acc)
134 {
135         g_return_if_fail(obj);
136         g_return_if_fail(signals);
137         g_return_if_fail(vel);
138
139         guint updated_timestamp = 0;
140
141         if(*prev_vel) {
142                 updated_timestamp = (*prev_vel)->updated_timestamp;
143                 if(!location_velocity_equal(*prev_vel, vel)) {
144                         location_velocity_free (*prev_vel);
145                 }
146         }
147
148         *prev_vel = location_velocity_copy (vel);
149
150         if(!updated_timestamp || vel->timestamp - updated_timestamp >= interval) {
151
152                 LOCATION_LOGD ("Signal emit: VELOCITY SERVICE_UPDATED");
153                 LocationVelocity *temp_vel = location_velocity_copy (*prev_vel);
154                 LocationAccuracy *temp_acc = location_accuracy_copy(acc);
155
156                 g_signal_emit (obj, signals[SERVICE_UPDATED], 0, VELOCITY_UPDATED, temp_vel, temp_acc);
157
158                 (*prev_vel)->updated_timestamp = vel->timestamp;
159
160                 location_velocity_free(temp_vel);
161                 location_accuracy_free(temp_acc);
162         }
163         else {
164                 (*prev_vel)->updated_timestamp = updated_timestamp;
165         }
166
167 }
168
169 void
170 satellite_signaling(LocationObject *obj,
171         guint32 signals[LAST_SIGNAL],
172         gboolean *prev_enabled,
173         guint interval,
174         guint *sat_timestamp,
175         LocationSatellite **prev_sat,
176         gboolean enabled,
177         const LocationSatellite *sat)
178 {
179         g_return_if_fail(obj);
180         g_return_if_fail(signals);
181         g_return_if_fail(sat);
182
183         if (*prev_sat) {
184                 location_satellite_free(*prev_sat);
185         }
186
187         *prev_sat = location_satellite_copy (sat);
188         if (!(*sat_timestamp) || sat->timestamp - *sat_timestamp > interval) {
189
190                 LOCATION_LOGD ("Signal emit: SATELLITE SERVICE_UPDATED");
191                 LocationSatellite *temp_sat = location_satellite_copy (sat);
192
193                 g_signal_emit (obj, signals[SERVICE_UPDATED], 0, SATELLITE_UPDATED, temp_sat, NULL);
194
195                 *sat_timestamp = sat->timestamp;
196
197                 location_satellite_free(temp_sat);
198         }
199 }