add autobuild tree
[apps/agl-service-navigation.git] / libnavi / src / BinderClient.cpp
1 // Copyright 2017 AW SOFTWARE CO.,LTD
2 // Copyright 2017 AISIN AW CO.,LTD
3
4 #include <cstring>
5
6 #include "BinderClient.h"
7 #include "JsonRequestGenerator.h"
8 #include "JsonResponseAnalyzer.h"
9
10 #include "traces.h"
11
12 /**
13  *  @brief constructor
14  */
15 BinderClient::BinderClient() : navicoreListener(nullptr)
16 {
17         requestMng = new RequestManage();
18 }
19
20 /**
21  *  @brief Destructor
22  */
23 BinderClient::~BinderClient()
24 {
25         delete requestMng;
26 }
27
28 /**
29  *  @brief Connect with the Binder server
30  */
31 bool BinderClient::ConnectServer(std::string url, naviapi::NavicoreListener* listener)
32 {
33         this->navicoreListener = listener;
34
35         if( !requestMng->Connect(url.c_str(), this))
36         {
37                 TRACE_ERROR("cannot connect to binding service.\n");
38                 return false;
39         }
40
41         return true;
42 }
43
44 /**
45  *  @brief Call Genivi's GetPosition via Binder and get the result
46  */
47 void BinderClient::NavicoreGetPosition(const std::vector< int32_t >& valuesToReturn)
48 {
49         // Check if it is connected
50         if( requestMng->IsConnect() )
51         {
52                 // JSON request generation
53                 std::string req_json = JsonRequestGenerator::CreateRequestGetPosition(valuesToReturn);
54
55                 // Send request
56                 if( requestMng->CallBinderAPI(API_NAME, VERB_GETPOSITION, req_json.c_str()) )
57                 {
58                         TRACE_DEBUG("navicore_getposition success.\n");
59                 }
60                 else
61                 {
62                         TRACE_ERROR("navicore_getposition failed.\n");
63                 }
64         }
65 }
66
67 /**
68  *  @brief Get route handle
69  */
70 void BinderClient::NavicoreGetAllRoutes()
71 {
72         // Check if it is connected
73         if( requestMng->IsConnect() )
74         {
75                 // JSON request generation
76                 std::string req_json = JsonRequestGenerator::CreateRequestGetAllRoutes();
77
78                 // Send request
79                 if( requestMng->CallBinderAPI(API_NAME, VERB_GETALLROUTES, req_json.c_str()) )
80                 {
81                         TRACE_DEBUG("navicore_getallroutes success.\n");
82                 }
83                 else
84                 {
85                         TRACE_ERROR("navicore_getallroutes failed.\n");
86                 }
87         }
88 }
89
90 /**
91  *  @brief Generate route handle
92  */
93 void BinderClient::NavicoreCreateRoute(const uint32_t& sessionHandle)
94 {
95         // Check if it is connected
96         if( requestMng->IsConnect() )
97         {
98                 // JSON request generation
99                 uint32_t session = requestMng->GetSessionHandle();
100                 std::string req_json = JsonRequestGenerator::CreateRequestCreateRoute(&session);
101
102                 // Send request
103                 if( requestMng->CallBinderAPI(API_NAME, VERB_CREATEROUTE, req_json.c_str()) )
104                 {
105                         TRACE_DEBUG("navicore_createroute success.\n");
106                 }
107                 else
108                 {
109                         TRACE_ERROR("navicore_createroute failed.\n");
110                 }
111         }
112 }
113
114 /**
115  *  @brief  Pause demo
116  */
117 void BinderClient::NavicorePauseSimulation(const uint32_t& sessionHandle)
118 {
119         // Check if it is connected
120         if( requestMng->IsConnect() )
121         {
122                 // JSON request generation
123                 uint32_t session = requestMng->GetSessionHandle();
124                 std::string req_json = JsonRequestGenerator::CreateRequestPauseSimulation(&session);
125
126                 // Send request
127                 if( requestMng->CallBinderAPI(API_NAME, VERB_PAUSESIMULATION, req_json.c_str()) )
128                 {
129                         TRACE_DEBUG("navicore_pausesimulationmode success.\n");
130                 }
131                 else
132                 {
133                         TRACE_ERROR("navicore_pausesimulationmode failed.\n");
134                 }
135         }
136 }
137
138 /**
139  *  @brief  Simulation mode setting
140  */
141 void BinderClient::NavicoreSetSimulationMode(const uint32_t& sessionHandle, const bool& activate)
142 {
143         // Check if it is connected
144         if( requestMng->IsConnect() )
145         {
146                 // JSON request generation
147                 uint32_t session = requestMng->GetSessionHandle();
148                 std::string req_json = JsonRequestGenerator::CreateRequestSetSimulationMode(&session, &activate);
149
150                 // Send request
151                 if( requestMng->CallBinderAPI(API_NAME, VERB_SETSIMULATIONMODE, req_json.c_str()) )
152                 {
153                         TRACE_DEBUG("navicore_setsimulationmode success.\n");
154                 }
155                 else
156                 {
157                         TRACE_ERROR("navicore_setsimulationmode failed.\n");
158                 }
159         }
160 }
161
162 /**
163  *  @brief  Delete route information
164  */
165 void BinderClient::NavicoreCancelRouteCalculation(const uint32_t& sessionHandle, const uint32_t& routeHandle)
166 {
167         // Check if it is connected
168         if( requestMng->IsConnect() )
169         {
170                 // JSON request generation
171                 uint32_t session = requestMng->GetSessionHandle();
172                 std::string req_json = JsonRequestGenerator::CreateRequestCancelRouteCalculation(&session, &routeHandle);
173
174                 // Send request
175                 if( requestMng->CallBinderAPI(API_NAME, VERB_CANCELROUTECALCULATION, req_json.c_str()) )
176                 {
177                         TRACE_DEBUG("navicore_cancelroutecalculation success.\n");
178                 }
179                 else
180                 {
181                         TRACE_ERROR("navicore_cancelroutecalculation failed.\n");
182                 }
183         }
184 }
185
186 /**
187  *  @brief Destination setting
188  */
189 void BinderClient::NavicoreSetWaypoints(const uint32_t& sessionHandle, const uint32_t& routeHandle, const bool& startFromCurrentPosition, const std::vector<naviapi::Waypoint>& waypointsList)
190 {
191         // Check if it is connected
192         if( requestMng->IsConnect() )
193         {
194                 // JSON request generation
195                 uint32_t session = requestMng->GetSessionHandle();
196                 uint32_t route = requestMng->GetRouteHandle();
197                 std::string req_json = JsonRequestGenerator::CreateRequestSetWaypoints(&session, &route, 
198                                         &startFromCurrentPosition, &waypointsList);
199
200                 // Send request
201                 if( requestMng->CallBinderAPI(API_NAME, VERB_SETWAYPOINTS, req_json.c_str()) )
202                 {
203                         TRACE_DEBUG("navicore_setwaypoints success.\n");
204                 }
205                 else
206                 {
207                         TRACE_ERROR("navicore_setwaypoints failed.\n");
208                 }
209         }
210 }
211
212 /**
213  *  @brief  Route calculation processing
214  */
215 void BinderClient::NavicoreCalculateRoute(const uint32_t& sessionHandle, const uint32_t& routeHandle)
216 {
217         // Check if it is connected
218         if( requestMng->IsConnect() )
219         {
220                 // JSON request generation
221                 uint32_t session = requestMng->GetSessionHandle();
222                 uint32_t route = requestMng->GetRouteHandle();
223                 std::string req_json = JsonRequestGenerator::CreateRequestCalculateroute(&session, &route);
224
225                 // Send request
226                 if( requestMng->CallBinderAPI(API_NAME, VERB_CALCULATEROUTE, req_json.c_str()) )
227                 {
228                         TRACE_DEBUG("navicore_calculateroute success.\n");
229                 }
230                 else
231                 {
232                         TRACE_ERROR("navicore_calculateroute failed.\n");
233                 }
234         }
235 }
236
237 /**
238  *  @brief  Retrieve session information
239  *  @return Map of session information
240  */
241 void BinderClient::NavicoreGetAllSessions()
242 {
243         // Check if it is connected
244         if( requestMng->IsConnect() )
245         {
246                 // JSON request generation
247                 std::string req_json = JsonRequestGenerator::CreateRequestGetAllSessions();
248
249                 // Send request
250                 if( requestMng->CallBinderAPI(API_NAME, VERB_GETALLSESSIONS, req_json.c_str()) )
251                 {
252                         TRACE_DEBUG("navicore_getallsessions success.\n");
253                 }
254                 else
255                 {
256                         TRACE_ERROR("navicore_getallsessions failed.\n");
257                 }
258         }
259 }
260
261 void BinderClient::OnReply(struct json_object* reply)
262 {
263         struct json_object* requestObject = nullptr;
264         json_object_object_get_ex(reply, "request", &requestObject);
265
266         struct json_object* infoObject = nullptr;
267         json_object_object_get_ex(requestObject, "info", &infoObject);
268
269         const char* info = json_object_get_string(infoObject);
270
271         char tmpVerb[256];
272         strcpy(tmpVerb, info);
273
274         // Create a new JSON response
275         const char* json_str = json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PRETTY);
276         std::string response_json = std::string( json_str );
277
278         if (strcmp(VERB_GETALLSESSIONS, tmpVerb) == 0)
279         {
280                 std::map<uint32_t, std::string> ret = JsonResponseAnalyzer::AnalyzeResponseGetAllSessions(response_json);
281
282                 // keep session handle
283                 requestMng->SetSessionHandle( ret.begin()->first );
284
285                 this->navicoreListener->getAllSessions_reply(ret);
286         }
287         else if (strcmp(VERB_GETPOSITION, tmpVerb) == 0)
288         {
289                 std::map< int32_t, naviapi::variant > ret = JsonResponseAnalyzer::AnalyzeResponseGetPosition(response_json);
290
291                 this->navicoreListener->getPosition_reply(ret);
292         }
293         else if (strcmp(VERB_GETALLROUTES, tmpVerb) == 0)
294         {
295                 std::vector< uint32_t > ret = JsonResponseAnalyzer::AnalyzeResponseGetAllRoutes(response_json);
296
297                 // route handle
298                 if(ret.size() > 0)
299                 {
300                         requestMng->SetRouteHandle(ret[0]);
301                 }
302
303                 this->navicoreListener->getAllRoutes_reply(ret);
304         }
305         else if (strcmp(VERB_CREATEROUTE, tmpVerb) == 0)
306         {
307                 uint32_t ret = JsonResponseAnalyzer::AnalyzeResponseCreateRoute(response_json);
308
309                 // keep route handle
310                 requestMng->SetRouteHandle(ret);
311
312                 this->navicoreListener->createRoute_reply(ret);
313         }
314 }
315