1. Code synchronization with tizen_2.4
[platform/core/api/location-manager.git] / src / location_batch.c
1 /*
2  * Copyright (c) 2011-2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <system_info.h>
21 #include "location_internal.h"
22 #include "location_batch.h"
23
24
25 /*
26 * Internal Implementation
27 */
28
29 static void __cb_batch_updated(GObject *self, guint num_of_location, gpointer userdata)
30 {
31         LOCATIONS_LOGD("Batch callback function has been invoked.");
32         location_manager_s *handle = (location_manager_s *) userdata;
33
34         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_BATCH]) {
35                 ((location_batch_cb) handle->user_cb[_LOCATIONS_EVENT_TYPE_BATCH])(num_of_location, handle->user_data[_LOCATIONS_EVENT_TYPE_BATCH]);
36         }
37 }
38
39 /*/////////////////////////////////////// */
40 /* Location Manager */
41 /*////////////////////////////////////// */
42
43 EXPORT_API int location_manager_set_location_batch_cb(location_manager_h manager, location_batch_cb callback, int batch_interval, int batch_period, void *user_data)
44 {
45         LOCATIONS_LOGD("location_manager_set_location_batch_cb");
46         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
47
48         LOCATIONS_CHECK_CONDITION(batch_interval >= 1 && batch_interval <= 120, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
49         LOCATIONS_CHECK_CONDITION(batch_period >= 120 && batch_period <= 600, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
50         LOCATIONS_NULL_ARG_CHECK(manager);
51         LOCATIONS_NULL_ARG_CHECK(callback);
52         location_manager_s *handle = (location_manager_s *) manager;
53         g_object_set(handle->object, "batch-period", batch_period, NULL);
54         g_object_set(handle->object, "batch-interval", batch_interval, NULL);
55         return __set_callback(_LOCATIONS_EVENT_TYPE_BATCH, manager, callback, user_data);
56 }
57
58 EXPORT_API int location_manager_unset_location_batch_cb(location_manager_h manager)
59 {
60         LOCATIONS_LOGD("location_manager_unset_location_batch_cb");
61         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
62         return __unset_callback(_LOCATIONS_EVENT_TYPE_BATCH, manager);
63 }
64
65 EXPORT_API int location_manager_start_batch(location_manager_h manager)
66 {
67         LOCATIONS_LOGD("location_manager_start_batch");
68         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
69         LOCATIONS_NULL_ARG_CHECK(manager);
70         location_manager_s *handle = (location_manager_s *) manager;
71
72         if (LOCATIONS_METHOD_GPS == handle->method) {
73                 if (!handle->sig_id[_LOCATION_SIGNAL_BATCH_UPDATED]) {
74                         handle->sig_id[_LOCATION_SIGNAL_BATCH_UPDATED] = g_signal_connect(handle->object, "batch-updated", G_CALLBACK(__cb_batch_updated), handle);
75                 }
76         } else {
77                 LOCATIONS_LOGE("method is not GPS");
78         }
79
80         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_BATCH] != NULL) {
81                 LOCATIONS_LOGI("batch status set : Start");
82         }
83
84         int ret = location_start_batch(handle->object);
85         if (ret != LOCATION_ERROR_NONE) {
86                 return __convert_error_code(ret);
87         }
88
89         return LOCATIONS_ERROR_NONE;
90 }
91
92 EXPORT_API int location_manager_stop_batch(location_manager_h manager)
93 {
94         LOCATIONS_LOGD("location_manager_stop_batch");
95         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
96         LOCATIONS_NULL_ARG_CHECK(manager);
97         location_manager_s *handle = (location_manager_s *) manager;
98
99         if (LOCATIONS_METHOD_GPS == handle->method) {
100                 if (handle->sig_id[_LOCATION_SIGNAL_BATCH_UPDATED]) {
101                         g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_BATCH_UPDATED]);
102                         handle->sig_id[_LOCATION_SIGNAL_BATCH_UPDATED] = 0;
103                 }
104         } else {
105                 LOCATIONS_LOGE("method is not GPS");
106         }
107
108         int ret = location_stop_batch(handle->object);
109         if (ret != LOCATION_ERROR_NONE) {
110                 return __convert_error_code(ret);
111         }
112
113         return LOCATIONS_ERROR_NONE;
114 }
115
116 EXPORT_API int location_manager_foreach_location_batch(location_manager_h manager, location_batch_get_location_cb callback, void *user_data)
117 {
118         LOCATIONS_LOGD("location_manager_foreach_location_batch");
119         LOCATIONS_NOT_SUPPORTED_CHECK(__is_location_supported());
120         LOCATIONS_NULL_ARG_CHECK(manager);
121         LOCATIONS_NULL_ARG_CHECK(callback);
122         location_manager_s *handle = (location_manager_s *) manager;
123         LocationBatch *batch = NULL;
124
125         int ret = location_get_batch(handle->object, &batch);
126         if (ret != LOCATION_ERROR_NONE || batch == NULL) {
127                 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
128                         LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d", LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
129                         return LOCATIONS_ERROR_INCORRECT_METHOD;
130                 } else if (ret == LOCATION_ERROR_NOT_ALLOWED) {
131                         LOCATIONS_LOGE("LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED");
132                         return LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED;
133                 }
134
135                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : batch is NULL ",
136                                LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
137                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
138         }
139
140         int i;
141         for (i = 0; i < batch->num_of_location; i++) {
142                 gdouble latitude;
143                 gdouble longitude;
144                 gdouble altitude;
145                 gdouble speed;
146                 gdouble direction;
147                 gdouble h_accuracy;
148                 gdouble v_accuracy;
149                 guint timestamp;
150
151                 location_get_batch_details(batch, i, &latitude, &longitude, &altitude, &speed, &direction, &h_accuracy, &v_accuracy, &timestamp);
152                 if (callback(latitude, longitude, altitude, speed, direction, h_accuracy, v_accuracy, timestamp, user_data) != TRUE) {
153                         break;
154                 }
155         }
156         location_batch_free(batch);
157         batch = NULL;
158         return LOCATIONS_ERROR_NONE;
159 }