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

#include <cstring>

#include "BinderClient.h"
#include "JsonRequestGenerator.h"
#include "JsonResponseAnalyzer.h"

#include "traces.h"

/**
 *  @brief constructor
 */
BinderClient::BinderClient() : navicoreListener(nullptr)
{
	requestMng = new RequestManage();
}

/**
 *  @brief Destructor
 */
BinderClient::~BinderClient()
{
	delete requestMng;
}

/**
 *  @brief Connect with the Binder server
 */
bool BinderClient::ConnectServer(std::string url, naviapi::NavicoreListener* listener)
{
	this->navicoreListener = listener;

	if( !requestMng->Connect(url.c_str(), this))
	{
		TRACE_ERROR("cannot connect to binding service.\n");
		return false;
	}

	return true;
}

/**
 *  @brief Call Genivi's GetPosition via Binder and get the result
 */
void BinderClient::NavicoreGetPosition(const std::vector< int32_t >& valuesToReturn)
{
	// Check if it is connected
	if( requestMng->IsConnect() )
	{
		// JSON request generation
		std::string req_json = JsonRequestGenerator::CreateRequestGetPosition(valuesToReturn);

		// Send request
		if( requestMng->CallBinderAPI(API_NAME, VERB_GETPOSITION, req_json.c_str()) )
		{
			TRACE_DEBUG("navicore_getposition success.\n");
		}
		else
		{
			TRACE_ERROR("navicore_getposition failed.\n");
		}
	}
}

/**
 *  @brief Get route handle
 */
void BinderClient::NavicoreGetAllRoutes()
{
	// Check if it is connected
	if( requestMng->IsConnect() )
	{
		// JSON request generation
		std::string req_json = JsonRequestGenerator::CreateRequestGetAllRoutes();

		// Send request
		if( requestMng->CallBinderAPI(API_NAME, VERB_GETALLROUTES, req_json.c_str()) )
		{
			TRACE_DEBUG("navicore_getallroutes success.\n");
		}
		else
		{
			TRACE_ERROR("navicore_getallroutes failed.\n");
		}
	}
}

/**
 *  @brief Generate route handle
 */
void BinderClient::NavicoreCreateRoute(const uint32_t& sessionHandle)
{
	// Check if it is connected
	if( requestMng->IsConnect() )
	{
		// JSON request generation
		uint32_t session = requestMng->GetSessionHandle();
		std::string req_json = JsonRequestGenerator::CreateRequestCreateRoute(&session);

		// Send request
		if( requestMng->CallBinderAPI(API_NAME, VERB_CREATEROUTE, req_json.c_str()) )
		{
			TRACE_DEBUG("navicore_createroute success.\n");
		}
		else
		{
			TRACE_ERROR("navicore_createroute failed.\n");
		}
	}
}

/**
 *  @brief  Pause demo
 */
void BinderClient::NavicorePauseSimulation(const uint32_t& sessionHandle)
{
	// Check if it is connected
	if( requestMng->IsConnect() )
	{
		// JSON request generation
		uint32_t session = requestMng->GetSessionHandle();
		std::string req_json = JsonRequestGenerator::CreateRequestPauseSimulation(&session);

		// Send request
		if( requestMng->CallBinderAPI(API_NAME, VERB_PAUSESIMULATION, req_json.c_str()) )
		{
			TRACE_DEBUG("navicore_pausesimulationmode success.\n");
		}
		else
		{
			TRACE_ERROR("navicore_pausesimulationmode failed.\n");
		}
	}
}

/**
 *  @brief  Simulation mode setting
 */
void BinderClient::NavicoreSetSimulationMode(const uint32_t& sessionHandle, const bool& activate)
{
	// Check if it is connected
	if( requestMng->IsConnect() )
	{
		// JSON request generation
		uint32_t session = requestMng->GetSessionHandle();
		std::string req_json = JsonRequestGenerator::CreateRequestSetSimulationMode(&session, &activate);

		// Send request
		if( requestMng->CallBinderAPI(API_NAME, VERB_SETSIMULATIONMODE, req_json.c_str()) )
		{
			TRACE_DEBUG("navicore_setsimulationmode success.\n");
		}
		else
		{
			TRACE_ERROR("navicore_setsimulationmode failed.\n");
		}
	}
}

/**
 *  @brief  Delete route information
 */
void BinderClient::NavicoreCancelRouteCalculation(const uint32_t& sessionHandle, const uint32_t& routeHandle)
{
	// Check if it is connected
	if( requestMng->IsConnect() )
	{
		// JSON request generation
		uint32_t session = requestMng->GetSessionHandle();
		std::string req_json = JsonRequestGenerator::CreateRequestCancelRouteCalculation(&session, &routeHandle);

		// Send request
		if( requestMng->CallBinderAPI(API_NAME, VERB_CANCELROUTECALCULATION, req_json.c_str()) )
		{
			TRACE_DEBUG("navicore_cancelroutecalculation success.\n");
		}
		else
		{
			TRACE_ERROR("navicore_cancelroutecalculation failed.\n");
		}
	}
}

