2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <locations.h>
21 #include "cam_debug.h"
24 location_manager_h location_handle;
25 CamLBSState m_lbs_state;
26 void (*lbs_update_cb) (void *data, int lbs_state);
34 static CamLBSInfo *cam_lbs_info = NULL; /* lbs_info */
36 void cam_lbs_set_state_from_accuracy_level()
38 g_return_if_fail(cam_lbs_info);
39 g_return_if_fail(cam_lbs_info->location_handle);
41 location_accuracy_level_e accuracy_level;
45 int ret = LOCATIONS_ERROR_NONE;
47 ret = location_manager_get_accuracy(cam_lbs_info->location_handle,
48 &accuracy_level, &horizontal, &vertical);
49 if (ret != LOCATIONS_ERROR_NONE) {
50 cam_debug(LOG_SYS, "location_manager_get_accuracy failed!! error = %d", ret);
54 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_DISABLE;
56 switch (accuracy_level) {
57 case LOCATIONS_ACCURACY_NONE:
58 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_SERVICE_START;
60 case LOCATIONS_ACCURACY_COUNTRY:
61 case LOCATIONS_ACCURACY_REGION :
62 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_SERVICE_ENABLE;
64 case LOCATIONS_ACCURACY_LOCALITY :
65 case LOCATIONS_ACCURACY_POSTALCODE:
66 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_ACCURANCY_ROUGH;
68 case LOCATIONS_ACCURACY_STREET :
69 case LOCATIONS_ACCURACY_DETAILED :
70 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_ACCURANCY_DETAILED;
73 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_DISABLE;
78 static void cam_lbs_position_updated_cb(double latitude, double longitude,
79 double altitude, time_t timestamp, void *user_data)
81 cam_debug(LOG_SYS, "cam_lbs_position_updated_cb : lat(%f), long(%f), alt(%f), time(%f)",
82 latitude, longitude, altitude, timestamp);
84 g_return_if_fail(cam_lbs_info);
86 cam_lbs_set_state_from_accuracy_level();
88 cam_lbs_info->latitude = latitude;
89 cam_lbs_info->longitude = longitude;
90 cam_lbs_info->altitude = altitude;
91 cam_lbs_info->time_stamp = timestamp;
93 /* call callback function */
94 if (cam_lbs_info->lbs_update_cb)
95 cam_lbs_info->lbs_update_cb(cam_lbs_info->cb_data, cam_lbs_get_state());
98 static void cam_lbs_service_state_changed_cb(location_service_state_e state, void *user_data)
100 cam_debug(LOG_SYS, "cam_lbs_service_state_changed_cb : state(%d)", state);
102 g_return_if_fail(cam_lbs_info);
103 g_return_if_fail(cam_lbs_info->location_handle);
106 case LOCATIONS_SERVICE_ENABLED : {
107 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_SERVICE_ENABLE;
109 double altitude = -1.0;
110 double latitude = -1.0;
111 double longitude = -1.0;
112 time_t timestamp = -1.0;
114 int ret = LOCATIONS_ERROR_NONE;
116 ret = location_manager_get_position(cam_lbs_info->location_handle,
117 &altitude, &latitude, &longitude, ×tamp);
118 if(ret != LOCATIONS_ERROR_NONE) {
119 cam_debug(LOG_SYS, "location_manager_get_position failed!! error = %d", ret);
123 cam_debug(LOG_SYS, "cam_lbs_service_state_changed_cb : alt(%f), lat(%f), long(%f), time(%f)",
124 altitude, latitude, longitude, timestamp);
126 cam_lbs_info->altitude = altitude;
127 cam_lbs_info->latitude = latitude;
128 cam_lbs_info->longitude = longitude;
129 cam_lbs_info->time_stamp = timestamp;
131 cam_lbs_set_state_from_accuracy_level();
133 /* call callback function */
134 if (cam_lbs_info->lbs_update_cb)
135 cam_lbs_info->lbs_update_cb(cam_lbs_info->cb_data, cam_lbs_get_state());
138 case LOCATIONS_SERVICE_DISABLED : {
139 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_DISABLE;
141 /* call callback function */
142 if (cam_lbs_info->lbs_update_cb)
143 cam_lbs_info->lbs_update_cb(cam_lbs_info->cb_data, cam_lbs_get_state());
151 gboolean cam_lbs_init(void)
153 g_return_val_if_fail(!cam_lbs_info, FALSE);
155 debug_fenter(LOG_UI);
157 cam_lbs_info = g_new0(CamLBSInfo, 1);
158 cam_lbs_info->location_handle = NULL;
159 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_DISABLE;
160 cam_lbs_info->lbs_update_cb = NULL;
161 cam_lbs_info->cb_data = NULL;
162 cam_lbs_info->altitude = -1.0;
163 cam_lbs_info->latitude = -1.0;
164 cam_lbs_info->longitude = -1.0;
165 cam_lbs_info->time_stamp = -1.0;
167 int ret = LOCATIONS_ERROR_NONE;
169 ret = location_manager_create(LOCATIONS_METHOD_HYBRID, &cam_lbs_info->location_handle);
170 if (ret != LOCATIONS_ERROR_NONE) {
171 cam_debug(LOG_SYS, "location_manager_create failed!! error = %d", ret);
175 ret = location_manager_set_service_state_changed_cb(cam_lbs_info->location_handle,
176 cam_lbs_service_state_changed_cb, (void*)NULL);
177 if (ret != LOCATIONS_ERROR_NONE) {
178 cam_debug(LOG_SYS, "location_manager_set_service_state_changed_cb failed!! error = %d", ret);
182 ret = location_manager_set_position_updated_cb(cam_lbs_info->location_handle,
183 cam_lbs_position_updated_cb, 30, (void*)NULL);
184 if (ret != LOCATIONS_ERROR_NONE) {
185 cam_debug(LOG_SYS, "location_manager_set_position_updated_cb failed!! error = %d", ret);
194 g_free(cam_lbs_info);
200 gboolean cam_lbs_finialize(void)
202 g_return_val_if_fail(cam_lbs_info, FALSE);
203 g_return_val_if_fail(cam_lbs_info->location_handle, FALSE);
205 debug_fenter(LOG_UI);
207 int ret = LOCATIONS_ERROR_NONE;
209 ret = location_manager_destroy(cam_lbs_info->location_handle);
212 g_free(cam_lbs_info);
215 if (ret != LOCATIONS_ERROR_NONE) {
216 cam_debug(LOG_SYS, "location_manager_destroy failed!! error = %d", ret);
223 gboolean cam_lbs_start(void (*lbs_update_cb) (void *data, int lbs_state), void *data)
225 g_return_val_if_fail(cam_lbs_info, FALSE);
226 g_return_val_if_fail(cam_lbs_info->location_handle, FALSE);
228 cam_lbs_info->lbs_update_cb = lbs_update_cb;
229 cam_lbs_info->cb_data = data;
231 int ret = LOCATIONS_ERROR_NONE;
233 ret = location_manager_start(cam_lbs_info->location_handle);
234 if (ret != LOCATIONS_ERROR_NONE) {
235 cam_debug(LOG_SYS, "location_manager_start failed!! error = %d", ret);
236 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_DISABLE;
240 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_SERVICE_START;
245 gboolean cam_lbs_stop(void)
247 g_return_val_if_fail(cam_lbs_info, FALSE);
248 g_return_val_if_fail(cam_lbs_info->location_handle, FALSE);
250 int ret = LOCATIONS_ERROR_NONE;
252 ret = location_manager_stop(cam_lbs_info->location_handle);
253 if (ret != LOCATIONS_ERROR_NONE) {
254 cam_debug(LOG_SYS, "location_manager_stop failed!! error = %d", ret);
258 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_DISABLE;
263 gboolean cam_lbs_is_valid(void)
265 g_return_val_if_fail(cam_lbs_info, FALSE);
267 if (cam_lbs_info->m_lbs_state > CAM_LBS_STATE_SERVICE_START)
273 gboolean cam_lbs_get_current_position(double *longitude, double *latitude,
274 double *altitude, time_t *time_stamp)
276 g_return_val_if_fail(cam_lbs_info, FALSE);
277 g_return_val_if_fail(cam_lbs_info->m_lbs_state >= CAM_LBS_STATE_SERVICE_ENABLE, FALSE);
279 cam_debug(LOG_SYS, "SYNC>> Current position:");
280 cam_debug(LOG_SYS, "\ttime: %f, lat: %f, long: %f, alt: %f",
281 cam_lbs_info->time_stamp, cam_lbs_info->latitude,
282 cam_lbs_info->longitude, cam_lbs_info->altitude);
284 *altitude = cam_lbs_info->altitude;
285 *latitude = cam_lbs_info->latitude;
286 *longitude = cam_lbs_info->longitude;
287 *time_stamp = cam_lbs_info->time_stamp;
292 gboolean cam_lbs_get_address(char *address, int max_length)
294 g_return_val_if_fail(cam_lbs_info, FALSE);
299 int cam_lbs_get_state(void)
301 g_return_val_if_fail(cam_lbs_info, CAM_LBS_STATE_DISABLE);
303 return cam_lbs_info->m_lbs_state;