summaryrefslogtreecommitdiff
path: root/src/navigation/navigation-core/routing-server-plugin/genivi_navigationcore_routing.cxx
blob: a2189c89aba61ebef1b9f8bc49d2cc1788f3d4c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
/**
* @licence app begin@
* SPDX-License-Identifier: MPL-2.0
*
* \copyright Copyright (C) 2013-2014, PCA Peugeot Citroen
*
* \file genivi_navigationcore_routing.cxx
*
* \brief This file is part of the Navit POC.
*
* \author Martin Schaller <martin.schaller@it-schaller.de>
* \author Philippe Colliot <philippe.colliot@mpsa.com>
*
* \version 1.0
*
* This Source Code Form is subject to the terms of the
* Mozilla Public License (MPL), v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* For further information see http://www.genivi.org/.
*
* List of changes:
* 
* <date>, <name>, <description of change>
*
* @licence end@
*/
#include <dbus-c++/glib-integration.h>
#include <config.h>
#define USE_PLUGINS 1
#include <navit/debug.h>
#include <navit/plugin.h>
#include <navit/item.h>
#include <navit/coord.h>
#include <navit/config_.h>
#include <navit/navit.h>
#include <navit/route.h>
#include <navit/transform.h>
#include <navit/command.h>
#include <navit/callback.h>
#include <navit/vehicle.h>
#include <navit/xmlconfig.h>
#include <navit/vehicleprofile.h>
#include <navit/roadprofile.h>
#include <navit/map.h>
#include <navit/event.h>

#include <unistd.h>

#include <CommonAPI/CommonAPI.hpp>
#include <CommonTypes.hpp>
#include <NavigationTypes.hpp>
#include <NavigationCoreTypes.hpp>
#include <RoutingStubDefault.hpp>

#if (!DEBUG_ENABLED)
#undef dbg
#define dbg(level,...) ;
#endif

using namespace v4::org::genivi::navigation::navigationcore;
using namespace v4::org::genivi::navigation;
using namespace v4::org::genivi;

struct vehicleprofile_settings {
	int flags_forward_mask,flags_reverse_mask;
	int ferry_weight;
	int highway_city_weight;
	int highway_land_weight;
};

class RoutingServerStub;

class RoutingObj
{
public:
    RoutingObj(RoutingServerStub *routing, uint32_t session, uint32_t handle);
    ~RoutingObj();
    void map_to_pcoord(Routing::WayPoint map, struct pcoord *pc);
    void SetCostModel(uint32_t SessionHandle, Routing::CostModel CostModel);
    void GetCostModel(Routing::CostModel &CostModel);
    void SetWaypoints(uint32_t SessionHandle, bool StartFromCurrentPosition, std::vector< Routing::WayPoint > Waypoints);
    void GetWaypoints(bool& StartFromCurrentPosition, std::vector<Routing::WayPoint> &Waypoints);
    void CalculateRoute(uint32_t SessionHandle);
    void GetRouteSegments(int16_t detailLevel , const std::vector< Routing::RouteSegmentType >& valuesToReturn, const uint32_t& numberOfSegments, const uint32_t& offset, uint32_t& totalNumberOfSegments, std::vector<Routing::RouteSegment>& RouteSegments);
    void GetRouteOverview(uint32_t routeHandle , Routing::RouteOverview &routeOverview);
    void GetRouteBoundingBox(NavigationTypes::Rectangle &boundingBox);
    void CancelRouteCalculation(uint32_t sessionHandle);
    bool RoutePreference(Routing::PreferenceMode preferenceMode,Routing::RoutePreferenceSource preferenceSource);
    void SetRoutePreferences(uint32_t sessionHandle, const std::string& country, const std::vector< Routing::RoadPreference >& routePreferencesList);
    void GetRoutePreferences(const std::string& country, std::vector< Routing::RoadPreference >& roadPreferenceList);

    uint32_t m_handle;
    RoutingServerStub *mp_routing;
    int m_route_status;
    struct attr m_route,m_vehicleprofile;

private:
    struct search_list *m_sl;
    uint32_t m_session;
    Routing::CostModel m_costmodel;
    std::vector< Routing::RoadPreference > m_route_preferences_list[2];
    std::vector< Routing::WayPoint > m_waypoints;
    bool m_startfromcurrentposition;
    int m_vehicleprofile_idx;
    struct vehicleprofile_settings m_vehicleprofile_settings[2];
    struct callback *m_callback;
    struct navit *get_navit(void);
    struct mapset *get_mapset(struct navit *navit);
    struct route *get_route(struct navit *navit);
    struct vehicle *get_vehicle(struct navit *navit);
    struct tracking *get_tracking(void);
};

static std::map<NavigationTypes::Handle, RoutingObj *> mp_handles;

static bool commands_registered;

static void
navit_genivi_get_route(struct navit *nav, char *function, struct attr **in, struct attr ***out, int *valid)
{
	if (!out || !in || !in[0])
		return;
    if (!ATTR_IS_INT(in[0]->type))
		return;
    RoutingObj *obj=mp_handles[in[0]->u.num];
	if (!obj)
		return;
    *out=attr_generic_add_attr(*out, &obj->m_route);
	*out=attr_generic_add_attr(*out, &obj->m_vehicleprofile);
}

