7b11c3fde00868b537e19802fb5c58738c6a0fc9
[platform/core/location/lbs-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         LocationAccuracy **prev_acc,
59         GList *prev_bound,
60         ZoneStatus *zone_status,
61         const LocationPosition *pos,
62         const LocationAccuracy *acc)
63 {
64         g_return_if_fail(zone_status);
65         g_return_if_fail(pos);
66         g_return_if_fail(acc);
67         g_return_if_fail(obj);
68         g_return_if_fail(signals);
69
70         int index = 0;
71         gboolean is_inside = FALSE;
72         GList *boundary_list = prev_bound;
73         LocationBoundary *boundary = NULL;
74
75         if (!pos->timestamp)    return;
76
77         if (*prev_pos) location_position_free (*prev_pos);
78         if (*prev_acc) location_accuracy_free (*prev_acc);
79
80         *prev_pos = location_position_copy(pos);
81         *prev_acc = location_accuracy_copy(acc);
82         LOCATION_LOGD("timestamp[%d], lat [%f], lon [%f]", (*prev_pos)->timestamp, (*prev_pos)->latitude, (*prev_pos)->longitude);
83
84         if (emit && pos->timestamp - *updated_timestamp >= interval) {
85                 LOCATION_LOGD("POSITION SERVICE_UPDATED");
86                 g_signal_emit(obj, signals[SERVICE_UPDATED], 0, POSITION_UPDATED, pos, acc);
87                 *updated_timestamp = pos->timestamp;
88         }
89
90         if(boundary_list) {
91                 while((boundary = (LocationBoundary *)g_list_nth_data(boundary_list, index))!= NULL) {
92                         is_inside = location_boundary_if_inside(boundary, pos);
93                         if(is_inside) {
94                                 break;
95                         }
96                         index++;
97                 }
98
99                 if(is_inside) {
100                         if(*zone_status != ZONE_STATUS_IN) {
101                                 LOCATION_LOGD("Signal emit: ZONE IN");
102                                 g_signal_emit(obj, signals[ZONE_IN], 0, NULL, pos, acc);
103                                 *zone_status = ZONE_STATUS_IN;
104                         }
105                 }
106                 else {
107                         if (*zone_status != ZONE_STATUS_OUT) {
108                                 LOCATION_LOGD("Signal emit : ZONE_OUT");
109                                 g_signal_emit(obj, signals[ZONE_OUT], 0, NULL, pos, acc);
110                                 *zone_status = ZONE_STATUS_OUT;
111                         }
112                 }
113         }
114 }
115
116 void
117 velocity_signaling (LocationObject *obj,
118         guint32 signals[LAST_SIGNAL],
119         gboolean *prev_enabled,
120         int interval,
121         gboolean emit,
122         guint *updated_timestamp,
123         LocationVelocity **prev_vel,
124         const LocationVelocity *vel,
125         const LocationAccuracy *acc)
126 {
127         g_return_if_fail(obj);
128         g_return_if_fail(signals);
129         g_return_if_fail(vel);
130
131         if (!vel->timestamp) return;
132
133         if (*prev_vel) location_velocity_free (*prev_vel);
134         *prev_vel = location_velocity_copy (vel);
135         LOCATION_LOGD("timestamp[%d]", (*prev_vel)->timestamp);
136
137         if (emit && vel->timestamp - *updated_timestamp >= interval) {
138                 LOCATION_LOGD("VELOCITY SERVICE_UPDATED");
139                 g_signal_emit(obj, signals[SERVICE_UPDATED], 0, VELOCITY_UPDATED, vel, acc);
140                 *updated_timestamp = vel->timestamp;
141         }
142 }
143
144 void
145 satellite_signaling(LocationObject *obj,
146         guint32 signals[LAST_SIGNAL],
147         gboolean *prev_enabled,
148         int interval,
149         gboolean emit,
150         guint *updated_timestamp,
151         LocationSatellite **prev_sat,
152         const LocationSatellite *sat)
153 {
154         g_return_if_fail(obj);
155         g_return_if_fail(signals);
156         g_return_if_fail(sat);
157
158         if (!sat->timestamp) return;
159
160         if (*prev_sat) location_satellite_free (*prev_sat);
161         *prev_sat = location_satellite_copy (sat);
162
163         if (emit && sat->timestamp - *updated_timestamp >= interval) {
164                 LOCATION_LOGD("SATELLITE SERVICE_UPDATED");
165                 g_signal_emit(obj, signals[SERVICE_UPDATED], 0, SATELLITE_UPDATED, sat, NULL);
166                 *updated_timestamp = sat->timestamp;
167         }
168
169 }