aboutsummaryrefslogtreecommitdiffstats
path: root/src/genivi_request.cpp
blob: a485667674ec5686ef8a5852b0da69bd0363f654 (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
// Copyright 2017 AW SOFTWARE CO.,LTD
// Copyright 2017 AISIN AW CO.,LTD

#include "genivi/navicore.h"
#include "genivi/genivi-navicore-constants.h"
#include "genivi_request.h"
#include <stdio.h>
#include <exception>
#include <dbus-c++-1/dbus-c++/dbus.h>

/**
 *  @brief Destructor
 */
GeniviRequest::~GeniviRequest()
{
	delete (Navicore*)navicore_;
	navicore_ = NULL;
}

/**
 *  @brief  DBus session creation
 */
void GeniviRequest::CreateDBusSession( )
{
	try
	{
		static DBus::BusDispatcher dispatcher;
		DBus::default_dispatcher = &dispatcher;
		DBus::Connection conn = DBus::Connection::SessionBus();

		navicore_ = new Navicore(conn, "/org/genivi/navicore", "org.agl.naviapi");
	}
	catch(const std::exception& e)
	{
		fprintf(stderr, "Error:%s\n", e.what());
	}
}

/**
 *  @brief      Check connection status
 *  @return     Presence / absence of connection
 */
bool GeniviRequest::CheckSession()
{
	if(this->navicore_ == NULL)
	{
		this->CreateDBusSession();
	}

	try
	{
		// Get connection status
		DBus::Connection conn = ((Navicore*)navicore_)->conn();
		bool isConnect = conn.connected();

		// If it is not connected, it issues an error
		if(!isConnect)
		{
			fprintf(stderr, "Service has no session.\n");
		}

		return isConnect;
	}
	catch(const std::exception& e)
	{
		fprintf(stderr, "Error:%s\n", e.what());
		return false;
	}
}

/**
 *  @brief      Call GeniviAPI GetPosition to get information
 *  @param[in]  valuesToReturn Key arrangement of information acquired from Genivi
 *  @return     Map information on key and value of information acquired from Genivi
 */
std::map< int32_t, double > GeniviRequest::NavicoreGetPosition( const std::vector< int32_t >& valuesToReturn )
{
	std::map< int32_t, double > ret;

	if( !CheckSession() )
	{
		return ret;
	}

	try
	{
		std::map< int32_t, ::DBus::Struct< uint8_t, ::DBus::Variant > >::iterator it;
		std::map< int32_t, ::DBus::Struct< uint8_t, ::DBus::Variant > > PosList =
		    ((Navicore*)navicore_)->GetPosition(valuesToReturn);
		for (it = PosList.begin(); it != PosList.end(); it++)
		{
			if (it->first == NAVICORE_LATITUDE || it->second._1 == NAVICORE_LATITUDE)
			{
				ret[it->first] = it->second._2.reader().get_double();
			}
			else if (it->first == NAVICORE_LONGITUDE || it->second._1 == NAVICORE_LONGITUDE)
			{
				ret[it->first] = it->second._2.reader().get_double();
			}
			else if (it->first == NAVICORE_HEADING || it->second._1 == NAVICORE_HEADING)
			{
				ret[it->first] = it->second._2.reader().get_uint32();
			}
#if 0 // no supported
			else if (it->first == NAVICORE_TIMESTAMP || it->second._1 == NAVICORE_TIMESTAMP)
			{
				ret[it->first] = it->second._2.reader().get_uint32();
			}
			else if (it->first == NAVICORE_SPEED || it->second._1 == NAVICORE_SPEED)
			{
				ret[it->first] = it->second._2.reader().get_int32();
			}
#endif
			else if (it->first == NAVICORE_SIMULATION_MODE || it->second._1 == NAVICORE_SIMULATION_MODE)
			{
				ret[it->first] = it->second._2.reader().get_bool();
			}
		}
	}
	catch(const std::exception& e)
	{
		fprintf(stderr, "Error:%s\n", e.what());
	}

	return ret;
}

/**
 *  @brief  Call GeniviAPI GetPosition to get information
 *  @return Route handle acquired from Genivi
 */
std::vector< uint32_t > GeniviRequest::NavicoreGetAllRoutes( void )
{
	if( !CheckSession() )
	{
		std::vector< uint32_t > no_route;
		return no_route;
	}

	std::vector< uint32_t > allRoutes;
	try
	{
		allRoutes = ((Navicore*)navicore_)->GetAllRoutes();
	}
	catch(const std::exception& e)
	{
		fprintf(stderr, "Error:%s\n", e.what());
	}

	return allRoutes;
}


/**
 *  @brief      Call GeniviAPI GetPosition to get information
 *  @param[in]  sessionHandle Session handle
 *  @return     Route handle acquired from Genivi
 */
uint32_t GeniviRequest::NavicoreCreateRoute( const uint32_t& sessionHandle )
{
	if( !CheckSession() )
	{
		return 0;
	}

	uint32_t routeHandle = 0;
	try
	{
		routeHandle = ((Navicore*)navicore_)->CreateRoute(sessionHandle);
	}
	catch(const std::exception& e)
	{
		fprintf(stderr, "Error:%s\n", e.what());
	}

	return routeHandle;
}

/**
 *  @brief      Call GeniviAPI PauseSimulation
 *  @param[in]  sessionHandle Session handle
 */
void GeniviRequest::NavicorePauseSimulation( const uint32_t& sessionHandle )
{
	if( !CheckSession() )
	{
		return;
	}

	try
	{
		((Navicore*)navicore_)->PauseSimulation(sessionHandle);
	}
	catch(const std::exception& e)
	{
		fprintf(stderr, "Error:%s\n", e.what());
	}
}


/**
 *  @brief      Call GeniviAPI SetSimulationMode
 *  @param[in]  sessionHandle Session handle
 *  @param[in]  activate Simulation mode enabled / disabled
 */
void GeniviRequest::NavicoreSetSimulationMode( const uint32_t& sessionHandle, const bool& activate )
{
	if( !CheckSession() )
	{
		return;
	}

	try
	{
		((Navicore*)navicore_)->SetSimulationMode(sessionHandle, activate);
	}
	catch(const std::exception& e)
	{
		fprintf(stderr, "Error:%s\n", e.what());
	}
}


/**
 *  @brief      Call GeniviAPI SetSimulationMode
 *  @param[in]  sessionHandle Session handle
 *  @param[in]  routeHandle Route handle
 */
void GeniviRequest::NavicoreCancelRouteCalculation( const uint32_t& sessionHandle, const uint32_t& routeHandle )
{
	if( !CheckSession() )
	{ 
		return;
	}

	try
	{
		((Navicore*)navicore_)->CancelRouteCalculation(sessionHandle, routeHandle);
	}
	catch(const std::exception& e)
	{
		fprintf(stderr, "Error:%s\n", e.what());
	}
}

/**
 *  @brief      Call GeniviAPI SetWaypoints
 *  @param[in]  sessionHandle Session handle
 *  @param[in]  routeHandle Route handle
 *  @param[in]  startFromCurrentPosition Whether or not to draw a route from the position of the vehicle
 *  @param[in]  waypointsList Destination coordinates
 */
void GeniviRequest::NavicoreSetWaypoints( const uint32_t& sessionHandle, const uint32_t& routeHandle,
                    const bool& startFromCurrentPosition, const std::vector<Waypoint>& waypointsList )
{
	if( !CheckSession() )
	{
		return;
	}

	std::vector<Waypoint>::const_iterator it;
	std::vector< std::map< int32_t, ::DBus::Struct< uint8_t, ::DBus::Variant > > > wpl;

	fprintf(stdout, "session: %d, route: %d, startFromCurrentPosition: %d\n",
	    sessionHandle, routeHandle, startFromCurrentPosition);

	for (it = waypointsList.begin(); it != waypointsList.end(); it++)
	{
		std::map< int32_t, ::DBus::Struct< uint8_t, ::DBus::Variant > > Point;
		::DBus::Struct< uint8_t, ::DBus::Variant > VarLat, VarLon;

		VarLat._1 = NAVICORE_LATITUDE;
		VarLat._2.writer().append_double(std::get<0>(*it));
		fprintf(stdout, "VarLat._1 : %x, VarLat._2 : %lf\n", VarLat._1, VarLat._2.reader().get_double());

		VarLon._1 = NAVICORE_LONGITUDE;
		VarLon._2.writer().append_double(std::get<1>(*it));
		fprintf(stdout, "VarLon._1 : %x, VarLon._2 : %lf\n", VarLon._1, VarLon._2.reader().get_double());

		Point[NAVICORE_LATITUDE] = VarLat;
		Point[NAVICORE_LONGITUDE] = VarLon;

		wpl.push_back(Point);
	}

	try
	{
		((Navicore*)navicore_)->SetWaypoints(sessionHandle, routeHandle, startFromCurrentPosition, wpl);
	}
	catch(const std::exception& e)
	{
		fprintf(stderr, "Error:%s\n", e.what());
	}
}

/**
 *  @brief      Call GeniviAPI CalculateRoute
 *  @param[in]  sessionHandle Session handle
 *  @param[in]  routeHandle Route handle
 */
void GeniviRequest::NavicoreCalculateRoute( const uint32_t& sessionHandle, const uint32_t& routeHandle )
{
	if( !CheckSession() )
	{
		return;
	}

	try
	{
		((Navicore*)navicore_)->CalculateRoute(sessionHandle, routeHandle);
	}
	catch(const std::exception& e)
	{
		fprintf(stderr, "Error:%s\n", e.what());
	}
}


/**
 *  @brief  Call GeniviAPI CalculateRoute
 *  @return Map information on key and value of information acquired from Genivi
 */
std::map<uint32_t, std::string> GeniviRequest::NavicoreGetAllSessions()
{
	std::map<uint32_t, std::string> ret;

	if( !CheckSession() )
	{
		return ret;
	}

	std::vector< ::DBus::Struct< uint32_t, std::string > > ncAllSessions;
	std::vector< ::DBus::Struct< uint32_t, std::string > >::iterator it;

	try
	{
		ncAllSessions = ((Navicore*)navicore_)->GetAllSessions();
		for (it = ncAllSessions.begin(); it != ncAllSessions.end(); it++)
		{
			ret[it->_1] = it->_2;
		}
	}
	catch(const std::exception& e)
	{
		fprintf(stderr, "Error:%s\n", e.what());
	}

	return ret;
}