Location setting restriction to support DPM
[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 /* Location Manager */
40 /*////////////////////////////////////// */
41
42 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)
43 {
44         LOCATIONS_LOGD("location_manager_set_location_batch_cb");
45         LOCATIONS_NOT_SUPPORTED_CHECK(__is_batch_supported());
46
47         LOCATIONS_CHECK_CONDITION(batch_interval >= 1 && batch_interval <= 255, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
48         LOCATIONS_CHECK_CONDITION(batch_period >= 1 && batch_period <= 60000, LOCATIONS_ERROR_INVALID_PARAMETER, "LOCATIONS_ERROR_INVALID_PARAMETER");
49         LOCATIONS_CHECK_CONDITION(batch_interval <= batch_period, 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_batch_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_batch_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         } else {
76                 LOCATIONS_LOGE("method is not GPS");
77         }
78
79         if (handle->user_cb[_LOCATIONS_EVENT_TYPE_BATCH] != NULL)
80                 LOCATIONS_LOGD("batch status set : Start");
81
82         int ret = location_start_batch(handle->object);
83         if (ret != LOCATION_ERROR_NONE)
84                 return __convert_error_code(ret);
85
86         return LOCATIONS_ERROR_NONE;
87 }
88
89 EXPORT_API int location_manager_stop_batch(location_manager_h manager)
90 {
91         LOCATIONS_LOGD("location_manager_stop_batch");
92         LOCATIONS_NOT_SUPPORTED_CHECK(__is_batch_supported());
93         LOCATIONS_NULL_ARG_CHECK(manager);
94         location_manager_s *handle = (location_manager_s *) manager;
95
96         if (LOCATIONS_METHOD_GPS == handle->method) {
97                 if (handle->sig_id[_LOCATION_SIGNAL_BATCH_UPDATED]) {
98                         g_signal_handler_disconnect(handle->object, handle->sig_id[_LOCATION_SIGNAL_BATCH_UPDATED]);
99                         handle->sig_id[_LOCATION_SIGNAL_BATCH_UPDATED] = 0;
100                 }
101         } else {
102                 LOCATIONS_LOGE("method is not GPS");
103         }
104
105         int ret = location_stop_batch(handle->object);
106         if (ret != LOCATION_ERROR_NONE)
107                 return __convert_error_code(ret);
108
109         return LOCATIONS_ERROR_NONE;
110 }
111
112 EXPORT_API int location_manager_foreach_location_batch(location_manager_h manager, location_batch_get_location_cb callback, void *user_data)
113 {
114         LOCATIONS_LOGD("location_manager_foreach_location_batch");
115         LOCATIONS_NOT_SUPPORTED_CHECK(__is_batch_supported());
116         LOCATIONS_NULL_ARG_CHECK(manager);
117         LOCATIONS_NULL_ARG_CHECK(callback);
118         location_manager_s *handle = (location_manager_s *) manager;
119         LocationBatch *batch = NULL;
120
121         int ret = location_get_batch(handle->object, &batch);
122         if (ret != LOCATION_ERROR_NONE || batch == NULL) {
123                 if (ret == LOCATION_ERROR_NOT_SUPPORTED) {
124                         LOCATIONS_LOGE("LOCATIONS_ERROR_INCORRECT_METHOD(0x%08x) : method - %d", LOCATIONS_ERROR_INCORRECT_METHOD, handle->method);
125                         return LOCATIONS_ERROR_INCORRECT_METHOD;
126                 } else if (ret == LOCATION_ERROR_NOT_ALLOWED) {
127                         LOCATIONS_LOGE("LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED");
128                         return LOCATIONS_ERROR_ACCESSIBILITY_NOT_ALLOWED;
129                 }
130
131                 LOCATIONS_LOGE("LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE(0x%08x) : batch is NULL ", LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE);
132                 return LOCATIONS_ERROR_SERVICE_NOT_AVAILABLE;
133         }
134
135         int i;
136         for (i = 0; i < batch->num_of_location; i++) {
137                 gdouble latitude;
138                 gdouble longitude;
139                 gdouble altitude;
140                 gdouble speed;
141                 gdouble direction;
142                 gdouble h_accuracy;
143                 gdouble v_accuracy;
144                 guint timestamp;
145
146                 location_get_batch_details(batch, i, &latitude, &longitude, &altitude, &speed, &direction, &h_accuracy, &v_accuracy, &timestamp);
147                 if (callback(latitude, longitude, altitude, speed, direction, h_accuracy, v_accuracy, timestamp, user_data) != TRUE)
148                         break;
149         }
150         location_batch_free(batch);
151         batch = NULL;
152         return LOCATIONS_ERROR_NONE;
153 }
154
155 EXPORT_API int location_manager_enable_restriction(bool enable)
156 {
157         int ret = LOCATION_ERROR_NONE;
158         ret = location_enable_restriction(enable);
159         return __convert_error_code(ret);
160 }