From 06acf7a42da0ab605d9dac3c4c0c66f426a06d17 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Mon, 27 Feb 2017 20:25:52 +0100 Subject: Changed the decoding function Change-Id: Ib27fb13d4fb7959509400a3c2eff2859d1ce9c2b Signed-off-by: Romain Forlot --- src/can-decoder.cpp | 83 +++++++++++++++++++++++++++++----------------- src/can-decoder.hpp | 29 ++++++++++++---- src/can-utils.hpp | 6 ++-- src/can_decode_message.cpp | 5 ++- 4 files changed, 80 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/can-decoder.cpp b/src/can-decoder.cpp index b163011f..b222bc62 100644 --- a/src/can-decoder.cpp +++ b/src/can-decoder.cpp @@ -27,17 +27,17 @@ float decoder_t::parseSignalBitfield(const CanSignal& signal, const CanMessage& } openxc_DynamicField decoder_t::noopDecoder(const CanSignal& signal, - const CanSignal& signals, float value, bool* send) + const CanSignal& signals, float value, bool* send) { decoded_value = { .has_type = true, .type = openxc_DynamicField_Type_NUM, .has_numeric_value = true, .numeric_value = value }; - return decoded_value; + return decoded_value; } openxc_DynamicField decoder_t::booleanDecoder(const CanSignal& signal, - const CanSignal& signals, float value, bool* send) + const CanSignal& signals, float value, bool* send) { decoded_value = { .has_type = true, .type = openxc_DynamicField_Type_BOOL, @@ -47,45 +47,66 @@ openxc_DynamicField decoder_t::booleanDecoder(const CanSignal& signal, } openxc_DynamicField decoder_t::ignoreDecoder(const CanSignal& signal, - const CanSignal& signals, float value, bool* send) + const CanSignal& signals, float value, bool* send) { - if(send) - *send = false; - - openxc_DynamicField decoded_value = {0}; - return decoded_value; + if(send) + *send = false; + + openxc_DynamicField decoded_value = {0}; + return decoded_value; } openxc_DynamicField decoder_t::stateDecoder(const CanSignal& signal, - const CanSignal& signals, float value, bool* send) + const CanSignal& signals, float value, bool* send) +{ + openxc_DynamicField decoded_value = {0}; + decoded_value.has_type = true; + decoded_value.type = openxc_DynamicField_Type_STRING; + decoded_value.has_string_value = true; + + const CanSignalState* signalState = lookupSignalState(value, signal); + if(signalState != NULL) { + ::strcpy(decoded_value.string_value, signalState->name); + } else { + *send = false; + } + return decoded_value; +} + +openxc_DynamicField decoder_t::translateSignal(CanSignal& signal, can_message_t& message, + const std::vector& signals) { - openxc_DynamicField decoded_value = {0}; - decoded_value.has_type = true; - decoded_value.type = openxc_DynamicField_Type_STRING; - decoded_value.has_string_value = true; + if(signal == nullptr || message == nullptr) + { + return {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; + } + + float value = parseSignalBitfield(signal, message); + + bool send = true; + // Must call the decoders every time, regardless of if we are going to + // decide to send the signal or not. + openxc_DynamicField decoded_value = decodeSignal(signal, + value, signals, &send); - const CanSignalState* signalState = lookupSignalState(value, signal); - if(signalState != NULL) { - ::strcpy(decoded_value.string_value, signalState->name); - } else { - *send = false; - } - return decoded_value; + signal.received = true; + signal.lastValue = value; + return decoded_value; } openxc_DynamicField decoder_t::decodeSignal(const CanSignal& signal, - float value, const std::vector& signals, bool* send) + float value, const std::vector& signals, bool* send) { - SignalDecoder decoder = signal->decoder == NULL ? - noopDecoder : signal->decoder; - openxc_DynamicField decoded_value = decoder(signal, signals, - value, send); - return decoded_value; + SignalDecoder decoder = signal->decoder == NULL ? + noopDecoder : signal->decoder; + openxc_DynamicField decoded_value = decoder(signal, signals, + value, send); + return decoded_value; } openxc_DynamicField decoder_t::decodeSignal(const CanSignal& signal, - const can_message_t& message, const std::vector& signals, bool* send) + const can_message_t& message, const std::vector& signals, bool* send) { - float value = parseSignalBitfield(signal, message); - return decodeSignal(signal, value, signals, send); -} \ No newline at end of file + float value = parseSignalBitfield(signal, message); + return decodeSignal(signal, value, signals, send); +} diff --git a/src/can-decoder.hpp b/src/can-decoder.hpp index 7f02d610..ac967300 100644 --- a/src/can-decoder.hpp +++ b/src/can-decoder.hpp @@ -53,7 +53,7 @@ class decoder_t * the signal. If an equivalent isn't found, send is sent to false and the * return value is undefined. */ - openxc_DynamicField stateDecoder(const CanSignal& signal, const CanSignal& signals, + static openxc_DynamicField stateDecoder(const CanSignal& signal, const CanSignal& signals, float value, bool* send); /* Public: Coerces a numerical value to a boolean. @@ -72,7 +72,7 @@ class decoder_t * is 0.0, otherwise true. The 'send' argument will not be modified as this * decoder always succeeds. */ - openxc_DynamicField booleanDecoder(const CanSignal& signal, const CanSignal& signals, + static openxc_DynamicField booleanDecoder(const CanSignal& signal, const CanSignal& signals, float value, bool* send); /* Public: Update the metadata for a signal and the newly received value. @@ -91,7 +91,7 @@ class decoder_t * * The return value is undefined. */ - openxc_DynamicField ignoreDecoder(const CanSignal& signal, const CanSignal& signals, + static openxc_DynamicField ignoreDecoder(const CanSignal& signal, const CanSignal& signals, float value, bool* send); /* Public: Wrap a raw CAN signal value in a DynamicField without modification. @@ -110,9 +110,26 @@ class decoder_t * its numeric value. The 'send' argument will not be modified as this decoder * always succeeds. */ - openxc_DynamicField noopDecoder(const CanSignal& signal, const CanSignal& signals, - float value, bool* send); + static openxc_DynamicField noopDecoder(const CanSignal& signal, const CanSignal& signals, + float value); + + /* Public: Parse a signal from a CAN message, apply any required transforations + * to get a human readable value and public the result to the pipeline. + * + * If the CanSignal has a non-NULL 'decoder' field, the raw CAN signal value + * will be passed to the decoder before publishing. + * + * signal - The details of the signal to decode and forward. + * message - The received CAN message that should contain this signal. + * signals - an array of all active signals. + * + * The decoder returns an openxc_DynamicField, which may contain a number, + * string or boolean. + */ + openxc_DynamicField translateSignal(CanSignal& signal, can_message_t& message, + const std::vector& signals); + /* Public: Parse a signal from a CAN message and apply any required * transforations to get a human readable value. * @@ -130,7 +147,7 @@ class decoder_t * string or boolean. If 'send' is false, the return value is undefined. */ openxc_DynamicField decodeSignal(const CanSignal& signal, const can_message_t& message, - const std::vector& signals, bool* send); + const std::vector& signals); /* Public: Decode a transformed, human readable value from an raw CAN signal * already parsed from a CAN message. diff --git a/src/can-utils.hpp b/src/can-utils.hpp index 76dc75b3..5938169a 100644 --- a/src/can-utils.hpp +++ b/src/can-utils.hpp @@ -50,8 +50,8 @@ * * @return a decoded value in an openxc_DynamicField struct. */ -typedef openxc_DynamicField (*SignalDecoder)(struct CanSignal* signal, - CanSignal* signals, int signalCount, float value, bool* send); +typedef openxc_DynamicField (*SignalDecoder)(const struct CanSignal& signal, + const CanSignal& signals, float value, bool* send); /** * @brief: The type signature for a CAN signal encoder. @@ -463,7 +463,7 @@ typedef struct CanSignal CanSignal; * outgoing CAN messages. */ struct CanMessageDefinition { - struct CanBus* bus; /*!< bus - A pointer to the bus this message is on. */ + //can_bus_dev_t bus; /*!< bus - A pointer to the bus this message is on. */ uint32_t id; /*!< id - The ID of the message.*/ CanMessageFormat format; /*!< format - the format of the message's ID.*/ FrequencyClock frequencyClock; /*!< clock - an optional frequency clock to control the output of this diff --git a/src/can_decode_message.cpp b/src/can_decode_message.cpp index 3c1825d1..c311e98a 100644 --- a/src/can_decode_message.cpp +++ b/src/can_decode_message.cpp @@ -32,7 +32,6 @@ void can_decode_message(can_bus_t &can_bus) std::vector ::iterator signals_i; openxc_VehicleMessage vehicle_message; openxc_DynamicField search_key, decoded_message; - bool send = true; decoder_t decoder; @@ -49,7 +48,7 @@ void can_decode_message(can_bus_t &can_bus) signals = find_can_signals(search_key); /* Decoding the message ! Don't kill the messenger ! */ - for(const auto& sig : signals) + for(auto& sig : signals) { { std::lock_guard subscribed_signals_lock(subscribed_signals_mutex); @@ -59,7 +58,7 @@ void can_decode_message(can_bus_t &can_bus) if(it_event != subscribed_signals.end() && afb_event_is_valid(it_event->second)) { - decoded_message = decoder.decodeSignal(sig, can_message, getSignals(), &send); + decoded_message = decoder.translateSignal(sig, can_message, getSignals()); openxc_SimpleMessage s_message = build_SimpleMessage(sig.genericName, decoded_message); vehicle_message = build_VehicleMessage_with_SimpleMessage(openxc_DynamicField_Type::openxc_DynamicField_Type_NUM, s_message); -- cgit 1.2.3-korg