tizen 2.4 release
[framework/location/maps-plugin-here.git] / src / here / here_api.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_api.h"
18 #include "here_types.h"
19 #include "here_geocode.h"
20 #include "here_revgeocode.h"
21 #include "here_place.h"
22 #include "here_route.h"
23 #include <common/HereConfig.h>
24
25
26 using namespace HERE_PLUGIN_NAMESPACE_PREFIX;
27
28 int HerePluginInit(maps_plugin_h *hPlugin)
29 {
30         if (!hPlugin)
31                 return HERE_ERROR_INVALID_PARAMETER;
32
33         HereManager::Create();
34
35         if (!HereManager::GetHandler())
36                 return HERE_ERROR_INVALID_OPERATION;
37
38         HereManager::GetHandler()->SetProxyAddress();
39
40         return HERE_ERROR_NONE;
41 }
42
43 int HerePluginShutdown(maps_plugin_h hPlugin)
44 {
45         if (!hPlugin)
46                 return HERE_ERROR_INVALID_PARAMETER;
47
48         if (HereManager::GetHandler())
49         {
50                 HereManager::GetHandler()->TerminateAllServices();
51                 HereManager::GetHandler()->Close();
52         }
53
54         return HERE_ERROR_NONE;
55 }
56
57 int HerePluginSetProviderKey(const char* szKey)
58 {
59         if (!szKey)
60                 return HERE_ERROR_INVALID_PARAMETER;
61
62         if (!HereManager::GetHandler())
63                 return HERE_ERROR_INVALID_OPERATION;
64
65         here_error_e error = HereManager::GetHandler()->SetCredentials(szKey);
66
67         return error;
68 }
69
70 int HerePluginGetProviderKey(char** szKey)
71 {
72         if (!szKey)
73                 return HERE_ERROR_INVALID_PARAMETER;
74
75         if (!HereManager::GetHandler())
76                 return HERE_ERROR_INVALID_OPERATION;
77
78         here_error_e error = HereManager::GetHandler()->GetCredentials(szKey);
79
80         return error;
81 }
82
83 int HerePluginSetPreference(maps_preference_h hPref)
84 {
85         if (!hPref)
86                 return HERE_ERROR_INVALID_PARAMETER;
87
88         if (!HereManager::GetHandler())
89                 return HERE_ERROR_INVALID_OPERATION;
90
91         here_error_e error = HereManager::GetHandler()->SetPreference(hPref);
92
93         return error;
94 }
95
96 int HerePluginGetPreference(maps_preference_h *hPref)
97 {
98         if (!hPref)
99                 return HERE_ERROR_INVALID_PARAMETER;
100
101         if (!HereManager::GetHandler())
102                 return HERE_ERROR_INVALID_OPERATION;
103
104         here_error_e error = HereManager::GetHandler()->GetPreference(hPref);
105
106         return error;
107 }
108
109 int HerePluginGeocode(const char* szAddr,
110         maps_item_hashtable_h hPref, maps_service_geocode_cb pCbFunc,
111         void *pUserData, int *nReqId)
112 {
113         /* checking parmaters */
114         if (!szAddr || (szAddr && *szAddr == '\0') || !pCbFunc || !nReqId)
115                 return HERE_ERROR_INVALID_PARAMETER;
116
117         if (!HereManager::GetHandler())
118                 return HERE_ERROR_INVALID_OPERATION;
119
120         /* creating instance */
121         HereGeocode *pGeocode =
122                 (HereGeocode*)(HereManager::GetHandler()->CreateInstance(HereManager::HERE_SVC_GEOCODE,
123                 (void*)pCbFunc, pUserData, nReqId));
124
125         if(!pGeocode)
126                 return HERE_ERROR_SERVICE_NOT_AVAILABLE;
127
128         /* sending request */
129         here_error_e error = HERE_ERROR_NONE;
130
131         do {
132                 error = pGeocode->PrepareQuery();
133                 if (error != HERE_ERROR_NONE) break;
134
135                 error = pGeocode->PreparePreference(hPref);
136
137                 error = pGeocode->StartGeocode(szAddr);
138         } while(0);
139
140         /* finishing task */
141         if(error != HERE_ERROR_NONE)
142                 pGeocode->TerminateService();
143
144         return error;
145 }
146
147 int HerePluginGeocodeByStructuredAddress(const maps_address_h hAddr,
148         maps_item_hashtable_h hPref, maps_service_geocode_cb pCbFunc,
149         void *pUserData, int *nReqId)
150 {
151         /* checking parmaters */
152         if (!hAddr || !pCbFunc || !nReqId)
153                 return HERE_ERROR_INVALID_PARAMETER;
154
155         if (!HereManager::GetHandler())
156                 return HERE_ERROR_INVALID_OPERATION;
157
158         /* creating instance */
159         HereGeocode *pGeocode =
160                 (HereGeocode*)(HereManager::GetHandler()->CreateInstance(HereManager::HERE_SVC_GEOCODE,
161                 (void*)pCbFunc, pUserData, nReqId));
162
163         if(!pGeocode)
164                 return HERE_ERROR_SERVICE_NOT_AVAILABLE;
165
166         /* sending request */
167         here_error_e error = HERE_ERROR_NONE;
168
169         do {
170                 error = pGeocode->PrepareQuery();
171                 if (error != HERE_ERROR_NONE) break;
172
173                 error = pGeocode->PreparePreference(hPref);
174
175                 error = pGeocode->StartGeocodeByStructuredAddress(hAddr);
176         } while(0);
177
178         /* finishing task */
179         if(error != HERE_ERROR_NONE)
180                 pGeocode->TerminateService();
181
182         return error;
183 }
184
185 int HerePluginGeocodeInsideArea(const char* szAddr, maps_area_h hArea,
186         maps_item_hashtable_h hPref, maps_service_geocode_cb pCbFunc,
187         void *pUserData, int *nReqId)
188 {
189         /* checking parmaters */
190         if (!szAddr || (szAddr && *szAddr == '\0') || !pCbFunc || !nReqId)
191                 return HERE_ERROR_INVALID_PARAMETER;
192
193         if (!hArea || !HereUtils::IsValid(*(maps_area_s*)hArea))
194                 return HERE_ERROR_INVALID_PARAMETER;
195
196         if (!HereManager::GetHandler())
197                 return HERE_ERROR_INVALID_OPERATION;
198
199         /* creating instance */
200         HereGeocode *pGeocode =
201                 (HereGeocode*)(HereManager::GetHandler()->CreateInstance(HereManager::HERE_SVC_GEOCODE,
202                 (void*)pCbFunc, pUserData, nReqId));
203
204         if(!pGeocode)
205                 return HERE_ERROR_SERVICE_NOT_AVAILABLE;
206
207         /* sending request */
208         here_error_e error = HERE_ERROR_NONE;
209
210         do {
211                 error = pGeocode->PrepareQuery();
212                 if (error != HERE_ERROR_NONE) break;
213
214                 error = pGeocode->PreparePreference(hPref);
215
216                 error = pGeocode->StartGeocodeInsideArea(szAddr, hArea);
217         } while(0);
218
219         /* finishing task */
220         if(error != HERE_ERROR_NONE)
221                 pGeocode->TerminateService();
222
223         return error;
224 }
225
226 int HerePluginReverseGeocode(double dLatitude, double dLongitude,
227         maps_item_hashtable_h hPref, maps_service_reverse_geocode_cb pCbFunc,
228         void *pUserData, int *nReqId)
229 {
230         /* checking parmaters */
231         if (!HereUtils::IsValidCoord(dLatitude, dLongitude))
232                 return HERE_ERROR_INVALID_PARAMETER;
233
234         if (!pCbFunc || !nReqId)
235                 return HERE_ERROR_INVALID_PARAMETER;
236
237         if (!HereManager::GetHandler())
238                 return HERE_ERROR_INVALID_OPERATION;
239
240         /* creating instance */
241         HereRevGeocode *pRevGeocode =
242                 (HereRevGeocode*)(HereManager::GetHandler()->CreateInstance(HereManager::HERE_SVC_REV_GEOCODE,
243                 (void*)pCbFunc, pUserData, nReqId));
244
245         if(!pRevGeocode)
246                 return HERE_ERROR_SERVICE_NOT_AVAILABLE;
247
248         /* sending request */
249         here_error_e error = HERE_ERROR_UNKNOWN;
250
251         do {
252                 error = pRevGeocode->PrepareQuery();
253                 if (error != HERE_ERROR_NONE) break;
254
255                 error = pRevGeocode->PreparePreference(hPref);
256
257                 error = pRevGeocode->PreparePosition(dLatitude, dLongitude);
258                 if (error != HERE_ERROR_NONE) break;
259
260                 error = pRevGeocode->StartRevGeocode(hPref);
261         } while(0);
262
263         /* finishing task */
264         if(error != HERE_ERROR_NONE)
265                 pRevGeocode->TerminateService();
266
267         return error;
268 }
269
270 int HerePluginSearchPlace(maps_coordinates_h hPos, int nDistance,
271         maps_item_hashtable_h hPref, maps_place_filter_h hFilter, maps_service_search_place_cb pCbFunc,
272         void *pUserData, int *nReqId)
273 {
274         /* checking parmaters */
275         if (!hPos || !HereUtils::IsValid(*(maps_coordinates_s*)hPos) || nDistance <= 0)
276                 return HERE_ERROR_INVALID_PARAMETER;
277
278         if (!hFilter || !pCbFunc || !nReqId)
279                 return HERE_ERROR_INVALID_PARAMETER;
280
281         if (!HereManager::GetHandler())
282                 return HERE_ERROR_INVALID_OPERATION;
283
284         /* creating instance */
285         HerePlace *pPlace =
286                 (HerePlace*)(HereManager::GetHandler()->CreateInstance(HereManager::HERE_SVC_PLACE,
287                 (void*)pCbFunc, pUserData, nReqId));
288
289         if(!pPlace)
290                 return HERE_ERROR_SERVICE_NOT_AVAILABLE;
291
292         /* sending request */
293         here_error_e error = HERE_ERROR_NONE;
294
295         do {
296                 error = pPlace->PrepareDiscoveryQuery();
297                 if (error != HERE_ERROR_NONE) break;
298
299                 error = pPlace->PrepareDiscoveryPreference(hPref);
300
301                 error = pPlace->PrepareDiscoveryFilter(hFilter);
302                 if (error != HERE_ERROR_NONE) break;
303
304                 error = pPlace->StartDiscoveryPlace(hPos, nDistance);
305         } while(0);
306
307         /* finishing task */
308         if(error != HERE_ERROR_NONE)
309                 pPlace->TerminateService();
310
311         return error;
312 }
313
314 int HerePluginSearchPlaceByArea(maps_area_h hArea,
315         maps_item_hashtable_h hPref, maps_place_filter_h hFilter, maps_service_search_place_cb pCbFunc,
316         void *pUserData, int *nReqId)
317 {
318         /* checking parmaters */
319         if (!hArea || !HereUtils::IsValid(*(maps_area_s*)hArea))
320                 return HERE_ERROR_INVALID_PARAMETER;
321
322         if (!hFilter || !pCbFunc || !nReqId)
323                 return HERE_ERROR_INVALID_PARAMETER;
324
325         if (!HereManager::GetHandler())
326                 return HERE_ERROR_INVALID_OPERATION;
327
328         /* creating instance */
329         HerePlace *pPlace =
330                 (HerePlace*)(HereManager::GetHandler()->CreateInstance(HereManager::HERE_SVC_PLACE,
331                 (void*)pCbFunc, pUserData, nReqId));
332
333         if(!pPlace)
334                 return HERE_ERROR_SERVICE_NOT_AVAILABLE;
335
336         /* sending request */
337         here_error_e error = HERE_ERROR_NONE;
338
339         do {
340                 error = pPlace->PrepareDiscoveryQuery();
341                 if (error != HERE_ERROR_NONE) break;
342
343                 error = pPlace->PrepareDiscoveryPreference(hPref);
344
345                 error = pPlace->PrepareDiscoveryFilter(hFilter);
346                 if (error != HERE_ERROR_NONE) break;
347
348                 error = pPlace->StartDiscoveryPlaceByArea(hArea);
349         } while(0);
350
351         /* finishing task */
352         if(error != HERE_ERROR_NONE)
353                 pPlace->TerminateService();
354
355         return error;
356 }
357
358 int HerePluginSearchPlaceByAddress(const char* szAddr, maps_area_h hArea,
359         maps_item_hashtable_h hPref, maps_place_filter_h hFilter, maps_service_search_place_cb pCbFunc,
360         void * pUserData, int *nReqId)
361 {
362         /* checking parmaters */
363         if (!szAddr || (szAddr && *szAddr == '\0') || !hFilter || !pCbFunc || !nReqId)
364                 return HERE_ERROR_INVALID_PARAMETER;
365
366         if (!hArea || !HereUtils::IsValid(*(maps_area_s*)hArea))
367                 return HERE_ERROR_INVALID_PARAMETER;
368
369         if (!HereManager::GetHandler())
370                 return HERE_ERROR_INVALID_OPERATION;
371
372         /* creating instance */
373         HerePlace *pPlace =
374                 (HerePlace*)(HereManager::GetHandler()->CreateInstance(HereManager::HERE_SVC_PLACE,
375                 (void*)pCbFunc, pUserData, nReqId));
376
377         if(!pPlace)
378                 return HERE_ERROR_SERVICE_NOT_AVAILABLE;
379
380         /* sending request */
381         here_error_e error = HERE_ERROR_NONE;
382
383         do {
384                 error = pPlace->PrepareDiscoveryQuery();
385                 if (error != HERE_ERROR_NONE) break;
386
387                 error = pPlace->PrepareDiscoveryPreference(hPref);
388
389                 error = pPlace->PrepareDiscoveryFilter(hFilter);
390                 if (error != HERE_ERROR_NONE) break;
391
392                 error = pPlace->StartDiscoveryPlaceByAddress(szAddr, hArea);
393         } while(0);
394
395         /* finishing task */
396         if(error != HERE_ERROR_NONE)
397                 pPlace->TerminateService();
398
399         return error;
400 }
401
402 int HerePluginSearchPlaceDetails(const char* szUrl,
403         maps_item_hashtable_h hPref, maps_service_search_place_cb pCbFunc,
404         void *pUserData, int *nReqId)
405 {
406         /* checking parmaters */
407         if (!szUrl || (szUrl && *szUrl == '\0') || !pCbFunc || !nReqId)
408                 return HERE_ERROR_INVALID_PARAMETER;
409
410         if (!HereManager::GetHandler())
411                 return HERE_ERROR_INVALID_OPERATION;
412
413         /* creating instance */
414         HerePlace *pPlace =
415                 (HerePlace*)(HereManager::GetHandler()->CreateInstance(HereManager::HERE_SVC_PLACE,
416                 (void*)pCbFunc, pUserData, nReqId));
417
418         if(!pPlace)
419                 return HERE_ERROR_SERVICE_NOT_AVAILABLE;
420
421         /* sending request */
422         here_error_e error = HERE_ERROR_NONE;
423
424         do {
425                 error = pPlace->PreparePlaceDetailsQuery();
426                 if (error != HERE_ERROR_NONE) break;
427
428                 error = pPlace->PreparePlaceDetailsPreference(hPref);
429
430                 error = pPlace->StartPlaceDetails(szUrl);
431         } while(0);
432
433         /* finishing task */
434         if(error != HERE_ERROR_NONE)
435                 pPlace->TerminateService();
436
437         return error;
438 }
439
440 int HerePluginSearchRoute(maps_coordinates_h hOrigin, maps_coordinates_h hDestination,
441         maps_item_hashtable_h hPref, maps_service_search_route_cb pCbFunc,
442         void *pUserData, int *nReqId)
443 {
444         /* checking parmaters */
445         if (!hOrigin || !hDestination || !pCbFunc || !nReqId)
446                 return HERE_ERROR_INVALID_PARAMETER;
447
448         if (!HereUtils::IsValid(*(maps_coordinates_s*)hOrigin) ||
449                 !HereUtils::IsValid(*(maps_coordinates_s*)hDestination))
450                 return HERE_ERROR_INVALID_PARAMETER;
451
452         if (!HereManager::GetHandler())
453                 return HERE_ERROR_INVALID_OPERATION;
454
455         /* creating instance */
456         HereRoute *pRoute =
457                 (HereRoute*)(HereManager::GetHandler()->CreateInstance(HereManager::HERE_SVC_ROUTE,
458                 (void*)pCbFunc, pUserData, nReqId));
459
460         if(!pRoute)
461                 return HERE_ERROR_SERVICE_NOT_AVAILABLE;
462
463         /* sending request */
464         here_error_e error = HERE_ERROR_NONE;
465
466         do {
467                 error = pRoute->PrepareQuery();
468                 if (error != HERE_ERROR_NONE) break;
469
470                 error = pRoute->PreparePreference(hPref);
471
472                 error = pRoute->PrepareWaypoint(hOrigin, hDestination);
473                 if (error != HERE_ERROR_NONE) break;
474
475                 error = pRoute->StartRoute();
476         } while(0);
477
478         /* finishing task */
479         if(error != HERE_ERROR_NONE)
480                 pRoute->TerminateService();
481
482         return error;
483 }
484
485 int HerePluginSearchRouteWaypoints(const maps_coordinates_h* hWaypointList, int nWaypointNum,
486         maps_item_hashtable_h hPref, maps_service_search_route_cb pCbFunc,
487         void* pUserData, int *nReqId)
488 {
489         /* checking parmaters */
490         if (!hWaypointList || nWaypointNum < 2 || !pCbFunc || !nReqId)
491                 return HERE_ERROR_INVALID_PARAMETER;
492
493         for (int i=0; i<nWaypointNum; i++)
494         {
495                 if (!HereUtils::IsValid(*(maps_coordinates_s*)hWaypointList[i]))
496                         return HERE_ERROR_INVALID_PARAMETER;
497         }
498
499         if (!HereManager::GetHandler())
500                 return HERE_ERROR_INVALID_OPERATION;
501
502         /* creating instance */
503         HereRoute *pRoute =
504                 (HereRoute*)(HereManager::GetHandler()->CreateInstance(HereManager::HERE_SVC_ROUTE,
505                 (void*)pCbFunc, pUserData, nReqId));
506
507         if(!pRoute)
508                 return HERE_ERROR_SERVICE_NOT_AVAILABLE;
509
510         /* sending request */
511         here_error_e error = HERE_ERROR_NONE;
512
513         do {
514                 error = pRoute->PrepareQuery();
515                 if (error != HERE_ERROR_NONE) break;
516
517                 error = pRoute->PrepareWaypoint(hWaypointList, nWaypointNum);
518                 if (error != HERE_ERROR_NONE) break;
519
520                 error = pRoute->PreparePreference(hPref);
521
522                 error = pRoute->StartRoute();
523         } while(0);
524
525         /* finishing task */
526         if(error != HERE_ERROR_NONE)
527                 pRoute->TerminateService();
528
529         return error;
530 }
531
532 int HerePluginCancelRequest(int nReqId)
533 {
534         if (nReqId <= 0)
535                 return HERE_ERROR_INVALID_PARAMETER;
536
537         if (!HereManager::GetHandler())
538                 return HERE_ERROR_INVALID_OPERATION;
539
540         return (HereManager::GetHandler()->CancelInstance(nReqId));
541 }