Files
dbc/core/decode_database.h
T
2026-10-07 18:32:51 -04:00

121 lines
3.5 KiB
C++

/**
* @file decode_database.h
* @brief
*
* Created: 2026-03-13
* Author: Deeaitch (Dim. Himro)
*
* Licensed under the MIT License.
* See LICENSE file in the project root for full license text.
*/
#ifndef DECODE_DATABASE_H
#define DECODE_DATABASE_H
#include "value_descriptions.h"
#include "multiplexing.h"
#include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>
/**
* @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 {
Multiplexing multiplexing;
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. */
ValueDescriptions valueDescriptions; /**< Raw integer state descriptions. */
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 */