Fix a crash after changes that velocity is updated before position
[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 void
30 enable_signaling (LocationObject *obj,
31         guint32 signals[LAST_SIGNAL],
32         gboolean *prev_enabled,
33         gboolean enabled,
34         LocationStatus status)
35 {
36         g_return_if_fail(obj);
37         g_return_if_fail(signals);
38         g_return_if_fail(prev_enabled);
39         if (*prev_enabled == TRUE && enabled == FALSE) {
40                 *prev_enabled = FALSE;
41                 LOCATION_LOGD("Signal emit: SERVICE_DISABLED");
42                 g_signal_emit (obj, signals[SERVICE_DISABLED], 0, LOCATION_STATUS_NO_FIX);
43         } else if (*prev_enabled == FALSE && enabled == TRUE){
44                 *prev_enabled = TRUE;
45                 LOCATION_LOGD("Signal emit: SERVICE_ENABLED");
46                 g_signal_emit (obj, signals[SERVICE_ENABLED], 0, status);
47         }
48 }
49
50 void
51 position_signaling (LocationObject *obj,
52         guint32 signals[LAST_SIGNAL],
53         gboolean *prev_enabled,
54         int interval,
55         gboolean emit,
56         guint *updated_timestamp,
57         LocationPosition **prev_pos,
58         GList *prev_bound,
59         ZoneStatus *zone_status,
60         const LocationPosition *pos,
61         const LocationAccuracy *acc)
62 {
63         g_return_if_fail(zone_status);
64         g_return_if_fail(pos);
65         g_return_if_fail(acc);
66         g_return_if_fail(obj);
67         g_return_if_fail(signals);
68
69         int index = 0;
70         gboolean is_inside = FALSE;
71         GList *boundary_list = prev_bound;
72         LocationBoundary *boundary = NULL;
73
74         if (!pos->timestamp)    return;
75
76         if (*prev_pos) location_position_free (*prev_pos);
77
78         *prev_pos = location_position_copy(pos);
79         LOCATION_LOGD("timestamp[%d], lat [%f], lon [%f]", (*prev_pos)->timestamp, (*prev_pos)->latitude, (*prev_pos)->longitude);
80
81         if (emit && pos->timestamp - *updated_timestamp >= interval) {
82                 LOCATION_LOGD("POSITION SERVICE_UPDATED");
83                 g_signal_emit(obj, signals[SERVICE_UPDATED], 0, POSITION_UPDATED, pos, acc);
84                 *updated_timestamp = pos->timestamp;
85         }
86
87         if(boundary_list) {
88                 while((boundary = (LocationBoundary *)g_list_nth_data(boundary_list, index))!= NULL) {
89                         is_inside = location_boundary_if_inside(boundary, pos);
90                         if(is_inside) {
91                                 break;
92                         }
93                         index++;
94                 }
95
96                 if(is_inside) {
97                         if(*zone_status != ZONE_STATUS_IN) {
98                                 LOCATION_LOGD("Signal emit: ZONE IN");
99                                 g_signal_emit(obj, signals[ZONE_IN], 0, NULL, pos, acc);
100                                 *zone_status = ZONE_STATUS_IN;
101                         }
102                 }
103                 else {
104                         if (*zone_status != ZONE_STATUS_OUT) {
105                                 LOCATION_LOGD("Signal emit : ZONE_OUT");
106                                 g_signal_emit(obj, signals[ZONE_OUT], 0, NULL, pos, acc);
107                                 *zone_status = ZONE_STATUS_OUT;
108                         }
109                 }
110         }
111 }
112
113 void
114 velocity_signaling (LocationObject *obj,
115         guint32 signals[LAST_SIGNAL],
116         gboolean *prev_enabled,
117         int interval,
118         gboolean emit,
119         guint *updated_timestamp,
120         LocationVelocity **prev_vel,
121         LocationAccuracy **prev_acc,
122         const LocationVelocity *vel,
123         const LocationAccuracy *acc)
124 {
125         g_return_if_fail(obj);
126         g_return_if_fail(signals);
127         g_return_if_fail(vel);
128
129         if (!vel->timestamp) return;
130
131         if (*prev_vel) location_velocity_free (*prev_vel);
132         if (*prev_acc) location_accuracy_free (*prev_acc);
133
134         *prev_vel = location_velocity_copy (vel);
135         *prev_acc = location_accuracy_copy (acc);
136         LOCATION_LOGD("timestamp[%d]", (*prev_vel)->timestamp);
137
138         if (emit && vel->timestamp - *updated_timestamp >= interval) {
139                 LOCATION_LOGD("VELOCITY SERVICE_UPDATED");
140                 g_signal_emit(obj, signals[SERVICE_UPDATED], 0, VELOCITY_UPDATED, vel, acc);
141                 *updated_timestamp = vel->timestamp;
142         }
143 }
144
145 void
146 satellite_signaling(LocationObject *obj,
147         guint32 signals[LAST_SIGNAL],
148         gboolean *prev_enabled,
149         int interval,
150         gboolean emit,
151         guint *updated_timestamp,
152         LocationSatellite **prev_sat,
153         const LocationSatellite *sat)
154 {
155         g_return_if_fail(obj);
156         g_return_if_fail(signals);
157         g_return_if_fail(sat);
158
159         if (!sat->timestamp) return;
160
161         if (*prev_sat) location_satellite_free (*prev_sat);
162         *prev_sat = location_satellite_copy (sat);
163
164         if (emit && sat->timestamp - *updated_timestamp >= interval) {
165                 LOCATION_LOGD("SATELLITE SERVICE_UPDATED");
166                 g_signal_emit(obj, signals[SERVICE_UPDATED], 0, SATELLITE_UPDATED, sat, NULL);
167                 *updated_timestamp = sat->timestamp;
168         }
169
170 }