172a22a4ae9e3bf5794f8e5cbfa6ca0069beea05
[apps/agl-service-navigation.git] / src / binder_reply.cpp
1 // Copyright 2017 AW SOFTWARE CO.,LTD
2 // Copyright 2017 AISIN AW CO.,LTD
3
4 #include "binder_reply.h"
5 #include "genivi/genivi-navicore-constants.h"
6
7 /**
8  *  @brief      GeniviAPI GetPosition call
9  *  @param[in]  posList Map information on key and value of information acquired from Genivi
10  *  @return     Response information
11  */
12 APIResponse BinderReply::ReplyNavicoreGetPosition( std::map<int32_t, double>& posList )
13 {
14         APIResponse response = {0};
15
16         // Json information to return as a response
17         struct json_object* response_json = json_object_new_array();
18         std::map<int32_t, double>::iterator it;
19     
20         // If the argument map is empty return
21         if(posList.empty())
22         {
23                 response.isSuccess  = false;
24                 response.errMessage = "posList is empty";
25                 response.json_data  = response_json;
26                 return response;
27         }
28
29         // Make the passed Genivi response json format
30         for (it = posList.begin(); it != posList.end(); it++)
31         {
32                 struct json_object* obj = json_object_new_object();
33
34                 switch(it->first)
35                 {
36                 case NAVICORE_LATITUDE:
37                         json_object_object_add(obj, "key", json_object_new_int(NAVICORE_LATITUDE));
38                         json_object_object_add(obj, "value", json_object_new_double(it->second) );
39                         json_object_array_add(response_json, obj);
40                         break;
41
42                 case NAVICORE_LONGITUDE:
43                         json_object_object_add(obj, "key", json_object_new_int(NAVICORE_LONGITUDE));
44                         json_object_object_add(obj, "value", json_object_new_double(it->second));
45                         json_object_array_add(response_json, obj);
46                         break;
47
48                 case NAVICORE_HEADING:
49                         json_object_object_add(obj, "key", json_object_new_int(NAVICORE_HEADING));
50                         json_object_object_add(obj, "value", json_object_new_boolean (it->second));
51                         json_object_array_add(response_json, obj);
52                         break;
53 #if 0
54                 // no support
55                 case NAVICORE_TIMESTAMP:
56                         json_object_object_add(obj, "key", json_object_new_int(NAVICORE_TIMESTAMP));
57                         json_object_object_add(obj, "value", json_object_new_int(it->second));
58                         json_object_array_add(response_json, obj);
59                         break;
60
61                 // no support
62                 case NAVICORE_SPEED:
63                         json_object_object_add(obj, "key", json_object_new_int(NAVICORE_SPEED));
64                         json_object_object_add(obj, "value", json_object_new_int(it->second));
65                         json_object_array_add(response_json, obj);
66                         break;
67 #endif
68
69                 case NAVICORE_SIMULATION_MODE:
70                         json_object_object_add(obj, "key", json_object_new_int(NAVICORE_SIMULATION_MODE));
71                         json_object_object_add(obj, "value", json_object_new_boolean (it->second));
72                         json_object_array_add(response_json, obj);
73                         break;
74
75                 default:
76                         fprintf(stderr, "Unknown key.");
77                         json_object_put(obj);
78                         break;
79                 }
80         }
81
82         response.json_data = response_json;
83         response.isSuccess = true;
84         return response;
85 }
86
87 /**
88  *  @brief      GeniviAPI GetAllRoutes call
89  *  @param[in]  allRoutes Route handle information
90  *  @return     Response information
91  */
92 APIResponse BinderReply::ReplyNavicoreGetAllRoutes( std::vector< uint32_t > &allRoutes )
93 {
94         APIResponse response = {0};
95
96         // Json information to return as a response
97         struct json_object* response_json = json_object_new_array();
98
99         if (0 < allRoutes.size())
100         {
101                 std::vector< uint32_t >::iterator it;
102
103                 for (it = allRoutes.begin(); it != allRoutes.end(); it++)
104                 {
105                         struct json_object* obj = json_object_new_object();
106                         json_object_object_add(obj, "route", json_object_new_int(*it));
107                         json_object_array_add(response_json, obj);
108                 }
109         }
110
111         response.json_data = response_json;
112         response.isSuccess = true;
113         return response;
114 }
115
116 /**
117  *  @brief      GeniviAPI CreateRoute call
118  *  @param[in]  route Route handle
119  *  @return     Response information
120  */
121 APIResponse BinderReply::ReplyNavicoreCreateRoute( uint32_t route )
122 {
123         APIResponse response;
124
125         // Json information to return as a response
126         struct json_object* response_json = json_object_new_object();
127         json_object_object_add(response_json, "route", json_object_new_int(route));
128
129         response.json_data = response_json;
130         response.isSuccess = true;
131         return response;
132 }
133
134 /**
135  *  @brief      GeniviAPI GetAllSessions call
136  *  @param[in]  allSessions Map information on key and value of information acquired from Genivi
137  *  @return     Response information
138  */
139 APIResponse BinderReply::ReplyNavicoreGetAllSessions( std::map<uint32_t, std::string> &allSessions )
140 {
141         APIResponse response = {0};
142
143         // Json information to return as a response
144         struct json_object* response_json = json_object_new_array();
145         std::map<uint32_t, std::string>::iterator it;
146
147         for (it = allSessions.begin(); it != allSessions.end(); it++)
148         {
149                 struct json_object* obj = json_object_new_object();
150
151                 if (NAVICORE_INVALID != it->first)
152                 {
153                         json_object_object_add(obj, "sessionHandle", json_object_new_int(it->first));
154                         json_object_object_add(obj, "client", json_object_new_string(it->second.c_str()));
155                         json_object_array_add(response_json, obj);
156                 }
157                 else
158                 {
159                         fprintf(stderr, "invalid key.");
160                         json_object_put(obj);
161                 }
162         }
163
164         response.json_data = response_json;
165         response.isSuccess = true;
166         return response;
167 }
168