111ac1d3cddfdc3046ad3c61ccf32a4b9e681abc
[apps/low-level-can-service.git] / src / 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 "../configuration.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 openxc_VehicleMessage build_VehicleMessage(active_diagnostic_request_t* request, const DiagnosticResponse& response, float parsed_value)
35 {
36         openxc_VehicleMessage message;
37
38         message.has_type = true;
39         message.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_DIAGNOSTIC;
40         message.has_diagnostic_response = true;
41         message.diagnostic_response.has_bus = true;
42         message.diagnostic_response.bus = configuration_t::instance().get_diagnostic_manager().get_can_bus_dev()->get_address();
43         message.diagnostic_response.has_message_id = true;
44
45         if(request->get_id() != OBD2_FUNCTIONAL_BROADCAST_ID)
46         {
47                 message.diagnostic_response.message_id = response.arbitration_id
48                         - DIAGNOSTIC_RESPONSE_ARBITRATION_ID_OFFSET;
49         }
50         else
51         {
52                 // must preserve responding arb ID for responses to functional broadcast
53                 // requests, as they are the actual module address and not just arb ID +
54                 // 8.
55                 message.diagnostic_response.message_id = response.arbitration_id;
56         }
57
58         message.diagnostic_response.has_mode = true;
59         message.diagnostic_response.mode = response.mode;
60         message.diagnostic_response.has_pid = response.has_pid;
61         if(message.diagnostic_response.has_pid)
62                 message.diagnostic_response.pid = response.pid;
63         message.diagnostic_response.has_success = true;
64         message.diagnostic_response.success = response.success;
65         message.diagnostic_response.has_negative_response_code = !response.success;
66         message.diagnostic_response.negative_response_code =
67                         response.negative_response_code;
68
69         if(response.payload_length > 0)
70         {
71                 if(request->get_decoder() != nullptr)
72                 {
73                         message.diagnostic_response.has_value = true;
74                         message.diagnostic_response.value = parsed_value;
75                 }
76                 else
77                 {
78                         message.diagnostic_response.has_payload = true;
79                         ::memcpy(message.diagnostic_response.payload.bytes, response.payload,
80                                         response.payload_length);
81                         message.diagnostic_response.payload.size = response.payload_length;
82                 }
83         }
84
85         return message;
86 }
87
88 /**
89  * @brief Build a specific VehicleMessage containing a SimpleMessage.
90  *
91  * @param[in] message - simple message to include into openxc_VehicleMessage
92  *
93  * @return a vehicle message including simple message that will be convert into 
94  * a JSON object before being pushed to the subscribers
95  */
96 openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message)
97 {
98         openxc_VehicleMessage v;
99         
100         v.has_type = true,
101         v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
102         v.has_simple_message = true;
103         v.simple_message =  message;
104         v.has_timestamp = true;
105         v.timestamp = system_time_ms();
106
107         return v;
108 }
109
110 /**
111  * @brief Build an empty VehicleMessage that isn't usable by at least the struct
112  *  is initialized for the most part and can be use to check a false return value.
113  *
114  * @return A VehicleMessage with all boolean value to false.
115  */
116 openxc_VehicleMessage build_VehicleMessage()
117 {
118         openxc_VehicleMessage v;
119
120         ::memset(&v, 0, sizeof(openxc_VehicleMessage));
121         return v;
122 }
123
124 bool is_valid(const openxc_VehicleMessage& v)
125 {
126         if (v.has_type == false &&
127                 v.has_can_message == false &&
128                 v.has_simple_message == false &&
129                 v.has_diagnostic_response == false &&
130                 v.has_control_command == false &&
131                 v.has_command_response == false &&
132                 v.has_timestamp == false)
133                         return false;
134         return true;
135 }
136
137 /**
138  * @brief Build an openxc_SimpleMessage associating a name to an openxc_DynamicField
139  *
140  * @param[in] name - const string reference name to assign to the created SimpleMessage
141  *  this will set has_name member to true and assign name to the name member. Maximum size for name is 
142  *  set to 100 char.
143  * @param[in] value - const reference with DynamicField to assign to SimpleMessage
144  *  value.
145  *
146  * @return an openxc_SimpleMessage struct initialized with name and value provided.
147  */
148 openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value)
149 {
150         openxc_SimpleMessage s;
151
152         s.has_name = true;
153         ::strncpy(s.name, name.c_str(), 100);
154         s.has_value = true;
155         s.value = value;
156
157         return s;
158 }
159
160 /**
161  * @brief Build an openxc_DynamicField with a string value
162  *
163  * @param[in] value - const string reference value to assign to builded
164  *  openxc_DynamicField.
165  *
166  * @return openxc_DynamicField initialized with a string value.
167  */
168 openxc_DynamicField build_DynamicField(const std::string& value)
169 {
170         openxc_DynamicField d;
171         d.has_type = true;
172         d.type = openxc_DynamicField_Type_STRING;
173         
174         d.has_string_value = true;
175         d.has_numeric_value = false;
176         d.has_boolean_value = false;
177         ::strncpy(d.string_value, value.c_str(), 100);
178         
179         return d;
180 }
181
182 /**
183  * @fn openxc_DynamicField build_DynamicField(double value);
184  *
185  * @brief Build an openxc_DynamicField with a double value
186  *
187  * @param[in] value - double value to assign to builded openxc_DynamicField.
188  *
189  * @return openxc_DynamicField initialized with a double value.
190  */
191 openxc_DynamicField build_DynamicField(double value)
192 {
193         openxc_DynamicField d;
194         d.has_type = true;
195         d.type = openxc_DynamicField_Type_NUM;
196         
197         d.has_string_value = false;
198         d.has_numeric_value = true;
199         d.has_boolean_value = false;
200         d.numeric_value = value;
201         
202         return d;
203 }
204
205 /**
206  * @brief Build an openxc_DynamicField with a boolean value
207  *
208  * @param[in] value - boolean value to assign to builded openxc_DynamicField.
209  *
210  * @return openxc_DynamicField initialized with a boolean value.
211  */
212 openxc_DynamicField build_DynamicField(bool value)
213 {
214         openxc_DynamicField d;
215         d.has_type = true;
216         d.type = openxc_DynamicField_Type_BOOL;
217         
218         d.has_string_value = false;
219         d.has_numeric_value = false;
220         d.has_boolean_value = true;
221         d.boolean_value = value;
222         
223         return d;
224 }
225
226 /**
227  * @brief Extract the simple message value from an openxc_VehicleMessage
228  *  and return it. If there isn't SimpleMessage in the VehicleMessage then
229  *  returned value will be a SimpleMessage with all field set at false.
230  *  DynamicField from SimpleMessage will be boolean DynamicField set to false too.
231  *
232  * @param[in] v_msg - const reference to openxc_VehicleMessage
233  *
234  * @return A simpleMessage from the provided VehicleMessage.
235  */
236 openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg)
237 {
238         if (v_msg.has_simple_message)
239                 return v_msg.simple_message;
240         
241         openxc_SimpleMessage s_msg = { false, "", false, build_DynamicField(false), false, build_DynamicField(false)};
242         return s_msg;
243 }
244
245 /**
246  * @brief Make a JSON object from a DynamicField
247  *
248  * @param[in] field - openxc_DynamicField struct to convert into
249  *  a json object.
250  * @param[out] value - pointer to the object to set up.
251  */
252 void jsonify_DynamicField(const openxc_DynamicField& field, json_object* value)
253 {
254         if(field.has_numeric_value)
255                 json_object_object_add(value, "value", json_object_new_double(field.numeric_value));
256         else if(field.has_boolean_value)
257                 json_object_object_add(value, "value", json_object_new_boolean(field.boolean_value));
258         else if(field.has_string_value)
259                 json_object_object_add(value, "value", json_object_new_string(field.string_value));
260 }
261
262 /**
263  * @brief Make a JSON object from a SimpleMessage
264  *
265  * @param[in] s_msg - const reference to an openxc_SimpleMessage 
266  * struct to convert into a json object.
267  * @param[out] json - pointer with the DynamicField converted into json object
268  * 
269  * @return True if SimpleMessage has been transformed into json object
270  *  and false if not. In such case, a json object is returned { "error": "error msg"}
271  */
272 bool jsonify_simple(const openxc_SimpleMessage& s_msg, json_object* json)
273 {
274         if(s_msg.has_name)
275         {
276                 json_object_object_add(json, "name", json_object_new_string(s_msg.name));
277                 jsonify_DynamicField(s_msg.value, json);
278                 return true;
279         }
280         json_object_object_add(json, "error", json_object_new_string("openxc_SimpleMessage doesn't have name'"));
281         return false;
282 }