cfd532f1146ac1f9c6c91172f727c1ddb05f2e17
[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 #if defined(TIZEN_DEVICE)
47 EXPORT_API gboolean location_set_sensor_batch(LocationBatch *batch, sensor_event_s *event)
48 {
49         g_return_val_if_fail(batch, FALSE);
50         g_return_val_if_fail(event, FALSE);
51         g_return_val_if_fail(batch->num_of_location > event->values[4], FALSE);
52         unsigned long long timestamp = event->timestamp;
53         float latitude  = event->values[0];
54         float longitude = event->values[1];
55         float altitude  = event->values[2];
56         float speed     = event->values[3];
57         int idx   = (int)(event->values[4]);
58
59         batch->batch_data[idx].timestamp = batch->start_time - (time_t)((timestamp / 1001000) % 100000);
60         batch->batch_data[idx].latitude = latitude;
61         batch->batch_data[idx].longitude = longitude;
62         batch->batch_data[idx].altitude = altitude;
63         batch->batch_data[idx].speed = speed;
64         batch->batch_data[idx].direction = 0;
65         batch->batch_data[idx].horizontal_accuracy = 0;
66         batch->batch_data[idx].vertical_accuracy = 0;
67
68         return TRUE;
69 }
70 #endif
71
72 EXPORT_API LocationBatch *
73 location_batch_copy(const LocationBatch *batch)
74 {
75         g_return_val_if_fail(batch, NULL);
76         LocationBatch *batch_dup = location_batch_new(batch->num_of_location);
77         batch_dup->num_of_location = batch->num_of_location;
78         int i = 0;
79         for (i = 0 ; i < batch_dup->num_of_location; i++)
80                 location_set_batch_details(batch_dup, i,
81                                 batch->batch_data[i].latitude, batch->batch_data[i].longitude, batch->batch_data[i].altitude,
82                                 batch->batch_data[i].speed, batch->batch_data[i].direction,
83                                 batch->batch_data[i].horizontal_accuracy, batch->batch_data[i].vertical_accuracy,
84                                 batch->batch_data[i].timestamp);
85
86         return batch_dup;
87 }
88
89 EXPORT_API gboolean
90 location_get_batch_details(const LocationBatch *batch, guint index,
91                         gdouble *latitude, gdouble *longitude, gdouble *altitude,
92                         gdouble *speed, gdouble *direction, gdouble *h_accuracy, gdouble *v_accuracy, guint *timestamp)
93 {
94         g_return_val_if_fail(batch, FALSE);
95         g_return_val_if_fail(latitude, FALSE);
96         g_return_val_if_fail(longitude, FALSE);
97         g_return_val_if_fail(altitude, FALSE);
98         g_return_val_if_fail(speed, FALSE);
99         g_return_val_if_fail(direction, FALSE);
100         g_return_val_if_fail(h_accuracy, FALSE);
101         g_return_val_if_fail(v_accuracy, FALSE);
102         g_return_val_if_fail(timestamp, FALSE);
103         g_return_val_if_fail(batch->batch_data, FALSE);
104
105         *latitude       = batch->batch_data[index].latitude;
106         *longitude      = batch->batch_data[index].longitude;
107         *altitude       = batch->batch_data[index].altitude;
108         *speed          = batch->batch_data[index].speed;
109         *direction      = batch->batch_data[index].direction;
110         *h_accuracy     = batch->batch_data[index].horizontal_accuracy;
111         *v_accuracy     = batch->batch_data[index].vertical_accuracy;
112         *timestamp      = batch->batch_data[index].timestamp;
113
114         return TRUE;
115 }
116
117 EXPORT_API gboolean
118 location_set_batch_details(LocationBatch *batch, guint index,
119                         gdouble latitude, gdouble longitude, gdouble altitude,
120                         gdouble speed, gdouble direction, gdouble h_accuracy, gdouble v_accuracy, guint timestamp)
121 {
122         g_return_val_if_fail(batch, FALSE);
123         g_return_val_if_fail(batch->batch_data, FALSE);
124         g_return_val_if_fail(index < batch->num_of_location, FALSE);
125
126         batch->batch_data[index].latitude = latitude;
127         batch->batch_data[index].longitude = longitude;
128         batch->batch_data[index].altitude = altitude;
129         batch->batch_data[index].speed = speed;
130         batch->batch_data[index].direction = direction;
131         batch->batch_data[index].horizontal_accuracy = h_accuracy;
132         batch->batch_data[index].vertical_accuracy = v_accuracy;
133         batch->batch_data[index].timestamp = timestamp;
134
135         return TRUE;
136 }
137
138 EXPORT_API void
139 location_batch_free(LocationBatch *batch)
140 {
141         g_return_if_fail(batch);
142         g_free(batch->batch_data);
143         g_slice_free(LocationBatch, batch);
144 }
145