User Consent API
[platform/core/location/maps-plugin-here.git] / src / here_utils.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "here_utils.h"
18 #include <common/CommunicationError.h>
19
20
21 static const double LATITUDE_RANGE = 85.05113;
22 static const double LONGITUDE_RANGE = 180.0;
23
24
25 extern "C"
26 {
27 int ConvertToMapsError(int nErr)
28 {
29         switch (nErr) {
30         case HERE_ERROR_NONE:                  return MAPS_ERROR_NONE;
31         case HERE_ERROR_PERMISSION_DENIED:     return MAPS_ERROR_PERMISSION_DENIED;
32         case HERE_ERROR_OUT_OF_MEMORY:         return MAPS_ERROR_OUT_OF_MEMORY;
33         case HERE_ERROR_INVALID_PARAMETER:     return MAPS_ERROR_INVALID_PARAMETER;
34         case HERE_ERROR_NOT_SUPPORTED:         return MAPS_ERROR_NOT_SUPPORTED;
35         case HERE_ERROR_CONNECTION_TIME_OUT:   return MAPS_ERROR_CONNECTION_TIME_OUT;
36         case HERE_ERROR_NETWORK_UNREACHABLE:   return MAPS_ERROR_NETWORK_UNREACHABLE;
37         case HERE_ERROR_INVALID_OPERATION:     return MAPS_ERROR_INVALID_OPERATION;
38         case HERE_ERROR_KEY_NOT_AVAILABLE:     return MAPS_ERROR_KEY_NOT_AVAILABLE;
39         case HERE_ERROR_RESOURCE_BUSY:         return MAPS_ERROR_RESOURCE_BUSY;
40         case HERE_ERROR_CANCELED:              return MAPS_ERROR_CANCELED;
41         case HERE_ERROR_UNKNOWN:               return MAPS_ERROR_UNKNOWN;
42         case HERE_ERROR_SERVICE_NOT_AVAILABLE: return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
43         case HERE_ERROR_NOT_FOUND:             return MAPS_ERROR_NOT_FOUND;
44         case HERE_ERROR_USER_NOT_CONSENTED:    return MAPS_ERROR_USER_NOT_CONSENTED;
45         }
46         return MAPS_ERROR_UNKNOWN;
47 }
48
49 int ConvertToHereError(int nErr)
50 {
51         switch (nErr) {
52         case MAPS_ERROR_NONE:                  return HERE_ERROR_NONE;
53         case MAPS_ERROR_PERMISSION_DENIED:     return HERE_ERROR_PERMISSION_DENIED;
54         case MAPS_ERROR_OUT_OF_MEMORY:         return HERE_ERROR_OUT_OF_MEMORY;
55         case MAPS_ERROR_INVALID_PARAMETER:     return HERE_ERROR_INVALID_PARAMETER;
56         case MAPS_ERROR_NOT_SUPPORTED:         return HERE_ERROR_NOT_SUPPORTED;
57         case MAPS_ERROR_CONNECTION_TIME_OUT:   return HERE_ERROR_CONNECTION_TIME_OUT;
58         case MAPS_ERROR_NETWORK_UNREACHABLE:   return HERE_ERROR_NETWORK_UNREACHABLE;
59         case MAPS_ERROR_INVALID_OPERATION:     return HERE_ERROR_INVALID_OPERATION;
60         case MAPS_ERROR_KEY_NOT_AVAILABLE:     return HERE_ERROR_KEY_NOT_AVAILABLE;
61         case MAPS_ERROR_RESOURCE_BUSY:         return HERE_ERROR_RESOURCE_BUSY;
62         case MAPS_ERROR_CANCELED:              return HERE_ERROR_CANCELED;
63         case MAPS_ERROR_UNKNOWN:               return HERE_ERROR_UNKNOWN;
64         case MAPS_ERROR_SERVICE_NOT_AVAILABLE: return HERE_ERROR_SERVICE_NOT_AVAILABLE;
65         case MAPS_ERROR_NOT_FOUND:             return HERE_ERROR_NOT_FOUND;
66         case MAPS_ERROR_USER_NOT_CONSENTED:    return HERE_ERROR_USER_NOT_CONSENTED;
67         }
68         return HERE_ERROR_UNKNOWN;
69 }
70
71 const char* ConverHereErrorToString(int nErr)
72 {
73         switch (nErr) {
74         case HERE_ERROR_NONE:                  return "No errors";
75         case HERE_ERROR_PERMISSION_DENIED:     return "Permission denied";
76         case HERE_ERROR_OUT_OF_MEMORY:         return "Out of memory";
77         case HERE_ERROR_INVALID_PARAMETER:     return "Invalid Parameter";
78         case HERE_ERROR_NOT_SUPPORTED:         return "Not suppoerted";
79         case HERE_ERROR_CONNECTION_TIME_OUT:   return "Connection time out";
80         case HERE_ERROR_NETWORK_UNREACHABLE:   return "Network unreachable";
81         case HERE_ERROR_INVALID_OPERATION:     return "Invalid operation";
82         case HERE_ERROR_KEY_NOT_AVAILABLE:     return "Key not available";
83         case HERE_ERROR_RESOURCE_BUSY:         return "Resource busy";
84         case HERE_ERROR_CANCELED:              return "Canceled";
85         case HERE_ERROR_UNKNOWN:               return "Unknown";
86         case HERE_ERROR_SERVICE_NOT_AVAILABLE: return "Service not available";
87         case HERE_ERROR_NOT_FOUND:             return "Not found";
88         case HERE_ERROR_USER_NOT_CONSENTED:    return "Not consented";
89         }
90         return "Unknown";
91 }
92
93 const char* ConvertMapsErrorToChar(int nErr)
94 {
95         switch (nErr) {
96         case MAPS_ERROR_NONE:                  return "MAPS_ERROR_NONE";
97         case MAPS_ERROR_PERMISSION_DENIED:     return "MAPS_ERROR_PERMISSION_DENIED";
98         case MAPS_ERROR_OUT_OF_MEMORY:         return "MAPS_ERROR_OUT_OF_MEMORY";
99         case MAPS_ERROR_INVALID_PARAMETER:     return "MAPS_ERROR_INVALID_PARAMETER";
100         case MAPS_ERROR_NOT_SUPPORTED:         return "MAPS_ERROR_NOT_SUPPORTED";
101         case MAPS_ERROR_CONNECTION_TIME_OUT:   return "MAPS_ERROR_CONNECTION_TIME_OUT";
102         case MAPS_ERROR_NETWORK_UNREACHABLE:   return "MAPS_ERROR_NETWORK_UNREACHABLE";
103         case MAPS_ERROR_INVALID_OPERATION:     return "MAPS_ERROR_INVALID_OPERATION";
104         case MAPS_ERROR_KEY_NOT_AVAILABLE:     return "MAPS_ERROR_KEY_NOT_AVAILABLE";
105         case MAPS_ERROR_RESOURCE_BUSY:         return "MAPS_ERROR_RESOURCE_BUSY";
106         case MAPS_ERROR_CANCELED:              return "MAPS_ERROR_CANCELED";
107         case MAPS_ERROR_UNKNOWN:               return "MAPS_ERROR_UNKNOWN";
108         case MAPS_ERROR_SERVICE_NOT_AVAILABLE: return "MAPS_ERROR_SERVICE_NOT_AVAILABLE";
109         case MAPS_ERROR_NOT_FOUND:             return "MAPS_ERROR_NOT_FOUND";
110         case MAPS_ERROR_USER_NOT_CONSENTED:    return "MAPS_ERROR_USER_NOT_CONSENTED";
111         }
112         return "MAPS_ERROR_UNKNOWN";
113 }
114 }
115
116
117 HERE_PLUGIN_BEGIN_NAMESPACE
118
119 HereUtils::HereUtils()
120 {
121 }
122
123 HereUtils::~HereUtils()
124 {
125 }
126
127 GeoRouteQuery::TravelMode HereUtils::Convert(maps_route_transport_mode_e nVal)
128 {
129         switch (nVal) {
130         case MAPS_ROUTE_TRANSPORT_MODE_CAR:           return GeoRouteQuery::TM_CarTravel;
131         case MAPS_ROUTE_TRANSPORT_MODE_PEDESTRIAN:    return GeoRouteQuery::TM_PedestrianTravel;
132         case MAPS_ROUTE_TRANSPORT_MODE_PUBLICTRANSIT: return GeoRouteQuery::TM_PublicTransitTravel;
133         default: break;
134         }
135         return GeoRouteQuery::TM_CarTravel;
136 }
137
138 maps_route_transport_mode_e HereUtils::Convert(GeoRouteQuery::TravelMode nVal)
139 {
140         switch (nVal) {
141         case GeoRouteQuery::TM_CarTravel:           return MAPS_ROUTE_TRANSPORT_MODE_CAR;
142         case GeoRouteQuery::TM_PedestrianTravel:    return MAPS_ROUTE_TRANSPORT_MODE_PEDESTRIAN;
143         case GeoRouteQuery::TM_PublicTransitTravel: return MAPS_ROUTE_TRANSPORT_MODE_PUBLICTRANSIT;
144         default: break;
145         }
146         return MAPS_ROUTE_TRANSPORT_MODE_CAR;
147 }
148
149 GeoRouteQuery::FeatureType HereUtils::Convert(maps_route_feature_e nVal)
150 {
151         switch (nVal) {
152         case MAPS_ROUTE_FEATURE_NO:             return GeoRouteQuery::FT_NoFeature;
153         case MAPS_ROUTE_FEATURE_TOLL:           return GeoRouteQuery::FT_TollFeature;
154         case MAPS_ROUTE_FEATURE_MOTORWAY:       return GeoRouteQuery::FT_MotorwayFeature;
155         case MAPS_ROUTE_FEATURE_BOATFERRY:      return GeoRouteQuery::FT_BoatFerryFeature;
156         case MAPS_ROUTE_FEATURE_RAILFERRY:      return GeoRouteQuery::FT_RailFerryFeature;
157         case MAPS_ROUTE_FEATURE_PUBLICTTRANSIT: return GeoRouteQuery::FT_PublicTransitFeature;
158         case MAPS_ROUTE_FEATURE_TUNNEL:         return GeoRouteQuery::FT_TunnelFeature;
159         case MAPS_ROUTE_FEATURE_DIRTROAD:       return GeoRouteQuery::FT_DirtRoadFeature;
160         case MAPS_ROUTE_FEATURE_PARKS:          return GeoRouteQuery::FT_ParksFeature;
161         case MAPS_ROUTE_FEATURE_HOVLANE:        return GeoRouteQuery::FT_HOVLane;
162         case MAPS_ROUTE_FEATURE_STAIRS:         return GeoRouteQuery::FT_Stairs;
163         default: break;
164         }
165         return GeoRouteQuery::FT_NoFeature;
166 }
167
168 GeoRouteQuery::FeatureWeight HereUtils::Convert(maps_route_feature_weight_e nVal)
169 {
170         switch (nVal) {
171         case MAPS_ROUTE_FEATURE_WEIGHT_NORMAL:        return GeoRouteQuery::FW_NormalFeatureWeight;
172         case MAPS_ROUTE_FEATURE_WEIGHT_PREFER:        return GeoRouteQuery::FW_PreferFeatureWeight;
173         case MAPS_ROUTE_FEATURE_WEIGHT_AVOID:         return GeoRouteQuery::FW_AvoidFeatureWeight;
174         case MAPS_ROUTE_FEATURE_WEIGHT_SOFTEXCLUDE:   return GeoRouteQuery::FW_SoftExcludeFeatureWeight;
175         case MAPS_ROUTE_FEATURE_WEIGHT_STRICTEXCLUDE: return GeoRouteQuery::FW_StrictExcludeFeatureWeight;
176         default: break;
177         }
178         return GeoRouteQuery::FW_NormalFeatureWeight;
179 }
180
181 maps_route_turn_type_e HereUtils::Convert(Maneuver::InstructionDirection nVal)
182 {
183         switch (nVal) {
184         case Maneuver::ID_NoDirection:          return MAPS_ROUTE_TURN_TYPE_NONE;
185         case Maneuver::ID_DirectionForward:     return MAPS_ROUTE_TURN_TYPE_STRAIGHT;
186         case Maneuver::ID_DirectionBearRight:   return MAPS_ROUTE_TURN_TYPE_BEAR_RIGHT;
187         case Maneuver::ID_DirectionLightRight:  return MAPS_ROUTE_TURN_TYPE_LIGHT_RIGHT;
188         case Maneuver::ID_DirectionRight:       return MAPS_ROUTE_TURN_TYPE_RIGHT;
189         case Maneuver::ID_DirectionHardRight:   return MAPS_ROUTE_TURN_TYPE_HARD_RIGHT;
190         case Maneuver::ID_DirectionUTurnRight:  return MAPS_ROUTE_TURN_TYPE_UTURN_RIGHT;
191         case Maneuver::ID_DirectionUTurnLeft:   return MAPS_ROUTE_TURN_TYPE_UTURN_LEFT;
192         case Maneuver::ID_DirectionHardLeft:    return MAPS_ROUTE_TURN_TYPE_HARD_LEFT;
193         case Maneuver::ID_DirectionLeft:        return MAPS_ROUTE_TURN_TYPE_LEFT;
194         case Maneuver::ID_DirectionLightLeft:   return MAPS_ROUTE_TURN_TYPE_LIGHT_LEFT;
195         case Maneuver::ID_DirectionBearLeft:    return MAPS_ROUTE_TURN_TYPE_BEAR_LEFT;
196         default: break;
197         }
198         return MAPS_ROUTE_TURN_TYPE_NONE;
199 }
200
201 Maneuver::InstructionDirection HereUtils::Convert(maps_route_turn_type_e nVal)
202 {
203         switch (nVal) {
204         case MAPS_ROUTE_TURN_TYPE_NONE:         return Maneuver::ID_NoDirection;
205         case MAPS_ROUTE_TURN_TYPE_STRAIGHT:     return Maneuver::ID_DirectionForward;
206         case MAPS_ROUTE_TURN_TYPE_BEAR_RIGHT:   return Maneuver::ID_DirectionBearRight;
207         case MAPS_ROUTE_TURN_TYPE_LIGHT_RIGHT:  return Maneuver::ID_DirectionLightRight;
208         case MAPS_ROUTE_TURN_TYPE_RIGHT:        return Maneuver::ID_DirectionRight;
209         case MAPS_ROUTE_TURN_TYPE_HARD_RIGHT:   return Maneuver::ID_DirectionHardRight;
210         case MAPS_ROUTE_TURN_TYPE_UTURN_RIGHT:  return Maneuver::ID_DirectionUTurnRight;
211         case MAPS_ROUTE_TURN_TYPE_UTURN_LEFT:   return Maneuver::ID_DirectionUTurnLeft;
212         case MAPS_ROUTE_TURN_TYPE_HARD_LEFT:    return Maneuver::ID_DirectionHardLeft;
213         case MAPS_ROUTE_TURN_TYPE_LEFT:         return Maneuver::ID_DirectionLeft;
214         case MAPS_ROUTE_TURN_TYPE_LIGHT_LEFT:   return Maneuver::ID_DirectionLightLeft;
215         case MAPS_ROUTE_TURN_TYPE_BEAR_LEFT:    return Maneuver::ID_DirectionBearLeft;
216         default: break;
217         }
218         return Maneuver::ID_NoDirection;
219 }
220
221 GeoTiledMap::MapType HereUtils::Convert(maps_view_type_e nMapType, bool bBuildings, bool bTraffic, bool bPublicTransit)
222 {
223         switch (nMapType) {
224         case MAPS_VIEW_TYPE_NORMAL:
225                 if (bPublicTransit)     return GeoTiledMap::MT_Normal_Day_Transit;
226                 if (bTraffic)           return GeoTiledMap::MT_Normal_Traffic_Day;
227                 return GeoTiledMap::MT_Normal_Day;
228         case MAPS_VIEW_TYPE_SATELLITE:
229                 return GeoTiledMap::MT_Satellite_Day;
230         case MAPS_VIEW_TYPE_TERRAIN:
231                 return GeoTiledMap::MT_Terrain_Day;
232         case MAPS_VIEW_TYPE_HYBRID:
233                 if (bPublicTransit)     return GeoTiledMap::MT_Hybrid_Day_Transit;
234                 if (bTraffic)           return GeoTiledMap::MT_Hybrid_Traffic_Day;
235                 return GeoTiledMap::MT_Hybrid_Day;
236         default: break;
237         }
238         return GeoTiledMap::MT_Normal_Day;
239 }
240
241 maps_error_e HereUtils::ConvertHttpCodeToMapsError(int nVal)
242 {
243         switch (nVal) {
244         case 200:/*Ok*/                         return MAPS_ERROR_NONE;
245         case 408:/*Request timeout*/
246         case 504:/*Gateway timeout*/
247         case 598:/*Network reading timeout*/
248         case 599:/*Network connection timeout*/ return MAPS_ERROR_CONNECTION_TIME_OUT;
249
250         case 404:/*Not found*/
251         case 407:/*Proxy auth. required*/
252         case 502:/*Bad gateway*/                return MAPS_ERROR_NETWORK_UNREACHABLE;
253
254         case 401:/*Unauthorized*/
255         case 402:/*Payment required*/           return MAPS_ERROR_KEY_NOT_AVAILABLE;
256
257         case 405:/*Method not allowed*/
258         case 413:/*Request entity too larget*/
259         case 414:/*Request uri too large*/      return MAPS_ERROR_INVALID_OPERATION;
260
261         case 403:/*Forbidden*/
262         case 500:/*Server internal error*/
263         case 501:/*Not implemented*/
264         case 503:/*Service unavailable*/        return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
265         }
266
267         if (nVal > 0 && nVal < 100) // curl error code
268                 return MAPS_ERROR_NETWORK_UNREACHABLE;
269
270         if (nVal >= 400 && nVal < 500) // http code 4xx (client-side error)
271                 return MAPS_ERROR_INVALID_OPERATION;
272
273         if (nVal >= 500 && nVal < 600) // http code 5xx (server-side error)
274                 return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
275
276         return MAPS_ERROR_UNKNOWN;
277 }
278
279 GeoBoundingBox& HereUtils::Convert(maps_area_h hArea, GeoBoundingBox& Box)
280 {
281         maps_area_s* area_s = (maps_area_s*)hArea;
282
283         if (!area_s || area_s->type != MAPS_AREA_RECTANGLE) return Box;
284
285         GeoCoordinates hereCoordLT(area_s->rect.top_left.latitude, area_s->rect.top_left.longitude);
286         GeoCoordinates hereCoordRB(area_s->rect.bottom_right.latitude, area_s->rect.bottom_right.longitude);
287
288         Box.SetTopLeft(hereCoordLT);
289         Box.SetBottomRight(hereCoordRB);
290
291         return Box;
292 }
293
294 maps_area_h& HereUtils::Convert(GeoBoundingBox Box, maps_area_h& hArea)
295 {
296         maps_coordinates_h mapsCoordLT, mapsCoordRB;
297         GeoCoordinates hereCoordLT, hereCoordRB;
298
299         hereCoordLT = Box.GetTopLeft();
300         hereCoordRB = Box.GetBottomRight();
301
302         maps_coordinates_create(hereCoordLT.GetLatitude(), hereCoordLT.GetLongitude(), &mapsCoordLT);
303         maps_coordinates_create(hereCoordRB.GetLatitude(), hereCoordRB.GetLongitude(), &mapsCoordRB);
304
305         maps_area_create_rectangle(mapsCoordLT, mapsCoordRB, &hArea);
306
307         maps_coordinates_destroy(mapsCoordLT);
308         maps_coordinates_destroy(mapsCoordRB);
309
310         return hArea;
311 }
312
313 GeoBoundingCircle& HereUtils::Convert(maps_area_h hArea, GeoBoundingCircle& circle)
314 {
315         maps_area_s* area_s = (maps_area_s*)hArea;
316
317         if (!area_s || area_s->type != MAPS_AREA_CIRCLE) return circle;
318
319         GeoCoordinates hereCoord(area_s->circle.center.latitude, area_s->circle.center.longitude);
320
321         circle.SetCenter(hereCoord);
322         circle.SetRadius(area_s->circle.radius);
323
324         return circle;
325 }
326
327 void HereUtils::Convert(String strUtf8, WString& strUtf16)
328 {
329         strUtf16.assign(strUtf8.begin(), strUtf8.end());
330 }
331
332 void HereUtils::Convert(WString strUtf16, String& strUtf8)
333 {
334         strUtf8.assign(strUtf16.begin(), strUtf16.end());
335 }
336
337 GeoBoundingBox& HereUtils::Convert(const char *src, GeoBoundingBox &box)
338 {
339         int i = 0;
340         char *token, *next;
341         double coord[4] = { 0.0, };
342
343         token = strtok_r((char*)src, ",;", &next);
344         while (token && i < 4) {
345                 coord[i++] = atof(token);
346                 token = strtok_r(NULL, ",;", &next);
347         }
348         box.SetTopLeft(GeoCoordinates(coord[0], coord[1]));
349         box.SetBottomRight(GeoCoordinates(coord[2], coord[3]));
350         return box;
351 }
352
353 bool HereUtils::IsValid(GeoCoordinates geoCoord)
354 {
355         return IsValidCoord(geoCoord.GetLatitude(), geoCoord.GetLongitude());
356 }
357
358 bool HereUtils::IsValid(maps_coordinates_s geoCoord)
359 {
360         return IsValidCoord(geoCoord.latitude, geoCoord.longitude);
361 }
362
363 bool HereUtils::IsValidCoord(double dLat, double dLng)
364 {
365         return ((dLat <= LATITUDE_RANGE && dLat >= -LATITUDE_RANGE) &&
366                 (dLng <= LONGITUDE_RANGE && dLng >= -LONGITUDE_RANGE));
367 }
368
369 bool HereUtils::IsValid(maps_area_s hArea)
370 {
371         if (hArea.type == MAPS_AREA_RECTANGLE) {
372                 return (HereUtils::IsValid(hArea.rect.top_left) &&
373                         HereUtils::IsValid(hArea.rect.bottom_right));
374         } else if (hArea.type == MAPS_AREA_CIRCLE) {
375                 return HereUtils::IsValid(hArea.circle.center);
376         } else {
377                 return false;
378         }
379 }
380
381 const double HereUtils::ConvertDistance(const double originValue, maps_distance_unit_e destUnit)
382 {
383         return ConvertDistance(originValue, MAPS_DISTANCE_UNIT_M, destUnit);
384 }
385
386 const double HereUtils::ConvertDistance(const double originValue, maps_distance_unit_e originUnit, maps_distance_unit_e destUnit)
387 {
388         double meterConstant[MAPS_DISTANCE_UNIT_YD+1] = { 1.0, 0.001, 3.2808399, 1.0936133 };
389         return originValue / meterConstant[originUnit] * meterConstant[destUnit];
390 }
391
392 HERE_PLUGIN_END_NAMESPACE