Added decoding support to runtime decode.
This commit is contained in:
126
decode_database.h
Normal file
126
decode_database.h
Normal file
@@ -0,0 +1,126 @@
|
||||
#ifndef DECODE_DATABASE_H
|
||||
#define DECODE_DATABASE_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <cstdint>
|
||||
|
||||
/**
|
||||
* @brief Signal byte order used for runtime decoding.
|
||||
*/
|
||||
enum class ByteOrder {
|
||||
Intel,
|
||||
Motorola
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Signal numeric type.
|
||||
*/
|
||||
enum class ValueType {
|
||||
Unsigned,
|
||||
Signed
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Runtime-ready signal definition.
|
||||
*/
|
||||
struct DecodeSignal {
|
||||
std::string name; /**< Signal name. */
|
||||
std::uint32_t startBit; /**< DBC start bit. */
|
||||
std::uint32_t length; /**< Signal length in bits. */
|
||||
ByteOrder byteOrder; /**< Intel or Motorola. */
|
||||
ValueType valueType; /**< Signed or unsigned. */
|
||||
double factor; /**< Scaling factor. */
|
||||
double offset; /**< Physical offset. */
|
||||
double minimum; /**< Minimum physical value. */
|
||||
double maximum; /**< Maximum physical value. */
|
||||
std::string unit; /**< Physical unit. */
|
||||
std::vector<std::string> receivers; /**< Receivers. */
|
||||
std::string comment; /**< Comment. */
|
||||
|
||||
DecodeSignal()
|
||||
: name()
|
||||
, startBit (0U)
|
||||
, length (0U)
|
||||
, byteOrder (ByteOrder::Intel)
|
||||
, valueType (ValueType::Unsigned)
|
||||
, factor (1.0)
|
||||
, offset (0.0)
|
||||
, minimum (0.0)
|
||||
, maximum (0.0)
|
||||
, unit()
|
||||
, receivers()
|
||||
, comment() {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Runtime-ready frame definition.
|
||||
*/
|
||||
struct DecodeFrame {
|
||||
std::string name; /**< Frame name. */
|
||||
std::uint32_t canId; /**< Normalized CAN ID. */
|
||||
bool isExtended; /**< true for extended frame. */
|
||||
std::uint8_t dlc; /**< Payload length. */
|
||||
std::uint32_t pgn; /**< PGN if available. */
|
||||
bool hasPgn; /**< true if PGN is valid. */
|
||||
std::string transmitter; /**< Transmitter ECU. */
|
||||
std::string comment; /**< Frame comment. */
|
||||
std::vector<DecodeSignal> signals; /**< Signal definitions. */
|
||||
|
||||
DecodeFrame()
|
||||
: name()
|
||||
, canId (0U)
|
||||
, isExtended (false)
|
||||
, dlc (0U)
|
||||
, pgn (0U)
|
||||
, hasPgn (false)
|
||||
, transmitter()
|
||||
, comment()
|
||||
, signals() {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Key for fast frame lookup.
|
||||
*/
|
||||
struct FrameKey {
|
||||
std::uint32_t canId;
|
||||
bool isExtended;
|
||||
|
||||
FrameKey()
|
||||
: canId (0U)
|
||||
, isExtended (false) {
|
||||
}
|
||||
|
||||
FrameKey (std::uint32_t id, bool extended)
|
||||
: canId (id)
|
||||
, isExtended (extended) {
|
||||
}
|
||||
|
||||
bool operator== (const FrameKey &other) const {
|
||||
return (canId == other.canId) && (isExtended == other.isExtended);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Hasher for frame key.
|
||||
*/
|
||||
struct FrameKeyHasher {
|
||||
std::size_t operator() (const FrameKey &key) const {
|
||||
const std::size_t a = static_cast<std::size_t> (key.canId);
|
||||
const std::size_t b = key.isExtended ? 1U : 0U;
|
||||
return (a * 1315423911U) ^ b;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Runtime decode database with fast lookup by CAN ID.
|
||||
*/
|
||||
struct DecodeDatabase {
|
||||
std::vector<DecodeFrame> frames;
|
||||
std::unordered_map<FrameKey, std::size_t, FrameKeyHasher> frameIndexByKey;
|
||||
};
|
||||
|
||||
#endif /* DECODE_DATABASE_H */
|
||||
Reference in New Issue
Block a user