cc57c641797486d1483a817df4371f4d93853b7d
[platform/core/location/location-module.git] / modules / osm / location-osm-route.c
1 /*
2  * location-module
3  *
4  * Copyright (c) 2010-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 <glib.h>
23 #include <json-glib.h>
24 #include <stdlib.h>
25
26 #include <location.h>
27 #include <location-map-service.h>
28 #include <location-map-service-ext.h>
29 #include "log.h"
30 #include "osm-route-yours.h"
31 #include "location-osm-route.h"
32 #include "osm-http.h"
33
34 static char *
35 _create_route_url (const LocationPosition *origin, const LocationPosition *destination, const GList *waypoint, const LocationMapPref *svc_pref, const LocationRoutePreference *pref)
36 {
37         gdouble flon = 0.0, flat = 0.0, tlon = 0.0, tlat = 0.0;
38         GList *ways = NULL;
39         char *mode = "motorcar";
40
41
42         flon = origin->longitude;
43         flat = origin->latitude;
44         tlon = destination->longitude;
45         tlat = destination->latitude;
46
47 #if 0
48         if (waypoint)
49 #endif
50
51         return yournavi_create_url (flat, flon, tlat, tlon, ways, 1, mode, 1);
52         //yournavi_create_url (gdouble flon, gdouble flat, gdouble tlon, gdouble tlat, const GList *ways, int type, char *mode, int instruction);
53
54 }
55
56 static void
57 _position_foreach_cb (gpointer data, gpointer user_data)
58 {
59         if (!data) return ;
60
61         osm_data_t *ndata = (osm_data_t *)user_data;
62
63         LocationPosition *pos = (LocationPosition*) data;
64         LocationPosition *duplicated = location_position_copy (pos);
65
66         ndata->waypoint = g_list_append(ndata->waypoint, duplicated);
67
68 }
69
70 #if 0
71 static void
72 _position_destory_cb (gpointer data)
73 {
74         if (!data) return;
75
76         LocationPosition *pos = (LocationPosition *)data;
77
78         location_position_free (pos);
79 }
80 #endif
81
82 static void
83 _get_position_from_coordinates (JsonArray *_array, guint index, JsonNode *element, gpointer user_data)
84 {
85         GList **pos_list = (GList **)user_data;
86         gdouble lat = 0.0, lon = 0.0;
87
88         JsonArray *array = json_node_get_array(element);
89         if (array) {
90                 lon = json_array_get_double_element (array, 0);
91                 lat = json_array_get_double_element (array, 1);
92                 *pos_list = g_list_append(*pos_list, location_position_new(0, lat, lon, 0.0, LOCATION_STATUS_2D_FIX));
93         }
94
95 }
96
97 int
98 convert_data_to_location_service (const char *result, int result_len, LocationPosition *origin, LocationPosition *dest, GList **route_list)
99 {
100         gboolean ret = FALSE;
101         GError *err = NULL;
102         JsonParser* parser = json_parser_new ();
103         JsonNode* node = NULL;
104         JsonObject *obj = NULL;
105         JsonObject *properties = NULL;
106         JsonArray *positions = NULL;
107 //      gchar *instructions = NULL;
108
109         GList *pos_list = NULL;
110         LocationRoute *route = NULL;
111         gchar *str_distance = NULL, *str_duration = NULL;
112         gdouble distance = 0.0;
113         glong duration = 0;
114
115         if (!result) return -1;
116
117         ret = json_parser_load_from_data(parser, result, result_len, &err);
118         if (ret == FALSE) {
119                 MOD_LOGW("Fail to parser data");
120                 return -1;
121         }
122
123         node = json_parser_get_root(parser);
124         if (!node) {
125                 MOD_LOGW("Fail to parser data");
126                 return -1;
127         }
128
129         obj = json_node_get_object(node);
130         if (!obj) {
131                 MOD_LOGW("Fail to parser data");
132                 return -1;
133         }
134
135         positions = json_object_get_array_member(obj, "coordinates");
136         if (positions) {
137                 json_array_foreach_element (positions, _get_position_from_coordinates, &pos_list);
138                 MOD_LOGW("foreach");
139         }
140
141         properties = json_object_get_object_member(obj, "properties");
142         if (properties) {
143                 str_distance  = (gchar *)json_object_get_string_member(properties, "distance");
144                 MOD_LOGW("distance [%s]", str_distance);
145         //      instructions = json_object_get_string_member (properties, "description");
146                 str_duration = (gchar *)json_object_get_string_member (properties, "traveltime");
147                 MOD_LOGW("time [%s]", str_duration);
148
149                 if (str_distance) {
150                         distance = atof (str_distance);
151                         MOD_LOGW("converted distance  [%f]", distance);
152                         g_free(str_distance);
153                 }
154
155                 if (str_duration) {
156                         duration = atol (str_duration);
157                         MOD_LOGW("converted duration  [%ld]", duration);
158                         g_free( str_duration);
159                 }
160         }
161         else {
162                 MOD_LOGW("property NULL");
163         }
164
165         LocationRouteStep *step = location_route_step_new ();
166
167         location_route_step_set_start_point (step, location_position_copy (origin));
168         location_route_step_set_end_point (step, location_position_copy (dest));
169
170
171         if (pos_list) {
172                 location_route_step_set_geometry (step, pos_list);
173         }
174         GList *step_list = NULL;
175         step_list = g_list_append (step_list, step);
176
177
178         // Route == RouteSegment
179         LocationRouteSegment *segment = location_route_segment_new();
180         location_route_segment_set_start_point (segment, location_position_copy (origin));
181         location_route_segment_set_end_point (segment, location_position_copy (dest));
182         location_route_segment_set_distance (segment, distance);
183         location_route_segment_set_duration (segment, duration);
184         location_route_segment_set_route_step (segment, step_list);
185
186         GList *seg_list = NULL;
187         seg_list = g_list_append (seg_list, segment);
188
189
190         route = location_route_new ();
191         location_route_set_origin(route, origin);
192         location_route_set_destination(route, dest);
193         location_route_set_total_distance(route, distance);
194         location_route_set_distance_unit(route, "M");
195         location_route_set_total_duration(route, duration);
196         location_route_set_route_segment(route, seg_list);
197
198         *route_list = g_list_append(*route_list, route);
199
200         return 0;
201 }
202
203 int
204 osm_request_route (const LocationPosition *origin,
205                         const LocationPosition *destination,
206                         GList *waypoint,
207                         const LocationMapPref *svc_pref,
208                         const LocationRoutePreference *pref,
209                         LocationRouteCB cb,
210                         const gpointer user_data,
211                         guint *req_id)
212 {
213         osm_data_t *ndata = g_new0(osm_data_t, 1);
214
215         ndata->route_cb = cb;
216         ndata->user_data = (gpointer)user_data;
217         ndata->origin = location_position_copy(origin);
218         ndata->dest = location_position_copy(destination);
219         if (waypoint) g_list_foreach(waypoint, _position_foreach_cb, ndata);
220
221         char *url = _create_route_url (origin, destination, waypoint, svc_pref, pref);
222         if (!url) {
223                 g_free(ndata);
224                 return LOCATION_ERROR_UNKNOWN;
225         }
226
227         int ret = curl_request_route_async(url, ndata, req_id);
228         if (ret != 0) {
229                 g_free(ndata);
230                 g_free(url);
231                 return LOCATION_ERROR_UNKNOWN;
232         }
233
234         g_free(ndata);
235         g_free(url);
236         return LOCATION_ERROR_NONE;
237 }
238
239 int
240 osm_cancel_route_request (guint req_id)
241 {
242         return curl_cancel_route_request (req_id);
243 }