Format
[apps/agl-service-can-low-level.git] / low-can-binding / utils / openxc-utils.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  * Author "Loic Collignon" <loic.collignon@iot.bzh>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include "openxc-utils.hpp"
20
21 #include "../binding/application.hpp"
22
23 ///
24 /// @brief Build a specific VehicleMessage containing a DiagnosticResponse.
25 ///
26 /// @param[in] request - Original request use to retrieve decoder and callback
27 /// @param[in] response - Response to the request that will be decoded if decoder set
28 ///  and put into the DiagnosticResponse of the VehicleMessage.
29 /// @param[in] parsed_value - raw parsed value of the payload from CAN message
30 ///
31 /// @return a vehicle message including simple message that will be convert into
32 /// a JSON object before being pushed to the subscribers
33 ///
34 const openxc_VehicleMessage build_VehicleMessage(active_diagnostic_request_t* request, const DiagnosticResponse& response, float parsed_value)
35 {
36         openxc_VehicleMessage message;
37         application_t& app = application_t::instance();
38
39         message.has_type = true;
40         message.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_DIAGNOSTIC;
41         message.has_diagnostic_response = true;
42         message.diagnostic_response.has_bus = true;
43         message.diagnostic_response.bus = app.get_can_bus_manager().get_can_device_index(
44                                                                                                                                 app.get_diagnostic_manager().get_bus_name());
45         message.diagnostic_response.has_message_id = true;
46
47         if(request->get_id() != OBD2_FUNCTIONAL_BROADCAST_ID)
48         {
49                 message.diagnostic_response.message_id = response.arbitration_id
50                         - DIAGNOSTIC_RESPONSE_ARBITRATION_ID_OFFSET;
51         }
52         else
53         {
54                 // must preserve responding arb ID for responses to functional broadcast
55                 // requests, as they are the actual module address and not just arb ID +
56                 // 8.
57                 message.diagnostic_response.message_id = response.arbitration_id;
58         }
59
60         message.diagnostic_response.has_mode = true;
61         message.diagnostic_response.mode = response.mode;
62         message.diagnostic_response.has_pid = response.has_pid;
63         if(message.diagnostic_response.has_pid)
64                 message.diagnostic_response.pid = response.pid;
65         message.diagnostic_response.has_success = true;
66         message.diagnostic_response.success = response.success;
67         message.diagnostic_response.has_negative_response_code = !response.success;
68         message.diagnostic_response.negative_response_code =
69                         response.negative_response_code;
70
71         if(response.payload_length > 0)
72         {
73                 if(request->get_decoder() != nullptr)
74                 {
75                         message.diagnostic_response.has_value = true;
76                         message.diagnostic_response.value = parsed_value;
77                 }
78                 else
79                 {
80                         message.diagnostic_response.has_payload = true;
81                         ::memcpy(message.diagnostic_response.payload.bytes, response.payload,
82                                         response.payload_length);
83                         message.diagnostic_response.payload.size = response.payload_length;
84                 }
85         }
86
87         return message;
88 }
89 ///
90 /// @brief Build a specific VehicleMessage containing a SimpleMessage with associated timestamp
91 ///
92 /// @param[in] message - simple message to include into openxc_VehicleMessage
93 /// @param[in] timestamp - timestamp from ioctl when reading the socket
94 ///
95 /// @return a vehicle message including simple message that will be convert into
96 /// a JSON object before being pushed to the subscribers
97 ///
98 const openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message, uint64_t timestamp)
99 {
100         openxc_VehicleMessage v;
101
102         v.has_type = true,
103         v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
104         v.has_simple_message = true;
105         v.simple_message =  message;
106         v.has_timestamp = true;
107         v.timestamp = timestamp;
108
109         return v;
110 }
111
112 ///
113 /// @brief Build a specific VehicleMessage containing a SimpleMessage.
114 ///
115 /// @param[in] message - simple message to include into openxc_VehicleMessage
116 ///
117 /// @return a vehicle message including simple message that will be convert into
118 /// a JSON object before being pushed to the subscribers
119 ///
120 const openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message)
121 {
122         openxc_VehicleMessage v;
123
124         v.has_type = true,
125         v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
126         v.has_simple_message = true;
127         v.simple_message =  message;
128         v.has_timestamp = true;
129         v.timestamp = system_time_us();
130
131         return v;
132 }
133
134 ///
135 /// @brief Build an empty VehicleMessage that isn't usable by at least the struct
136 ///  is initialized for the most part and can be use to check a false return value.
137 ///
138 /// @return A VehicleMessage with all boolean value to false.
139 ///
140 openxc_VehicleMessage build_VehicleMessage()
141 {
142         openxc_VehicleMessage v;
143
144         ::memset(&v, 0, sizeof(openxc_VehicleMessage));
145         return v;
146 }
147
148 bool is_valid(const openxc_VehicleMessage& v)
149 {
150         if (v.has_type == false &&
151                 v.has_can_message == false &&
152                 v.has_simple_message == false &&
153                 v.has_diagnostic_response == false &&
154                 v.has_control_command == false &&
155                 v.has_command_response == false &&
156                 v.has_timestamp == false)
157                         return false;
158         return true;
159 }
160
161 ///
162 /// @brief Build an openxc_SimpleMessage associating a name to an openxc_DynamicField
163 ///
164 /// @param[in] name - const string reference name to assign to the created SimpleMessage
165 ///  this will set has_name member to true and assign name to the name member. Maximum size for name is
166 ///  set to 100 char.
167 /// @param[in] value - const reference with DynamicField to assign to SimpleMessage
168 ///  value.
169 ///
170 /// @return an openxc_SimpleMessage struct initialized with name and value provided.
171 ///
172 const openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value)
173 {
174         openxc_SimpleMessage s;
175
176         s.has_name = true;
177         ::strncpy(s.name, name.c_str(), 100);
178         s.has_value = true;
179         s.value = value;
180
181         return s;
182 }
183
184 ///
185 /// @brief Build an openxc_DynamicField with a string value
186 ///
187 /// @param[in] value - const string reference value to assign to builded
188 ///  openxc_DynamicField.
189 ///
190 /// @return openxc_DynamicField initialized with a string value.
191 ///
192 const openxc_DynamicField build_DynamicField(const char* value)
193 {
194         openxc_DynamicField d;
195         d.has_type = true;
196         d.type = openxc_DynamicField_Type_STRING;
197
198         d.has_string_value = true;
199         d.has_numeric_value = false;
200         d.has_boolean_value = false;
201         ::strncpy(d.string_value, value, 100);
202
203         return d;
204 }
205
206 ///
207 /// @brief Build an openxc_DynamicField with a string value
208 ///
209 /// @param[in] value - const string reference value to assign to builded
210 ///  openxc_DynamicField.
211 ///
212 /// @return openxc_DynamicField initialized with a string value.
213 ///
214 const openxc_DynamicField build_DynamicField(const std::string& value)
215 {
216         openxc_DynamicField d;
217         d.has_type = true;
218         d.type = openxc_DynamicField_Type_STRING;
219
220         d.has_string_value = true;
221         d.has_numeric_value = false;
222         d.has_boolean_value = false;
223         ::strncpy(d.string_value, value.c_str(), 100);
224
225         return d;
226 }
227
228 ///
229 /// @fn openxc_DynamicField build_DynamicField(double value);
230 ///
231 /// @brief Build an openxc_DynamicField with a double value
232 ///
233 /// @param[in] value - double value to assign to builded openxc_DynamicField.
234 ///
235 /// @return openxc_DynamicField initialized with a double value.
236 ///
237 const openxc_DynamicField build_DynamicField(double value)
238 {
239         openxc_DynamicField d;
240         d.has_type = true;
241         d.type = openxc_DynamicField_Type_NUM;
242
243         d.has_string_value = false;
244         d.has_numeric_value = true;
245         d.has_boolean_value = false;
246         d.numeric_value = value;
247
248         return d;
249 }
250
251 ///
252 /// @brief Build an openxc_DynamicField with a boolean value
253 ///
254 /// @param[in] value - boolean value to assign to builded openxc_DynamicField.
255 ///
256 /// @return openxc_DynamicField initialized with a boolean value.
257 ///
258 const openxc_DynamicField build_DynamicField(bool value)
259 {
260         openxc_DynamicField d;
261         d.has_type = true;
262         d.type = openxc_DynamicField_Type_BOOL;
263
264         d.has_string_value = false;
265         d.has_numeric_value = false;
266         d.has_boolean_value = true;
267         d.boolean_value = value;
268
269         return d;
270 }
271
272 int get_bool_from_DynamicField(const openxc_VehicleMessage& v_msg, bool* ret)
273 {
274         if(v_msg.has_simple_message && v_msg.simple_message.has_value && v_msg.simple_message.value.has_boolean_value)
275         {
276                 *ret = v_msg.simple_message.value.boolean_value;
277                 return 0;
278         }
279
280         return -1;
281 }
282
283 double get_numerical_from_DynamicField(const openxc_VehicleMessage& v_msg)
284 {
285         return (v_msg.has_simple_message && v_msg.simple_message.has_value && v_msg.simple_message.value.has_numeric_value) ?
286                 v_msg.simple_message.value.numeric_value : -1.0;
287 }
288
289 const std::string get_string_from_DynamicField(const openxc_VehicleMessage& v_msg)
290 {
291 return (v_msg.has_simple_message && v_msg.simple_message.has_value && v_msg.simple_message.value.has_string_value) ?
292                 v_msg.simple_message.value.string_value : "";
293 }
294
295 ///
296 /// @brief Extract the simple message value from an openxc_VehicleMessage
297 ///  and return it. If there isn't SimpleMessage in the VehicleMessage then
298 ///  returned value will be a SimpleMessage with all field set at false.
299 ///  DynamicField from SimpleMessage will be boolean DynamicField set to false too.
300 ///
301 /// @param[in] v_msg - const reference to openxc_VehicleMessage
302 ///
303 /// @return A simpleMessage from the provided VehicleMessage.
304 ///
305 const openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg)
306 {
307         if (v_msg.has_simple_message)
308                 return v_msg.simple_message;
309
310         openxc_SimpleMessage s_msg = { false, "", false, build_DynamicField(false), false, build_DynamicField(false)};
311         return s_msg;
312 }
313
314 ///
315 /// @brief Make a JSON object from a DynamicField
316 ///
317 /// @param[in] field - openxc_DynamicField struct to convert into
318 ///  a json object.
319 /// @param[out] value - pointer to the object to set up.
320 ///
321 void jsonify_DynamicField(const openxc_DynamicField& field, json_object* value)
322 {
323         if(field.has_numeric_value)
324                 json_object_object_add(value, "value", json_object_new_double(field.numeric_value));
325         else if(field.has_boolean_value)
326                 json_object_object_add(value, "value", json_object_new_boolean(field.boolean_value));
327         else if(field.has_string_value)
328                 json_object_object_add(value, "value", json_object_new_string(field.string_value));
329 }
330
331 ///
332 /// @brief Make a JSON object from a SimpleMessage
333 ///
334 /// @param[in] s_msg - const reference to an openxc_SimpleMessage
335 /// struct to convert into a json object.
336 /// @param[out] json - pointer with the DynamicField converted into json object
337 ///
338 /// @return True if SimpleMessage has been transformed into json object
339 ///  and false if not. In such case, a json object is returned { "error": "error msg"}
340 ///
341 bool jsonify_simple(const openxc_SimpleMessage& s_msg, json_object* json)
342 {
343         if(s_msg.has_name)
344         {
345                 json_object_object_add(json, "name", json_object_new_string(s_msg.name));
346                 jsonify_DynamicField(s_msg.value, json);
347                 return true;
348         }
349         json_object_object_add(json, "error", json_object_new_string("openxc_SimpleMessage doesn't have name'"));
350         return false;
351 }