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