summaryrefslogtreecommitdiff
path: root/src/navigation/navigation-core/mapmatchedposition-server-plugin/genivi_navigationcore_mapmatchedposition.cxx
blob: 3f66d0a07f1302c23e968ef5c1085c7c6ce2dce2 (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
/**
* @licence app begin@
* SPDX-License-Identifier: MPL-2.0
*
* \copyright Copyright (C) 2013-2014, PCA Peugeot Citroen
*
* \file genivi_navigationcore_mapmatchedposition.cxx
*
* \brief This file is part of the Navit POC.
*
* \author Martin Schaller <martin.schaller@it-schaller.de>
*
* \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@
*/
#ifndef DBUS_HAS_RECURSIVE_MUTEX
#define DBUS_HAS_RECURSIVE_MUTEX
#endif
#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/config_.h>
#include <navit/navit.h>
#include <navit/callback.h>
#include <navit/navigation.h>
#include <navit/map.h>
#include <navit/transform.h>
#include <navit/track.h>
#include <navit/vehicle.h>
#include <navit/route.h>
#include <navit/config_.h>

#include <CommonAPI/CommonAPI.hpp>
#include <CommonTypes.hpp>
#include <NavigationTypes.hpp>
#include <NavigationCoreTypes.hpp>
#include <MapMatchedPositionStubDefault.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;

static struct attr config_cb,navit_cb,tracking_cb;
static struct tracking *tracking;
static struct item last_item;
static char *mp_street_name;
static struct attr vehicle_speed={attr_speed,(char *)40};

static struct navit *
get_navit(void)
{
	struct attr navit;
	if (!config_get_attr(config, attr_navit, &navit, NULL))
		return NULL;
	return navit.u.navit;
}

static struct vehicle *
get_vehicle(const char *source_prefix)
{
	struct navit *navit=get_navit();
	struct attr vehicle;
	struct vehicle *ret=NULL;
	if (!source_prefix) {
		if (navit_get_attr(navit, attr_vehicle, &vehicle, NULL))
			ret=vehicle.u.vehicle;
	} else {
		struct attr_iter *iter=navit_attr_iter_new();
		while (navit_get_attr(navit, attr_vehicle, &vehicle, iter)) {
			struct attr source;
			if (vehicle.u.vehicle && vehicle_get_attr(vehicle.u.vehicle, attr_source, &source, NULL) && 
				!strncmp(source.u.str, source_prefix, strlen(source_prefix))) { 
				ret=vehicle.u.vehicle;
				break;
			}
		}
		navit_attr_iter_destroy(iter);
	}	
	return ret;
}

class  MapMatchedPositionServerStub : public MapMatchedPositionStubDefault
{
	public:
    MapMatchedPositionServerStub()
	{
        m_version.setVersionMajor(3);
        m_version.setVersionMinor(0);
        m_version.setVersionMicro(0);
        m_version.setDate("21-01-2014");

        m_simulationMode = MapMatchedPosition::SimulationStatus::SIMULATION_STATUS_NO_SIMULATION;
    }

    ~MapMatchedPositionServerStub(){}

    /**
     * description: getVersion = 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: setSimulationMode = This method activates or deactivates the simulation mode
     */
    void setSimulationMode(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, bool _activate, setSimulationModeReply_t _reply){
        dbg(lvl_debug,"enter Activate=%d\n",_activate);
        MapMatchedPosition::SimulationStatus newSimulationMode;
        struct attr vehicle;
        vehicle.type=attr_vehicle;
        vehicle.u.vehicle=get_vehicle(_activate ? "demo:":"enhancedposition:");
        if (vehicle.u.vehicle) {
            newSimulationMode = _activate ? MapMatchedPosition::SimulationStatus::SIMULATION_STATUS_FIXED_POSITION : MapMatchedPosition::SimulationStatus::SIMULATION_STATUS_NO_SIMULATION;
            if (newSimulationMode != m_simulationMode) {
                m_simulationMode=newSimulationMode;
                fireSimulationStatusChangedSelective(m_simulationMode);
            }
            demo_update(false);
            struct navit *navit=get_navit();
            navit_set_attr(navit, &vehicle);
        } else {
            dbg(lvl_debug,"Failed to get vehicle\n");
            }

        _reply();
    }

    /**
     * description: getSimulationStatus = This method retrieves the simulation status
     */
    void getSimulationStatus(const std::shared_ptr<CommonAPI::ClientId> _client, getSimulationStatusReply_t _reply){
        _reply(m_simulationMode);
    }

    /**
     * description: setSimulationSpeed = This method sets the speed factor for the simulation mode
     */
    void setSimulationSpeed(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, uint8_t _speedFactor, setSimulationSpeedReply_t _reply){
        int speed=_speedFactor*40/4;
        if (vehicle_speed.u.num != speed) {
            vehicle_speed.u.num=speed;
            fireSimulationSpeedChangedSelective(_speedFactor);
            if (m_simulationMode == MapMatchedPosition::SimulationStatus::SIMULATION_STATUS_RUNNING) {
                demo_update(true);
            }
        }
        _reply();
    }

    /**
     * description: getSimulationSpeed = returns the speed factor for the simulation mode
     */
    void getSimulationSpeed(const std::shared_ptr<CommonAPI::ClientId> _client, getSimulationSpeedReply_t _reply){
        _reply(vehicle_speed.u.num*4/40);
    }

    /**
     * description: startSimulation = This method starts, or resumes, a Follow Active Route
     *   simulation
     */
    void startSimulation(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, startSimulationReply_t _reply){
        if (m_simulationMode == MapMatchedPosition::SimulationStatus::SIMULATION_STATUS_PAUSED || m_simulationMode == MapMatchedPosition::SimulationStatus::SIMULATION_STATUS_FIXED_POSITION ) {
            m_simulationMode=MapMatchedPosition::SimulationStatus::SIMULATION_STATUS_RUNNING;
            fireSimulationStatusChangedSelective(m_simulationMode);
            demo_update(true);
        }
        _reply();
    }

    /**
     * description: pauseSimulation = This method freezes the current location
     */
    void pauseSimulation(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, pauseSimulationReply_t _reply){
        if (m_simulationMode == MapMatchedPosition::SimulationStatus::SIMULATION_STATUS_RUNNING) {
            m_simulationMode=MapMatchedPosition::SimulationStatus::SIMULATION_STATUS_PAUSED;
            fireSimulationStatusChangedSelective(m_simulationMode);
            demo_update(false);
        }
        _reply();
    }

    /**
     * description: getPosition = This method returns the current position
     */
    void getPosition(const std::shared_ptr<CommonAPI::ClientId> _client, std::vector<MapMatchedPosition::PositionItemKey> _valuesToReturn, getPositionReply_t _reply){
        MapMatchedPosition::getPositionError _error = MapMatchedPosition::getPositionError::OK;
        dbg(lvl_debug,"enter\n");
        MapMatchedPosition::PositionItemDict map;
        struct attr attr;
        for (std::size_t i = 0 ; i < _valuesToReturn.size() ; i++) {
            switch (_valuesToReturn[i]) {
            case MapMatchedPosition::PositionItemKey::LATITUDE:
                if (tracking_get_attr(tracking, attr_position_coord_geo, &attr, NULL))
                    map[MapMatchedPosition::PositionItemKey::LATITUDE]=attr.u.coord_geo->lat;
                break;
            case MapMatchedPosition::PositionItemKey::LONGITUDE:
                if (tracking_get_attr(tracking, attr_position_coord_geo, &attr, NULL))
                    map[MapMatchedPosition::PositionItemKey::LONGITUDE]=attr.u.coord_geo->lng;
                break;
            case MapMatchedPosition::PositionItemKey::SPEED:
                if (tracking_get_attr(tracking, attr_position_speed, &attr, NULL))
                    map[MapMatchedPosition::PositionItemKey::SPEED]=*attr.u.numd;
                break;
            case MapMatchedPosition::PositionItemKey::HEADING:
                if (tracking_get_attr(tracking, attr_position_direction, &attr, NULL))
                    map[MapMatchedPosition::PositionItemKey::HEADING]=*attr.u.numd;
                break;
            }
        }
        _reply(_error,map);
    }

    /**
     * description: setPosition = This method sets the position to a specific location
     */
    void setPosition(const std::shared_ptr<CommonAPI::ClientId> _client, ::v4::org::genivi::navigation::NavigationTypes::Handle _sessionHandle, MapMatchedPosition::PositionItemDict _position, setPositionReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getAddress = This method returns the current address
     */
    void getCurrentAddress(const std::shared_ptr<CommonAPI::ClientId> _client, std::vector<MapMatchedPosition::AddressItemKey> _valuesToReturn, getCurrentAddressReply_t _reply){
        MapMatchedPosition::getCurrentAddressError _error = MapMatchedPosition::getCurrentAddressError::OK;
        MapMatchedPosition::AddressItemDict ret;
        std::vector< MapMatchedPosition::AddressItemKey >::const_iterator it;
        for (it = _valuesToReturn.begin(); it < _valuesToReturn.end(); it++) {
            if (*it == MapMatchedPosition::AddressItemKey::STREET && mp_street_name) {
                ret[*it]= std::string(mp_street_name);
            }
        }
        _reply(_error, ret);
    }

    /**
     * description: positionOnSegment = This method returns the vehicle position on a route segment
     */
    void getPositionOnSegment(const std::shared_ptr<CommonAPI::ClientId> _client, std::vector<MapMatchedPosition::PositionOnSegmentKey> _valuesToReturn, getPositionOnSegmentReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

    /**
     * description: getStatus = This method returns the current status
     */
    void getStatus(const std::shared_ptr<CommonAPI::ClientId> _client, std::vector<MapMatchedPosition::PositionStatus> _valuesToReturn, getStatusReply_t _reply){
        throw DBus::ErrorNotSupported("Not yet supported");
    }

private:
    CommonTypes::Version m_version;
    MapMatchedPosition::SimulationStatus m_simulationMode;

    void
    demo_update(bool start)
    {
        struct attr vehicle_null_speed={attr_speed,(char *)0};
        struct vehicle *vehicle=get_vehicle("demo:");
        vehicle_set_attr(vehicle, start?&vehicle_speed:&vehicle_null_speed);
    }

};

static std::shared_ptr<MapMatchedPositionServerStub> myServiceMapMatchedPosition;

static void
tracking_attr_position_coord_geo(void)
{
	struct attr position_coord_geo, current_item;
    dbg(lvl_debug,"enter\n");
	if (tracking_get_attr(tracking, attr_position_coord_geo, &position_coord_geo, NULL)) {
        std::vector< MapMatchedPosition::PositionItemKey >changes;
        changes.push_back(MapMatchedPosition::PositionItemKey::LATITUDE);
        changes.push_back(MapMatchedPosition::PositionItemKey::LONGITUDE);
        changes.push_back(MapMatchedPosition::PositionItemKey::SPEED);
        changes.push_back(MapMatchedPosition::PositionItemKey::HEADING);
        myServiceMapMatchedPosition->firePositionUpdateEvent(changes);
	}
	if (tracking_get_attr(tracking, attr_current_item, &current_item, NULL) && current_item.u.item) {
		if (!item_is_equal(last_item, *current_item.u.item)) {
			last_item=*current_item.u.item;
			char *new_street_name=NULL;
			struct map_rect *mr=map_rect_new(last_item.map, NULL);
			struct item *item;
			if (mr && (item = map_rect_get_item_byid(mr, last_item.id_hi, last_item.id_lo))) {
				struct attr label;
				if (item_attr_get(item, attr_label, &label)) 
					new_street_name=label.u.str;
			}
            if (g_strcmp0(new_street_name, mp_street_name)) {
                g_free(mp_street_name);
                mp_street_name=g_strdup(new_street_name);
                std::vector< MapMatchedPosition::AddressItemKey >changes;
                changes.push_back(MapMatchedPosition::AddressItemKey::STREET);
                myServiceMapMatchedPosition->fireAddressUpdateEvent(changes);
			}
			if (mr)
				map_rect_destroy(mr);
		}
	}
}

static void
navit_attr_navit(struct navit *navit)
{
	struct attr tracking_attr;
	if (navit_get_attr(navit, attr_trackingo, &tracking_attr, NULL)) {
		tracking=tracking_attr.u.tracking;
		tracking_cb.type=attr_callback;
		tracking_cb.u.callback=callback_new_attr_0(callback_cast(tracking_attr_position_coord_geo), attr_position_coord_geo);
		tracking_add_attr(tracking, &tracking_cb);
	}
}

static void
config_attr_navit(struct navit *navit, int add)
{
	if (!tracking && !navit_cb.u.callback && add) {
		navit_cb.type=attr_callback;
		navit_cb.u.callback=callback_new_attr_0(callback_cast(navit_attr_navit), attr_navit);
		navit_add_attr(navit, &navit_cb);
	}
}

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 = "MapMatchedPosition";

    myServiceMapMatchedPosition = std::make_shared<MapMatchedPositionServerStub>();

    bool successfullyRegistered = runtime->registerService(domain, instance, myServiceMapMatchedPosition);
    while (!successfullyRegistered) {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        successfullyRegistered = runtime->registerService(domain, instance, myServiceMapMatchedPosition);
    }
	config_cb.type=attr_callback;
	config_cb.u.callback=callback_new_attr_0(callback_cast(config_attr_navit), attr_navit);
	config_add_attr(config, &config_cb);
}