115 lines
3.3 KiB
C++
115 lines
3.3 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 = 0U; /**< DBC start bit. */
|
|
std::uint32_t length = 0U; /**< Signal length in bits. */
|
|
ByteOrder byteOrder = ByteOrder::Intel; /**< Intel or Motorola. */
|
|
ValueType valueType = ValueType::Unsigned; /**< Signed or unsigned. */
|
|
double factor = 1.0; /**< Scaling factor. */
|
|
double offset = 0.0; /**< Physical offset. */
|
|
double minimum = 0.0; /**< Minimum physical value. */
|
|
double maximum = 0.0; /**< 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() = default;
|
|
};
|
|
|
|
/**
|
|
* @brief Runtime-ready frame definition.
|
|
*/
|
|
struct DecodeFrame {
|
|
std::string name; /**< Frame name. */
|
|
std::uint32_t canId = 0U; /**< Normalized CAN ID. */
|
|
bool isExtended = false; /**< true for extended frame. */
|
|
std::uint8_t dlc = 0U; /**< Payload length. */
|
|
std::uint32_t pgn = 0U; /**< PGN if available. */
|
|
bool hasPgn = false; /**< true if PGN is valid. */
|
|
std::string transmitter; /**< Transmitter ECU. */
|
|
std::string comment; /**< Frame comment. */
|
|
std::vector<DecodeSignal> signals; /**< Signal definitions. */
|
|
|
|
DecodeFrame() = default;
|
|
};
|
|
|
|
/**
|
|
* @brief Key for fast frame lookup.
|
|
*/
|
|
struct FrameKey {
|
|
std::uint32_t canId = 0U;
|
|
bool isExtended = false;
|
|
|
|
FrameKey() = default;
|
|
|
|
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 */
|