2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "can-decoder.hpp"
19 #include "canutil/read.h"
20 #include "../utils/openxc-utils.hpp"
22 /* Public: Parse the signal's bitfield from the given data and return the raw
25 * @param[in] signal - The signal to parse from the data.
26 * @param[in] message - can_message_t to parse
28 * @return Returns the raw value of the signal parsed as a bitfield from the given byte
31 float decoder_t::parseSignalBitfield(can_signal_t& signal, const can_message_t& message)
33 return bitfield_parse_float(message.get_data(), CAN_MESSAGE_SIZE,
34 signal.get_bit_position(), signal.get_bit_size(), signal.get_factor(),
38 /* Public: Wrap a raw CAN signal value in a DynamicField without modification.
40 * This is an implementation of the SignalDecoder type signature, and can be
41 * used directly in the can_signal_t.decoder field.
43 * @param[in] signal - The details of the signal that contains the state mapping.
44 * @param[in] signals - The list of all signals
45 * @param[in] value - The numerical value that will be wrapped in a DynamicField.
46 * @param[out]send - An output argument that will be set to false if the value should
47 * not be sent for any reason.
49 * @return Returns a DynamicField with the original, unmodified raw CAN signal value as
50 * its numeric value. The 'send' argument will not be modified as this decoder
53 openxc_DynamicField decoder_t::noopDecoder(can_signal_t& signal,
54 const std::vector<can_signal_t>& signals, float value, bool* send)
56 openxc_DynamicField decoded_value = build_DynamicField(value);
60 /* Public: Coerces a numerical value to a boolean.
62 * This is an implementation of the SignalDecoder type signature, and can be
63 * used directly in the can_signal_t.decoder field.
65 * @param[in] signal - The details of the signal that contains the state mapping.
66 * @param[in] signals - The list of all signals
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.
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.
75 openxc_DynamicField decoder_t::booleanDecoder(can_signal_t& signal,
76 const std::vector<can_signal_t>& signals, float value, bool* send)
78 openxc_DynamicField decoded_value = build_DynamicField(value == 0.0 ? false : true);
82 /* Public: Update the metadata for a signal and the newly received value.
84 * This is an implementation of the SignalDecoder type signature, and can be
85 * used directly in the can_signal_t.decoder field.
87 * This function always flips 'send' to false.
89 * @param[in] signal - The details of the signal that contains the state mapping.
90 * @param[in] signals - The list of all signals.
91 * @param[in] value - The numerical value that will be converted to a boolean.
92 * @param[out] send - This output argument will always be set to false, so the caller will
93 * know not to publish this value to the pipeline.
95 * @return Return value is undefined.
97 openxc_DynamicField decoder_t::ignoreDecoder(can_signal_t& signal,
98 const std::vector<can_signal_t>& signals, float value, bool* send)
103 openxc_DynamicField decoded_value;
105 return decoded_value;
108 /* Public: Find and return the corresponding string state for a CAN signal's
111 * This is an implementation of the SignalDecoder type signature, and can be
112 * used directly in the can_signal_t.decoder field.
114 * @param[in] signal - The details of the signal that contains the state mapping.
115 * @param[in] signals - The list of all signals.
116 * @param[in] value - The numerical value that should map to a state.
117 * @param[out] send - An output argument that will be set to false if the value should
118 * not be sent for any reason.
120 * @return Returns a DynamicField with a string value if a matching state is found in
121 * the signal. If an equivalent isn't found, send is sent to false and the
122 * return value is undefined.
124 openxc_DynamicField decoder_t::stateDecoder(can_signal_t& signal,
125 const std::vector<can_signal_t>& signals, float value, bool* send)
127 const std::string signal_state = signal.get_states(value);
128 openxc_DynamicField decoded_value = build_DynamicField(signal_state);
129 if(signal_state.size() <= 0)
132 ERROR(binder_interface, "stateDecoder: No state found with index: %d", (int)value);
134 return decoded_value;
138 /* Public: Parse a signal from a CAN message, apply any required transforations
139 * to get a human readable value and public the result to the pipeline.
141 * If the can_signal_t has a non-NULL 'decoder' field, the raw CAN signal value
142 * will be passed to the decoder before publishing.
144 * @param[in] signal - The details of the signal to decode and forward.
145 * @param[in] message - The received CAN message that should contain this signal.
146 * @param[in] signals - an array of all active signals.
148 * The decoder returns an openxc_DynamicField, which may contain a number,
151 openxc_DynamicField decoder_t::translateSignal(can_signal_t& signal, can_message_t& message,
152 const std::vector<can_signal_t>& signals)
154 if(&signal == nullptr || &message == nullptr)
156 openxc_DynamicField ret = {0, openxc_DynamicField_Type_BOOL, 0, "", 0, 0, 0, 0};
160 float value = decoder_t::parseSignalBitfield(signal, message);
161 DEBUG(binder_interface, "translateSignal: Decoded message from parseSignalBitfield: %f", value);
164 // Must call the decoders every time, regardless of if we are going to
165 // decide to send the signal or not.
166 openxc_DynamicField decoded_value = decoder_t::decodeSignal(signal,
167 value, signals, &send);
169 signal.set_received(true);
170 signal.set_last_value(value);
171 return decoded_value;
174 /* Public: Parse a signal from a CAN message and apply any required
175 * transforations to get a human readable value.
177 * If the can_signal_t has a non-NULL 'decoder' field, the raw CAN signal value
178 * will be passed to the decoder before returning.
180 * @param[in] signal - The details of the signal to decode and forward.
181 * @param[in] value - The numerical value that will be converted to a boolean.
182 * @param[in] signals - an array of all active signals.
183 * @param[out] send - An output parameter that will be flipped to false if the value could
186 * @return The decoder returns an openxc_DynamicField, which may contain a number,
187 * string or boolean. If 'send' is false, the return value is undefined.
189 openxc_DynamicField decoder_t::decodeSignal( can_signal_t& signal,
190 float value, const std::vector<can_signal_t>& signals, bool* send)
192 SignalDecoder decoder = signal.get_decoder() == nullptr ?
193 noopDecoder : signal.get_decoder();
194 openxc_DynamicField decoded_value = decoder(signal, signals,
196 return decoded_value;
199 /* Public: Decode a transformed, human readable value from an raw CAN signal
200 * already parsed from a CAN message.
202 * This is the same as decodeSignal but you must parse the bitfield value of the signal from the CAN
203 * message yourself. This is useful if you need that raw value for something
206 * @param[in] signal - The details of the signal to decode and forward.
207 * @param[in] value - The numerical value that will be converted to a boolean.
208 * @param[in] signals - an array of all active signals.
209 * @param[out] send - An output parameter that will be flipped to false if the value could
212 openxc_DynamicField decoder_t::decodeSignal( can_signal_t& signal,
213 const can_message_t& message, const std::vector<can_signal_t>& signals, bool* send)
215 float value = parseSignalBitfield(signal, message);
216 return decodeSignal(signal, value, signals, send);