/**
 *  @brief Destination setting
 */
void BinderClient::NavicoreSetWaypoints(const uint32_t& sessionHandle, const uint32_t& routeHandle, const bool& startFromCurrentPosition, const std::vector<naviapi::Waypoint>& waypointsList)
{
	// Check if it is connected
	if( requestMng->IsConnect() )
	{
		// JSON request generation
		uint32_t session = requestMng->GetSessionHandle();
		uint32_t route = requestMng->GetRouteHandle();
		std::string req_json = JsonRequestGenerator::CreateRequestSetWaypoints(&session, &route, 
					&startFromCurrentPosition, &waypointsList);

		// Send request
		if( requestMng->CallBinderAPI(API_NAME, VERB_SETWAYPOINTS, req_json.c_str()) )
		{
			TRACE_DEBUG("navicore_setwaypoints success.\n");
		}
		else
		{
			TRACE_ERROR("navicore_setwaypoints failed.\n");
		}
	}
}

/**
 *  @brief  Route calculation processing
 */
void BinderClient::NavicoreCalculateRoute(const uint32_t& sessionHandle, const uint32_t& routeHandle)
{
	// Check if it is connected
	if( requestMng->IsConnect() )
	{
		// JSON request generation
		uint32_t session = requestMng->GetSessionHandle();
		uint32_t route = requestMng->GetRouteHandle();
		std::string req_json = JsonRequestGenerator::CreateRequestCalculateroute(&session, &route);

		// Send request
		if( requestMng->CallBinderAPI(API_NAME, VERB_CALCULATEROUTE, req_json.c_str()) )
		{
			TRACE_DEBUG("navicore_calculateroute success.\n");
		}
		else
		{
			TRACE_ERROR("navicore_calculateroute failed.\n");
		}
	}
}

/**
 *  @brief  Retrieve session information
 *  @return Map of session information
 */
void BinderClient::NavicoreGetAllSessions()
{
	// Check if it is connected
	if( requestMng->IsConnect() )
	{
		// JSON request generation
		std::string req_json = JsonRequestGenerator::CreateRequestGetAllSessions();

		// Send request
		if( requestMng->CallBinderAPI(API_NAME, VERB_GETALLSESSIONS, req_json.c_str()) )
		{
			TRACE_DEBUG("navicore_getallsessions success.\n");
		}
		else
		{
			TRACE_ERROR("navicore_getallsessions failed.\n");
		}
	}
}

void BinderClient::OnReply(struct json_object* reply)
{
	struct json_object* requestObject = nullptr;
	json_object_object_get_ex(reply, "request", &requestObject);

	struct json_object* infoObject = nullptr;
	json_object_object_get_ex(requestObject, "info", &infoObject);

	const char* info = json_object_get_string(infoObject);

	char tmpVerb[256];
	strcpy(tmpVerb, info);

	// Create a new JSON response
	const char* json_str = json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PRETTY);
	std::string response_json = std::string( json_str );

	if (strcmp(VERB_GETALLSESSIONS, tmpVerb) == 0)
	{
		std::map<uint32_t, std::string> ret = JsonResponseAnalyzer::AnalyzeResponseGetAllSessions(response_json);

		// keep session handle
		requestMng->SetSessionHandle( ret.begin()->first );

		this->navicoreListener->getAllSessions_reply(ret);
	}
	else if (strcmp(VERB_GETPOSITION, tmpVerb) == 0)
	{
		std::map< int32_t, naviapi::variant > ret = JsonResponseAnalyzer::AnalyzeResponseGetPosition(response_json);

		this->navicoreListener->getPosition_reply(ret);
	}
	else if (strcmp(VERB_GETALLROUTES, tmpVerb) == 0)
	{
		std::vector< uint32_t > ret = JsonResponseAnalyzer::AnalyzeResponseGetAllRoutes(response_json);

		// route handle
		if(ret.size() > 0)
		{
			requestMng->SetRouteHandle(ret[0]);
		}

		this->navicoreListener->getAllRoutes_reply(ret);
	}
	else if (strcmp(VERB_CREATEROUTE, tmpVerb) == 0)
	{
		uint32_t ret = JsonResponseAnalyzer::AnalyzeResponseCreateRoute(response_json);

		// keep route handle
		requestMng->SetRouteHandle(ret);

		this->navicoreListener->createRoute_reply(ret);
	}
}