File indexing completed on 2024-12-22 03:35:43

0001 #include "DBCParser.h"
0002 
0003 #include <cmath>
0004 
0005 #include <QHash>
0006 #include <QStringList>
0007 
0008 DbcParser::ParseStatus DbcParser::isValid() {
0009     return m_parseFileStatus;
0010 }
0011 DbcParser::ParseStatus DbcParser::parseFile(const QString& filename) {
0012     m_parseFileStatus = DbcParser::ParseStatus::ErrorDBCParserUnsupported;
0013 #ifdef HAVE_DBC_PARSER
0014     try {
0015         m_parser.parse_file(filename.toStdString());
0016         m_parseFileStatus = DbcParser::ParseStatus::Success;
0017     } catch (libdbc::validity_error e) {
0018         // e.what(); // TODO: turn on
0019         m_parseFileStatus = DbcParser::ParseStatus::ErrorInvalidFile;
0020     }
0021 #else
0022     Q_UNUSED(filename)
0023 #endif
0024     return m_parseFileStatus;
0025 }
0026 
0027 DbcParser::ParseStatus DbcParser::parseMessage(const uint32_t id, const std::vector<uint8_t>& data, std::vector<double>& out) {
0028 #ifdef HAVE_DBC_PARSER
0029     if (m_parseFileStatus != ParseStatus::Success)
0030         return m_parseFileStatus;
0031     switch (m_parser.parseMessage(id, data, out)) {
0032     case libdbc::Message::ParseSignalsStatus::Success:
0033         return ParseStatus::Success;
0034     case libdbc::Message::ParseSignalsStatus::ErrorMessageToLong:
0035         return ParseStatus::ErrorMessageToLong;
0036     case libdbc::Message::ParseSignalsStatus::ErrorBigEndian:
0037         return ParseStatus::ErrorBigEndian;
0038     case libdbc::Message::ParseSignalsStatus::ErrorUnknownID:
0039         return ParseStatus::ErrorUnknownID;
0040     case libdbc::Message::ParseSignalsStatus::ErrorInvalidConversion:
0041         return ParseStatus::ErrorInvalidConversion;
0042     }
0043 #else
0044     Q_UNUSED(id)
0045     Q_UNUSED(data)
0046     Q_UNUSED(out)
0047 #endif
0048     return ParseStatus::ErrorDBCParserUnsupported;
0049 }
0050 
0051 /*!
0052  * \brief numberSignals
0053  * Determines the number of signals
0054  * \param ids Vector with all id's found in a log file
0055  * \return
0056  */
0057 void DbcParser::getSignals(const QVector<uint32_t> ids, PrefixType p, SuffixType s, QHash<uint32_t, int>& idIndex, Signals& out) const {
0058     out.signal_names.clear();
0059     out.value_descriptions.clear();
0060 #ifdef HAVE_DBC_PARSER
0061     for (const auto id : ids) {
0062         for (const auto& message : m_parser.get_messages()) {
0063             if (message.id() == id) {
0064                 idIndex.insert(id, out.value_descriptions.size());
0065                 for (const auto& signal_ : message.getSignals()) {
0066                     std::string signal_name;
0067                     switch (p) {
0068                     case PrefixType::Message:
0069                         signal_name += message.name() + "_";
0070                         break;
0071                     case PrefixType::None:
0072                         break;
0073                     }
0074 
0075                     signal_name += signal_.name;
0076 
0077                     switch (s) {
0078                     case SuffixType::None:
0079                         break;
0080                     case SuffixType::Unit:
0081                         signal_name += "_" + signal_.unit;
0082                         break;
0083                     case SuffixType::UnitIfAvailable:
0084                         if (signal_.unit.size() != 0)
0085                             signal_name += "_" + signal_.unit;
0086                         break;
0087                     }
0088                     out.signal_names.append(QString::fromStdString(signal_name));
0089                     std::vector<ValueDescriptions> vd;
0090                     vd.reserve(signal_.svDescriptions.size());
0091                     for (const auto& svdescription : signal_.svDescriptions) {
0092                         vd.push_back({svdescription.value, QString::fromStdString(svdescription.description)});
0093                     }
0094                     out.value_descriptions.push_back({vd});
0095                 }
0096                 break;
0097             }
0098         }
0099     }
0100 #else
0101     Q_UNUSED(ids)
0102     Q_UNUSED(p)
0103     Q_UNUSED(s)
0104     Q_UNUSED(idIndex)
0105     Q_UNUSED(out)
0106 #endif
0107 }