summaryrefslogtreecommitdiffstats
path: root/src/api.cpp
blob: 8573b4705131045e7032247295d3755948bf05af (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
// Copyright 2017 AW SOFTWARE CO.,LTD
// Copyright 2017 AISIN AW CO.,LTD

#include <string.h>

#include "binder_reply.h"
#include "genivi_request.h"
#include "analyze_request.h"
#include "genivi/genivi-navicore-constants.h"

#define AFB_BINDING_VERSION 2

extern "C" {
	#include <afb/afb-binding.h>
}

/**
 *  Variable declaration
 */
GeniviRequest* geniviRequest;	// Send request to Genivi
BinderReply* binderReply;	// Convert Genivi response result to json format
AnalyzeRequest* analyzeRequest;	// Analyze BinderClient's request and create arguments to pass to GeniviAPI

/**
 *  @brief navicore_getposition request callback
 *  @param[in] req Request from client
 */
void OnRequestNavicoreGetPosition(afb_req req)
{
	AFB_REQ_NOTICE(req, "--> Start %s()", __func__);
	AFB_REQ_DEBUG(req, "request navicore_getposition");

	// Request of Json format request
	json_object* req_json = afb_req_json(req);
	const char* req_json_str = json_object_to_json_string(req_json);
	AFB_REQ_NOTICE(req, "req_json_str = %s", req_json_str);

	// Request analysis and create arguments to pass to Genivi
	std::vector< int32_t > Params;
	if( !analyzeRequest->CreateParamsGetPosition( req_json_str, Params ))
	{
		afb_req_fail(req, "failed", "navicore_getposition Bad Request");
		return;
	}

	// GENIVI API call
	std::map< int32_t, double > posList = geniviRequest->NavicoreGetPosition( Params );

	// Convert to json style response
	APIResponse response = binderReply->ReplyNavicoreGetPosition( posList );

	// On success
	if(response.isSuccess)
	{
		AFB_REQ_NOTICE(req, "res_json_str = %s", json_object_to_json_string(response.json_data));
		// Return success to BinderClient
		afb_req_success(req, response.json_data, "navicore_getposition");
	}
	else
	{
		AFB_REQ_ERROR(req, "%s - %s:%d", response.errMessage.c_str(), __FILE__, __LINE__);
		afb_req_fail(req, "failed", "navicore_getposition Bad Request");

		// Json object release
		json_object_put(response.json_data);
	}

	AFB_REQ_NOTICE(req, "<-- End %s()", __func__);
}


/**
 *  @brief navicore_getallroutes request callback
 *  @param[in] req Request from client
 */
void OnRequestNavicoreGetAllRoutes(afb_req req)
{
	AFB_REQ_NOTICE(req, "--> Start %s()", __func__);
	AFB_REQ_DEBUG(req, "request navicore_getallroutes");

	// No request information in json format
	AFB_REQ_NOTICE(req, "req_json_str = none");

	// GENEVI API call
	std::vector< uint32_t > allRoutes = geniviRequest->NavicoreGetAllRoutes();

	// Convert to json style response
	APIResponse response = binderReply->ReplyNavicoreGetAllRoutes( allRoutes );

	// On success
	if(response.isSuccess)
	{
		AFB_REQ_NOTICE(req, "res_json_str = %s", json_object_to_json_string(response.json_data));
		// Return success to BinderClient
		afb_req_success(req, response.json_data, "navicore_getallroutes");
	}
	else
	{
		AFB_REQ_ERROR(req, "%s - %s:%d", response.errMessage.c_str(), __FILE__, __LINE__);
		afb_req_fail(req, "failed", "navicore_getallroutes Bad Request");

		// json object release
		json_object_put(response.json_data);
	}

	AFB_REQ_NOTICE(req, "<-- End %s()", __func__);
}


/**
 *  @brief navicore_createroute request callback
 *  @param[in] req Request from client
 */
void OnRequestNavicoreCreateRoute(afb_req req)
{
	AFB_REQ_NOTICE(req, "--> Start %s ", __func__);
	AFB_REQ_DEBUG(req, "request navicore_createroute");

	// Request of json format request
	json_object* req_json = afb_req_json(req);
	const char* req_json_str = json_object_to_json_string(req_json);
	AFB_REQ_NOTICE(req, "req_json_str = %s", req_json_str);

	// Request analysis and create arguments to pass to Genivi
	uint32_t sessionHdl = 0;
	if( !analyzeRequest->CreateParamsCreateRoute( req_json_str, sessionHdl ))
	{
		afb_req_fail(req, "failed", "navicore_createroute Bad Request");
		return;
	}

	// GENEVI API call
	uint32_t routeHdl = geniviRequest->NavicoreCreateRoute( sessionHdl );

	// Convert to json style response
	APIResponse response = binderReply->ReplyNavicoreCreateRoute( routeHdl );

	// On success
	if(response.isSuccess)
	{
		AFB_REQ_NOTICE(req, "res_json_str = %s", json_object_to_json_string(response.json_data));
		// Return success to BinderClient
		afb_req_success(req, response.json_data, "navicore_createroute");
	}
	else
	{
		AFB_REQ_ERROR(req, "%s - %s:%d", response.errMessage.c_str(), __FILE__, __LINE__);
		afb_req_fail(req, "failed", "navicore_createroute Bad Request");

		// json object release
		json_object_put(response.json_data);
	}

	AFB_REQ_NOTICE(req, "<-- End %s()", __func__);
}


/**
 *  @brief navicore_pausesimulation request callback
 *  @param[in] req Request from client
 */
void OnRequestNavicorePauseSimulation(afb_req req)
{
	AFB_REQ_NOTICE(req, "--> Start %s()", __func__);
	AFB_REQ_DEBUG(req, "request navicore_pausesimulation");

	// Request of json format request
	json_object* req_json = afb_req_json(req);
	const char* req_json_str = json_object_to_json_string(req_json);
	AFB_REQ_NOTICE(req, "req_json_str = %s", req_json_str);

	// Request analysis and create arguments to pass to Genivi
	uint32_t sessionHdl = 0;
	if( !analyzeRequest->CreateParamsPauseSimulation( req_json_str, sessionHdl ))
	{
		afb_req_fail(req, "failed", "navicore_pausesimulation Bad Request");
		return;
	}

	// GENEVI API call
	geniviRequest->NavicorePauseSimulation( sessionHdl );

	// No reply unnecessary API for conversion to json format response is unnecessary
	AFB_REQ_NOTICE(req, "res_json_str = none");

	// Return success to BinderClient
	afb_req_success(req, NULL, "navicore_pausesimulation");

	AFB_REQ_NOTICE(req, "<-- End %s()", __func__);
}


/**
 *  @brief navicore_setsimulationmode request callback
 *  @param[in] req Request from client
 */
void OnRequestNavicoreSetSimulationMode(afb_req req)
{
	AFB_REQ_NOTICE(req, "--> Start %s()", __func__);
	AFB_REQ_DEBUG(req, "request navicore_setsimulationmode");

	// Request of json format request
	json_object* req_json = afb_req_json(req);
	const char* req_json_str = json_object_to_json_string(req_json);
	AFB_REQ_NOTICE(req, "req_json_str = %s", req_json_str);

	// Request analysis and create arguments to pass to Genivi
	uint32_t sessionHdl = 0;
	bool simuMode = false;
	if( !analyzeRequest->CreateParamsSetSimulationMode( req_json_str, sessionHdl, simuMode ))
	{
		afb_req_fail(req, "failed", "navicore_setsimulationmode Bad Request");
		return;
	}

	// GENEVI API call
	geniviRequest->NavicoreSetSimulationMode( sessionHdl, simuMode );

	// No reply unnecessary API for conversion to json format response is unnecessary
	AFB_REQ_NOTICE(req, "res_json_str = none");

	// Return success to BinderClient
	afb_req_success(req, NULL, "navicore_setsimulationmode");

	AFB_REQ_NOTICE(req, "<-- End %s()", __func__);
}


/**
 *  @brief navicore_cancelroutecalculation request callback
 *  @param[in] req Request from client
 */
void OnRequestNavicoreCancelRouteCalculation(afb_req req)
{
	AFB_REQ_NOTICE(req, "--> Start %s()", __func__);
	AFB_REQ_DEBUG(req, "request navicore_cancelroutecalculation");

	// Request of Json format request
	json_object* req_json = afb_req_json(req);
	const char* req_json_str = json_object_to_json_string(req_json);
	AFB_REQ_NOTICE(req, "req_json_str = %s", req_json_str);

	// Request analysis and create arguments to pass to Genivi
	uint32_t sessionHdl = 0;
	uint32_t routeHdl = 0;
	if( !analyzeRequest->CreateParamsCancelRouteCalculation( req_json_str, sessionHdl, routeHdl ))
	{
		afb_req_fail(req, "failed", "navicore_cancelroutecalculation Bad Request");
		return;
	}

	// GENEVI API call
	geniviRequest->NavicoreCancelRouteCalculation( sessionHdl, routeHdl );

	// No reply unnecessary API for conversion to json format response is unnecessary
	AFB_REQ_NOTICE(req, "res_json_str = none");

	// Return success to BinderClient
	afb_req_success(req, NULL, "navicore_cancelroutecalculation");

	AFB_REQ_NOTICE(req, "<-- End %s()", __func__);
}


/**
 *  @brief navicore_setwaypoints request callback
 *  @param[in] req Request from client
 */
void OnRequestNavicoreWaypoints(afb_req req)
{
	AFB_REQ_NOTICE(req, "--> Start %s()", __func__);
	AFB_REQ_DEBUG(req, "request navicore_setwaypoints");

	// Request of Json format request
	json_object* req_json = afb_req_json(req);
	const char* req_json_str = json_object_to_json_string(req_json);
	AFB_REQ_NOTICE(req, "req_json_str = %s", req_json_str);

	// Request analysis and create arguments to pass to Genivi
	uint32_t sessionHdl = 0;
	uint32_t routeHdl = 0;
	bool currentPos = false;
	std::vector<Waypoint> waypointsList;
	if( !analyzeRequest->CreateParamsSetWaypoints( req_json_str, sessionHdl, routeHdl, currentPos, waypointsList ))
	{
		afb_req_fail(req, "failed", "navicore_setwaypoints Bad Request");
		return;
	}

	// GENEVI API call
	geniviRequest->NavicoreSetWaypoints( sessionHdl, routeHdl, currentPos, waypointsList );

	// No reply unnecessary API for conversion to json format response is unnecessary
	AFB_REQ_NOTICE(req, "res_json_str = none");

	// Return success to BinderClient
	afb_req_success(req, NULL, "navicore_setwaypoints");

	AFB_REQ_NOTICE(req, "<-- End %s()", __func__);
}


/**
 *  @brief navicore_calculateroute request callback
 *  @param[in] req Request from client
 */
void OnRequestNavicoreCalculateRoute(afb_req req)
{
	AFB_REQ_NOTICE(req, "--> Start %s()", __func__);
	AFB_REQ_DEBUG(req, "request navicore_calculateroute");

	// Request of Json format request
	json_object* req_json = afb_req_json(req);
	const char* req_json_str = json_object_to_json_string(req_json);
	AFB_REQ_NOTICE(req, "req_json_str = %s", req_json_str);

	// Request analysis and create arguments to pass to Genivi
	uint32_t sessionHdl = 0;
	uint32_t routeHdl = 0;
	if( !analyzeRequest->CreateParamsCalculateRoute( req_json_str, sessionHdl, routeHdl ))
	{
		afb_req_fail(req, "failed", "navicore_calculateroute Bad Request");
		return;
	}

	// GENEVI API call
	geniviRequest->NavicoreCalculateRoute( sessionHdl, routeHdl );

	// No reply unnecessary API for conversion to json format response is unnecessary
	AFB_REQ_NOTICE(req, "res_json_str = none");

	// Return success to BinderClient
	afb_req_success(req, NULL, "navicore_calculateroute");

	AFB_REQ_NOTICE(req, "<-- End %s()", __func__);
}


/**
 *  @brief navicore_getallsessions request callback
 *  @param[in] req Request from client
 */
void OnRequestNavicoreGetAllSessions(afb_req req)
{
	AFB_REQ_NOTICE(req, "--> Start %s()", __func__);
	AFB_REQ_DEBUG(req, "request navicore_getallsessions");

	// No request information in Json format
	AFB_REQ_NOTICE(req, "req_json_str = none");

	// GENEVI API call
	std::map<uint32_t, std::string> allSessions = geniviRequest->NavicoreGetAllSessions();

	// Convert to json style response
	APIResponse response = binderReply->ReplyNavicoreGetAllSessions( allSessions );

	// On success
	if(response.isSuccess)
	{
		AFB_REQ_NOTICE(req, "res_json_str = %s", json_object_to_json_string(response.json_data));
		// Return success to BinderClient
		afb_req_success(req, response.json_data, "navicore_getallsessions");
	}
	else
	{
		AFB_REQ_ERROR(req, "%s - %s:%d", response.errMessage.c_str(), __FILE__, __LINE__);
		afb_req_fail(req, "failed", "navicore_getallsessions Bad Request");

		// json object release
		json_object_put(response.json_data);
	}

	AFB_REQ_NOTICE(req, "<-- End %s()", __func__);
}


/**
 *  @brief Callback called at service startup
 */
int Init()
{
	// Create instance
	geniviRequest   = new GeniviRequest();
	binderReply	 = new BinderReply();
	analyzeRequest  = new AnalyzeRequest();
	
	return 0;
}

/**
 *  @brief API definition
 */
const afb_verb_v2 verbs[] = 
{
	 { verb : "navicore_getposition",			callback : OnRequestNavicoreGetPosition },
	 { verb : "navicore_getallroutes",		   callback : OnRequestNavicoreGetAllRoutes },
	 { verb : "navicore_createroute",			callback : OnRequestNavicoreCreateRoute },
	 { verb : "navicore_pausesimulation",		callback : OnRequestNavicorePauseSimulation },
	 { verb : "navicore_setsimulationmode",	  callback : OnRequestNavicoreSetSimulationMode },
	 { verb : "navicore_cancelroutecalculation", callback : OnRequestNavicoreCancelRouteCalculation },
	 { verb : "navicore_setwaypoints",		   callback : OnRequestNavicoreWaypoints },
	 { verb : "navicore_calculateroute",		 callback : OnRequestNavicoreCalculateRoute },
	 { verb : "navicore_getallsessions",		 callback : OnRequestNavicoreGetAllSessions },
	 { verb : NULL }
};

/**
 *  @brief Service definition
 */
const afb_binding_v2 afbBindingV2 = 
{
	 "naviapi",
	 "",
	 "",
	 verbs,
	 NULL,
	 Init
};