Closes #11
This commit is contained in:
1 parent
0067cb4ddd
commit
f17ddb6aad
104 files changed
+1928
-889
No files matched your search
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* @file dbc_decoder.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 DBC_DECODER_H
|
||||
#define DBC_DECODER_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#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::variant<std::int64_t, std::uint64_t> rawValue; /**< Exact signed or unsigned raw value. */
|
||||
double physicalValue; /**< Converted physical value. */
|
||||
bool valid; /**< true if decoding succeeded. */
|
||||
|
||||
DecodedSignalValue()
|
||||
: definition(nullptr), rawValue(std::uint64_t(0U)), 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 */
|
||||
Reference in new issue
Block a user