First implementation

This commit is contained in:
Yury Shuvakin
2025-07-02 18:00:35 +09:00
commit 6cf2010c08
107 changed files with 10803 additions and 0 deletions

12
CanDriver/CanDriver.pri Normal file
View File

@@ -0,0 +1,12 @@
QT += widgets xml serialport
INCLUDEPATH += $$PWD
include($$PWD/core/core.pri)
include($$PWD/driver/driver.pri)
include($$PWD/parser/dbc/dbc.pri)
include($$PWD/driver/CANBlastDriver/CANBlastDriver.pri)
include($$PWD/driver/SLCANDriver/SLCANDriver.pri)
win32:include($$PWD/driver/CandleApiDriver/CandleApiDriver.pri)
unix:include($$PWD/driver/SocketCanDriver/SocketCanDriver.pri)

302
CanDriver/core/Backend.cpp Normal file
View File

@@ -0,0 +1,302 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#include "Backend.h"
#include "LogModel.h"
#include <QDateTime>
#include <QDebug>
#include <QFile>
#include <core/CanTrace.h>
#include <core/MeasurementSetup.h>
#include <core/MeasurementNetwork.h>
#include <core/MeasurementInterface.h>
#include <driver/CanDriver.h>
#include <driver/CanInterface.h>
#include <driver/CanListener.h>
#include <parser/dbc/DbcParser.h>
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;
qDebug() << msg;
emit onLogMessage(dt, level, msg);
}

111
CanDriver/core/Backend.h Normal file
View File

@@ -0,0 +1,111 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include <QObject>
#include <QList>
#include <QMutex>
#include <QDateTime>
#include <QElapsedTimer>
#include <driver/CanDriver.h>
#include <core/CanDb.h>
#include <core/MeasurementSetup.h>
#include <core/Log.h>
class MeasurementNetwork;
class CanTrace;
class CanListener;
class CanDbMessage;
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);
public slots:
private:
static Backend *_instance;
bool _measurementRunning;
uint64_t _measurementStartTime;
QElapsedTimer _timerSinceStart;
QList<CanDriver*> _drivers;
MeasurementSetup _setup;
CanTrace *_trace;
QList<CanListener*> _listeners;
LogModel *_logModel;
};

89
CanDriver/core/CanDb.cpp Normal file
View File

@@ -0,0 +1,89 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#include "CanDb.h"
#include <QFileInfo>
#include <QDomDocument>
#include <core/Backend.h>
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;
}

73
CanDriver/core/CanDb.h Normal file
View File

@@ -0,0 +1,73 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QString>
#include <QList>
#include <QMap>
#include <QSharedPointer>
#include "CanDbNode.h"
#include "CanDbMessage.h"
class QDomDocument;
class QDomElement;
class Backend;
class CanDb;
class CanDbMessage;
typedef QMap<QString,CanDbNode*> CanDbNodeMap;
typedef QMap<uint32_t, CanDbMessage*> CanDbMessageList;
typedef QSharedPointer<CanDb> 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;
};

View File

@@ -0,0 +1,112 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#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;
}

View File

@@ -0,0 +1,73 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include <QString>
#include "CanDb.h"
#include "CanDbSignal.h"
class CanDbNode;
class CanDbSignal;
typedef QList<CanDbSignal*> 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;
};

View File

@@ -0,0 +1,49 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#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;
}

View File

@@ -0,0 +1,43 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QString>
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;
};

View File

@@ -0,0 +1,229 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#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());
}

View File

@@ -0,0 +1,106 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "CanMessage.h"
#include "CanDbMessage.h"
#include <QString>
#include <QMap>
class CanDbMessage;
typedef QMap<uint64_t,QString> 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;
};

View File

@@ -0,0 +1,350 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#include "CanMessage.h"
#include <core/portable_endian.h>
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(8), _isFD(false), _interface(0), _u8()
{
_timestamp.tv_sec = 0;
_timestamp.tv_usec = 0;
}
CanMessage::CanMessage(uint32_t can_id)
: _dlc(8), _isFD(false), _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<sizeof(_u8)) {
return _u8[index];
} else {
return 0;
}
}
void CanMessage::setByte(const uint8_t index, const uint8_t value) {
if (index<sizeof(_u8)) {
_u8[index] = value;
}
}
uint64_t CanMessage::extractRawSignal(uint8_t start_bit, const uint8_t length, const bool isBigEndian) const
{
// if ((start_bit+length) > (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;
}
const QByteArray CanMessage::getData() const
{
return QByteArray(reinterpret_cast<const char *>(_u8), _dlc);
}
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<getLength(); i++)
{
outstr += QString().sprintf("%02X ", getByte(i));
}
return outstr;
}

