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