All: Make format coherent with the whole project
[apps/agl-service-can-low-level.git] / low-can-binding / can / can-decoder.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "can-decoder.hpp"
19
20 #include "canutil/read.h"
21 #include "../utils/openxc-utils.hpp"
22 #include "message-definition.hpp"
23 #include "../binding/low-can-hat.hpp"
24 #include "../utils/converter.hpp"
25
26 /// @brief Parses the signal's bitfield from the given data and returns the raw
27 /// value.
28 ///
29 /// @param[in] signal - The signal to be parsed from the data.
30 /// @param[in] message - message_t to parse
31 ///
32 /// @return Returns the raw value of the signal parsed as a bitfield from the given byte
33 /// array.
34 ///
35 float decoder_t::parse_signal_bitfield(signal_t& signal, std::shared_ptr<message_t> message)
36 {
37         const std::vector<uint8_t> data = message->get_data_vector();
38         std::vector<uint8_t> data_signal;
39         uint8_t bit_size = (uint8_t) signal.get_bit_size();
40         uint32_t bit_position = signal.get_bit_position();
41
42         int new_start_byte = 0;
43         int new_end_byte = 0;
44         uint8_t new_start_bit = 0;
45         uint8_t new_end_bit = 0;
46
47         converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit);
48
49         for(int i=new_start_byte;i<=new_end_byte;i++)
50                 data_signal.push_back(data[i]);
51
52 //      if(bit_size > 255)
53 //              AFB_ERROR("Error signal %s to long bit size", signal.get_name().c_str());
54
55 //      if(new_start_bit > 255)
56 //              AFB_ERROR("Too long signal offset %d", new_start_bit);
57
58         if(data_signal.size() > 65535)
59                 AFB_ERROR("Too long data signal %s", signal.get_name().c_str());
60
61         return bitfield_parse_float(data_signal.data(), (uint16_t) data_signal.size(),
62                         new_start_bit, bit_size, signal.get_factor(),
63                         signal.get_offset());
64 }
65
66
67 /// @brief Decode and return string bytes (hex) for a CAN signal's.
68 ///
69 /// This is an implementation of the Signal type signature, and can be
70 /// used directly in the signal_t.decoder field.
71 ///
72 /// @param[in] signal  - The details of the signal.
73 /// @param[in] message - The message with data to decode.
74 /// @param[out] send - An output argument that will be set to false if the value should
75 ///     not be sent for any reason.
76 ///
77 /// @return Returns a DynamicField with a string value of bytes (hex)
78 ///
79 openxc_DynamicField decoder_t::decode_bytes(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
80 {
81         int i=0;
82         openxc_DynamicField decoded_value;
83         std::vector<uint8_t> data = message->get_data_vector();
84         uint32_t length = message->get_length();
85         uint32_t bit_position = signal.get_bit_position();
86         uint32_t bit_size = signal.get_bit_size();
87         std::vector<uint8_t> new_data = std::vector<uint8_t>();
88         new_data.reserve(bit_size << 3);
89
90         int new_start_byte = 0;
91         int new_end_byte = 0;
92         uint8_t new_start_bit = 0;
93         uint8_t new_end_bit = 0;
94
95         converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit);
96
97         if(new_end_byte >= length)
98         {
99                 new_end_byte = length-1;
100         }
101
102         if(new_start_byte >= length)
103         {
104                 AFB_ERROR("Error in description of signals");
105                 return decoded_value;
106         }
107
108         uint8_t first = data[new_start_byte];
109         int mask_first = 0;
110         for(i=new_start_bit;i<8;i++)
111         {
112                 mask_first = mask_first | (1 << i);
113         }
114
115         uint8_t mask_first_v = 0;
116         if(mask_first > 255)
117         {
118                 AFB_ERROR("Error mask decode bytes");
119         }
120         else
121         {
122                 mask_first_v = (uint8_t)mask_first;
123         }
124
125         data[new_start_byte]=first&mask_first_v;
126
127         uint8_t last = data[new_end_byte];
128         int mask_last = 0;
129         for(i=0;i<=new_end_bit;i++)
130         {
131                 mask_last = mask_last | (1 << (7-i));
132         }
133
134         uint8_t mask_last_v = 0;
135         if(mask_last > 255)
136         {
137                 AFB_ERROR("Error mask decode bytes");
138         }
139         else
140         {
141                 mask_last_v = (uint8_t)mask_last;
142         }
143
144         data[new_end_byte]=last&mask_last_v;
145
146
147         for(i=new_start_byte;i<=new_end_byte;i++)
148         {
149                 new_data.push_back(data[i]);
150         }
151
152         decoded_value = build_DynamicField(new_data);
153
154         return decoded_value;
155 }
156
157 /// @brief Wraps a raw CAN signal value in a DynamicField without modification.
158 ///
159 /// This is an implementation of the Signal type signature, and can be
160 /// used directly in the signal_t.decoder field.
161 ///
162 /// @param[in] signal - The details of the signal that contains the state mapping.
163 /// @param[in] message - The message with data to decode.
164 /// @param[out] send - An output argument that will be set to false if the value should
165 ///     not be sent for any reason.
166 ///
167 /// @return Returns a DynamicField with the original, unmodified raw CAN signal value as
168 /// its numeric value. The 'send' argument will not be modified as this decoder
169 /// always succeeds.
170 ///
171 openxc_DynamicField decoder_t::decode_noop(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
172 {
173         float value = decoder_t::parse_signal_bitfield(signal, message);
174         AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
175         openxc_DynamicField decoded_value = build_DynamicField(value);
176
177         // Don't send if they is no changes
178         if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
179         {
180                 *send = false;
181         }
182         signal.set_last_value(value);
183
184         return decoded_value;
185 }
186 /// @brief Coerces a numerical value to a boolean.
187 ///
188 /// This is an implementation of the Signal type signature, and can be
189 /// used directly in the signal_t.decoder field.
190 ///
191 /// @param[in] signal  - The details of the signal that contains the state mapping.
192 /// @param[in] message - The message with data to decode.
193 /// @param[out] send - An output argument that will be set to false if the value should
194 ///     not be sent for any reason.
195 ///
196 /// @return Returns a DynamicField with a boolean value of false if the raw signal value
197 /// is 0.0, otherwise true. The 'send' argument will not be modified as this
198 /// decoder always succeeds.
199 ///
200 openxc_DynamicField decoder_t::decode_boolean(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
201 {
202         float value = decoder_t::parse_signal_bitfield(signal, message);
203         AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
204         openxc_DynamicField decoded_value = build_DynamicField(value == 0.0 ? false : true);
205
206         // Don't send if they is no changes
207         if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
208         {
209                 *send = false;
210         }
211         signal.set_last_value(value);
212
213
214         return decoded_value;
215 }
216 /// @brief Update the metadata for a signal and the newly received value.
217 ///
218 /// This is an implementation of the Signal type signature, and can be
219 /// used directly in the signal_t.decoder field.
220 ///
221 /// This function always flips 'send' to false.
222 ///
223 /// @param[in] signal  - The details of the signal that contains the state mapping.
224 /// @param[in] message - The message with data to decode.
225 /// @param[out] send - This output argument will always be set to false, so the caller will
226 ///      know not to publish this value to the pipeline.
227 ///
228 /// @return Return value is undefined.
229 ///
230 openxc_DynamicField decoder_t::decode_ignore(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
231 {
232         float value = decoder_t::parse_signal_bitfield(signal, message);
233         if(send)
234           *send = false;
235
236         signal.set_last_value(value);
237         openxc_DynamicField decoded_value;
238
239         return decoded_value;
240 }
241
242 /// @brief Find and return the corresponding string state for a CAN signal's
243 /// raw integer value.
244 ///
245 /// This is an implementation of the Signal type signature, and can be
246 /// used directly in the signal_t.decoder field.
247 ///
248 /// @param[in] signal  - The details of the signal that contains the state mapping.
249 /// @param[in] message - The message with data to decode.
250 /// @param[out] send - An output argument that will be set to false if the value should
251 ///     not be sent for any reason.
252 ///
253 /// @return Returns a DynamicField with a string value if a matching state is found in
254 /// the signal. If an equivalent isn't found, send is sent to false and the
255 /// return value is undefined.
256 ///
257 openxc_DynamicField decoder_t::decode_state(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
258 {
259         float value = decoder_t::parse_signal_bitfield(signal, message);
260         AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
261         const std::string signal_state = signal.get_states((uint8_t)value);
262         openxc_DynamicField decoded_value = build_DynamicField(signal_state);
263         if(signal_state.size() <= 0)
264         {
265                 *send = false;
266                 AFB_ERROR("No state found with index: %d", (int)value);
267         }
268
269         // Don't send if they is no changes
270         if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
271         {
272                 *send = false;
273         }
274         signal.set_last_value(value);
275
276
277         return decoded_value;
278 }
279
280
281 /// @brief Parse a signal from a CAN message, apply any required transforations
282 ///      to get a human readable value and public the result to the pipeline.
283 ///
284 /// If the signal_t has a non-NULL 'decoder' field, the raw CAN signal value
285 /// will be passed to the decoder before publishing.
286 ///
287 /// @param[in] signal - The details of the signal to decode and forward.
288 /// @param[in] message - The message with data to decode.
289 /// @param[out] send - An output parameter that will be flipped to false if the value could
290 ///      not be decoded.
291 ///
292 /// The decoder returns an openxc_DynamicField, which may contain a number,
293 /// string or boolean.
294 ///
295 openxc_DynamicField decoder_t::translate_signal(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
296 {
297
298         // Must call the decoders every time, regardless of if we are going to
299         // decide to send the signal or not.
300         openxc_DynamicField decoded_value = decoder_t::decode_signal(signal,
301                         message, send);
302
303         signal.set_received(true);
304         signal.set_timestamp(message->get_timestamp());
305         signal.get_message()->set_last_value(message);
306         return decoded_value;
307 }
308
309 /// @brief Parse a signal from a CAN message and apply any required
310 /// transforations to get a human readable value.
311 ///
312 /// If the signal_t has a non-NULL 'decoder' field, the raw CAN signal value
313 /// will be passed to the decoder before returning.
314 ///
315 /// @param[in] signal - The details of the signal to decode and forward.
316 /// @param[in] message - The message with data to decode.
317 /// @param[out] send - An output parameter that will be flipped to false if the value could
318 ///      not be decoded.
319 ///
320 /// @return The decoder returns an openxc_DynamicField, which may contain a number,
321 /// string or boolean. If 'send' is false, the return value is undefined.
322 ///
323 openxc_DynamicField decoder_t::decode_signal( signal_t& signal, std::shared_ptr<message_t> message, bool* send)
324 {
325         signal_decoder decoder = signal.get_decoder() == nullptr ?
326                                                         decode_noop : signal.get_decoder();
327
328         openxc_DynamicField decoded_value = decoder(signal,
329                         message, send);
330         return decoded_value;
331 }
332
333 ///
334 /// @brief Decode the payload of an OBD-II PID.
335 ///
336 /// This function matches the type signature for a DiagnosticResponse, so
337 /// it can be used as the decoder for a DiagnosticRequest. It returns the decoded
338 /// value of the PID, using the standard formulas (see
339 /// http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01).
340 ///
341 /// @param[in] response - the received DiagnosticResponse (the data is in response.payload,
342 ///  a byte array). This is most often used when the byte order is
343 ///  signiticant, i.e. with many OBD-II PID formulas.
344 /// @param[in] parsed_payload - the entire payload of the response parsed as an int.
345 ///
346 /// @return Float decoded value.
347 ///
348 float decoder_t::decode_obd2_response(const DiagnosticResponse* response, float parsed_payload)
349 {
350         return diagnostic_decode_obd2_pid(response);
351 }