106
CanDriver/core/CanMessage.h Normal file
View File

@@ -0,0 +1,106 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include <sys/time.h>
#include <QString>
#include <QDateTime>
#include <driver/CanDriver.h>
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);
const QByteArray getData() const;
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 = 0;
uint8_t _dlc = 8;
bool _isFD = false;
bool _isBRS = false;
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;
};

215
CanDriver/core/CanTrace.cpp Normal file
View File

@@ -0,0 +1,215 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#include "CanTrace.h"
#include <QMutexLocker>
#include <QFile>
#include <QTextStream>
#include <core/Backend.h>
#include <core/CanMessage.h>
#include <core/CanDbMessage.h>
#include <core/CanDbSignal.h>
#include <driver/CanInterface.h>
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; i<size(); i++) {
CanMessage *msg = &_data[i];
QString line;
line.append(QString().sprintf("(%.6f) ", msg->getFloatTimestamp()));
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; i<msg->getLength(); 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<size(); i++) {
CanMessage &msg = _data[i];
double t_current = msg.getFloatTimestamp();
QString id_hex_str = QString().sprintf("%x", msg.getId());
QString id_dec_str = QString().sprintf("%d", msg.getId());
if (msg.isExtended()) {
id_hex_str.append("x");
id_dec_str.append("x");
}
// TODO how to handle RTR flag?
QString line = QString().sprintf(
"%11.6lf 1 %-15s %s d %d %s Length = %d BitCount = %d ID = %s",
t_current-t_start,
id_hex_str.toStdString().c_str(),
"Rx", // TODO handle Rx/Tx
msg.getLength(),
msg.getDataHexString().toStdString().c_str(),
0, // TODO Length (transfer time in ns)
0, // TODO BitCount (overall frame length, including stuff bits)
id_dec_str.toStdString().c_str()
);
stream << line << endl;
}
stream << "End TriggerBlock" << endl;
}
bool CanTrace::getMuxedSignalFromCache(const CanDbSignal *signal, uint64_t *raw_value)
{
if (_muxCache.contains(signal)) {
*raw_value = _muxCache[signal];
return true;
} else {
return false;
}
}

87
CanDriver/core/CanTrace.h Normal file
View File

@@ -0,0 +1,87 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QObject>
#include <QMutex>
#include <QTimer>
#include <QVector>
#include <QMap>
#include <QFile>
#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<CanMessage> _data;
int _dataRowsUsed;
int _newRows;
bool _isTimerRunning;
QMap<const CanDbSignal*,uint64_t> _muxCache;
QMutex _mutex;
QMutex _timerMutex;
QTimer _flushTimer;
void startTimer();
};

63
CanDriver/core/Log.cpp Normal file
View File

