removed wrong contacts, added authors
[platform/core/location/lbs-location.git] / location / manager / location-batch.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #define MAX_BATCH_ITEM          8
24 #define BATCH_SENTENCE_SIZE     256
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <tzplatform_config.h>
30 #include <sensor.h>
31 #include "location-batch.h"
32 #include "location-log.h"
33
34
35 EXPORT_API LocationBatch *
36 location_batch_new(int num_of_location)
37 {
38         LocationBatch *batch = g_slice_new0(LocationBatch);
39         g_return_val_if_fail(batch, NULL);
40
41         batch->num_of_location = num_of_location;
42         batch->batch_data = g_new0(LocationBatchDetail, batch->num_of_location);
43         return batch;
44 }
45
46 EXPORT_API gboolean
47 location_set_batch_parse_details(LocationBatch *batch, char *location_info, int i)
48 {
49         g_return_val_if_fail(batch, FALSE);
50         g_return_val_if_fail(batch->batch_data, FALSE);
51
52         char location[128] = {0,};
53         char *last_location[MAX_BATCH_ITEM] = {0,};
54         char *last = NULL;
55         int index = 0;
56
57         snprintf(location, sizeof(location), "%s", location_info);
58
59         last_location[index] = (char *)strtok_r(location, ";", &last);
60         while (last_location[index] != NULL) {
61                 switch (index) {
62                 case 0:
63                                 batch->batch_data[i].timestamp = strtod(last_location[index], NULL);
64                                 break;
65                 case 1:
66                                 batch->batch_data[i].latitude = strtod(last_location[index], NULL);
67                                 break;
68                 case 2:
69                                 batch->batch_data[i].longitude = strtod(last_location[index], NULL);
70                                 break;
71                 case 3:
72                                 batch->batch_data[i].altitude = strtod(last_location[index], NULL);
73                                 break;
74                 case 4:
75                                 batch->batch_data[i].speed = strtod(last_location[index], NULL);
76                                 break;
77                 case 5:
78                                 batch->batch_data[i].direction = strtod(last_location[index], NULL);
79                                 break;
80                 case 6:
81                                 batch->batch_data[i].horizontal_accuracy = strtod(last_location[index], NULL);
82                                 break;
83                 case 7:
84                                 batch->batch_data[i].vertical_accuracy = strtod(last_location[index], NULL);
85                                 break;
86                 default:
87                                 break;
88                 }
89                 if (++index == MAX_BATCH_ITEM) break;
90                 last_location[index] = (char *)strtok_r(NULL, ";", &last);
91         }
92
93         return TRUE;
94 }
95
96 EXPORT_API LocationBatch *
97 location_get_batch_file(int num_of_location)
98 {
99         LOCATION_LOGD("location_get_batch_file [num_of_location = %d]", num_of_location);
100         LocationBatch *batch = location_batch_new(num_of_location);
101         batch->num_of_location = num_of_location;
102
103         const char *batch_path = tzplatform_mkpath(TZ_SYS_MEDIA, "lbs-server/location_batch.log");
104         FILE *fd = fopen(batch_path, "r");
105
106         if (fd != NULL) {
107                 char buf[BATCH_SENTENCE_SIZE] = { 0, };
108                 int i = 0;
109
110                 for (i = 0; i < num_of_location; i++) {
111                         if (fgets(buf, BATCH_SENTENCE_SIZE, fd) != NULL)
112                                 location_set_batch_parse_details(batch, buf, i);
113                         else
114                                 LOCATION_LOGE("Batch fgets failed");
115                 }
116
117                 if (fd != 0) {
118                         if (fclose(fd) != 0)
119                                 LOCATION_LOGE("Batch fclose failed");
120                         fd = NULL;
121                 }
122         } else {
123                 LOCATION_LOGE("Batch fopen failed. fd is NULL");
124         }
125         return batch;
126 }
127
128 #if defined(TIZEN_DEVICE)
129 EXPORT_API gboolean location_set_sensor_batch(LocationBatch *batch, sensor_event_s *event)
130 {
131         g_return_val_if_fail(batch, FALSE);
132         g_return_val_if_fail(event, FALSE);
133         g_return_val_if_fail(batch->num_of_location > event->values[4], FALSE);
134         unsigned long long timestamp = event->timestamp;
135         float latitude  = event->values[0];
136         float longitude = event->values[1];
137         float altitude  = event->values[2];
138         float speed     = event->values[3];
139         int idx   = (int)(event->values[4]);
140
141         batch->batch_data[idx].timestamp = batch->start_time - (time_t)((timestamp / 1001000) % 100000);
142         batch->batch_data[idx].latitude = latitude;
143         batch->batch_data[idx].longitude = longitude;
144         batch->batch_data[idx].altitude = altitude;
145         batch->batch_data[idx].speed = speed;
146         batch->batch_data[idx].direction = 0;
147         batch->batch_data[idx].horizontal_accuracy = 0;
148         batch->batch_data[idx].vertical_accuracy = 0;
149
150         return TRUE;
151 }
152 #endif
153
154 EXPORT_API LocationBatch *
155 location_batch_copy(const LocationBatch *batch)
156 {
157         g_return_val_if_fail(batch, NULL);
158         LocationBatch *batch_dup = location_batch_new(batch->num_of_location);
159         batch_dup->num_of_location = batch->num_of_location;
160         int i = 0;
161         for (i = 0 ; i < batch_dup->num_of_location; i++)
162                 location_set_batch_details(batch_dup, i,
163                                                                         batch->batch_data[i].latitude,
164                                                                         batch->batch_data[i].longitude,
165                                                                         batch->batch_data[i].altitude,
166                                                                         batch->batch_data[i].speed,
167                                                                         batch->batch_data[i].direction,
168                                                                         batch->batch_data[i].horizontal_accuracy,
169                                                                         batch->batch_data[i].vertical_accuracy,
170                                                                         batch->batch_data[i].timestamp);
171         return batch_dup;
172 }
173
174 EXPORT_API gboolean
175 location_get_batch_details(const LocationBatch *batch,
176                                                         guint index,
177                                                         gdouble *latitude,
178                                                         gdouble *longitude,
179                                                         gdouble *altitude,
180                                                         gdouble *speed,
181                                                         gdouble *direction,
182                                                         gdouble *h_accuracy,
183                                                         gdouble *v_accuracy,
184                                                         guint *timestamp)
185 {
186         g_return_val_if_fail(batch, FALSE);
187         g_return_val_if_fail(latitude, FALSE);
188         g_return_val_if_fail(longitude, FALSE);
189         g_return_val_if_fail(altitude, FALSE);
190         g_return_val_if_fail(speed, FALSE);
191         g_return_val_if_fail(direction, FALSE);
192         g_return_val_if_fail(h_accuracy, FALSE);
193         g_return_val_if_fail(v_accuracy, FALSE);
194         g_return_val_if_fail(timestamp, FALSE);
195         g_return_val_if_fail(batch->batch_data, FALSE);
196
197         *latitude       = batch->batch_data[index].latitude;
198         *longitude      = batch->batch_data[index].longitude;
199         *altitude       = batch->batch_data[index].altitude;
200         *speed          = batch->batch_data[index].speed;
201         *direction      = batch->batch_data[index].direction;
202         *h_accuracy     = batch->batch_data[index].horizontal_accuracy;
203         *v_accuracy     = batch->batch_data[index].vertical_accuracy;
204         *timestamp      = batch->batch_data[index].timestamp;
205
206         return TRUE;
207 }
208
209 EXPORT_API gboolean
210 location_set_batch_details(LocationBatch *batch,
211                                                         guint index,
212                                                         gdouble latitude,
213                                                         gdouble longitude,
214                                                         gdouble altitude,
215                                                         gdouble speed,
216                                                         gdouble direction,
217                                                         gdouble h_accuracy,
218                                                         gdouble v_accuracy,
219                                                         guint timestamp)
220 {
221         g_return_val_if_fail(batch, FALSE);
222         g_return_val_if_fail(batch->batch_data, FALSE);
223         g_return_val_if_fail(index < batch->num_of_location, FALSE);
224
225         batch->batch_data[index].latitude = latitude;
226         batch->batch_data[index].longitude = longitude;
227         batch->batch_data[index].altitude = altitude;
228         batch->batch_data[index].speed = speed;
229         batch->batch_data[index].direction = direction;
230         batch->batch_data[index].horizontal_accuracy = h_accuracy;
231         batch->batch_data[index].vertical_accuracy = v_accuracy;
232         batch->batch_data[index].timestamp = timestamp;
233
234         return TRUE;
235 }
236
237 EXPORT_API void
238 location_batch_free(LocationBatch *batch)
239 {
240         g_return_if_fail(batch);
241         g_free(batch->batch_data);
242         g_slice_free(LocationBatch, batch);
243 }
244