tizen beta release
[framework/location/gps-manager.git] / gps-manager / last_position.c
1 /*
2  * gps-manager
3  *
4  * Copyright (c) 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7  *          Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <math.h>
26
27 #include "gps_manager_data_types.h"
28 #include "last_position.h"
29 #include "debug_util.h"
30 #include "setting.h"
31
32 static void gps_manager_set_last_position(last_pos_t * last_pos)
33 {
34         LOG_GPS(DBG_LOW, "set Last Latitude = %f Longitude = %f Altitude = %f H_Accuracy = %f V_Accuracy = %f",
35                    last_pos->latitude, last_pos->longitude, last_pos->altitude, last_pos->hor_accuracy, last_pos->ver_accuracy);
36
37         setting_set_int(LAST_TIMESTAMP, last_pos->timestamp);
38         setting_set_double(LAST_LATITUDE, last_pos->latitude);
39         setting_set_double(LAST_LONGITUDE, last_pos->longitude);
40         setting_set_double(LAST_ALTITUDE, last_pos->altitude);
41         setting_set_double(LAST_HOR_ACCURACY, last_pos->hor_accuracy);
42         setting_set_double(LAST_VER_ACCURACY, last_pos->ver_accuracy);
43 }
44
45 double deg2rad(double deg)
46 {
47         return (deg * M_PI / 180);
48 }
49
50 void gps_manager_get_last_position(last_pos_t * last_pos)
51 {
52         setting_get_int(LAST_TIMESTAMP, &last_pos->timestamp);
53         setting_get_double(LAST_LATITUDE, &last_pos->latitude);
54         setting_get_double(LAST_LONGITUDE, &last_pos->longitude);
55         setting_get_double(LAST_ALTITUDE, &last_pos->altitude);
56         setting_get_double(LAST_HOR_ACCURACY, &last_pos->hor_accuracy);
57         setting_get_double(LAST_VER_ACCURACY, &last_pos->ver_accuracy);
58
59         LOG_GPS(DBG_LOW, "get Last Latitude = %f Longitude = %f Altitude = %f H_Accuracy = %f V_Accuracy = %f",
60                    last_pos->latitude, last_pos->longitude, last_pos->altitude, last_pos->hor_accuracy, last_pos->ver_accuracy);
61 }
62
63 int gps_manager_distance_to_last_position(const pos_data_t * pos, const last_pos_t * last_pos)
64 {
65         double delta_lat, delta_long;
66         double dist;
67
68         delta_lat = pos->latitude - last_pos->latitude;
69         delta_long = pos->longitude - last_pos->longitude;
70
71         dist = sin(deg2rad(delta_lat) / 2) * sin(deg2rad(delta_lat) / 2) +
72             cos(deg2rad(pos->latitude)) * cos(deg2rad(last_pos->latitude)) *
73             sin(deg2rad(delta_long) / 2) * sin(deg2rad(delta_long) / 2);
74         dist = 2 * atan2(sqrt(dist), sqrt(1 - dist));
75         dist = 6371 * dist;     // unit: 'km'
76
77         if (dist > 0.3) {
78                 return 0;
79         } else {
80                 return -1;
81         }
82 }
83
84 void gps_manager_update_last_position(const pos_data_t * pos, last_pos_t * last_pos)
85 {
86         last_pos->timestamp = pos->timestamp;
87         last_pos->latitude = pos->latitude;
88         last_pos->longitude = pos->longitude;
89         last_pos->altitude = pos->altitude;
90         last_pos->hor_accuracy = pos->hor_accuracy;
91         last_pos->ver_accuracy = pos->ver_accuracy;
92
93         gps_manager_set_last_position(last_pos);
94 }