@@ -0,0 +1,63 @@
/*
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#include "Log.h"
#include <core/Backend.h>
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);
}

43
CanDriver/core/Log.h Normal file
View File

@@ -0,0 +1,43 @@
/*
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QDateTime>
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);

160
CanDriver/core/LogModel.cpp Normal file
View File

@@ -0,0 +1,160 @@
/*
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#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 "";
}
}

71
CanDriver/core/LogModel.h Normal file
View File

@@ -0,0 +1,71 @@
/*
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QAbstractItemModel>
#include <QDateTime>
#include <QList>
#include <core/Backend.h>
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<LogItem*> _items;
static QString logLevelText(log_level_t level);
};

View File

@@ -0,0 +1,215 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#include "MeasurementInterface.h"
#include <core/Backend.h>
#include <driver/CanDriver.h>
#include <driver/CanInterface.h>
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;
}

View File

@@ -0,0 +1,92 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QDomDocument>
#include <driver/CanDriver.h>
#include <driver/CanInterface.h>
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;
};

View File

@@ -0,0 +1,159 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#include "MeasurementNetwork.h"
#include "MeasurementInterface.h"
#include <core/Backend.h>
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<MeasurementInterface *> 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> 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; i<ifList.length(); i++) {
QDomElement elIntf = ifList.item(i).toElement();
QString driverName = elIntf.attribute("driver");
QString deviceName = elIntf.attribute("name");
CanInterface *intf = backend.getInterfaceByDriverAndName(driverName, deviceName);
if (intf) {
MeasurementInterface *mi = addCanInterface(intf->getId());
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<dbList.length(); i++) {
QDomElement elDb = dbList.item(i).toElement();
QString filename = elDb.attribute("filename", QString());
if (!filename.isEmpty()) {
addCanDb(backend.loadDbc(filename));
} else {
log_error(QString("Unable to load CanDB: %1").arg(filename));
}
}
return true;
}

View File

@@ -0,0 +1,61 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QString>
#include <QList>
#include <QDomDocument>
#include <driver/CanDriver.h>
#include <driver/CanInterface.h>
#include <core/CanDb.h>
class Backend;
class MeasurementInterface;
class MeasurementNetwork
{
public:
MeasurementNetwork();
void cloneFrom(MeasurementNetwork &origin);
void addInterface(MeasurementInterface *intf);
void removeInterface(MeasurementInterface *intf);
QList<MeasurementInterface*> interfaces();
MeasurementInterface *addCanInterface(CanInterfaceId canif);
CanInterfaceIdList getReferencedCanInterfaces();
void addCanDb(pCanDb candb);
void reloadCanDbs(Backend *backend);
QList<pCanDb> _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<MeasurementInterface*> _interfaces;
};

View File

@@ -0,0 +1,145 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#include "MeasurementSetup.h"
#include <QThread>
#include <QMetaType>
#include <core/CanTrace.h>
#include <core/CanMessage.h>
#include <core/MeasurementNetwork.h>
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; i<networks.length(); i++) {
MeasurementNetwork *network = createNetwork();
if (!network->loadXML(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<MeasurementNetwork *> MeasurementSetup::getNetworks()
{
return _networks;
}

View File

@@ -0,0 +1,63 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QObject>
#include <QList>
#include <QDomDocument>
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<MeasurementNetwork*> 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<MeasurementNetwork*> _networks;
};

28
CanDriver/core/core.pri Normal file
View File

@@ -0,0 +1,28 @@
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/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/Log.h

View File

@@ -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 <endian.h>
#elif defined(__WINDOWS__)
# include <sys/param.h>
# 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

View File

@@ -0,0 +1,13 @@
CONFIG += c++11
QT += network
SOURCES += \
$$PWD/CANBlasterDriver.cpp \
$$PWD/CANBlasterInterface.cpp
HEADERS += \
$$PWD/CANBlasterDriver.h \
$$PWD/CANBlasterInterface.h
FORMS +=

View File

@@ -0,0 +1,137 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#include "CANBlasterDriver.h"
#include "CANBlasterInterface.h"
#include <core/Backend.h>
#include <errno.h>
#include <cstring>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <QCoreApplication>
#include <QDebug>
#include <QtNetwork/QUdpSocket>
#include <QNetworkDatagram>
#include <QJsonDocument>
#include <QJsonObject>
#include <QList>
CANBlasterDriver::CANBlasterDriver(Backend &backend)
: CanDriver(backend)
{
}
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<QString> 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<CANBlasterInterface*>(intf);
if (scif->getIfIndex() == index) {
scif->setName(name);
return scif;
}
}
CANBlasterInterface *scif = new CANBlasterInterface(this, index, name, fd_support);
addInterface(scif);
return scif;
}

View File

@@ -0,0 +1,41 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QString>
#include <core/Backend.h>
#include <driver/CanDriver.h>
class CANBlasterInterface;
class SetupDialogInterfacePage;
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);
};

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "CANBlasterInterface.h"
#include <core/Backend.h>
#include <core/MeasurementInterface.h>
#include <core/CanMessage.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <QString>
#include <QStringList>
#include <QProcess>
#include <QThread>
#include <QTimer>
#include <QNetworkDatagram>
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<CanTiming> CANBlasterInterface::getAvailableBitrates()
{
QList<CanTiming> retval;
QList<unsigned> bitrates({10000, 20000, 50000, 83333, 100000, 125000, 250000, 500000, 800000, 1000000});
QList<unsigned> bitrates_fd({0, 2000000, 5000000});
QList<unsigned> 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<CanMessage> &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<frame.len && i<CAN_MAX_DLEN; i++)
{
msg.setDataAt(i, frame.data[i]);
}
msglist.append(msg);
return true;
}
else
{
return false;
}
}
return false;
}

View File

@@ -0,0 +1,237 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "../CanInterface.h"
#include <core/MeasurementInterface.h>
#include <QtNetwork/QUdpSocket>
#include <QTimer>
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<CanTiming> 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<CanMessage> &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))

View File

@@ -0,0 +1,107 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#include "CanDriver.h"
#include <core/Backend.h>
#include <driver/CanInterface.h>
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<CanInterface *> 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;
}

View File

@@ -0,0 +1,63 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include <QList>
#include <QString>
class Backend;
class CanInterface;
typedef uint16_t CanInterfaceId;
typedef QList<uint16_t> 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<CanInterface*> 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<CanInterface*> _interfaces;
void setId(int id);
};

View File

@@ -0,0 +1,101 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#include "CanInterface.h"
#include <QList>
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<CanTiming> CanInterface::getAvailableBitrates()
{
QList<CanTiming> 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;
}

View File

@@ -0,0 +1,92 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QString>
#include <stdint.h>
#include "CanDriver.h"
#include "CanTiming.h"
#include <QObject>
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<CanTiming> getAvailableBitrates();
virtual void open();
virtual void close();
virtual bool isOpen();
virtual void sendMessage(const CanMessage &msg) = 0;
virtual bool readMessage(QList<CanMessage> &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;
};

View File

@@ -0,0 +1,97 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#include "CanListener.h"
#include <QThread>
#include <core/Backend.h>
#include <core/CanTrace.h>
#include <core/CanMessage.h>
#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<CanMessage> 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();
}

View File

@@ -0,0 +1,60 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QObject>
#include <driver/CanDriver.h>
#include <driver/CanInterface.h>
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;
};

View File

@@ -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)
);
}

View File

@@ -0,0 +1,37 @@
#ifndef CANTIMING_H
#define CANTIMING_H
#include <QString>
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

View File

@@ -0,0 +1,77 @@
/*
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#include "CandleApiDriver.h"
#include "api/candle.h"
#include "CandleApiInterface.h"
CandleApiDriver::CandleApiDriver(Backend &backend)
: CanDriver(backend)
{
}
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; i<num_interfaces; i++) {
if (candle_dev_get(clist, i, &dev)) {
CandleApiInterface *cif = findInterface(dev);
if (cif == NULL) {
cif = new CandleApiInterface(this, dev);
addInterface(cif);
} else {
cif->update(dev);
}
}
}
}
candle_list_free(clist);
}
return true;
}
CandleApiInterface *CandleApiDriver::findInterface(candle_handle dev)
{
foreach (CanInterface *intf, getInterfaces()) {
CandleApiInterface *cif = dynamic_cast<CandleApiInterface*>(intf);
if (cif->getPath() == std::wstring(candle_dev_get_path(dev))) {
return cif;
}
}
return NULL;
}

View File

@@ -0,0 +1,41 @@
/*
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <core/Backend.h>
#include <driver/CanDriver.h>
#include "api/candle.h"
class CandleApiInterface;
class CandleApiDriver : public CanDriver
{
public:
CandleApiDriver(Backend &backend);
virtual QString getName();
virtual bool update();
private:
CandleApiInterface *findInterface(candle_handle dev);
};

View File

@@ -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 +=

View File

@@ -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<CanTiming> CandleApiInterface::getAvailableBitrates()
{
QList<CanTiming> 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<CanMessage> &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<dlc; i++) {
msg.setByte(i, data[i]);
}
uint32_t dev_ts = candle_frame_timestamp_us(&frame) - _deviceTicksStart;
uint64_t ts_us = _hostOffsetStart + dev_ts;
uint64_t us_since_start = _backend.getUsecsSinceMeasurementStart();
if (us_since_start > 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;
}

View File

@@ -0,0 +1,70 @@
#ifndef CANDLEAPIINTERFACE_H
#define CANDLEAPIINTERFACE_H
#include <driver/CanInterface.h>
#include <core/MeasurementInterface.h>
#include "api/candle.h"
#include "CandleApiTiming.h"
#include <windows.h>
#include <QList>
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<CanTiming> getAvailableBitrates();
virtual void open();
virtual bool isOpen();
virtual void close();
virtual void sendMessage(const CanMessage &msg);
virtual bool readMessage(QList<CanMessage> &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<CandleApiTiming> _timings;
bool setBitTiming(uint32_t bitrate, uint32_t samplePoint);
};
#endif // CANDLEAPIINTERFACE_H

View File

@@ -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;
}

View File

@@ -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

View File

@@ -0,0 +1,66 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/>
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.

View File

@@ -0,0 +1,589 @@
/*
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#include "candle.h"
#include <stdlib.h>
#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; i<CANDLE_MAX_DEVICES; i++) {
SP_DEVICE_INTERFACE_DATA interfaceData;
interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
if (SetupDiEnumDeviceInterfaces(hdi, NULL, &guid, i, &interfaceData)) {
if (!candle_read_di(hdi, interfaceData, &l->dev[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; i<ifaceDescriptor.bNumEndpoints; i++) {
WINUSB_PIPE_INFORMATION pipeInfo;
if (!WinUsb_QueryPipe(dev->winUSBHandle, 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; i<CANDLE_URB_COUNT; i++) {
if (dev->rxevents[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; i<CANDLE_URB_COUNT; i++) {
HANDLE ev = CreateEvent(NULL, true, false, NULL);
dev->rxevents[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;
}

View File

@@ -0,0 +1,166 @@
/*
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#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

View File

@@ -0,0 +1,152 @@
/*
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#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;
}

View File

@@ -0,0 +1,37 @@
/*
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#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);

View File

@@ -0,0 +1,90 @@
/*
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include <windows.h>
#include <winbase.h>
#include <winusb.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
#undef __CRT__NO_INLINE
#include <strsafe.h>
#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;

View File

@@ -0,0 +1,37 @@
/*
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#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

View File

@@ -0,0 +1,104 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#include "SLCANDriver.h"
#include "SLCANInterface.h"
#include <core/Backend.h>
#include <errno.h>
#include <cstring>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
//
#include <QCoreApplication>
#include <QDebug>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
SLCANDriver::SLCANDriver(Backend &backend)
: CanDriver(backend)
{
}
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<SLCANInterface*>(intf);
if (scif->getIfIndex() == index) {
scif->setName(name);
return scif;
}
}
SLCANInterface *scif = new SLCANInterface(this, index, name, fd_support);
addInterface(scif);
return scif;
}

View File

@@ -0,0 +1,41 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QString>
#include <core/Backend.h>
#include <driver/CanDriver.h>
class SLCANInterface;
class SetupDialogInterfacePage;
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);
};

View File

@@ -0,0 +1,12 @@
CONFIG += c++11
SOURCES += \
$$PWD/SLCANInterface.cpp \
$$PWD/SLCANDriver.cpp
HEADERS += \
$$PWD/SLCANInterface.h \
$$PWD/SLCANDriver.h
FORMS +=

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "SLCANInterface.h"
#include <core/Backend.h>
#include <core/MeasurementInterface.h>
#include <core/CanMessage.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <QString>
#include <QStringList>
#include <QProcess>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QThread>
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<CanTiming> SLCANInterface::getAvailableBitrates()
{
QList<CanTiming> retval;
QList<unsigned> bitrates({10000, 20000, 50000, 83333, 100000, 125000, 250000, 500000, 800000, 1000000});
QList<unsigned> bitrates_fd({0, 2000000, 5000000});
QList<unsigned> 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<CanMessage> &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<datas.count(); i++)
{
// If incrementing the head will hit the tail, we've filled the buffer. Reset and discard all data.
if(((_rxbuf_head + 1) % RXCIRBUF_LEN) == _rxbuf_tail)
{
_rxbuf_head = 0;
_rxbuf_tail = 0;
}
else
{
// Put inbound data at the head locatoin
_rxbuf[_rxbuf_head] = datas.at(i);
_rxbuf_head = (_rxbuf_head + 1) % RXCIRBUF_LEN; // Wrap at MTU
}
}
_rxbuf_mutex.unlock();
}
//////////////////////////
bool ret = false;
_rxbuf_mutex.lock();
while(_rxbuf_tail != _rxbuf_head)
{
// Save data if room
if(_rx_linbuf_ctr < SLCAN_MTU)
{
_rx_linbuf[_rx_linbuf_ctr++] = _rxbuf[_rxbuf_tail];
// If we have a newline, then we just finished parsing a CAN message.
if(_rxbuf[_rxbuf_tail] == '\r')
{
CanMessage msg;
ret = parseMessage(msg);
msglist.append(msg);
_rx_linbuf_ctr = 0;
}
}
// Discard data if not
else
{
perror("Linbuf full");
_rx_linbuf_ctr = 0;
}
_rxbuf_tail = (_rxbuf_tail + 1) % RXCIRBUF_LEN;
}
_rxbuf_mutex.unlock();
return ret;
}
bool SLCANInterface::parseMessage(CanMessage &msg)
{
// Set timestamp to current time
struct timeval tv;
gettimeofday(&tv,NULL);
msg.setTimestamp(tv);
// Defaults
msg.setErrorFrame(0);
msg.setInterfaceId(getId());
msg.setId(0);
bool msg_is_fd = false;
// Convert from ASCII (2nd character to end)
for (int i = 1; i < _rx_linbuf_ctr; i++)
{
// Lowercase letters
if(_rx_linbuf[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);
}*/
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "../CanInterface.h"
#include <core/MeasurementInterface.h>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QMutex>
// 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<CanTiming> 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<CanMessage> &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);
};

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "SocketCanDriver.h"
#include "SocketCanInterface.h"
#include <core/Backend.h>
#include <driver/GenericCanSetupPage.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/can/netlink.h>
#include <netlink/route/link.h>
#include <netlink/route/link/can.h>
#include <errno.h>
#include <cstring>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
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<SocketCanInterface*>(intf);
if (scif->getIfIndex() == index) {
scif->setName(name);
return scif;
}
}
SocketCanInterface *scif = new SocketCanInterface(this, index, name);
addInterface(scif);
return scif;
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QString>
#include <core/Backend.h>
#include <driver/CanDriver.h>
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;
};

