New constructor j1939 message definition
[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 "can-message-definition.hpp"
23 #include "../binding/low-can-hat.hpp"
24
25 /// @brief Parses the signal's bitfield from the given data and returns the raw
26 /// value.
27 ///
28 /// @param[in] signal - The signal to be parsed from the data.
29 /// @param[in] message - can_message_t to parse
30 ///
31 /// @return Returns the raw value of the signal parsed as a bitfield from the given byte
32 /// array.
33 ///
34 float decoder_t::parse_signal_bitfield(can_signal_t& signal, const message_t& message)
35 {
36          return bitfield_parse_float(message.get_data(), CAN_MESSAGE_SIZE,
37                         signal.get_bit_position(), signal.get_bit_size(), signal.get_factor(),
38                         signal.get_offset());
39 }
40
41 /// @brief Wraps a raw CAN signal value in a DynamicField without modification.
42 ///
43 /// This is an implementation of the Signal type signature, and can be
44 /// used directly in the can_signal_t.decoder field.
45 ///
46 /// @param[in] signal - The details of the signal that contains the state mapping.
47 /// @param[in] value - The numerical value that will be wrapped in a DynamicField.
48 /// @param[out] send - An output argument that will be set to false if the value should
49 ///     not be sent for any reason.
50 ///
51 /// @return Returns a DynamicField with the original, unmodified raw CAN signal value as
52 /// its numeric value. The 'send' argument will not be modified as this decoder
53 /// always succeeds.
54 ///
55 openxc_DynamicField decoder_t::decode_noop(can_signal_t& signal, float value, bool* send)
56 {
57         openxc_DynamicField decoded_value = build_DynamicField(value);
58
59         return decoded_value;
60 }
61 /// @brief Coerces a numerical value to a boolean.
62 ///
63 /// This is an implementation of the Signal type signature, and can be
64 /// used directly in the can_signal_t.decoder field.
65 ///
66 /// @param[in] signal  - The details of the signal that contains the state mapping.
67 /// @param[in] value - The numerical value that will be converted to a boolean.
68 /// @param[out] send - An output argument that will be set to false if the value should
69 ///     not be sent for any reason.
70 ///
71 /// @return Returns a DynamicField with a boolean value of false if the raw signal value
72 /// is 0.0, otherwise true. The 'send' argument will not be modified as this
73 /// decoder always succeeds.
74 ///
75 openxc_DynamicField decoder_t::decode_boolean(can_signal_t& signal, float value, bool* send)
76 {
77         openxc_DynamicField decoded_value = build_DynamicField(value == 0.0 ? false : true);
78
79         return decoded_value;
80 }
81 /// @brief Update the metadata for a signal and the newly received value.
82 ///
83 /// This is an implementation of the Signal type signature, and can be
84 /// used directly in the can_signal_t.decoder field.
85 ///
86 /// This function always flips 'send' to false.
87 ///
88 /// @param[in] signal  - The details of the signal that contains the state mapping.
89 /// @param[in] value - The numerical value that will be converted to a boolean.
90 /// @param[out] send - This output argument will always be set to false, so the caller will
91 ///      know not to publish this value to the pipeline.
92 ///
93 /// @return Return value is undefined.
94 ///
95 openxc_DynamicField decoder_t::decode_ignore(can_signal_t& signal, float value, bool* send)
96 {
97         if(send)
98           *send = false;
99
100         openxc_DynamicField decoded_value;
101
102         return decoded_value;
103 }
104
105 /// @brief Find and return the corresponding string state for a CAN signal's
106 /// raw integer value.
107 ///
108 /// This is an implementation of the Signal type signature, and can be
109 /// used directly in the can_signal_t.decoder field.
110 ///
111 /// @param[in] signal  - The details of the signal that contains the state mapping.
112 /// @param[in] value - The numerical value that should map to a state.
113 /// @param[out] send - An output argument that will be set to false if the value should
114 ///     not be sent for any reason.
115 ///
116 /// @return Returns a DynamicField with a string value if a matching state is found in
117 /// the signal. If an equivalent isn't found, send is sent to false and the
118 /// return value is undefined.
119 ///
120 openxc_DynamicField decoder_t::decode_state(can_signal_t& signal, float value, bool* send)
121 {
122         const std::string signal_state = signal.get_states((uint8_t)value);
123         openxc_DynamicField decoded_value = build_DynamicField(signal_state);
124         if(signal_state.size() <= 0)
125         {
126                 *send = false;
127                 AFB_ERROR("No state found with index: %d", (int)value);
128         }
129         return decoded_value;
130 }
131
132
133 /// @brief Parse a signal from a CAN message, apply any required transforations
134 ///      to get a human readable value and public the result to the pipeline.
135 ///
136 /// If the can_signal_t has a non-NULL 'decoder' field, the raw CAN signal value
137 /// will be passed to the decoder before publishing.
138 ///
139 /// @param[in] signal - The details of the signal to decode and forward.
140 /// @param[in] message - The received CAN message that should contain this signal.
141 /// @param[out] send - An output parameter that will be flipped to false if the value could
142 ///      not be decoded.
143 ///
144 /// The decoder returns an openxc_DynamicField, which may contain a number,
145 /// string or boolean.
146 ///
147 openxc_DynamicField decoder_t::translate_signal(can_signal_t& signal, const message_t& message, bool* send)
148 {
149         float value = decoder_t::parse_signal_bitfield(signal, message);
150         AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
151
152         // Must call the decoders every time, regardless of if we are going to
153         // decide to send the signal or not.
154         openxc_DynamicField decoded_value = decoder_t::decode_signal(signal,
155                         value, send);
156
157         signal.set_received(true);
158
159         // Don't send if they is no changes
160         if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
161         {
162                 *send = false;
163         }
164         signal.set_last_value(value);
165         signal.set_timestamp(message.get_timestamp());
166         signal.get_message()->set_last_value(message);
167         return decoded_value;
168 }
169
170 /// @brief Parse a signal from a CAN message and apply any required
171 /// transforations to get a human readable value.
172 ///
173 /// If the can_signal_t has a non-NULL 'decoder' field, the raw CAN signal value
174 /// will be passed to the decoder before returning.
175 ///
176 /// @param[in] signal - The details of the signal to decode and forward.
177 /// @param[in] value - The numerical value that will be converted to a boolean.
178 /// @param[out] send - An output parameter that will be flipped to false if the value could
179 ///      not be decoded.
180 ///
181 /// @return The decoder returns an openxc_DynamicField, which may contain a number,
182 /// string or boolean. If 'send' is false, the return value is undefined.
183 ///
184 openxc_DynamicField decoder_t::decode_signal( can_signal_t& signal, float value, bool* send)
185 {
186         signal_decoder decoder = signal.get_decoder() == nullptr ?
187                                                         decode_noop : signal.get_decoder();
188         openxc_DynamicField decoded_value = decoder(signal,
189                         value, send);
190         return decoded_value;
191 }
192
193 /// @brief Decode a transformed, human readable value from an raw CAN signal
194 /// already parsed from a CAN message.
195 ///
196 /// This is the same as decode_signal but you must parse the bitfield value of the signal from the CAN
197 /// message yourself. This is useful if you need that raw value for something
198 /// else.
199 ///
200 /// @param[in] signal - The details of the signal to decode and forward.
201 /// @param[in] message - Raw CAN message to decode
202 /// @param[out] send - An output parameter that will be flipped to false if the value could
203 ///      not be decoded.
204 ///
205 openxc_DynamicField decoder_t::decode_signal( can_signal_t& signal, const can_message_t& message, bool* send)
206 {
207         float value = parse_signal_bitfield(signal, message);
208         return decode_signal(signal, value, send);
209 }
210
211
212 ///
213 /// @brief Decode the payload of an OBD-II PID.
214 ///
215 /// This function matches the type signature for a DiagnosticResponse, so
216 /// it can be used as the decoder for a DiagnosticRequest. It returns the decoded
217 /// value of the PID, using the standard formulas (see
218 /// http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01).
219 ///
220 /// @param[in] response - the received DiagnosticResponse (the data is in response.payload,
221 ///  a byte array). This is most often used when the byte order is
222 ///  signiticant, i.e. with many OBD-II PID formulas.
223 /// @param[in] parsed_payload - the entire payload of the response parsed as an int.
224 ///
225 /// @return Float decoded value.
226 ///
227 float decoder_t::decode_obd2_response(const DiagnosticResponse* response, float parsed_payload)
228 {
229         return diagnostic_decode_obd2_pid(response);
230 }