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