View File

@@ -0,0 +1,12 @@
CONFIG += c++11
SOURCES += \
$$PWD/SocketCanInterface.cpp \
$$PWD/SocketCanDriver.cpp
HEADERS += \
$$PWD/SocketCanInterface.h \
$$PWD/SocketCanDriver.h
FORMS +=

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "SocketCanInterface.h"
#include <core/Backend.h>
#include <core/MeasurementInterface.h>
#include <core/CanMessage.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <QString>
#include <QStringList>
#include <QProcess>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <linux/if.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <linux/can/netlink.h>
#include <linux/sockios.h>
#include <netlink/version.h>
#include <netlink/route/link.h>
#include <netlink/route/link/can.h>
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<CanTiming> SocketCanInterface::getAvailableBitrates()
{
QList<CanTiming> retval;
QList<unsigned> bitrates({10000, 20000, 50000, 83333, 100000, 125000, 250000, 500000, 800000, 1000000});
QList<unsigned> 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<len; i++) {
frame.data[i] = msg.getByte(i);
}
::write(_fd, &frame, sizeof(struct can_frame));
}
bool SocketCanInterface::readMessage(QList<CanMessage> &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<len; i++) {
msg.setByte(i, frame.data[i]);
}
msglist.append(msg);
return true;
} else {
return false;
}
}