static struct command_table commands[] = {
	{"navit_genivi_get_route",command_cast(navit_genivi_get_route)},
};

class  RoutingServerStub : public RoutingStubDefault
{
	public:

#define MAX_ROUTE_HANDLES 256

    RoutingServerStub()
	{
        m_version.setVersionMajor(3);
        m_version.setVersionMinor(0);
        m_version.setVersionMicro(0);
        m_version.setDate("21-01-2014");
    }

    /**
     * description: This method returns the API version implemented by the server application
     */
    void getVersion(const std::shared_ptr<CommonAPI::ClientId> _client, getVersionReply_t _reply) {
        _reply(m_version);
    }

    /**
     * description: This method creates a route
     */
    void createRoute(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, createRouteReply_t _reply){
        Routing::createRouteError _error = Routing::createRouteError::OK;
        dbg(lvl_debug,"enter\n");
        NavigationTypes::Handle routeHandle=1;
        while (mp_handles[routeHandle]) {
            routeHandle++;
            if (routeHandle == MAX_ROUTE_HANDLES)
                _error = Routing::createRouteError::ROUTING_ERROR_NOMOREROUTEHANDLES;
        }
        mp_handles[routeHandle]=new RoutingObj(this, _sessionHandle, routeHandle);
        _reply(_error, routeHandle);
    }

    /**
     * description: This method deletes a route and its associated resources
     */
    void deleteRoute(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, deleteRouteReply_t _reply){
        Routing::deleteRouteError _error = Routing::deleteRouteError::OK;
        RoutingObj *obj=mp_handles[_routeHandle];
        if (!obj)
            _error = Routing::deleteRouteError::ROUTING_ERROR_ROUTENOTAVAILABLE;
        delete(obj);
        mp_handles[_routeHandle]=NULL;
        fireRouteDeletedEvent(_routeHandle);
        _reply(_error);
    }

    /**
     * description: This method sets the cost model
     */
    void setCostModel(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, Routing::CostModel _costModel, setCostModelReply_t _reply){
        Routing::setCostModelError _error = Routing::setCostModelError::OK;
        RoutingObj *obj=mp_handles[_routeHandle];
        if (!obj)
           _error = Routing::setCostModelError::ROUTING_ERROR_OPERATIONNOTALLOWED;
        obj->SetCostModel(_sessionHandle, _costModel);
        _reply(_error);
    }

