diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e35d6e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ + +/build +*.user diff --git a/VkuMonitor.pro b/VkuMonitor.pro new file mode 100644 index 0000000..dc9a25a --- /dev/null +++ b/VkuMonitor.pro @@ -0,0 +1,46 @@ +QT += quick widgets gui xml serialport + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +INCLUDEPATH += cpp + +include($$PWD/cpp/core/core.pri) +include($$PWD/cpp/driver/driver.pri) +include($$PWD/cpp/parser/dbc/dbc.pri) + +include($$PWD/cpp/driver/CANBlastDriver/CANBlastDriver.pri) +include($$PWD/cpp/driver/SLCANDriver/SLCANDriver.pri) +win32:include($$PWD/cpp/driver/CandleApiDriver/CandleApiDriver.pri) +unix:include($$PWD/cpp/driver/SocketCanDriver/SocketCanDriver.pri) + +include($$PWD/cpp/window/SetupDialog/SetupDialog.pri) + +HEADERS += \ + cpp/CanController.h + +SOURCES += \ + cpp/CanController.cpp \ + cpp/main.cpp + +RESOURCES += \ + qml/qml.qrc \ + fonts/fonts.qrc + +TRANSLATIONS += \ + translations/VkuMonitor_ru_RU.ts + +CONFIG += lrelease +CONFIG += embed_translations + +# Additional import path used to resolve QML modules in Qt Creator's code model +QML_IMPORT_PATH = qml qml/Controls qml/Utils + +# Additional import path used to resolve QML modules just for Qt Quick Designer +QML_DESIGNER_IMPORT_PATH = + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/cpp/CanController.cpp b/cpp/CanController.cpp new file mode 100644 index 0000000..85f33c6 --- /dev/null +++ b/cpp/CanController.cpp @@ -0,0 +1,254 @@ +#include "CanController.h" + +#include +#include +#include +#include +#include + +#include +#include +#include + +#if defined(__linux__) +#include +#else +#include +#endif + +#include + +#include +#include +#include +#include + +CanController::CanController(QObject* parent) + : QObject{parent} +{ + setupDrivers(); + setupDefaultParameters(); + + auto& backend = Backend::instance(); + connect(backend.getTrace(), &CanTrace::messageEnqueued, this, &CanController::handlePackage); + + QSettings settings("settings.ini", QSettings::IniFormat); + auto sendParametersTimeout = settings.value("sendParametersTimeout", 50).toInt(); + + const auto sendParametersTimer = new QTimer(this); + sendParametersTimer->start(sendParametersTimeout); + connect(sendParametersTimer, &QTimer::timeout, this, &CanController::sendParameters); + + // auto statusTimer = new QTimer(this); + // statusTimer->start(5000); + + // statusTimer->callOnTimeout([this] + // { + // QVariantMap statusMap; + // statusMap.insert("time", QDateTime::currentDateTime().toString()); + // statusMap.insert("status", "0x" + QString::number(0xff, 16).toUpper().rightJustified(2, '0')); + // statusMap.insert("description", "asdfasdfasdf"); + // statuses_.prepend(statusMap); + // emit statusesChanged(); + // }); + + QTimer::singleShot(0, this, &CanController::connectCan); +} + +CanController::~CanController() +{ +} + +QObject* CanController::qmlInstance(QQmlEngine* /*engine*/, QJSEngine* /*scriptEngine*/) +{ + return new CanController; +} + +void CanController::connectCan() +{ + auto& backend = Backend::instance(); + backend.clearTrace(); + + QSettings settings("settings.ini", QSettings::IniFormat); + auto bitrate = settings.value("bitrate", 100000).toInt(); + + for (auto* network: backend.getSetup().getNetworks()) + { + for (auto* mi: network->interfaces()) + { + mi->setBitrate(bitrate); + } + } + + backend.startMeasurement(); +} + +void CanController::disconnectCan() +{ + Backend::instance().stopMeasurement(); +} + +void CanController::vkuClosure() +{ + CanMessage message(0x201); + message.setLength(8); + message.setByte(0, 0x01); + + auto& backend = Backend::instance(); + for (const auto interfaceId: backend.getInterfaceList()) + { + auto interface = backend.getInterfaceById(interfaceId); + if (interface) + { + interface->sendMessage(message); + } + } +} + +void CanController::emergencyReset() +{ + CanMessage message(0x201); + message.setLength(8); + message.setByte(0, 0x02); + + auto& backend = Backend::instance(); + for (const auto interfaceId: backend.getInterfaceList()) + { + auto interface = backend.getInterfaceById(interfaceId); + if (interface) + { + interface->sendMessage(message); + } + } +} + +void CanController::sendParameters() +{ + CanMessage message(0x202); + message.setLength(8); + + const auto maximumCurrent = maximumCurrent_.toUInt(); + message.setByte(0, maximumCurrent & 0xFF); + + const auto breakingDelay = breakingDelay_.toUInt(); + message.setByte(1, (breakingDelay >> 8) & 0xFF); + message.setByte(2, breakingDelay & 0xFF); + + const auto breakingCurrent = breakingCurrent_.toUInt(); + message.setByte(3, breakingCurrent & 0xFF); + + const auto emergencyDelay = emergencyDelay_.toUInt(); + message.setByte(4, emergencyDelay & 0xFF); + + const auto retriesAfterEmergencyBreak = retriesAfterEmergencyBreak_.toUInt(); + message.setByte(5, retriesAfterEmergencyBreak & 0xFF); + + // qDebug() << "Sending parameters: " << maximumCurrent; + + auto& backend = Backend::instance(); + for (const auto interfaceId: backend.getInterfaceList()) + { + auto interface = backend.getInterfaceById(interfaceId); + if (interface) + { + interface->sendMessage(message); + } + } +} + +void CanController::handlePackage(int index) +{ + auto& backend = Backend::instance(); + auto message = backend.getTrace()->getMessage(index); + if (!message) + { + return; + } + + if (message->getId() == 0x501) + { + handleCurrentState(message); + } + + if (message->getId() == 0x502) + { + handleStatus(message); + } +} + +void CanController::handleCurrentState(const CanMessage* message) +{ + quint16 inputVoltage = (message->getByte(0) << 8) + message->getByte(1); + setProperty("inputVoltage", QString::number(inputVoltage)); + + quint16 outputVoltage = (message->getByte(2) << 8) + message->getByte(3); + setProperty("outputVoltage", QString::number(outputVoltage)); + + quint8 outputCurrent = message->getByte(4); + setProperty("outputCurrent", QString::number(outputCurrent)); + + qint16 radiatorTemperature = (message->getByte(5) << 8) + message->getByte(6); + setProperty("radiatorTemperature", QString::number(radiatorTemperature)); +} + +void CanController::handleStatus(const CanMessage* message) +{ + QMap statusDescriptionMap = + { + {0x00, tr("Reset")}, + {0x01, tr("No incoming messages via CAN interface")}, + {0x03, tr("Waiting for a command for closing")}, + {0x04, tr("Work via CAN")}, + {0x05, tr("Work via button signal")}, + {0x06, tr("Work via CAN or button signal")}, + {0x20, tr("Exceeding input voltage")}, + {0x22, tr("Exceeding maximum current")}, + {0x18, tr("Exceeding radiator temperature")}, + {0x02, tr("Pause after breaking")}, + {0x80, tr("Blocking due to emergency")} + }; + + quint8 status = message->getByte(0); + auto statusDescription = statusDescriptionMap.value(status, tr("Unknown status")); + + QVariantMap statusMap; + statusMap.insert("time", QDateTime::currentDateTime().toString()); + statusMap.insert("status", "0x" + QString::number(status, 16).toUpper().rightJustified(2, '0')); + statusMap.insert("description", statusDescription); + + if (statuses_.isEmpty() || statuses_.first().toMap()["status"] != statusMap["status"]) + { + statuses_.prepend(statusMap); + emit statusesChanged(); + } + + quint8 emergencyCounter = message->getByte(1); + setProperty("emergencyCounter", QString::number(emergencyCounter)); + + bool isVkuClosed = (message->getByte(2) & 0x1) == 1; + setProperty("isVkuClosed", isVkuClosed); +} + +void CanController::setupDrivers() +{ + auto& backend = Backend::instance(); + +#if defined(__linux__) + backend.addCanDriver(*(new SocketCanDriver(backend))); +#else + backend.addCanDriver(*(new CandleApiDriver(backend))); +#endif + // backend.addCanDriver(*(new SLCANDriver(backend))); + // backend.addCanDriver(*(new CANBlasterDriver(backend))); + + backend.setDefaultSetup(); +} + +void CanController::setupDefaultParameters() +{ + setProperty("maximumCurrent", QString::number(95)); + setProperty("emergencyDelay", QString::number(2)); + setProperty("breakingDelay", QString::number(0)); + setProperty("retriesAfterEmergencyBreak", QString::number(2)); + setProperty("breakingCurrent", QString::number(100)); +} diff --git a/cpp/CanController.h b/cpp/CanController.h new file mode 100644 index 0000000..2a7296d --- /dev/null +++ b/cpp/CanController.h @@ -0,0 +1,89 @@ +#ifndef CANCONTROLLER_H +#define CANCONTROLLER_H + +#include +#include + +class QQmlEngine; +class QJSEngine; + +class CanMessage; + +class CanController : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QString inputVoltage MEMBER inputVoltage_ NOTIFY inputVoltageChanged) + Q_PROPERTY(QString radiatorTemperature MEMBER radiatorTemperature_ NOTIFY radiatorTemperatureChanged) + Q_PROPERTY(QString outputVoltage MEMBER outputVoltage_ NOTIFY outputVoltageChanged) + Q_PROPERTY(QString emergencyCounter MEMBER emergencyCounter_ NOTIFY emergencyCounterChanged) + Q_PROPERTY(QString outputCurrent MEMBER outputCurrent_ NOTIFY outputCurrentChanged) + + Q_PROPERTY(QString maximumCurrent MEMBER maximumCurrent_ NOTIFY maximumCurrentChanged) + Q_PROPERTY(QString emergencyDelay MEMBER emergencyDelay_ NOTIFY emergencyDelayChanged) + Q_PROPERTY(QString breakingDelay MEMBER breakingDelay_ NOTIFY breakingDelayChanged) + Q_PROPERTY(QString retriesAfterEmergencyBreak MEMBER retriesAfterEmergencyBreak_ NOTIFY retriesAfterEmergencyBreakChanged) + Q_PROPERTY(QString breakingCurrent MEMBER breakingCurrent_ NOTIFY breakingCurrentChanged) + + Q_PROPERTY(QVariantList statuses MEMBER statuses_ NOTIFY statusesChanged) + Q_PROPERTY(bool isVkuClosed MEMBER isVkuClosed_ NOTIFY isVkuClosedChanged) + +public: + explicit CanController(QObject* parent = nullptr); + ~CanController(); + + static QObject* qmlInstance(QQmlEngine* engine, QJSEngine* scriptEngine); + +public slots: + void connectCan(); + void disconnectCan(); + + void vkuClosure(); + void emergencyReset(); + +private slots: + void sendParameters(); + +signals: + void inputVoltageChanged(); + void radiatorTemperatureChanged(); + void outputVoltageChanged(); + void emergencyCounterChanged(); + void outputCurrentChanged(); + + void maximumCurrentChanged(); + void emergencyDelayChanged(); + void breakingDelayChanged(); + void retriesAfterEmergencyBreakChanged(); + void breakingCurrentChanged(); + + void statusesChanged(); + void isVkuClosedChanged(); + +private slots: + void handlePackage(int index); + void handleCurrentState(const CanMessage* message); + void handleStatus(const CanMessage* message); + +private: + void setupDrivers(); + void setupDefaultParameters(); + +private: + QString inputVoltage_; + QString radiatorTemperature_; + QString outputVoltage_; + QString emergencyCounter_; + QString outputCurrent_; + + QString maximumCurrent_; + QString emergencyDelay_; + QString breakingDelay_; + QString retriesAfterEmergencyBreak_; + QString breakingCurrent_; + + QVariantList statuses_; + bool isVkuClosed_ = false; +}; + +#endif // CANCONTROLLER_H diff --git a/cpp/core/Backend.cpp b/cpp/core/Backend.cpp new file mode 100644 index 0000000..6bcac21 --- /dev/null +++ b/cpp/core/Backend.cpp @@ -0,0 +1,300 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "Backend.h" +#include "LogModel.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +Backend *Backend::_instance = 0; + +Backend::Backend() + : QObject(0), + _measurementRunning(false), + _measurementStartTime(0), + _setup(this) +{ + _logModel = new LogModel(*this); + + setDefaultSetup(); + _trace = new CanTrace(*this, this, 100); + + connect(&_setup, SIGNAL(onSetupChanged()), this, SIGNAL(onSetupChanged())); +} + +Backend &Backend::instance() +{ + if (!_instance) { + _instance = new Backend; + } + return *_instance; +} + +Backend::~Backend() +{ + delete _trace; +} + +void Backend::addCanDriver(CanDriver &driver) +{ + driver.init(_drivers.size()); + _drivers.append(&driver); +} + +bool Backend::startMeasurement() +{ + log_info("Starting measurement"); + + _measurementStartTime = QDateTime::currentMSecsSinceEpoch(); + _timerSinceStart.start(); + + int i=0; + foreach (MeasurementNetwork *network, _setup.getNetworks()) { + i++; + foreach (MeasurementInterface *mi, network->interfaces()) { + + CanInterface *intf = getInterfaceById(mi->canInterface()); + if (intf) { + intf->applyConfig(*mi); + + log_info(QString("Listening on interface: %1").arg(intf->getName())); + CanListener *listener = new CanListener(0, *this, *intf); + listener->startThread(); + _listeners.append(listener); + } + } + } + + _measurementRunning = true; + emit beginMeasurement(); + return true; +} + +bool Backend::stopMeasurement() +{ + if (_measurementRunning) { + foreach (CanListener *listener, _listeners) { + listener->requestStop(); + } + + foreach (CanListener *listener, _listeners) { + log_info(QString("Closing interface: %1").arg(getInterfaceName(listener->getInterfaceId()))); + listener->waitFinish(); + } + + qDeleteAll(_listeners); + _listeners.clear(); + + log_info("Measurement stopped"); + + _measurementRunning = false; + + emit endMeasurement(); + } + return true; +} + +bool Backend::isMeasurementRunning() const +{ + return _measurementRunning; +} + +void Backend::loadDefaultSetup(MeasurementSetup &setup) +{ + setup.clear(); + int i = 1; + + foreach (CanDriver *driver, _drivers) { + driver->update(); + foreach (CanInterfaceId intf, driver->getInterfaceIds()) { + MeasurementNetwork *network = setup.createNetwork(); + network->setName(QString().sprintf("Network %d", i++)); + + MeasurementInterface *mi = new MeasurementInterface(); + mi->setCanInterface(intf); + mi->setBitrate(500000); + network->addInterface(mi); + } + } +} + +void Backend::setDefaultSetup() +{ + loadDefaultSetup(_setup); +} + +MeasurementSetup &Backend::getSetup() +{ + return _setup; +} + +void Backend::setSetup(MeasurementSetup &new_setup) +{ + _setup.cloneFrom(new_setup); +} + +double Backend::currentTimeStamp() const +{ + return ((double)QDateTime::currentMSecsSinceEpoch()) / 1000; +} + +CanTrace *Backend::getTrace() +{ + return _trace; +} + +void Backend::clearTrace() +{ + _trace->clear(); +} + +CanDbMessage *Backend::findDbMessage(const CanMessage &msg) const +{ + return _setup.findDbMessage(msg); +} + +CanInterfaceIdList Backend::getInterfaceList() +{ + CanInterfaceIdList result; + foreach (CanDriver *driver, _drivers) { + foreach (CanInterfaceId id, driver->getInterfaceIds()) { + result.append(id); + } + } + return result; +} + +CanDriver *Backend::getDriverById(CanInterfaceId id) +{ + CanDriver *driver = _drivers.value((id>>8) & 0xFF); + if (!driver) { + log_critical(QString("Unable to get driver for interface id: %1. This should never happen.").arg(QString().number(id))); + } + return driver; +} + +CanInterface *Backend::getInterfaceById(CanInterfaceId id) +{ + CanDriver *driver = getDriverById(id); + return driver ? driver->getInterfaceById(id) : 0; +} + +QString Backend::getInterfaceName(CanInterfaceId id) +{ + CanInterface *intf = getInterfaceById(id); + if (intf) { + return intf->getName(); + } else { + log_critical(QString("Trying to get name from unknown interface id: %1. This should never happen.").arg(QString().number(id))); + return ""; + } +} + +QString Backend::getDriverName(CanInterfaceId id) +{ + CanDriver *driver = getDriverById(id); + return driver ? driver->getName() : ""; +} + +CanDriver *Backend::getDriverByName(QString driverName) +{ + foreach (CanDriver *driver, _drivers) { + if (driver->getName()==driverName) { + return driver; + } + } + return 0; +} + +CanInterface *Backend::getInterfaceByDriverAndName(QString driverName, QString deviceName) +{ + CanDriver *driver = getDriverByName(driverName); + if (driver) { + return driver->getInterfaceByName(deviceName); + } else { + return 0; + } + +} + +pCanDb Backend::loadDbc(QString filename) +{ + DbcParser parser; + + QFile *dbc = new QFile(filename); + pCanDb candb(new CanDb()); + parser.parseFile(dbc, *candb); + delete dbc; + + return candb; +} + +void Backend::clearLog() +{ + _logModel->clear(); +} + +LogModel &Backend::getLogModel() const +{ + return *_logModel; +} + +double Backend::getTimestampAtMeasurementStart() const +{ + return (double)_measurementStartTime / 1000.0; +} + +uint64_t Backend::getUsecsAtMeasurementStart() const +{ + return _measurementStartTime * 1000; +} + +uint64_t Backend::getNsecsSinceMeasurementStart() const +{ + return _timerSinceStart.nsecsElapsed(); +} + +uint64_t Backend::getUsecsSinceMeasurementStart() const +{ + return getNsecsSinceMeasurementStart() / 1000; +} + +void Backend::logMessage(const QDateTime dt, const log_level_t level, const QString msg) +{ + // QFile logfile("tkr.log"); + // logfile.open(QIODevice::WriteOnly | QIODevice::Append); + // logfile.write(QString("%1 %2 %3\n").arg(dt.toString("yyyy-MM-dd hh:mm:ss.zzz")).arg(level).arg(msg).toUtf8()); + // logfile.close(); + + qDebug() << "logMessage" << dt << level << msg; + emit onLogMessage(dt, level, msg); +} diff --git a/cpp/core/Backend.h b/cpp/core/Backend.h new file mode 100644 index 0000000..9e2a40e --- /dev/null +++ b/cpp/core/Backend.h @@ -0,0 +1,114 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class MeasurementNetwork; +class CanTrace; +class CanListener; +class CanDbMessage; +class SetupDialog; +class LogModel; + +class Backend : public QObject +{ + Q_OBJECT +public: + static Backend &instance(); + + explicit Backend(); + virtual ~Backend(); + + void addCanDriver(CanDriver &driver); + + bool startMeasurement(); + bool stopMeasurement(); + bool isMeasurementRunning() const; + double getTimestampAtMeasurementStart() const; + uint64_t getUsecsAtMeasurementStart() const; + uint64_t getNsecsSinceMeasurementStart() const; + uint64_t getUsecsSinceMeasurementStart() const; + + + void logMessage(const QDateTime dt, const log_level_t level, const QString msg); + + MeasurementSetup &getSetup(); + void loadDefaultSetup(MeasurementSetup &setup); + void setDefaultSetup(); + void setSetup(MeasurementSetup &new_setup); + + double currentTimeStamp() const; + + CanTrace *getTrace(); + void clearTrace(); + + CanDbMessage *findDbMessage(const CanMessage &msg) const; + + CanInterfaceIdList getInterfaceList(); + CanDriver *getDriverById(CanInterfaceId id); + CanInterface *getInterfaceById(CanInterfaceId id); + QString getInterfaceName(CanInterfaceId id); + QString getDriverName(CanInterfaceId id); + + CanDriver *getDriverByName(QString driverName); + CanInterface *getInterfaceByDriverAndName(QString driverName, QString deviceName); + + pCanDb loadDbc(QString filename); + + void clearLog(); + LogModel &getLogModel() const; + +signals: + void beginMeasurement(); + void endMeasurement(); + + void onSetupChanged(); + + void onLogMessage(const QDateTime dt, const log_level_t level, const QString msg); + + void onSetupDialogCreated(SetupDialog &dlg); + +public slots: + +private: + static Backend *_instance; + + bool _measurementRunning; + uint64_t _measurementStartTime; + QElapsedTimer _timerSinceStart; + QList _drivers; + MeasurementSetup _setup; + CanTrace *_trace; + QList _listeners; + + LogModel *_logModel; +}; diff --git a/cpp/core/CanDb.cpp b/cpp/core/CanDb.cpp new file mode 100644 index 0000000..b8dd89a --- /dev/null +++ b/cpp/core/CanDb.cpp @@ -0,0 +1,89 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "CanDb.h" +#include +#include + +#include + +CanDb::CanDb() +{ + +} + +QString CanDb::getFileName() +{ + QFileInfo fi(getPath()); + return fi.fileName(); +} + +QString CanDb::getDirectory() +{ + QFileInfo fi(getPath()); + return fi.absolutePath(); +} + +CanDbNode *CanDb::getOrCreateNode(QString node_name) +{ + if (!_nodes.contains(node_name)) { + CanDbNode *node = new CanDbNode(this); + node->setName(node_name); + _nodes[node_name] = node; + return node; + } else { + return _nodes[node_name]; + } +} + +CanDbMessage *CanDb::getMessageById(uint32_t raw_id) +{ + if (_messages.contains(raw_id)) { + return _messages[raw_id]; + } else { + return 0; + } +} + +void CanDb::addMessage(CanDbMessage *msg) +{ + _messages[msg->getRaw_id()] = msg; +} + +QString CanDb::getComment() const +{ + return _comment; +} + +void CanDb::setComment(const QString &comment) +{ + _comment = comment; +} + +bool CanDb::saveXML(Backend &backend, QDomDocument &xml, QDomElement &root) +{ + (void) backend; + (void) xml; + root.setAttribute("type", "dbc"); + root.setAttribute("filename", _path); + return true; +} + diff --git a/cpp/core/CanDb.h b/cpp/core/CanDb.h new file mode 100644 index 0000000..48175f1 --- /dev/null +++ b/cpp/core/CanDb.h @@ -0,0 +1,73 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include +#include + +#include "CanDbNode.h" +#include "CanDbMessage.h" + +class QDomDocument; +class QDomElement; + +class Backend; +class CanDb; +class CanDbMessage; + +typedef QMap CanDbNodeMap; +typedef QMap CanDbMessageList; +typedef QSharedPointer pCanDb; + +class CanDb +{ + public: + CanDb(); + + void setPath(QString path) { _path = path; } + QString getPath() { return _path; } + QString getFileName(); + QString getDirectory(); + + void setVersion(QString version) { _version = version; } + QString getVersion() { return _version; } + + CanDbNode *getOrCreateNode(QString node_name); + + CanDbMessage *getMessageById(uint32_t raw_id); + void addMessage(CanDbMessage *msg); + + QString getComment() const; + void setComment(const QString &comment); + + bool saveXML(Backend &backend, QDomDocument &xml, QDomElement &root); + +private: + QString _path; + QString _version; + QString _comment; + CanDbNodeMap _nodes; + CanDbMessageList _messages; + +}; diff --git a/cpp/core/CanDbMessage.cpp b/cpp/core/CanDbMessage.cpp new file mode 100644 index 0000000..4819ce8 --- /dev/null +++ b/cpp/core/CanDbMessage.cpp @@ -0,0 +1,112 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "CanDbMessage.h" + +CanDbMessage::CanDbMessage(CanDb *parent) + : _parent(parent), _raw_id(0), _dlc(0), _sender(0), _muxer(0) +{ +} + +QString CanDbMessage::getName() const +{ + return _name; +} + +void CanDbMessage::setName(const QString &name) +{ + _name = name; +} + +uint32_t CanDbMessage::getRaw_id() const +{ + return _raw_id; +} + +void CanDbMessage::setRaw_id(const uint32_t &raw_id) +{ + _raw_id = raw_id; +} + +uint8_t CanDbMessage::getDlc() const +{ + return _dlc; +} + +void CanDbMessage::setDlc(const uint8_t &dlc) +{ + _dlc = dlc; +} + +CanDbNode *CanDbMessage::getSender() const +{ + return _sender; +} + +void CanDbMessage::setSender(CanDbNode *sender) +{ + _sender = sender; +} + +void CanDbMessage::addSignal(CanDbSignal *signal) +{ + _signals.append(signal); +} + +CanDbSignal *CanDbMessage::getSignal(int num) +{ + if (_signals.size()>num) { + return _signals[num]; + } else { + return 0; + } +} + +CanDbSignal *CanDbMessage::getSignalByName(QString signal_name) +{ + CanDbSignal *signal; + foreach (signal, _signals) { + if (signal->name() == signal_name) { + return signal; + } + } + return 0; +} + +QString CanDbMessage::getComment() const +{ + return _comment; +} + +void CanDbMessage::setComment(const QString &comment) +{ + _comment = comment; +} + +CanDbSignal *CanDbMessage::getMuxer() const +{ + return _muxer; +} + +void CanDbMessage::setMuxer(CanDbSignal *muxer) +{ + _muxer = muxer; +} diff --git a/cpp/core/CanDbMessage.h b/cpp/core/CanDbMessage.h new file mode 100644 index 0000000..da81997 --- /dev/null +++ b/cpp/core/CanDbMessage.h @@ -0,0 +1,73 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include "CanDb.h" +#include "CanDbSignal.h" + +class CanDbNode; +class CanDbSignal; + +typedef QList CanDbSignalList; + +class CanDbMessage +{ + public: + CanDbMessage(CanDb *parent); + + QString getName() const; + void setName(const QString &name); + + uint32_t getRaw_id() const; + void setRaw_id(const uint32_t &raw_id); + + uint8_t getDlc() const; + void setDlc(const uint8_t &dlc); + + CanDbNode *getSender() const; + void setSender(CanDbNode *sender); + + void addSignal(CanDbSignal *signal); + CanDbSignal *getSignal(int num); + CanDbSignal *getSignalByName(QString signal_name); + + CanDbSignalList getSignals() { return _signals; } + + QString getComment() const; + void setComment(const QString &comment); + + CanDbSignal *getMuxer() const; + void setMuxer(CanDbSignal *muxer); + +private: + CanDb *_parent; + QString _name; + uint32_t _raw_id; + uint8_t _dlc; + CanDbNode *_sender; + CanDbSignalList _signals; + QString _comment; + CanDbSignal *_muxer; + +}; diff --git a/cpp/core/CanDbNode.cpp b/cpp/core/CanDbNode.cpp new file mode 100644 index 0000000..b16e970 --- /dev/null +++ b/cpp/core/CanDbNode.cpp @@ -0,0 +1,49 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "CanDbNode.h" + +CanDbNode::CanDbNode(CanDb *parent) + : _parent(parent) +{ + +} + +QString CanDbNode::name() const +{ + return _name; +} + +void CanDbNode::setName(const QString &name) +{ + _name = name; +} + +QString CanDbNode::comment() const +{ + return _comment; +} + +void CanDbNode::setComment(const QString &comment) +{ + _comment = comment; +} + diff --git a/cpp/core/CanDbNode.h b/cpp/core/CanDbNode.h new file mode 100644 index 0000000..c4b2d80 --- /dev/null +++ b/cpp/core/CanDbNode.h @@ -0,0 +1,43 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include + +class CanDb; + +class CanDbNode +{ +public: + CanDbNode(CanDb *parent); + + QString name() const; + void setName(const QString &name); + + QString comment() const; + void setComment(const QString &comment); + +private: + CanDb *_parent; + QString _name; + QString _comment; +}; diff --git a/cpp/core/CanDbSignal.cpp b/cpp/core/CanDbSignal.cpp new file mode 100644 index 0000000..c039be0 --- /dev/null +++ b/cpp/core/CanDbSignal.cpp @@ -0,0 +1,229 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "CanDbSignal.h" + +CanDbSignal::CanDbSignal(CanDbMessage *parent) + : _parent(parent), + _isUnsigned(false), + _isBigEndian(false), + _factor(1), + _offset(0), + _min(0), + _max(0), + _isMuxer(false), + _isMuxed(false), + _muxValue(0) +{ +} + +QString CanDbSignal::name() const +{ + return _name; +} + +void CanDbSignal::setName(const QString &name) +{ + _name = name; +} + +uint8_t CanDbSignal::startBit() const +{ + return _startBit; +} + +void CanDbSignal::setStartBit(uint8_t startBit) +{ + _startBit = startBit; +} + +uint8_t CanDbSignal::length() const +{ + return _length; +} + +void CanDbSignal::setLength(uint8_t length) +{ + _length = length; +} + +QString CanDbSignal::comment() const +{ + return _comment; +} + +void CanDbSignal::setComment(const QString &comment) +{ + _comment = comment; +} + +QString CanDbSignal::getValueName(const uint64_t value) const +{ + if (_valueTable.contains(value)) { + return _valueTable[value]; + } else { + return QString(); + } +} + +void CanDbSignal::setValueName(const uint64_t value, const QString &name) +{ + _valueTable[value] = name; +} + +double CanDbSignal::convertRawValueToPhysical(const uint64_t rawValue) +{ + if (isUnsigned()) { + uint64_t v = rawValue; + return v * _factor + _offset; + } else { + // TODO check with DBC that actually contains signed values?! + int64_t v = (int64_t)(rawValue<<(64-_length)); + v>>=(64-_length); + return v * _factor + _offset; + + } +} + +double CanDbSignal::extractPhysicalFromMessage(const CanMessage &msg) +{ + return convertRawValueToPhysical(extractRawDataFromMessage(msg)); +} + +double CanDbSignal::getFactor() const +{ + return _factor; +} + +void CanDbSignal::setFactor(double factor) +{ + _factor = factor; +} + +double CanDbSignal::getOffset() const +{ + return _offset; +} + +void CanDbSignal::setOffset(double offset) +{ + _offset = offset; +} + +double CanDbSignal::getMinimumValue() const +{ + return _min; +} + +void CanDbSignal::setMinimumValue(double min) +{ + _min = min; +} + +double CanDbSignal::getMaximumValue() const +{ + return _max; +} + +void CanDbSignal::setMaximumValue(double max) +{ + _max = max; +} + +QString CanDbSignal::getUnit() const +{ + return _unit; +} + +void CanDbSignal::setUnit(const QString &unit) +{ + _unit = unit; +} + +bool CanDbSignal::isUnsigned() const +{ + return _isUnsigned; +} + +void CanDbSignal::setUnsigned(bool isUnsigned) +{ + _isUnsigned = isUnsigned; +} +bool CanDbSignal::isBigEndian() const +{ + return _isBigEndian; +} + +void CanDbSignal::setIsBigEndian(bool isBigEndian) +{ + _isBigEndian = isBigEndian; +} + +bool CanDbSignal::isMuxer() const +{ + return _isMuxer; +} + +void CanDbSignal::setIsMuxer(bool isMuxer) +{ + _isMuxer = isMuxer; +} + +bool CanDbSignal::isMuxed() const +{ + return _isMuxed; +} + +void CanDbSignal::setIsMuxed(bool isMuxed) +{ + _isMuxed = isMuxed; +} + +uint32_t CanDbSignal::getMuxValue() const +{ + return _muxValue; +} + +void CanDbSignal::setMuxValue(const uint32_t &muxValue) +{ + _muxValue = muxValue; +} + +bool CanDbSignal::isPresentInMessage(const CanMessage &msg) +{ + if ((_startBit + _length)>(8*msg.getLength())) { + return false; + } + + if (!_isMuxed) { return true; } + + CanDbSignal *muxer = _parent->getMuxer(); + if (!muxer) { return false; } + + return _muxValue == muxer->extractRawDataFromMessage(msg); +} + +uint64_t CanDbSignal::extractRawDataFromMessage(const CanMessage &msg) +{ + return msg.extractRawSignal(startBit(), length(), isBigEndian()); +} + + diff --git a/cpp/core/CanDbSignal.h b/cpp/core/CanDbSignal.h new file mode 100644 index 0000000..64ff4b4 --- /dev/null +++ b/cpp/core/CanDbSignal.h @@ -0,0 +1,106 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include "CanMessage.h" +#include "CanDbMessage.h" +#include +#include + +class CanDbMessage; + +typedef QMap CanDbValueTable; + +class CanDbSignal +{ +public: + CanDbSignal(CanDbMessage *parent); + QString name() const; + void setName(const QString &name); + + uint8_t startBit() const; + void setStartBit(uint8_t startBit); + + uint8_t length() const; + void setLength(uint8_t length); + + QString comment() const; + void setComment(const QString &comment); + + QString getValueName(const uint64_t value) const; + void setValueName(const uint64_t value, const QString &name); + + double getFactor() const; + void setFactor(double factor); + + double getOffset() const; + void setOffset(double offset); + + double getMinimumValue() const; + void setMinimumValue(double min); + + double getMaximumValue() const; + void setMaximumValue(double max); + + QString getUnit() const; + void setUnit(const QString &unit); + + bool isUnsigned() const; + void setUnsigned(bool isUnsigned); + + bool isBigEndian() const; + void setIsBigEndian(bool isBigEndian); + + bool isMuxer() const; + void setIsMuxer(bool isMuxer); + + bool isMuxed() const; + void setIsMuxed(bool isMuxed); + + uint32_t getMuxValue() const; + void setMuxValue(const uint32_t &muxValue); + + bool isPresentInMessage(const CanMessage &msg); + uint64_t extractRawDataFromMessage(const CanMessage &msg); + + double convertRawValueToPhysical(const uint64_t rawValue); + double extractPhysicalFromMessage(const CanMessage &msg); + + +private: + CanDbMessage *_parent; + QString _name; + uint8_t _startBit; + uint8_t _length; + bool _isUnsigned; + bool _isBigEndian; + double _factor; + double _offset; + double _min; + double _max; + QString _unit; + bool _isMuxer; + bool _isMuxed; + uint32_t _muxValue; + QString _comment; + CanDbValueTable _valueTable; +}; diff --git a/cpp/core/CanMessage.cpp b/cpp/core/CanMessage.cpp new file mode 100644 index 0000000..386cfbf --- /dev/null +++ b/cpp/core/CanMessage.cpp @@ -0,0 +1,346 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + + + +#include "CanMessage.h" +#include + +enum { + id_flag_extended = 0x80000000, + id_flag_rtr = 0x40000000, + id_flag_error = 0x20000000, + id_mask_extended = 0x1FFFFFFF, + id_mask_standard = 0x7FF +}; + +CanMessage::CanMessage() + : _raw_id(0), _dlc(0), _isFD(false), _interface(0), _u8() +{ + _timestamp.tv_sec = 0; + _timestamp.tv_usec = 0; + +} + +CanMessage::CanMessage(uint32_t can_id) + : _dlc(0), _interface(0), _u8() +{ + _timestamp.tv_sec = 0; + _timestamp.tv_usec = 0; + setId(can_id); +} + +CanMessage::CanMessage(const CanMessage &msg) +{ + cloneFrom(msg); +} + +void CanMessage::cloneFrom(const CanMessage &msg) +{ + _raw_id = msg._raw_id; + _dlc = msg._dlc; + + // Copy data + for(int i=0; i<64; i++) + { + _u8[i] = msg._u8[i]; + } + + _interface = msg._interface; + _timestamp = msg._timestamp; +} + + +uint32_t CanMessage::getRawId() const { + return _raw_id; +} + +void CanMessage::setRawId(const uint32_t raw_id) { + _raw_id = raw_id; +} + +uint32_t CanMessage::getId() const { + if (isExtended()) { + return _raw_id & id_mask_extended; + } else { + return _raw_id & id_mask_standard; + } +} + +void CanMessage::setId(const uint32_t id) { + _raw_id &= ~ id_mask_extended; + _raw_id = id; + if (id>0x7FF) { + setExtended(true); + } +} + +bool CanMessage::isExtended() const { + return (_raw_id & id_flag_extended) != 0; +} + +void CanMessage::setExtended(const bool isExtended) { + if (isExtended) { + _raw_id |= id_flag_extended; + } else { + _raw_id &= ~id_flag_extended; + } +} + +bool CanMessage::isRTR() const { + return (_raw_id & id_flag_rtr) != 0; +} + +void CanMessage::setRTR(const bool isRTR) { + if (isRTR) { + _raw_id |= id_flag_rtr; + } else { + _raw_id &= ~id_flag_rtr; + } +} + +bool CanMessage::isFD() const { + return _isFD; +} + +void CanMessage::setFD(const bool isFD) { + _isFD = isFD; +} + +bool CanMessage::isBRS() const { + return _isBRS; +} + +void CanMessage::setBRS(const bool isBRS) { + _isBRS = isBRS; +} + +bool CanMessage::isErrorFrame() const { + return (_raw_id & id_flag_error) != 0; +} + +void CanMessage::setErrorFrame(const bool isErrorFrame) { + if (isErrorFrame) { + _raw_id |= id_flag_error; + } else { + _raw_id &= ~id_flag_error; + } +} + +CanInterfaceId CanMessage::getInterfaceId() const +{ + return _interface; +} + +void CanMessage::setInterfaceId(CanInterfaceId interface) +{ + _interface = interface; +} + +uint8_t CanMessage::getLength() const { + return _dlc; +} + +void CanMessage::setLength(const uint8_t dlc) { + // Limit to CANFD max length + if (dlc<=64) { + _dlc = dlc; + } else { + _dlc = 8; + } +} + +uint8_t CanMessage::getByte(const uint8_t index) const { + if (index (getLength()*8)) { +// return 0; +// } + + // FIXME: This only gives access to data bytes 0-8. Need to rework for CANFD. + uint64_t data = le64toh(_u64[0]); + + data >>= start_bit; + + uint64_t mask = 0xFFFFFFFFFFFFFFFF; + mask <<= length; + mask = ~mask; + + data &= mask; + + // If the length is greater than 8, we need to byteswap to preserve endianness + if (isBigEndian && (length > 8)) + { + + // Swap bytes + data = __builtin_bswap64(data); + + // Shift out unused bits + data >>= 64 - length; + } + + return data; +} + +void CanMessage::setDataAt(uint8_t position, uint8_t data) +{ + if(position < 64) + _u8[position] = data; + else + return; +} + +void CanMessage::setData(const uint8_t d0) { + _dlc = 1; + _u8[0] = d0; +} + +void CanMessage::setData(const uint8_t d0, const uint8_t d1) { + _dlc = 2; + _u8[0] = d0; + _u8[1] = d1; +} + +void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2) { + _dlc = 3; + _u8[0] = d0; + _u8[1] = d1; + _u8[2] = d2; +} + +void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, + const uint8_t d3) { + _dlc = 4; + _u8[0] = d0; + _u8[1] = d1; + _u8[2] = d2; + _u8[3] = d3; +} + +void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, + const uint8_t d3, const uint8_t d4) { + _dlc = 5; + _u8[0] = d0; + _u8[1] = d1; + _u8[2] = d2; + _u8[3] = d3; + _u8[4] = d4; +} + +void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, + const uint8_t d3, const uint8_t d4, const uint8_t d5) { + _dlc = 6; + _u8[0] = d0; + _u8[1] = d1; + _u8[2] = d2; + _u8[3] = d3; + _u8[4] = d4; + _u8[5] = d5; +} + +void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, + const uint8_t d3, const uint8_t d4, const uint8_t d5, + const uint8_t d6) { + _dlc = 7; + _u8[0] = d0; + _u8[1] = d1; + _u8[2] = d2; + _u8[3] = d3; + _u8[4] = d4; + _u8[5] = d5; + _u8[6] = d6; +} + +void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, + const uint8_t d3, const uint8_t d4, const uint8_t d5, const uint8_t d6, + const uint8_t d7) { + _dlc = 8; + _u8[0] = d0; + _u8[1] = d1; + _u8[2] = d2; + _u8[3] = d3; + _u8[4] = d4; + _u8[5] = d5; + _u8[6] = d6; + _u8[7] = d7; +} + +timeval CanMessage::getTimestamp() const +{ + return _timestamp; +} + +void CanMessage::setTimestamp(const timeval timestamp) +{ + _timestamp = timestamp; +} + +void CanMessage::setTimestamp(const uint64_t seconds, const uint32_t micro_seconds) +{ + _timestamp.tv_sec = seconds; + _timestamp.tv_usec = micro_seconds; +} + +double CanMessage::getFloatTimestamp() const +{ + return (double)_timestamp.tv_sec + ((double)_timestamp.tv_usec/1000000); +} + +QDateTime CanMessage::getDateTime() const +{ + return QDateTime::fromMSecsSinceEpoch((qint64)(1000*getFloatTimestamp())); +} + +QString CanMessage::getIdString() const +{ + if (isExtended()) { + return QString().sprintf("0x%08X", getId()); + } else { + return QString().sprintf("0x%03X", getId()); + } +} + +QString CanMessage::getDataHexString() const +{ + if(getLength() == 0) + return ""; + + QString outstr = ""; + for(int i=0; i + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include + +#include +#include +#include + +class CanMessage { +public: + CanMessage(); + CanMessage(uint32_t can_id); + CanMessage(const CanMessage &msg); + void cloneFrom(const CanMessage &msg); + + uint32_t getRawId() const; + void setRawId(const uint32_t raw_id); + + uint32_t getId() const; + void setId(const uint32_t id); + + bool isExtended() const; + void setExtended(const bool isExtended); + + bool isRTR() const; + void setRTR(const bool isRTR); + + bool isFD() const; + void setFD(const bool isFD); + + bool isBRS() const; + void setBRS(const bool isFD); + + bool isErrorFrame() const; + void setErrorFrame(const bool isErrorFrame); + + CanInterfaceId getInterfaceId() const; + void setInterfaceId(CanInterfaceId interface); + + uint8_t getLength() const; + void setLength(const uint8_t dlc); + + uint8_t getByte(const uint8_t index) const; + void setByte(const uint8_t index, const uint8_t value); + + uint64_t extractRawSignal(uint8_t start_bit, const uint8_t length, const bool isBigEndian) const; + + void setDataAt(uint8_t position, uint8_t data); + void setData(const uint8_t d0); + void setData(const uint8_t d0, const uint8_t d1); + void setData(const uint8_t d0, const uint8_t d1, const uint8_t d2); + void setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, const uint8_t d3); + void setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, const uint8_t d3, const uint8_t d4); + void setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, const uint8_t d3, const uint8_t d4, const uint8_t d5); + void setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, const uint8_t d3, const uint8_t d4, const uint8_t d5, const uint8_t d6); + void setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, const uint8_t d3, const uint8_t d4, const uint8_t d5, const uint8_t d6, const uint8_t d7); + + struct timeval getTimestamp() const; + void setTimestamp(const struct timeval timestamp); + void setTimestamp(const uint64_t seconds, const uint32_t micro_seconds); + + double getFloatTimestamp() const; + QDateTime getDateTime() const; + + QString getIdString() const; + QString getDataHexString() const; + +private: + uint32_t _raw_id; + uint8_t _dlc; + bool _isFD; + bool _isBRS; + CanInterfaceId _interface; + union { + uint8_t _u8[8*8]; + uint16_t _u16[4*8]; + uint32_t _u32[2*8]; + uint64_t _u64[8]; + }; + struct timeval _timestamp; + +}; diff --git a/cpp/core/CanTrace.cpp b/cpp/core/CanTrace.cpp new file mode 100644 index 0000000..d558cf6 --- /dev/null +++ b/cpp/core/CanTrace.cpp @@ -0,0 +1,215 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "CanTrace.h" +#include +#include +#include + +#include +#include +#include +#include +#include + +CanTrace::CanTrace(Backend &backend, QObject *parent, int flushInterval) + : QObject(parent), + _backend(backend), + _isTimerRunning(false), + _mutex(QMutex::Recursive), + _timerMutex(), + _flushTimer(this) +{ + clear(); + _flushTimer.setSingleShot(true); + _flushTimer.setInterval(flushInterval); + connect(&_flushTimer, SIGNAL(timeout()), this, SLOT(flushQueue())); +} + +unsigned long CanTrace::size() +{ + QMutexLocker locker(&_mutex); + return _dataRowsUsed; +} + +void CanTrace::clear() +{ + QMutexLocker locker(&_mutex); + emit beforeClear(); + _data.resize(pool_chunk_size); + _dataRowsUsed = 0; + _newRows = 0; + emit afterClear(); +} + +const CanMessage *CanTrace::getMessage(int idx) +{ + QMutexLocker locker(&_mutex); + if (idx >= (_dataRowsUsed + _newRows)) { + return 0; + } else { + return &_data[idx]; + } +} + +void CanTrace::enqueueMessage(const CanMessage &msg, bool more_to_follow) +{ + QMutexLocker locker(&_mutex); + + int idx = size() + _newRows; + if (idx>=_data.size()) { + _data.resize(_data.size() + pool_chunk_size); + } + + _data[idx].cloneFrom(msg); + _newRows++; + + if (!more_to_follow) { + startTimer(); + } + + emit messageEnqueued(idx); +} + +void CanTrace::flushQueue() +{ + { + QMutexLocker locker(&_timerMutex); + _isTimerRunning = false; + } + + QMutexLocker locker(&_mutex); + if (_newRows) { + emit beforeAppend(_newRows); + + // see if we have muxed messages. cache muxed values, if any. + MeasurementSetup &setup = _backend.getSetup(); + for (int i=_dataRowsUsed; i<_dataRowsUsed + _newRows; i++) { + CanMessage &msg = _data[i]; + CanDbMessage *dbmsg = setup.findDbMessage(msg); + if (dbmsg && dbmsg->getMuxer()) { + foreach (CanDbSignal *signal, dbmsg->getSignals()) { + if (signal->isMuxed() && signal->isPresentInMessage(msg)) { + _muxCache[signal] = signal->extractRawDataFromMessage(msg); + } + } + } + } + + _dataRowsUsed += _newRows; + _newRows = 0; + emit afterAppend(); + } + +} + +void CanTrace::startTimer() +{ + QMutexLocker locker(&_timerMutex); + if (!_isTimerRunning) { + _isTimerRunning = true; + QMetaObject::invokeMethod(&_flushTimer, "start", Qt::QueuedConnection); + } +} + +void CanTrace::saveCanDump(QFile &file) +{ + QMutexLocker locker(&_mutex); + QTextStream stream(&file); + for (unsigned int i=0; igetFloatTimestamp())); + line.append(_backend.getInterfaceName(msg->getInterfaceId())); + if (msg->isExtended()) { + line.append(QString().sprintf(" %08X#", msg->getId())); + } else { + line.append(QString().sprintf(" %03X#", msg->getId())); + } + for (int i=0; igetLength(); i++) { + line.append(QString().sprintf("%02X", msg->getByte(i))); + } + stream << line << endl; + } +} + +void CanTrace::saveVectorAsc(QFile &file) +{ + QMutexLocker locker(&_mutex); + QTextStream stream(&file); + + if (_data.length()<1) { + return; + } + + + auto firstMessage = _data.first(); + double t_start = firstMessage.getFloatTimestamp(); + + QLocale locale_c(QLocale::C); + QString dt_start = locale_c.toString(firstMessage.getDateTime(), "ddd MMM dd hh:mm:ss.zzz ap yyyy"); + + stream << "date " << dt_start << endl; + stream << "base hex timestamps absolute" << endl; + stream << "internal events logged" << endl; + stream << "// version 8.5.0" << endl; + stream << "Begin Triggerblock " << dt_start << endl; + stream << " 0.000000 Start of measurement" << endl; + + for (unsigned int i=0; i + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "CanMessage.h" + +class CanInterface; +class CanDbMessage; +class CanDbSignal; +class MeasurementSetup; +class Backend; + +class CanTrace : public QObject +{ + Q_OBJECT + +public: + explicit CanTrace(Backend &backend, QObject *parent, int flushInterval); + + unsigned long size(); + void clear(); + const CanMessage *getMessage(int idx); + void enqueueMessage(const CanMessage &msg, bool more_to_follow=false); + + void saveCanDump(QFile &file); + void saveVectorAsc(QFile &file); + + bool getMuxedSignalFromCache(const CanDbSignal *signal, uint64_t *raw_value); + +signals: + void messageEnqueued(int idx); + void beforeAppend(int num_messages); + void afterAppend(); + void beforeClear(); + void afterClear(); + +private slots: + void flushQueue(); + +private: + enum { + pool_chunk_size = 1024 + }; + + Backend &_backend; + + QVector _data; + int _dataRowsUsed; + int _newRows; + bool _isTimerRunning; + + QMap _muxCache; + + QMutex _mutex; + QMutex _timerMutex; + QTimer _flushTimer; + + void startTimer(); + + +}; diff --git a/cpp/core/ConfigurableWidget.cpp b/cpp/core/ConfigurableWidget.cpp new file mode 100644 index 0000000..0609892 --- /dev/null +++ b/cpp/core/ConfigurableWidget.cpp @@ -0,0 +1,44 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "ConfigurableWidget.h" +#include +#include + +ConfigurableWidget::ConfigurableWidget(QWidget *parent) : QWidget(parent) +{ + +} + +bool ConfigurableWidget::saveXML(Backend &backend, QDomDocument &xml, QDomElement &root) +{ + (void) backend; + (void) xml; + (void) root; + return true; +} + +bool ConfigurableWidget::loadXML(Backend &backend, QDomElement &el) +{ + (void) backend; + (void) el; + return true; +} diff --git a/cpp/core/ConfigurableWidget.h b/cpp/core/ConfigurableWidget.h new file mode 100644 index 0000000..bc1a358 --- /dev/null +++ b/cpp/core/ConfigurableWidget.h @@ -0,0 +1,43 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include + +class Backend; +class QDomDocument; +class QDomElement; + +class ConfigurableWidget : public QWidget +{ + Q_OBJECT +public: + explicit ConfigurableWidget(QWidget *parent = 0); + virtual bool saveXML(Backend &backend, QDomDocument &xml, QDomElement &root); + virtual bool loadXML(Backend &backend, QDomElement &el); + +signals: + void settingsChanged(ConfigurableWidget *sender); + +public slots: +}; diff --git a/cpp/core/Log.cpp b/cpp/core/Log.cpp new file mode 100644 index 0000000..f8e9ed0 --- /dev/null +++ b/cpp/core/Log.cpp @@ -0,0 +1,63 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "Log.h" +#include + +void log_msg(const QDateTime dt, const log_level_t level, const QString msg) +{ + Backend::instance().logMessage(dt, level, msg); +} + +void log_msg(const log_level_t level, const QString msg) +{ + Backend::instance().logMessage(QDateTime::currentDateTime(), level, msg); +} + +void log_debug(const QString msg) +{ + log_msg(log_level_debug, msg); +} + +void log_info(const QString msg) +{ + log_msg(log_level_info, msg); +} + +void log_warning(const QString msg) +{ + log_msg(log_level_warning, msg); +} + +void log_error(const QString msg) +{ + log_msg(log_level_error, msg); +} + +void log_critical(const QString msg) +{ + log_msg(log_level_critical, msg); +} + +void log_fatal(const QString msg) +{ + log_msg(log_level_fatal, msg); +} diff --git a/cpp/core/Log.h b/cpp/core/Log.h new file mode 100644 index 0000000..08fc132 --- /dev/null +++ b/cpp/core/Log.h @@ -0,0 +1,43 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include + +typedef enum log_level { + log_level_debug, + log_level_info, + log_level_warning, + log_level_error, + log_level_critical, + log_level_fatal +} log_level_t; + +void log_msg(const QDateTime dt, const log_level_t level, const QString msg); +void log_msg(const log_level_t level, const QString msg); + +void log_debug(const QString msg); +void log_info(const QString msg); +void log_warning(const QString msg); +void log_error(const QString msg); +void log_critical(const QString msg); +void log_fatal(const QString msg); diff --git a/cpp/core/LogModel.cpp b/cpp/core/LogModel.cpp new file mode 100644 index 0000000..5ecc707 --- /dev/null +++ b/cpp/core/LogModel.cpp @@ -0,0 +1,160 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + + +#include "LogModel.h" + +LogModel::LogModel(Backend &backend) +{ + connect(&backend, SIGNAL(onLogMessage(QDateTime,log_level_t,QString)), this, SLOT(onLogMessage(QDateTime,log_level_t,QString))); +} + +LogModel::~LogModel() +{ + qDeleteAll(_items); + _items.clear(); +} + +void LogModel::clear() +{ + beginResetModel(); + _items.clear(); + endResetModel(); +} + +QModelIndex LogModel::index(int row, int column, const QModelIndex &parent) const +{ + if (parent.isValid()) { + return QModelIndex(); + } else { + return createIndex(row, column, (quintptr)0); + } +} + +QModelIndex LogModel::parent(const QModelIndex &child) const +{ + (void) child; + return QModelIndex(); +} + +int LogModel::rowCount(const QModelIndex &parent) const +{ + (void) parent; + return _items.size(); +} + +int LogModel::columnCount(const QModelIndex &parent) const +{ + (void) parent; + return column_count; +} + +bool LogModel::hasChildren(const QModelIndex &parent) const +{ + return !parent.isValid(); +} + +QVariant LogModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role == Qt::DisplayRole) { + + if (orientation == Qt::Horizontal) { + switch (section) { + case column_time: + return QString("Time"); + case column_level: + return QString("Level"); + case column_text: + return QString("Message"); + } + } + + } + return QVariant(); +} + +QVariant LogModel::data(const QModelIndex &index, int role) const +{ + + if (role == Qt::TextAlignmentRole) { + switch (index.column()) { + case column_time: + return Qt::AlignRight + Qt::AlignVCenter; + case column_level: + return Qt::AlignCenter + Qt::AlignVCenter; + case column_text: + return Qt::AlignLeft + Qt::AlignVCenter; + default: + return QVariant(); + } + } + + if (role != Qt::DisplayRole) { + return QVariant(); + } + + if (!index.isValid()) { + return QVariant(); + } + + LogItem *item = _items.value(index.row(), 0); + if (item) { + + switch (index.column()) { + case column_time: + return item->dt.toString("hh:mm:ss"); + case column_level: + return logLevelText(item->level); + case column_text: + return item->text; + default: + return QVariant(); + } + + } + return QVariant(); +} + +void LogModel::onLogMessage(const QDateTime dt, const log_level_t level, const QString msg) +{ + LogItem *item = new LogItem(); + item->dt = dt; + item->level = level; + item->text = msg; + + beginInsertRows(QModelIndex(), _items.size(), _items.size()); + _items.append(item); + endInsertRows(); +} + +QString LogModel::logLevelText(log_level_t level) +{ + switch (level) { + case log_level_debug: return "debug"; + case log_level_info: return "info"; + case log_level_warning: return "warning"; + case log_level_error: return "error"; + case log_level_critical: return "critical"; + case log_level_fatal: return "fatal"; + default: return ""; + } +} + diff --git a/cpp/core/LogModel.h b/cpp/core/LogModel.h new file mode 100644 index 0000000..c223bcf --- /dev/null +++ b/cpp/core/LogModel.h @@ -0,0 +1,71 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include +#include + +class LogItem { +public: + QDateTime dt; + log_level_t level; + QString text; +}; + +class LogModel : public QAbstractItemModel +{ + Q_OBJECT + +public: + enum { + column_time, + column_level, + column_text, + column_count + }; + +public: + LogModel(Backend &backend); + virtual ~LogModel(); + + void clear(); + + virtual QModelIndex index(int row, int column, const QModelIndex &parent) const; + virtual QModelIndex parent(const QModelIndex &child) const; + + virtual int rowCount(const QModelIndex &parent) const; + virtual int columnCount(const QModelIndex &parent) const; + virtual bool hasChildren(const QModelIndex &parent) const; + + virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const; + virtual QVariant data(const QModelIndex &index, int role) const; + +public slots: + void onLogMessage(const QDateTime dt, const log_level_t level, const QString msg); + +private: + QList _items; + + static QString logLevelText(log_level_t level); +}; diff --git a/cpp/core/MeasurementInterface.cpp b/cpp/core/MeasurementInterface.cpp new file mode 100644 index 0000000..58d7669 --- /dev/null +++ b/cpp/core/MeasurementInterface.cpp @@ -0,0 +1,215 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "MeasurementInterface.h" + +#include +#include +#include + + +MeasurementInterface::MeasurementInterface() + : _doConfigure(true), + _bitrate(500000), + _samplePoint(875), + _isCanFD(false), + _fdBitrate(4000000), + _fdSamplePoint(875), + + _isListenOnlyMode(false), + _isOneShotMode(false), + _isTripleSampling(false), + _doAutoRestart(false), + _autoRestartMs(100) +{ + +} + +bool MeasurementInterface::loadXML(Backend &backend, QDomElement &el) +{ + (void) backend; + + _doConfigure = el.attribute("configure", "0").toInt() != 0; + + _bitrate = el.attribute("bitrate", "500000").toInt(); + _samplePoint = el.attribute("sample-point", "875").toInt(); + _isCanFD = el.attribute("can-fd", "0").toInt() != 0; + _fdBitrate = el.attribute("bitrate-fd", "500000").toInt(); + _fdSamplePoint = el.attribute("sample-point-fd", "875").toInt(); + + _isListenOnlyMode = el.attribute("listen-only", "0").toInt() != 0; + _isOneShotMode = el.attribute("one-shot", "0").toInt() != 0; + _isTripleSampling = el.attribute("triple-sampling", "0").toInt() != 0; + _doAutoRestart = el.attribute("auto-restart", "0").toInt() != 0; + _autoRestartMs = el.attribute("auto-restart-time", "100").toInt(); + + return true; +} + +bool MeasurementInterface::saveXML(Backend &backend, QDomDocument &xml, QDomElement &root) +{ + (void) xml; + + root.setAttribute("type", "can"); + root.setAttribute("driver", backend.getDriverName(_canif)); + root.setAttribute("name", backend.getInterfaceName(_canif)); + + root.setAttribute("configure", _doConfigure ? 1 : 0); + + root.setAttribute("bitrate", _bitrate); + root.setAttribute("sample-point", _samplePoint); + root.setAttribute("can-fd", _isCanFD ? 1 : 0); + root.setAttribute("bitrate-fd", _fdBitrate); + root.setAttribute("sample-point-fd", _fdSamplePoint); + + root.setAttribute("listen-only", _isListenOnlyMode ? 1 : 0); + root.setAttribute("one-shot", _isOneShotMode ? 1 : 0); + root.setAttribute("triple-sampling", _isTripleSampling ? 1 : 0); + root.setAttribute("auto-restart", _doAutoRestart ? 1 : 0); + root.setAttribute("auto-restart-time", _autoRestartMs); + + return true; +} + +unsigned MeasurementInterface::bitrate() const +{ + return _bitrate; +} + +void MeasurementInterface::setBitrate(unsigned bitrate) +{ + _bitrate = bitrate; +} + +CanInterfaceId MeasurementInterface::canInterface() const +{ + return _canif; +} + +void MeasurementInterface::setCanInterface(CanInterfaceId canif) +{ + _canif = canif; +} + +void MeasurementInterface::cloneFrom(MeasurementInterface &origin) +{ + *this = origin; +} + +bool MeasurementInterface::doConfigure() const +{ + return _doConfigure; +} + +void MeasurementInterface::setDoConfigure(bool doConfigure) +{ + _doConfigure = doConfigure; +} + +bool MeasurementInterface::isListenOnlyMode() const +{ + return _isListenOnlyMode; +} + +void MeasurementInterface::setListenOnlyMode(bool isListenOnlyMode) +{ + _isListenOnlyMode = isListenOnlyMode; +} + +bool MeasurementInterface::isOneShotMode() const +{ + return _isOneShotMode; +} + +void MeasurementInterface::setOneShotMode(bool isOneShotMode) +{ + _isOneShotMode = isOneShotMode; +} + +bool MeasurementInterface::isTripleSampling() const +{ + return _isTripleSampling; +} + +void MeasurementInterface::setTripleSampling(bool isTripleSampling) +{ + _isTripleSampling = isTripleSampling; +} + +bool MeasurementInterface::isCanFD() const +{ + return _isCanFD; +} + +void MeasurementInterface::setCanFD(bool isCanFD) +{ + _isCanFD = isCanFD; +} + +int MeasurementInterface::samplePoint() const +{ + return _samplePoint; +} + +void MeasurementInterface::setSamplePoint(int samplePoint) +{ + _samplePoint = samplePoint; +} + +unsigned MeasurementInterface::fdBitrate() const +{ + return _fdBitrate; +} + +void MeasurementInterface::setFdBitrate(unsigned fdBitrate) +{ + _fdBitrate = fdBitrate; +} + +unsigned MeasurementInterface::fdSamplePoint() const +{ + return _fdSamplePoint; +} + +void MeasurementInterface::setFdSamplePoint(unsigned fdSamplePoint) +{ + _fdSamplePoint = fdSamplePoint; +} + +bool MeasurementInterface::doAutoRestart() const +{ + return _doAutoRestart; +} + +void MeasurementInterface::setAutoRestart(bool doAutoRestart) +{ + _doAutoRestart = doAutoRestart; +} + +int MeasurementInterface::autoRestartMs() const +{ + return _autoRestartMs; +} + +void MeasurementInterface::setAutoRestartMs(int autoRestartMs) +{ + _autoRestartMs = autoRestartMs; +} diff --git a/cpp/core/MeasurementInterface.h b/cpp/core/MeasurementInterface.h new file mode 100644 index 0000000..f73e522 --- /dev/null +++ b/cpp/core/MeasurementInterface.h @@ -0,0 +1,92 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include + +class Backend; + +class MeasurementInterface +{ +public: + MeasurementInterface(); + + CanInterfaceId canInterface() const; + void setCanInterface(CanInterfaceId canif); + + void cloneFrom(MeasurementInterface &origin); + bool loadXML(Backend &backend, QDomElement &el); + bool saveXML(Backend &backend, QDomDocument &xml, QDomElement &root); + + bool doConfigure() const; + void setDoConfigure(bool doConfigure); + + bool isListenOnlyMode() const; + void setListenOnlyMode(bool isListenOnlyMode); + + bool isOneShotMode() const; + void setOneShotMode(bool isOneShotMode); + + bool isTripleSampling() const; + void setTripleSampling(bool isTripleSampling); + + bool isCanFD() const; + void setCanFD(bool isCanFD); + + unsigned bitrate() const; + void setBitrate(unsigned bitrate); + + int samplePoint() const; + void setSamplePoint(int samplePoint); + + unsigned fdBitrate() const; + void setFdBitrate(unsigned fdBitrate); + + unsigned fdSamplePoint() const; + void setFdSamplePoint(unsigned fdSamplePoint); + + bool doAutoRestart() const; + void setAutoRestart(bool doAutoRestart); + + int autoRestartMs() const; + void setAutoRestartMs(int autoRestartMs); + +private: + CanInterfaceId _canif; + + bool _doConfigure; + + unsigned _bitrate; + unsigned _samplePoint; + + bool _isCanFD; + unsigned _fdBitrate; + unsigned _fdSamplePoint; + + bool _isListenOnlyMode; + bool _isOneShotMode; + bool _isTripleSampling; + bool _doAutoRestart; + int _autoRestartMs; +}; diff --git a/cpp/core/MeasurementNetwork.cpp b/cpp/core/MeasurementNetwork.cpp new file mode 100644 index 0000000..79c0844 --- /dev/null +++ b/cpp/core/MeasurementNetwork.cpp @@ -0,0 +1,159 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "MeasurementNetwork.h" +#include "MeasurementInterface.h" + +#include + + +MeasurementNetwork::MeasurementNetwork() +{ +} + +void MeasurementNetwork::cloneFrom(MeasurementNetwork &origin) +{ + _name = origin._name; + foreach (MeasurementInterface *omi, origin._interfaces) { + MeasurementInterface *mi = new MeasurementInterface(); + mi->cloneFrom(*omi); + _interfaces.append(mi); + } + _canDbs = origin._canDbs; +} + +void MeasurementNetwork::addInterface(MeasurementInterface *intf) +{ + _interfaces.append(intf); +} + +void MeasurementNetwork::removeInterface(MeasurementInterface *intf) +{ + _interfaces.removeAll(intf); +} + +QList MeasurementNetwork::interfaces() +{ + return _interfaces; +} + +MeasurementInterface *MeasurementNetwork::addCanInterface(CanInterfaceId canif) +{ + MeasurementInterface *mi = new MeasurementInterface(); + mi->setCanInterface(canif); + addInterface(mi); + return mi; +} + +CanInterfaceIdList MeasurementNetwork::getReferencedCanInterfaces() +{ + CanInterfaceIdList list; + foreach (MeasurementInterface *mi, _interfaces) { + list << mi->canInterface(); + } + return list; +} + +void MeasurementNetwork::addCanDb(QSharedPointer candb) +{ + _canDbs.append(candb); +} + +void MeasurementNetwork::reloadCanDbs(Backend *backend) +{ + foreach(pCanDb db, _canDbs) + { + db = backend->loadDbc(db->getPath()); + } +} + + +QString MeasurementNetwork::name() const +{ + return _name; +} + +void MeasurementNetwork::setName(const QString &name) +{ + _name = name; +} + +bool MeasurementNetwork::saveXML(Backend &backend, QDomDocument &xml, QDomElement &root) +{ + root.setAttribute("name", _name); + + QDomElement interfacesNode = xml.createElement("interfaces"); + foreach (MeasurementInterface *intf, _interfaces) { + QDomElement intfNode = xml.createElement("interface"); + if (!intf->saveXML(backend, xml, intfNode)) { + return false; + } + interfacesNode.appendChild(intfNode); + } + root.appendChild(interfacesNode); + + QDomElement candbsNode = xml.createElement("databases"); + foreach (pCanDb candb, _canDbs) { + QDomElement dbNode = xml.createElement("database"); + if (!candb->saveXML(backend, xml, dbNode)) { + return false; + } + candbsNode.appendChild(dbNode); + } + root.appendChild(candbsNode); + + + return true; +} + +bool MeasurementNetwork::loadXML(Backend &backend, QDomElement el) +{ + setName(el.attribute("name", "unnamed network")); + + QDomNodeList ifList = el.firstChildElement("interfaces").elementsByTagName("interface"); + for (int i=0; igetId()); + mi->loadXML(backend, elIntf); + } else { + log_error(QString("Could not find interface %1/%2, which is referenced in the workspace config file.").arg(driverName, deviceName)); + } + } + + + QDomNodeList dbList = el.firstChildElement("databases").elementsByTagName("database"); + for (int i=0; i + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include + +#include +#include +#include + +class Backend; +class MeasurementInterface; + +class MeasurementNetwork +{ +public: + MeasurementNetwork(); + void cloneFrom(MeasurementNetwork &origin); + + void addInterface(MeasurementInterface *intf); + void removeInterface(MeasurementInterface *intf); + QList interfaces(); + + MeasurementInterface *addCanInterface(CanInterfaceId canif); + CanInterfaceIdList getReferencedCanInterfaces(); + + void addCanDb(pCanDb candb); + void reloadCanDbs(Backend *backend); + QList _canDbs; + + QString name() const; + void setName(const QString &name); + + bool saveXML(Backend &backend, QDomDocument &xml, QDomElement &root); + bool loadXML(Backend &backend, QDomElement el); + +private: + QString _name; + QList _interfaces; +}; diff --git a/cpp/core/MeasurementSetup.cpp b/cpp/core/MeasurementSetup.cpp new file mode 100644 index 0000000..34f7807 --- /dev/null +++ b/cpp/core/MeasurementSetup.cpp @@ -0,0 +1,145 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "MeasurementSetup.h" + +#include +#include + +#include +#include +#include + +MeasurementSetup::MeasurementSetup(QObject *parent) + : QObject(parent) +{ +} + +MeasurementSetup::~MeasurementSetup() +{ + qDeleteAll(_networks); +} + +void MeasurementSetup::clear() +{ + qDeleteAll(_networks); + _networks.clear(); + emit onSetupChanged(); +} + +void MeasurementSetup::cloneFrom(MeasurementSetup &origin) +{ + clear(); + foreach (MeasurementNetwork *network, origin._networks) { + MeasurementNetwork *network_copy = new MeasurementNetwork(); + network_copy->cloneFrom(*network); + _networks.append(network_copy); + } + emit onSetupChanged(); +} + +bool MeasurementSetup::saveXML(Backend &backend, QDomDocument &xml, QDomElement &root) +{ + foreach (MeasurementNetwork *network, _networks) { + QDomElement networkNode = xml.createElement("network"); + if (!network->saveXML(backend, xml, networkNode)) { + return false; + } + root.appendChild(networkNode); + } + return true; +} + +bool MeasurementSetup::loadXML(Backend &backend, QDomElement &el) +{ + clear(); + + QDomNodeList networks = el.elementsByTagName("network"); + for (int i=0; iloadXML(backend, networks.item(i).toElement())) { + return false; + } + } + + emit onSetupChanged(); + return true; +} + + +MeasurementNetwork *MeasurementSetup::createNetwork() +{ + MeasurementNetwork *network = new MeasurementNetwork(); + _networks.append(network); + return network; +} + +void MeasurementSetup::removeNetwork(MeasurementNetwork *network) +{ + _networks.removeAll(network); +} + + +CanDbMessage *MeasurementSetup::findDbMessage(const CanMessage &msg) const +{ + CanDbMessage *result = 0; + + foreach (MeasurementNetwork *network, _networks) { + foreach (pCanDb db, network->_canDbs) { + result = db->getMessageById(msg.getRawId()); + if (result != 0) { + return result; + } + } + } + return result; +} + +QString MeasurementSetup::getInterfaceName(const CanInterface &interface) const +{ + return interface.getName(); +} + +int MeasurementSetup::countNetworks() const +{ + return _networks.length(); +} + +MeasurementNetwork *MeasurementSetup::getNetwork(int index) const +{ + return _networks.value(index); +} + +MeasurementNetwork *MeasurementSetup::getNetworkByName(QString name) const +{ + foreach (MeasurementNetwork *network, _networks) { + if (network->name() == name) { + return network; + } + } + return 0; +} + +QList MeasurementSetup::getNetworks() +{ + return _networks; +} + diff --git a/cpp/core/MeasurementSetup.h b/cpp/core/MeasurementSetup.h new file mode 100644 index 0000000..0fb190b --- /dev/null +++ b/cpp/core/MeasurementSetup.h @@ -0,0 +1,63 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include + +class Backend; +class MeasurementNetwork; +class CanTrace; +class CanMessage; +class CanInterface; +class CanDbMessage; + +class MeasurementSetup : public QObject +{ + Q_OBJECT + +public: + explicit MeasurementSetup(QObject *parent); + virtual ~MeasurementSetup(); + void clear(); + + CanDbMessage *findDbMessage(const CanMessage &msg) const; + QString getInterfaceName(const CanInterface &interface) const; + + int countNetworks() const; + MeasurementNetwork *getNetwork(int index) const; + MeasurementNetwork *getNetworkByName(QString name) const; + QList getNetworks(); + MeasurementNetwork *createNetwork(); + void removeNetwork(MeasurementNetwork *network); + + void cloneFrom(MeasurementSetup &origin); + bool saveXML(Backend &backend, QDomDocument &xml, QDomElement &root); + bool loadXML(Backend &backend, QDomElement &el); + +signals: + void onSetupChanged(); + +private: + QList _networks; +}; diff --git a/cpp/core/core.pri b/cpp/core/core.pri new file mode 100644 index 0000000..fa50c62 --- /dev/null +++ b/cpp/core/core.pri @@ -0,0 +1,30 @@ +SOURCES += \ + $$PWD/Backend.cpp \ + $$PWD/CanMessage.cpp \ + $$PWD/CanTrace.cpp \ + $$PWD/CanDbMessage.cpp \ + $$PWD/CanDb.cpp \ + $$PWD/CanDbNode.cpp \ + $$PWD/CanDbSignal.cpp \ + $$PWD/MeasurementSetup.cpp \ + $$PWD/MeasurementNetwork.cpp \ + $$PWD/MeasurementInterface.cpp \ + $$PWD/LogModel.cpp \ + $$PWD/ConfigurableWidget.cpp \ + $$PWD/Log.cpp + +HEADERS += \ + $$PWD/portable_endian.h \ + $$PWD/Backend.h \ + $$PWD/CanMessage.h \ + $$PWD/CanTrace.h \ + $$PWD/CanDbMessage.h \ + $$PWD/CanDb.h \ + $$PWD/CanDbNode.h \ + $$PWD/CanDbSignal.h \ + $$PWD/MeasurementSetup.h \ + $$PWD/MeasurementNetwork.h \ + $$PWD/MeasurementInterface.h \ + $$PWD/LogModel.h \ + $$PWD/ConfigurableWidget.h \ + $$PWD/Log.h diff --git a/cpp/core/portable_endian.h b/cpp/core/portable_endian.h new file mode 100644 index 0000000..2875b8d --- /dev/null +++ b/cpp/core/portable_endian.h @@ -0,0 +1,73 @@ +// "License": Public Domain +// I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like. +// In case there are jurisdictions that don't support putting things in the public domain you can also consider it to +// be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it +// an example on how to get the endian conversion functions on different platforms. + +#pragma once + +#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__) + +# define __WINDOWS__ + +#endif + +#if defined(__linux__) || defined(__CYGWIN__) + +# include + +#elif defined(__WINDOWS__) + +# include + +# if BYTE_ORDER == LITTLE_ENDIAN + +# define htobe16(x) htons(x) +# define htole16(x) (x) +# define be16toh(x) ntohs(x) +# define le16toh(x) (x) + +# define htobe32(x) htonl(x) +# define htole32(x) (x) +# define be32toh(x) ntohl(x) +# define le32toh(x) (x) + +# define htobe64(x) htonll(x) +# define htole64(x) (x) +# define be64toh(x) ntohll(x) +# define le64toh(x) (x) + +# elif BYTE_ORDER == BIG_ENDIAN + + /* that would be xbox 360 */ +# define htobe16(x) (x) +# define htole16(x) __builtin_bswap16(x) +# define be16toh(x) (x) +# define le16toh(x) __builtin_bswap16(x) + +# define htobe32(x) (x) +# define htole32(x) __builtin_bswap32(x) +# define be32toh(x) (x) +# define le32toh(x) __builtin_bswap32(x) + +# define htobe64(x) (x) +# define htole64(x) __builtin_bswap64(x) +# define be64toh(x) (x) +# define le64toh(x) __builtin_bswap64(x) + +# else + +# error byte order not supported + +# endif + +# define __BYTE_ORDER BYTE_ORDER +# define __BIG_ENDIAN BIG_ENDIAN +# define __LITTLE_ENDIAN LITTLE_ENDIAN +# define __PDP_ENDIAN PDP_ENDIAN + +#else + +# error platform not supported + +#endif diff --git a/cpp/driver/CANBlastDriver/CANBlastDriver.pri b/cpp/driver/CANBlastDriver/CANBlastDriver.pri new file mode 100644 index 0000000..7ce4018 --- /dev/null +++ b/cpp/driver/CANBlastDriver/CANBlastDriver.pri @@ -0,0 +1,13 @@ +CONFIG += c++11 +QT += network + +SOURCES += \ + $$PWD/CANBlasterDriver.cpp \ + $$PWD/CANBlasterInterface.cpp + +HEADERS += \ + $$PWD/CANBlasterDriver.h \ + $$PWD/CANBlasterInterface.h + +FORMS += + diff --git a/cpp/driver/CANBlastDriver/CANBlasterDriver.cpp b/cpp/driver/CANBlastDriver/CANBlasterDriver.cpp new file mode 100644 index 0000000..79ae195 --- /dev/null +++ b/cpp/driver/CANBlastDriver/CANBlasterDriver.cpp @@ -0,0 +1,140 @@ +/* + + Copyright (c) 2022 Ethan Zonca + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + + +#include "CANBlasterDriver.h" +#include "CANBlasterInterface.h" +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +CANBlasterDriver::CANBlasterDriver(Backend &backend) + : CanDriver(backend), + setupPage(new GenericCanSetupPage()) +{ + QObject::connect(&backend, SIGNAL(onSetupDialogCreated(SetupDialog&)), setupPage, SLOT(onSetupDialogCreated(SetupDialog&))); +} + +CANBlasterDriver::~CANBlasterDriver() { +} + +bool CANBlasterDriver::update() { + + deleteAllInterfaces(); + + // TODO: Listen for multicast packets for discovery of canblaster servers + QUdpSocket udpSocket; + QHostAddress groupAddress; + groupAddress.setAddress("239.255.43.21"); + udpSocket.bind(QHostAddress::AnyIPv4, 20000, QUdpSocket::ShareAddress); + udpSocket.joinMulticastGroup(groupAddress); + + // Record start time + struct timeval start_time; + gettimeofday(&start_time,NULL); + + fprintf(stderr, "CANblaster: start listen\r\n"); + + QList detected_servers; + + while(1) + { + while (udpSocket.hasPendingDatagrams()) + { + QNetworkDatagram res = udpSocket.receiveDatagram(1024); + if(res.isValid()) + { + QByteArray asd = res.data(); + qDebug() << asd; + QJsonDocument document = QJsonDocument::fromJson(res.data()); + QJsonObject rootObj = document.object(); + + + if(rootObj.length() == 2 && + rootObj["protocol"].toString() == "CANblaster" && + rootObj["version"].toInt() == 1) + { + if(!detected_servers.contains(res.senderAddress().toString())) + detected_servers.append(res.senderAddress().toString()); + } + else + { + fprintf(stderr, "Invalid CANblaster server. Protocol: %s Version: %d \r\n", rootObj["protocol"].toString().toStdString().c_str(), rootObj["version"].toInt()); + } + } + } + + struct timeval tv; + gettimeofday(&tv,NULL); + + // Iterate until timer expires + if(tv.tv_sec - start_time.tv_sec > 2) + break; + } + fprintf(stderr, "CANblaster: stop listen\r\n"); + + fprintf(stderr, "Found %d servers: \r\n", detected_servers.length()); + int interface_cnt = 0; + + for(QString server: detected_servers) + { + fprintf(stderr, " - %s\r\n", server.toStdString().c_str()); + createOrUpdateInterface(interface_cnt++, server, false); + } + + + return true; +} + +QString CANBlasterDriver::getName() { + return "CANblaster"; +} + + + +CANBlasterInterface *CANBlasterDriver::createOrUpdateInterface(int index, QString name, bool fd_support) { + + foreach (CanInterface *intf, getInterfaces()) { + CANBlasterInterface *scif = dynamic_cast(intf); + if (scif->getIfIndex() == index) { + scif->setName(name); + return scif; + } + } + + + CANBlasterInterface *scif = new CANBlasterInterface(this, index, name, fd_support); + addInterface(scif); + return scif; +} diff --git a/cpp/driver/CANBlastDriver/CANBlasterDriver.h b/cpp/driver/CANBlastDriver/CANBlasterDriver.h new file mode 100644 index 0000000..52de91d --- /dev/null +++ b/cpp/driver/CANBlastDriver/CANBlasterDriver.h @@ -0,0 +1,43 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include + +class CANBlasterInterface; +class SetupDialogInterfacePage; +class GenericCanSetupPage; + +class CANBlasterDriver: public CanDriver { +public: + CANBlasterDriver(Backend &backend); + virtual ~CANBlasterDriver(); + + virtual QString getName(); + virtual bool update(); + +private: + CANBlasterInterface *createOrUpdateInterface(int index, QString name, bool fd_support); + GenericCanSetupPage *setupPage; +}; diff --git a/cpp/driver/CANBlastDriver/CANBlasterInterface.cpp b/cpp/driver/CANBlastDriver/CANBlasterInterface.cpp new file mode 100644 index 0000000..397a658 --- /dev/null +++ b/cpp/driver/CANBlastDriver/CANBlasterInterface.cpp @@ -0,0 +1,314 @@ +/* + + Copyright (c) 2022 Ethan Zonca + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "CANBlasterInterface.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +CANBlasterInterface::CANBlasterInterface(CANBlasterDriver *driver, int index, QString name, bool fd_support) + : CanInterface((CanDriver *)driver), + _idx(index), + _isOpen(false), + _name(name), + _ts_mode(ts_mode_SIOCSHWTSTAMP), + _socket(NULL) +{ + // Set defaults + _settings.setBitrate(500000); + _settings.setSamplePoint(875); + + _config.supports_canfd = fd_support; + + // Record start time + gettimeofday(&_heartbeat_time,NULL); +} + +CANBlasterInterface::~CANBlasterInterface() { +} + +QString CANBlasterInterface::getDetailsStr() const { + if(_config.supports_canfd) + { + return "CANBlaster client with CANFD support"; + } + else + { + return "CANBlaster client with standard CAN support"; + } +} + +QString CANBlasterInterface::getName() const { + return _name; +} + +void CANBlasterInterface::setName(QString name) { + _name = name; +} + +QList CANBlasterInterface::getAvailableBitrates() +{ + QList retval; + QList bitrates({10000, 20000, 50000, 83333, 100000, 125000, 250000, 500000, 800000, 1000000}); + QList bitrates_fd({0, 2000000, 5000000}); + + QList samplePoints({875}); + + unsigned i=0; + foreach (unsigned br, bitrates) { + foreach(unsigned br_fd, bitrates_fd) { + foreach (unsigned sp, samplePoints) { + retval << CanTiming(i++, br, br_fd, sp); + } + } + } + + return retval; +} + + +void CANBlasterInterface::applyConfig(const MeasurementInterface &mi) +{ + // Save settings for port configuration + _settings = mi; +} + + + +bool CANBlasterInterface::supportsTimingConfiguration() +{ + return _config.supports_timing; +} + +bool CANBlasterInterface::supportsCanFD() +{ + return _config.supports_canfd; +} + +bool CANBlasterInterface::supportsTripleSampling() +{ + return false; +} + +unsigned CANBlasterInterface::getBitrate() +{ + return 0; +} + +uint32_t CANBlasterInterface::getCapabilities() +{ + uint32_t retval = + CanInterface::capability_config_os | + CanInterface::capability_listen_only | + CanInterface::capability_auto_restart; + + if (supportsCanFD()) { + retval |= CanInterface::capability_canfd; + } + + if (supportsTripleSampling()) { + retval |= CanInterface::capability_triple_sampling; + } + + return retval; +} + +bool CANBlasterInterface::updateStatistics() +{ + return false; +} + +uint32_t CANBlasterInterface::getState() +{ + if(_isOpen) + return state_ok; + else + return state_bus_off; +} + +int CANBlasterInterface::getNumRxFrames() +{ + return _status.rx_count; +} + +int CANBlasterInterface::getNumRxErrors() +{ + return _status.rx_errors; +} + +int CANBlasterInterface::getNumTxFrames() +{ + return _status.tx_count; +} + +int CANBlasterInterface::getNumTxErrors() +{ + return _status.tx_errors; +} + +int CANBlasterInterface::getNumRxOverruns() +{ + return _status.rx_overruns; +} + +int CANBlasterInterface::getNumTxDropped() +{ + return _status.tx_dropped; +} + +int CANBlasterInterface::getIfIndex() { + return _idx; +} + +const char *CANBlasterInterface::cname() +{ + return _name.toStdString().c_str(); +} + +void CANBlasterInterface::open() +{ + + // Start off with a fresh socket + if(_socket != NULL) + { + delete _socket; + } + _socket = new QUdpSocket(); + + if(_socket->bind(QHostAddress::AnyIPv4, 20001)) + { + _isOpen = true; + } + else + { + perror("CANBlaster Bind Failed!"); + _isOpen = false; + } +} + +void CANBlasterInterface::close() +{ + if(_socket != NULL && _socket->isOpen()) + { + _socket->close(); + } + _isOpen = false; +} + +bool CANBlasterInterface::isOpen() +{ + return _isOpen; +} + +void CANBlasterInterface::sendMessage(const CanMessage &msg) { + +} + +bool CANBlasterInterface::readMessage(QList &msglist, unsigned int timeout_ms) +{ + // Don't saturate the thread + QThread().usleep(250); + + // Record start time + struct timeval now; + gettimeofday(&now,NULL); + + if(now.tv_sec - _heartbeat_time.tv_sec > 1) + { + + _heartbeat_time.tv_sec = now.tv_sec; + + if(_isOpen) + { + QByteArray Data; + Data.append("Heartbeat"); + _socket->writeDatagram(Data, QHostAddress(getName()), 20002); + } + } + + // NOTE: This only works with standard CAN frames right now! + + // Process all pending datagrams + // TODO: Could switch to if from while but the caller expects 1 can frame + if (_isOpen && _socket->hasPendingDatagrams()) + { + can_frame frame; + QHostAddress address; + quint16 port; + int res = _socket->readDatagram((char*)&frame, sizeof(can_frame), &address, &port); + + // TODO: Read all bytes... to CANFD_MTU max +// if (nbytes == CANFD_MTU) { +// printf("got CAN FD frame with length %d\n", cfd.len); +// /* cfd.flags contains valid data */ +// } else if (nbytes == CAN_MTU) { +// printf("got Classical CAN frame with length %d\n", cfd.len); +// /* cfd.flags is undefined */ +// } else { +// fprintf(stderr, "read: invalid CAN(FD) frame\n"); +// return 1; +// } + + if(res > 0) + { + // Set timestamp to current time + struct timeval tv; + gettimeofday(&tv,NULL); + CanMessage msg; + msg.setTimestamp(tv); + + msg.setInterfaceId(getId()); + msg.setId(frame.can_id & CAN_ERR_MASK); + msg.setBRS(false); + msg.setErrorFrame(frame.can_id & CAN_ERR_FLAG); + msg.setExtended(frame.can_id & CAN_EFF_FLAG); + msg.setRTR(frame.can_id & CAN_RTR_FLAG); + msg.setLength(frame.len); + + for(int i=0; i. + +*/ + +#pragma once + +#include "../CanInterface.h" +#include +#include +#include + +class CANBlasterDriver; + +typedef struct { + bool supports_canfd; + bool supports_timing; + uint32_t state; + uint32_t base_freq; + uint32_t sample_point; + uint32_t ctrl_mode; + uint32_t restart_ms; +} can_config_t; + +typedef struct { + uint32_t can_state; + + uint64_t rx_count; + int rx_errors; + uint64_t rx_overruns; + + uint64_t tx_count; + int tx_errors; + uint64_t tx_dropped; +} can_status_t; + +class CANBlasterInterface: public CanInterface { +public: + CANBlasterInterface(CANBlasterDriver *driver, int index, QString name, bool fd_support); + virtual ~CANBlasterInterface(); + + QString getDetailsStr() const; + virtual QString getName() const; + void setName(QString name); + + virtual QList getAvailableBitrates(); + + virtual void applyConfig(const MeasurementInterface &mi); + + bool supportsTimingConfiguration(); + bool supportsCanFD(); + bool supportsTripleSampling(); + + virtual unsigned getBitrate(); + virtual uint32_t getCapabilities(); + + + virtual void open(); + virtual void close(); + virtual bool isOpen(); + + virtual void sendMessage(const CanMessage &msg); + virtual bool readMessage(QList &msglist, unsigned int timeout_ms); + + virtual bool updateStatistics(); + virtual uint32_t getState(); + virtual int getNumRxFrames(); + virtual int getNumRxErrors(); + virtual int getNumRxOverruns(); + + virtual int getNumTxFrames(); + virtual int getNumTxErrors(); + virtual int getNumTxDropped(); + + + int getIfIndex(); + +private: + typedef enum { + ts_mode_SIOCSHWTSTAMP, + ts_mode_SIOCGSTAMPNS, + ts_mode_SIOCGSTAMP + } ts_mode_t; + + int _idx; + bool _isOpen; + QString _name; + + MeasurementInterface _settings; + + can_config_t _config; + can_status_t _status; + ts_mode_t _ts_mode; + + struct timeval _heartbeat_time; + QUdpSocket* _socket; + const char *cname(); + +}; + + +/* Duplicated from Linux can.h for compilation on Windows. Not a great solution. */ + + +/* special address description flags for the CAN_ID */ +#define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */ +#define CAN_RTR_FLAG 0x40000000U /* remote transmission request */ +#define CAN_ERR_FLAG 0x20000000U /* error message frame */ + +/* valid bits in CAN ID for frame formats */ +#define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */ +#define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */ +#define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */ + +/* + * Controller Area Network Identifier structure + * + * bit 0-28 : CAN identifier (11/29 bit) + * bit 29 : error message frame flag (0 = data frame, 1 = error message) + * bit 30 : remote transmission request flag (1 = rtr frame) + * bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit) + */ +typedef uint32_t canid_t; + +#define CAN_SFF_ID_BITS 11 +#define CAN_EFF_ID_BITS 29 + +/* + * Controller Area Network Error Message Frame Mask structure + * + * bit 0-28 : error class mask (see include/uapi/linux/can/error.h) + * bit 29-31 : set to zero + */ +typedef uint32_t can_err_mask_t; + +/* CAN payload length and DLC definitions according to ISO 11898-1 */ +#define CAN_MAX_DLC 8 +#define CAN_MAX_RAW_DLC 15 +#define CAN_MAX_DLEN 8 + +/* CAN FD payload length and DLC definitions according to ISO 11898-7 */ +#define CANFD_MAX_DLC 15 +#define CANFD_MAX_DLEN 64 + +/** + * struct can_frame - Classical CAN frame structure (aka CAN 2.0B) + * @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition + * @len: CAN frame payload length in byte (0 .. 8) + * @can_dlc: deprecated name for CAN frame payload length in byte (0 .. 8) + * @__pad: padding + * @__res0: reserved / padding + * @len8_dlc: optional DLC value (9 .. 15) at 8 byte payload length + * len8_dlc contains values from 9 .. 15 when the payload length is + * 8 bytes but the DLC value (see ISO 11898-1) is greater then 8. + * CAN_CTRLMODE_CC_LEN8_DLC flag has to be enabled in CAN driver. + * @data: CAN frame payload (up to 8 byte) + */ +struct can_frame { + canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ + union { + /* CAN frame payload length in byte (0 .. CAN_MAX_DLEN) + * was previously named can_dlc so we need to carry that + * name for legacy support + */ + uint8_t len; + uint8_t can_dlc; /* deprecated */ + } __attribute__((packed)); /* disable padding added in some ABIs */ + uint8_t __pad; /* padding */ + uint8_t __res0; /* reserved / padding */ + uint8_t len8_dlc; /* optional DLC for 8 byte payload length (9 .. 15) */ + uint8_t data[CAN_MAX_DLEN] __attribute__((aligned(8))); +}; + +/* + * defined bits for canfd_frame.flags + * + * The use of struct canfd_frame implies the FD Frame (FDF) bit to + * be set in the CAN frame bitstream on the wire. The FDF bit switch turns + * the CAN controllers bitstream processor into the CAN FD mode which creates + * two new options within the CAN FD frame specification: + * + * Bit Rate Switch - to indicate a second bitrate is/was used for the payload + * Error State Indicator - represents the error state of the transmitting node + * + * As the CANFD_ESI bit is internally generated by the transmitting CAN + * controller only the CANFD_BRS bit is relevant for real CAN controllers when + * building a CAN FD frame for transmission. Setting the CANFD_ESI bit can make + * sense for virtual CAN interfaces to test applications with echoed frames. + * + * The struct can_frame and struct canfd_frame intentionally share the same + * layout to be able to write CAN frame content into a CAN FD frame structure. + * When this is done the former differentiation via CAN_MTU / CANFD_MTU gets + * lost. CANFD_FDF allows programmers to mark CAN FD frames in the case of + * using struct canfd_frame for mixed CAN / CAN FD content (dual use). + * N.B. the Kernel APIs do NOT provide mixed CAN / CAN FD content inside of + * struct canfd_frame therefore the CANFD_FDF flag is disregarded by Linux. + */ +#define CANFD_BRS 0x01 /* bit rate switch (second bitrate for payload data) */ +#define CANFD_ESI 0x02 /* error state indicator of the transmitting node */ +#define CANFD_FDF 0x04 /* mark CAN FD for dual use of struct canfd_frame */ + +/** + * struct canfd_frame - CAN flexible data rate frame structure + * @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition + * @len: frame payload length in byte (0 .. CANFD_MAX_DLEN) + * @flags: additional flags for CAN FD + * @__res0: reserved / padding + * @__res1: reserved / padding + * @data: CAN FD frame payload (up to CANFD_MAX_DLEN byte) + */ +struct canfd_frame { + canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ + uint8_t len; /* frame payload length in byte */ + uint8_t flags; /* additional flags for CAN FD */ + uint8_t __res0; /* reserved / padding */ + uint8_t __res1; /* reserved / padding */ + uint8_t data[CANFD_MAX_DLEN] __attribute__((aligned(8))); +}; + +#define CAN_MTU (sizeof(struct can_frame)) +#define CANFD_MTU (sizeof(struct canfd_frame)) diff --git a/cpp/driver/CanDriver.cpp b/cpp/driver/CanDriver.cpp new file mode 100644 index 0000000..b3dbf07 --- /dev/null +++ b/cpp/driver/CanDriver.cpp @@ -0,0 +1,107 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "CanDriver.h" +#include +#include + +CanDriver::CanDriver(Backend &backend) + : _backend(backend) +{ + setId(0); +} + +CanDriver::~CanDriver() { + // TODO Auto-generated destructor stub +} + +Backend &CanDriver::backend() const +{ + return _backend; +} + +void CanDriver::init(int id) +{ + setId(id); +} + +void CanDriver::setId(int id) +{ + _id = id; +} + +int CanDriver::id() +{ + return _id; +} + +QList CanDriver::getInterfaces() const +{ + return _interfaces; +} + +CanInterfaceIdList CanDriver::getInterfaceIds() const +{ + CanInterfaceIdList retval; + foreach (CanInterface *intf, _interfaces) { + retval.push_back(intf->getId()); + } + return retval; +} + +CanInterface *CanDriver::getInterfaceById(CanInterfaceId id) +{ + return _interfaces.value(id & 0xFF); +} + +CanInterfaceId CanDriver::addInterface(CanInterface *intf) +{ + intf->setId((id()<<8) | _interfaces.size()); + _interfaces.push_back(intf); + return intf->getId(); +} + +void CanDriver::deleteInterface(CanInterface *intf) +{ + delete intf; + _interfaces.removeOne(intf); +} + +void CanDriver::deleteAllInterfaces() +{ + for(CanInterface* interface: _interfaces) + { + delete interface; + _interfaces.removeOne(interface); + } +} + + + +CanInterface *CanDriver::getInterfaceByName(QString ifName) +{ + foreach (CanInterface *intf, _interfaces) { + if (intf->getName() == ifName) { + return intf; + } + } + return 0; +} diff --git a/cpp/driver/CanDriver.h b/cpp/driver/CanDriver.h new file mode 100644 index 0000000..9c07628 --- /dev/null +++ b/cpp/driver/CanDriver.h @@ -0,0 +1,63 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include + +class Backend; +class CanInterface; + +typedef uint16_t CanInterfaceId; +typedef QList CanInterfaceIdList; + +class CanDriver { +public: + CanDriver(Backend &backend); + virtual ~CanDriver(); + + Backend &backend() const; + + void init(int id); + + int id(); + + virtual QString getName() = 0; + virtual bool update() = 0; + + CanInterfaceIdList getInterfaceIds() const; + QList getInterfaces() const; + CanInterface *getInterfaceById(CanInterfaceId id); + CanInterfaceId addInterface(CanInterface *intf); + void deleteInterface(CanInterface *intf); + void deleteAllInterfaces(); + + CanInterface *getInterfaceByName(QString ifName); + +private: + Backend &_backend; + int _id; + QList _interfaces; + + void setId(int id); +}; diff --git a/cpp/driver/CanInterface.cpp b/cpp/driver/CanInterface.cpp new file mode 100644 index 0000000..9cbc402 --- /dev/null +++ b/cpp/driver/CanInterface.cpp @@ -0,0 +1,101 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "CanInterface.h" + +#include + +CanInterface::CanInterface(CanDriver *driver) + : _id(-1), _driver(driver) +{ +} + +CanInterface::~CanInterface() { +} + +CanDriver* CanInterface::getDriver() { + return _driver; +} + +QString CanInterface::getDetailsStr() const +{ + return ""; +} + +uint32_t CanInterface::getCapabilities() +{ + return 0; +} + +QList CanInterface::getAvailableBitrates() +{ + QList retval; + retval << CanTiming(0, 10000, 0, 875) \ + << CanTiming(1, 20000, 0, 875) \ + << CanTiming(2, 50000, 0, 875) \ + << CanTiming(3, 83333, 0, 875) \ + << CanTiming(4, 100000, 0, 875) \ + << CanTiming(5, 125000, 0, 875) \ + << CanTiming(6, 250000, 0, 875) \ + << CanTiming(7, 500000, 0, 875) \ + << CanTiming(8, 800000, 0, 875) \ + << CanTiming(9, 1000000, 0, 875); + return retval; +} + +void CanInterface::open() { +} + +void CanInterface::close() { +} + +bool CanInterface::isOpen() +{ + return false; +} + +bool CanInterface::updateStatistics() +{ + return false; +} + +QString CanInterface::getStateText() +{ + switch (getState()) { + case state_ok: return "ok"; + case state_warning: return "warning"; + case state_passive: return "error passive"; + case state_bus_off: return "bus off"; + case state_stopped: return "stopped"; + case state_unknown: return "unknown"; + default: return ""; + } +} + +CanInterfaceId CanInterface::getId() const +{ + return _id; +} + +void CanInterface::setId(CanInterfaceId id) +{ + _id = id; +} diff --git a/cpp/driver/CanInterface.h b/cpp/driver/CanInterface.h new file mode 100644 index 0000000..8993e55 --- /dev/null +++ b/cpp/driver/CanInterface.h @@ -0,0 +1,92 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include "CanDriver.h" +#include "CanTiming.h" +#include + +class CanMessage; +class MeasurementInterface; + +class CanInterface: public QObject { +public: + enum { + state_ok, + state_warning, + state_passive, + state_bus_off, + state_stopped, + state_unknown + }; + + enum { + capability_canfd = 0x01, + capability_listen_only = 0x02, + capability_triple_sampling = 0x04, + capability_one_shot = 0x08, + capability_auto_restart = 0x10, + capability_config_os = 0x20 + }; + +public: + CanInterface(CanDriver *driver); + virtual ~CanInterface(); + virtual CanDriver *getDriver(); + virtual QString getName() const = 0; + virtual QString getDetailsStr() const; + + virtual void applyConfig(const MeasurementInterface &mi) = 0; + + virtual unsigned getBitrate() = 0; + + virtual uint32_t getCapabilities(); + virtual QList getAvailableBitrates(); + + virtual void open(); + virtual void close(); + + virtual bool isOpen(); + + virtual void sendMessage(const CanMessage &msg) = 0; + virtual bool readMessage(QList &msglist, unsigned int timeout_ms) = 0; + + virtual bool updateStatistics(); + virtual uint32_t getState() = 0; + virtual int getNumRxFrames() = 0; + virtual int getNumRxErrors() = 0; + virtual int getNumTxFrames() = 0; + virtual int getNumTxErrors() = 0; + virtual int getNumRxOverruns() = 0; + virtual int getNumTxDropped() = 0; + + QString getStateText(); + + CanInterfaceId getId() const; + void setId(CanInterfaceId id); + +private: + CanInterfaceId _id; + CanDriver *_driver; +}; diff --git a/cpp/driver/CanListener.cpp b/cpp/driver/CanListener.cpp new file mode 100644 index 0000000..8c18f27 --- /dev/null +++ b/cpp/driver/CanListener.cpp @@ -0,0 +1,97 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "CanListener.h" + +#include + +#include +#include +#include +#include "CanInterface.h" + +CanListener::CanListener(QObject *parent, Backend &backend, CanInterface &intf) + : QObject(parent), + _backend(backend), + _intf(intf), + _shouldBeRunning(true), + _openComplete(false) +{ + _thread = new QThread(); +} + +CanListener::~CanListener() +{ + delete _thread; +} + +CanInterfaceId CanListener::getInterfaceId() +{ + return _intf.getId(); +} + +CanInterface &CanListener::getInterface() +{ + return _intf; +} + +void CanListener::run() +{ + // Note: open and close done from run() so all operations take place in the same thread + //CanMessage msg; + QList rxMessages; + CanTrace *trace = _backend.getTrace(); + _intf.open(); + _openComplete = true; + while (_shouldBeRunning) { + if (_intf.readMessage(rxMessages, 1000)) { + for(CanMessage msg: rxMessages) + { + trace->enqueueMessage(msg, false); + } + rxMessages.clear(); + } + } + _intf.close(); + _thread->quit(); +} + +void CanListener::startThread() +{ + moveToThread(_thread); + connect(_thread, SIGNAL(started()), this, SLOT(run())); + _thread->start(); + + // Wait for interface to be open before returning so that beginMeasurement is emitted after interface open + while(!_openComplete) + QThread().usleep(250); +} + +void CanListener::requestStop() +{ + _shouldBeRunning = false; +} + +void CanListener::waitFinish() +{ + requestStop(); + _thread->wait(); +} diff --git a/cpp/driver/CanListener.h b/cpp/driver/CanListener.h new file mode 100644 index 0000000..bbad80f --- /dev/null +++ b/cpp/driver/CanListener.h @@ -0,0 +1,60 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include + +class QThread; +class CanMessage; +class Backend; + +class CanListener : public QObject +{ + Q_OBJECT + +public: + explicit CanListener(QObject *parent, Backend &backend, CanInterface &intf); + virtual ~CanListener(); + + CanInterfaceId getInterfaceId(); + CanInterface &getInterface(); + +signals: + void messageReceived(const CanMessage &msg); + +public slots: + void run(); + + void startThread(); + void requestStop(); + void waitFinish(); + +private: + Backend &_backend; + CanInterface &_intf; + bool _shouldBeRunning; + bool _openComplete; + QThread *_thread; + +}; diff --git a/cpp/driver/CanTiming.cpp b/cpp/driver/CanTiming.cpp new file mode 100644 index 0000000..aaf4f2b --- /dev/null +++ b/cpp/driver/CanTiming.cpp @@ -0,0 +1,49 @@ +#include "CanTiming.h" + +CanTiming::CanTiming(unsigned id, unsigned bitrate, unsigned bitrate_fd, unsigned samplePoint) + : _id(id), + _bitrate(bitrate), + _bitrate_fd(bitrate_fd), + _samplePoint(samplePoint) +{ +} + +unsigned CanTiming::getId() +{ + return _id; +} + +unsigned CanTiming::getBitrate() +{ + return _bitrate; +} + +unsigned CanTiming::getBitrateFD() +{ + return _bitrate_fd; +} + +bool CanTiming::isCanFD() +{ + return _bitrate_fd != 0; +} + +unsigned CanTiming::getSamplePoint() +{ + return _samplePoint; +} + +QString CanTiming::getSamplePointStr(unsigned samplePoint) +{ + return QString::number(samplePoint/10.0, 'f', 1).append("%"); +} + +QString CanTiming::getTitle() +{ + QString format = isCanFD() ? "%1/%3(FD) kBit/s, sample point %2" : "%1 kBit/s, sample point %2"; + return format.arg( + QString::number(getBitrate()/1000, 'f', 2), + getSamplePointStr(getSamplePoint()), + QString::number(getBitrateFD()/1000, 'f', 2) + ); +} diff --git a/cpp/driver/CanTiming.h b/cpp/driver/CanTiming.h new file mode 100644 index 0000000..b186cc9 --- /dev/null +++ b/cpp/driver/CanTiming.h @@ -0,0 +1,37 @@ +#ifndef CANTIMING_H +#define CANTIMING_H + +#include + +class CanTiming +{ +public: + /*! + * \brief CanTiming generic CAN timing description + * \param id internal id to identify this timing for the corresponding CAN driver + * \param bitrate bitrate in bits per second, e.g. 500000 + * \param bitrate_fd bitrate for the FD part, or zero this is not a CanFD timing + * \param samplePoint sample point, e.g. 0.875 + */ + CanTiming(unsigned id, unsigned bitrate, unsigned bitrate_fd, unsigned samplePoint); + unsigned getId(); + unsigned getBitrate(); + unsigned getBitrateFD(); + bool isCanFD(); + unsigned getSamplePoint(); + static QString getSamplePointStr(unsigned samplePoint); + + /*! + * \brief getTitle get a brief one-line description of this timing + * \return description, e.g. "500kBit/s, sample point 87.5%" + */ + QString getTitle(); + +private: + unsigned _id; + unsigned _bitrate; + unsigned _bitrate_fd; + unsigned _samplePoint; +}; + +#endif // CANTIMING_H diff --git a/cpp/driver/CandleApiDriver/CandleApiDriver.cpp b/cpp/driver/CandleApiDriver/CandleApiDriver.cpp new file mode 100644 index 0000000..75dd352 --- /dev/null +++ b/cpp/driver/CandleApiDriver/CandleApiDriver.cpp @@ -0,0 +1,80 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "CandleApiDriver.h" +#include "api/candle.h" + +#include "CandleApiInterface.h" +#include + +CandleApiDriver::CandleApiDriver(Backend &backend) + : CanDriver(backend), + setupPage(new GenericCanSetupPage(0)) +{ + QObject::connect(&backend, SIGNAL(onSetupDialogCreated(SetupDialog&)), setupPage, SLOT(onSetupDialogCreated(SetupDialog&))); +} + +QString CandleApiDriver::getName() +{ + return "CandleAPI"; +} + +bool CandleApiDriver::update() +{ + deleteAllInterfaces(); + + candle_list_handle clist; + uint8_t num_interfaces; + candle_handle dev; + + if (candle_list_scan(&clist)) { + if (candle_list_length(clist, &num_interfaces)) { + for (uint8_t i=0; iupdate(dev); + } + + } + + } + } + candle_list_free(clist); + } + + return true; +} + +CandleApiInterface *CandleApiDriver::findInterface(candle_handle dev) +{ + foreach (CanInterface *intf, getInterfaces()) { + CandleApiInterface *cif = dynamic_cast(intf); + if (cif->getPath() == std::wstring(candle_dev_get_path(dev))) { + return cif; + } + } + return NULL; +} diff --git a/cpp/driver/CandleApiDriver/CandleApiDriver.h b/cpp/driver/CandleApiDriver/CandleApiDriver.h new file mode 100644 index 0000000..9805dc1 --- /dev/null +++ b/cpp/driver/CandleApiDriver/CandleApiDriver.h @@ -0,0 +1,44 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include "api/candle.h" + +class CandleApiInterface; +class GenericCanSetupPage; + +class CandleApiDriver : public CanDriver +{ +public: + CandleApiDriver(Backend &backend); + + virtual QString getName(); + virtual bool update(); + +private: + GenericCanSetupPage *setupPage; + CandleApiInterface *findInterface(candle_handle dev); + +}; + diff --git a/cpp/driver/CandleApiDriver/CandleApiDriver.pri b/cpp/driver/CandleApiDriver/CandleApiDriver.pri new file mode 100644 index 0000000..971fdcc --- /dev/null +++ b/cpp/driver/CandleApiDriver/CandleApiDriver.pri @@ -0,0 +1,24 @@ +QMAKE_CFLAGS += -std=gnu11 + +SOURCES += \ + $$PWD/CandleApiDriver.cpp \ + $$PWD/CandleApiInterface.cpp \ + $$PWD/api/candle.c \ + $$PWD/api/candle_ctrl_req.c \ + $$PWD/CandleApiTiming.cpp + +#win32:LIBS += -L$$PWD/api/ -lcandle_api +win32: LIBS += -lSetupApi +win32: LIBS += -lOle32 +win32: LIBS += -lwinusb + +HEADERS += \ + $$PWD/api/candle.h \ + $$PWD/CandleApiDriver.h \ + $$PWD/CandleApiInterface.h \ + $$PWD/api/candle_ctrl_req.h \ + $$PWD/api/candle_defs.h \ + $$PWD/api/ch_9.h \ + $$PWD/CandleApiTiming.h + +FORMS += diff --git a/cpp/driver/CandleApiDriver/CandleApiInterface.cpp b/cpp/driver/CandleApiDriver/CandleApiInterface.cpp new file mode 100644 index 0000000..95419c3 --- /dev/null +++ b/cpp/driver/CandleApiDriver/CandleApiInterface.cpp @@ -0,0 +1,391 @@ +#include "CandleApiDriver.h" +#include "CandleApiInterface.h" + +CandleApiInterface::CandleApiInterface(CandleApiDriver *driver, candle_handle handle) + : CanInterface(driver), + _hostOffsetStart(0), + _deviceTicksStart(0), + _handle(handle), + _backend(driver->backend()), + _numRx(0), + _numTx(0), + _numTxErr(0) +{ + _settings.setBitrate(500000); + _settings.setSamplePoint(875); + + + + // Timings for 170MHz processors (CANable 2.0) + // Tseg1: 2..256 Tseg2: 2..128 sjw: 1..128 brp: 1..512 + // Note: as expressed below, Tseg1 does not include 1 count for prop phase + _timings + << CandleApiTiming(170000000, 10000, 875, 68, 217, 31) + << CandleApiTiming(170000000, 20000, 875, 34, 217, 31) + << CandleApiTiming(170000000, 50000, 875, 17, 173, 25) + << CandleApiTiming(170000000, 83333, 875, 8, 221, 32) + << CandleApiTiming(170000000, 100000, 875, 10, 147, 21) + << CandleApiTiming(170000000, 125000, 875, 8, 147, 21) + << CandleApiTiming(170000000, 250000, 875, 4, 147, 21) + << CandleApiTiming(170000000, 500000, 875, 2, 147, 21) + << CandleApiTiming(170000000, 1000000, 875, 1, 147, 21); + + + // Timings for 48MHz processors (CANable 0.X) + _timings + // sample point: 50.0% + << CandleApiTiming(48000000, 10000, 500, 300, 6, 8) + << CandleApiTiming(48000000, 20000, 500, 150, 6, 8) + << CandleApiTiming(48000000, 50000, 500, 60, 6, 8) + << CandleApiTiming(48000000, 83333, 500, 36, 6, 8) + << CandleApiTiming(48000000, 100000, 500, 30, 6, 8) + << CandleApiTiming(48000000, 125000, 500, 24, 6, 8) + << CandleApiTiming(48000000, 250000, 500, 12, 6, 8) + << CandleApiTiming(48000000, 500000, 500, 6, 6, 8) + << CandleApiTiming(48000000, 800000, 500, 3, 8, 9) + << CandleApiTiming(48000000, 1000000, 500, 3, 6, 8) + + // sample point: 62.5% + << CandleApiTiming(48000000, 10000, 625, 300, 8, 6) + << CandleApiTiming(48000000, 20000, 625, 150, 8, 6) + << CandleApiTiming(48000000, 50000, 625, 60, 8, 6) + << CandleApiTiming(48000000, 83333, 625, 36, 8, 6) + << CandleApiTiming(48000000, 100000, 625, 30, 8, 6) + << CandleApiTiming(48000000, 125000, 625, 24, 8, 6) + << CandleApiTiming(48000000, 250000, 625, 12, 8, 6) + << CandleApiTiming(48000000, 500000, 625, 6, 8, 6) + << CandleApiTiming(48000000, 800000, 600, 4, 7, 6) + << CandleApiTiming(48000000, 1000000, 625, 3, 8, 6) + + // sample point: 75.0% + << CandleApiTiming(48000000, 10000, 750, 300, 10, 4) + << CandleApiTiming(48000000, 20000, 750, 150, 10, 4) + << CandleApiTiming(48000000, 50000, 750, 60, 10, 4) + << CandleApiTiming(48000000, 83333, 750, 36, 10, 4) + << CandleApiTiming(48000000, 100000, 750, 30, 10, 4) + << CandleApiTiming(48000000, 125000, 750, 24, 10, 4) + << CandleApiTiming(48000000, 250000, 750, 12, 10, 4) + << CandleApiTiming(48000000, 500000, 750, 6, 10, 4) + << CandleApiTiming(48000000, 800000, 750, 3, 13, 5) + << CandleApiTiming(48000000, 1000000, 750, 3, 10, 4) + + // sample point: 87.5% + << CandleApiTiming(48000000, 10000, 875, 300, 12, 2) + << CandleApiTiming(48000000, 20000, 875, 150, 12, 2) + << CandleApiTiming(48000000, 50000, 875, 60, 12, 2) + << CandleApiTiming(48000000, 83333, 875, 36, 12, 2) + << CandleApiTiming(48000000, 100000, 875, 30, 12, 2) + << CandleApiTiming(48000000, 125000, 875, 24, 12, 2) + << CandleApiTiming(48000000, 250000, 875, 12, 12, 2) + << CandleApiTiming(48000000, 500000, 875, 6, 12, 2) + << CandleApiTiming(48000000, 800000, 867, 4, 11, 2) + << CandleApiTiming(48000000, 1000000, 875, 3, 12, 2); + + + _timings + // sample point: 50.0% + << CandleApiTiming(16000000, 10000, 520, 64, 11, 12) + << CandleApiTiming(16000000, 20000, 500, 50, 6, 8) + << CandleApiTiming(16000000, 50000, 500, 20, 6, 8) + << CandleApiTiming(16000000, 83333, 500, 12, 6, 8) + << CandleApiTiming(16000000, 100000, 500, 10, 6, 8) + << CandleApiTiming(16000000, 125000, 500, 8, 6, 8) + << CandleApiTiming(16000000, 250000, 500, 4, 6, 8) + << CandleApiTiming(16000000, 500000, 500, 2, 6, 8) + << CandleApiTiming(16000000, 800000, 500, 1, 8, 10) + << CandleApiTiming(16000000, 1000000, 500, 1, 6, 8) + + // sample point: 62.5% + << CandleApiTiming(16000000, 10000, 625, 64, 14, 9) + << CandleApiTiming(16000000, 20000, 625, 50, 8, 6) + << CandleApiTiming(16000000, 50000, 625, 20, 8, 6) + << CandleApiTiming(16000000, 83333, 625, 12, 8, 6) + << CandleApiTiming(16000000, 100000, 625, 10, 8, 6) + << CandleApiTiming(16000000, 125000, 625, 8, 8, 6) + << CandleApiTiming(16000000, 250000, 625, 4, 8, 6) + << CandleApiTiming(16000000, 500000, 625, 2, 8, 6) + << CandleApiTiming(16000000, 800000, 625, 1, 11, 7) + << CandleApiTiming(16000000, 1000000, 625, 1, 8, 6) + + // sample point: 75.0% + << CandleApiTiming(16000000, 20000, 750, 50, 10, 4) + << CandleApiTiming(16000000, 50000, 750, 20, 10, 4) + << CandleApiTiming(16000000, 83333, 750, 12, 10, 4) + << CandleApiTiming(16000000, 100000, 750, 10, 10, 4) + << CandleApiTiming(16000000, 125000, 750, 8, 10, 4) + << CandleApiTiming(16000000, 250000, 750, 4, 10, 4) + << CandleApiTiming(16000000, 500000, 750, 2, 10, 4) + << CandleApiTiming(16000000, 800000, 750, 1, 13, 5) + << CandleApiTiming(16000000, 1000000, 750, 1, 10, 4) + + // sample point: 87.5% + << CandleApiTiming(16000000, 20000, 875, 50, 12, 2) + << CandleApiTiming(16000000, 50000, 875, 20, 12, 2) + << CandleApiTiming(16000000, 83333, 875, 12, 12, 2) + << CandleApiTiming(16000000, 100000, 875, 10, 12, 2) + << CandleApiTiming(16000000, 125000, 875, 8, 12, 2) + << CandleApiTiming(16000000, 250000, 875, 4, 12, 2) + << CandleApiTiming(16000000, 500000, 875, 2, 12, 2) + << CandleApiTiming(16000000, 800000, 900, 2, 7, 1) + << CandleApiTiming(16000000, 1000000, 875, 1, 12, 2); +} + +CandleApiInterface::~CandleApiInterface() +{ + +} + +QString CandleApiInterface::getName() const +{ + return "candle" + QString::number(getId() & 0xFF); +} + +QString CandleApiInterface::getDetailsStr() const +{ + return QString::fromStdWString(getPath()); +} + +void CandleApiInterface::applyConfig(const MeasurementInterface &mi) +{ + _settings = mi; +} + +unsigned CandleApiInterface::getBitrate() +{ + return _settings.bitrate(); +} + +uint32_t CandleApiInterface::getCapabilities() +{ + candle_capability_t caps; + + if (candle_channel_get_capabilities(_handle, 0, &caps)) { + + uint32_t retval = 0; + + if (caps.feature & CANDLE_MODE_LISTEN_ONLY) { + retval |= CanInterface::capability_listen_only; + } + + if (caps.feature & CANDLE_MODE_ONE_SHOT) { + retval |= CanInterface::capability_one_shot; + } + + if (caps.feature & CANDLE_MODE_TRIPLE_SAMPLE) { + retval |= CanInterface::capability_triple_sampling; + } + + return retval; + + } else { + return 0; + } +} + +QList CandleApiInterface::getAvailableBitrates() +{ + QList retval; + + candle_capability_t caps; + if (candle_channel_get_capabilities(_handle, 0, &caps)) { + int i = 0; + foreach (const CandleApiTiming t, _timings) { + if (t.getBaseClk() == caps.fclk_can) { + retval << CanTiming(i++, t.getBitrate(), 0, t.getSamplePoint()); + } + } + } + + return retval; +} + +bool CandleApiInterface::setBitTiming(uint32_t bitrate, uint32_t samplePoint) +{ + candle_capability_t caps; + if (!candle_channel_get_capabilities(_handle, 0, &caps)) { + return false; + } + + foreach (const CandleApiTiming t, _timings) { + if ( (t.getBaseClk() == caps.fclk_can) + && (t.getBitrate()==bitrate) + && (t.getSamplePoint()==samplePoint) ) + { + candle_bittiming_t timing = t.getTiming(); + return candle_channel_set_timing(_handle, 0, &timing); + } + } + + // no valid timing found + return false; +} + +void CandleApiInterface::open() +{ + if (!candle_dev_open(_handle)) { + // TODO what? + _isOpen = false; + return; + } + + if (!setBitTiming(_settings.bitrate(), _settings.samplePoint())) { + // TODO what? + _isOpen = false; + return; + } + + uint32_t flags = 0; + if (_settings.isListenOnlyMode()) { + flags |= CANDLE_MODE_LISTEN_ONLY; + } + if (_settings.isOneShotMode()) { + flags |= CANDLE_MODE_ONE_SHOT; + } + if (_settings.isTripleSampling()) { + flags |= CANDLE_MODE_TRIPLE_SAMPLE; + } + + _numRx = 0; + _numTx = 0; + _numTxErr = 0; + + uint32_t t_dev; + if (candle_dev_get_timestamp_us(_handle, &t_dev)) { + _hostOffsetStart = + _backend.getUsecsAtMeasurementStart() + + _backend.getUsecsSinceMeasurementStart(); + _deviceTicksStart = t_dev; + } + + candle_channel_start(_handle, 0, flags); + _isOpen = true; +} + +bool CandleApiInterface::isOpen() +{ + return _isOpen; +} + +void CandleApiInterface::close() +{ + candle_channel_stop(_handle, 0); + candle_dev_close(_handle); + _isOpen = false; +} + +void CandleApiInterface::sendMessage(const CanMessage &msg) +{ + candle_frame_t frame; + + frame.can_id = msg.getId(); + if (msg.isExtended()) { + frame.can_id |= CANDLE_ID_EXTENDED; + } + if (msg.isRTR()) { + frame.can_id |= CANDLE_ID_RTR; + } + + frame.can_dlc = msg.getLength(); + for (int i=0; i<8; i++) { + frame.data[i] = msg.getByte(i); + } + + if (candle_frame_send(_handle, 0, &frame)) { + _numTx++; + } else { + _numTxErr++; + } +} + +bool CandleApiInterface::readMessage(QList &msglist, unsigned int timeout_ms) +{ + candle_frame_t frame; + CanMessage msg; + + if (candle_frame_read(_handle, &frame, timeout_ms)) { + + if (candle_frame_type(&frame)==CANDLE_FRAMETYPE_RECEIVE) { + _numRx++; + + msg.setInterfaceId(getId()); + msg.setErrorFrame(false); + msg.setId(candle_frame_id(&frame)); + msg.setExtended(candle_frame_is_extended_id(&frame)); + msg.setRTR(candle_frame_is_rtr(&frame)); + + uint8_t dlc = candle_frame_dlc(&frame); + uint8_t *data = candle_frame_data(&frame); + msg.setLength(dlc); + for (int i=0; i 0x180000000) { // device timestamp overflow must have happend at least once + ts_us += us_since_start & 0xFFFFFFFF00000000; + } + + msg.setTimestamp(ts_us/1000000, ts_us % 1000000); + msglist.append(msg); + return true; + } + + } + + return false; +} + +bool CandleApiInterface::updateStatistics() +{ + return true; +} + +uint32_t CandleApiInterface::getState() +{ + return CanInterface::state_ok; +} + +int CandleApiInterface::getNumRxFrames() +{ + return _numRx; +} + +int CandleApiInterface::getNumRxErrors() +{ + return 0; +} + +int CandleApiInterface::getNumTxFrames() +{ + return _numTx; +} + +int CandleApiInterface::getNumTxErrors() +{ + return _numTxErr; +} + +int CandleApiInterface::getNumRxOverruns() +{ + return 0; +} + +int CandleApiInterface::getNumTxDropped() +{ + return 0; +} + +wstring CandleApiInterface::getPath() const +{ + return wstring(candle_dev_get_path(_handle)); +} + +void CandleApiInterface::update(candle_handle dev) +{ + candle_dev_free(_handle); + _handle = dev; +} + diff --git a/cpp/driver/CandleApiDriver/CandleApiInterface.h b/cpp/driver/CandleApiDriver/CandleApiInterface.h new file mode 100644 index 0000000..1b7db52 --- /dev/null +++ b/cpp/driver/CandleApiDriver/CandleApiInterface.h @@ -0,0 +1,70 @@ +#ifndef CANDLEAPIINTERFACE_H +#define CANDLEAPIINTERFACE_H + +#include +#include +#include "api/candle.h" +#include "CandleApiTiming.h" +#include +#include + +class CandleApiDriver; + +using namespace std; + +class CandleApiInterface : public CanInterface +{ +public: + CandleApiInterface(CandleApiDriver *driver, candle_handle handle); + virtual ~CandleApiInterface(); + + virtual QString getName() const; + virtual QString getDetailsStr() const; + + virtual void applyConfig(const MeasurementInterface &mi); + + virtual unsigned getBitrate(); + + virtual uint32_t getCapabilities(); + virtual QList getAvailableBitrates(); + + virtual void open(); + virtual bool isOpen(); + virtual void close(); + + virtual void sendMessage(const CanMessage &msg); + virtual bool readMessage(QList &msglist, unsigned int timeout_ms); + + virtual bool updateStatistics(); + virtual uint32_t getState(); + virtual int getNumRxFrames(); + virtual int getNumRxErrors(); + virtual int getNumTxFrames(); + virtual int getNumTxErrors(); + virtual int getNumRxOverruns(); + virtual int getNumTxDropped(); + + wstring getPath() const; + + void update(candle_handle dev); + +private: + + uint64_t _hostOffsetStart; + uint32_t _deviceTicksStart; + bool _isOpen; + + candle_handle _handle; + MeasurementInterface _settings; + Backend &_backend; + + uint64_t _numRx; + uint64_t _numTx; + uint64_t _numTxErr; + + QList _timings; + + bool setBitTiming(uint32_t bitrate, uint32_t samplePoint); +}; + +#endif // CANDLEAPIINTERFACE_H diff --git a/cpp/driver/CandleApiDriver/CandleApiTiming.cpp b/cpp/driver/CandleApiDriver/CandleApiTiming.cpp new file mode 100644 index 0000000..ce89ae2 --- /dev/null +++ b/cpp/driver/CandleApiDriver/CandleApiTiming.cpp @@ -0,0 +1,33 @@ +#include "CandleApiTiming.h" + +CandleApiTiming::CandleApiTiming(uint32_t baseClk, uint32_t bitrate, uint32_t samplePoint, uint32_t brp, uint32_t phase_seg1, uint32_t phase_seg2) + : _baseClk(baseClk), + _bitrate(bitrate), + _samplePoint(samplePoint) +{ + _timing.brp = brp; + _timing.phase_seg1 = phase_seg1; + _timing.phase_seg2 = phase_seg2; + _timing.sjw = 1; + _timing.prop_seg = 1; +} + +uint32_t CandleApiTiming::getBaseClk() const +{ + return _baseClk; +} + +uint32_t CandleApiTiming::getBitrate() const +{ + return _bitrate; +} + +uint32_t CandleApiTiming::getSamplePoint() const +{ + return _samplePoint; +} + +candle_bittiming_t CandleApiTiming::getTiming() const +{ + return _timing; +} diff --git a/cpp/driver/CandleApiDriver/CandleApiTiming.h b/cpp/driver/CandleApiDriver/CandleApiTiming.h new file mode 100644 index 0000000..19eb04f --- /dev/null +++ b/cpp/driver/CandleApiDriver/CandleApiTiming.h @@ -0,0 +1,31 @@ +#ifndef CANDLEAPITIMING_H +#define CANDLEAPITIMING_H + +#include "api/candle.h" + +class CandleApiTiming +{ +public: + CandleApiTiming( + uint32_t baseClk, + uint32_t bitrate, + uint32_t samplePoint, + uint32_t brp, + uint32_t phase_seg1, + uint32_t phase_seg2 + ); + + uint32_t getBaseClk() const; + uint32_t getBitrate() const; + uint32_t getSamplePoint() const; + candle_bittiming_t getTiming() const; + +private: + uint32_t _baseClk; + uint32_t _bitrate; + uint32_t _samplePoint; + candle_bittiming_t _timing; + +}; + +#endif // CANDLEAPITIMING_H diff --git a/cpp/driver/CandleApiDriver/api/LICENSE b/cpp/driver/CandleApiDriver/api/LICENSE new file mode 100644 index 0000000..cf8eac1 --- /dev/null +++ b/cpp/driver/CandleApiDriver/api/LICENSE @@ -0,0 +1,66 @@ +GNU LESSER GENERAL PUBLIC LICENSE + +Version 3, 29 June 2007 + +Copyright © 2007 Free Software Foundation, Inc. + +Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. + +This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. +0. Additional Definitions. + +As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License. + +“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. + +An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. + +A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”. + +The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. + +The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. +1. Exception to Section 3 of the GNU GPL. + +You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. +2. Conveying Modified Versions. + +If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: + + a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or + b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. + +3. Object Code Incorporating Material from Library Header Files. + +The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. + b) Accompany the object code with a copy of the GNU GPL and this license document. + +4. Combined Works. + +You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: + + a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. + b) Accompany the Combined Work with a copy of the GNU GPL and this license document. + c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. + d) Do one of the following: + 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. + 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. + e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) + +5. Combined Libraries. + +You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. + b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. + +6. Revised Versions of the GNU Lesser General Public License. + +The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. + +If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. + diff --git a/cpp/driver/CandleApiDriver/api/candle.c b/cpp/driver/CandleApiDriver/api/candle.c new file mode 100644 index 0000000..45840f6 --- /dev/null +++ b/cpp/driver/CandleApiDriver/api/candle.c @@ -0,0 +1,589 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of the candle windows API. + + This library is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation, either + version 3 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . + +*/ + +#include "candle.h" +#include + +#include "candle_defs.h" +#include "candle_ctrl_req.h" +#include "ch_9.h" + +static bool candle_dev_interal_open(candle_handle hdev); + +static bool candle_read_di(HDEVINFO hdi, SP_DEVICE_INTERFACE_DATA interfaceData, candle_device_t *dev) +{ + /* get required length first (this call always fails with an error) */ + ULONG requiredLength=0; + SetupDiGetDeviceInterfaceDetail(hdi, &interfaceData, NULL, 0, &requiredLength, NULL); + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { + dev->last_error = CANDLE_ERR_SETUPDI_IF_DETAILS; + return false; + } + + PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data = + (PSP_DEVICE_INTERFACE_DETAIL_DATA) LocalAlloc(LMEM_FIXED, requiredLength); + + if (detail_data != NULL) { + detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); + } else { + dev->last_error = CANDLE_ERR_MALLOC; + return false; + } + + bool retval = true; + ULONG length = requiredLength; + if (!SetupDiGetDeviceInterfaceDetail(hdi, &interfaceData, detail_data, length, &requiredLength, NULL) ) { + dev->last_error = CANDLE_ERR_SETUPDI_IF_DETAILS2; + retval = false; + } else if (FAILED(StringCchCopy(dev->path, sizeof(dev->path), detail_data->DevicePath))) { + dev->last_error = CANDLE_ERR_PATH_LEN; + retval = false; + } + + LocalFree(detail_data); + + if (!retval) { + return false; + } + + /* try to open to read device infos and see if it is avail */ + if (candle_dev_interal_open(dev)) { + dev->state = CANDLE_DEVSTATE_AVAIL; + candle_dev_close(dev); + } else { + dev->state = CANDLE_DEVSTATE_INUSE; + } + + dev->last_error = CANDLE_ERR_OK; + return true; +} + +bool __stdcall candle_list_scan(candle_list_handle *list) +{ + if (list==NULL) { + return false; + } + + candle_list_t *l = (candle_list_t *)calloc(1, sizeof(candle_list_t)); + *list = l; + if (l==NULL) { + return false; + } + + GUID guid; + if (CLSIDFromString(L"{c15b4308-04d3-11e6-b3ea-6057189e6443}", &guid) != NOERROR) { + l->last_error = CANDLE_ERR_CLSID; + return false; + } + + HDEVINFO hdi = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); + if (hdi == INVALID_HANDLE_VALUE) { + l->last_error = CANDLE_ERR_GET_DEVICES; + return false; + } + + bool rv = false; + for (unsigned i=0; idev[i])) { + l->last_error = l->dev[i].last_error; + rv = false; + break; + } + + } else { + + DWORD err = GetLastError(); + if (err==ERROR_NO_MORE_ITEMS) { + l->num_devices = i; + l->last_error = CANDLE_ERR_OK; + rv = true; + } else { + l->last_error = CANDLE_ERR_SETUPDI_IF_ENUM; + rv = false; + } + break; + + } + + } + + SetupDiDestroyDeviceInfoList(hdi); + + return rv; + +} + +bool __stdcall DLL candle_list_free(candle_list_handle list) +{ + free(list); + return true; +} + +bool __stdcall DLL candle_list_length(candle_list_handle list, uint8_t *len) +{ + candle_list_t *l = (candle_list_t *)list; + *len = l->num_devices; + return true; +} + +bool __stdcall DLL candle_dev_get(candle_list_handle list, uint8_t dev_num, candle_handle *hdev) +{ + candle_list_t *l = (candle_list_t *)list; + if (l==NULL) { + return false; + } + + if (dev_num >= CANDLE_MAX_DEVICES) { + l->last_error = CANDLE_ERR_DEV_OUT_OF_RANGE; + return false; + } + + candle_device_t *dev = calloc(1, sizeof(candle_device_t)); + *hdev = dev; + if (dev==NULL) { + l->last_error = CANDLE_ERR_MALLOC; + return false; + } + + memcpy(dev, &l->dev[dev_num], sizeof(candle_device_t)); + l->last_error = CANDLE_ERR_OK; + dev->last_error = CANDLE_ERR_OK; + return true; +} + + +bool __stdcall DLL candle_dev_get_state(candle_handle hdev, candle_devstate_t *state) +{ + if (hdev==NULL) { + return false; + } else { + candle_device_t *dev = (candle_device_t*)hdev; + *state = dev->state; + return true; + } +} + +wchar_t __stdcall DLL *candle_dev_get_path(candle_handle hdev) +{ + if (hdev==NULL) { + return NULL; + } else { + candle_device_t *dev = (candle_device_t*)hdev; + return dev->path; + } +} + +static bool candle_dev_interal_open(candle_handle hdev) +{ + candle_device_t *dev = (candle_device_t*)hdev; + + memset(dev->rxevents, 0, sizeof(dev->rxevents)); + memset(dev->rxurbs, 0, sizeof(dev->rxurbs)); + + dev->deviceHandle = CreateFile( + dev->path, + GENERIC_WRITE | GENERIC_READ, + FILE_SHARE_WRITE | FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, + NULL + ); + + if (dev->deviceHandle == INVALID_HANDLE_VALUE) { + dev->last_error = CANDLE_ERR_CREATE_FILE; + return false; + } + + if (!WinUsb_Initialize(dev->deviceHandle, &dev->winUSBHandle)) { + dev->last_error = CANDLE_ERR_WINUSB_INITIALIZE; + goto close_handle; + } + + USB_INTERFACE_DESCRIPTOR ifaceDescriptor; + if (!WinUsb_QueryInterfaceSettings(dev->winUSBHandle, 0, &ifaceDescriptor)) { + dev->last_error = CANDLE_ERR_QUERY_INTERFACE; + goto winusb_free; + } + + dev->interfaceNumber = ifaceDescriptor.bInterfaceNumber; + unsigned pipes_found = 0; + + for (uint8_t i=0; iwinUSBHandle, 0, i, &pipeInfo)) { + dev->last_error = CANDLE_ERR_QUERY_PIPE; + goto winusb_free; + } + + if (pipeInfo.PipeType == UsbdPipeTypeBulk && USB_ENDPOINT_DIRECTION_IN(pipeInfo.PipeId)) { + dev->bulkInPipe = pipeInfo.PipeId; + pipes_found++; + } else if (pipeInfo.PipeType == UsbdPipeTypeBulk && USB_ENDPOINT_DIRECTION_OUT(pipeInfo.PipeId)) { + dev->bulkOutPipe = pipeInfo.PipeId; + pipes_found++; + } else { + dev->last_error = CANDLE_ERR_PARSE_IF_DESCR; + goto winusb_free; + } + + } + + if (pipes_found != 2) { + dev->last_error = CANDLE_ERR_PARSE_IF_DESCR; + goto winusb_free; + } + + char use_raw_io = 1; + if (!WinUsb_SetPipePolicy(dev->winUSBHandle, dev->bulkInPipe, RAW_IO, sizeof(use_raw_io), &use_raw_io)) { + dev->last_error = CANDLE_ERR_SET_PIPE_RAW_IO; + goto winusb_free; + } + + if (!candle_ctrl_set_host_format(dev)) { + goto winusb_free; + } + + if (!candle_ctrl_get_config(dev, &dev->dconf)) { + goto winusb_free; + } + + if (!candle_ctrl_get_capability(dev, 0, &dev->bt_const)) { + dev->last_error = CANDLE_ERR_GET_BITTIMING_CONST; + goto winusb_free; + } + + dev->last_error = CANDLE_ERR_OK; + return true; + +winusb_free: + WinUsb_Free(dev->winUSBHandle); + +close_handle: + CloseHandle(dev->deviceHandle); + return false; + +} + +static bool candle_prepare_read(candle_device_t *dev, unsigned urb_num) +{ + bool rc = WinUsb_ReadPipe( + dev->winUSBHandle, + dev->bulkInPipe, + dev->rxurbs[urb_num].buf, + sizeof(dev->rxurbs[urb_num].buf), + NULL, + &dev->rxurbs[urb_num].ovl + ); + + if (rc || (GetLastError()!=ERROR_IO_PENDING)) { + dev->last_error = CANDLE_ERR_PREPARE_READ; + return false; + } else { + dev->last_error = CANDLE_ERR_OK; + return true; + } +} + +static bool candle_close_rxurbs(candle_device_t *dev) +{ + for (unsigned i=0; irxevents[i] != NULL) { + CloseHandle(dev->rxevents[i]); + } + } + return true; +} + + +bool __stdcall DLL candle_dev_open(candle_handle hdev) +{ + candle_device_t *dev = (candle_device_t*)hdev; + + if (candle_dev_interal_open(dev)) { + for (unsigned i=0; irxevents[i] = ev; + dev->rxurbs[i].ovl.hEvent = ev; + if (!candle_prepare_read(dev, i)) { + candle_close_rxurbs(dev); + return false; // keep last_error from prepare_read call + } + } + dev->last_error = CANDLE_ERR_OK; + return true; + } else { + return false; // keep last_error from open_device call + } + +} + +bool __stdcall DLL candle_dev_get_timestamp_us(candle_handle hdev, uint32_t *timestamp_us) +{ + return candle_ctrl_get_timestamp(hdev, timestamp_us); +} + +bool __stdcall DLL candle_dev_close(candle_handle hdev) +{ + candle_device_t *dev = (candle_device_t*)hdev; + + candle_close_rxurbs(dev); + + WinUsb_Free(dev->winUSBHandle); + dev->winUSBHandle = NULL; + CloseHandle(dev->deviceHandle); + dev->deviceHandle = NULL; + + dev->last_error = CANDLE_ERR_OK; + return true; +} + +bool __stdcall DLL candle_dev_free(candle_handle hdev) +{ + free(hdev); + return true; +} + +candle_err_t __stdcall DLL candle_dev_last_error(candle_handle hdev) +{ + candle_device_t *dev = (candle_device_t*)hdev; + return dev->last_error; +} + +bool __stdcall DLL candle_channel_count(candle_handle hdev, uint8_t *num_channels) +{ + // TODO check if info was already read from device; try to do so; throw error... + candle_device_t *dev = (candle_device_t*)hdev; + *num_channels = dev->dconf.icount+1; + return true; +} + +bool __stdcall DLL candle_channel_get_capabilities(candle_handle hdev, uint8_t ch, candle_capability_t *cap) +{ + // TODO check if info was already read from device; try to do so; throw error... + candle_device_t *dev = (candle_device_t*)hdev; + memcpy(cap, &dev->bt_const.feature, sizeof(candle_capability_t)); + return true; +} + +bool __stdcall DLL candle_channel_set_timing(candle_handle hdev, uint8_t ch, candle_bittiming_t *data) +{ + // TODO ensure device is open, check channel count.. + candle_device_t *dev = (candle_device_t*)hdev; + return candle_ctrl_set_bittiming(dev, ch, data); +} + +bool __stdcall DLL candle_channel_set_bitrate(candle_handle hdev, uint8_t ch, uint32_t bitrate) +{ + // TODO ensure device is open, check channel count.. + candle_device_t *dev = (candle_device_t*)hdev; + + if (dev->bt_const.fclk_can != 48000000) { + /* this function only works for the candleLight base clock of 48MHz */ + dev->last_error = CANDLE_ERR_BITRATE_FCLK; + return false; + } + + candle_bittiming_t t; + t.prop_seg = 1; + t.sjw = 1; + t.phase_seg1 = 13 - t.prop_seg; + t.phase_seg2 = 2; + + switch (bitrate) { + case 10000: + t.brp = 300; + break; + + case 20000: + t.brp = 150; + break; + + case 50000: + t.brp = 60; + break; + + case 83333: + t.brp = 36; + break; + + case 100000: + t.brp = 30; + break; + + case 125000: + t.brp = 24; + break; + + case 250000: + t.brp = 12; + break; + + case 500000: + t.brp = 6; + break; + + case 800000: + t.brp = 4; + t.phase_seg1 = 12 - t.prop_seg; + t.phase_seg2 = 2; + break; + + case 1000000: + t.brp = 3; + break; + + default: + dev->last_error = CANDLE_ERR_BITRATE_UNSUPPORTED; + return false; + } + + return candle_ctrl_set_bittiming(dev, ch, &t); +} + +bool __stdcall DLL candle_channel_start(candle_handle hdev, uint8_t ch, uint32_t flags) +{ + // TODO ensure device is open, check channel count.. + candle_device_t *dev = (candle_device_t*)hdev; + flags |= CANDLE_MODE_HW_TIMESTAMP; + return candle_ctrl_set_device_mode(dev, ch, CANDLE_DEVMODE_START, flags); +} + +bool __stdcall DLL candle_channel_stop(candle_handle hdev, uint8_t ch) +{ + // TODO ensure device is open, check channel count.. + candle_device_t *dev = (candle_device_t*)hdev; + return candle_ctrl_set_device_mode(dev, ch, CANDLE_DEVMODE_RESET, 0); +} + +bool __stdcall DLL candle_frame_send(candle_handle hdev, uint8_t ch, candle_frame_t *frame) +{ + // TODO ensure device is open, check channel count.. + candle_device_t *dev = (candle_device_t*)hdev; + + unsigned long bytes_sent = 0; + + frame->echo_id = 0; + frame->channel = ch; + + bool rc = WinUsb_WritePipe( + dev->winUSBHandle, + dev->bulkOutPipe, + (uint8_t*)frame, + sizeof(*frame), + &bytes_sent, + 0 + ); + + dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SEND_FRAME; + return rc; + +} + +bool __stdcall DLL candle_frame_read(candle_handle hdev, candle_frame_t *frame, uint32_t timeout_ms) +{ + // TODO ensure device is open.. + candle_device_t *dev = (candle_device_t*)hdev; + + DWORD wait_result = WaitForMultipleObjects(CANDLE_URB_COUNT, dev->rxevents, false, timeout_ms); + if (wait_result == WAIT_TIMEOUT) { + dev->last_error = CANDLE_ERR_READ_TIMEOUT; + return false; + } + + if ( (wait_result < WAIT_OBJECT_0) || (wait_result >= WAIT_OBJECT_0 + CANDLE_URB_COUNT) ) { + dev->last_error = CANDLE_ERR_READ_WAIT; + return false; + } + + DWORD urb_num = wait_result - WAIT_OBJECT_0; + DWORD bytes_transfered; + + if (!WinUsb_GetOverlappedResult(dev->winUSBHandle, &dev->rxurbs[urb_num].ovl, &bytes_transfered, false)) { + candle_prepare_read(dev, urb_num); + dev->last_error = CANDLE_ERR_READ_RESULT; + return false; + } + + if (bytes_transfered < sizeof(*frame)-4) { + candle_prepare_read(dev, urb_num); + dev->last_error = CANDLE_ERR_READ_SIZE; + return false; + } + + if (bytes_transfered < sizeof(*frame)) { + frame->timestamp_us = 0; + } + + memcpy(frame, dev->rxurbs[urb_num].buf, sizeof(*frame)); + + return candle_prepare_read(dev, urb_num); +} + +candle_frametype_t __stdcall DLL candle_frame_type(candle_frame_t *frame) +{ + if (frame->echo_id != 0xFFFFFFFF) { + return CANDLE_FRAMETYPE_ECHO; + }; + + if (frame->can_id & CANDLE_ID_ERR) { + return CANDLE_FRAMETYPE_ERROR; + } + + return CANDLE_FRAMETYPE_RECEIVE; +} + +uint32_t __stdcall DLL candle_frame_id(candle_frame_t *frame) +{ + return frame->can_id & 0x1FFFFFFF; +} + +bool __stdcall DLL candle_frame_is_extended_id(candle_frame_t *frame) +{ + return (frame->can_id & CANDLE_ID_EXTENDED) != 0; +} + +bool __stdcall DLL candle_frame_is_rtr(candle_frame_t *frame) +{ + return (frame->can_id & CANDLE_ID_RTR) != 0; +} + +uint8_t __stdcall DLL candle_frame_dlc(candle_frame_t *frame) +{ + return frame->can_dlc; +} + +uint8_t __stdcall DLL *candle_frame_data(candle_frame_t *frame) +{ + return frame->data; +} + +uint32_t __stdcall DLL candle_frame_timestamp_us(candle_frame_t *frame) +{ + return frame->timestamp_us; +} diff --git a/cpp/driver/CandleApiDriver/api/candle.h b/cpp/driver/CandleApiDriver/api/candle.h new file mode 100644 index 0000000..6810c56 --- /dev/null +++ b/cpp/driver/CandleApiDriver/api/candle.h @@ -0,0 +1,166 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of the candle windows API. + + This library is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation, either + version 3 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . + +*/ + +#pragma once +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void* candle_list_handle; +typedef void* candle_handle; + +typedef enum { + CANDLE_DEVSTATE_AVAIL, + CANDLE_DEVSTATE_INUSE +} candle_devstate_t; + +typedef enum { + CANDLE_FRAMETYPE_UNKNOWN, + CANDLE_FRAMETYPE_RECEIVE, + CANDLE_FRAMETYPE_ECHO, + CANDLE_FRAMETYPE_ERROR, + CANDLE_FRAMETYPE_TIMESTAMP_OVFL +} candle_frametype_t; + +enum { + CANDLE_ID_EXTENDED = 0x80000000, + CANDLE_ID_RTR = 0x40000000, + CANDLE_ID_ERR = 0x20000000 +}; + +typedef enum { + CANDLE_MODE_NORMAL = 0x00, + CANDLE_MODE_LISTEN_ONLY = 0x01, + CANDLE_MODE_LOOP_BACK = 0x02, + CANDLE_MODE_TRIPLE_SAMPLE = 0x04, + CANDLE_MODE_ONE_SHOT = 0x08, + CANDLE_MODE_HW_TIMESTAMP = 0x10, +} candle_mode_t; + +typedef enum { + CANDLE_ERR_OK = 0, + CANDLE_ERR_CREATE_FILE = 1, + CANDLE_ERR_WINUSB_INITIALIZE = 2, + CANDLE_ERR_QUERY_INTERFACE = 3, + CANDLE_ERR_QUERY_PIPE = 4, + CANDLE_ERR_PARSE_IF_DESCR = 5, + CANDLE_ERR_SET_HOST_FORMAT = 6, + CANDLE_ERR_GET_DEVICE_INFO = 7, + CANDLE_ERR_GET_BITTIMING_CONST = 8, + CANDLE_ERR_PREPARE_READ = 9, + CANDLE_ERR_SET_DEVICE_MODE = 10, + CANDLE_ERR_SET_BITTIMING = 11, + CANDLE_ERR_BITRATE_FCLK = 12, + CANDLE_ERR_BITRATE_UNSUPPORTED = 13, + CANDLE_ERR_SEND_FRAME = 14, + CANDLE_ERR_READ_TIMEOUT = 15, + CANDLE_ERR_READ_WAIT = 16, + CANDLE_ERR_READ_RESULT = 17, + CANDLE_ERR_READ_SIZE = 18, + CANDLE_ERR_SETUPDI_IF_DETAILS = 19, + CANDLE_ERR_SETUPDI_IF_DETAILS2 = 20, + CANDLE_ERR_MALLOC = 21, + CANDLE_ERR_PATH_LEN = 22, + CANDLE_ERR_CLSID = 23, + CANDLE_ERR_GET_DEVICES = 24, + CANDLE_ERR_SETUPDI_IF_ENUM = 25, + CANDLE_ERR_SET_TIMESTAMP_MODE = 26, + CANDLE_ERR_DEV_OUT_OF_RANGE = 27, + CANDLE_ERR_GET_TIMESTAMP = 28, + CANDLE_ERR_SET_PIPE_RAW_IO = 29 +} candle_err_t; + +#pragma pack(push,1) + +typedef struct { + uint32_t echo_id; + uint32_t can_id; + uint8_t can_dlc; + uint8_t channel; + uint8_t flags; + uint8_t reserved; + uint8_t data[8]; + uint32_t timestamp_us; +} candle_frame_t; + +typedef struct { + uint32_t feature; + uint32_t fclk_can; + uint32_t tseg1_min; + uint32_t tseg1_max; + uint32_t tseg2_min; + uint32_t tseg2_max; + uint32_t sjw_max; + uint32_t brp_min; + uint32_t brp_max; + uint32_t brp_inc; +} candle_capability_t; + +typedef struct { + uint32_t prop_seg; + uint32_t phase_seg1; + uint32_t phase_seg2; + uint32_t sjw; + uint32_t brp; +} candle_bittiming_t; + +#pragma pack(pop) + +#define DLL + +bool __stdcall DLL candle_list_scan(candle_list_handle *list); +bool __stdcall DLL candle_list_free(candle_list_handle list); +bool __stdcall DLL candle_list_length(candle_list_handle list, uint8_t *len); + +bool __stdcall DLL candle_dev_get(candle_list_handle list, uint8_t dev_num, candle_handle *hdev); +bool __stdcall DLL candle_dev_get_state(candle_handle hdev, candle_devstate_t *state); +wchar_t __stdcall DLL *candle_dev_get_path(candle_handle hdev); +bool __stdcall DLL candle_dev_open(candle_handle hdev); +bool __stdcall DLL candle_dev_get_timestamp_us(candle_handle hdev, uint32_t *timestamp_us); +bool __stdcall DLL candle_dev_close(candle_handle hdev); +bool __stdcall DLL candle_dev_free(candle_handle hdev); + +bool __stdcall DLL candle_channel_count(candle_handle hdev, uint8_t *num_channels); +bool __stdcall DLL candle_channel_get_capabilities(candle_handle hdev, uint8_t ch, candle_capability_t *cap); +bool __stdcall DLL candle_channel_set_timing(candle_handle hdev, uint8_t ch, candle_bittiming_t *data); +bool __stdcall DLL candle_channel_set_bitrate(candle_handle hdev, uint8_t ch, uint32_t bitrate); +bool __stdcall DLL candle_channel_start(candle_handle hdev, uint8_t ch, uint32_t flags); +bool __stdcall DLL candle_channel_stop(candle_handle hdev, uint8_t ch); + +bool __stdcall DLL candle_frame_send(candle_handle hdev, uint8_t ch, candle_frame_t *frame); +bool __stdcall DLL candle_frame_read(candle_handle hdev, candle_frame_t *frame, uint32_t timeout_ms); + +candle_frametype_t __stdcall DLL candle_frame_type(candle_frame_t *frame); +uint32_t __stdcall DLL candle_frame_id(candle_frame_t *frame); +bool __stdcall DLL candle_frame_is_extended_id(candle_frame_t *frame); +bool __stdcall DLL candle_frame_is_rtr(candle_frame_t *frame); +uint8_t __stdcall DLL candle_frame_dlc(candle_frame_t *frame); +uint8_t __stdcall DLL *candle_frame_data(candle_frame_t *frame); +uint32_t __stdcall DLL candle_frame_timestamp_us(candle_frame_t *frame); + +candle_err_t __stdcall DLL candle_dev_last_error(candle_handle hdev); + +#ifdef __cplusplus +} +#endif diff --git a/cpp/driver/CandleApiDriver/api/candle_ctrl_req.c b/cpp/driver/CandleApiDriver/api/candle_ctrl_req.c new file mode 100644 index 0000000..f44d3e1 --- /dev/null +++ b/cpp/driver/CandleApiDriver/api/candle_ctrl_req.c @@ -0,0 +1,152 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of the candle windows API. + + This library is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation, either + version 3 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . + +*/ + +#include "candle_ctrl_req.h" +#include "ch_9.h" + +enum { + CANDLE_BREQ_HOST_FORMAT = 0, + CANDLE_BREQ_BITTIMING, + CANDLE_BREQ_MODE, + CANDLE_BREQ_BERR, + CANDLE_BREQ_BT_CONST, + CANDLE_BREQ_DEVICE_CONFIG, + CANDLE_TIMESTAMP_GET, +}; + +static bool usb_control_msg(WINUSB_INTERFACE_HANDLE hnd, uint8_t request, uint8_t requesttype, uint16_t value, uint16_t index, void *data, uint16_t size) +{ + WINUSB_SETUP_PACKET packet; + memset(&packet, 0, sizeof(packet)); + + packet.Request = request; + packet.RequestType = requesttype; + packet.Value = value; + packet.Index = index; + packet.Length = size; + + unsigned long bytes_sent = 0; + return WinUsb_ControlTransfer(hnd, packet, (uint8_t*)data, size, &bytes_sent, 0); +} + +bool candle_ctrl_set_host_format(candle_device_t *dev) +{ + candle_host_config_t hconf; + hconf.byte_order = 0x0000beef; + + bool rc = usb_control_msg( + dev->winUSBHandle, + CANDLE_BREQ_HOST_FORMAT, + USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + 1, + dev->interfaceNumber, + &hconf, + sizeof(hconf) + ); + + dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_HOST_FORMAT; + return rc; +} + +bool candle_ctrl_set_device_mode(candle_device_t *dev, uint8_t channel, uint32_t mode, uint32_t flags) +{ + candle_device_mode_t dm; + dm.mode = mode; + dm.flags = flags; + + bool rc = usb_control_msg( + dev->winUSBHandle, + CANDLE_BREQ_MODE, + USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + channel, + dev->interfaceNumber, + &dm, + sizeof(dm) + ); + + dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_DEVICE_MODE; + return rc; +} + + +bool candle_ctrl_get_config(candle_device_t *dev, candle_device_config_t *dconf) +{ + bool rc = usb_control_msg( + dev->winUSBHandle, + CANDLE_BREQ_DEVICE_CONFIG, + USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + 1, + dev->interfaceNumber, + dconf, + sizeof(*dconf) + ); + + dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_DEVICE_INFO; + return rc; +} + +bool candle_ctrl_get_timestamp(candle_device_t *dev, uint32_t *current_timestamp) +{ + bool rc = usb_control_msg( + dev->winUSBHandle, + CANDLE_TIMESTAMP_GET, + USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + 1, + dev->interfaceNumber, + current_timestamp, + sizeof(*current_timestamp) + ); + + dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_TIMESTAMP; + return rc; +} + +bool candle_ctrl_get_capability(candle_device_t *dev, uint8_t channel, candle_capability_t *data) +{ + bool rc = usb_control_msg( + dev->winUSBHandle, + CANDLE_BREQ_BT_CONST, + USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + channel, + 0, + data, + sizeof(*data) + ); + + dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_BITTIMING_CONST; + return rc; +} + +bool candle_ctrl_set_bittiming(candle_device_t *dev, uint8_t channel, candle_bittiming_t *data) +{ + bool rc = usb_control_msg( + dev->winUSBHandle, + CANDLE_BREQ_BITTIMING, + USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + channel, + 0, + data, + sizeof(*data) + ); + + dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_BITTIMING; + return rc; +} diff --git a/cpp/driver/CandleApiDriver/api/candle_ctrl_req.h b/cpp/driver/CandleApiDriver/api/candle_ctrl_req.h new file mode 100644 index 0000000..3d0cf21 --- /dev/null +++ b/cpp/driver/CandleApiDriver/api/candle_ctrl_req.h @@ -0,0 +1,37 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of the candle windows API. + + This library is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation, either + version 3 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . + +*/ + +#pragma once + +#include "candle_defs.h" + +enum { + CANDLE_DEVMODE_RESET = 0, + CANDLE_DEVMODE_START = 1 +}; + +bool candle_ctrl_set_host_format(candle_device_t *dev); +bool candle_ctrl_set_device_mode(candle_device_t *dev, uint8_t channel, uint32_t mode, uint32_t flags); +bool candle_ctrl_get_config(candle_device_t *dev, candle_device_config_t *dconf); +bool candle_ctrl_get_capability(candle_device_t *dev, uint8_t channel, candle_capability_t *data); +bool candle_ctrl_set_bittiming(candle_device_t *dev, uint8_t channel, candle_bittiming_t *data); +bool candle_ctrl_get_timestamp(candle_device_t *dev, uint32_t *current_timestamp); + diff --git a/cpp/driver/CandleApiDriver/api/candle_defs.h b/cpp/driver/CandleApiDriver/api/candle_defs.h new file mode 100644 index 0000000..e09a49e --- /dev/null +++ b/cpp/driver/CandleApiDriver/api/candle_defs.h @@ -0,0 +1,90 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of the candle windows API. + + This library is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation, either + version 3 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . + +*/ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#undef __CRT__NO_INLINE +#include +#define __CRT__NO_INLINE + +#include "candle.h" + +#define CANDLE_MAX_DEVICES 32 +#define CANDLE_URB_COUNT 30 + +#pragma pack(push,1) + +typedef struct { + uint32_t byte_order; +} candle_host_config_t; + +typedef struct { + uint8_t reserved1; + uint8_t reserved2; + uint8_t reserved3; + uint8_t icount; + uint32_t sw_version; + uint32_t hw_version; +} candle_device_config_t; + +typedef struct { + uint32_t mode; + uint32_t flags; +} candle_device_mode_t; + +#pragma pack(pop) + + +typedef struct { + OVERLAPPED ovl; + uint8_t buf[128]; +} canlde_rx_urb; + +typedef struct { + wchar_t path[256]; + candle_devstate_t state; + candle_err_t last_error; + + HANDLE deviceHandle; + WINUSB_INTERFACE_HANDLE winUSBHandle; + UCHAR interfaceNumber; + UCHAR bulkInPipe; + UCHAR bulkOutPipe; + + candle_device_config_t dconf; + candle_capability_t bt_const; + canlde_rx_urb rxurbs[CANDLE_URB_COUNT]; + HANDLE rxevents[CANDLE_URB_COUNT]; +} candle_device_t; + +typedef struct { + uint8_t num_devices; + candle_err_t last_error; + candle_device_t dev[CANDLE_MAX_DEVICES]; +} candle_list_t; diff --git a/cpp/driver/CandleApiDriver/api/ch_9.h b/cpp/driver/CandleApiDriver/api/ch_9.h new file mode 100644 index 0000000..ba1994b --- /dev/null +++ b/cpp/driver/CandleApiDriver/api/ch_9.h @@ -0,0 +1,37 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of the candle windows API. + + This library is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation, either + version 3 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . + +*/ + +#pragma once + +#define USB_DIR_OUT 0 /* to device */ +#define USB_DIR_IN 0x80 /* to host */ + +#define USB_TYPE_MASK (0x03 << 5) +#define USB_TYPE_STANDARD (0x00 << 5) +#define USB_TYPE_CLASS (0x01 << 5) +#define USB_TYPE_VENDOR (0x02 << 5) +#define USB_TYPE_RESERVED (0x03 << 5) + +#define USB_RECIP_MASK 0x1f +#define USB_RECIP_DEVICE 0x00 +#define USB_RECIP_INTERFACE 0x01 +#define USB_RECIP_ENDPOINT 0x02 +#define USB_RECIP_OTHER 0x03 diff --git a/cpp/driver/GenericCanSetupPage.cpp b/cpp/driver/GenericCanSetupPage.cpp new file mode 100644 index 0000000..4afa0f9 --- /dev/null +++ b/cpp/driver/GenericCanSetupPage.cpp @@ -0,0 +1,172 @@ +#include "GenericCanSetupPage.h" +#include "ui_GenericCanSetupPage.h" +#include +#include +#include +#include +#include +#include + +GenericCanSetupPage::GenericCanSetupPage(QWidget *parent) : + QWidget(parent), + ui(new Ui::GenericCanSetupPage), + _mi(0), + _enable_ui_updates(false) +{ + ui->setupUi(this); + connect(ui->cbBitrate, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); + connect(ui->cbSamplePoint, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); + connect(ui->cbBitrateFD, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); + connect(ui->cbSamplePointFD, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); + + connect(ui->cbConfigOS, SIGNAL(stateChanged(int)), this, SLOT(updateUI())); + connect(ui->cbListenOnly, SIGNAL(stateChanged(int)), this, SLOT(updateUI())); + connect(ui->cbOneShot, SIGNAL(stateChanged(int)), this, SLOT(updateUI())); + connect(ui->cbTripleSampling, SIGNAL(stateChanged(int)), this, SLOT(updateUI())); + connect(ui->cbAutoRestart, SIGNAL(stateChanged(int)), this, SLOT(updateUI())); +} + +GenericCanSetupPage::~GenericCanSetupPage() +{ + delete ui; +} + +void GenericCanSetupPage::onSetupDialogCreated(SetupDialog &dlg) +{ + dlg.addPage(this); + connect(&dlg, SIGNAL(onShowInterfacePage(SetupDialog&,MeasurementInterface*)), this, SLOT(onShowInterfacePage(SetupDialog&,MeasurementInterface*))); +} + +void GenericCanSetupPage::onShowInterfacePage(SetupDialog &dlg, MeasurementInterface *mi) +{ + _mi = mi; + CanInterface *intf = backend().getInterfaceById(_mi->canInterface()); + + _enable_ui_updates = false; + + ui->laDriver->setText(intf->getDriver()->getName()); + ui->laInterface->setText(intf->getName()); + ui->laInterfaceDetails->setText(intf->getDetailsStr()); + + fillBitratesList(intf, _mi->bitrate()); + fillFdBitrate(intf, _mi->bitrate()); + fillSamplePointsForBitrate(intf, _mi->bitrate(), _mi->samplePoint()); + + + ui->cbConfigOS->setChecked(!_mi->doConfigure()); + ui->cbListenOnly->setChecked(_mi->isListenOnlyMode()); + ui->cbOneShot->setChecked(_mi->isOneShotMode()); + ui->cbTripleSampling->setChecked(_mi->isTripleSampling()); + ui->cbAutoRestart->setChecked(_mi->doAutoRestart()); + + disenableUI(_mi->doConfigure()); + dlg.displayPage(this); + + _enable_ui_updates = true; +} + +void GenericCanSetupPage::updateUI() +{ + if (_enable_ui_updates && (_mi!=0)) { + CanInterface *intf = backend().getInterfaceById(_mi->canInterface()); + + _mi->setDoConfigure(!ui->cbConfigOS->isChecked()); + _mi->setListenOnlyMode(ui->cbListenOnly->isChecked()); + _mi->setOneShotMode(ui->cbOneShot->isChecked()); + _mi->setTripleSampling(ui->cbTripleSampling->isChecked()); + _mi->setAutoRestart(ui->cbAutoRestart->isChecked()); + _mi->setBitrate(ui->cbBitrate->currentData().toUInt()); + _mi->setSamplePoint(ui->cbSamplePoint->currentData().toUInt()); + + _enable_ui_updates = false; + + disenableUI(_mi->doConfigure()); + fillSamplePointsForBitrate( + intf, + ui->cbBitrate->currentData().toUInt(), + ui->cbSamplePoint->currentData().toUInt() + ); + _enable_ui_updates = true; + + } + + +} + +void GenericCanSetupPage::fillBitratesList(CanInterface *intf, unsigned selectedBitrate) +{ + QList bitrates; + foreach (CanTiming t, intf->getAvailableBitrates()) { + if (!bitrates.contains(t.getBitrate())) { + bitrates.append(t.getBitrate()); + } + } + qSort(bitrates); + + ui->cbBitrate->clear(); + foreach (uint32_t br, bitrates) { + ui->cbBitrate->addItem(QString::number(br), br); + } + ui->cbBitrate->setCurrentText(QString::number(selectedBitrate)); +} + +void GenericCanSetupPage::fillSamplePointsForBitrate(CanInterface *intf, unsigned selectedBitrate, unsigned selectedSamplePoint) +{ + QList samplePoints; + foreach(CanTiming t, intf->getAvailableBitrates()) { + if (t.getBitrate() == selectedBitrate) { + if (!samplePoints.contains(t.getSamplePoint())) { + samplePoints.append(t.getSamplePoint()); + } + } + } + qSort(samplePoints); + + ui->cbSamplePoint->clear(); + foreach (uint32_t sp, samplePoints) { + ui->cbSamplePoint->addItem(CanTiming::getSamplePointStr(sp), sp); + } + ui->cbSamplePoint->setCurrentText(CanTiming::getSamplePointStr(selectedSamplePoint)); +} + + +void GenericCanSetupPage::fillFdBitrate(CanInterface *intf, unsigned selectedBitrate) +{ + QList fdBitrates; + foreach(CanTiming t, intf->getAvailableBitrates()) { + if (t.getBitrate() == selectedBitrate) { + if (t.isCanFD() && !fdBitrates.contains(t.getBitrateFD())) { + fdBitrates.append(t.getBitrateFD()); + } + } + } + qSort(fdBitrates); + + ui->cbBitrateFD->clear(); + foreach (uint32_t fd_br, fdBitrates) { + ui->cbBitrateFD->addItem(QString::number(fd_br), fd_br); + } +} + +void GenericCanSetupPage::disenableUI(bool enabled) +{ + + CanInterface *intf = backend().getInterfaceById(_mi->canInterface()); + uint32_t caps = intf->getCapabilities(); + + ui->cbBitrate->setEnabled(enabled); + ui->cbSamplePoint->setEnabled(enabled); + ui->cbConfigOS->setEnabled(caps & CanInterface::capability_config_os); + + ui->cbBitrateFD->setEnabled(enabled && (caps & CanInterface::capability_canfd)); + ui->cbSamplePointFD->setEnabled(enabled && (caps & CanInterface::capability_canfd)); + ui->cbListenOnly->setEnabled(enabled && (caps & CanInterface::capability_listen_only)); + ui->cbOneShot->setEnabled(enabled && (caps & CanInterface::capability_one_shot)); + ui->cbTripleSampling->setEnabled(enabled && (caps & CanInterface::capability_triple_sampling)); + ui->cbAutoRestart->setEnabled(enabled && (caps & CanInterface::capability_auto_restart)); +} + +Backend &GenericCanSetupPage::backend() +{ + return Backend::instance(); +} diff --git a/cpp/driver/GenericCanSetupPage.h b/cpp/driver/GenericCanSetupPage.h new file mode 100644 index 0000000..78bcfe7 --- /dev/null +++ b/cpp/driver/GenericCanSetupPage.h @@ -0,0 +1,43 @@ +#ifndef GENERICCANSETUPPAGE_H +#define GENERICCANSETUPPAGE_H + +#include + +namespace Ui { +class GenericCanSetupPage; +} + +class CanInterface; +class SetupDialog; +class MeasurementInterface; +class Backend; + +class GenericCanSetupPage : public QWidget +{ + Q_OBJECT + +public: + explicit GenericCanSetupPage(QWidget *parent = 0); + ~GenericCanSetupPage(); + +public slots: + void onSetupDialogCreated(SetupDialog &dlg); + void onShowInterfacePage(SetupDialog &dlg, MeasurementInterface *mi); + +private slots: + void updateUI(); + +private: + Ui::GenericCanSetupPage *ui; + MeasurementInterface *_mi; + bool _enable_ui_updates; + + void fillBitratesList(CanInterface *intf, unsigned selectedBitrate); + void fillSamplePointsForBitrate(CanInterface *intf, unsigned selectedBitrate, unsigned selectedSamplePoint); + void fillFdBitrate(CanInterface *intf, unsigned selectedBitrate); + void disenableUI(bool enabled); + + Backend &backend(); +}; + +#endif // GENERICCANSETUPPAGE_H diff --git a/cpp/driver/GenericCanSetupPage.ui b/cpp/driver/GenericCanSetupPage.ui new file mode 100644 index 0000000..5590427 --- /dev/null +++ b/cpp/driver/GenericCanSetupPage.ui @@ -0,0 +1,249 @@ + + + GenericCanSetupPage + + + + 0 + 0 + 795 + 633 + + + + Form + + + + + 9 + 9 + 66 + 16 + + + + Driver: + + + + + + 140 + 9 + 610 + 16 + + + + TextLabel + + + + + + 9 + 28 + 82 + 16 + + + + Interface: + + + + + + 140 + 28 + 610 + 16 + + + + TextLabel + + + + + + 9 + 47 + 117 + 16 + + + + Interface Details: + + + + + + 140 + 47 + 610 + 16 + + + + TextLabel + + + + + + 9 + 84 + 69 + 16 + + + + Bitrate: + + + + + + 140 + 84 + 170 + 20 + + + + + + + 9 + 110 + 98 + 16 + + + + Sample Point: + + + + + + 140 + 110 + 170 + 20 + + + + + + + 9 + 150 + 104 + 16 + + + + CanFD Bitrate: + + + + + + 140 + 150 + 170 + 20 + + + + + + + 9 + 176 + 130 + 16 + + + + CanFD SamplePoint: + + + + + + 140 + 176 + 170 + 20 + + + + + + + 9 + 220 + 41 + 16 + + + + Options: + + + + + + 140 + 220 + 411 + 126 + + + + + + + configured by operating system + + + + + + + Listen only mode + + + + + + + One-Shot mode + + + + + + + Triple Sampling + + + + + + + Auto-Restart on bus off condition + + + + + + + + + diff --git a/cpp/driver/SLCANDriver/SLCANDriver.cpp b/cpp/driver/SLCANDriver/SLCANDriver.cpp new file mode 100644 index 0000000..51b3a3c --- /dev/null +++ b/cpp/driver/SLCANDriver/SLCANDriver.cpp @@ -0,0 +1,107 @@ +/* + + Copyright (c) 2022 Ethan Zonca + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + + +#include "SLCANDriver.h" +#include "SLCANInterface.h" +#include +#include + +#include +#include +#include +#include +#include + +// +#include +#include +#include +#include + +SLCANDriver::SLCANDriver(Backend &backend) + : CanDriver(backend), + setupPage(new GenericCanSetupPage()) +{ + QObject::connect(&backend, SIGNAL(onSetupDialogCreated(SetupDialog&)), setupPage, SLOT(onSetupDialogCreated(SetupDialog&))); +} + +SLCANDriver::~SLCANDriver() { +} + +bool SLCANDriver::update() { + + deleteAllInterfaces(); + + int interface_cnt = 0; + + foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { + fprintf(stderr, "Name : %s \r\n", info.portName().toStdString().c_str()); + fprintf(stderr, " Description : %s \r\n", info.description().toStdString().c_str()); + fprintf(stderr, " Manufacturer: %s \r\n", info.manufacturer().toStdString().c_str()); + + if(info.vendorIdentifier() == 0xad50 && info.productIdentifier() == 0x60C4) + { + + perror(" ++ CANable 1.0 or similar ST USB CDC device detected"); + + // Create new slcan interface without FD support + SLCANInterface *intf = createOrUpdateInterface(interface_cnt, info.portName(), false); + interface_cnt++; + } + else if(info.vendorIdentifier() == 0x16D0 && info.productIdentifier() == 0x117E) + { + perror(" ++ CANable 2.0 detected"); + + // Create new slcan interface without FD support + SLCANInterface *intf = createOrUpdateInterface(interface_cnt, info.portName(), true); + interface_cnt++; + } + else + { + perror(" !! This is not a CANable device!"); + } + } + + return true; +} + +QString SLCANDriver::getName() { + return "CANable SLCAN"; +} + + + +SLCANInterface *SLCANDriver::createOrUpdateInterface(int index, QString name, bool fd_support) { + + foreach (CanInterface *intf, getInterfaces()) { + SLCANInterface *scif = dynamic_cast(intf); + if (scif->getIfIndex() == index) { + scif->setName(name); + return scif; + } + } + + + SLCANInterface *scif = new SLCANInterface(this, index, name, fd_support); + addInterface(scif); + return scif; +} diff --git a/cpp/driver/SLCANDriver/SLCANDriver.h b/cpp/driver/SLCANDriver/SLCANDriver.h new file mode 100644 index 0000000..0fe6e49 --- /dev/null +++ b/cpp/driver/SLCANDriver/SLCANDriver.h @@ -0,0 +1,43 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include + +class SLCANInterface; +class SetupDialogInterfacePage; +class GenericCanSetupPage; + +class SLCANDriver: public CanDriver { +public: + SLCANDriver(Backend &backend); + virtual ~SLCANDriver(); + + virtual QString getName(); + virtual bool update(); + +private: + SLCANInterface *createOrUpdateInterface(int index, QString name, bool fd_support); + GenericCanSetupPage *setupPage; +}; diff --git a/cpp/driver/SLCANDriver/SLCANDriver.pri b/cpp/driver/SLCANDriver/SLCANDriver.pri new file mode 100644 index 0000000..3562fab --- /dev/null +++ b/cpp/driver/SLCANDriver/SLCANDriver.pri @@ -0,0 +1,12 @@ +CONFIG += c++11 + +SOURCES += \ + $$PWD/SLCANInterface.cpp \ + $$PWD/SLCANDriver.cpp + +HEADERS += \ + $$PWD/SLCANInterface.h \ + $$PWD/SLCANDriver.h + +FORMS += + diff --git a/cpp/driver/SLCANDriver/SLCANInterface.cpp b/cpp/driver/SLCANDriver/SLCANInterface.cpp new file mode 100644 index 0000000..ff26ad0 --- /dev/null +++ b/cpp/driver/SLCANDriver/SLCANInterface.cpp @@ -0,0 +1,742 @@ +/* + + Copyright (c) 2022 Ethan Zonca + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "SLCANInterface.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +SLCANInterface::SLCANInterface(SLCANDriver *driver, int index, QString name, bool fd_support) + : CanInterface((CanDriver *)driver), + _idx(index), + _isOpen(false), + _serport(NULL), + _msg_queue(), + _name(name), + _rx_linbuf_ctr(0), + _rxbuf_head(0), + _rxbuf_tail(0), + _ts_mode(ts_mode_SIOCSHWTSTAMP) +{ + // Set defaults + _settings.setBitrate(500000); + _settings.setSamplePoint(875); + + _config.supports_canfd = fd_support; +} + +SLCANInterface::~SLCANInterface() { +} + +QString SLCANInterface::getDetailsStr() const { + if(_config.supports_canfd) + { + return "CANable with CANFD support"; + } + else + { + return "CANable with standard CAN support"; + } +} + +QString SLCANInterface::getName() const { + return _name; +} + +void SLCANInterface::setName(QString name) { + _name = name; +} + +QList SLCANInterface::getAvailableBitrates() +{ + QList retval; + QList bitrates({10000, 20000, 50000, 83333, 100000, 125000, 250000, 500000, 800000, 1000000}); + QList bitrates_fd({0, 2000000, 5000000}); + + QList samplePoints({875}); + + unsigned i=0; + foreach (unsigned br, bitrates) { + foreach(unsigned br_fd, bitrates_fd) { + foreach (unsigned sp, samplePoints) { + retval << CanTiming(i++, br, br_fd, sp); + } + } + } + + return retval; +} + + +void SLCANInterface::applyConfig(const MeasurementInterface &mi) +{ + // Save settings for port configuration + _settings = mi; +} + +bool SLCANInterface::updateStatus() +{ + return false; +} + +bool SLCANInterface::readConfig() +{ + return false; +} + +bool SLCANInterface::readConfigFromLink(rtnl_link *link) +{ + return false; +} + +bool SLCANInterface::supportsTimingConfiguration() +{ + return _config.supports_timing; +} + +bool SLCANInterface::supportsCanFD() +{ + return _config.supports_canfd; +} + +bool SLCANInterface::supportsTripleSampling() +{ + return false; +} + +unsigned SLCANInterface::getBitrate() +{ + return _settings.bitrate(); +} + +uint32_t SLCANInterface::getCapabilities() +{ + uint32_t retval = + CanInterface::capability_config_os | + CanInterface::capability_listen_only | + CanInterface::capability_auto_restart; + + if (supportsCanFD()) { + retval |= CanInterface::capability_canfd; + } + + if (supportsTripleSampling()) { + retval |= CanInterface::capability_triple_sampling; + } + + return retval; +} + +bool SLCANInterface::updateStatistics() +{ + return updateStatus(); +} + +uint32_t SLCANInterface::getState() +{ + if(_isOpen) + return state_ok; + else + return state_bus_off; +} + +int SLCANInterface::getNumRxFrames() +{ + return _status.rx_count; +} + +int SLCANInterface::getNumRxErrors() +{ + return _status.rx_errors; +} + +int SLCANInterface::getNumTxFrames() +{ + return _status.tx_count; +} + +int SLCANInterface::getNumTxErrors() +{ + return _status.tx_errors; +} + +int SLCANInterface::getNumRxOverruns() +{ + return _status.rx_overruns; +} + +int SLCANInterface::getNumTxDropped() +{ + return _status.tx_dropped; +} + +int SLCANInterface::getIfIndex() { + return _idx; +} + +void SLCANInterface::open() +{ + if(_serport != NULL) + { + delete _serport; + } + + _serport = new QSerialPort(); + + _serport_mutex.lock(); + _serport->setPortName(_name); + _serport->setBaudRate(1000000); + _serport->setDataBits(QSerialPort::Data8); + _serport->setParity(QSerialPort::NoParity); + _serport->setStopBits(QSerialPort::OneStop); + _serport->setFlowControl(QSerialPort::NoFlowControl); + _serport->setReadBufferSize(2048); + if (_serport->open(QIODevice::ReadWrite)) { + //perror("Serport connected!"); + } else { + perror("Serport connect failed!"); + _serport_mutex.unlock(); + _isOpen = false; + return; + } + _serport->flush(); + _serport->clear(); + + // Set the classic CAN bitrate + switch(_settings.bitrate()) + { + case 1000000: + _serport->write("S8\r", 3); + _serport->flush(); + break; + case 750000: + _serport->write("S7\r", 3); + _serport->flush(); + break; + case 500000: + _serport->write("S6\r", 3); + _serport->flush(); + break; + case 250000: + _serport->write("S5\r", 3); + _serport->flush(); + break; + case 125000: + _serport->write("S4\r", 3); + _serport->flush(); + break; + case 100000: + _serport->write("S3\r", 3); + _serport->flush(); + break; + case 83333: + _serport->write("S9\r", 3); + _serport->flush(); + break; + case 50000: + _serport->write("S2\r", 3); + _serport->flush(); + break; + case 20000: + _serport->write("S1\r", 3); + _serport->flush(); + break; + case 10000: + _serport->write("S0\r", 3); + _serport->flush(); + break; + default: + // Default to 10k + _serport->write("S0\r", 3); + _serport->flush(); + break; + } + + _serport->waitForBytesWritten(300); + + + + // Set configured BRS rate + if(_config.supports_canfd) + { + switch(_settings.fdBitrate()) + { + case 2000000: + _serport->write("Y2\r", 3); + _serport->flush(); + break; + case 5000000: + _serport->write("Y5\r", 3); + _serport->flush(); + break; + } + } + + _serport->waitForBytesWritten(300); + + + // Open the port + _serport->write("O\r\n", 3); + _serport->flush(); + + _isOpen = true; + + // Release port mutex + _serport_mutex.unlock(); +} + +void SLCANInterface::close() +{ + _serport_mutex.lock(); + + if (_serport->isOpen()) + { + // Close CAN port + _serport->write("C\r", 2); + _serport->flush(); + _serport->waitForBytesWritten(300); + _serport->clear(); + _serport->close(); + } + + _isOpen = false; + _serport_mutex.unlock(); +} + +bool SLCANInterface::isOpen() +{ + return _isOpen; +} + +void SLCANInterface::sendMessage(const CanMessage &msg) { + + // SLCAN_MTU plus null terminator + char buf[SLCAN_MTU+1] = {0}; + + uint8_t msg_idx = 0; + + // Message is FD + // Add character for frame type + if(msg.isFD()) + { + if(msg.isBRS()) + { + buf[msg_idx] = 'b'; + + } + else + { + buf[msg_idx] = 'd'; + } + } + // Message is not FD + // Add character for frame type + else + { + if (msg.isRTR()) { + buf[msg_idx] = 'r'; + } + else + { + buf[msg_idx] = 't'; + } + } + + // Assume standard identifier + uint8_t id_len = SLCAN_STD_ID_LEN; + uint32_t tmp = msg.getId(); + + // Check if extended + if (msg.isExtended()) + { + // Convert first char to upper case for extended frame + buf[msg_idx] -= 32; + id_len = SLCAN_EXT_ID_LEN; + } + msg_idx++; + + // Add identifier to buffer + for(uint8_t j = id_len; j > 0; j--) + { + // Add nibble to buffer + buf[j] = (tmp & 0xF); + tmp = tmp >> 4; + msg_idx++; + } + + // Sanity check length + int8_t bytes = msg.getLength(); + + + if(bytes < 0) + return; + if(bytes > 64) + return; + + // If canfd + if(bytes > 8) + { + switch(bytes) + { + case 12: + bytes = 0x9; + break; + case 16: + bytes = 0xA; + break; + case 20: + bytes = 0xB; + break; + case 24: + bytes = 0xC; + break; + case 32: + bytes = 0xD; + break; + case 48: + bytes = 0xE; + break; + case 64: + bytes = 0xF; + break; + } + } + + // Add DLC to buffer + buf[msg_idx++] = bytes; + + // Add data bytes + for (uint8_t j = 0; j < msg.getLength(); j++) + { + buf[msg_idx++] = (msg.getByte(j) >> 4); + buf[msg_idx++] = (msg.getByte(j) & 0x0F); + } + + // Convert to ASCII (2nd character to end) + for (uint8_t j = 1; j < msg_idx; j++) + { + if (buf[j] < 0xA) { + buf[j] += 0x30; + } else { + buf[j] += 0x37; + } + } + + // Add CR for slcan EOL + buf[msg_idx++] = '\r'; + + // Ensure null termination + buf[msg_idx] = '\0'; + + _msg_queue.append(QString(buf)); + +} + +bool SLCANInterface::readMessage(QList &msglist, unsigned int timeout_ms) +{ + // Don't saturate the thread. Read the buffer every 1ms. + QThread().msleep(1); + + // Transmit all items that are queued + while(!_msg_queue.empty()) + { + // Consume first item + QString tmp = _msg_queue.front(); + _msg_queue.pop_front(); + + _serport_mutex.lock(); + // Write string to serial device + _serport->write(tmp.toStdString().c_str(), tmp.length()); + _serport->flush(); + _serport->waitForBytesWritten(300); + _serport_mutex.unlock(); + } + + // RX doesn't work on windows unless we call this for some reason + _serport->waitForReadyRead(1); + + if(_serport->bytesAvailable()) + { + // This is called when readyRead() is emitted + QByteArray datas = _serport->readAll(); + _rxbuf_mutex.lock(); + for(int i=0; i= 'a') + _rx_linbuf[i] = _rx_linbuf[i] - 'a' + 10; + // Uppercase letters + else if(_rx_linbuf[i] >= 'A') + _rx_linbuf[i] = _rx_linbuf[i] - 'A' + 10; + // Numbers + else + _rx_linbuf[i] = _rx_linbuf[i] - '0'; + } + + + // Handle each incoming command + switch(_rx_linbuf[0]) + { + + // Transmit data frame command + case 'T': + msg.setExtended(1); + break; + case 't': + msg.setExtended(0); + break; + + // Transmit remote frame command + case 'r': + msg.setExtended(0); + msg.setRTR(1); + break; + case 'R': + msg.setExtended(1); + msg.setRTR(1); + break; + + // CANFD transmit - no BRS + case 'd': + msg.setExtended(0); + msg_is_fd = true; + break; + case 'D': + msg.setExtended(1); + msg_is_fd = true; + break; + + // CANFD transmit - with BRS + case 'b': + msg.setExtended(0); + msg_is_fd = true; + break; + case 'B': + msg.setExtended(1); + msg_is_fd = true; + break; + + + + // Invalid command + default: + return false; + } + + // Start parsing at second byte (skip command byte) + uint8_t parse_loc = 1; + + // Default to standard id len + uint8_t id_len = SLCAN_STD_ID_LEN; + + // Update length if message is extended ID + if(msg.isExtended()) + id_len = SLCAN_EXT_ID_LEN; + + uint32_t id_tmp = 0; + + // Iterate through ID bytes + while(parse_loc <= id_len) + { + id_tmp *= 16; + id_tmp += _rx_linbuf[parse_loc++]; + } + + + msg.setId(id_tmp); + + // Attempt to parse DLC and check sanity + uint8_t dlc_code_raw = _rx_linbuf[parse_loc++]; + + // If dlc is too long for an FD frame + if(msg_is_fd && dlc_code_raw > 0xF) + { + return false; + } + if(!msg_is_fd && dlc_code_raw > 0x8) + { + return false; + } + + if(dlc_code_raw > 0x8) + { + switch(dlc_code_raw) + { + case 0x9: + dlc_code_raw = 12; + break; + case 0xA: + dlc_code_raw = 16; + break; + case 0xB: + dlc_code_raw = 20; + break; + case 0xC: + dlc_code_raw = 24; + break; + case 0xD: + dlc_code_raw = 32; + break; + case 0xE: + dlc_code_raw = 48; + break; + case 0xF: + dlc_code_raw = 64; + break; + default: + dlc_code_raw = 0; + perror("Invalid length"); + break; + } + } + + msg.setLength(dlc_code_raw); + + // Calculate number of bytes we expect in the message + int8_t bytes_in_msg = dlc_code_raw; + + if(bytes_in_msg < 0) { + perror("Invalid length < 0"); + return false; + } + if(bytes_in_msg > 64) { + perror("Invalid length > 64"); + return false; + } + + // Parse data + // TODO: Guard against walking off the end of the string! + for (uint8_t i = 0; i < bytes_in_msg; i++) + { + msg.setByte(i, (_rx_linbuf[parse_loc] << 4) + _rx_linbuf[parse_loc+1]); + parse_loc += 2; + } + + // Reset buffer + _rx_linbuf_ctr = 0; + _rx_linbuf[0] = '\0'; + return true; + + +/* + + // FIXME + if (_ts_mode == ts_mode_SIOCSHWTSTAMP) { + // TODO implement me + _ts_mode = ts_mode_SIOCGSTAMPNS; + } + + if (_ts_mode==ts_mode_SIOCGSTAMPNS) { + if (ioctl(_fd, SIOCGSTAMPNS, &ts_rcv) == 0) { + msg.setTimestamp(ts_rcv.tv_sec, ts_rcv.tv_nsec/1000); + } else { + _ts_mode = ts_mode_SIOCGSTAMP; + } + } + + if (_ts_mode==ts_mode_SIOCGSTAMP) { + ioctl(_fd, SIOCGSTAMP, &tv_rcv); + msg.setTimestamp(tv_rcv.tv_sec, tv_rcv.tv_usec); + }*/ + + +} diff --git a/cpp/driver/SLCANDriver/SLCANInterface.h b/cpp/driver/SLCANDriver/SLCANInterface.h new file mode 100644 index 0000000..6f0b11f --- /dev/null +++ b/cpp/driver/SLCANDriver/SLCANInterface.h @@ -0,0 +1,133 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include "../CanInterface.h" +#include +#include +#include +#include + +// Maximum rx buffer len +#define SLCAN_MTU 138 + 1 + 16 // canfd 64 frame plus \r plus some padding +#define SLCAN_STD_ID_LEN 3 +#define SLCAN_EXT_ID_LEN 8 + +#define RXCIRBUF_LEN 8192 // Buffer for received serial data, serviced at 1ms intervals + +class SLCANDriver; + +typedef struct { + bool supports_canfd; + bool supports_timing; + uint32_t state; + uint32_t base_freq; + uint32_t sample_point; + uint32_t ctrl_mode; + uint32_t restart_ms; +} can_config_t; + +typedef struct { + uint32_t can_state; + + uint64_t rx_count; + int rx_errors; + uint64_t rx_overruns; + + uint64_t tx_count; + int tx_errors; + uint64_t tx_dropped; +} can_status_t; + +class SLCANInterface: public CanInterface { +public: + SLCANInterface(SLCANDriver *driver, int index, QString name, bool fd_support); + virtual ~SLCANInterface(); + + QString getDetailsStr() const; + virtual QString getName() const; + void setName(QString name); + + virtual QList getAvailableBitrates(); + + virtual void applyConfig(const MeasurementInterface &mi); + virtual bool readConfig(); + virtual bool readConfigFromLink(struct rtnl_link *link); + + bool supportsTimingConfiguration(); + bool supportsCanFD(); + bool supportsTripleSampling(); + + virtual unsigned getBitrate(); + virtual uint32_t getCapabilities(); + + + virtual void open(); + virtual void close(); + virtual bool isOpen(); + + virtual void sendMessage(const CanMessage &msg); + virtual bool readMessage(QList &msglist, unsigned int timeout_ms); + + virtual bool updateStatistics(); + virtual uint32_t getState(); + virtual int getNumRxFrames(); + virtual int getNumRxErrors(); + virtual int getNumRxOverruns(); + + virtual int getNumTxFrames(); + virtual int getNumTxErrors(); + virtual int getNumTxDropped(); + + int getIfIndex(); + +private: + typedef enum { + ts_mode_SIOCSHWTSTAMP, + ts_mode_SIOCGSTAMPNS, + ts_mode_SIOCGSTAMP + } ts_mode_t; + + int _idx; + bool _isOpen; + QSerialPort* _serport; + QStringList _msg_queue; + QMutex _serport_mutex; + QString _name; + char _rx_linbuf[SLCAN_MTU]; + int _rx_linbuf_ctr; + + char _rxbuf[RXCIRBUF_LEN]; + uint32_t _rxbuf_head; + uint32_t _rxbuf_tail; + + QMutex _rxbuf_mutex; + MeasurementInterface _settings; + + can_config_t _config; + can_status_t _status; + ts_mode_t _ts_mode; + + bool updateStatus(); + bool parseMessage(CanMessage &msg); + +}; diff --git a/cpp/driver/SocketCanDriver/SocketCanDriver.cpp b/cpp/driver/SocketCanDriver/SocketCanDriver.cpp new file mode 100644 index 0000000..c002854 --- /dev/null +++ b/cpp/driver/SocketCanDriver/SocketCanDriver.cpp @@ -0,0 +1,99 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + + +#include "SocketCanDriver.h" +#include "SocketCanInterface.h" +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +SocketCanDriver::SocketCanDriver(Backend &backend) + : CanDriver(backend), + setupPage(new GenericCanSetupPage()) +{ + QObject::connect(&backend, SIGNAL(onSetupDialogCreated(SetupDialog&)), setupPage, SLOT(onSetupDialogCreated(SetupDialog&))); +} + +SocketCanDriver::~SocketCanDriver() { +} + +bool SocketCanDriver::update() { + + deleteAllInterfaces(); + + struct nl_sock *sock = nl_socket_alloc(); + struct nl_cache *cache; + + nl_connect(sock, NETLINK_ROUTE); + int result = rtnl_link_alloc_cache(sock, AF_UNSPEC, &cache); + + if (result < 0) { + log_error(QString("Could not access netlink device list: %1").arg(result)); + } else { + + for (struct nl_object *obj = nl_cache_get_first(cache); obj!=0; obj=nl_cache_get_next(obj)) { + struct rtnl_link *link = (struct rtnl_link *)obj; + + if (rtnl_link_get_arptype(link)==ARPHRD_CAN) { + SocketCanInterface *intf = createOrUpdateInterface(rtnl_link_get_ifindex(link), QString(rtnl_link_get_name(link))); + intf->readConfigFromLink(link); + } + } + } + + nl_cache_free(cache); + nl_close(sock); + nl_socket_free(sock); + + return true; +} + +QString SocketCanDriver::getName() { + return "SocketCAN"; +} + +SocketCanInterface *SocketCanDriver::createOrUpdateInterface(int index, QString name) { + + foreach (CanInterface *intf, getInterfaces()) { + SocketCanInterface *scif = dynamic_cast(intf); + if (scif->getIfIndex() == index) { + scif->setName(name); + return scif; + } + } + + + SocketCanInterface *scif = new SocketCanInterface(this, index, name); + addInterface(scif); + return scif; +} diff --git a/cpp/driver/SocketCanDriver/SocketCanDriver.h b/cpp/driver/SocketCanDriver/SocketCanDriver.h new file mode 100644 index 0000000..4675942 --- /dev/null +++ b/cpp/driver/SocketCanDriver/SocketCanDriver.h @@ -0,0 +1,43 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include + +class SocketCanInterface; +class SetupDialogInterfacePage; +class GenericCanSetupPage; + +class SocketCanDriver: public CanDriver { +public: + SocketCanDriver(Backend &backend); + virtual ~SocketCanDriver(); + + virtual QString getName(); + virtual bool update(); + +private: + SocketCanInterface *createOrUpdateInterface(int index, QString name); + GenericCanSetupPage *setupPage; +}; diff --git a/cpp/driver/SocketCanDriver/SocketCanDriver.pri b/cpp/driver/SocketCanDriver/SocketCanDriver.pri new file mode 100644 index 0000000..390efee --- /dev/null +++ b/cpp/driver/SocketCanDriver/SocketCanDriver.pri @@ -0,0 +1,12 @@ +CONFIG += c++11 + +SOURCES += \ + $$PWD/SocketCanInterface.cpp \ + $$PWD/SocketCanDriver.cpp + +HEADERS += \ + $$PWD/SocketCanInterface.h \ + $$PWD/SocketCanDriver.h + +FORMS += + diff --git a/cpp/driver/SocketCanDriver/SocketCanInterface.cpp b/cpp/driver/SocketCanDriver/SocketCanInterface.cpp new file mode 100644 index 0000000..542a66b --- /dev/null +++ b/cpp/driver/SocketCanDriver/SocketCanInterface.cpp @@ -0,0 +1,472 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "SocketCanInterface.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +SocketCanInterface::SocketCanInterface(SocketCanDriver *driver, int index, QString name) + : CanInterface((CanDriver *)driver), + _idx(index), + _isOpen(false), + _fd(0), + _name(name), + _ts_mode(ts_mode_SIOCSHWTSTAMP) +{ +} + +SocketCanInterface::~SocketCanInterface() { +} + +QString SocketCanInterface::getName() const { + return _name; +} + +void SocketCanInterface::setName(QString name) { + _name = name; +} + +QList SocketCanInterface::getAvailableBitrates() +{ + QList retval; + QList bitrates({10000, 20000, 50000, 83333, 100000, 125000, 250000, 500000, 800000, 1000000}); + QList samplePoints({500, 625, 750, 875}); + + unsigned i=0; + foreach (unsigned br, bitrates) { + foreach (unsigned sp, samplePoints) { + retval << CanTiming(i++, br, 0, sp); + } + } + + return retval; +} + +QString SocketCanInterface::buildIpRouteCmd(const MeasurementInterface &mi) +{ + QStringList cmd; + cmd.append("ip"); + cmd.append("link"); + cmd.append("set"); + cmd.append(getName()); + cmd.append("up"); + cmd.append("type"); + cmd.append("can"); + + cmd.append("bitrate"); + cmd.append(QString().number(mi.bitrate())); + cmd.append("sample-point"); + cmd.append(QString().number((float)mi.samplePoint()/1000.0, 'f', 3)); + + if (mi.isCanFD()) { + cmd.append("dbitrate"); + cmd.append(QString().number(mi.fdBitrate())); + cmd.append("dsample-point"); + cmd.append(QString().number((float)mi.fdSamplePoint()/1000.0, 'f', 3)); + cmd.append("fd"); + cmd.append("on"); + } + + cmd.append("restart-ms"); + if (mi.doAutoRestart()) { + cmd.append(QString().number(mi.autoRestartMs())); + } else { + cmd.append("0"); + } + + return cmd.join(' '); +} + +QStringList SocketCanInterface::buildCanIfConfigArgs(const MeasurementInterface &mi) +{ + QStringList args; + args << "-d"; + args << "-i" << getName(); + args << "-b" << QString::number(mi.bitrate()); + args << "-p" << QString::number(mi.samplePoint()); + args << "-u"; + return args; +} + + +void SocketCanInterface::applyConfig(const MeasurementInterface &mi) +{ + if (!mi.doConfigure()) { + log_info(QString("interface %1 not managed by cangaroo, not touching configuration").arg(getName())); + return; + } + + log_info(QString("calling canifconfig to reconfigure interface %1").arg(getName())); + QStringList sl = buildCanIfConfigArgs(mi); + sl.prepend("canifconfig"); + log_info(sl.join(" ")); + + QProcess canIfConfig; + canIfConfig.start("canifconfig", buildCanIfConfigArgs(mi)); + if (!canIfConfig.waitForFinished()) { + log_error(QString("timeout waiting for canifconfig")); + return; + } + + if (canIfConfig.exitStatus()!=QProcess::NormalExit) { + log_error(QString("canifconfig crashed")); + return; + } + + if (canIfConfig.exitCode() != 0) { + log_error(QString("canifconfig failed: ") + QString(canIfConfig.readAllStandardError()).trimmed()); + return; + } + +} + +#if (LIBNL_CURRENT<=216) +#warning we need at least libnl3 version 3.2.22 to be able to get link status via netlink +int rtnl_link_can_state(struct rtnl_link *link, uint32_t *state) { + (void) link; + (void) state; + return -1; +} +#endif + +bool SocketCanInterface::updateStatus() +{ + bool retval = false; + + struct nl_sock *sock = nl_socket_alloc(); + struct nl_cache *cache; + struct rtnl_link *link; + uint32_t state; + + _status.can_state = state_unknown; + + nl_connect(sock, NETLINK_ROUTE); + if (rtnl_link_alloc_cache(sock, AF_UNSPEC, &cache) >= 0) { + if (rtnl_link_get_kernel(sock, _idx, 0, &link) == 0) { + + _status.rx_count = rtnl_link_get_stat(link, RTNL_LINK_RX_PACKETS); + _status.rx_overruns = rtnl_link_get_stat(link, RTNL_LINK_RX_OVER_ERR); + _status.tx_count = rtnl_link_get_stat(link, RTNL_LINK_TX_PACKETS); + _status.tx_dropped = rtnl_link_get_stat(link, RTNL_LINK_TX_DROPPED); + + if (rtnl_link_is_can(link)) { + if (rtnl_link_can_state(link, &state)==0) { + _status.can_state = state; + } + _status.rx_errors = rtnl_link_can_berr_rx(link); + _status.tx_errors = rtnl_link_can_berr_tx(link); + } else { + _status.rx_errors = 0; + _status.tx_errors = 0; + } + retval = true; + } + } + + nl_cache_free(cache); + nl_close(sock); + nl_socket_free(sock); + + return retval; +} + +bool SocketCanInterface::readConfig() +{ + bool retval = false; + + struct nl_sock *sock = nl_socket_alloc(); + struct nl_cache *cache; + struct rtnl_link *link; + + nl_connect(sock, NETLINK_ROUTE); + int result = rtnl_link_alloc_cache(sock, AF_UNSPEC, &cache); + + if (result>=0) { + if (rtnl_link_get_kernel(sock, _idx, 0, &link) == 0) { + retval = readConfigFromLink(link); + } + } + + nl_cache_free(cache); + nl_close(sock); + nl_socket_free(sock); + + return retval; +} + +bool SocketCanInterface::readConfigFromLink(rtnl_link *link) +{ + _config.state = state_unknown; + _config.supports_canfd = (rtnl_link_get_mtu(link)==72); + _config.supports_timing = rtnl_link_is_can(link); + if (_config.supports_timing) { + rtnl_link_can_freq(link, &_config.base_freq); + rtnl_link_can_get_ctrlmode(link, &_config.ctrl_mode); + rtnl_link_can_get_bittiming(link, &_config.bit_timing); + rtnl_link_can_get_sample_point(link, &_config.sample_point); + rtnl_link_can_get_restart_ms(link, &_config.restart_ms); + } else { + // maybe a vcan interface? + } + return true; +} + +bool SocketCanInterface::supportsTimingConfiguration() +{ + return _config.supports_timing; +} + +bool SocketCanInterface::supportsCanFD() +{ + return _config.supports_canfd; +} + +bool SocketCanInterface::supportsTripleSampling() +{ + return false; +} + +unsigned SocketCanInterface::getBitrate() { + if (readConfig()) { + return _config.bit_timing.bitrate; + } else { + return 0; + } +} + +uint32_t SocketCanInterface::getCapabilities() +{ + uint32_t retval = + CanInterface::capability_config_os | + CanInterface::capability_listen_only | + CanInterface::capability_auto_restart; + + if (supportsCanFD()) { + retval |= CanInterface::capability_canfd; + } + + if (supportsTripleSampling()) { + retval |= CanInterface::capability_triple_sampling; + } + + return retval; +} + +bool SocketCanInterface::updateStatistics() +{ + return updateStatus(); +} + +uint32_t SocketCanInterface::getState() +{ + switch (_status.can_state) { + case CAN_STATE_ERROR_ACTIVE: return state_ok; + case CAN_STATE_ERROR_WARNING: return state_warning; + case CAN_STATE_ERROR_PASSIVE: return state_passive; + case CAN_STATE_BUS_OFF: return state_bus_off; + case CAN_STATE_STOPPED: return state_stopped; + default: return state_unknown; + } +} + +int SocketCanInterface::getNumRxFrames() +{ + return _status.rx_count; +} + +int SocketCanInterface::getNumRxErrors() +{ + return _status.rx_errors; +} + +int SocketCanInterface::getNumTxFrames() +{ + return _status.tx_count; +} + +int SocketCanInterface::getNumTxErrors() +{ + return _status.tx_errors; +} + +int SocketCanInterface::getNumRxOverruns() +{ + return _status.rx_overruns; +} + +int SocketCanInterface::getNumTxDropped() +{ + return _status.tx_dropped; +} + +int SocketCanInterface::getIfIndex() { + return _idx; +} + +const char *SocketCanInterface::cname() +{ + return _name.toStdString().c_str(); +} + +void SocketCanInterface::open() { + if((_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { + perror("Error while opening socket"); + _isOpen = false; + } + + struct ifreq ifr; + struct sockaddr_can addr; + strcpy(ifr.ifr_name, _name.toStdString().c_str()); + ioctl(_fd, SIOCGIFINDEX, &ifr); + + addr.can_family = AF_CAN; + addr.can_ifindex = ifr.ifr_ifindex; + + if(bind(_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + perror("Error in socket bind"); + _isOpen = false; + } + + _isOpen = true; +} + +bool SocketCanInterface::isOpen() +{ + return _isOpen; +} + +void SocketCanInterface::close() { + ::close(_fd); + _isOpen = false; +} + +void SocketCanInterface::sendMessage(const CanMessage &msg) { + struct can_frame frame; + + frame.can_id = msg.getId(); + + if (msg.isExtended()) { + frame.can_id |= CAN_EFF_FLAG; + } + + if (msg.isRTR()) { + frame.can_id |= CAN_RTR_FLAG; + } + + if (msg.isErrorFrame()) { + frame.can_id |= CAN_ERR_FLAG; + } + + uint8_t len = msg.getLength(); + if (len>8) { len = 8; } + + frame.can_dlc = len; + for (int i=0; i &msglist, unsigned int timeout_ms) { + + struct can_frame frame; + struct timespec ts_rcv; + struct timeval tv_rcv; + struct timeval timeout; + //struct ifreq hwtstamp; + fd_set fdset; + + timeout.tv_sec = timeout_ms / 1000; + timeout.tv_usec = 1000 * (timeout_ms % 1000); + + FD_ZERO(&fdset); + FD_SET(_fd, &fdset); + + CanMessage msg; + + int rv = select(_fd+1, &fdset, NULL, NULL, &timeout); + if (rv>0) { + + if (read(_fd, &frame, sizeof(struct can_frame)) < 0) { + return false; + } + + if (_ts_mode == ts_mode_SIOCSHWTSTAMP) { + // TODO implement me + _ts_mode = ts_mode_SIOCGSTAMPNS; + } + + if (_ts_mode==ts_mode_SIOCGSTAMPNS) { + if (ioctl(_fd, SIOCGSTAMPNS, &ts_rcv) == 0) { + msg.setTimestamp(ts_rcv.tv_sec, ts_rcv.tv_nsec/1000); + } else { + _ts_mode = ts_mode_SIOCGSTAMP; + } + } + + if (_ts_mode==ts_mode_SIOCGSTAMP) { + ioctl(_fd, SIOCGSTAMP, &tv_rcv); + msg.setTimestamp(tv_rcv.tv_sec, tv_rcv.tv_usec); + } + + msg.setId(frame.can_id); + msg.setExtended((frame.can_id & CAN_EFF_FLAG)!=0); + msg.setRTR((frame.can_id & CAN_RTR_FLAG)!=0); + msg.setErrorFrame((frame.can_id & CAN_ERR_FLAG)!=0); + msg.setInterfaceId(getId()); + + uint8_t len = frame.can_dlc; + if (len>8) { len = 8; } + + msg.setLength(len); + for (int i=0; i. + +*/ + +#pragma once + +#include "../CanInterface.h" +#include + +class SocketCanDriver; + +typedef struct { + bool supports_canfd; + bool supports_timing; + uint32_t state; + uint32_t base_freq; + uint32_t sample_point; + uint32_t ctrl_mode; + uint32_t restart_ms; + struct can_bittiming bit_timing; +} can_config_t; + +typedef struct { + uint32_t can_state; + + uint64_t rx_count; + int rx_errors; + uint64_t rx_overruns; + + uint64_t tx_count; + int tx_errors; + uint64_t tx_dropped; +} can_status_t; + +class SocketCanInterface: public CanInterface { +public: + SocketCanInterface(SocketCanDriver *driver, int index, QString name); + virtual ~SocketCanInterface(); + + virtual QString getName() const; + void setName(QString name); + + virtual QList getAvailableBitrates(); + + virtual void applyConfig(const MeasurementInterface &mi); + virtual bool readConfig(); + virtual bool readConfigFromLink(struct rtnl_link *link); + + bool supportsTimingConfiguration(); + bool supportsCanFD(); + bool supportsTripleSampling(); + + virtual unsigned getBitrate(); + virtual uint32_t getCapabilities(); + + + virtual void open(); + virtual bool isOpen(); + virtual void close(); + + virtual void sendMessage(const CanMessage &msg); + virtual bool readMessage(QList &msglist, unsigned int timeout_ms); + + virtual bool updateStatistics(); + virtual uint32_t getState(); + virtual int getNumRxFrames(); + virtual int getNumRxErrors(); + virtual int getNumRxOverruns(); + + virtual int getNumTxFrames(); + virtual int getNumTxErrors(); + virtual int getNumTxDropped(); + + + int getIfIndex(); + +private: + typedef enum { + ts_mode_SIOCSHWTSTAMP, + ts_mode_SIOCGSTAMPNS, + ts_mode_SIOCGSTAMP + } ts_mode_t; + + int _idx; + bool _isOpen; + int _fd; + QString _name; + + can_config_t _config; + can_status_t _status; + ts_mode_t _ts_mode; + + const char *cname(); + bool updateStatus(); + + QString buildIpRouteCmd(const MeasurementInterface &mi); + QStringList buildCanIfConfigArgs(const MeasurementInterface &mi); +}; diff --git a/cpp/driver/driver.pri b/cpp/driver/driver.pri new file mode 100644 index 0000000..9d24491 --- /dev/null +++ b/cpp/driver/driver.pri @@ -0,0 +1,16 @@ +SOURCES += \ + $$PWD/CanInterface.cpp \ + $$PWD/CanListener.cpp \ + $$PWD/CanDriver.cpp \ + $$PWD/CanTiming.cpp \ + $$PWD/GenericCanSetupPage.cpp + +HEADERS += \ + $$PWD/CanInterface.h \ + $$PWD/CanListener.h \ + $$PWD/CanDriver.h \ + $$PWD/CanTiming.h \ + $$PWD/GenericCanSetupPage.h + +FORMS += \ + $$PWD/GenericCanSetupPage.ui diff --git a/cpp/main.cpp b/cpp/main.cpp new file mode 100644 index 0000000..3e3cd2f --- /dev/null +++ b/cpp/main.cpp @@ -0,0 +1,71 @@ +#include +#include + +#include +#include +#include + +#include "CanController.h" + +int main(int argc, char *argv[]) +{ +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); +#endif + QApplication app(argc, argv); + + QCoreApplication::setOrganizationName("Vku"); + QCoreApplication::setOrganizationDomain("Vku.rus"); + QCoreApplication::setApplicationName("Vku monitor"); + + QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-Black.ttf"); + QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-BlackItalic.ttf"); + QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-Bold.ttf"); + QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-BoldItalic.ttf"); + QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-Italic.ttf"); + QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-Light.ttf"); + QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-LightItalic.ttf"); + QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-Medium.ttf"); + QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-MediumItalic.ttf"); + QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-Regular.ttf"); + QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-Thin.ttf"); + QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-ThinItalic.ttf"); + + // QTranslator translator; + // const QStringList uiLanguages = QLocale::system().uiLanguages(); + // for (const QString &locale : uiLanguages) + // { + // const QString baseName = "VkuMonitor_" + QLocale(locale).name(); + // if (translator.load(":/i18n/" + baseName)) + // { + // app.installTranslator(&translator); + // break; + // } + // } + + QTranslator translator; + if (translator.load(":/i18n/VkuMonitor_ru_RU")) + { + app.installTranslator(&translator); + } + + QQmlApplicationEngine engine; + + qmlRegisterSingletonType("Vku", 1, 0, "CanController", &CanController::qmlInstance); + + const QUrl url(QStringLiteral("qrc:/main.qml")); + QObject::connect( + &engine, + &QQmlApplicationEngine::objectCreated, + &app, + [url](QObject *obj, const QUrl &objUrl) { + if (!obj && url == objUrl) + QCoreApplication::exit(-1); + }, + Qt::QueuedConnection); + + engine.addImportPath("qrc:/"); + engine.load(url); + + return app.exec(); +} diff --git a/cpp/parser/dbc/DbcParser.cpp b/cpp/parser/dbc/DbcParser.cpp new file mode 100644 index 0000000..f12f119 --- /dev/null +++ b/cpp/parser/dbc/DbcParser.cpp @@ -0,0 +1,688 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "DbcParser.h" +#include +#include +#include +#include +#include + +#include "DbcTokens.h" + +DbcParser::DbcParser() + : _errorLine(0), _errorColumn(0) +{ +} + +bool DbcParser::parseFile(QFile *file, CanDb &candb) +{ + DbcTokenList tokens; + if (tokenize(file, tokens) != err_ok) { + QString msg = QString("error parsing dbc file %1").arg(file->fileName()); + if (_errorLine) { + msg += QString(" at line %1, column %2").arg(_errorLine).arg(_errorColumn); + } + log_error(msg); + return false; + } + + candb.setPath(file->fileName()); + return parse(candb, tokens); +} + +DbcToken *DbcParser::createNewToken(QChar ch, int line, int column) +{ + static const QString acceptableIdStartChars("ABCDEFGHIKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"); + static const QRegExp numberRegExp("^(\\d+(\\.\\d*)?(E[-+]?\\d*)?)$"); + + if (ch.isSpace()) { + return new DbcWhitespaceToken(line, column); + } else if (ch.isDigit()) { + return new DbcRegExpToken(line, column, dbc_tok_number, numberRegExp); + } else if (ch == '"') { + return new DbcStringToken(line, column); + } else if (ch == ':') { + return new DbcSingleCharToken(line, column, dbc_tok_colon, ':'); + } else if (ch == '|') { + return new DbcSingleCharToken(line, column, dbc_tok_pipe, '|'); + } else if (ch == '@') { + return new DbcSingleCharToken(line, column, dbc_tok_at, '@'); + } else if (ch == '+') { + return new DbcSingleCharToken(line, column, dbc_tok_plus, '+'); + } else if (ch == '-') { + return new DbcSingleCharToken(line, column, dbc_tok_minus, '-'); + } else if (ch == '(') { + return new DbcSingleCharToken(line, column, dbc_tok_parenth_open, '('); + } else if (ch == ')') { + return new DbcSingleCharToken(line, column, dbc_tok_parenth_close, ')'); + } else if (ch == '[') { + return new DbcSingleCharToken(line, column, dbc_tok_bracket_open, '['); + } else if (ch == ']') { + return new DbcSingleCharToken(line, column, dbc_tok_bracket_close, ']'); + } else if (ch == ',') { + return new DbcSingleCharToken(line, column, dbc_tok_comma, ','); + } else if (ch == ';') { + return new DbcSingleCharToken(line, column, dbc_tok_semicolon, ';'); + } else if (acceptableIdStartChars.contains(ch)) { + return new DbcIdentifierToken(line, column); + } else { + return 0; + } +} + +DbcParser::error_t DbcParser::tokenize(QFile *file, DbcParser::DbcTokenList &tokens) +{ + + if (!file->open(QIODevice::ReadOnly)) { + // TODO raise cannot open file exception + return err_cannot_open_file; + } + + DbcToken *currentToken = 0; + int line = 1; + int column = 0; + + error_t retval = err_ok; + + QTextStream in(file); + in.setCodec("ISO 8859-1"); + + while (true) { + QString s = in.read(1); + if (s.isEmpty()) { break; } + + QChar ch = s[0]; + + if (ch=='\n') { + line++; + column=1; + } else { + column++; + } + + if (currentToken) { + if (!currentToken->appendChar(ch)) { + tokens.append(currentToken); + currentToken = 0; + } + } + + if (!currentToken) { + currentToken = createNewToken(ch, line, column); + if (currentToken) { + currentToken->appendChar(ch); + } else { + retval = err_tokenize_error; + _errorColumn = column; + _errorLine = line; + break; + } + } + + } + + file->close(); + return retval; +} + +bool DbcParser::isSectionEnding(DbcToken *token, bool newLineIsSectionEnding) +{ + if (!token) { + return true; + } else { + int numNewLinesForEnding = newLineIsSectionEnding ? 1 : 2; + dbc_token_type_t type = token->getType(); + return ( (type==dbc_tok_semicolon) || ( (type==dbc_tok_whitespace) && (token->countLineBreaks()>=numNewLinesForEnding))); + } +} + +DbcToken *DbcParser::readToken(DbcParser::DbcTokenList &tokens, int typeMask, bool skipWhitespace, bool skipSectionEnding, bool newLineIsSectionEnding) +{ + while (true) { + if (tokens.isEmpty()) { return 0; } + + DbcToken *token = tokens.first(); + dbc_token_type_t type = token->getType(); + + if (type & typeMask) { + + tokens.pop_front(); + return token; + + } else if (isSectionEnding(token, newLineIsSectionEnding)) { + + if (skipSectionEnding) { + tokens.pop_front(); + free(token); + continue; + } else { + return 0; + } + + } else if (skipWhitespace && (type==dbc_tok_whitespace)) { + + tokens.pop_front(); + free(token); + continue; + + } else { + + return 0; + + } + } + + return 0; +} + +bool DbcParser::expectSectionEnding(DbcTokenList &tokens, bool newLineIsSectionEnding) +{ + if (tokens.isEmpty()) { + return true; + } + + DbcToken *token = readToken(tokens, dbc_tok_whitespace|dbc_tok_semicolon); + if (!token) { + return false; + } + + if (!isSectionEnding(token, newLineIsSectionEnding)) { + free(token); + return false; + } else { + free(token); + return true; + } + +} + +bool DbcParser::expectLineBreak(DbcParser::DbcTokenList &tokens) +{ + bool found_line_break; + + DbcToken *token = readToken(tokens, dbc_tok_whitespace); + if (token) { + found_line_break = token->countLineBreaks()>0; + free(token); + } else { + found_line_break = false; + } + + return found_line_break; +} + +bool DbcParser::expectAndSkipToken(DbcTokenList &tokens, dbc_token_type_t type, bool skipWhitespace, bool skipSectionEnding) +{ + DbcToken *token = readToken(tokens, type, skipWhitespace, skipSectionEnding); + if (!token) { + return false; + } else { + free(token); + return true; + } +} + +bool DbcParser::expectData(DbcParser::DbcTokenList &tokens, dbc_token_type_t type, QString *data, bool skipWhitespace, bool skipSectionEnding, bool newLineIsSectionEnding) +{ + DbcToken *token; + if (!(token = readToken(tokens, type, skipWhitespace, skipSectionEnding, newLineIsSectionEnding))) { + return false; + } + + if (data) { + data->clear(); + data->append(token->getData()); + } + + free(token); + return true; +} + +bool DbcParser::expectIdentifier(DbcParser::DbcTokenList &tokens, QString *id, bool skipWhitespace, bool skipSectionEnding, bool newLineIsSectionEnding) +{ + return expectData(tokens, dbc_tok_identifier, id, skipWhitespace, skipSectionEnding, newLineIsSectionEnding); +} + +bool DbcParser::expectString(DbcParser::DbcTokenList &tokens, QString *str, bool skipWhitespace) +{ + QString quotedStr; + bool ok = expectData(tokens, dbc_tok_string, "edStr, skipWhitespace); + if (ok && quotedStr.length()>=2) { + *str = quotedStr.mid(1, quotedStr.length()-2); + return true; + } else { + return false; + } +} + +bool DbcParser::expectNumber(DbcParser::DbcTokenList &tokens, QString *str, bool skipWhitespace) +{ + QString data; + if (expectAndSkipToken(tokens, dbc_tok_minus, skipWhitespace)) { + *str = "-"; + } else if (expectAndSkipToken(tokens, dbc_tok_plus, skipWhitespace)) { + *str = "+"; + } + + if (expectData(tokens, dbc_tok_number, &data, skipWhitespace)) { + *str += data; + return true; + } else { + return false; + } + +} + +bool DbcParser::expectInt(DbcParser::DbcTokenList &tokens, int *i, int base, bool skipWhitespace) +{ + QString data; + if (!expectNumber(tokens, &data, skipWhitespace)) { + return false; + } + + bool convert_ok; + *i = data.toInt(&convert_ok, base); + return convert_ok; +} + +bool DbcParser::expectLongLong(DbcTokenList &tokens, long long *i, int base, bool skipWhitespace) +{ + QString data; + if (!expectNumber(tokens, &data, skipWhitespace)) { + return false; + } + + bool convert_ok; + *i = data.toLongLong(&convert_ok, base); + return convert_ok; +} + +bool DbcParser::expectDouble(DbcTokenList &tokens, double *df, bool skipWhitespace) +{ + QString data; + if (!expectNumber(tokens, &data, skipWhitespace)) { + return false; + } + + bool convert_ok; + *df = data.toDouble(&convert_ok); + return convert_ok; +} + +void DbcParser::skipUntilSectionEnding(DbcTokenList &tokens) +{ + while (!tokens.isEmpty()) { + DbcToken *token = readToken(tokens, dbc_tok_ALL, false, false); + if (!token) { return; } + if (isSectionEnding(token)) { + free(token); + return; + } else { + free(token); + } + } +} + +bool DbcParser::parseIdentifierList(DbcTokenList &tokens, QStringList *list, bool newLineIsSectionEnding) +{ + if (!expectAndSkipToken(tokens, dbc_tok_colon)) { + return false; + } + + QString id; + while (expectIdentifier(tokens, &id, true, false, newLineIsSectionEnding)) { + if (list) { + list->append(id); + } + } + + return expectSectionEnding(tokens, newLineIsSectionEnding); +} + +bool DbcParser::parse(CanDb &candb, DbcTokenList &tokens) +{ + _dbcVersion.clear(); + _nsEntries.clear(); + _buEntries.clear(); + + while (!tokens.isEmpty()) { + if (!parseSection(candb, tokens)) { + return false; + } + } + + return true; +} + +bool DbcParser::parseSection(CanDb &candb, DbcTokenList &tokens) { + bool retval = true; + + QString sectionName; + QStringList strings; + + while (retval) { + + if (tokens.isEmpty()) { + break; + } + + if (expectIdentifier(tokens, §ionName, true, true)) { + if (sectionName == "VERSION") { + retval &= parseSectionVersion(candb, tokens); + } else if (sectionName == "NS_") { + strings.clear(); + retval &= parseIdentifierList(tokens, &strings); + } else if (sectionName == "BS_") { + retval &= parseSectionBs(tokens); + } else if (sectionName == "BU_") { + retval &= parseSectionBu(candb, tokens); + } else if (sectionName == "BO_") { + retval &= parseSectionBo(candb, tokens); + } else if (sectionName == "CM_") { + retval &= parseSectionCm(candb, tokens); + } else if (sectionName == "VAL_") { + retval &= parseSectionVal(candb, tokens); + } else { + skipUntilSectionEnding(tokens); + } + + } else { + retval = false; + } + + } + + if (!retval) { + log_error("dbc parse error"); + } + return retval; + + /* + if (sectionName == "BA_") { return tokSectionBa; } + if (sectionName == "BA_REL_") { return tokSectionBaRel; } + if (sectionName == "BA_DEF_") { return tokSectionBaDef; } + if (sectionName == "BA_DEF_DEF_") { return tokSectionBaDefDef; } + if (sectionName == "BA_DEF_DEF_REL_") { return tokSectionBaDefDefRel; } +*/ +} + +bool DbcParser::parseSectionVersion(CanDb &candb, DbcTokenList &tokens) +{ + QString version; + if (!expectString(tokens, &version)) { return false; } + candb.setVersion(version); + return expectSectionEnding(tokens); +} + +bool DbcParser::parseSectionBs(DbcParser::DbcTokenList &tokens) +{ + if (!expectAndSkipToken(tokens, dbc_tok_colon)) { + return false; + } + + return expectSectionEnding(tokens); +} + +bool DbcParser::parseSectionBu(CanDb &candb, DbcParser::DbcTokenList &tokens) +{ + QStringList strings; + QString s; + + if (!parseIdentifierList(tokens, &strings, true)) { + return false; + } + + foreach(s, strings) { + candb.getOrCreateNode(s); + } + + return true; +} + + +bool DbcParser::parseSectionBo(CanDb &candb, DbcTokenList &tokens) +{ + long long can_id; + int dlc; + QString msg_name; + QString sender; + + if (!expectLongLong(tokens, &can_id)) { return false; } + if (!expectIdentifier(tokens, &msg_name)) { return false; } + if (!expectAndSkipToken(tokens, dbc_tok_colon)) { return false; } + if (!expectInt(tokens, &dlc)) { return false; } + if (!expectIdentifier(tokens, &sender)) { return false; } + + CanDbMessage *msg = new CanDbMessage(&candb); + msg->setRaw_id(can_id); + msg->setName(msg_name); + msg->setDlc(dlc); + msg->setSender(candb.getOrCreateNode(sender)); + candb.addMessage(msg); + + QString subsect; + while (true) { + if (expectSectionEnding(tokens)) { + return true; + } else { + if (!expectIdentifier(tokens, &subsect)) { + return false; + } + + if (subsect!="SG_") { + return false; + } + + if (!parseSectionBoSg(candb, msg, tokens)) { + return false; + } + } + } + + +} + +bool DbcParser::parseSectionBoSg(CanDb &candb, CanDbMessage *msg, DbcTokenList &tokens) +{ + (void)candb; + + QString signal_name; + QString mux_indicator; + int start_bit = 0; + int length = 0; + int byte_order = 0; + double factor = 1; + double offset = 0; + double minimum = 0; + double maximum = 0; + QString unit; + QString receiver; + QStringList receivers; + + CanDbSignal *signal = new CanDbSignal(msg); + msg->addSignal(signal); + + if (!expectIdentifier(tokens, &signal_name)) { return false; } + signal->setName(signal_name); + + + if (expectIdentifier(tokens, &mux_indicator)) { + if (mux_indicator=="M") { + signal->setIsMuxer(true); + msg->setMuxer(signal); + } else if (mux_indicator.startsWith('m')) { + signal->setIsMuxed(true); + bool ok; + signal->setMuxValue(mux_indicator.mid(1).toUInt(&ok)); + if (!ok) { return false; } + } else { + return false; + } + } + + if (!expectAndSkipToken(tokens, dbc_tok_colon)) { return false; } + if (!expectInt(tokens, &start_bit)) { return false; } + + signal->setStartBit(start_bit); + + if (!expectAndSkipToken(tokens, dbc_tok_pipe)) { return false; } + if (!expectInt(tokens, &length)) { return false; } + signal->setLength(length); + + if (!expectAndSkipToken(tokens, dbc_tok_at)) { return false; } + if (!expectInt(tokens, &byte_order)) { return false; } + signal->setIsBigEndian(byte_order==0); + + // If the signal is big endian, convert the start bit to the Intel-style start bit for further parsing + if(signal->isBigEndian()) + { + // This will be the number of 8-bit rows above the message + uint8_t row_position = signal->startBit() >> 3; + + // Bit position in current row (0-7) + uint8_t column_position = signal->startBit() & 0b111; + + // Calcualte the normalized start bit position (bit index starting at 0) + uint8_t normalized_position = (row_position * 8) + (7 - column_position); + + signal->setStartBit(normalized_position); + } + + if (expectAndSkipToken(tokens, dbc_tok_plus)) { + signal->setUnsigned(true); + } else { + if (expectAndSkipToken(tokens, dbc_tok_minus)) { + signal->setUnsigned(false); + } else { + return false; + } + } + + if (!expectAndSkipToken(tokens, dbc_tok_parenth_open)) { return false; } + if (!expectDouble(tokens, &factor)) { return false; } + signal->setFactor(factor); + if (!expectAndSkipToken(tokens, dbc_tok_comma)) { return false; } + if (!expectDouble(tokens, &offset)) { return false; } + signal->setOffset(offset); + if (!expectAndSkipToken(tokens, dbc_tok_parenth_close)) { return false; } + + if (!expectAndSkipToken(tokens, dbc_tok_bracket_open)) { return false; } + if (!expectDouble(tokens, &minimum)) { return false; } + signal->setMinimumValue(minimum); + if (!expectAndSkipToken(tokens, dbc_tok_pipe)) { return false; } + if (!expectDouble(tokens, &maximum)) { return false; } + signal->setMaximumValue(maximum); + if (!expectAndSkipToken(tokens, dbc_tok_bracket_close)) { return false; } + + if (!expectString(tokens, &unit)) { return false; } + signal->setUnit(unit); + + if (!expectIdentifier(tokens, &receiver)) { return false; } + receivers.append(receiver); + + while (expectAndSkipToken(tokens, dbc_tok_comma, false, false)) { + if (!expectIdentifier(tokens, &receiver)) { return false; } + receivers.append(receiver); + } + + + return true; +} + +bool DbcParser::parseSectionCm(CanDb &candb, DbcParser::DbcTokenList &tokens) +{ + QString s; + QString idtype; + QString id; + long long ll; + + if (expectString(tokens, &s)) { // DBC file comment + candb.setComment(s); + return true; + } + + if (!expectIdentifier(tokens, &idtype)) { return false; } + + if (idtype=="BU_") { + + if (!expectIdentifier(tokens, &id)) { return false; } + if (!expectString(tokens, &s)) { return false; } + candb.getOrCreateNode(id)->setComment(s); + return expectSectionEnding(tokens); + + } else if (idtype=="BO_") { + + if (!expectLongLong(tokens, &ll)) { return false; } + if (!expectString(tokens, &s)) { return false; } + CanDbMessage *msg = candb.getMessageById(ll); + if (!msg) { return false; } + msg->setComment(s); + return expectSectionEnding(tokens); + + } else if (idtype=="SG_") { + + if (!expectLongLong(tokens, &ll)) { return false; } + CanDbMessage *msg = candb.getMessageById(ll); + if (!msg) { return false; } + + if (!expectIdentifier(tokens, &id)) { return false; } + CanDbSignal *signal = msg->getSignalByName(id); + if (!signal) { return false; } + + if (!expectString(tokens, &s)) { return false; } + signal->setComment(s); + + return expectSectionEnding(tokens); + + } else { + + return false; + + } + + +} + +bool DbcParser::parseSectionVal(CanDb &candb, DbcParser::DbcTokenList &tokens) +{ + long long can_id; + QString signal_id; + long long value; + QString name; + + if (!expectLongLong(tokens, &can_id)) { return false; } + CanDbMessage *msg = candb.getMessageById(can_id); + if (!msg) { return false; } + + if (!expectIdentifier(tokens, &signal_id)) { return false; } + CanDbSignal *signal = msg->getSignalByName(signal_id); + if (!signal) { return false; } + + while (!expectAndSkipToken(tokens, dbc_tok_semicolon)) { + if (!expectLongLong(tokens, &value)) { return false; } + if (!expectString(tokens, &name)) { return false; } + signal->setValueName(value, name); + } + + return true; +} + diff --git a/cpp/parser/dbc/DbcParser.h b/cpp/parser/dbc/DbcParser.h new file mode 100644 index 0000000..0065d05 --- /dev/null +++ b/cpp/parser/dbc/DbcParser.h @@ -0,0 +1,92 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include +#include + +#include + +#include "DbcTokens.h" + +class CanDbMessage; + +class DbcParser +{ + +public: + typedef QList DbcTokenList; + + typedef enum { + err_ok, + err_cannot_open_file, + err_tokenize_error, + } error_t; + +public: + DbcParser(); + bool parseFile(QFile *file, CanDb &candb); + +private: + int _errorLine; + int _errorColumn; + + QString _dbcVersion; + QStringList _nsEntries; + QStringList _buEntries; + + DbcToken *createNewToken(QChar ch, int line, int column); + error_t tokenize(QFile *file, DbcTokenList &tokens); + + bool isSectionEnding(DbcToken *token, bool newLineIsSectionEnding=false); + bool expectSectionEnding(DbcTokenList &tokens, bool newLineIsSectionEnding=false); + bool expectLineBreak(DbcTokenList &tokens); + bool expectAndSkipToken(DbcTokenList &tokens, dbc_token_type_t type, bool skipWhitespace=true, bool skipSectionEnding=false); + + bool expectData(DbcTokenList &tokens, dbc_token_type_t type, QString *data, bool skipWhitespace=true, bool skipSectionEnding=false, bool newLineIsSectionEnding=false); + bool expectIdentifier(DbcTokenList &tokens, QString *id, bool skipWhitespace=true, bool skipSectionEnding=false, bool newLineIsSectionEnding=false); + bool expectString(DbcTokenList &tokens, QString *str, bool skipWhitespace=true); + + bool expectNumber(DbcTokenList &tokens, QString *str, bool skipWhitespace=true); + + bool expectInt(DbcTokenList &tokens, int *i, int base=10, bool skipWhitespace=true); + bool expectLongLong(DbcTokenList &tokens, long long *i, int base=10, bool skipWhitespace=true); + bool expectDouble(DbcTokenList &tokens, double *df, bool skipWhitespace=true); + void skipUntilSectionEnding(DbcTokenList &tokens); + + DbcToken *readToken(DbcTokenList &tokens, int typeMask, bool skipWhitespace=true, bool skipSectionEnding=false, bool newLineIsSectionEnding=false); + + bool parse(CanDb &candb, DbcTokenList &tokens); + bool parseIdentifierList(DbcTokenList &tokens, QStringList *list, bool newLineIsSectionEnding=false); + + bool parseSection(CanDb &candb, DbcTokenList &tokens); + bool parseSectionVersion(CanDb &candb, DbcTokenList &tokens); + bool parseSectionBs(DbcTokenList &tokens); + bool parseSectionBu(CanDb &candb, DbcTokenList &tokens); + bool parseSectionBo(CanDb &candb, DbcTokenList &tokens); + bool parseSectionBoSg(CanDb &candb, CanDbMessage *msg, DbcTokenList &tokens); + bool parseSectionCm(CanDb &candb, DbcTokenList &tokens); + bool parseSectionVal(CanDb &candb, DbcTokenList &tokens); + +}; diff --git a/cpp/parser/dbc/DbcTokens.cpp b/cpp/parser/dbc/DbcTokens.cpp new file mode 100644 index 0000000..5a77dbe --- /dev/null +++ b/cpp/parser/dbc/DbcTokens.cpp @@ -0,0 +1,137 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "DbcTokens.h" + +DbcToken::DbcToken(int line, int column, dbc_token_type_t type) + : _line(line), _col(column), _type(type), _numLineBreaks(0) +{ + +} + +bool DbcToken::appendChar(QChar ch) +{ + if (acceptsChar(ch)) { + if (ch=='\n') { + _numLineBreaks++; + } + _data.append(ch); + return true; + } else { + return false; + } +} + +dbc_token_type_t DbcToken::getType() +{ + return _type; +} + +QString DbcToken::getData() +{ + return _data; +} + +int DbcToken::countLineBreaks() +{ + return _numLineBreaks; +} + +int DbcToken::getLine() +{ + return _line; +} + +int DbcToken::getColumn() +{ + return _col; +} + + +DbcIdentifierToken::DbcIdentifierToken(int line, int column) + : DbcToken(line, column, dbc_tok_identifier) +{ +} + +bool DbcIdentifierToken::acceptsChar(QChar ch) +{ + static const QString acceptableStartChars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"); + static const QString acceptableChars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"); + if (_data.isEmpty()) { + return acceptableStartChars.contains(ch); + } else { + return acceptableChars.contains(ch); + } +} + + +DbcStringToken::DbcStringToken(int line, int column) + : DbcToken(line, column, dbc_tok_string) +{ +} + +bool DbcStringToken::acceptsChar(QChar ch) +{ + if (_data.isEmpty()) { + return (ch=='"'); + } else if (_data.length()<2) { + return true; + } else { + return !_data.endsWith('"'); + } +} + + + + +DbcWhitespaceToken::DbcWhitespaceToken(int line, int column) + : DbcToken(line, column, dbc_tok_whitespace) +{ +} + +bool DbcWhitespaceToken::acceptsChar(QChar ch) +{ + return ch.isSpace(); +} + +DbcRegExpToken::DbcRegExpToken(int line, int column, dbc_token_type_t type, QRegExp re) + : DbcToken(line, column, type), + _re(re) +{ +} + +bool DbcRegExpToken::acceptsChar(QChar ch) +{ + QString s = _data + ch; + return (_re.indexIn(s)==0); +} + +DbcSingleCharToken::DbcSingleCharToken(int line, int column, dbc_token_type_t type, QChar ch) + : DbcToken(line, column, type), + _ch(ch) +{ +} + +bool DbcSingleCharToken::acceptsChar(QChar ch) +{ + return (ch==_ch) && _data.isEmpty(); +} + diff --git a/cpp/parser/dbc/DbcTokens.h b/cpp/parser/dbc/DbcTokens.h new file mode 100644 index 0000000..4c7cfe4 --- /dev/null +++ b/cpp/parser/dbc/DbcTokens.h @@ -0,0 +1,102 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include + +typedef enum { + dbc_tok_whitespace = 1, + dbc_tok_identifier = 2, + dbc_tok_string = 4, + dbc_tok_number = 8, + dbc_tok_colon = 16, + dbc_tok_pipe = 32, + dbc_tok_at = 64, + dbc_tok_plus = 128, + dbc_tok_parenth_open = 256, + dbc_tok_parenth_close = 512, + dbc_tok_bracket_open = 1024, + dbc_tok_bracket_close = 2048, + dbc_tok_comma = 4096, + dbc_tok_semicolon = 8192, + dbc_tok_minus = 16384, + + dbc_tok_ALL = 0xFFFFFFFF +} dbc_token_type_t; + +class DbcToken { +public: + DbcToken(int line, int column, dbc_token_type_t type); + virtual bool acceptsChar(QChar ch) = 0; + + bool appendChar(QChar ch); + dbc_token_type_t getType(); + QString getData(); + int countLineBreaks(); + int getLine(); + int getColumn(); + +protected: + QString _data; +private: + int _line; + int _col; + dbc_token_type_t _type; + int _numLineBreaks; +}; + +class DbcWhitespaceToken : public DbcToken { +public: + DbcWhitespaceToken(int line, int column); + virtual bool acceptsChar(QChar ch); + +}; + +class DbcIdentifierToken : public DbcToken { +public: + DbcIdentifierToken(int line, int column); + virtual bool acceptsChar(QChar ch); +}; + +class DbcStringToken : public DbcToken { +public: + DbcStringToken(int line, int column); + virtual bool acceptsChar(QChar ch); +}; + +class DbcRegExpToken : public DbcToken { +public: + DbcRegExpToken(int line, int column, dbc_token_type_t type, QRegExp re); + virtual bool acceptsChar(QChar ch); +private: + QRegExp _re; +}; + +class DbcSingleCharToken : public DbcToken { +public: + DbcSingleCharToken(int line, int column, dbc_token_type_t type, QChar ch); + virtual bool acceptsChar(QChar ch); +private: + QChar _ch; +}; diff --git a/cpp/parser/dbc/dbc.pri b/cpp/parser/dbc/dbc.pri new file mode 100644 index 0000000..b6d4f63 --- /dev/null +++ b/cpp/parser/dbc/dbc.pri @@ -0,0 +1,7 @@ +HEADERS += \ + $$PWD/DbcParser.h \ + $$PWD/DbcTokens.h + +SOURCES += \ + $$PWD/DbcParser.cpp \ + $$PWD/DbcTokens.cpp diff --git a/cpp/window/CanStatusWindow/CanStatusWindow.cpp b/cpp/window/CanStatusWindow/CanStatusWindow.cpp new file mode 100644 index 0000000..23585d4 --- /dev/null +++ b/cpp/window/CanStatusWindow/CanStatusWindow.cpp @@ -0,0 +1,109 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + + +#include "CanStatusWindow.h" +#include "ui_CanStatusWindow.h" + +#include +#include +#include +#include +#include +#include + +CanStatusWindow::CanStatusWindow(QWidget *parent, Backend &backend) : + ConfigurableWidget(parent), + ui(new Ui::CanStatusWindow), + _backend(backend), + _timer(new QTimer(this)) +{ + ui->setupUi(this); + ui->treeWidget->setHeaderLabels(QStringList() + << "Driver" << "Interface" << "State" + << "Rx Frames" << "Rx Errors" << "Rx Overrun" + << "Tx Frames" << "Tx Errors" << "Tx Dropped" + << "# Warning" << "# Passive" << "# Bus Off" << " #Restarts" + ); + ui->treeWidget->setColumnWidth(0, 80); + ui->treeWidget->setColumnWidth(1, 70); + + connect(&backend, SIGNAL(beginMeasurement()), this, SLOT(beginMeasurement())); + connect(&backend, SIGNAL(endMeasurement()), this, SLOT(endMeasurement())); + connect(_timer, SIGNAL(timeout()), this, SLOT(update())); +} + +CanStatusWindow::~CanStatusWindow() +{ + delete ui; +} + +void CanStatusWindow::beginMeasurement() +{ + ui->treeWidget->clear(); + foreach (CanInterfaceId ifid, backend().getInterfaceList()) { + CanInterface *intf = backend().getInterfaceById(ifid); + QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget); + item->setData(0, Qt::UserRole, QVariant::fromValue((void*)intf)); + item->setText(column_driver, intf->getDriver()->getName()); + item->setText(column_interface, intf->getName()); + + item->setTextAlignment(column_driver, Qt::AlignLeft); + item->setTextAlignment(column_interface, Qt::AlignLeft); + item->setTextAlignment(column_state, Qt::AlignCenter); + for (int i=column_rx_frames; isetTextAlignment(i, Qt::AlignRight); + } + + ui->treeWidget->addTopLevelItem(item); + } + update(); + _timer->start(100); +} + +void CanStatusWindow::endMeasurement() +{ + _timer->stop(); +} + +void CanStatusWindow::update() +{ + for (QTreeWidgetItemIterator it(ui->treeWidget); *it; ++it) { + QTreeWidgetItem *item = *it; + CanInterface *intf = (CanInterface *)item->data(0, Qt::UserRole).value(); + if (intf) { + intf->updateStatistics(); + item->setText(column_state, intf->getStateText()); + item->setText(column_rx_frames, QString().number(intf->getNumRxFrames())); + item->setText(column_rx_errors, QString().number(intf->getNumRxErrors())); + item->setText(column_rx_overrun, QString().number(intf->getNumRxOverruns())); + item->setText(column_tx_frames, QString().number(intf->getNumTxFrames())); + item->setText(column_tx_errors, QString().number(intf->getNumTxErrors())); + item->setText(column_tx_dropped, QString().number(intf->getNumTxDropped())); + } + } + +} + +Backend &CanStatusWindow::backend() +{ + return _backend; +} diff --git a/cpp/window/CanStatusWindow/CanStatusWindow.h b/cpp/window/CanStatusWindow/CanStatusWindow.h new file mode 100644 index 0000000..7b7e688 --- /dev/null +++ b/cpp/window/CanStatusWindow/CanStatusWindow.h @@ -0,0 +1,70 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include + +namespace Ui { +class CanStatusWindow; +} + +class Backend; +class QTimer; + +class CanStatusWindow : public ConfigurableWidget +{ + Q_OBJECT + +public: + enum { + column_driver, + column_interface, + column_state, + column_rx_frames, + column_rx_errors, + column_rx_overrun, + column_tx_frames, + column_tx_errors, + column_tx_dropped, + column_num_warning, + column_num_passive, + column_num_busoff, + column_num_restarts, + column_count + }; + +public: + explicit CanStatusWindow(QWidget *parent, Backend &backend); + ~CanStatusWindow(); + +private slots: + void beginMeasurement(); + void endMeasurement(); + void update(); + +private: + Ui::CanStatusWindow *ui; + Backend &_backend; + + Backend &backend(); + QTimer *_timer; +}; diff --git a/cpp/window/CanStatusWindow/CanStatusWindow.pri b/cpp/window/CanStatusWindow/CanStatusWindow.pri new file mode 100644 index 0000000..9952393 --- /dev/null +++ b/cpp/window/CanStatusWindow/CanStatusWindow.pri @@ -0,0 +1,8 @@ +SOURCES += \ + $$PWD/CanStatusWindow.cpp \ + +HEADERS += \ + $$PWD/CanStatusWindow.h \ + +FORMS += \ + $$PWD/CanStatusWindow.ui \ diff --git a/cpp/window/CanStatusWindow/CanStatusWindow.ui b/cpp/window/CanStatusWindow/CanStatusWindow.ui new file mode 100644 index 0000000..fcbea7e --- /dev/null +++ b/cpp/window/CanStatusWindow/CanStatusWindow.ui @@ -0,0 +1,114 @@ + + + CanStatusWindow + + + + 0 + 0 + 756 + 459 + + + + Can Status + + + + 2 + + + 2 + + + 2 + + + 2 + + + + + 0 + + + false + + + 13 + + + 80 + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + + 5 + + + + + 6 + + + + + 7 + + + + + 8 + + + + + 9 + + + + + 10 + + + + + 11 + + + + + 12 + + + + + 13 + + + + + + + + + diff --git a/cpp/window/GraphWindow/GraphWindow.cpp b/cpp/window/GraphWindow/GraphWindow.cpp new file mode 100644 index 0000000..81b63bd --- /dev/null +++ b/cpp/window/GraphWindow/GraphWindow.cpp @@ -0,0 +1,136 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "GraphWindow.h" +#include "ui_GraphWindow.h" + +#include + +#include +#include + +#define NUM_GRAPH_POINTS 20 + +GraphWindow::GraphWindow(QWidget *parent, Backend &backend) : + ConfigurableWidget(parent), + ui(new Ui::GraphWindow), + _backend(backend) +{ + ui->setupUi(this); + + + data_series = new QLineSeries(); + + for(uint32_t i=0; iappend(i, i); + } + + data_chart = new QChart(); + data_chart->legend()->hide(); + data_chart->addSeries(data_series); + data_chart->createDefaultAxes(); + data_chart->setTitle("Simple line chart example"); + + + // Have a box pop up that allows the user to select a signal from the loaded DBC to graph + // On OK, add that CanDbMessage to a list. + // Either sample the values regularly with a timer or somehow emit a signal when the message + // is received that we catch here... + + //backend.findDbMessage() + +// CanDbMessage *result = 0; + +// foreach (MeasurementNetwork *network, _networks) { +// foreach (pCanDb db, network->_canDbs) { +// result = db->getMessageById(msg.getRawId()); +// if (result != 0) { +// return result; +// } +// } +// } +// return result; + + + + ui->chartView->setChart(data_chart); + ui->chartView->setRenderHint(QPainter::Antialiasing); + +// connect(ui->buttonTest, SIGNAL(released()), this, SLOT(testAddData())); + + +} + +void GraphWindow::testAddData(qreal new_yval) +{ + QLineSeries* serbuf = new QLineSeries(); + + // Start autorange at first point + qreal ymin = data_series->at(1).y(); + qreal ymax = ymin; + + // Copy all points but first one + for(uint32_t i=1; i < data_series->count(); i++) + { + serbuf->append(data_series->at(i).x()-1, data_series->at(i).y()); + + // Autoranging + if(data_series->at(i).y() < ymin) + ymin = data_series->at(i).y(); + if(data_series->at(i).y() > ymax) + ymax = data_series->at(i).y(); + } + + // Apply Y margin and set range + ymin -= 1; + ymax += 1; + data_chart->axisY()->setRange(ymin, ymax); + + // Add new point in + serbuf->append(serbuf->points().at(serbuf->count()-1).x() + 1, new_yval); + testcount++; + + // Replace data + data_series->replace(serbuf->points()); + + delete serbuf; +} + +GraphWindow::~GraphWindow() +{ + delete ui; + delete data_chart; + delete data_series; +} + +bool GraphWindow::saveXML(Backend &backend, QDomDocument &xml, QDomElement &root) +{ + if (!ConfigurableWidget::saveXML(backend, xml, root)) { return false; } + root.setAttribute("type", "GraphWindow"); + return true; +} + +bool GraphWindow::loadXML(Backend &backend, QDomElement &el) +{ + if (!ConfigurableWidget::loadXML(backend, el)) { return false; } + return true; +} diff --git a/cpp/window/GraphWindow/GraphWindow.h b/cpp/window/GraphWindow/GraphWindow.h new file mode 100644 index 0000000..7698360 --- /dev/null +++ b/cpp/window/GraphWindow/GraphWindow.h @@ -0,0 +1,58 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace Ui { +class GraphWindow; +} + +class QDomDocument; +class QDomElement; + +class GraphWindow : public ConfigurableWidget +{ + Q_OBJECT + +public: + explicit GraphWindow(QWidget *parent, Backend &backend); + ~GraphWindow(); + virtual bool saveXML(Backend &backend, QDomDocument &xml, QDomElement &root); + virtual bool loadXML(Backend &backend, QDomElement &el); + +private slots: + void testAddData(qreal new_yval); + +private: + QLineSeries *data_series; + QChart *data_chart; + uint32_t testcount; + + Ui::GraphWindow *ui; + Backend &_backend; +}; diff --git a/cpp/window/GraphWindow/GraphWindow.pri b/cpp/window/GraphWindow/GraphWindow.pri new file mode 100644 index 0000000..8e87b92 --- /dev/null +++ b/cpp/window/GraphWindow/GraphWindow.pri @@ -0,0 +1,8 @@ +SOURCES += \ + $$PWD/GraphWindow.cpp + +HEADERS += \ + $$PWD/GraphWindow.h + +FORMS += \ + $$PWD/GraphWindow.ui diff --git a/cpp/window/GraphWindow/GraphWindow.ui b/cpp/window/GraphWindow/GraphWindow.ui new file mode 100644 index 0000000..bf4b596 --- /dev/null +++ b/cpp/window/GraphWindow/GraphWindow.ui @@ -0,0 +1,77 @@ + + + GraphWindow + + + + 0 + 0 + 870 + 274 + + + + + 0 + 0 + + + + + 301 + 274 + + + + Graph + + + + + 10 + 10 + 281 + 231 + + + + + 281 + 231 + + + + + + + 210 + 250 + 75 + 23 + + + + Test + + + + + + 390 + 30 + 256 + 192 + + + + + + + QChartView + QGraphicsView +
QtCharts
+
+
+ + +
diff --git a/cpp/window/LogWindow/LogWindow.cpp b/cpp/window/LogWindow/LogWindow.cpp new file mode 100644 index 0000000..ece91a5 --- /dev/null +++ b/cpp/window/LogWindow/LogWindow.cpp @@ -0,0 +1,62 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "LogWindow.h" +#include "ui_LogWindow.h" + +#include +#include +#include + +LogWindow::LogWindow(QWidget *parent, Backend &backend) : + ConfigurableWidget(parent), + ui(new Ui::LogWindow) +{ + connect(&backend.getLogModel(), SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(rowsInserted(QModelIndex,int,int))); + + ui->setupUi(this); + ui->treeView->setModel(&backend.getLogModel()); +} + +LogWindow::~LogWindow() +{ + delete ui; +} + +bool LogWindow::saveXML(Backend &backend, QDomDocument &xml, QDomElement &root) +{ + if (!ConfigurableWidget::saveXML(backend, xml, root)) { return false; } + root.setAttribute("type", "LogWindow"); + return true; +} + +bool LogWindow::loadXML(Backend &backend, QDomElement &el) +{ + return ConfigurableWidget::loadXML(backend, el); +} + +void LogWindow::rowsInserted(const QModelIndex &parent, int first, int last) +{ + (void) parent; + (void) first; + (void) last; + ui->treeView->scrollToBottom(); +} diff --git a/cpp/window/LogWindow/LogWindow.h b/cpp/window/LogWindow/LogWindow.h new file mode 100644 index 0000000..76ea051 --- /dev/null +++ b/cpp/window/LogWindow/LogWindow.h @@ -0,0 +1,51 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include + +namespace Ui { +class LogWindow; +} + +class QDomDocument; +class QDomElement; +class LogModel; + +class LogWindow : public ConfigurableWidget +{ + Q_OBJECT + +public: + explicit LogWindow(QWidget *parent, Backend &backend); + ~LogWindow(); + + virtual bool saveXML(Backend &backend, QDomDocument &xml, QDomElement &root); + virtual bool loadXML(Backend &backend, QDomElement &el); + +private slots: + void rowsInserted(const QModelIndex & parent, int first, int last); + +private: + Ui::LogWindow *ui; +}; diff --git a/cpp/window/LogWindow/LogWindow.pri b/cpp/window/LogWindow/LogWindow.pri new file mode 100644 index 0000000..dbc31cc --- /dev/null +++ b/cpp/window/LogWindow/LogWindow.pri @@ -0,0 +1,10 @@ +SOURCES += \ + $$PWD/LogWindow.cpp \ + + +HEADERS += \ + $$PWD/LogWindow.h \ + + +FORMS += \ + $$PWD/LogWindow.ui \ diff --git a/cpp/window/LogWindow/LogWindow.ui b/cpp/window/LogWindow/LogWindow.ui new file mode 100644 index 0000000..d0ce889 --- /dev/null +++ b/cpp/window/LogWindow/LogWindow.ui @@ -0,0 +1,36 @@ + + + LogWindow + + + + 0 + 0 + 400 + 300 + + + + Log + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + + + diff --git a/cpp/window/RawTxWindow/RawTxWindow.cpp b/cpp/window/RawTxWindow/RawTxWindow.cpp new file mode 100644 index 0000000..96c0afb --- /dev/null +++ b/cpp/window/RawTxWindow/RawTxWindow.cpp @@ -0,0 +1,735 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "RawTxWindow.h" +#include "ui_RawTxWindow.h" + +#include +#include +#include +#include + +RawTxWindow::RawTxWindow(QWidget *parent, Backend &backend) : + ConfigurableWidget(parent), + ui(new Ui::RawTxWindow), + _backend(backend) +{ + ui->setupUi(this); + + connect(ui->singleSendButton, SIGNAL(released()), this, SLOT(sendRawMessage())); + connect(ui->repeatSendButton, SIGNAL(toggled(bool)), this, SLOT(sendRepeatMessage(bool))); + + connect(ui->spinBox_RepeatRate, SIGNAL(valueChanged(int)), this, SLOT(changeRepeatRate(int))); + + connect(ui->comboBoxInterface, SIGNAL(currentIndexChanged(int)), this, SLOT(updateCapabilities())); + connect(ui->checkbox_FD, SIGNAL(stateChanged(int)), this, SLOT(updateCapabilities())); + + connect(&backend, SIGNAL(beginMeasurement()), this, SLOT(refreshInterfaces())); + + // Timer for repeating messages + repeatmsg_timer = new QTimer(this); + connect(repeatmsg_timer, SIGNAL(timeout()), this, SLOT(sendRawMessage())); + + + // TODO: Grey out checkboxes that are invalid depending on DLC spinbox state + //connect(ui->fieldDLC, SIGNAL(valueChanged(int)), this, SLOT(changeDLC(int))); + connect(ui->comboBoxDLC, SIGNAL(currentIndexChanged(int)), this, SLOT(changeDLC())); + + // Disable TX until interfaces are present + this->setDisabled(1); + +} + + +RawTxWindow::~RawTxWindow() +{ + delete ui; +} + + +void RawTxWindow::changeDLC() +{ + + ui->fieldByte0_0->setEnabled(true); + ui->fieldByte1_0->setEnabled(true); + ui->fieldByte2_0->setEnabled(true); + ui->fieldByte3_0->setEnabled(true); + ui->fieldByte4_0->setEnabled(true); + ui->fieldByte5_0->setEnabled(true); + ui->fieldByte6_0->setEnabled(true); + ui->fieldByte7_0->setEnabled(true); + + ui->fieldByte0_1->setEnabled(true); + ui->fieldByte1_1->setEnabled(true); + ui->fieldByte2_1->setEnabled(true); + ui->fieldByte3_1->setEnabled(true); + ui->fieldByte4_1->setEnabled(true); + ui->fieldByte5_1->setEnabled(true); + ui->fieldByte6_1->setEnabled(true); + ui->fieldByte7_1->setEnabled(true); + + ui->fieldByte0_2->setEnabled(true); + ui->fieldByte1_2->setEnabled(true); + ui->fieldByte2_2->setEnabled(true); + ui->fieldByte3_2->setEnabled(true); + ui->fieldByte4_2->setEnabled(true); + ui->fieldByte5_2->setEnabled(true); + ui->fieldByte6_2->setEnabled(true); + ui->fieldByte7_2->setEnabled(true); + + ui->fieldByte0_3->setEnabled(true); + ui->fieldByte1_3->setEnabled(true); + ui->fieldByte2_3->setEnabled(true); + ui->fieldByte3_3->setEnabled(true); + ui->fieldByte4_3->setEnabled(true); + ui->fieldByte5_3->setEnabled(true); + ui->fieldByte6_3->setEnabled(true); + ui->fieldByte7_3->setEnabled(true); + + ui->fieldByte0_4->setEnabled(true); + ui->fieldByte1_4->setEnabled(true); + ui->fieldByte2_4->setEnabled(true); + ui->fieldByte3_4->setEnabled(true); + ui->fieldByte4_4->setEnabled(true); + ui->fieldByte5_4->setEnabled(true); + ui->fieldByte6_4->setEnabled(true); + ui->fieldByte7_4->setEnabled(true); + + ui->fieldByte0_5->setEnabled(true); + ui->fieldByte1_5->setEnabled(true); + ui->fieldByte2_5->setEnabled(true); + ui->fieldByte3_5->setEnabled(true); + ui->fieldByte4_5->setEnabled(true); + ui->fieldByte5_5->setEnabled(true); + ui->fieldByte6_5->setEnabled(true); + ui->fieldByte7_5->setEnabled(true); + + ui->fieldByte0_6->setEnabled(true); + ui->fieldByte1_6->setEnabled(true); + ui->fieldByte2_6->setEnabled(true); + ui->fieldByte3_6->setEnabled(true); + ui->fieldByte4_6->setEnabled(true); + ui->fieldByte5_6->setEnabled(true); + ui->fieldByte6_6->setEnabled(true); + ui->fieldByte7_6->setEnabled(true); + + ui->fieldByte0_7->setEnabled(true); + ui->fieldByte1_7->setEnabled(true); + ui->fieldByte2_7->setEnabled(true); + ui->fieldByte3_7->setEnabled(true); + ui->fieldByte4_7->setEnabled(true); + ui->fieldByte5_7->setEnabled(true); + ui->fieldByte6_7->setEnabled(true); + ui->fieldByte7_7->setEnabled(true); + + uint8_t dlc = ui->comboBoxDLC->currentData().toUInt(); + + switch(dlc) + { + case 0: + ui->fieldByte0_0->setEnabled(false); + //fallthrough + case 1: + ui->fieldByte1_0->setEnabled(false); + //fallthrough + + case 2: + ui->fieldByte2_0->setEnabled(false); + //fallthrough + + case 3: + ui->fieldByte3_0->setEnabled(false); + //fallthrough + + case 4: + ui->fieldByte4_0->setEnabled(false); + //fallthrough + + case 5: + ui->fieldByte5_0->setEnabled(false); + //fallthrough + + case 6: + ui->fieldByte6_0->setEnabled(false); + //fallthrough + + case 7: + ui->fieldByte7_0->setEnabled(false); + //fallthrough + + case 8: + ui->fieldByte0_1->setEnabled(false); + ui->fieldByte1_1->setEnabled(false); + ui->fieldByte2_1->setEnabled(false); + ui->fieldByte3_1->setEnabled(false); + //fallthrough + case 12: + ui->fieldByte4_1->setEnabled(false); + ui->fieldByte5_1->setEnabled(false); + ui->fieldByte6_1->setEnabled(false); + ui->fieldByte7_1->setEnabled(false); + //fallthrough + case 16: + ui->fieldByte0_2->setEnabled(false); + ui->fieldByte1_2->setEnabled(false); + ui->fieldByte2_2->setEnabled(false); + ui->fieldByte3_2->setEnabled(false); + //fallthrough + case 20: + ui->fieldByte4_2->setEnabled(false); + ui->fieldByte5_2->setEnabled(false); + ui->fieldByte6_2->setEnabled(false); + ui->fieldByte7_2->setEnabled(false); + //fallthrough + case 24: + ui->fieldByte0_3->setEnabled(false); + ui->fieldByte1_3->setEnabled(false); + ui->fieldByte2_3->setEnabled(false); + ui->fieldByte3_3->setEnabled(false); + ui->fieldByte4_3->setEnabled(false); + ui->fieldByte5_3->setEnabled(false); + ui->fieldByte6_3->setEnabled(false); + ui->fieldByte7_3->setEnabled(false); + //fallthrough + case 32: + ui->fieldByte0_4->setEnabled(false); + ui->fieldByte1_4->setEnabled(false); + ui->fieldByte2_4->setEnabled(false); + ui->fieldByte3_4->setEnabled(false); + ui->fieldByte4_4->setEnabled(false); + ui->fieldByte5_4->setEnabled(false); + ui->fieldByte6_4->setEnabled(false); + ui->fieldByte7_4->setEnabled(false); + + ui->fieldByte0_5->setEnabled(false); + ui->fieldByte1_5->setEnabled(false); + ui->fieldByte2_5->setEnabled(false); + ui->fieldByte3_5->setEnabled(false); + ui->fieldByte4_5->setEnabled(false); + ui->fieldByte5_5->setEnabled(false); + ui->fieldByte6_5->setEnabled(false); + ui->fieldByte7_5->setEnabled(false); + //fallthrough + case 48: + ui->fieldByte0_6->setEnabled(false); + ui->fieldByte1_6->setEnabled(false); + ui->fieldByte2_6->setEnabled(false); + ui->fieldByte3_6->setEnabled(false); + ui->fieldByte4_6->setEnabled(false); + ui->fieldByte5_6->setEnabled(false); + ui->fieldByte6_6->setEnabled(false); + ui->fieldByte7_6->setEnabled(false); + + ui->fieldByte0_7->setEnabled(false); + ui->fieldByte1_7->setEnabled(false); + ui->fieldByte2_7->setEnabled(false); + ui->fieldByte3_7->setEnabled(false); + ui->fieldByte4_7->setEnabled(false); + ui->fieldByte5_7->setEnabled(false); + ui->fieldByte6_7->setEnabled(false); + ui->fieldByte7_7->setEnabled(false); + + } +// repeatmsg_timer->setInterval(ms); +} + +void RawTxWindow::updateCapabilities() +{ + + // check if intf suports fd, if, enable, else dis + //CanInterface *intf = _backend.getInterfaceById(idx); + if(ui->comboBoxInterface->count() > 0) + { + // By default BRS should be available + + CanInterface *intf = _backend.getInterfaceById((CanInterfaceId)ui->comboBoxInterface->currentData().toUInt()); + + if(intf == NULL) + { + return; + } + + int idx_restore = ui->comboBoxDLC->currentIndex(); + + // If CANFD is available + if(intf->getCapabilities() & intf->capability_canfd) + { + ui->comboBoxDLC->clear(); + ui->comboBoxDLC->addItem("0", 0); + ui->comboBoxDLC->addItem("1", 1); + ui->comboBoxDLC->addItem("2", 2); + ui->comboBoxDLC->addItem("3", 3); + ui->comboBoxDLC->addItem("4", 4); + ui->comboBoxDLC->addItem("5", 5); + ui->comboBoxDLC->addItem("6", 6); + ui->comboBoxDLC->addItem("7", 7); + ui->comboBoxDLC->addItem("8", 8); + ui->comboBoxDLC->addItem("12", 12); + ui->comboBoxDLC->addItem("16", 16); + ui->comboBoxDLC->addItem("20", 20); + ui->comboBoxDLC->addItem("24", 24); + ui->comboBoxDLC->addItem("32", 32); + ui->comboBoxDLC->addItem("48", 48); + ui->comboBoxDLC->addItem("64", 64); + + // Restore previous selected DLC if available + if(idx_restore > 1 && idx_restore < ui->comboBoxDLC->count()) + ui->comboBoxDLC->setCurrentIndex(idx_restore); + + ui->checkbox_FD->setDisabled(0); + + // Enable BRS if this is an FD frame + if(ui->checkbox_FD->isChecked()) + { + // Enable BRS if FD enabled + ui->checkbox_BRS->setDisabled(0); + + // Disable RTR if FD enabled + ui->checkBox_IsRTR->setDisabled(1); + ui->checkBox_IsRTR->setChecked(false); + } + else + { + // Disable BRS if FD disabled + ui->checkbox_BRS->setDisabled(1); + ui->checkbox_BRS->setChecked(false); + + // Enable RTR if FD disabled + ui->checkBox_IsRTR->setDisabled(0); + + } + showFDFields(); + } + else + { + // CANFD not available + ui->comboBoxDLC->clear(); + ui->comboBoxDLC->addItem("0", 0); + ui->comboBoxDLC->addItem("1", 1); + ui->comboBoxDLC->addItem("2", 2); + ui->comboBoxDLC->addItem("3", 3); + ui->comboBoxDLC->addItem("4", 4); + ui->comboBoxDLC->addItem("5", 5); + ui->comboBoxDLC->addItem("6", 6); + ui->comboBoxDLC->addItem("7", 7); + ui->comboBoxDLC->addItem("8", 8); + + // Restore previous selected DLC if available + if(idx_restore > 1 && idx_restore < ui->comboBoxDLC->count()) + ui->comboBoxDLC->setCurrentIndex(idx_restore); + + // Unset/disable FD / BRS checkboxes + ui->checkbox_FD->setDisabled(1); + ui->checkbox_BRS->setDisabled(1); + ui->checkbox_FD->setChecked(false); + ui->checkbox_BRS->setChecked(false); + + // Enable RTR (could be disabled by FD checkbox being set) + ui->checkBox_IsRTR->setDisabled(0); + + hideFDFields(); + + } + } +} + +void RawTxWindow::changeRepeatRate(int ms) +{ + repeatmsg_timer->setInterval(ms); +} + +void RawTxWindow::sendRepeatMessage(bool enable) +{ + if(enable) + { + repeatmsg_timer->start(ui->spinBox_RepeatRate->value()); + } + else + { + repeatmsg_timer->stop(); + } +} + + + + +void RawTxWindow::disableTxWindow(int disable) +{ + if(disable) + { + this->setDisabled(1); + } + else + { + // Only enable if an interface is present + if(_backend.getInterfaceList().count() > 0) + this->setDisabled(0); + else + this->setDisabled(1); + } +} + +void RawTxWindow::refreshInterfaces() +{ + ui->comboBoxInterface->clear(); + + int cb_idx = 0; + + // TODO: Only show interfaces that are included in active MeasurementInterfaces! + foreach (CanInterfaceId ifid, _backend.getInterfaceList()) { + CanInterface *intf = _backend.getInterfaceById(ifid); + + if(intf->isOpen()) + { + ui->comboBoxInterface->addItem(intf->getName() + " " + intf->getDriver()->getName()); + ui->comboBoxInterface->setItemData(cb_idx, QVariant(ifid)); + cb_idx++; + } + } + + if(cb_idx == 0) + disableTxWindow(1); + else + disableTxWindow(0); + + updateCapabilities(); +} + +void RawTxWindow::sendRawMessage() +{ + CanMessage msg; + + bool en_extended = ui->checkBox_IsExtended->isChecked(); + bool en_rtr = ui->checkBox_IsRTR->isChecked(); + + uint8_t data_int[64]; + int data_ctr = 0; + + data_int[data_ctr++] = ui->fieldByte0_0->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte1_0->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte2_0->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte3_0->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte4_0->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte5_0->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte6_0->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte7_0->text().toUpper().toInt(NULL, 16); + + data_int[data_ctr++] = ui->fieldByte0_1->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte1_1->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte2_1->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte3_1->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte4_1->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte5_1->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte6_1->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte7_1->text().toUpper().toInt(NULL, 16); + + data_int[data_ctr++] = ui->fieldByte0_2->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte1_2->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte2_2->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte3_2->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte4_2->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte5_2->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte6_2->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte7_2->text().toUpper().toInt(NULL, 16); + + data_int[data_ctr++] = ui->fieldByte0_3->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte1_3->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte2_3->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte3_3->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte4_3->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte5_3->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte6_3->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte7_3->text().toUpper().toInt(NULL, 16); + + data_int[data_ctr++] = ui->fieldByte0_4->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte1_4->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte2_4->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte3_4->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte4_4->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte5_4->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte6_4->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte7_4->text().toUpper().toInt(NULL, 16); + + data_int[data_ctr++] = ui->fieldByte0_5->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte1_5->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte2_5->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte3_5->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte4_5->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte5_5->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte6_5->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte7_5->text().toUpper().toInt(NULL, 16); + + data_int[data_ctr++] = ui->fieldByte0_6->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte1_6->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte2_6->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte3_6->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte4_6->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte5_6->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte6_6->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte7_6->text().toUpper().toInt(NULL, 16); + + data_int[data_ctr++] = ui->fieldByte0_7->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte1_7->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte2_7->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte3_7->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte4_7->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte5_7->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte6_7->text().toUpper().toInt(NULL, 16); + data_int[data_ctr++] = ui->fieldByte7_7->text().toUpper().toInt(NULL, 16); + + + uint32_t address = ui->fieldAddress->text().toUpper().toUInt(NULL, 16); + + // If address is beyond std address namespace, force extended + if(address > 0x7ff) + { + en_extended = true; + ui->checkBox_IsExtended->setChecked(true); + } + + // If address is larger than max for extended, clip + if(address >= 0x1FFFFFFF) + { + address = address & 0x1FFFFFFF; + ui->fieldAddress->setText(QString::number( address, 16 ).toUpper()); + } + + uint8_t dlc =ui->comboBoxDLC->currentData().toUInt(); + + // If DLC > 8, must be FD + if(dlc > 8) + { + ui->checkbox_FD->setChecked(true); + } + + // Set payload data + for(int i=0; icheckbox_BRS->isChecked()) + msg.setBRS(true); + if(ui->checkbox_FD->isChecked()) + msg.setFD(true); + + CanInterface *intf = _backend.getInterfaceById((CanInterfaceId)ui->comboBoxInterface->currentData().toUInt()); + intf->sendMessage(msg); + + + char outmsg[256]; + snprintf(outmsg, 256, "Send [%s] to %d on port %s [ext=%u rtr=%u err=%u fd=%u brs=%u]", + msg.getDataHexString().toLocal8Bit().constData(), msg.getId(), intf->getName().toLocal8Bit().constData(), + msg.isExtended(), msg.isRTR(), msg.isErrorFrame(), msg.isFD(), msg.isBRS()); + log_info(outmsg); + +} + +bool RawTxWindow::saveXML(Backend &backend, QDomDocument &xml, QDomElement &root) +{ + if (!ConfigurableWidget::saveXML(backend, xml, root)) { return false; } + root.setAttribute("type", "RawTxWindow"); + return true; +} + +bool RawTxWindow::loadXML(Backend &backend, QDomElement &el) +{ + if (!ConfigurableWidget::loadXML(backend, el)) { return false; } + return true; +} + +void RawTxWindow::hideFDFields() +{ + + ui->label_col21->hide(); + ui->label_col22->hide(); + ui->label_col23->hide(); + ui->label_col24->hide(); + ui->label_col25->hide(); + ui->label_col26->hide(); + ui->label_col27->hide(); + ui->label_col28->hide(); + + ui->label_pay2->hide(); + ui->label_pay3->hide(); + ui->label_pay4->hide(); + ui->label_pay5->hide(); + ui->label_pay6->hide(); + ui->label_pay7->hide(); + ui->label_pay8->hide(); + + ui->fieldByte0_1->hide(); + ui->fieldByte1_1->hide(); + ui->fieldByte2_1->hide(); + ui->fieldByte3_1->hide(); + ui->fieldByte4_1->hide(); + ui->fieldByte5_1->hide(); + ui->fieldByte6_1->hide(); + ui->fieldByte7_1->hide(); + + ui->fieldByte0_2->hide(); + ui->fieldByte1_2->hide(); + ui->fieldByte2_2->hide(); + ui->fieldByte3_2->hide(); + ui->fieldByte4_2->hide(); + ui->fieldByte5_2->hide(); + ui->fieldByte6_2->hide(); + ui->fieldByte7_2->hide(); + + ui->fieldByte0_3->hide(); + ui->fieldByte1_3->hide(); + ui->fieldByte2_3->hide(); + ui->fieldByte3_3->hide(); + ui->fieldByte4_3->hide(); + ui->fieldByte5_3->hide(); + ui->fieldByte6_3->hide(); + ui->fieldByte7_3->hide(); + + ui->fieldByte0_4->hide(); + ui->fieldByte1_4->hide(); + ui->fieldByte2_4->hide(); + ui->fieldByte3_4->hide(); + ui->fieldByte4_4->hide(); + ui->fieldByte5_4->hide(); + ui->fieldByte6_4->hide(); + ui->fieldByte7_4->hide(); + + ui->fieldByte0_5->hide(); + ui->fieldByte1_5->hide(); + ui->fieldByte2_5->hide(); + ui->fieldByte3_5->hide(); + ui->fieldByte4_5->hide(); + ui->fieldByte5_5->hide(); + ui->fieldByte6_5->hide(); + ui->fieldByte7_5->hide(); + + ui->fieldByte0_6->hide(); + ui->fieldByte1_6->hide(); + ui->fieldByte2_6->hide(); + ui->fieldByte3_6->hide(); + ui->fieldByte4_6->hide(); + ui->fieldByte5_6->hide(); + ui->fieldByte6_6->hide(); + ui->fieldByte7_6->hide(); + + ui->fieldByte0_7->hide(); + ui->fieldByte1_7->hide(); + ui->fieldByte2_7->hide(); + ui->fieldByte3_7->hide(); + ui->fieldByte4_7->hide(); + ui->fieldByte5_7->hide(); + ui->fieldByte6_7->hide(); + ui->fieldByte7_7->hide(); +} + + +void RawTxWindow::showFDFields() +{ + + ui->label_col21->show(); + ui->label_col22->show(); + ui->label_col23->show(); + ui->label_col24->show(); + ui->label_col25->show(); + ui->label_col26->show(); + ui->label_col27->show(); + ui->label_col28->show(); + + ui->label_pay2->show(); + ui->label_pay3->show(); + ui->label_pay4->show(); + ui->label_pay5->show(); + ui->label_pay6->show(); + ui->label_pay7->show(); + ui->label_pay8->show(); + + + ui->fieldByte0_1->show(); + ui->fieldByte1_1->show(); + ui->fieldByte2_1->show(); + ui->fieldByte3_1->show(); + ui->fieldByte4_1->show(); + ui->fieldByte5_1->show(); + ui->fieldByte6_1->show(); + ui->fieldByte7_1->show(); + + ui->fieldByte0_2->show(); + ui->fieldByte1_2->show(); + ui->fieldByte2_2->show(); + ui->fieldByte3_2->show(); + ui->fieldByte4_2->show(); + ui->fieldByte5_2->show(); + ui->fieldByte6_2->show(); + ui->fieldByte7_2->show(); + + ui->fieldByte0_3->show(); + ui->fieldByte1_3->show(); + ui->fieldByte2_3->show(); + ui->fieldByte3_3->show(); + ui->fieldByte4_3->show(); + ui->fieldByte5_3->show(); + ui->fieldByte6_3->show(); + ui->fieldByte7_3->show(); + + ui->fieldByte0_4->show(); + ui->fieldByte1_4->show(); + ui->fieldByte2_4->show(); + ui->fieldByte3_4->show(); + ui->fieldByte4_4->show(); + ui->fieldByte5_4->show(); + ui->fieldByte6_4->show(); + ui->fieldByte7_4->show(); + + ui->fieldByte0_5->show(); + ui->fieldByte1_5->show(); + ui->fieldByte2_5->show(); + ui->fieldByte3_5->show(); + ui->fieldByte4_5->show(); + ui->fieldByte5_5->show(); + ui->fieldByte6_5->show(); + ui->fieldByte7_5->show(); + + ui->fieldByte0_6->show(); + ui->fieldByte1_6->show(); + ui->fieldByte2_6->show(); + ui->fieldByte3_6->show(); + ui->fieldByte4_6->show(); + ui->fieldByte5_6->show(); + ui->fieldByte6_6->show(); + ui->fieldByte7_6->show(); + + ui->fieldByte0_7->show(); + ui->fieldByte1_7->show(); + ui->fieldByte2_7->show(); + ui->fieldByte3_7->show(); + ui->fieldByte4_7->show(); + ui->fieldByte5_7->show(); + ui->fieldByte6_7->show(); + ui->fieldByte7_7->show(); +} + diff --git a/cpp/window/RawTxWindow/RawTxWindow.h b/cpp/window/RawTxWindow/RawTxWindow.h new file mode 100644 index 0000000..63f7d85 --- /dev/null +++ b/cpp/window/RawTxWindow/RawTxWindow.h @@ -0,0 +1,63 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include + +namespace Ui { +class RawTxWindow; +} + +class QDomDocument; +class QDomElement; + +class RawTxWindow : public ConfigurableWidget +{ + Q_OBJECT + +public: + explicit RawTxWindow(QWidget *parent, Backend &backend); + ~RawTxWindow(); + + virtual bool saveXML(Backend &backend, QDomDocument &xml, QDomElement &root); + virtual bool loadXML(Backend &backend, QDomElement &el); + +private slots: + void changeDLC(); + void updateCapabilities(); + void changeRepeatRate(int ms); + void sendRepeatMessage(bool enable); + void disableTxWindow(int disable); + void refreshInterfaces(); + void sendRawMessage(); + + +private: + Ui::RawTxWindow *ui; + Backend &_backend; + QTimer *repeatmsg_timer; + void hideFDFields(); + void showFDFields(); + +}; diff --git a/cpp/window/RawTxWindow/RawTxWindow.pri b/cpp/window/RawTxWindow/RawTxWindow.pri new file mode 100644 index 0000000..c000556 --- /dev/null +++ b/cpp/window/RawTxWindow/RawTxWindow.pri @@ -0,0 +1,8 @@ +SOURCES += \ + $$PWD/RawTxWindow.cpp + +HEADERS += \ + $$PWD/RawTxWindow.h + +FORMS += \ + $$PWD/RawTxWindow.ui diff --git a/cpp/window/RawTxWindow/RawTxWindow.ui b/cpp/window/RawTxWindow/RawTxWindow.ui new file mode 100644 index 0000000..79ee6bc --- /dev/null +++ b/cpp/window/RawTxWindow/RawTxWindow.ui @@ -0,0 +1,1895 @@ + + + RawTxWindow + + + + 0 + 0 + 756 + 220 + + + + + 0 + 0 + + + + + 371 + 220 + + + + Raw Transmit + + + + + 450 + 10 + 41 + 25 + + + + Send + + + + + + 500 + 10 + 95 + 25 + + + + Send Repeat + + + true + + + + + + 667 + 10 + 31 + 26 + + + + ms + + + + + + 10 + 70 + 72 + 20 + + + + hhhhhhhh + + + 000 + + + + + + 10 + 50 + 71 + 17 + + + + Address + + + + + + 100 + 50 + 41 + 17 + + + + DLC + + + + + + 600 + 10 + 61 + 26 + + + + 10000 + + + 1000 + + + + + + 10 + 130 + 141 + 23 + + + + RTR + + + + + + 10 + 100 + 141 + 23 + + + + Extended ID + + + + + + 110 + 10 + 331 + 25 + + + + + + + 10 + 10 + 91 + 26 + + + + Interface: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 10 + 190 + 151 + 23 + + + + BRS + + + + + + 10 + 160 + 151 + 23 + + + + FD + + + + + + 90 + 70 + 51 + 20 + + + + + + + 390 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 390 + 50 + 21 + 16 + + + + 8 + + + Qt::AlignCenter + + + + + + 570 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 180 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 270 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 240 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 480 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 360 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 660 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 390 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 430 + 160 + 21 + 16 + + + + 8 + + + Qt::AlignCenter + + + + + + 540 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 570 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 160 + 70 + 21 + 16 + + + + 1 + + + Qt::AlignCenter + + + + + + 600 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 540 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 390 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 330 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 480 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 660 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 240 + 50 + 21 + 16 + + + + 3 + + + Qt::AlignCenter + + + + + + 600 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 270 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 180 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 450 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 360 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 430 + 70 + 21 + 16 + + + + 2 + + + Qt::AlignCenter + + + + + + 360 + 50 + 21 + 16 + + + + 7 + + + Qt::AlignCenter + + + + + + 510 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 300 + 50 + 21 + 16 + + + + 5 + + + Qt::AlignCenter + + + + + + 180 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 450 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 160 + 100 + 21 + 16 + + + + 3 + + + Qt::AlignCenter + + + + + + 450 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 330 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 510 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 480 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 180 + 50 + 21 + 16 + + + + 1 + + + Qt::AlignCenter + + + + + + 210 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 300 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 160 + 130 + 21 + 16 + + + + 5 + + + Qt::AlignCenter + + + + + + 540 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 480 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 510 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 570 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 430 + 100 + 21 + 16 + + + + 4 + + + Qt::AlignCenter + + + + + + 240 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 390 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 270 + 50 + 21 + 16 + + + + 4 + + + Qt::AlignCenter + + + + + + 330 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 600 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 240 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 660 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 210 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 330 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 630 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 630 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 300 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 240 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 630 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 210 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 210 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 660 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 450 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 270 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 630 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 300 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 180 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 360 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 600 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 430 + 130 + 21 + 16 + + + + 6 + + + Qt::AlignCenter + + + + + + 210 + 50 + 21 + 16 + + + + 2 + + + Qt::AlignCenter + + + + + + 330 + 50 + 21 + 16 + + + + 6 + + + Qt::AlignCenter + + + + + + 270 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 360 + 70 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 570 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 540 + 100 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 300 + 130 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 510 + 160 + 23 + 20 + + + + hh + + + 00 + + + 2 + + + + + + 160 + 160 + 21 + 16 + + + + 7 + + + Qt::AlignCenter + + + + + + 660 + 50 + 21 + 16 + + + + 8 + + + Qt::AlignCenter + + + + + + 540 + 50 + 21 + 16 + + + + 4 + + + Qt::AlignCenter + + + + + + 510 + 50 + 21 + 16 + + + + 3 + + + Qt::AlignCenter + + + + + + 630 + 50 + 21 + 16 + + + + 7 + + + Qt::AlignCenter + + + + + + 570 + 50 + 21 + 16 + + + + 5 + + + Qt::AlignCenter + + + + + + 480 + 50 + 21 + 16 + + + + 2 + + + Qt::AlignCenter + + + + + + 600 + 50 + 21 + 16 + + + + 6 + + + Qt::AlignCenter + + + + + + 450 + 50 + 21 + 16 + + + + 1 + + + Qt::AlignCenter + + + + + comboBoxInterface + singleSendButton + repeatSendButton + spinBox_RepeatRate + fieldAddress + comboBoxDLC + checkBox_IsExtended + checkBox_IsRTR + checkbox_FD + checkbox_BRS + fieldByte0_0 + fieldByte1_0 + fieldByte2_0 + fieldByte3_0 + fieldByte4_0 + fieldByte5_0 + fieldByte6_0 + fieldByte7_0 + fieldByte0_1 + fieldByte1_1 + fieldByte2_1 + fieldByte3_1 + fieldByte4_1 + fieldByte5_1 + fieldByte6_1 + fieldByte7_1 + fieldByte0_2 + fieldByte1_2 + fieldByte2_2 + fieldByte3_2 + fieldByte4_2 + fieldByte5_2 + fieldByte6_2 + fieldByte7_2 + fieldByte0_3 + fieldByte1_3 + fieldByte2_3 + fieldByte3_3 + fieldByte4_3 + fieldByte5_3 + fieldByte6_3 + fieldByte7_3 + fieldByte0_4 + fieldByte1_4 + fieldByte2_4 + fieldByte3_4 + fieldByte4_4 + fieldByte5_4 + fieldByte6_4 + fieldByte7_4 + fieldByte0_5 + fieldByte1_5 + fieldByte2_5 + fieldByte3_5 + fieldByte4_5 + fieldByte5_5 + fieldByte6_5 + fieldByte7_5 + fieldByte0_6 + fieldByte1_6 + fieldByte2_6 + fieldByte3_6 + fieldByte4_6 + fieldByte5_6 + fieldByte6_6 + fieldByte7_6 + fieldByte0_7 + fieldByte1_7 + fieldByte2_7 + fieldByte3_7 + fieldByte4_7 + fieldByte5_7 + fieldByte6_7 + fieldByte7_7 + + + + diff --git a/cpp/window/SetupDialog/SelectCanInterfacesDialog.cpp b/cpp/window/SetupDialog/SelectCanInterfacesDialog.cpp new file mode 100644 index 0000000..0f410a4 --- /dev/null +++ b/cpp/window/SetupDialog/SelectCanInterfacesDialog.cpp @@ -0,0 +1,69 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "SelectCanInterfacesDialog.h" +#include "ui_SelectCanInterfacesDialog.h" +#include + +SelectCanInterfacesDialog::SelectCanInterfacesDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::SelectCanInterfacesDialog) +{ + ui->setupUi(this); + ui->treeWidget->setHeaderLabels(QStringList() << "Device" << "Driver" << "Description"); +} + +SelectCanInterfacesDialog::~SelectCanInterfacesDialog() +{ + delete ui; +} + +bool SelectCanInterfacesDialog::selectInterfaces(Backend &backend, CanInterfaceIdList &selectedInterfaces, const CanInterfaceIdList &excludeInterfaces) +{ + ui->treeWidget->clear(); + + CanInterfaceIdList allInterfaces; + foreach (CanInterfaceId intf, backend.getInterfaceList()) { + if (!excludeInterfaces.contains(intf)) { + allInterfaces.append(intf); + } + } + + + foreach (CanInterfaceId intf, allInterfaces) { + QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui->treeWidget); + treeItem->setText(0, backend.getInterfaceName(intf)); + treeItem->setText(1, backend.getDriverName(intf)); + treeItem->setText(2, ""); + } + + if (exec()==QDialog::Accepted) { + selectedInterfaces.clear(); + foreach (QModelIndex idx, ui->treeWidget->selectionModel()->selectedRows()) { + if (idx.isValid()) { + selectedInterfaces.append(allInterfaces[idx.row()]); + } + } + return true; + } else { + return false; + } +} diff --git a/cpp/window/SetupDialog/SelectCanInterfacesDialog.h b/cpp/window/SetupDialog/SelectCanInterfacesDialog.h new file mode 100644 index 0000000..a00b832 --- /dev/null +++ b/cpp/window/SetupDialog/SelectCanInterfacesDialog.h @@ -0,0 +1,47 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include +#include + +class Backend; + +namespace Ui { +class SelectCanInterfacesDialog; +} + +class SelectCanInterfacesDialog : public QDialog +{ + Q_OBJECT + +public: + explicit SelectCanInterfacesDialog(QWidget *parent = 0); + ~SelectCanInterfacesDialog(); + + bool selectInterfaces(Backend &backend, CanInterfaceIdList &selectedInterfaces, const CanInterfaceIdList &excludeInterfaces); + +private: + Ui::SelectCanInterfacesDialog *ui; +}; diff --git a/cpp/window/SetupDialog/SelectCanInterfacesDialog.ui b/cpp/window/SetupDialog/SelectCanInterfacesDialog.ui new file mode 100644 index 0000000..2aeb0b8 --- /dev/null +++ b/cpp/window/SetupDialog/SelectCanInterfacesDialog.ui @@ -0,0 +1,154 @@ + + + SelectCanInterfacesDialog + + + + 0 + 0 + 570 + 438 + + + + Select CAN Interface(s) + + + + :/assets/cangaroo.png:/assets/cangaroo.png + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + 0 + + + 3 + + + 100 + + + + 1 + + + + + 2 + + + + + 3 + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + &Create Interface... + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + + + buttonBox + accepted() + SelectCanInterfacesDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + SelectCanInterfacesDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/cpp/window/SetupDialog/SetupDialog.cpp b/cpp/window/SetupDialog/SetupDialog.cpp new file mode 100644 index 0000000..d52d65f --- /dev/null +++ b/cpp/window/SetupDialog/SetupDialog.cpp @@ -0,0 +1,340 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "SetupDialog.h" +#include "ui_SetupDialog.h" +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "SetupDialogTreeModel.h" +#include "SetupDialogTreeItem.h" + +#include "SelectCanInterfacesDialog.h" + +SetupDialog::SetupDialog(Backend &backend, QWidget *parent) : + QDialog(parent), + ui(new Ui::SetupDialog), + _backend(&backend), + _currentNetwork(0) +{ + ui->setupUi(this); + + QIcon icon(":/assets/cangaroo.png"); + setWindowIcon(icon); + + _actionAddInterface = new QAction("Add...", this); + _actionDeleteInterface = new QAction("Delete", this); + _actionAddCanDb = new QAction("Add...", this); + _actionDeleteCanDb = new QAction("Delete", this); + _actionReloadCanDbs = new QAction("Reload", this); + + model = new SetupDialogTreeModel(_backend, this); + + ui->treeView->setModel(model); + ui->interfacesTreeView->setModel(model); + ui->candbsTreeView->setModel(model); + + for (int i=0; icolumnCount(); i++) { + ui->treeView->setColumnHidden(i, true); + ui->interfacesTreeView->setColumnHidden(i, true); + ui->candbsTreeView->setColumnHidden(i, true); + } + + ui->treeView->setColumnHidden(SetupDialogTreeModel::column_device, false); + + ui->interfacesTreeView->setColumnHidden(SetupDialogTreeModel::column_device, false); + ui->interfacesTreeView->setColumnHidden(SetupDialogTreeModel::column_driver, false); + ui->interfacesTreeView->setColumnHidden(SetupDialogTreeModel::column_bitrate, false); + + ui->candbsTreeView->setColumnHidden(SetupDialogTreeModel::column_filename, false); + ui->candbsTreeView->setColumnHidden(SetupDialogTreeModel::column_path, false); + + connect(ui->treeView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(treeViewContextMenu(QPoint))); + connect(ui->edNetworkName, SIGNAL(textChanged(QString)), this, SLOT(edNetworkNameChanged())); + + connect(ui->treeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(treeViewSelectionChanged(QItemSelection,QItemSelection))); + connect(ui->candbsTreeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(updateButtons())); + connect(ui->interfacesTreeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(updateButtons())); + + connect(ui->btReloadDatabases, SIGNAL (released()), this, SLOT(executeReloadCanDbs())); + connect(ui->btRefreshNetworks, SIGNAL(released()), this, SLOT(on_btRefreshNetwork_clicked())); + + connect(_actionAddCanDb, SIGNAL(triggered()), this, SLOT(executeAddCanDb())); + connect(_actionDeleteCanDb, SIGNAL(triggered()), this, SLOT(executeDeleteCanDb())); + + connect(_actionAddInterface, SIGNAL(triggered()), this, SLOT(executeAddInterface())); + connect(_actionDeleteInterface, SIGNAL(triggered()), this, SLOT(executeDeleteInterface())); + + + emit backend.onSetupDialogCreated(*this); +} + +SetupDialog::~SetupDialog() +{ + delete ui; + delete model; +} + +void SetupDialog::addPage(QWidget *widget) +{ + ui->stackedWidget->addWidget(widget); +} + +void SetupDialog::displayPage(QWidget *widget) +{ + ui->stackedWidget->setCurrentWidget(widget); +} + +bool SetupDialog::showSetupDialog(MeasurementSetup &setup) +{ + model->load(setup); + ui->treeView->expandAll(); + + QModelIndex first = model->index(0, 0, QModelIndex()); + ui->treeView->setCurrentIndex(first); + + updateButtons(); + return exec()==QDialog::Accepted; +} + +void SetupDialog::treeViewSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected) +{ + (void) selected; + (void) deselected; + + _currentNetwork = 0; + + if (selected.isEmpty()) { + ui->stackedWidget->setCurrentWidget(ui->emptyPage); + updateButtons(); + return; + } + + QModelIndex idx = selected.first().topLeft(); + SetupDialogTreeItem *item = static_cast(idx.internalPointer()); + + + _currentNetwork = item->network; + + if (item->network) { + ui->edNetworkName->setText(item->network->name()); + } + + if (item) { + switch (item->getType()) { + + case SetupDialogTreeItem::type_network: + ui->stackedWidget->setCurrentWidget(ui->networkPage); + break; + + case SetupDialogTreeItem::type_interface_root: + ui->stackedWidget->setCurrentWidget(ui->interfacesPage); + ui->interfacesTreeView->setRootIndex(getSelectedIndex()); + break; + + case SetupDialogTreeItem::type_interface: + emit onShowInterfacePage(*this, item->intf); + break; + + case SetupDialogTreeItem::type_candb_root: + ui->stackedWidget->setCurrentWidget(ui->candbsPage); + ui->candbsTreeView->setRootIndex(getSelectedIndex()); + break; + + default: + ui->stackedWidget->setCurrentWidget(ui->emptyPage); + break; + + + } + } + + updateButtons(); +} + +QModelIndex SetupDialog::getSelectedIndex() +{ + QModelIndexList list = ui->treeView->selectionModel()->selectedRows(); + if (list.isEmpty()) { + return QModelIndex(); + } else { + return list.first(); + } +} + +SetupDialogTreeItem *SetupDialog::getSelectedItem() +{ + const QModelIndex index = getSelectedIndex(); + SetupDialogTreeItem *item = static_cast(index.internalPointer()); + return item; +} + +void SetupDialog::treeViewContextMenu(const QPoint &pos) +{ + (void) pos; + + QMenu contextMenu; + + SetupDialogTreeItem *item = getSelectedItem(); + if (item) { + switch (item->getType()) { + case SetupDialogTreeItem::type_interface_root: + contextMenu.addAction(_actionAddInterface); + break; + case SetupDialogTreeItem::type_interface: + contextMenu.addAction(_actionDeleteInterface); + break; + case SetupDialogTreeItem::type_candb_root: + contextMenu.addAction(_actionAddCanDb); + break; + case SetupDialogTreeItem::type_candb: + contextMenu.addAction(_actionDeleteCanDb); + contextMenu.addAction(_actionReloadCanDbs); + break; + default: + break; + } + } + + + QPoint globalPos = ui->treeView->mapToGlobal(pos); + contextMenu.exec(globalPos); +} + +void SetupDialog::edNetworkNameChanged() +{ + if (_currentNetwork) { + _currentNetwork->setName(ui->edNetworkName->text()); + model->dataChanged(getSelectedIndex(), getSelectedIndex()); + } +} + +void SetupDialog::addInterface(const QModelIndex &parent) +{ + SelectCanInterfacesDialog dlg(0); + CanInterfaceIdList list; + if (dlg.selectInterfaces(*_backend, list, _currentNetwork->getReferencedCanInterfaces())) { + foreach (CanInterfaceId intf, list) { + model->addInterface(parent, intf); + } + } + +} + +void SetupDialog::executeAddInterface() +{ + addInterface(ui->treeView->selectionModel()->currentIndex()); +} + +void SetupDialog::on_btAddInterface_clicked() +{ + addInterface(ui->treeView->selectionModel()->currentIndex()); +} + +void SetupDialog::executeDeleteInterface() +{ + model->deleteInterface(ui->treeView->selectionModel()->currentIndex()); +} + +void SetupDialog::on_btRemoveInterface_clicked() +{ + model->deleteInterface(ui->interfacesTreeView->selectionModel()->currentIndex()); +} + +void SetupDialog::addCanDb(const QModelIndex &parent) +{ + QString filename = QFileDialog::getOpenFileName(this, "Load CAN Database", "", "Vector DBC Files (*.dbc)"); + if (!filename.isNull()) { + pCanDb candb = _backend->loadDbc(filename); + model->addCanDb(parent, candb); + } +} + +void SetupDialog::reloadCanDbs(const QModelIndex &parent) +{ + SetupDialogTreeItem *parentItem = static_cast(parent.internalPointer()); + + parentItem->network->reloadCanDbs(_backend); +} + +void SetupDialog::executeAddCanDb() +{ + addCanDb(ui->treeView->selectionModel()->currentIndex()); +} + + +void SetupDialog::executeReloadCanDbs() +{ + reloadCanDbs(ui->treeView->selectionModel()->currentIndex()); +} + +void SetupDialog::on_btAddDatabase_clicked() +{ + addCanDb(ui->treeView->selectionModel()->currentIndex()); +} + +void SetupDialog::executeDeleteCanDb() +{ + model->deleteCanDb(getSelectedIndex()); +} + +void SetupDialog::on_btRemoveDatabase_clicked() +{ + model->deleteCanDb(ui->candbsTreeView->selectionModel()->currentIndex()); +} + +void SetupDialog::updateButtons() +{ + ui->btRemoveDatabase->setEnabled(ui->candbsTreeView->selectionModel()->hasSelection()); + +// ui->btReloadDatabases->setEnabled(ui->candbsTreeView->children.count() > 0); + + ui->btRemoveInterface->setEnabled(ui->interfacesTreeView->selectionModel()->hasSelection()); + + SetupDialogTreeItem *item = getSelectedItem(); + ui->btRemoveNetwork->setEnabled(ui->treeView->selectionModel()->hasSelection() && item && (item->getType()==SetupDialogTreeItem::type_network)); +} + +void SetupDialog::on_btAddNetwork_clicked() +{ + QModelIndex idx = model->indexOfItem(model->addNetwork()); + ui->treeView->expand(idx); + ui->treeView->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect); +} + +void SetupDialog::on_btRemoveNetwork_clicked() +{ + model->deleteNetwork(getSelectedIndex()); +} + +void SetupDialog::on_btRefreshNetworks_clicked() +{ + _backend->setDefaultSetup(); + showSetupDialog(_backend->getSetup()); +} + diff --git a/cpp/window/SetupDialog/SetupDialog.h b/cpp/window/SetupDialog/SetupDialog.h new file mode 100644 index 0000000..3fb40f8 --- /dev/null +++ b/cpp/window/SetupDialog/SetupDialog.h @@ -0,0 +1,101 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include + +class Backend; +class MeasurementSetup; +class MeasurementNetwork; +class MeasurementInterface; +class QItemSelection; +class SetupDialogTreeItem; +class SetupDialogTreeModel; + +namespace Ui { +class SetupDialog; +} + +class SetupDialog : public QDialog +{ + Q_OBJECT + +public: + explicit SetupDialog(Backend &backend, QWidget *parent = 0); + ~SetupDialog(); + + bool showSetupDialog(MeasurementSetup &setup); + void addPage(QWidget *widget); + void displayPage(QWidget *widget); + +signals: + void onShowInterfacePage(SetupDialog &dlg, MeasurementInterface *mi); + +public slots: + void treeViewSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected); + void treeViewContextMenu(const QPoint& pos); + +private slots: + void edNetworkNameChanged(); + + void on_btAddInterface_clicked(); + void on_btRemoveInterface_clicked(); + + void on_btAddDatabase_clicked(); + void on_btRemoveDatabase_clicked(); + void updateButtons(); + + void executeAddCanDb(); + void executeReloadCanDbs(); + void executeDeleteCanDb(); + + + + void executeAddInterface(); + void executeDeleteInterface(); + + void on_btAddNetwork_clicked(); + void on_btRemoveNetwork_clicked(); + void on_btRefreshNetworks_clicked(); + +private: + Ui::SetupDialog *ui; + Backend *_backend; + + QAction *_actionDeleteInterface; + QAction *_actionDeleteCanDb; + QAction *_actionAddInterface; + QAction *_actionAddCanDb; + QAction *_actionReloadCanDbs; + + SetupDialogTreeModel *model; + MeasurementNetwork *_currentNetwork; + + QModelIndex getSelectedIndex(); + SetupDialogTreeItem *getSelectedItem(); + + void addCanDb(const QModelIndex &parent); + void reloadCanDbs(const QModelIndex &parent); + void addInterface(const QModelIndex &parent); + +}; diff --git a/cpp/window/SetupDialog/SetupDialog.pri b/cpp/window/SetupDialog/SetupDialog.pri new file mode 100644 index 0000000..b9d1164 --- /dev/null +++ b/cpp/window/SetupDialog/SetupDialog.pri @@ -0,0 +1,15 @@ +SOURCES += \ + $$PWD/SetupDialog.cpp \ + $$PWD/SetupDialogTreeModel.cpp \ + $$PWD/SetupDialogTreeItem.cpp \ + $$PWD/SelectCanInterfacesDialog.cpp + +HEADERS += \ + $$PWD/SetupDialog.h \ + $$PWD/SetupDialogTreeModel.h \ + $$PWD/SetupDialogTreeItem.h \ + $$PWD/SelectCanInterfacesDialog.h + +FORMS += \ + $$PWD/SetupDialog.ui \ + $$PWD/SelectCanInterfacesDialog.ui diff --git a/cpp/window/SetupDialog/SetupDialog.ui b/cpp/window/SetupDialog/SetupDialog.ui new file mode 100644 index 0000000..4cfe7d0 --- /dev/null +++ b/cpp/window/SetupDialog/SetupDialog.ui @@ -0,0 +1,315 @@ + + + SetupDialog + + + + 0 + 0 + 1036 + 835 + + + + Measurement Setup + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Horizontal + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 2 + 0 + + + + Qt::CustomContextMenu + + + QAbstractItemView::SingleSelection + + + true + + + false + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Add Network + + + + + + + Remove Network + + + + + + + Refresh + + + + + + + + + + + + 3 + 0 + + + + 0 + + + + + + + + Network name: + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + CAN interfaces assigned to this network: + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Add &Interface... + + + + + + + &Remove Interface + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + false + + + false + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Add &Database... + + + + + + + &Remove Database + + + + + + + Reload Databases + + + + + + + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + SetupDialog + accept() + + + 266 + 825 + + + 157 + 274 + + + + + buttonBox + rejected() + SetupDialog + reject() + + + 334 + 825 + + + 286 + 274 + + + + + diff --git a/cpp/window/SetupDialog/SetupDialogTreeItem.cpp b/cpp/window/SetupDialog/SetupDialogTreeItem.cpp new file mode 100644 index 0000000..bfa3653 --- /dev/null +++ b/cpp/window/SetupDialog/SetupDialogTreeItem.cpp @@ -0,0 +1,117 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "SetupDialogTreeItem.h" +#include +#include +#include "SetupDialogTreeModel.h" + +SetupDialogTreeItem::SetupDialogTreeItem(item_type type, Backend *backend, SetupDialogTreeItem *parent) + : setup(0), network(0), intf(0), candb(0), _backend(backend), _type(type), _parent(parent) +{ + +} + +SetupDialogTreeItem::~SetupDialogTreeItem() +{ + qDeleteAll(_children); +} + +SetupDialogTreeItem *SetupDialogTreeItem::getParentItem() const +{ + return _parent; +} + +int SetupDialogTreeItem::getChildCount() const +{ + return _children.length(); +} + +void SetupDialogTreeItem::appendChild(SetupDialogTreeItem *child) +{ + _children.append(child); +} + +void SetupDialogTreeItem::removeChild(SetupDialogTreeItem *child) +{ + _children.removeAll(child); +} + +SetupDialogTreeItem *SetupDialogTreeItem::child(int row) const +{ + return _children.value(row); +} + +int SetupDialogTreeItem::row() const +{ + if (_parent) { + return _parent->_children.indexOf(const_cast(this)); + } else { + return 0; + } +} + +QVariant SetupDialogTreeItem::dataInterface(const QModelIndex &index) const +{ + switch (index.column()) { + case SetupDialogTreeModel::column_device: + return _backend->getInterfaceName(intf->canInterface()); + case SetupDialogTreeModel::column_driver: + return _backend->getDriverName(intf->canInterface()); + case SetupDialogTreeModel::column_bitrate: + return intf->bitrate(); + default: + return QVariant(); + } +} + +QVariant SetupDialogTreeItem::dataCanDb(const QModelIndex &index) const +{ + switch (index.column()) { + case SetupDialogTreeModel::column_device: + return candb->getFileName(); + case SetupDialogTreeModel::column_filename: + return candb->getFileName(); + case SetupDialogTreeModel::column_path: + return candb->getDirectory(); + default: + return QVariant(); + } +} + +QVariant SetupDialogTreeItem::dataDisplayRole(const QModelIndex &index) const +{ + switch (_type) { + case type_root: return "Setup"; + case type_network: return (network!=0) ? network->name() : QVariant(); + case type_interface_root: return "Interfaces"; + case type_interface: return dataInterface(index); + case type_candb_root: return "Can Databases"; + case type_candb: return dataCanDb(index); + } + return QVariant(); +} + +SetupDialogTreeItem::item_type SetupDialogTreeItem::getType() +{ + return _type; +} + diff --git a/cpp/window/SetupDialog/SetupDialogTreeItem.h b/cpp/window/SetupDialog/SetupDialogTreeItem.h new file mode 100644 index 0000000..2a13108 --- /dev/null +++ b/cpp/window/SetupDialog/SetupDialogTreeItem.h @@ -0,0 +1,74 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include + +#include +#include +#include +#include +#include + +class SetupDialogTreeItem +{ +public: + + typedef enum { + type_root, + type_network, + type_interface_root, + type_interface, + type_candb_root, + type_candb, + } item_type; + +public: + SetupDialogTreeItem(item_type type, Backend *backend, SetupDialogTreeItem *parent=0); + virtual ~SetupDialogTreeItem(); + + MeasurementSetup *setup; + MeasurementNetwork *network; + MeasurementInterface *intf; + pCanDb candb; + + SetupDialogTreeItem *getParentItem() const; + int getChildCount() const; + void appendChild(SetupDialogTreeItem *child); + void removeChild(SetupDialogTreeItem *child); + + SetupDialogTreeItem *child(int row) const; + int row() const; + + QVariant dataDisplayRole(const QModelIndex &index) const; + item_type getType(); + +private: + Backend *_backend; + item_type _type; + SetupDialogTreeItem *_parent; + QList _children; + + QVariant dataInterface(const QModelIndex &index) const; + QVariant dataCanDb(const QModelIndex &index) const; +}; diff --git a/cpp/window/SetupDialog/SetupDialogTreeModel.cpp b/cpp/window/SetupDialog/SetupDialogTreeModel.cpp new file mode 100644 index 0000000..d7b4ae0 --- /dev/null +++ b/cpp/window/SetupDialog/SetupDialogTreeModel.cpp @@ -0,0 +1,263 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "SetupDialogTreeModel.h" + +SetupDialogTreeModel::SetupDialogTreeModel(Backend *backend, QObject *parent) + : QAbstractItemModel(parent), + _backend(backend), + _rootItem(0) +{ +} + +SetupDialogTreeModel::~SetupDialogTreeModel() +{ + if (_rootItem) { + delete _rootItem; + } +} + +QVariant SetupDialogTreeModel::data(const QModelIndex &index, int role) const +{ + SetupDialogTreeItem *item = static_cast(index.internalPointer()); + + if (item) { + + if (role==Qt::DisplayRole) { + return item->dataDisplayRole(index); + } + + } + + return QVariant(); +} + +QVariant SetupDialogTreeModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + (void) orientation; + + if (role==Qt::DisplayRole) { + switch (section) { + case column_device: return "Device"; + case column_driver: return "Driver"; + case column_bitrate: return "Bitrate"; + case column_filename: return "Filename"; + case column_path: return "Path"; + default: return ""; + } + } else { + return QVariant(); + } +} + +QModelIndex SetupDialogTreeModel::index(int row, int column, const QModelIndex &parent) const +{ + if (!hasIndex(row, column, parent)) { + return QModelIndex(); + } else { + SetupDialogTreeItem *parentItem = itemOrRoot(parent); + SetupDialogTreeItem *childItem = parentItem->child(row); + return childItem ? createIndex(row, column, childItem) : QModelIndex(); + } +} + +QModelIndex SetupDialogTreeModel::parent(const QModelIndex &index) const +{ + if (!index.isValid()) { return QModelIndex(); } + + SetupDialogTreeItem *childItem = static_cast(index.internalPointer()); + SetupDialogTreeItem *parentItem = childItem->getParentItem(); + + return (parentItem == _rootItem) ? QModelIndex() : createIndex(parentItem->row(), 0, parentItem); +} + +int SetupDialogTreeModel::rowCount(const QModelIndex &parent) const +{ + int retval = 0; + if (parent.column() <= 0) { + SetupDialogTreeItem *item = itemOrRoot(parent); + if (item) { + retval = item->getChildCount(); + } + } + return retval; +} + +int SetupDialogTreeModel::columnCount(const QModelIndex &parent) const +{ + (void) parent; + return column_count; +} + +QModelIndex SetupDialogTreeModel::indexOfItem(const SetupDialogTreeItem *item) const +{ + return createIndex(item->row(), 0, (void*)item); +} + +SetupDialogTreeItem *SetupDialogTreeModel::addNetwork() +{ + QString s; + int i=0; + while (true) { + s = QString("Network %1").arg(++i); + if (_rootItem->setup->getNetworkByName(s)==0) { + break; + } + } + + beginInsertRows(QModelIndex(), _rootItem->getChildCount(), _rootItem->getChildCount()); + MeasurementNetwork *network = _rootItem->setup->createNetwork(); + network->setName(s); + SetupDialogTreeItem *item = loadNetwork(_rootItem, *network); + endInsertRows(); + + return item; +} + +void SetupDialogTreeModel::deleteNetwork(const QModelIndex &index) +{ + SetupDialogTreeItem *item = static_cast(index.internalPointer()); + beginRemoveRows(index.parent(), index.row(), index.row()); + _rootItem->removeChild(item); + _rootItem->setup->removeNetwork(item->network); + endRemoveRows(); +} + +SetupDialogTreeItem *SetupDialogTreeModel::addCanDb(const QModelIndex &parent, pCanDb db) +{ + SetupDialogTreeItem *parentItem = static_cast(parent.internalPointer()); + if (!parentItem) { return 0; } + + SetupDialogTreeItem *item = 0; + if (parentItem->network) { + beginInsertRows(parent, rowCount(parent), rowCount(parent)); + parentItem->network->addCanDb(db); + item = loadCanDb(*parentItem, db); + endInsertRows(); + } + return item; +} + +void SetupDialogTreeModel::deleteCanDb(const QModelIndex &index) +{ + SetupDialogTreeItem *item = static_cast(index.internalPointer()); + if (!item) { return; } + + SetupDialogTreeItem *parentItem = item->getParentItem(); + if (parentItem && parentItem->network && parentItem->network->_canDbs.contains(item->candb)) { + parentItem->network->_canDbs.removeAll(item->candb); + beginRemoveRows(index.parent(), item->row(), item->row()); + item->getParentItem()->removeChild(item); + endRemoveRows(); + } +} + +SetupDialogTreeItem *SetupDialogTreeModel::addInterface(const QModelIndex &parent, CanInterfaceId &interface) +{ + SetupDialogTreeItem *parentItem = static_cast(parent.internalPointer()); + if (!parentItem) { return 0; } + + SetupDialogTreeItem *item = 0; + if (parentItem && parentItem->network) { + beginInsertRows(parent, parentItem->getChildCount(), parentItem->getChildCount()); + MeasurementInterface *mi = parentItem->network->addCanInterface(interface); + item = loadMeasurementInterface(*parentItem, mi); + endInsertRows(); + } + return item; +} + +void SetupDialogTreeModel::deleteInterface(const QModelIndex &index) +{ + SetupDialogTreeItem *item = static_cast(index.internalPointer()); + if (!item) { return; } + + SetupDialogTreeItem *parentItem = item->getParentItem(); + if (parentItem && parentItem->network && parentItem->network->interfaces().contains(item->intf)) { + parentItem->network->removeInterface(item->intf); + beginRemoveRows(index.parent(), item->row(), item->row()); + item->getParentItem()->removeChild(item); + endRemoveRows(); + } +} + +SetupDialogTreeItem *SetupDialogTreeModel::itemOrRoot(const QModelIndex &index) const +{ + return index.isValid() ? static_cast(index.internalPointer()) : _rootItem; +} + +SetupDialogTreeItem *SetupDialogTreeModel::loadMeasurementInterface(SetupDialogTreeItem &parent, MeasurementInterface *intf) +{ + SetupDialogTreeItem *item = new SetupDialogTreeItem(SetupDialogTreeItem::type_interface, _backend, &parent); + item->intf = intf; + parent.appendChild(item); + return item; +} + +SetupDialogTreeItem *SetupDialogTreeModel::loadCanDb(SetupDialogTreeItem &parent, pCanDb &db) +{ + SetupDialogTreeItem *item = new SetupDialogTreeItem(SetupDialogTreeItem::type_candb, _backend, &parent); + item->candb = db; + parent.appendChild(item); + return item; +} + +SetupDialogTreeItem *SetupDialogTreeModel::loadNetwork(SetupDialogTreeItem *root, MeasurementNetwork &network) +{ + SetupDialogTreeItem *item_network = new SetupDialogTreeItem(SetupDialogTreeItem::type_network, _backend, root); + item_network->network = &network; + + SetupDialogTreeItem *item_intf_root = new SetupDialogTreeItem(SetupDialogTreeItem::type_interface_root, _backend, item_network); + item_intf_root->network = &network; + item_network->appendChild(item_intf_root); + + SetupDialogTreeItem *item_candb_root = new SetupDialogTreeItem(SetupDialogTreeItem::type_candb_root, _backend, item_network); + item_candb_root->network = &network; + item_network->appendChild(item_candb_root); + + foreach (MeasurementInterface *intf, network.interfaces()) { + loadMeasurementInterface(*item_intf_root, intf); + } + + foreach (pCanDb candb, network._canDbs) { + loadCanDb(*item_candb_root, candb); + } + + root->appendChild(item_network); + return item_network; +} + +void SetupDialogTreeModel::load(MeasurementSetup &setup) +{ + SetupDialogTreeItem *_newRoot = new SetupDialogTreeItem(SetupDialogTreeItem::type_root, 0); + _newRoot->setup = &setup; + + foreach (MeasurementNetwork *network, setup.getNetworks()) { + loadNetwork(_newRoot, *network); + } + + beginResetModel(); + SetupDialogTreeItem *_oldRoot = _rootItem; + _rootItem = _newRoot; + delete _oldRoot; + endResetModel(); +} + diff --git a/cpp/window/SetupDialog/SetupDialogTreeModel.h b/cpp/window/SetupDialog/SetupDialogTreeModel.h new file mode 100644 index 0000000..6f53fd1 --- /dev/null +++ b/cpp/window/SetupDialog/SetupDialogTreeModel.h @@ -0,0 +1,77 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include "SetupDialogTreeItem.h" + +#include +#include +#include + +class SetupDialogTreeModel : public QAbstractItemModel +{ + Q_OBJECT + +public: + enum { + column_device, + column_driver, + column_bitrate, + column_filename, + column_path, + column_count + }; + +public: + explicit SetupDialogTreeModel(Backend *backend, QObject *parent=0); + virtual ~SetupDialogTreeModel(); + + QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; + QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE; + int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; + int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; + + QModelIndex indexOfItem(const SetupDialogTreeItem *item) const; + + void load(MeasurementSetup &setup); + + SetupDialogTreeItem *addNetwork(); + void deleteNetwork(const QModelIndex &index); + int getNetworkCount(); + + SetupDialogTreeItem *addCanDb(const QModelIndex &parent, pCanDb db); + void deleteCanDb(const QModelIndex &index); + + SetupDialogTreeItem *addInterface(const QModelIndex &parent, CanInterfaceId &interface); + void deleteInterface(const QModelIndex &index); + +private: + Backend *_backend; + SetupDialogTreeItem *_rootItem; + SetupDialogTreeItem *itemOrRoot(const QModelIndex &index) const; + + SetupDialogTreeItem *loadNetwork(SetupDialogTreeItem *root, MeasurementNetwork &network); + SetupDialogTreeItem *loadMeasurementInterface(SetupDialogTreeItem &parent, MeasurementInterface *intf); + SetupDialogTreeItem *loadCanDb(SetupDialogTreeItem &parent, pCanDb &db); +}; diff --git a/cpp/window/TraceWindow/AggregatedTraceViewItem.cpp b/cpp/window/TraceWindow/AggregatedTraceViewItem.cpp new file mode 100644 index 0000000..28cefc4 --- /dev/null +++ b/cpp/window/TraceWindow/AggregatedTraceViewItem.cpp @@ -0,0 +1,71 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "AggregatedTraceViewItem.h" + +AggregatedTraceViewItem::AggregatedTraceViewItem(AggregatedTraceViewItem *parent) + : _parent(parent) +{ +} + +AggregatedTraceViewItem::~AggregatedTraceViewItem() +{ + qDeleteAll(_children); +} + +void AggregatedTraceViewItem::appendChild(AggregatedTraceViewItem *child) +{ + _children.append(child); +} + +AggregatedTraceViewItem *AggregatedTraceViewItem::child(int row) const +{ + return _children.value(row); +} + +int AggregatedTraceViewItem::childCount() const +{ + return _children.count(); +} + +int AggregatedTraceViewItem::row() const +{ + if (_parent) { + return _parent->_children.indexOf(const_cast(this)); + } else { + return 0; + } +} + +AggregatedTraceViewItem *AggregatedTraceViewItem::parent() const +{ + return _parent; +} + +AggregatedTraceViewItem *AggregatedTraceViewItem::firstChild() const +{ + return _children.first(); +} + +AggregatedTraceViewItem *AggregatedTraceViewItem::lastChild() const +{ + return _children.last(); +} diff --git a/cpp/window/TraceWindow/AggregatedTraceViewItem.h b/cpp/window/TraceWindow/AggregatedTraceViewItem.h new file mode 100644 index 0000000..1536d58 --- /dev/null +++ b/cpp/window/TraceWindow/AggregatedTraceViewItem.h @@ -0,0 +1,48 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include + +class AggregatedTraceViewItem +{ +public: + AggregatedTraceViewItem(AggregatedTraceViewItem *parent); + virtual ~AggregatedTraceViewItem(); + + void appendChild(AggregatedTraceViewItem *child); + AggregatedTraceViewItem *child(int row) const; + int childCount() const; + int row() const; + AggregatedTraceViewItem *parent() const; + AggregatedTraceViewItem *firstChild() const; + AggregatedTraceViewItem *lastChild() const; + + CanMessage _lastmsg, _prevmsg; + +private: + AggregatedTraceViewItem *_parent; + QList _children; + +}; diff --git a/cpp/window/TraceWindow/AggregatedTraceViewModel.cpp b/cpp/window/TraceWindow/AggregatedTraceViewModel.cpp new file mode 100644 index 0000000..13dd315 --- /dev/null +++ b/cpp/window/TraceWindow/AggregatedTraceViewModel.cpp @@ -0,0 +1,220 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "AggregatedTraceViewModel.h" +#include + +#include +#include +#include + +AggregatedTraceViewModel::AggregatedTraceViewModel(Backend &backend) + : BaseTraceViewModel(backend) +{ + _rootItem = new AggregatedTraceViewItem(0); + connect(backend.getTrace(), SIGNAL(beforeAppend(int)), this, SLOT(beforeAppend(int))); + connect(backend.getTrace(), SIGNAL(beforeClear()), this, SLOT(beforeClear())); + connect(backend.getTrace(), SIGNAL(afterClear()), this, SLOT(afterClear())); + + connect(&backend, SIGNAL(onSetupChanged()), this, SLOT(onSetupChanged())); +} + +void AggregatedTraceViewModel::createItem(const CanMessage &msg) +{ + AggregatedTraceViewItem *item = new AggregatedTraceViewItem(_rootItem); + item->_lastmsg = msg; + + CanDbMessage *dbmsg = backend()->findDbMessage(msg); + if (dbmsg) { + for (int i=0; igetSignals().length(); i++) { + item->appendChild(new AggregatedTraceViewItem(item)); + } + } + + _rootItem->appendChild(item); + _map[makeUniqueKey(msg)] = item; +} + +void AggregatedTraceViewModel::updateItem(const CanMessage &msg) +{ + AggregatedTraceViewItem *item = _map.value(makeUniqueKey(msg)); + if (item) { + item->_prevmsg = item->_lastmsg; + item->_lastmsg = msg; + } +} + +void AggregatedTraceViewModel::onUpdateModel() +{ + + if (!_pendingMessageInserts.isEmpty()) { + beginInsertRows(QModelIndex(), _rootItem->childCount(), _rootItem->childCount()+_pendingMessageInserts.size()-1); + foreach (CanMessage msg, _pendingMessageInserts) { + createItem(msg); + } + endInsertRows(); + _pendingMessageInserts.clear(); + } + + if (!_pendingMessageUpdates.isEmpty()) { + foreach (CanMessage msg, _pendingMessageUpdates) { + updateItem(msg); + } + _pendingMessageUpdates.clear(); + } + + if (_rootItem->childCount()>0) { + dataChanged(createIndex(0, 0, _rootItem->firstChild()), createIndex(_rootItem->childCount()-1, column_count-1, _rootItem->lastChild())); + } + +} + +void AggregatedTraceViewModel::onSetupChanged() +{ + beginResetModel(); + endResetModel(); +} + +void AggregatedTraceViewModel::beforeAppend(int num_messages) +{ + CanTrace *trace = backend()->getTrace(); + int start_id = trace->size(); + + for (int i=start_id; igetMessage(i); + unique_key_t key = makeUniqueKey(*msg); + if (_map.contains(key) || _pendingMessageInserts.contains(key)) { + _pendingMessageUpdates.append(*msg); + } else { + _pendingMessageInserts[key] = *msg; + } + } + + onUpdateModel(); +} + +void AggregatedTraceViewModel::beforeClear() +{ + beginResetModel(); + delete _rootItem; + _map.clear(); + _rootItem = new AggregatedTraceViewItem(0); +} + +void AggregatedTraceViewModel::afterClear() +{ + endResetModel(); +} + +AggregatedTraceViewModel::unique_key_t AggregatedTraceViewModel::makeUniqueKey(const CanMessage &msg) const +{ + return ((uint64_t)msg.getInterfaceId() << 32) | msg.getRawId(); +} + +double AggregatedTraceViewModel::getTimeDiff(const timeval t1, const timeval t2) const +{ + double diff = t2.tv_sec - t1.tv_sec; + diff += ((double)(t2.tv_usec - t1.tv_usec)) / 1000000; + return diff; +} + + +QModelIndex AggregatedTraceViewModel::index(int row, int column, const QModelIndex &parent) const +{ + if (!hasIndex(row, column, parent)) { + return QModelIndex(); + } + + const AggregatedTraceViewItem *parentItem = parent.isValid() ? static_cast(parent.internalPointer()) : _rootItem; + const AggregatedTraceViewItem *childItem = parentItem->child(row); + + if (childItem) { + return createIndex(row, column, const_cast(childItem)); + } else { + return QModelIndex(); + } +} + +QModelIndex AggregatedTraceViewModel::parent(const QModelIndex &index) const +{ + if (!index.isValid()) { + return QModelIndex(); + } + + AggregatedTraceViewItem *childItem = (AggregatedTraceViewItem*) index.internalPointer(); + AggregatedTraceViewItem *parentItem = childItem->parent(); + + if (parentItem == _rootItem) { + return QModelIndex(); + } + + return createIndex(parentItem->row(), 0, parentItem); +} + +int AggregatedTraceViewModel::rowCount(const QModelIndex &parent) const +{ + if (parent.column() > 0) { + return 0; + } + + AggregatedTraceViewItem *parentItem; + if (parent.isValid()) { + parentItem = (AggregatedTraceViewItem*)parent.internalPointer(); + } else { + parentItem = _rootItem; + } + return parentItem->childCount(); +} + +QVariant AggregatedTraceViewModel::data_DisplayRole(const QModelIndex &index, int role) const +{ + AggregatedTraceViewItem *item = (AggregatedTraceViewItem *)index.internalPointer(); + if (!item) { return QVariant(); } + + if (item->parent() == _rootItem) { // CanMessage row + return data_DisplayRole_Message(index, role, item->_lastmsg, item->_prevmsg); + } else { // CanSignal Row + return data_DisplayRole_Signal(index, role, item->parent()->_lastmsg); + } +} + +QVariant AggregatedTraceViewModel::data_TextColorRole(const QModelIndex &index, int role) const +{ + (void) role; + AggregatedTraceViewItem *item = (AggregatedTraceViewItem *)index.internalPointer(); + if (!item) { return QVariant(); } + + if (item->parent() == _rootItem) { // CanMessage row + + struct timeval now; + gettimeofday(&now, 0); + + int color = getTimeDiff(item->_lastmsg.getTimestamp(), now)*100; + if (color>200) { color = 200; } + if (color<0) { color = 0; } + + return QVariant::fromValue(QColor(color, color, color)); + } else { // CanSignal Row + return data_TextColorRole_Signal(index, role, item->parent()->_lastmsg); + } +} + + diff --git a/cpp/window/TraceWindow/AggregatedTraceViewModel.h b/cpp/window/TraceWindow/AggregatedTraceViewModel.h new file mode 100644 index 0000000..3e378be --- /dev/null +++ b/cpp/window/TraceWindow/AggregatedTraceViewModel.h @@ -0,0 +1,76 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include +#include + +#include "BaseTraceViewModel.h" +#include +#include + +#include "AggregatedTraceViewItem.h" + + +class CanTrace; + +class AggregatedTraceViewModel : public BaseTraceViewModel +{ + Q_OBJECT + +public: + typedef uint64_t unique_key_t; + typedef QMap CanIdMap; + +public: + AggregatedTraceViewModel(Backend &backend); + + virtual QModelIndex index(int row, int column, const QModelIndex &parent) const; + virtual QModelIndex parent(const QModelIndex &child) const; + virtual int rowCount(const QModelIndex &parent) const; + +private: + CanIdMap _map; + AggregatedTraceViewItem *_rootItem; + QList _pendingMessageUpdates; + QMap _pendingMessageInserts; + + unique_key_t makeUniqueKey(const CanMessage &msg) const; + void createItem(const CanMessage &msg, AggregatedTraceViewItem *item, unique_key_t key); + double getTimeDiff(const timeval t1, const timeval t2) const; + +protected: + virtual QVariant data_DisplayRole(const QModelIndex &index, int role) const; + virtual QVariant data_TextColorRole(const QModelIndex &index, int role) const; + +private slots: + void createItem(const CanMessage &msg); + void updateItem(const CanMessage &msg); + void onUpdateModel(); + void onSetupChanged(); + + void beforeAppend(int num_messages); + void beforeClear(); + void afterClear(); +}; diff --git a/cpp/window/TraceWindow/BaseTraceViewModel.cpp b/cpp/window/TraceWindow/BaseTraceViewModel.cpp new file mode 100644 index 0000000..62f77a5 --- /dev/null +++ b/cpp/window/TraceWindow/BaseTraceViewModel.cpp @@ -0,0 +1,271 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "BaseTraceViewModel.h" + +#include +#include + +#include +#include +#include +#include + +BaseTraceViewModel::BaseTraceViewModel(Backend &backend) +{ + _backend = &backend; +} + +int BaseTraceViewModel::columnCount(const QModelIndex &parent) const +{ + (void) parent; + return column_count; +} + +QVariant BaseTraceViewModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role == Qt::DisplayRole) { + + if (orientation == Qt::Horizontal) { + switch (section) { + case column_timestamp: + return QString("Timestamp"); + case column_channel: + return QString("Channel"); + case column_direction: + return QString("Rx/Tx"); + case column_canid: + return QString("CAN ID"); + case column_sender: + return QString("Sender"); + case column_name: + return QString("Name"); + case column_dlc: + return QString("DLC"); + case column_data: + return QString("Data"); + case column_comment: + return QString("Comment"); + } + } + + } + return QVariant(); +} + + +QVariant BaseTraceViewModel::data(const QModelIndex &index, int role) const +{ + switch (role) { + case Qt::DisplayRole: + return data_DisplayRole(index, role); + case Qt::TextAlignmentRole: + return data_TextAlignmentRole(index, role); + case Qt::TextColorRole: + return data_TextColorRole(index, role); + default: + return QVariant(); + } +} + +Backend *BaseTraceViewModel::backend() const +{ + return _backend; +} + +CanTrace *BaseTraceViewModel::trace() const +{ + return _backend->getTrace(); +} + +timestamp_mode_t BaseTraceViewModel::timestampMode() const +{ + return _timestampMode; +} + +void BaseTraceViewModel::setTimestampMode(timestamp_mode_t timestampMode) +{ + _timestampMode = timestampMode; +} + +QVariant BaseTraceViewModel::formatTimestamp(timestamp_mode_t mode, const CanMessage ¤tMsg, const CanMessage &lastMsg) const +{ + + if (mode==timestamp_mode_delta) { + + double t_current = currentMsg.getFloatTimestamp(); + double t_last = lastMsg.getFloatTimestamp(); + if (t_last==0) { + return QVariant(); + } else { + return QString().sprintf("%.04lf", t_current-t_last); + } + + } else if (mode==timestamp_mode_absolute) { + + return currentMsg.getDateTime().toString("hh:mm:ss.zzz"); + + } else if (mode==timestamp_mode_relative) { + + double t_current = currentMsg.getFloatTimestamp(); + return QString().sprintf("%.04lf", t_current - backend()->getTimestampAtMeasurementStart()); + + } + + return QVariant(); +} + +QVariant BaseTraceViewModel::data_DisplayRole(const QModelIndex &index, int role) const +{ + (void) index; + (void) role; + return QVariant(); +} + +QVariant BaseTraceViewModel::data_DisplayRole_Message(const QModelIndex &index, int role, const CanMessage ¤tMsg, const CanMessage &lastMsg) const +{ + (void) role; + CanDbMessage *dbmsg = backend()->findDbMessage(currentMsg); + + switch (index.column()) { + + case column_timestamp: + return formatTimestamp(_timestampMode, currentMsg, lastMsg); + + case column_channel: + return backend()->getInterfaceName(currentMsg.getInterfaceId()); + + case column_direction: + return "rx"; + + case column_canid: + return currentMsg.getIdString(); + + case column_name: + return (dbmsg) ? dbmsg->getName() : ""; + + case column_sender: + return (dbmsg) ? dbmsg->getSender()->name() : ""; + + case column_dlc: + return currentMsg.getLength(); + + case column_data: + return currentMsg.getDataHexString(); + + case column_comment: + return (dbmsg) ? dbmsg->getComment() : ""; + + default: + return QVariant(); + + } +} + +QVariant BaseTraceViewModel::data_DisplayRole_Signal(const QModelIndex &index, int role, const CanMessage &msg) const +{ + (void) role; + uint64_t raw_data; + QString value_name; + QString unit; + + CanDbMessage *dbmsg = backend()->findDbMessage(msg); + if (!dbmsg) { return QVariant(); } + + CanDbSignal *dbsignal = dbmsg->getSignal(index.row()); + if (!dbsignal) { return QVariant(); } + + switch (index.column()) { + + case column_name: + return dbsignal->name(); + + case column_data: + + if (dbsignal->isPresentInMessage(msg)) { + raw_data = dbsignal->extractRawDataFromMessage(msg); + } else { + if (!trace()->getMuxedSignalFromCache(dbsignal, &raw_data)) { + return QVariant(); + } + } + + value_name = dbsignal->getValueName(raw_data); + if (value_name.isEmpty()) { + unit = dbsignal->getUnit(); + if (unit.isEmpty()) { + return dbsignal->convertRawValueToPhysical(raw_data); + } else { + return QString("%1 %2").arg(dbsignal->convertRawValueToPhysical(raw_data)).arg(unit); + } + } else { + return QString("%1 - %2").arg(raw_data).arg(value_name); + } + + case column_comment: + return dbsignal->comment().replace('\n', ' '); + + default: + return QVariant(); + + } +} + +QVariant BaseTraceViewModel::data_TextAlignmentRole(const QModelIndex &index, int role) const +{ + (void) role; + switch (index.column()) { + case column_timestamp: return Qt::AlignRight + Qt::AlignVCenter; + case column_channel: return Qt::AlignCenter + Qt::AlignVCenter; + case column_direction: return Qt::AlignCenter + Qt::AlignVCenter; + case column_canid: return Qt::AlignRight + Qt::AlignVCenter; + case column_sender: return Qt::AlignLeft + Qt::AlignVCenter; + case column_name: return Qt::AlignLeft + Qt::AlignVCenter; + case column_dlc: return Qt::AlignCenter + Qt::AlignVCenter; + case column_data: return Qt::AlignLeft + Qt::AlignVCenter; + case column_comment: return Qt::AlignLeft + Qt::AlignVCenter; + default: return QVariant(); + } +} + +QVariant BaseTraceViewModel::data_TextColorRole(const QModelIndex &index, int role) const +{ + (void) index; + (void) role; + return QVariant(); +} + +QVariant BaseTraceViewModel::data_TextColorRole_Signal(const QModelIndex &index, int role, const CanMessage &msg) const +{ + (void) role; + + CanDbMessage *dbmsg = backend()->findDbMessage(msg); + if (!dbmsg) { return QVariant(); } + + CanDbSignal *dbsignal = dbmsg->getSignal(index.row()); + if (!dbsignal) { return QVariant(); } + + if (dbsignal->isPresentInMessage(msg)) { + return QVariant(); // default text color + } else { + return QVariant::fromValue(QColor(200,200,200)); + } +} diff --git a/cpp/window/TraceWindow/BaseTraceViewModel.h b/cpp/window/TraceWindow/BaseTraceViewModel.h new file mode 100644 index 0000000..3ef60cc --- /dev/null +++ b/cpp/window/TraceWindow/BaseTraceViewModel.h @@ -0,0 +1,76 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include "TraceViewTypes.h" + +class Backend; +class CanTrace; +class CanMessage; +class CanDbSignal; + +class BaseTraceViewModel : public QAbstractItemModel +{ + Q_OBJECT + +public: + enum { + column_timestamp, + column_channel, + column_direction, + column_canid, + column_sender, + column_name, + column_dlc, + column_data, + column_comment, + column_count + }; + +public: + BaseTraceViewModel(Backend &backend); + virtual int columnCount(const QModelIndex &parent) const; + virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const; + virtual QVariant data(const QModelIndex &index, int role) const; + + Backend *backend() const; + CanTrace *trace() const; + + timestamp_mode_t timestampMode() const; + void setTimestampMode(timestamp_mode_t timestampMode); + +protected: + virtual QVariant data_DisplayRole(const QModelIndex &index, int role) const; + virtual QVariant data_DisplayRole_Message(const QModelIndex &index, int role, const CanMessage ¤tMsg, const CanMessage &lastMsg) const; + virtual QVariant data_DisplayRole_Signal(const QModelIndex &index, int role, const CanMessage &msg) const; + virtual QVariant data_TextAlignmentRole(const QModelIndex &index, int role) const; + virtual QVariant data_TextColorRole(const QModelIndex &index, int role) const; + virtual QVariant data_TextColorRole_Signal(const QModelIndex &index, int role, const CanMessage &msg) const; + + QVariant formatTimestamp(timestamp_mode_t mode, const CanMessage ¤tMsg, const CanMessage &lastMsg) const; + +private: + Backend *_backend; + timestamp_mode_t _timestampMode; + +}; diff --git a/cpp/window/TraceWindow/LinearTraceViewModel.cpp b/cpp/window/TraceWindow/LinearTraceViewModel.cpp new file mode 100644 index 0000000..a6ce1e5 --- /dev/null +++ b/cpp/window/TraceWindow/LinearTraceViewModel.cpp @@ -0,0 +1,143 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "LinearTraceViewModel.h" +#include +#include +#include + +LinearTraceViewModel::LinearTraceViewModel(Backend &backend) + : BaseTraceViewModel(backend) +{ + connect(backend.getTrace(), SIGNAL(beforeAppend(int)), this, SLOT(beforeAppend(int))); + connect(backend.getTrace(), SIGNAL(afterAppend()), this, SLOT(afterAppend())); + connect(backend.getTrace(), SIGNAL(beforeClear()), this, SLOT(beforeClear())); + connect(backend.getTrace(), SIGNAL(afterClear()), this, SLOT(afterClear())); +} + +QModelIndex LinearTraceViewModel::index(int row, int column, const QModelIndex &parent) const +{ + if (parent.isValid() && parent.internalId()) { + return createIndex(row, column, (unsigned int)(0x80000000 | parent.internalId())); + } else { + return createIndex(row, column, row+1); + } +} + +QModelIndex LinearTraceViewModel::parent(const QModelIndex &child) const +{ + (void) child; + quintptr id = child.internalId(); + if (id & 0x80000000) { + return createIndex(id & 0x7FFFFFFF, 0, (unsigned int)(id & 0x7FFFFFFF)); + } + return QModelIndex(); +} + +int LinearTraceViewModel::rowCount(const QModelIndex &parent) const +{ + if (parent.isValid()) { + quintptr id = parent.internalId(); + if (id & 0x80000000) { // node of a message + return 0; + } else { // a message + const CanMessage *msg = trace()->getMessage(id-1); + if (msg) { + CanDbMessage *dbmsg = backend()->findDbMessage(*msg); + return (dbmsg!=0) ? dbmsg->getSignals().length() : 0; + } else { + return 0; + } + } + } else { + return trace()->size(); + } +} + +int LinearTraceViewModel::columnCount(const QModelIndex &parent) const +{ + (void) parent; + return column_count; +} + +bool LinearTraceViewModel::hasChildren(const QModelIndex &parent) const +{ + return rowCount(parent)>0; +} + +void LinearTraceViewModel::beforeAppend(int num_messages) +{ + beginInsertRows(QModelIndex(), trace()->size(), trace()->size()+num_messages-1); +} + +void LinearTraceViewModel::afterAppend() +{ + endInsertRows(); +} + +void LinearTraceViewModel::beforeClear() +{ + beginResetModel(); +} + +void LinearTraceViewModel::afterClear() +{ + endResetModel(); +} + +QVariant LinearTraceViewModel::data_DisplayRole(const QModelIndex &index, int role) const +{ + quintptr id = index.internalId(); + int msg_id = (id & ~0x80000000)-1; + + const CanMessage *msg = trace()->getMessage(msg_id); + if (!msg) { return QVariant(); } + + if (id & 0x80000000) { + return data_DisplayRole_Signal(index, role, *msg); + } else if (id) { + if (msg_id>1) { + const CanMessage *prev_msg = trace()->getMessage(msg_id-1); + return data_DisplayRole_Message(index, role, *msg, *prev_msg); + } else { + return data_DisplayRole_Message(index, role, *msg, CanMessage()); + } + } + + return QVariant(); +} + +QVariant LinearTraceViewModel::data_TextColorRole(const QModelIndex &index, int role) const +{ + (void) role; + + quintptr id = index.internalId(); + + if (id & 0x80000000) { // CanSignal row + int msg_id = (id & ~0x80000000)-1; + const CanMessage *msg = trace()->getMessage(msg_id); + if (msg) { + return data_TextColorRole_Signal(index, role, *msg); + } + } + + return QVariant(); +} diff --git a/cpp/window/TraceWindow/LinearTraceViewModel.h b/cpp/window/TraceWindow/LinearTraceViewModel.h new file mode 100644 index 0000000..ede5085 --- /dev/null +++ b/cpp/window/TraceWindow/LinearTraceViewModel.h @@ -0,0 +1,53 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include +#include +#include "BaseTraceViewModel.h" + +class Backend; + +class LinearTraceViewModel : public BaseTraceViewModel +{ + Q_OBJECT + +public: + LinearTraceViewModel(Backend &backend); + + virtual QModelIndex index(int row, int column, const QModelIndex &parent) const; + virtual QModelIndex parent(const QModelIndex &child) const; + virtual int rowCount(const QModelIndex &parent) const; + virtual int columnCount(const QModelIndex &parent) const; + virtual bool hasChildren(const QModelIndex &parent) const; + +private slots: + void beforeAppend(int num_messages); + void afterAppend(); + void beforeClear(); + void afterClear(); + +private: + virtual QVariant data_DisplayRole(const QModelIndex &index, int role) const; + virtual QVariant data_TextColorRole(const QModelIndex &index, int role) const; +}; diff --git a/cpp/window/TraceWindow/TraceFilterModel.cpp b/cpp/window/TraceWindow/TraceFilterModel.cpp new file mode 100644 index 0000000..7a8102e --- /dev/null +++ b/cpp/window/TraceWindow/TraceFilterModel.cpp @@ -0,0 +1,41 @@ +#include "TraceFilterModel.h" + +TraceFilterModel::TraceFilterModel(QObject *parent) + : QSortFilterProxyModel{parent}, + _filterText("") +{ + setRecursiveFilteringEnabled(false); +} + + +void TraceFilterModel::setFilterText(QString filtertext) +{ + _filterText = filtertext; +} + +bool TraceFilterModel::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const +{ + // Pass all on no filter + if(_filterText.length() == 0) + return true; + + QModelIndex idx0 = sourceModel()->index(source_row, 1, source_parent); // Channel + QModelIndex idx1 = sourceModel()->index(source_row, 3, source_parent); // CAN ID + QModelIndex idx2 = sourceModel()->index(source_row, 4, source_parent); // Sender + QModelIndex idx3 = sourceModel()->index(source_row, 5, source_parent); // Name + + QString datastr0 = sourceModel()->data(idx0).toString(); + QString datastr1 = sourceModel()->data(idx1).toString(); + QString datastr2 = sourceModel()->data(idx2).toString(); + QString datastr3 = sourceModel()->data(idx3).toString(); + + fprintf(stderr, "Data for acceptance is %s\r\n", datastr1.toStdString().c_str()); + + if( datastr0.contains(_filterText) || + datastr1.contains(_filterText) || + datastr2.contains(_filterText) || + datastr3.contains(_filterText)) + return true; + else + return false; +} diff --git a/cpp/window/TraceWindow/TraceFilterModel.h b/cpp/window/TraceWindow/TraceFilterModel.h new file mode 100644 index 0000000..12f2d07 --- /dev/null +++ b/cpp/window/TraceWindow/TraceFilterModel.h @@ -0,0 +1,21 @@ +#ifndef TRACEFILTER_H +#define TRACEFILTER_H + +#include + +class TraceFilterModel : public QSortFilterProxyModel +{ +public: + explicit TraceFilterModel(QObject *parent = nullptr); + + +public slots: + void setFilterText(QString filtertext); + +private: + QString _filterText; +protected: + virtual bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const override; +}; + +#endif // TRACEFILTER_H diff --git a/cpp/window/TraceWindow/TraceViewTypes.h b/cpp/window/TraceWindow/TraceViewTypes.h new file mode 100644 index 0000000..aecd0a1 --- /dev/null +++ b/cpp/window/TraceWindow/TraceViewTypes.h @@ -0,0 +1,30 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +typedef enum timestamp_mode { + timestamp_mode_absolute, + timestamp_mode_relative, + timestamp_mode_delta, + timestamp_modes_count +} timestamp_mode_t; + diff --git a/cpp/window/TraceWindow/TraceWindow.cpp b/cpp/window/TraceWindow/TraceWindow.cpp new file mode 100644 index 0000000..e71d304 --- /dev/null +++ b/cpp/window/TraceWindow/TraceWindow.cpp @@ -0,0 +1,217 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#include "TraceWindow.h" +#include "ui_TraceWindow.h" + +#include +#include +#include "LinearTraceViewModel.h" +#include "AggregatedTraceViewModel.h" +#include "TraceFilterModel.h" + +TraceWindow::TraceWindow(QWidget *parent, Backend &backend) : + ConfigurableWidget(parent), + ui(new Ui::TraceWindow), + _backend(&backend) +{ + ui->setupUi(this); + + _linearTraceViewModel = new LinearTraceViewModel(backend); + _linearProxyModel = new QSortFilterProxyModel(this); + _linearProxyModel->setSourceModel(_linearTraceViewModel); + _linearProxyModel->setDynamicSortFilter(true); + + _aggregatedTraceViewModel = new AggregatedTraceViewModel(backend); + _aggregatedProxyModel = new QSortFilterProxyModel(this); + _aggregatedProxyModel->setSourceModel(_aggregatedTraceViewModel); + _aggregatedProxyModel->setDynamicSortFilter(true); + + _aggFilteredModel = new TraceFilterModel(this); + _aggFilteredModel->setSourceModel(_aggregatedProxyModel); + _linFilteredModel = new TraceFilterModel(this); + _linFilteredModel->setSourceModel(_linearProxyModel); + + + + setMode(mode_aggregated); + setAutoScroll(false); + + QFont font("Monospace"); + font.setStyleHint(QFont::TypeWriter); + ui->tree->setFont(font); + ui->tree->setAlternatingRowColors(true); + + ui->tree->setUniformRowHeights(true); + ui->tree->setColumnWidth(0, 120); + ui->tree->setColumnWidth(1, 70); + ui->tree->setColumnWidth(2, 50); + ui->tree->setColumnWidth(3, 90); + ui->tree->setColumnWidth(4, 200); + ui->tree->setColumnWidth(5, 200); + ui->tree->setColumnWidth(6, 50); + ui->tree->setColumnWidth(7, 200); + ui->tree->sortByColumn(BaseTraceViewModel::column_canid, Qt::AscendingOrder); + + ui->cbTimestampMode->addItem("absolute", 0); + ui->cbTimestampMode->addItem("relative", 1); + ui->cbTimestampMode->addItem("delta", 2); + setTimestampMode(timestamp_mode_delta); + + connect(_linearTraceViewModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(rowsInserted(QModelIndex,int,int))); + + connect(ui->filterLineEdit, SIGNAL(textChanged(QString)), this, SLOT(on_cbFilterChanged())); +} + +TraceWindow::~TraceWindow() +{ + delete ui; + delete _aggregatedTraceViewModel; + delete _linearTraceViewModel; +} + +void TraceWindow::setMode(TraceWindow::mode_t mode) +{ + bool isChanged = (_mode != mode); + _mode = mode; + + if (_mode==mode_linear) { + ui->tree->setSortingEnabled(false); + ui->tree->setModel(_linFilteredModel); //_linearTraceViewModel); + ui->cbAutoScroll->setEnabled(true); + } else { + ui->tree->setSortingEnabled(true); + ui->tree->setModel(_aggFilteredModel); //_aggregatedProxyModel); + ui->cbAutoScroll->setEnabled(false); + } + + if (isChanged) { + ui->cbAggregated->setChecked(_mode==mode_aggregated); + emit(settingsChanged(this)); + } +} + +void TraceWindow::setAutoScroll(bool doAutoScroll) +{ + if (doAutoScroll != _doAutoScroll) { + _doAutoScroll = doAutoScroll; + ui->cbAutoScroll->setChecked(_doAutoScroll); + emit(settingsChanged(this)); + } +} + +void TraceWindow::setTimestampMode(int mode) +{ + timestamp_mode_t new_mode; + if ( (mode>=0) && (modesetTimestampMode(new_mode); + _linearTraceViewModel->setTimestampMode(new_mode); + + if (new_mode != _timestampMode) { + _timestampMode = new_mode; + for (int i=0; icbTimestampMode->count(); i++) { + if (ui->cbTimestampMode->itemData(i).toInt() == new_mode) { + ui->cbTimestampMode->setCurrentIndex(i); + } + } + emit(settingsChanged(this)); + } +} + +bool TraceWindow::saveXML(Backend &backend, QDomDocument &xml, QDomElement &root) +{ + if (!ConfigurableWidget::saveXML(backend, xml, root)) { + return false; + } + + root.setAttribute("type", "TraceWindow"); + root.setAttribute("mode", (_mode==mode_linear) ? "linear" : "aggregated"); + root.setAttribute("TimestampMode", _timestampMode); + + QDomElement elLinear = xml.createElement("LinearTraceView"); + elLinear.setAttribute("AutoScroll", (ui->cbAutoScroll->checkState() == Qt::Checked) ? 1 : 0); + root.appendChild(elLinear); + + QDomElement elAggregated = xml.createElement("AggregatedTraceView"); + elAggregated.setAttribute("SortColumn", _aggregatedProxyModel->sortColumn()); + root.appendChild(elAggregated); + + return true; +} + +bool TraceWindow::loadXML(Backend &backend, QDomElement &el) +{ + if (!ConfigurableWidget::loadXML(backend, el)) { + return false; + } + + setMode((el.attribute("mode", "linear") == "linear") ? mode_linear : mode_aggregated); + setTimestampMode(el.attribute("TimestampMode", "0").toInt()); + + QDomElement elLinear = el.firstChildElement("LinearTraceView"); + setAutoScroll(elLinear.attribute("AutoScroll", "0").toInt() != 0); + + QDomElement elAggregated = el.firstChildElement("AggregatedTraceView"); + int sortColumn = elAggregated.attribute("SortColumn", "-1").toInt(); + ui->tree->sortByColumn(sortColumn); + + return true; +} + +void TraceWindow::rowsInserted(const QModelIndex &parent, int first, int last) +{ + (void) parent; + (void) first; + (void) last; + + if ((_mode==mode_linear) && (ui->cbAutoScroll->checkState() == Qt::Checked)) { + ui->tree->scrollToBottom(); + } + +} + +void TraceWindow::on_cbAggregated_stateChanged(int i) +{ + setMode( (i==Qt::Checked) ? mode_aggregated : mode_linear ); +} + +void TraceWindow::on_cbAutoScroll_stateChanged(int i) +{ + setAutoScroll(i==Qt::Checked); +} + +void TraceWindow::on_cbTimestampMode_currentIndexChanged(int index) +{ + setTimestampMode((timestamp_mode_t)ui->cbTimestampMode->itemData(index).toInt()); +} + +void TraceWindow::on_cbFilterChanged() +{ + _aggFilteredModel->setFilterText(ui->filterLineEdit->text()); + _linFilteredModel->setFilterText(ui->filterLineEdit->text()); + _aggFilteredModel->invalidate(); + _linFilteredModel->invalidate(); +} diff --git a/cpp/window/TraceWindow/TraceWindow.h b/cpp/window/TraceWindow/TraceWindow.h new file mode 100644 index 0000000..ba3ee4a --- /dev/null +++ b/cpp/window/TraceWindow/TraceWindow.h @@ -0,0 +1,82 @@ +/* + + Copyright (c) 2015, 2016 Hubert Denkmair + + This file is part of cangaroo. + + cangaroo is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + cangaroo is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cangaroo. If not, see . + +*/ + +#pragma once + +#include +#include "TraceViewTypes.h" +#include "TraceFilterModel.h" + +namespace Ui { +class TraceWindow; +} + +class QDomDocument; +class QDomElement; +class QSortFilterProxyModel; +class LinearTraceViewModel; +class AggregatedTraceViewModel; +class Backend; + +class TraceWindow : public ConfigurableWidget +{ + Q_OBJECT + +public: + typedef enum mode { + mode_linear, + mode_aggregated + } mode_t; + + explicit TraceWindow(QWidget *parent, Backend &backend); + ~TraceWindow(); + + void setMode(mode_t mode); + void setAutoScroll(bool doAutoScroll); + void setTimestampMode(int mode); + + virtual bool saveXML(Backend &backend, QDomDocument &xml, QDomElement &root); + virtual bool loadXML(Backend &backend, QDomElement &el); + +public slots: + void rowsInserted(const QModelIndex & parent, int first, int last); + +private slots: + void on_cbAggregated_stateChanged(int i); + void on_cbAutoScroll_stateChanged(int i); + + void on_cbTimestampMode_currentIndexChanged(int index); + void on_cbFilterChanged(void); + +private: + Ui::TraceWindow *ui; + Backend *_backend; + mode_t _mode; + bool _doAutoScroll; + timestamp_mode_t _timestampMode; + + TraceFilterModel * _aggFilteredModel; + TraceFilterModel * _linFilteredModel; + LinearTraceViewModel *_linearTraceViewModel; + AggregatedTraceViewModel *_aggregatedTraceViewModel; + QSortFilterProxyModel *_aggregatedProxyModel; + QSortFilterProxyModel *_linearProxyModel; +}; diff --git a/cpp/window/TraceWindow/TraceWindow.pri b/cpp/window/TraceWindow/TraceWindow.pri new file mode 100644 index 0000000..cdc268d --- /dev/null +++ b/cpp/window/TraceWindow/TraceWindow.pri @@ -0,0 +1,19 @@ +SOURCES += \ + $$PWD/TraceFilterModel.cpp \ + $$PWD/LinearTraceViewModel.cpp \ + $$PWD/AggregatedTraceViewModel.cpp \ + $$PWD/BaseTraceViewModel.cpp \ + $$PWD/AggregatedTraceViewItem.cpp \ + $$PWD/TraceWindow.cpp \ + +HEADERS += \ + $$PWD/LinearTraceViewModel.h \ + $$PWD/AggregatedTraceViewModel.h \ + $$PWD/BaseTraceViewModel.h \ + $$PWD/AggregatedTraceViewItem.h \ + $$PWD/TraceFilterModel.h \ + $$PWD/TraceWindow.h \ + $$PWD/TraceViewTypes.h \ + +FORMS += \ + $$PWD/TraceWindow.ui diff --git a/cpp/window/TraceWindow/TraceWindow.ui b/cpp/window/TraceWindow/TraceWindow.ui new file mode 100644 index 0000000..691d22e --- /dev/null +++ b/cpp/window/TraceWindow/TraceWindow.ui @@ -0,0 +1,96 @@ + + + TraceWindow + + + + 0 + 0 + 918 + 616 + + + + Trace View + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Timestamps: + + + + + + + + + + aggregate by ID + + + false + + + + + + + auto scroll + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Filter: + + + + + + + + + + + + + QAbstractItemView::ScrollPerPixel + + + + + + + + diff --git a/fonts/fonts.qrc b/fonts/fonts.qrc new file mode 100644 index 0000000..df94610 --- /dev/null +++ b/fonts/fonts.qrc @@ -0,0 +1,16 @@ + + + fonts/Roboto/Roboto-Black.ttf + fonts/Roboto/Roboto-BlackItalic.ttf + fonts/Roboto/Roboto-Bold.ttf + fonts/Roboto/Roboto-BoldItalic.ttf + fonts/Roboto/Roboto-Italic.ttf + fonts/Roboto/Roboto-Light.ttf + fonts/Roboto/Roboto-LightItalic.ttf + fonts/Roboto/Roboto-Medium.ttf + fonts/Roboto/Roboto-MediumItalic.ttf + fonts/Roboto/Roboto-Regular.ttf + fonts/Roboto/Roboto-Thin.ttf + fonts/Roboto/Roboto-ThinItalic.ttf + + diff --git a/fonts/fonts/Roboto/LICENSE.txt b/fonts/fonts/Roboto/LICENSE.txt new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/fonts/fonts/Roboto/LICENSE.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/fonts/fonts/Roboto/Roboto-Black.ttf b/fonts/fonts/Roboto/Roboto-Black.ttf new file mode 100644 index 0000000..0112e7d Binary files /dev/null and b/fonts/fonts/Roboto/Roboto-Black.ttf differ diff --git a/fonts/fonts/Roboto/Roboto-BlackItalic.ttf b/fonts/fonts/Roboto/Roboto-BlackItalic.ttf new file mode 100644 index 0000000..b2c6aca Binary files /dev/null and b/fonts/fonts/Roboto/Roboto-BlackItalic.ttf differ diff --git a/fonts/fonts/Roboto/Roboto-Bold.ttf b/fonts/fonts/Roboto/Roboto-Bold.ttf new file mode 100644 index 0000000..43da14d Binary files /dev/null and b/fonts/fonts/Roboto/Roboto-Bold.ttf differ diff --git a/fonts/fonts/Roboto/Roboto-BoldItalic.ttf b/fonts/fonts/Roboto/Roboto-BoldItalic.ttf new file mode 100644 index 0000000..bcfdab4 Binary files /dev/null and b/fonts/fonts/Roboto/Roboto-BoldItalic.ttf differ diff --git a/fonts/fonts/Roboto/Roboto-Italic.ttf b/fonts/fonts/Roboto/Roboto-Italic.ttf new file mode 100644 index 0000000..1b5eaa3 Binary files /dev/null and b/fonts/fonts/Roboto/Roboto-Italic.ttf differ diff --git a/fonts/fonts/Roboto/Roboto-Light.ttf b/fonts/fonts/Roboto/Roboto-Light.ttf new file mode 100644 index 0000000..e7307e7 Binary files /dev/null and b/fonts/fonts/Roboto/Roboto-Light.ttf differ diff --git a/fonts/fonts/Roboto/Roboto-LightItalic.ttf b/fonts/fonts/Roboto/Roboto-LightItalic.ttf new file mode 100644 index 0000000..2d277af Binary files /dev/null and b/fonts/fonts/Roboto/Roboto-LightItalic.ttf differ diff --git a/fonts/fonts/Roboto/Roboto-Medium.ttf b/fonts/fonts/Roboto/Roboto-Medium.ttf new file mode 100644 index 0000000..ac0f908 Binary files /dev/null and b/fonts/fonts/Roboto/Roboto-Medium.ttf differ diff --git a/fonts/fonts/Roboto/Roboto-MediumItalic.ttf b/fonts/fonts/Roboto/Roboto-MediumItalic.ttf new file mode 100644 index 0000000..fc36a47 Binary files /dev/null and b/fonts/fonts/Roboto/Roboto-MediumItalic.ttf differ diff --git a/fonts/fonts/Roboto/Roboto-Regular.ttf b/fonts/fonts/Roboto/Roboto-Regular.ttf new file mode 100644 index 0000000..ddf4bfa Binary files /dev/null and b/fonts/fonts/Roboto/Roboto-Regular.ttf differ diff --git a/fonts/fonts/Roboto/Roboto-Thin.ttf b/fonts/fonts/Roboto/Roboto-Thin.ttf new file mode 100644 index 0000000..2e0dee6 Binary files /dev/null and b/fonts/fonts/Roboto/Roboto-Thin.ttf differ diff --git a/fonts/fonts/Roboto/Roboto-ThinItalic.ttf b/fonts/fonts/Roboto/Roboto-ThinItalic.ttf new file mode 100644 index 0000000..084f9c0 Binary files /dev/null and b/fonts/fonts/Roboto/Roboto-ThinItalic.ttf differ diff --git a/qml/Controls/ColoredLabel.qml b/qml/Controls/ColoredLabel.qml new file mode 100644 index 0000000..e4f84c5 --- /dev/null +++ b/qml/Controls/ColoredLabel.qml @@ -0,0 +1,23 @@ +import QtQuick 2.12 +import QtQuick.Layouts 1.12 +import QtQuick.Controls 2.12 as QuickControls + +import Utils 1.0 + +QuickControls.Label { + font.family: "Roboto" + font.weight: Font.Normal + font.pixelSize: Ui.scale(17) + + topPadding: Ui.scale(6) + bottomPadding: Ui.scale(6) + leftPadding: Ui.scale(12) + rightPadding: Ui.scale(12) + + property color backgroundColor: Palette.backgroundColor + + background: Rectangle { + radius: Ui.scale(4) + color: backgroundColor + } +} diff --git a/qml/Controls/Indicator.qml b/qml/Controls/Indicator.qml new file mode 100644 index 0000000..1e32d68 --- /dev/null +++ b/qml/Controls/Indicator.qml @@ -0,0 +1,33 @@ +import QtQuick 2.12 +import QtQuick.Layouts 1.12 + +import Controls 1.0 +import Utils 1.0 + +GridLayout { + columns: 2 + columnSpacing: Ui.scale(8) + rowSpacing: Ui.scale(4) + + property string label + property string indicator + property alias value: valueField + + SecondaryLabel { + text: label + topPadding: lineCount == 1 ? Ui.scale(12) : 0 + Layout.columnSpan: 2 + Layout.fillWidth: true + } + + TextField { + id: valueField + selectByMouse: true + Layout.fillWidth: true + } + + IndicatorLabel { + text: indicator + Layout.preferredWidth: Ui.scale(18) + } +} diff --git a/qml/Controls/IndicatorLabel.qml b/qml/Controls/IndicatorLabel.qml new file mode 100644 index 0000000..91eab71 --- /dev/null +++ b/qml/Controls/IndicatorLabel.qml @@ -0,0 +1,12 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.12 as QuickControls + +import Utils 1.0 + +QuickControls.Label { + id: control + font.pixelSize: Ui.scale(21) + font.family: "Roboto" + font.weight: Font.DemiBold + color: Palette.indicatorLabelColor +} diff --git a/qml/Controls/PrimaryButton.qml b/qml/Controls/PrimaryButton.qml new file mode 100644 index 0000000..9ab799c --- /dev/null +++ b/qml/Controls/PrimaryButton.qml @@ -0,0 +1,34 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.12 as QuickControls + +import Utils 1.0 + +QuickControls.Button { + id: control + + implicitHeight: Ui.scale(52) + + font.pixelSize: Ui.scale(21) + font.family: "Roboto" + font.weight: Font.DemiBold + + contentItem: Text { + text: control.text + font: control.font + opacity: enabled ? 1.0 : 0.3 + color: Palette.primaryButtonTextColor + horizontalAlignment: Text.AlignHCenter + verticalAlignment: Text.AlignVCenter + elide: Text.ElideRight + } + + background: Rectangle { + radius: Ui.scale(32) + color: control.down ? Palette.primaryButtonPushedBackgroundColor : Palette.primaryButtonBackgroundColor + border.width: Ui.scale(2) + border.color: color + + width: control.width + height: control.height + } +} diff --git a/qml/Controls/PrimaryLabel.qml b/qml/Controls/PrimaryLabel.qml new file mode 100644 index 0000000..c921612 --- /dev/null +++ b/qml/Controls/PrimaryLabel.qml @@ -0,0 +1,12 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.12 as QuickControls + +import Utils 1.0 + +QuickControls.Label { + id: control + font.pixelSize: Ui.scale(21) + font.family: "Roboto" + font.weight: Font.Bold + color: Palette.primaryLabelColor +} diff --git a/qml/Controls/SecondaryButton.qml b/qml/Controls/SecondaryButton.qml new file mode 100644 index 0000000..6b7fc41 --- /dev/null +++ b/qml/Controls/SecondaryButton.qml @@ -0,0 +1,34 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.12 as QuickControls + +import Utils 1.0 + +QuickControls.Button { + id: control + + implicitHeight: Ui.scale(52) + + font.pixelSize: Ui.scale(21) + font.family: "Roboto" + font.weight: Font.DemiBold + + contentItem: Text { + text: control.text + font: control.font + opacity: enabled ? 1.0 : 0.3 + color: Palette.secondaryButtonTextColor + horizontalAlignment: Text.AlignHCenter + verticalAlignment: Text.AlignVCenter + elide: Text.ElideRight + } + + background: Rectangle { + radius: Ui.scale(32) + color: control.down ? Palette.secondaryButtonPushedBackgroundColor : Palette.secondaryButtonBackgroundColor + border.width: Ui.scale(2) + border.color: Palette.secondaryButtonBorderColor + + width: control.width + height: control.height + } +} diff --git a/qml/Controls/SecondaryLabel.qml b/qml/Controls/SecondaryLabel.qml new file mode 100644 index 0000000..f9105ab --- /dev/null +++ b/qml/Controls/SecondaryLabel.qml @@ -0,0 +1,13 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.12 as QuickControls + +import Utils 1.0 + +QuickControls.Label { + id: control + font.pixelSize: Ui.scale(12) + font.family: "Roboto" + font.weight: Font.Normal + color: Palette.secondaryLabelColor + wrapMode : Text.Wrap +} diff --git a/qml/Controls/Separator.qml b/qml/Controls/Separator.qml new file mode 100644 index 0000000..33464b5 --- /dev/null +++ b/qml/Controls/Separator.qml @@ -0,0 +1,16 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.12 as QuickControls + +import Utils 1.0 + +QuickControls.ToolSeparator { + padding: vertical ? Ui.scale(10) : Ui.scale(2) + topPadding: vertical ? Ui.scale(2) : Ui.scale(10) + bottomPadding: vertical ? Ui.scale(2) : Ui.scale(10) + + contentItem: Rectangle { + implicitWidth: parent.vertical ? Ui.scale(1) : Ui.scale(24) + implicitHeight: parent.vertical ? Ui.scale(24) : Ui.scale(1) + color: Palette.separatorColor + } +} diff --git a/qml/Controls/TextField.qml b/qml/Controls/TextField.qml new file mode 100644 index 0000000..b747bf0 --- /dev/null +++ b/qml/Controls/TextField.qml @@ -0,0 +1,34 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.12 as QuickControls + +import Utils 1.0 + +QuickControls.TextField { + id: control + + implicitHeight: Ui.scale(50) + + horizontalAlignment: Text.AlignLeft + verticalAlignment: Text.AlignVCenter + + topPadding: Ui.scale(12) + bottomPadding: Ui.scale(12) + leftPadding: Ui.scale(24) + rightPadding: Ui.scale(24) + + font.family: "Roboto" + font.pixelSize: Ui.scale(21) + font.weight: Font.Bold + + color: Palette.primaryLabelColor + + background: Rectangle { + radius: Ui.scale(5) + color: Palette.textFieldBackgroundColor + border.width: Ui.scale(1) + border.color: control.acceptableInput ? Palette.borderColor : "red" + + width: control.width + height: control.height + } +} diff --git a/qml/Controls/qmldir b/qml/Controls/qmldir new file mode 100644 index 0000000..d23a96b --- /dev/null +++ b/qml/Controls/qmldir @@ -0,0 +1,10 @@ +module Controls +ColoredLabel 1.0 ColoredLabel.qml +Indicator 1.0 Indicator.qml +IndicatorLabel 1.0 IndicatorLabel.qml +PrimaryButton 1.0 PrimaryButton.qml +PrimaryLabel 1.0 PrimaryLabel.qml +SecondaryButton 1.0 SecondaryButton.qml +SecondaryLabel 1.0 SecondaryLabel.qml +Separator 1.0 Separator.qml +TextField 1.0 TextField.qml diff --git a/qml/Utils/Palette.qml b/qml/Utils/Palette.qml new file mode 100644 index 0000000..4d2252b --- /dev/null +++ b/qml/Utils/Palette.qml @@ -0,0 +1,27 @@ +pragma Singleton + +import QtQuick 2.12 + +QtObject { + property color backgroundColor: "#F7F5F5" + property color textFieldBackgroundColor: "#FFFFFF" + + property color borderColor: "#C7C7C7" + property color separatorColor: "#C7C7C7" + + property color primaryLabelColor: "#232323" + property color secondaryLabelColor: "#232323" + property color indicatorLabelColor: "#A6A6A6" + + property color primaryButtonBackgroundColor: "#003ACE" + property color primaryButtonPushedBackgroundColor: "#0031AD" + property color primaryButtonTextColor: "#FFFFFF" + + property color secondaryButtonBackgroundColor: "#F7F5F5" + property color secondaryButtonPushedBackgroundColor: "#E5ECFF" + property color secondaryButtonBorderColor: "#003ACE" + property color secondaryButtonTextColor: "#003ACE" + + property color timeSectionBackgroundColor: "#F8F8F8" + property color statusSectionBackgroundColor: "#FDD855" +} diff --git a/qml/Utils/Ui.qml b/qml/Utils/Ui.qml new file mode 100644 index 0000000..25e7845 --- /dev/null +++ b/qml/Utils/Ui.qml @@ -0,0 +1,18 @@ +pragma Singleton + +import QtQml 2.12 +import QtQuick 2.12 +import QtQuick.Window 2.12 + +Item { + property real currentWidth: 1024 + property real currentHeight: 768 + + property real referenceWidth: 1024 + property real referenceHeight: 768 + + function scale(size) { + const value = Math.max(Math.round(size * Math.min(currentWidth / referenceWidth, currentHeight / referenceHeight)), 1) + return value + } +} diff --git a/qml/Utils/qmldir b/qml/Utils/qmldir new file mode 100644 index 0000000..661bd81 --- /dev/null +++ b/qml/Utils/qmldir @@ -0,0 +1,3 @@ +module Utils +singleton Palette 1.0 Palette.qml +singleton Ui 1.0 Ui.qml diff --git a/qml/main.qml b/qml/main.qml new file mode 100644 index 0000000..f621c23 --- /dev/null +++ b/qml/main.qml @@ -0,0 +1,249 @@ +import QtQml 2.12 +import QtQuick 2.12 +import QtQuick.Layouts 1.12 +import QtQuick.Controls 2.12 as QuickControls + +import Controls 1.0 +import Utils 1.0 +import Vku 1.0 + +QuickControls.ApplicationWindow { + id: mainWindow + width: 1024 + height: 768 + visible: true + title: qsTr("Vku Monitor") + + ColumnLayout { + anchors.fill: parent + anchors.topMargin: Ui.scale(32) + anchors.bottomMargin: Ui.scale(32) + anchors.leftMargin: Ui.scale(50) + anchors.rightMargin: Ui.scale(50) + + spacing: Ui.scale(12) + + RowLayout { + id: topLayout + spacing: Ui.scale(40) + Layout.fillWidth: true + + ColumnLayout { + spacing: Ui.scale(12) + + Layout.fillWidth: true + Layout.fillHeight: true + Layout.preferredWidth: 100 + + PrimaryLabel { + text: qsTr("Current state") + } + + GridLayout { + columns: 2 + columnSpacing: Ui.scale(28) + rowSpacing: Ui.scale(8) + + Layout.fillWidth: true + Layout.fillHeight: true + + Indicator { + label: qsTr("Input voltage") + indicator: qsTr("V") + value.text: CanController.inputVoltage + value.readOnly: true + Layout.fillWidth: true + Layout.preferredWidth: 100 + } + + Indicator { + label: qsTr("Radiator temperature") + indicator: qsTr("°С") + value.text: CanController.radiatorTemperature + value.readOnly: true + Layout.fillWidth: true + Layout.preferredWidth: 100 + } + + Indicator { + label: qsTr("Output voltage") + indicator: qsTr("V") + value.text: CanController.outputVoltage + value.readOnly: true + Layout.fillWidth: true + Layout.preferredWidth: 100 + } + + Indicator { + label: qsTr("Emergency counter") + indicator: "" + value.text: CanController.emergencyCounter + value.readOnly: true + Layout.fillWidth: true + Layout.preferredWidth: 100 + } + + Indicator { + label: qsTr("Output current") + indicator: qsTr("A") + value.text: CanController.outputCurrent + value.readOnly: true + Layout.fillWidth: true + Layout.preferredWidth: 100 + } + } + + PrimaryButton { + text: CanController.isVkuClosed ? qsTr("VKU opening") : qsTr("VKU closing") + Layout.fillWidth: true + Layout.topMargin: Ui.scale(12) + onClicked: CanController.vkuClosure() + } + } + + Separator { + id: separator + orientation: Qt.Vertical + Layout.fillHeight: true + } + + ColumnLayout { + spacing: Ui.scale(12) + + Layout.fillWidth: true + Layout.fillHeight: true + Layout.preferredWidth: 100 + + PrimaryLabel { + text: qsTr("Parameters control") + Layout.fillWidth: true + } + + GridLayout { + columns: 2 + columnSpacing: Ui.scale(28) + rowSpacing: Ui.scale(8) + + Layout.fillWidth: true + Layout.fillHeight: true + + Indicator { + label: qsTr("Maximum current") + indicator: qsTr("A") + value.text: CanController.maximumCurrent + value.onEditingFinished: CanController.maximumCurrent = value.text + value.validator: IntValidator { bottom: 0; top: 255 } + Layout.fillWidth: true + Layout.preferredWidth: 100 + } + + Indicator { + label: qsTr("Duration of delay after emergency") + indicator: qsTr("s") + value.text: CanController.emergencyDelay + value.onEditingFinished: CanController.emergencyDelay = value.text + value.validator: DoubleValidator { bottom: 2; top: 15; decimals: 1; notation: DoubleValidator.StandardNotation } + Layout.fillWidth: true + Layout.preferredWidth: 100 + } + + Indicator { + label: qsTr("Breaking delay") + indicator: qsTr("ms") + value.text: CanController.breakingDelay + value.onEditingFinished: CanController.breakingDelay = value.text + value.validator: IntValidator { bottom: 0; top: 255 } + Layout.fillWidth: true + Layout.preferredWidth: 100 + } + + Indicator { + label: qsTr("Number of retries after emergency breaking") + indicator: "" + value.text: CanController.retriesAfterEmergencyBreak + value.onEditingFinished: CanController.retriesAfterEmergencyBreak = value.text + value.validator: IntValidator { bottom: 1; top: 7 } + Layout.fillWidth: true + Layout.preferredWidth: 100 + } + + Indicator { + label: qsTr("Breaking current") + indicator: qsTr("A") + value.text: CanController.breakingCurrent + value.onEditingFinished: CanController.breakingCurrent = value.text + value.validator: IntValidator { bottom: 0; top: 255 } + Layout.fillWidth: true + Layout.preferredWidth: 100 + } + } + + SecondaryButton { + text: qsTr("Emergency reset") + Layout.fillWidth: true + Layout.topMargin: Ui.scale(12) + onClicked: CanController.emergencyReset() + } + } + } + + PrimaryLabel { + text: qsTr("Status") + visible: CanController.statuses.length > 0 + Layout.topMargin: Ui.scale(20) + } + + ListView { + clip: true + model: CanController.statuses + + // ???? + implicitHeight: mainWindow.height + + Layout.fillWidth: true + Layout.fillHeight: true + + delegate: GridLayout { + width: parent.width + columns: 4 + + columnSpacing: Ui.scale(20) + rowSpacing: Ui.scale(8) + + Separator { + bottomPadding: Ui.scale(8) + orientation: Qt.Horizontal + Layout.fillWidth: true + Layout.columnSpan: 4 + } + + ColoredLabel { + text: modelData.time + backgroundColor: Palette.timeSectionBackgroundColor + } + + PrimaryLabel { + text: modelData.status + } + + ColoredLabel { + text: modelData.description + backgroundColor: Palette.statusSectionBackgroundColor + } + + Item { + Layout.fillWidth: true + } + } + } + } + + background: Rectangle { + color: Palette.backgroundColor + } + + Component.onCompleted: { + Ui.currentWidth = Qt.binding(function(){ return width }) + Ui.currentHeight = Qt.binding(function(){ return height }) + } +} diff --git a/qml/qml.qrc b/qml/qml.qrc new file mode 100644 index 0000000..9ff3b97 --- /dev/null +++ b/qml/qml.qrc @@ -0,0 +1,18 @@ + + + main.qml + Controls/PrimaryLabel.qml + Controls/SecondaryLabel.qml + Controls/PrimaryButton.qml + Controls/SecondaryButton.qml + Controls/TextField.qml + Controls/IndicatorLabel.qml + Controls/Indicator.qml + Controls/Separator.qml + Utils/Palette.qml + Utils/Ui.qml + Controls/qmldir + Utils/qmldir + Controls/ColoredLabel.qml + + diff --git a/settings.ini b/settings.ini new file mode 100644 index 0000000..7d76363 --- /dev/null +++ b/settings.ini @@ -0,0 +1,3 @@ +[General] +bitrate=100000 +sendParametersTimeout=50 diff --git a/translations/VkuMonitor_ru_RU.qm b/translations/VkuMonitor_ru_RU.qm new file mode 100644 index 0000000..3056812 Binary files /dev/null and b/translations/VkuMonitor_ru_RU.qm differ diff --git a/translations/VkuMonitor_ru_RU.ts b/translations/VkuMonitor_ru_RU.ts new file mode 100644 index 0000000..3f8c1b3 --- /dev/null +++ b/translations/VkuMonitor_ru_RU.ts @@ -0,0 +1,342 @@ + + + + + CanController + + + Reset + Сброс + + + + No incoming messages via CAN interface + Нет входящих сообщений по CAN + + + + Waiting for a command for closing + Ожидание команды на замыкание + + + + Work via CAN + Работа по CAN + + + + Work via button signal + Работа по сигналу кнопки + + + + Work via CAN or button signal + Работа по CAN или сигналу кнопки + + + + Exceeding input voltage + Превышение входного напряжения + + + + Exceeding maximum current + Превышение максимального тока + + + + Exceeding radiator temperature + Превышение температуры радиатора + + + + Pause after breaking + Пауза после размыкания + + + + Blocking due to emergency + Блокировка по аварии + + + + Unknown status + Неизвестный статус + + + + GenericCanSetupPage + + + Form + + + + + Driver: + + + + + + + TextLabel + + + + + Interface: + + + + + Interface Details: + + + + + Bitrate: + + + + + Sample Point: + + + + + CanFD Bitrate: + + + + + CanFD SamplePoint: + + + + + Options: + + + + + configured by operating system + + + + + Listen only mode + + + + + One-Shot mode + + + + + Triple Sampling + + + + + Auto-Restart on bus off condition + + + + + SelectCanInterfacesDialog + + + Select CAN Interface(s) + + + + + &Create Interface... + + + + + SetupDialog + + + Measurement Setup + + + + + Add Network + + + + + Remove Network + + + + + Refresh + + + + + Network name: + + + + + CAN interfaces assigned to this network: + + + + + Add &Interface... + + + + + &Remove Interface + + + + + Add &Database... + + + + + &Remove Database + + + + + Reload Databases + + + + + main + + + Vku Monitor + Монитор ВКУ + + + + Current state + Текущее состояние + + + + Input voltage + Входное напряжение + + + + + V + В + + + + Radiator temperature + Температура радиатора + + + + °С + + + + + Output voltage + Выходное напряжение + + + Number of retries after an emergency + Количество повторов после аварии + + + + Emergency counter + Счетчик аварий + + + + Output current + Выходной ток + + + + + + A + А + + + VKU closure + Замыкание ВКУ + + + + VKU opening + Замыкание ВКУ + + + + VKU closing + Размыкание ВКУ + + + + Parameters control + Управление параметрами + + + + Maximum current + Максимальный ток + + + + Duration of delay after emergency + Длительность задержки после аварии + + + + s + с + + + + Breaking delay + Задержка на размыкание + + + + ms + мс + + + + Number of retries after emergency breaking + Количество повторов после аварийного размыкания + + + + Breaking current + Допустимый ток размыкания + + + + Emergency reset + Сброс аварии + + + + Status + Статус + + +