Files
dbc/dbc_decoder.h

100 lines
2.9 KiB
C++

#ifndef DBC_DECODER_H
#define DBC_DECODER_H
#include <vector>
#include <cstdint>
#include "decode_database.h"
/**
* @brief Raw CAN frame used for runtime or trace decoding.
*/
struct RawCanFrame {
std::uint32_t canId; /**< Normalized CAN ID. */
bool isExtended; /**< true for extended frame. */
std::vector<std::uint8_t> data; /**< Payload bytes. */
RawCanFrame()
: canId (0U)
, isExtended (false)
, data() {
}
};
/**
* @brief One decoded signal value.
*/
struct DecodedSignalValue {
const DecodeSignal *definition; /**< Signal definition. */
std::int64_t rawValue; /**< Extracted raw integer value. */
double physicalValue; /**< Converted physical value. */
bool valid; /**< true if decoding succeeded. */
DecodedSignalValue()
: definition (nullptr)
, rawValue (0)
, physicalValue (0.0)
, valid (false) {
}
};
/**
* @brief Fully decoded frame.
*/
struct DecodedFrameValue {
const DecodeFrame *definition; /**< Frame definition. */
std::vector<DecodedSignalValue> signals; /**< Decoded signal values. */
bool valid; /**< true if frame was matched. */
DecodedFrameValue()
: definition (nullptr)
, signals()
, valid (false) {
}
};
/**
* @brief Runtime CAN decoder using prebuilt decode database.
*/
class DbcDecoder {
public:
/**
* @brief Find frame definition by CAN ID.
* @param database Runtime decode database.
* @param canId Normalized CAN ID.
* @param isExtended true for extended frame.
* @return Pointer to frame definition or nullptr.
*/
const DecodeFrame *FindFrame (const DecodeDatabase &database,
std::uint32_t canId,
bool isExtended) const;
/**
* @brief Decode one raw CAN frame.
* @param database Runtime decode database.
* @param frame Raw CAN frame.
* @return Decoded frame value.
*/
DecodedFrameValue Decode (const DecodeDatabase &database,
const RawCanFrame &frame) const;
private:
static bool ExtractUnsigned (const std::vector<std::uint8_t> &data,
const DecodeSignal &signal,
std::uint64_t &value);
static bool ExtractIntel (const std::vector<std::uint8_t> &data,
std::uint32_t startBit,
std::uint32_t length,
std::uint64_t &value);
static bool ExtractMotorola (const std::vector<std::uint8_t> &data,
std::uint32_t startBit,
std::uint32_t length,
std::uint64_t &value);
static std::int64_t SignExtend (std::uint64_t value, std::uint32_t bitLength);
};
#endif /* DBC_DECODER_H */