    /**
     * description: This method retrieves the selected cost model
     */
    void getCostModel(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, getCostModelReply_t _reply){
        Routing::CostModel costModel;
        dbg(lvl_debug,"enter\n");
        RoutingObj *obj=mp_handles[_routeHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Route handle invalid");
        obj->GetCostModel(costModel);
        _reply(costModel);
    }

    /**
     * description: This method retrieves a list of supported cost models
     */
    void getSupportedCostModels(const std::shared_ptr<CommonAPI::ClientId> _client, getSupportedCostModelsReply_t _reply){
        std::vector< Routing::CostModel > costModels;
        costModels.resize(2);
        costModels[0]=Routing::CostModel::FASTEST;
        costModels[1]=Routing::CostModel::SHORTEST;
        _reply(costModels);
    }

    /**
     * description: This method sets a list of route preferences
     */
    void setRoutePreferences(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, std::string _countryCode, std::vector<Routing::RoadPreference> _roadPreferenceList, std::vector<Routing::ConditionPreference> _conditionPreferenceList, setRoutePreferencesReply_t _reply){
        Routing::setRoutePreferencesError _error = Routing::setRoutePreferencesError::OK;
        RoutingObj *obj=mp_handles[_routeHandle];
        if (!obj)
           _error = Routing::setRoutePreferencesError::ROUTING_ERROR_OPERATIONNOTALLOWED;
        obj->SetRoutePreferences(_sessionHandle, _countryCode, _roadPreferenceList);
        _reply(_error);
    }

    /**
     * description: This method retrieves a list of selected route preferences
     */
    void getRoutePreferences(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, std::string _countryCode, getRoutePreferencesReply_t _reply){
        Routing::RoadPreference roadPreference;
        std::vector<Routing::RoadPreference> _roadPreferenceList;
        Routing::ConditionPreference conditionPreference;
        std::vector<Routing::ConditionPreference> _conditionPreferenceList;

        RoutingObj *obj=mp_handles[_routeHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Route handle invalid");
        obj->GetRoutePreferences(_countryCode, _roadPreferenceList);
        if (_roadPreferenceList.size() == 0)
        { //add a default value (bug in qml to be fixed)
            roadPreference.setMode(Routing::PreferenceMode::INVALID);
            roadPreference.setSource(Routing::RoutePreferenceSource::INVALID);
            _roadPreferenceList.push_back(roadPreference);
        }
        conditionPreference.setMode(Routing::PreferenceMode::USE);
        conditionPreference.setSource(Routing::ConditionPreferenceSource::TRAFFIC_REALTIME);
        _conditionPreferenceList.push_back(conditionPreference); //by default
        _reply(_roadPreferenceList,_conditionPreferenceList);
    }

    /**
     * description: This method retrieves a list of supported route preferences
     */
    void getSupportedRoutePreferences(const std::shared_ptr<CommonAPI::ClientId> _client, getSupportedRoutePreferencesReply_t _reply){
        Routing::RoadPreference roadPreference;
        std::vector<Routing::RoadPreference> _routePreferencesList;
        Routing::ConditionPreference conditionPreference;
        std::vector<Routing::ConditionPreference> _conditionPreferenceList;
        roadPreference.setMode(Routing::PreferenceMode::AVOID);
        roadPreference.setSource(Routing::RoutePreferenceSource::HIGHWAYS_MOTORWAYS);
        _routePreferencesList.push_back(roadPreference);
        roadPreference.setMode(Routing::PreferenceMode::AVOID);
        roadPreference.setSource(Routing::RoutePreferenceSource::TOLL_ROADS);
        _routePreferencesList.push_back(roadPreference);
        roadPreference.setMode(Routing::PreferenceMode::AVOID);
        roadPreference.setSource(Routing::RoutePreferenceSource::FERRY);
        _routePreferencesList.push_back(roadPreference);
        conditionPreference.setMode(Routing::PreferenceMode::USE);
        conditionPreference.setSource(Routing::ConditionPreferenceSource::TRAFFIC_REALTIME);
        _conditionPreferenceList.push_back(conditionPreference);
        _reply(_routePreferencesList,_conditionPreferenceList);
    }

    /**
     * description: This method sets the time schedule for the route to be calculated
     */
    void setRouteSchedule(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, Routing::RouteSchedule _routeSchedule, setRouteScheduleReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
        _reply();
    }

    /**
     * description: This method gets the time schedule for the route to be calculated
     */
    void getRouteSchedule(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, std::vector<Routing::Schedule> _valuesToReturn, getRouteScheduleReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: This method sets a list of means of transportation that must be considered when
     *   calculating a route
     */
    void setTransportationMeans(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, std::vector<Routing::TransportationMeans> _transportationMeansList, setTransportationMeansReply_t _reply){
        Routing::setTransportationMeansError _error = Routing::setTransportationMeansError::OK;
        throw DBus::ErrorNotSupported("Not yet supported");
        _reply(_error);
    }

    /**
     * description: getTransportationMeans = This method retrieves the selected means of
     *   transportation
     */
    void getTransportationMeans(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, getTransportationMeansReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getSupportedTransportationMeans = This method retrieves a list of supported
     *   means of transportation
     */
    void getSupportedTransportationMeans(const std::shared_ptr<CommonAPI::ClientId> _client, getSupportedTransportationMeansReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: setExcludedAreas = This method sets the areas to be excluded when calculating a
     *   route
     */
    void setExcludedAreas(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, std::vector< ::v4::org::genivi::navigation::NavigationTypes::Polygon> _excludedAreas, setExcludedAreasReply_t _reply){
        Routing::setExcludedAreasError _error = Routing::setExcludedAreasError::OK;
        throw DBus::ErrorNotSupported("Not yet supported");
        _reply(_error);
    }

    /**
     * description: getExcludedAreas = This method retrieves the areas to be excluded when
     *   calculating a route
     */
    void getExcludedAreas(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, getExcludedAreasReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: setWaypoints = This method sets a list of waypoints
     */
    void setWaypoints(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, bool _startFromCurrentPosition, std::vector<Routing::WayPoint> _waypointsList, setWaypointsReply_t _reply){
        Routing::setWaypointsError _error = Routing::setWaypointsError::OK;
        dbg(lvl_debug,"enter\n");
        RoutingObj *obj=mp_handles[_routeHandle];
        if (!obj)
            _error = Routing::setWaypointsError::ROUTING_ERROR_OPERATIONNOTALLOWED;
        obj->SetWaypoints(_sessionHandle, _startFromCurrentPosition, _waypointsList);
        _reply(_error);
    }

    /**
     * description: getWaypoints = This method retrieves a list of waypoints
     */
    void getWaypoints(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, getWaypointsReply_t _reply){
        dbg(lvl_debug,"enter\n");
        bool _startFromCurrentPosition;
        std::vector<Routing::WayPoint> _waypointsList;
        RoutingObj *obj=mp_handles[_routeHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Route handle invalid");
        obj->GetWaypoints(_startFromCurrentPosition, _waypointsList);
        _reply(_startFromCurrentPosition,_waypointsList);
    }

    /**
     * description: calculateRoute = This method starts a route calculation
     */
    void calculateRoute(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, calculateRouteReply_t _reply){
        Routing::calculateRouteError _error = Routing::calculateRouteError::OK;
        dbg(lvl_debug,"enter\n");
        RoutingObj *obj=mp_handles[_routeHandle];
        if (!obj)
            _error = Routing::calculateRouteError::ROUTING_ERROR_OPERATIONNOTALLOWED;
        obj->CalculateRoute(_sessionHandle);
        _reply(_error);
    }

    /**
     * description: cancelRouteCalculation = This method cancels a route calculation
     */
    void cancelRouteCalculation(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, cancelRouteCalculationReply_t _reply){
        RoutingObj *obj=mp_handles[_routeHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Route handle invalid");
        obj->CancelRouteCalculation(_sessionHandle);
        fireRouteCalculationCancelledEvent(_routeHandle);
        _reply();
    }

    /**
     * description: calculateAlternativeRoutes = This method allows a client to calculate alternative routes
     *   that differs from a  calculated route
     */
    void calculateAlternativeRoutes(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _calculatedRoute, uint16_t _numberOfAlternativeRoutes, calculateAlternativeRoutesReply_t _reply) {
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getRouteSegments = This method retrieves a list of segments for a given route
     *   starting from the one closest to the current position to the one closest to
     *   the destination
     */
    void getRouteSegments(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, int16_t _detailLevel, std::vector< Routing::RouteSegmentType > _valuesToReturn, uint32_t _numberOfSegments, uint32_t _offset, getRouteSegmentsReply_t _reply){
        dbg(lvl_debug,"enter\n");
        uint32_t _totalNumberOfSegments;
        std::vector<Routing::RouteSegment> _routeSegments;
        RoutingObj *obj=mp_handles[_routeHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Route handle invalid");
        obj->GetRouteSegments(_detailLevel,_valuesToReturn, _numberOfSegments, _offset, _totalNumberOfSegments, _routeSegments);
        _reply(_totalNumberOfSegments,_routeSegments);
    }

    /**
     * description: getRouteOverview = This method retrieves general information about a given route
     */
    void getRouteOverview(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, std::vector<Routing::RouteOverviewType> _valuesToReturn, getRouteOverviewReply_t _reply){
        Routing::RouteOverview _routeOverview;
        RoutingObj *obj=mp_handles[_routeHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Route handle invalid");
        obj->GetRouteOverview(_routeHandle, _routeOverview);
        _reply(_routeOverview);

    }

    /**
     * description: getRouteBoundingBox = This method retrieves the bounding box containing a
     *   calculated route
     */
    void getRouteBoundingBox(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, getRouteBoundingBoxReply_t _reply){
        NavigationTypes::Rectangle _boundingBox;
        RoutingObj *obj=mp_handles[_routeHandle];
        if (!obj)
            throw DBus::ErrorInvalidArgs("Route handle invalid");
        obj->GetRouteBoundingBox(_boundingBox);
        _reply(_boundingBox);

    }

    /**
     * description: getAllRoutes = This method retrieves the handles of all created routes
     */
    void getAllRoutes(const std::shared_ptr<CommonAPI::ClientId> _client, getAllRoutesReply_t _reply){
        std::vector< NavigationTypes::Handle >_routesList;
        std::map<uint32_t, RoutingObj *>::const_iterator itr;

        for(itr = mp_handles.begin(); itr != mp_handles.end(); itr++)
            _routesList.push_back((*itr).first);
        _reply(_routesList);
    }

    /**
     * description: setBlockedRouteStretches = This method sets blocked streches on a given route
     */
    void setBlockedRouteStretches(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, std::vector<Routing::BlockedRouteElement> _blockParameters, setBlockedRouteStretchesReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
        _reply();
    }

    /**
     * description: getBlockedRouteStretches = This method retrieves all blocked streches on a
     *   given route
     */
    void getBlockedRouteStretches(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _routeHandle, getBlockedRouteStretchesReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }
	
private:
    CommonTypes::Version m_version;

};

struct navit *
RoutingObj::get_navit(void)
{
    struct attr navit,callback_list;
    if (!config_get_attr(config, attr_navit, &navit, NULL))
        return NULL;

    if (!commands_registered && navit_get_attr(navit.u.navit, attr_callback_list, &callback_list, NULL)) {
        command_add_table(callback_list.u.callback_list, commands, sizeof(commands)/sizeof(struct command_table), navit.u.navit);
        commands_registered=true;
    }

    return navit.u.navit;
}

struct mapset *
RoutingObj::get_mapset(struct navit *navit)
{
    struct attr mapset;
    if (!navit_get_attr(navit, attr_mapset, &mapset, NULL))
        return NULL;
    return mapset.u.mapset;
}

struct route *
RoutingObj::get_route(struct navit *navit)
{
    struct attr route;
    if (!navit_get_attr(navit, attr_route, &route, NULL))
        return NULL;
    return route.u.route;
}

struct vehicle *
RoutingObj::get_vehicle(struct navit *navit)
{
    struct attr vehicle;
    if (!navit_get_attr(navit, attr_vehicle, &vehicle, NULL))
        return NULL;
    return vehicle.u.vehicle;
}

struct tracking *
RoutingObj::get_tracking(void)
{
    struct navit *navit=get_navit();
    struct attr tracking;
    if (!navit)
        return NULL;
    if (!navit_get_attr(navit, attr_trackingo, &tracking, NULL))
        return NULL;
    return tracking.u.tracking;
}

void
RoutingObj_Callback(struct RoutingObj *obj)
{
    struct attr route_status;
    if (!route_get_attr(obj->m_route.u.route, attr_route_status, &route_status, NULL)) {
        dbg(lvl_debug,"failed to get route status\n");
        return;
    }
    if (route_status.u.num == route_status_destination_set) {
        obj->mp_routing->fireRouteCalculationProgressUpdateEvent(obj->m_handle, Routing::CalculationStatus::CALCULATION_OK, 5);
        obj->m_route_status=route_status.u.num;
    }
    if (route_status.u.num == route_status_building_graph) {
        obj->m_route_status=route_status.u.num;
    }
    if (route_status.u.num == route_status_building_path && obj->m_route_status == route_status_building_graph) {
        obj->mp_routing->fireRouteCalculationProgressUpdateEvent(obj->m_handle, Routing::CalculationStatus::CALCULATION_OK, 50);
        obj->m_route_status=route_status.u.num;
    }
    if (route_status.u.num == route_status_path_done_new ||
        (obj->m_route_status==route_status_destination_set && route_status.u.num == route_status_path_done_incremental)) {
        if (route_status.u.num == route_status_path_done_new)
            obj->mp_routing->fireRouteCalculationProgressUpdateEvent(obj->m_handle, Routing::CalculationStatus::CALCULATION_OK, 100);
        obj->m_route_status=route_status.u.num;
        dbg(lvl_debug,"callback routing ok\n");
        Routing::UnfullfilledRoutePreference unfulfilled_preferences;
        //tbd complete the unfulfilled_preferences
        obj->mp_routing->fireRouteCalculationSuccessfulEvent(obj->m_handle, unfulfilled_preferences);
    }
    if (route_status.u.num == route_status_not_found) {
        obj->m_route_status=route_status.u.num;
        dbg(lvl_debug,"callback routing failed\n");
        Routing::UnfullfilledRoutePreference unfulfilled_preferences;
        //tbd complete the unfulfilled_preferences
        obj->mp_routing->fireRouteCalculationFailedEvent(obj->m_handle, Routing::CalculationError::UNREACHABLE_DESTINATION, unfulfilled_preferences);
    }
}

RoutingObj::RoutingObj(RoutingServerStub *routing, uint32_t session, uint32_t handle)
{
    struct attr callback;

    mp_routing=routing;
    m_session=session;
    m_handle=handle;
    m_route.type=attr_route;
    m_route.u.route=route_new(NULL, NULL);
    m_vehicleprofile.type=attr_vehicleprofile;
    m_vehicleprofile.u.vehicleprofile=NULL;
    m_callback=callback_new_attr_1(reinterpret_cast<void (*)(void)>(RoutingObj_Callback), attr_route_status, this);
    memset(m_vehicleprofile_settings, 0, sizeof(m_vehicleprofile_settings));
    SetCostModel(handle, Routing::CostModel::FASTEST);
    callback.type=attr_callback;
    callback.u.callback=m_callback;
    route_add_attr(m_route.u.route, &callback);
    route_set_mapset(m_route.u.route, get_mapset(get_navit()));
}

RoutingObj::~RoutingObj()
{
    struct attr callback;

    callback.type=attr_callback;
    callback.u.callback=m_callback;
    route_remove_attr(m_route.u.route, &callback);
    callback_destroy(m_callback);
    route_destroy(m_route.u.route);
}

void
RoutingObj::SetCostModel(uint32_t SessionHandle, Routing::CostModel CostModel)
{
    const char *vehicleprofile_name;
    switch (CostModel) {
    case Routing::CostModel::FASTEST:
        vehicleprofile_name="car";
        m_vehicleprofile_idx=0;
        break;
    case Routing::CostModel::SHORTEST:
        vehicleprofile_name="car_shortest";
        m_vehicleprofile_idx=1;
        break;
    default:
        throw DBus::ErrorInvalidArgs("Invalid cost model");
    }
    m_costmodel=CostModel;
    m_vehicleprofile.u.vehicleprofile=NULL;
    GList *vehicleprofiles=navit_get_vehicleprofiles(get_navit());

    while (vehicleprofiles) {
        struct attr name;
        if (vehicleprofile_get_attr((struct vehicleprofile *)vehicleprofiles->data, attr_name, &name, NULL)) {
            if ((!strcmp(name.u.str,vehicleprofile_name))) {
                m_vehicleprofile.u.vehicleprofile=(struct vehicleprofile *)vehicleprofiles->data;
                break;
            }
        }
        vehicleprofiles=g_list_next(vehicleprofiles);
    }
    if (!m_vehicleprofile.u.vehicleprofile)
        throw DBus::ErrorFailed("internal error:no vehicleprofile found");
}

void
RoutingObj::GetCostModel(Routing::CostModel &CostModel)
{
    CostModel=m_costmodel;
}

void
RoutingObj::SetWaypoints(uint32_t SessionHandle, bool StartFromCurrentPosition, std::vector<Routing::WayPoint> Waypoints)
{
    Routing::WayPoint waypoint;
    Routing::WayPoint::iterator it;
    if (StartFromCurrentPosition) {
        if (Waypoints.size() != 1)
            throw DBus::ErrorFailed("StartFromCurrentPosition is set, but Waypoint size is not 1");
    } else {
        if (Waypoints.size() != 2)
            throw DBus::ErrorFailed("StartFromCurrentPosition is not set, but Waypoint size is not 2");
    }
    for (size_t i=0 ; i < Waypoints.size(); i++) {
        waypoint = Waypoints[i];
        if (waypoint.find(Routing::WaypointElementType::LATITUDE) == waypoint.end())
            throw DBus::ErrorInvalidArgs("Waypoint doesn't contain Latitude");
        if (waypoint.find(Routing::WaypointElementType::LONGITUDE) == waypoint.end())
            throw DBus::ErrorInvalidArgs("Waypoint doesn't contain Longitude");
    }
    m_startfromcurrentposition=StartFromCurrentPosition;
    m_waypoints=Waypoints;

}

void
RoutingObj::GetWaypoints(bool& StartFromCurrentPosition, std::vector< Routing::WayPoint >& Waypoints)
{
    StartFromCurrentPosition=m_startfromcurrentposition;
    Waypoints=m_waypoints;
}

void
RoutingObj::map_to_pcoord(Routing::WayPoint map, struct pcoord *pc)
{
    struct coord_geo g;
    struct coord c;
    g.lat=(map[Routing::WaypointElementType::LATITUDE]).get<double>();
    g.lng=(map[Routing::WaypointElementType::LONGITUDE]).get<double>();
    transform_from_geo(projection_mg, &g, &c);
    pc->pro=projection_mg;
    pc->x=c.x;
    pc->y=c.y;
    dbg(lvl_debug,"lat %f lon %f is 0x%x,0x%x\n",g.lat,g.lng,pc->x,pc->y);

}

void
RoutingObj::CalculateRoute(uint32_t SessionHandle)
{
    struct pcoord pc;

    if (!m_waypoints.size())
        throw DBus::ErrorFailed("no waypoints set");
    route_set_profile(m_route.u.route,m_vehicleprofile.u.vehicleprofile);
    if (!m_startfromcurrentposition) {
        route_set_destination(m_route.u.route, NULL, 0);
        map_to_pcoord(m_waypoints[0], &pc);
        route_set_position(m_route.u.route, &pc);
        map_to_pcoord(m_waypoints[1], &pc);
    } else {
        struct tracking *tracking=get_tracking();
        if (tracking)
            route_set_position_from_tracking(m_route.u.route, tracking, projection_mg);
        map_to_pcoord(m_waypoints[0], &pc);
    }
    m_route_status = route_status_destination_set;
    route_set_destination(m_route.u.route, &pc, 1);
}

static bool
vector_contains(const std::vector< Routing::RouteSegmentType >& vector, Routing::RouteSegmentType val)
{
    size_t i;
	for (i = 0 ; i < vector.size() ; i++) {
		if (vector[i] == val)
			return true;
	}
	return false;
}

static void
get_map(struct coord *c, struct item *item, int is_end, const std::vector< Routing::RouteSegmentType >& valuesToReturn, Routing::RouteSegment &map)
{
    Routing::RouteSegmentType lat_key=is_end?Routing::RouteSegmentType::END_LATITUDE:Routing::RouteSegmentType::START_LATITUDE;
    Routing::RouteSegmentType lon_key=is_end?Routing::RouteSegmentType::END_LONGITUDE:Routing::RouteSegmentType::START_LONGITUDE;
	if (vector_contains(valuesToReturn, lat_key) || vector_contains(valuesToReturn, lon_key)) {
		struct coord_geo g;
		transform_to_geo(projection_mg, c, &g);
		if (vector_contains(valuesToReturn, lat_key))
            map[lat_key]=(double)g.lat;
		if (vector_contains(valuesToReturn, lon_key))
            map[lon_key]=g.lng;
	}
    if (item && (vector_contains(valuesToReturn, Routing::RouteSegmentType::DISTANCE) || vector_contains(valuesToReturn, Routing::RouteSegmentType::TIME) || vector_contains(valuesToReturn, Routing::RouteSegmentType::SPEED))) {
		struct attr length, time, speed;
        if (item_attr_get(item, attr_length, &length) && vector_contains(valuesToReturn, Routing::RouteSegmentType::DISTANCE)) {
            map[Routing::RouteSegmentType::DISTANCE]=(double)length.u.num;
		}
        if (item_attr_get(item, attr_time, &time) && vector_contains(valuesToReturn, Routing::RouteSegmentType::TIME)) {
            map[Routing::RouteSegmentType::TIME]=(uint16_t)((time.u.num+5)/10);
		}
        if (item_attr_get(item, attr_speed, &speed) && vector_contains(valuesToReturn, Routing::RouteSegmentType::SPEED)) {
            map[Routing::RouteSegmentType::SPEED]=(uint16_t)speed.u.num;
		}
	}
    if (item && vector_contains(valuesToReturn, Routing::RouteSegmentType::ROAD_NAME)) {
		struct attr street_item;
		if (item_attr_get(item, attr_street_item, &street_item)) {
			struct map_rect *mr=map_rect_new(street_item.u.item->map, NULL);
			struct item *item2=map_rect_get_item_byid(mr, street_item.u.item->id_hi, street_item.u.item->id_lo);
			struct attr label;
			if (item2 && item_attr_get(item2, attr_label, &label)) 
                map[Routing::RouteSegmentType::ROAD_NAME]=std::string(label.u.str);
			map_rect_destroy(mr);
		}
	}
}

void RoutingObj::GetRouteSegments(int16_t detailLevel , const std::vector< Routing::RouteSegmentType >& valuesToReturn, const uint32_t& numberOfSegments, const uint32_t& offset, uint32_t& totalNumberOfSegments, std::vector<Routing::RouteSegment>& RouteSegments)
{
    struct map *m=route_get_map(m_route.u.route);
    if (!m)
        throw DBus::ErrorFailed("internal error:failed to get route map");
    struct map_rect *mr=map_rect_new(m, NULL);
    if (!mr)
        throw DBus::ErrorFailed("internal error:failed to create route map rect");
    struct item *item;
    struct coord c[128],last;
    int count,i;
    int pos;
    bool intermediate=vector_contains(valuesToReturn, Routing::RouteSegmentType::INTERMEDIATE_POINTS);
    totalNumberOfSegments=0;
    while ((item=map_rect_get_item(mr))) {
        if (item->type == type_street_route) {
            pos=0;
            if (totalNumberOfSegments >= offset && totalNumberOfSegments < offset+numberOfSegments) {
                std::vector<DBus::Struct<uint16_t, double, double, double> >intermediate_points;
                do {
                    count=item_coord_get(item, c, 128);
                    for (i = 0 ; i < count ; i++) {
                        switch (pos) {
                        case 0: /* last not valid */
                            pos++;
                            break;
                        case 1: /* last is start coordinates */
                            {
                                Routing::RouteSegment map;
                                get_map(&last, item, 0, valuesToReturn, map);
                                RouteSegments.push_back(map);
                                pos++;
                            }
                            break;
                        case 2: /* last is intermediate coordinates */
                            if (intermediate)  {
                                struct coord_geo g;
                                transform_to_geo(projection_mg, &last, &g);
                                DBus::Struct<uint16_t, double, double, double> point;
                                point._1=0;
                                point._2=g.lat;
                                point._3=g.lng;
                                point._4=0;
                                intermediate_points.push_back(point);
                            }
                        }
                        last=c[i];
                    }
                } while (count == 128);
#if 0
                if (intermediate_points.size()) {
                    DBus::Variant variant;
                    DBus::MessageIter iter=variant.writer();
                    iter << intermediate_points;
                    RouteSegments.back()[GENIVI_NAVIGATIONCORE_INTERMEDIATE_POINTS]=variant;
                }
#endif
            }
            totalNumberOfSegments++;
        }
    }
    if (pos == 2) /* last is final coordinates */
        get_map(&last, NULL, 1, valuesToReturn, RouteSegments.back());
    map_rect_destroy(mr);
}

void
RoutingObj::GetRouteOverview(uint32_t routeHandle , Routing::RouteOverview &routeOverview)
{
    struct attr destination_length, destination_time;
    if (!route_get_attr(m_route.u.route,attr_destination_length, &destination_length, NULL))
        throw DBus::ErrorFailed("internal error:failed to get attribute destination_length");
    if (!route_get_attr(m_route.u.route,attr_destination_time, &destination_time, NULL))
        throw DBus::ErrorFailed("internal error:failed to get attribute destination_time");

    routeOverview[Routing::RouteOverviewType::TOTAL_DISTANCE] = (uint32_t)destination_length.u.num;
    routeOverview[Routing::RouteOverviewType::TOTAL_TIME] = (uint32_t)(destination_time.u.num/10);
}

void
RoutingObj::GetRouteBoundingBox(NavigationTypes::Rectangle &boundingBox)
{
    struct coord_rect r;
    struct coord_geo g;
    bool first=true;
    struct map *m=route_get_map(m_route.u.route);
    struct item *item;
    NavigationTypes::Coordinate2D value;
    if (!m)
        throw DBus::ErrorFailed("internal error:failed to get route map");
    struct map_rect *mr=map_rect_new(m, NULL);
    if (!mr)
        throw DBus::ErrorFailed("internal error:failed to create route map rect");
    while ((item=map_rect_get_item(mr))) {
        struct coord c[128];
        int count,i;
        do {
            count=item_coord_get(item, c, 128);
            for (i = 0 ; i < count ; i++) {
                if (first) {
                    first=0;
                    r.lu=c[i];
                    r.rl=c[i];
                } else
                    coord_rect_extend(&r, &c[i]);
            }
        } while (count == 128);
    }
    map_rect_destroy(mr);
    if (first)
        throw DBus::ErrorFailed("no route available");
    dbg(lvl_debug,"bounding box 0x%x,0x%x-0x%x,0x%x\n",r.lu.x,r.lu.y,r.rl.x,r.rl.y);
    transform_to_geo(projection_mg, &r.lu, &g);
    value.setLatitude(g.lat);
    value.setLongitude(g.lng);
    boundingBox.setTopLeft(value);
    dbg(lvl_debug,"%f,%f\n",g.lat,g.lng);
    transform_to_geo(projection_mg, &r.rl, &g);
    value.setLatitude(g.lat);
    value.setLongitude(g.lng);
    boundingBox.setBottomRight(value);
    dbg(lvl_debug,"-%f,%f\n",g.lat,g.lng);
}

void
RoutingObj::CancelRouteCalculation(uint32_t sessionHandle)
{
    route_set_destination(m_route.u.route, NULL, 0);
}

bool
RoutingObj::RoutePreference(Routing::PreferenceMode preferenceMode,Routing::RoutePreferenceSource preferenceSource)
{
    size_t index;

    for (index=0;index<m_route_preferences_list[m_vehicleprofile_idx].size();index++)
    {
        if (((m_route_preferences_list[m_vehicleprofile_idx].at(index)).getSource() == preferenceSource) && ((m_route_preferences_list[m_vehicleprofile_idx].at(index)).getMode() == preferenceMode))
            return true;
    }

    return false;
}

void
RoutingObj::SetRoutePreferences(uint32_t sessionHandle, const std::string& country, const std::vector< Routing::RoadPreference >& routePreferencesList)
{
    struct attr flags_forward_mask,flags_reverse_mask,roadprofile,item_types,route_weight;
    m_route_preferences_list[m_vehicleprofile_idx]=routePreferencesList;
    struct vehicleprofile_settings *s=&m_vehicleprofile_settings[m_vehicleprofile_idx];

    if (!s->flags_forward_mask) {
        vehicleprofile_get_attr(m_vehicleprofile.u.vehicleprofile, attr_flags_forward_mask, &flags_forward_mask, NULL);
        vehicleprofile_get_attr(m_vehicleprofile.u.vehicleprofile, attr_flags_reverse_mask, &flags_reverse_mask, NULL);
        s->flags_forward_mask=flags_forward_mask.u.num;
        s->flags_reverse_mask=flags_reverse_mask.u.num;
    }
    flags_forward_mask.type=attr_flags_forward_mask;
    flags_reverse_mask.type=attr_flags_reverse_mask;
    if (RoutePreference(Routing::PreferenceMode::AVOID, Routing::RoutePreferenceSource::TOLL_ROADS)) {
        flags_forward_mask.u.num=s->flags_forward_mask | AF_TOLL;
        flags_reverse_mask.u.num=s->flags_reverse_mask | AF_TOLL;
    } else {
        flags_forward_mask.u.num=s->flags_forward_mask;
        flags_reverse_mask.u.num=s->flags_reverse_mask;
    }
    vehicleprofile_set_attr(m_vehicleprofile.u.vehicleprofile, &flags_forward_mask);
    vehicleprofile_set_attr(m_vehicleprofile.u.vehicleprofile, &flags_reverse_mask);
    struct attr_iter *iter=vehicleprofile_attr_iter_new();
    while (vehicleprofile_get_attr(m_vehicleprofile.u.vehicleprofile, attr_roadprofile, &roadprofile, iter)) {
        if (roadprofile_get_attr(roadprofile.u.roadprofile, attr_item_types, &item_types, NULL)) {
            int *par=NULL;
            bool active=false;
            switch (item_types.u.item_types[0]) {
            case type_ferry:
                par=&s->ferry_weight;
                active=RoutePreference(Routing::PreferenceMode::AVOID, Routing::RoutePreferenceSource::FERRY);
                break;
            case type_highway_city:
                par=&s->highway_city_weight;
                active=RoutePreference(Routing::PreferenceMode::AVOID, Routing::RoutePreferenceSource::HIGHWAYS_MOTORWAYS);
                break;
            case type_highway_land:
                par=&s->highway_land_weight;
                active=RoutePreference(Routing::PreferenceMode::AVOID, Routing::RoutePreferenceSource::HIGHWAYS_MOTORWAYS);
                break;
            default:
                break;
            }
            if (par) {
                if (!*par) {
                    roadprofile_get_attr(roadprofile.u.roadprofile, attr_route_weight, &route_weight, NULL);
                    *par=route_weight.u.num;
                }
                route_weight.type=attr_route_weight;
                route_weight.u.num=active?0:*par;
                roadprofile_set_attr(roadprofile.u.roadprofile, &route_weight);
            }
        }
    }
    vehicleprofile_attr_iter_destroy(iter);

}

void
RoutingObj::GetRoutePreferences(const std::string& country, std::vector< Routing::RoadPreference >& roadPreferenceList)
{
    roadPreferenceList=m_route_preferences_list[m_vehicleprofile_idx];
}

static std::shared_ptr<RoutingServerStub> myServiceRouting;

void
plugin_init(void)
{
    // Common API data init
    std::shared_ptr < CommonAPI::Runtime > runtime = CommonAPI::Runtime::get();

    const std::string &domain = "local";
    const std::string &instance = "Routing";

    myServiceRouting = std::make_shared<RoutingServerStub>();

    bool successfullyRegistered = runtime->registerService(domain, instance, myServiceRouting);
    while (!successfullyRegistered) {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        successfullyRegistered = runtime->registerService(domain, instance, myServiceRouting);
    }
}