4 * Copyright (c) 2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #define MAX_BATCH_ITEM 8
24 #define BATCH_SENTENCE_SIZE 256
29 #include <tzplatform_config.h>
31 #include "location-batch.h"
32 #include "location-log.h"
35 EXPORT_API LocationBatch *
36 location_batch_new(int num_of_location)
38 LocationBatch *batch = g_slice_new0(LocationBatch);
39 g_return_val_if_fail(batch, NULL);
41 batch->num_of_location = num_of_location;
42 batch->batch_data = g_new0(LocationBatchDetail, batch->num_of_location);
47 location_set_batch_parse_details(LocationBatch *batch, char *location_info, int i)
49 g_return_val_if_fail(batch, FALSE);
50 g_return_val_if_fail(batch->batch_data, FALSE);
52 char location[128] = {0,};
53 char *last_location[MAX_BATCH_ITEM] = {0,};
57 snprintf(location, sizeof(location), "%s", location_info);
59 last_location[index] = (char *)strtok_r(location, ";", &last);
60 while (last_location[index] != NULL) {
63 batch->batch_data[i].timestamp = strtod(last_location[index], NULL);
66 batch->batch_data[i].latitude = strtod(last_location[index], NULL);
69 batch->batch_data[i].longitude = strtod(last_location[index], NULL);
72 batch->batch_data[i].altitude = strtod(last_location[index], NULL);
75 batch->batch_data[i].speed = strtod(last_location[index], NULL);
78 batch->batch_data[i].direction = strtod(last_location[index], NULL);
81 batch->batch_data[i].horizontal_accuracy = strtod(last_location[index], NULL);
84 batch->batch_data[i].vertical_accuracy = strtod(last_location[index], NULL);
89 if (++index == MAX_BATCH_ITEM) break;
90 last_location[index] = (char *)strtok_r(NULL, ";", &last);
96 EXPORT_API LocationBatch *
97 location_get_batch_file(int num_of_location)
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;
103 const char *batch_path = tzplatform_mkpath(TZ_SYS_MEDIA, "lbs-server/location_batch.log");
104 FILE *fd = fopen(batch_path, "r");
107 char buf[BATCH_SENTENCE_SIZE] = { 0, };
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);
114 LOCATION_LOGE("Batch fgets failed");
119 LOCATION_LOGE("Batch fclose failed");
123 LOCATION_LOGE("Batch fopen failed. fd is NULL");
128 #if defined(TIZEN_DEVICE)
129 EXPORT_API gboolean location_set_sensor_batch(LocationBatch *batch, sensor_event_s *event)
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]);
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;
154 EXPORT_API LocationBatch *
155 location_batch_copy(const LocationBatch *batch)
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;
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);
175 location_get_batch_details(const LocationBatch *batch,
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);
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;
210 location_set_batch_details(LocationBatch *batch,
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);
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;
238 location_batch_free(LocationBatch *batch)
240 g_return_if_fail(batch);
241 g_free(batch->batch_data);
242 g_slice_free(LocationBatch, batch);