Closes #14
This commit is contained in:
1 parent
51fcdb88ac
commit
0708316a37
155 files changed
+5190
-2691
No files matched your search
+53
-53
@@ -12,9 +12,11 @@
|
||||
#ifndef DECODE_DATABASE_H
|
||||
#define DECODE_DATABASE_H
|
||||
|
||||
#include "value_descriptions.h"
|
||||
#include "definition_key.h"
|
||||
#include "multiplexing.h"
|
||||
#include "value_descriptions.h"
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
@@ -40,19 +42,19 @@ enum class ValueType {
|
||||
*/
|
||||
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. */
|
||||
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;
|
||||
};
|
||||
@@ -61,54 +63,52 @@ struct DecodeSignal {
|
||||
* @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. */
|
||||
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);
|
||||
FrameKey GetKey() const {
|
||||
return FrameKey(canId, isExtended);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Hasher for frame key.
|
||||
* @brief Immutable runtime snapshot. Copies share storage; reload replaces a snapshot.
|
||||
* Owning lookup handles and decoded results keep old definitions alive.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
};
|
||||
class DecodeDatabase {
|
||||
public:
|
||||
DecodeDatabase() = default;
|
||||
/**
|
||||
* Freeze definitions and build the frame index.
|
||||
* Do not retain mutable aliases into moved input vectors.
|
||||
* @throws std::invalid_argument for duplicate frame keys or signal names.
|
||||
*/
|
||||
explicit DecodeDatabase(std::vector<DecodeFrame> frames);
|
||||
|
||||
/**
|
||||
* @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;
|
||||
/** Borrowed view, valid while this snapshot remains alive and unchanged. */
|
||||
const std::vector<DecodeFrame> &GetFrames() const;
|
||||
/** Owning handles retain the entire snapshot across reload or destruction. */
|
||||
std::shared_ptr<const DecodeFrame> FindFrame(const FrameKey &key) const;
|
||||
std::shared_ptr<const DecodeSignal> FindSignal(const SignalKey &key) const;
|
||||
|
||||
private:
|
||||
struct Storage {
|
||||
std::vector<DecodeFrame> frames;
|
||||
std::unordered_map<FrameKey, std::size_t, FrameKeyHasher> frameIndexByKey;
|
||||
|
||||
explicit Storage(std::vector<DecodeFrame> definitions);
|
||||
};
|
||||
|
||||
std::shared_ptr<const Storage> storage;
|
||||
};
|
||||
|
||||
#endif /* DECODE_DATABASE_H */
|
||||
Reference in new issue
Block a user