Add gitlab issue/merge request templates
[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 #include "converter.hpp"
21 #include "../binding/application.hpp"
22
23
24 ///
25 /// @brief Build a specific VehicleMessage containing a DiagnosticResponse.
26 ///
27 /// @param[in] request - Original request use to retrieve decoder and callback
28 /// @param[in] response - Response to the request that will be decoded if decoder set
29 ///  and put into the DiagnosticResponse of the VehicleMessage.
30 /// @param[in] parsed_value - raw parsed value of the payload from CAN message
31 ///
32 /// @return a vehicle message including simple message that will be convert into
33 /// a JSON object before being pushed to the subscribers
34 ///
35 const openxc_VehicleMessage build_VehicleMessage(active_diagnostic_request_t* request, const DiagnosticResponse& response, float parsed_value)
36 {
37         openxc_VehicleMessage message;
38         ::memset(&message, 0, sizeof(message));
39         application_t& app = application_t::instance();
40
41         message.has_type = true;
42         message.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_DIAGNOSTIC;
43         message.has_diagnostic_response = true;
44         message.diagnostic_response.has_bus = true;
45         message.diagnostic_response.bus = app.get_can_bus_manager().get_can_device_index(
46                                                                                                                                 app.get_diagnostic_manager().get_bus_name());
47         message.diagnostic_response.has_message_id = true;
48
49         if(request->get_id() != OBD2_FUNCTIONAL_BROADCAST_ID)
50         {
51                 message.diagnostic_response.message_id = response.arbitration_id
52                         - DIAGNOSTIC_RESPONSE_ARBITRATION_ID_OFFSET;
53         }
54         else
55         {
56                 // must preserve responding arb ID for responses to functional broadcast
57                 // requests, as they are the actual module address and not just arb ID +
58                 // 8.
59                 message.diagnostic_response.message_id = response.arbitration_id;
60         }
61
62         message.diagnostic_response.has_mode = true;
63         message.diagnostic_response.mode = response.mode;
64         message.diagnostic_response.has_pid = response.has_pid;
65         if(message.diagnostic_response.has_pid)
66                 message.diagnostic_response.pid = response.pid;
67         message.diagnostic_response.has_success = true;
68         message.diagnostic_response.success = response.success;
69         message.diagnostic_response.has_negative_response_code = !response.success;
70         message.diagnostic_response.negative_response_code =
71                         response.negative_response_code;
72
73         if(response.payload_length > 0)
74         {
75                 if(request->get_decoder() != nullptr)
76                 {
77                         message.diagnostic_response.has_value = true;
78                         message.diagnostic_response.value = parsed_value;
79                 }
80                 else
81                 {
82                         message.diagnostic_response.has_payload = true;
83                         ::memcpy(message.diagnostic_response.payload.bytes, response.payload,
84                                         response.payload_length);
85                         message.diagnostic_response.payload.size = response.payload_length;
86                 }
87         }
88
89         return message;
90 }
91 ///
92 /// @brief Build a specific VehicleMessage containing a SimpleMessage with associated timestamp
93 ///
94 /// @param[in] message - simple message to include into openxc_VehicleMessage
95 /// @param[in] timestamp - timestamp from ioctl when reading the socket
96 ///
97 /// @return a vehicle message including simple message that will be convert into
98 /// a JSON object before being pushed to the subscribers
99 ///
100 const openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message, uint64_t timestamp)
101 {
102         openxc_VehicleMessage v;
103         ::memset(&v, 0, sizeof(v));
104
105         v.has_type = true,
106         v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
107         v.has_simple_message = true;
108         v.simple_message =  message;
109         v.has_timestamp = true;
110         v.timestamp = timestamp;
111
112         return v;
113 }
114
115 ///
116 /// @brief Build a specific VehicleMessage containing a SimpleMessage.
117 ///
118 /// @param[in] message - simple message to include into openxc_VehicleMessage
119 ///
120 /// @return a vehicle message including simple message that will be convert into
121 /// a JSON object before being pushed to the subscribers
122 ///
123 const openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message)
124 {
125         openxc_VehicleMessage v;
126         ::memset(&v, 0, sizeof(v));
127
128         v.has_type = true,
129         v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
130         v.has_simple_message = true;
131         v.simple_message =  message;
132         v.has_timestamp = true;
133         v.timestamp = system_time_us();
134
135         return v;
136 }
137
138 ///
139 /// @brief Build an empty VehicleMessage that isn't usable by at least the struct
140 ///  is initialized for the most part and can be use to check a false return value.
141 ///
142 /// @return A VehicleMessage with all boolean value to false.
143 ///
144 openxc_VehicleMessage build_VehicleMessage()
145 {
146         openxc_VehicleMessage v;
147
148         ::memset(&v, 0, sizeof(v));
149         return v;
150 }
151
152 bool is_valid(const openxc_VehicleMessage& v)
153 {
154         if (v.has_type == false &&
155                 v.has_can_message == false &&
156                 v.has_simple_message == false &&
157                 v.has_diagnostic_response == false &&
158                 v.has_control_command == false &&
159                 v.has_command_response == false &&
160                 v.has_timestamp == false)
161                         return false;
162         return true;
163 }
164
165 ///
166 /// @brief Build an openxc_SimpleMessage associating a name to an openxc_DynamicField
167 ///
168 /// @param[in] name - const string reference name to assign to the created SimpleMessage
169 ///  this will set has_name member to true and assign name to the name member. Maximum size for name is
170 ///  set to 100 char.
171 /// @param[in] value - const reference with DynamicField to assign to SimpleMessage
172 ///  value.
173 ///
174 /// @return an openxc_SimpleMessage struct initialized with name and value provided.
175 ///
176 const openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value)
177 {
178         openxc_SimpleMessage s;
179
180         s.has_name = true;
181         ::strncpy(s.name, name.c_str(), 100);
182         s.has_value = true;
183         s.value = value;
184
185         return s;
186 }
187
188 /// @brief Build an openxc_DynamicField with a json object
189 ///
190 /// @param[in] value - const json_object pointer to assign to convert in an
191 ///  openxc_DynamicField.
192 ///
193 /// @return openxc_DynamicField initialized with a json object.
194 ///
195 const openxc_DynamicField build_DynamicField(json_object* value)
196 {
197         switch(json_object_get_type(value))
198         {
199                 case json_type_string:
200                         return build_DynamicField(json_object_get_string(value));
201                 case json_type_double:
202                         return build_DynamicField(json_object_get_double(value));
203                 case json_type_int:
204                         return build_DynamicField(json_object_get_double(value));
205                 case json_type_boolean:
206                         return build_DynamicField((bool)json_object_get_boolean(value));
207                 default:
208                         openxc_DynamicField d;
209                         ::memset(&d, 0, sizeof(openxc_DynamicField));
210                         return d;
211         }
212 }
213
214 const openxc_DynamicField build_DynamicField(std::vector<uint8_t> &array)
215 {
216         openxc_DynamicField d;
217         d.has_type = true;
218         d.type = openxc_DynamicField_Type_BYTES;
219
220         d.has_string_value = false;
221         d.has_numeric_value = false;
222         d.has_boolean_value = false;
223         d.has_bytes_value = true;
224
225
226         size_t size = array.size();
227
228         if(size > 2040)
229         {
230                 AFB_ERROR("Error to generate array dynamic field, too large data");
231                 return d;
232         }
233         else
234         {
235                  d.length_array = (uint32_t) size;
236         }
237
238         for(int i=0;i<size;i++)
239                 d.bytes_value[i] = array[i];
240
241         return d;
242 }
243
244
245 ///
246 /// @brief Build an openxc_DynamicField with a string value
247 ///
248 /// @param[in] value - const string reference value to assign to builded
249 ///  openxc_DynamicField.
250 ///
251 /// @return openxc_DynamicField initialized with a string value.
252 ///
253 const openxc_DynamicField build_DynamicField(const char* value)
254 {
255         openxc_DynamicField d;
256         d.has_type = true;
257         d.type = openxc_DynamicField_Type_STRING;
258
259         d.has_string_value = true;
260         d.has_numeric_value = false;
261         d.has_boolean_value = false;
262         d.has_bytes_value = false;
263         d.has_json_value = false;
264         ::strncpy(d.string_value, value, 100);
265
266         return d;
267 }
268
269 ///
270 /// @brief Build an openxc_DynamicField with a string value
271 ///
272 /// @param[in] value - const string reference value to assign to builded
273 ///  openxc_DynamicField.
274 ///
275 /// @return openxc_DynamicField initialized with a string value.
276 ///
277 const openxc_DynamicField build_DynamicField(const std::string& value)
278 {
279         openxc_DynamicField d;
280         d.has_type = true;
281         d.type = openxc_DynamicField_Type_STRING;
282
283         d.has_string_value = true;
284         d.has_numeric_value = false;
285         d.has_boolean_value = false;
286         d.has_bytes_value = false;
287         d.has_json_value = false;
288         ::strncpy(d.string_value, value.c_str(), 100);
289
290         return d;
291 }
292
293 ///
294 /// @fn openxc_DynamicField build_DynamicField(double value);
295 ///
296 /// @brief Build an openxc_DynamicField with a double value
297 ///
298 /// @param[in] value - double value to assign to builded openxc_DynamicField.
299 ///
300 /// @return openxc_DynamicField initialized with a double value.
301 ///
302 const openxc_DynamicField build_DynamicField(double value)
303 {
304         openxc_DynamicField d;
305         d.has_type = true;
306         d.type = openxc_DynamicField_Type_NUM;
307
308         d.has_string_value = false;
309         d.has_numeric_value = true;
310         d.has_boolean_value = false;
311         d.has_bytes_value = false;
312         d.has_json_value = false;
313         d.numeric_value = value;
314
315         return d;
316 }
317
318 ///
319 /// @brief Build an openxc_DynamicField with a boolean value
320 ///
321 /// @param[in] value - boolean value to assign to builded openxc_DynamicField.
322 ///
323 /// @return openxc_DynamicField initialized with a boolean value.
324 ///
325 const openxc_DynamicField build_DynamicField(bool value)
326 {
327         openxc_DynamicField d;
328         d.has_type = true;
329         d.type = openxc_DynamicField_Type_BOOL;
330
331         d.has_string_value = false;
332         d.has_numeric_value = false;
333         d.has_boolean_value = true;
334         d.has_bytes_value = false;
335         d.has_json_value = false;
336         d.boolean_value = value;
337
338         return d;
339 }
340
341 ///
342 /// @brief Build an openxc_DynamicField with a json value
343 ///
344 /// @param[in] value - json value to assign to builded openxc_DynamicField.
345 ///
346 /// @return openxc_DynamicField initialized with a json value.
347 ///
348 const openxc_DynamicField build_DynamicField_json(json_object *value)
349 {
350         openxc_DynamicField d;
351         d.has_type = true;
352         d.type = openxc_DynamicField_Type_JSON;
353
354         d.has_string_value = false;
355         d.has_numeric_value = false;
356         d.has_boolean_value = false;
357         d.has_bytes_value = false;
358         d.has_json_value = true;
359         d.json_value = value;
360
361         return d;
362 }
363
364 int get_bool_from_DynamicField(const openxc_VehicleMessage& v_msg, bool* ret)
365 {
366         if(v_msg.has_simple_message && v_msg.simple_message.has_value && v_msg.simple_message.value.has_boolean_value)
367         {
368                 *ret = v_msg.simple_message.value.boolean_value;
369                 return 0;
370         }
371
372         return -1;
373 }
374
375 double get_numerical_from_DynamicField(const openxc_VehicleMessage& v_msg)
376 {
377         return (v_msg.has_simple_message && v_msg.simple_message.has_value && v_msg.simple_message.value.has_numeric_value) ?
378                 v_msg.simple_message.value.numeric_value : -1.0;
379 }
380
381 const std::string get_string_from_DynamicField(const openxc_VehicleMessage& v_msg)
382 {
383 return (v_msg.has_simple_message && v_msg.simple_message.has_value && v_msg.simple_message.value.has_string_value) ?
384                 v_msg.simple_message.value.string_value : "";
385 }
386
387 ///
388 /// @brief Extract the simple message value from an openxc_VehicleMessage
389 ///  and return it. If there isn't SimpleMessage in the VehicleMessage then
390 ///  returned value will be a SimpleMessage with all field set at false.
391 ///  DynamicField from SimpleMessage will be boolean DynamicField set to false too.
392 ///
393 /// @param[in] v_msg - const reference to openxc_VehicleMessage
394 ///
395 /// @return A simpleMessage from the provided VehicleMessage.
396 ///
397 const openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg)
398 {
399         if (v_msg.has_simple_message)
400                 return v_msg.simple_message;
401
402         openxc_SimpleMessage s_msg = { false, "", false, build_DynamicField(false), false, build_DynamicField(false)};
403         return s_msg;
404 }
405
406 ///
407 /// @brief Make a JSON object from a DynamicField
408 ///
409 /// @param[in] field - openxc_DynamicField struct to convert into
410 ///  a json object.
411 /// @param[out] value - pointer to the object to set up.
412 ///
413 void jsonify_DynamicField(const openxc_DynamicField& field, json_object* value)
414 {
415         if(field.has_numeric_value)
416                 json_object_object_add(value, "value", json_object_new_double(field.numeric_value));
417         else if(field.has_boolean_value)
418                 json_object_object_add(value, "value", json_object_new_boolean(field.boolean_value));
419         else if(field.has_string_value)
420                 json_object_object_add(value, "value", json_object_new_string(field.string_value));
421         else if(field.has_bytes_value)
422                 json_object_object_add(value, "value", json_object_new_string(converter_t::to_hex(field.bytes_value, field.length_array).c_str()));
423         else if(field.has_json_value)
424                 json_object_object_add(value, "signals", json_object_get(field.json_value));
425 }
426
427 ///
428 /// @brief Make a JSON object from a SimpleMessage
429 ///
430 /// @param[in] s_msg - const reference to an openxc_SimpleMessage
431 /// struct to convert into a json object.
432 /// @param[out] json - pointer with the DynamicField converted into json object
433 ///
434 /// @return True if SimpleMessage has been transformed into json object
435 ///  and false if not. In such case, a json object is returned { "error": "error msg"}
436 ///
437 bool jsonify_simple(const openxc_SimpleMessage& s_msg, json_object* json)
438 {
439         if(s_msg.has_name)
440         {
441                 json_object_object_add(json, "name", json_object_new_string(s_msg.name));
442                 jsonify_DynamicField(s_msg.value, json);
443                 return true;
444         }
445         json_object_object_add(json, "error", json_object_new_string("openxc_SimpleMessage doesn't have name'"));
446         return false;
447 }
448
449 ///
450 /// @brief Make a JSON object from a VehicleMessage
451 ///
452 /// @param[in] v_msg - const reference to an openxc_VehicleMessage
453 /// struct to convert into a json object.
454 /// @param[in] sig - signal reference to the subscription of openxc_VehicleMessage,
455 /// to get more informations about it
456 /// @param[out] json - pointer with the DynamicField converted into json object
457 ///
458 /// @return True if VehicleMessage has been transformed into json object
459 ///  and false if not. In such case, a json object is returned { "error": "error msg"}
460 ///
461 bool jsonify_vehicle(const openxc_VehicleMessage& v_msg, std::shared_ptr<signal_t> sig, json_object* json)
462 {
463         if(jsonify_simple(get_simple_message(v_msg), json))
464         {
465                 if(sig != nullptr && sig->get_unit() != "")
466                         json_object_object_add(json, "unit", json_object_new_string(sig->get_unit().c_str()));
467
468                 if(v_msg.has_timestamp)
469                         json_object_object_add(json, "timestamp", json_object_new_int64(v_msg.timestamp));
470
471                 return true;
472         }
473         json_object_object_add(json, "error", json_object_new_string("openxc_SimpleMessage doesn't have name'"));
474         return false;
475 }