a485667674ec5686ef8a5852b0da69bd0363f654
[apps/agl-service-navigation.git] / src / genivi_request.cpp
1 // Copyright 2017 AW SOFTWARE CO.,LTD
2 // Copyright 2017 AISIN AW CO.,LTD
3
4 #include "genivi/navicore.h"
5 #include "genivi/genivi-navicore-constants.h"
6 #include "genivi_request.h"
7 #include <stdio.h>
8 #include <exception>
9 #include <dbus-c++-1/dbus-c++/dbus.h>
10
11 /**
12  *  @brief Destructor
13  */
14 GeniviRequest::~GeniviRequest()
15 {
16         delete (Navicore*)navicore_;
17         navicore_ = NULL;
18 }
19
20 /**
21  *  @brief  DBus session creation
22  */
23 void GeniviRequest::CreateDBusSession( )
24 {
25         try
26         {
27                 static DBus::BusDispatcher dispatcher;
28                 DBus::default_dispatcher = &dispatcher;
29                 DBus::Connection conn = DBus::Connection::SessionBus();
30
31                 navicore_ = new Navicore(conn, "/org/genivi/navicore", "org.agl.naviapi");
32         }
33         catch(const std::exception& e)
34         {
35                 fprintf(stderr, "Error:%s\n", e.what());
36         }
37 }
38
39 /**
40  *  @brief      Check connection status
41  *  @return     Presence / absence of connection
42  */
43 bool GeniviRequest::CheckSession()
44 {
45         if(this->navicore_ == NULL)
46         {
47                 this->CreateDBusSession();
48         }
49
50         try
51         {
52                 // Get connection status
53                 DBus::Connection conn = ((Navicore*)navicore_)->conn();
54                 bool isConnect = conn.connected();
55
56                 // If it is not connected, it issues an error
57                 if(!isConnect)
58                 {
59                         fprintf(stderr, "Service has no session.\n");
60                 }
61
62                 return isConnect;
63         }
64         catch(const std::exception& e)
65         {
66                 fprintf(stderr, "Error:%s\n", e.what());
67                 return false;
68         }
69 }
70
71 /**
72  *  @brief      Call GeniviAPI GetPosition to get information
73  *  @param[in]  valuesToReturn Key arrangement of information acquired from Genivi
74  *  @return     Map information on key and value of information acquired from Genivi
75  */
76 std::map< int32_t, double > GeniviRequest::NavicoreGetPosition( const std::vector< int32_t >& valuesToReturn )
77 {
78         std::map< int32_t, double > ret;
79
80         if( !CheckSession() )
81         {
82                 return ret;
83         }
84
85         try
86         {
87                 std::map< int32_t, ::DBus::Struct< uint8_t, ::DBus::Variant > >::iterator it;
88                 std::map< int32_t, ::DBus::Struct< uint8_t, ::DBus::Variant > > PosList =
89                     ((Navicore*)navicore_)->GetPosition(valuesToReturn);
90                 for (it = PosList.begin(); it != PosList.end(); it++)
91                 {
92                         if (it->first == NAVICORE_LATITUDE || it->second._1 == NAVICORE_LATITUDE)
93                         {
94                                 ret[it->first] = it->second._2.reader().get_double();
95                         }
96                         else if (it->first == NAVICORE_LONGITUDE || it->second._1 == NAVICORE_LONGITUDE)
97                         {
98                                 ret[it->first] = it->second._2.reader().get_double();
99                         }
100                         else if (it->first == NAVICORE_HEADING || it->second._1 == NAVICORE_HEADING)
101                         {
102                                 ret[it->first] = it->second._2.reader().get_uint32();
103                         }
104 #if 0 // no supported
105                         else if (it->first == NAVICORE_TIMESTAMP || it->second._1 == NAVICORE_TIMESTAMP)
106                         {
107                                 ret[it->first] = it->second._2.reader().get_uint32();
108                         }
109                         else if (it->first == NAVICORE_SPEED || it->second._1 == NAVICORE_SPEED)
110                         {
111                                 ret[it->first] = it->second._2.reader().get_int32();
112                         }
113 #endif
114                         else if (it->first == NAVICORE_SIMULATION_MODE || it->second._1 == NAVICORE_SIMULATION_MODE)
115                         {
116                                 ret[it->first] = it->second._2.reader().get_bool();
117                         }
118                 }
119         }
120         catch(const std::exception& e)
121         {
122                 fprintf(stderr, "Error:%s\n", e.what());
123         }
124
125         return ret;
126 }
127
128 /**
129  *  @brief  Call GeniviAPI GetPosition to get information
130  *  @return Route handle acquired from Genivi
131  */
132 std::vector< uint32_t > GeniviRequest::NavicoreGetAllRoutes( void )
133 {
134         if( !CheckSession() )
135         {
136                 std::vector< uint32_t > no_route;
137                 return no_route;
138         }
139
140         std::vector< uint32_t > allRoutes;
141         try
142         {
143                 allRoutes = ((Navicore*)navicore_)->GetAllRoutes();
144         }
145         catch(const std::exception& e)
146         {
147                 fprintf(stderr, "Error:%s\n", e.what());
148         }
149
150         return allRoutes;
151 }
152
153
154 /**
155  *  @brief      Call GeniviAPI GetPosition to get information
156  *  @param[in]  sessionHandle Session handle
157  *  @return     Route handle acquired from Genivi
158  */
159 uint32_t GeniviRequest::NavicoreCreateRoute( const uint32_t& sessionHandle )
160 {
161         if( !CheckSession() )
162         {
163                 return 0;
164         }
165
166         uint32_t routeHandle = 0;
167         try
168         {
169                 routeHandle = ((Navicore*)navicore_)->CreateRoute(sessionHandle);
170         }
171         catch(const std::exception& e)
172         {
173                 fprintf(stderr, "Error:%s\n", e.what());
174         }
175
176         return routeHandle;
177 }
178
179 /**
180  *  @brief      Call GeniviAPI PauseSimulation
181  *  @param[in]  sessionHandle Session handle
182  */
183 void GeniviRequest::NavicorePauseSimulation( const uint32_t& sessionHandle )
184 {
185         if( !CheckSession() )
186         {
187                 return;
188         }
189
190         try
191         {
192                 ((Navicore*)navicore_)->PauseSimulation(sessionHandle);
193         }
194         catch(const std::exception& e)
195         {
196                 fprintf(stderr, "Error:%s\n", e.what());
197         }
198 }
199
200
201 /**
202  *  @brief      Call GeniviAPI SetSimulationMode
203  *  @param[in]  sessionHandle Session handle
204  *  @param[in]  activate Simulation mode enabled / disabled
205  */
206 void GeniviRequest::NavicoreSetSimulationMode( const uint32_t& sessionHandle, const bool& activate )
207 {
208         if( !CheckSession() )
209         {
210                 return;
211         }
212
213         try
214         {
215                 ((Navicore*)navicore_)->SetSimulationMode(sessionHandle, activate);
216         }
217         catch(const std::exception& e)
218         {
219                 fprintf(stderr, "Error:%s\n", e.what());
220         }
221 }
222
223
224 /**
225  *  @brief      Call GeniviAPI SetSimulationMode
226  *  @param[in]  sessionHandle Session handle
227  *  @param[in]  routeHandle Route handle
228  */
229 void GeniviRequest::NavicoreCancelRouteCalculation( const uint32_t& sessionHandle, const uint32_t& routeHandle )
230 {
231         if( !CheckSession() )
232         { 
233                 return;
234         }
235
236         try
237         {
238                 ((Navicore*)navicore_)->CancelRouteCalculation(sessionHandle, routeHandle);
239         }
240         catch(const std::exception& e)
241         {
242                 fprintf(stderr, "Error:%s\n", e.what());
243         }
244 }
245
246 /**
247  *  @brief      Call GeniviAPI SetWaypoints
248  *  @param[in]  sessionHandle Session handle
249  *  @param[in]  routeHandle Route handle
250  *  @param[in]  startFromCurrentPosition Whether or not to draw a route from the position of the vehicle
251  *  @param[in]  waypointsList Destination coordinates
252  */
253 void GeniviRequest::NavicoreSetWaypoints( const uint32_t& sessionHandle, const uint32_t& routeHandle,
254                     const bool& startFromCurrentPosition, const std::vector<Waypoint>& waypointsList )
255 {
256         if( !CheckSession() )
257         {
258                 return;
259         }
260
261         std::vector<Waypoint>::const_iterator it;
262         std::vector< std::map< int32_t, ::DBus::Struct< uint8_t, ::DBus::Variant > > > wpl;
263
264         fprintf(stdout, "session: %d, route: %d, startFromCurrentPosition: %d\n",
265             sessionHandle, routeHandle, startFromCurrentPosition);
266
267         for (it = waypointsList.begin(); it != waypointsList.end(); it++)
268         {
269                 std::map< int32_t, ::DBus::Struct< uint8_t, ::DBus::Variant > > Point;
270                 ::DBus::Struct< uint8_t, ::DBus::Variant > VarLat, VarLon;
271
272                 VarLat._1 = NAVICORE_LATITUDE;
273                 VarLat._2.writer().append_double(std::get<0>(*it));
274                 fprintf(stdout, "VarLat._1 : %x, VarLat._2 : %lf\n", VarLat._1, VarLat._2.reader().get_double());
275
276                 VarLon._1 = NAVICORE_LONGITUDE;
277                 VarLon._2.writer().append_double(std::get<1>(*it));
278                 fprintf(stdout, "VarLon._1 : %x, VarLon._2 : %lf\n", VarLon._1, VarLon._2.reader().get_double());
279
280                 Point[NAVICORE_LATITUDE] = VarLat;
281                 Point[NAVICORE_LONGITUDE] = VarLon;
282
283                 wpl.push_back(Point);
284         }
285
286         try
287         {
288                 ((Navicore*)navicore_)->SetWaypoints(sessionHandle, routeHandle, startFromCurrentPosition, wpl);
289         }
290         catch(const std::exception& e)
291         {
292                 fprintf(stderr, "Error:%s\n", e.what());
293         }
294 }
295
296 /**
297  *  @brief      Call GeniviAPI CalculateRoute
298  *  @param[in]  sessionHandle Session handle
299  *  @param[in]  routeHandle Route handle
300  */
301 void GeniviRequest::NavicoreCalculateRoute( const uint32_t& sessionHandle, const uint32_t& routeHandle )
302 {
303         if( !CheckSession() )
304         {
305                 return;
306         }
307
308         try
309         {
310                 ((Navicore*)navicore_)->CalculateRoute(sessionHandle, routeHandle);
311         }
312         catch(const std::exception& e)
313         {
314                 fprintf(stderr, "Error:%s\n", e.what());
315         }
316 }
317
318
319 /**
320  *  @brief  Call GeniviAPI CalculateRoute
321  *  @return Map information on key and value of information acquired from Genivi
322  */
323 std::map<uint32_t, std::string> GeniviRequest::NavicoreGetAllSessions()
324 {
325         std::map<uint32_t, std::string> ret;
326
327         if( !CheckSession() )
328         {
329                 return ret;
330         }
331
332         std::vector< ::DBus::Struct< uint32_t, std::string > > ncAllSessions;
333         std::vector< ::DBus::Struct< uint32_t, std::string > >::iterator it;
334
335         try
336         {
337                 ncAllSessions = ((Navicore*)navicore_)->GetAllSessions();
338                 for (it = ncAllSessions.begin(); it != ncAllSessions.end(); it++)
339                 {
340                         ret[it->_1] = it->_2;
341                 }
342         }
343         catch(const std::exception& e)
344         {
345                 fprintf(stderr, "Error:%s\n", e.what());
346         }
347
348         return ret;
349 }
350