View File

@@ -0,0 +1,115 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "../CanInterface.h"
#include <linux/can/netlink.h>
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<CanTiming> 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<CanMessage> &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);
};

View File

@@ -0,0 +1,11 @@
SOURCES += \
$$PWD/CanInterface.cpp \
$$PWD/CanListener.cpp \
$$PWD/CanDriver.cpp \
$$PWD/CanTiming.cpp
HEADERS += \
$$PWD/CanInterface.h \
$$PWD/CanListener.h \
$$PWD/CanDriver.h \
$$PWD/CanTiming.h

View File

@@ -0,0 +1,688 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#include "DbcParser.h"
#include <QTextStream>
#include <stdint.h>
#include <iostream>
#include <core/Backend.h>
#include <core/CanDb.h>
#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, &quotedStr, 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, &sectionName, 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;
}

View File

@@ -0,0 +1,92 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QFile>
#include <QRegExp>
#include <QList>
#include <qstringlist.h>
#include <core/CanDb.h>
#include "DbcTokens.h"
class CanDbMessage;
class DbcParser
{
public:
typedef QList<DbcToken *> 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);
};

View File

@@ -0,0 +1,137 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#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();
}

View File

@@ -0,0 +1,102 @@
/*
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QFile>
#include <QChar>
#include <QRegExp>
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;
};

View File

@@ -0,0 +1,7 @@
HEADERS += \
$$PWD/DbcParser.h \
$$PWD/DbcTokens.h
SOURCES += \
$$PWD/DbcParser.cpp \
$$PWD/DbcTokens.cpp