Merge branch 'master' of github.com:collin80/SavvyCAN into WIP
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include <QString>
|
||||
#include "canconfactory.h"
|
||||
#include "socketcan.h"
|
||||
#include "serialbusconnection.h"
|
||||
#include "gvretserial.h"
|
||||
|
||||
using namespace CANCon;
|
||||
@@ -9,7 +9,7 @@ CANConnection* CanConFactory::create(type pType, QString pPortName)
|
||||
{
|
||||
switch(pType) {
|
||||
case SOCKETCAN:
|
||||
return new SocketCan(pPortName);
|
||||
return new SerialBusConnection(pPortName);
|
||||
case GVRET_SERIAL:
|
||||
return new GVRetSerial(pPortName);
|
||||
default: {}
|
||||
|
||||
@@ -11,6 +11,16 @@ CANConnectionModel::~CANConnectionModel()
|
||||
{
|
||||
}
|
||||
|
||||
enum class Column {
|
||||
Bus = 0, ///< A sequential number describing the bus
|
||||
Type = 1, ///< The CAN driver/backend type, e.g. GVRET, peakcan, or socketcan
|
||||
Port = 2, ///< The CAN hardware port, e.g. can0 for socketcan
|
||||
Speed = 3, ///< The bus speed in bit/second
|
||||
ListenOnly = 4, ///< True if the bus is in listen-only mode
|
||||
SingleWire = 5, ///< True if the bus operates in single-wire mode
|
||||
Active = 6, ///< True if the bus is activated for sending and receiving
|
||||
Status = 7 ///< The bus status as text message
|
||||
};
|
||||
|
||||
QVariant CANConnectionModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
@@ -19,32 +29,24 @@ QVariant CANConnectionModel::headerData(int section, Qt::Orientation orientation
|
||||
|
||||
if (orientation == Qt::Horizontal)
|
||||
{
|
||||
switch (section)
|
||||
switch (Column(section))
|
||||
{
|
||||
case 0:
|
||||
case Column::Bus:
|
||||
return QString(tr("Bus"));
|
||||
break;
|
||||
case 1:
|
||||
case Column::Type:
|
||||
return QString(tr("Type"));
|
||||
break;
|
||||
case 2:
|
||||
case Column::Port:
|
||||
return QString(tr("Port"));
|
||||
break;
|
||||
case 3:
|
||||
case Column::Speed:
|
||||
return QString(tr("Speed"));
|
||||
break;
|
||||
case 4:
|
||||
case Column::ListenOnly:
|
||||
return QString(tr("Listen Only"));
|
||||
break;
|
||||
case 5:
|
||||
case Column::SingleWire:
|
||||
return QString(tr("Single Wire"));
|
||||
break;
|
||||
case 6:
|
||||
case Column::Active:
|
||||
return QString(tr("Active"));
|
||||
break;
|
||||
case 7:
|
||||
case Column::Status:
|
||||
return QString(tr("Status"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,23 +92,19 @@ Qt::ItemFlags CANConnectionModel::flags(const QModelIndex &index) const
|
||||
bool editParams = false;
|
||||
if (conn_p->getType() == CANCon::GVRET_SERIAL) editParams = true;
|
||||
|
||||
switch (index.column())
|
||||
switch (Column(index.column()))
|
||||
{
|
||||
case 3: //speed
|
||||
case Column::Speed:
|
||||
if (editParams) return Qt::ItemFlag::ItemIsEditable | Qt::ItemFlag::ItemIsEnabled;
|
||||
else return Qt::ItemFlag::NoItemFlags;
|
||||
break;
|
||||
case 4: //listen only
|
||||
case 5: //single wire
|
||||
return Qt::ItemFlag::NoItemFlags;
|
||||
case Column::ListenOnly:
|
||||
case Column::SingleWire:
|
||||
if (editParams) return Qt::ItemFlag::ItemIsEditable | Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsUserCheckable;
|
||||
else return Qt::ItemFlag::NoItemFlags;
|
||||
break;
|
||||
case 6: //enabled
|
||||
return Qt::ItemFlag::NoItemFlags;
|
||||
case Column::Active:
|
||||
return Qt::ItemFlag::ItemIsEditable | Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsUserCheckable;
|
||||
break;
|
||||
default:
|
||||
return Qt::ItemFlag::ItemIsEnabled;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,20 +120,21 @@ bool CANConnectionModel::setData(const QModelIndex &index, const QVariant &value
|
||||
ret = conn_p->getBusSettings(busId, bus);
|
||||
if (!ret) return false;
|
||||
|
||||
switch (index.column())
|
||||
switch (Column(index.column()))
|
||||
{
|
||||
case 3: //speed
|
||||
case Column::Speed:
|
||||
bus.speed = value.toInt();
|
||||
break;
|
||||
case 4: //listen only
|
||||
case Column::ListenOnly:
|
||||
bus.listenOnly = value.toBool();
|
||||
break;
|
||||
case 5: //single wire
|
||||
case Column::SingleWire:
|
||||
bus.singleWire = value.toBool();
|
||||
break;
|
||||
case 6: //active
|
||||
case Column::Active:
|
||||
bus.active = value.toBool();
|
||||
break;
|
||||
default: {}
|
||||
}
|
||||
conn_p->setBusSettings(busId, bus);
|
||||
return true;
|
||||
@@ -157,15 +156,14 @@ QVariant CANConnectionModel::data(const QModelIndex &index, int role) const
|
||||
|
||||
//qDebug() << "ConnP: " << conn_p << " ret " << ret;
|
||||
|
||||
if (role == Qt::DisplayRole) {
|
||||
if (role == Qt::DisplayRole) {
|
||||
|
||||
switch (index.column())
|
||||
switch (Column(index.column()))
|
||||
{
|
||||
case 0: //bus
|
||||
case Column::Bus:
|
||||
//return QString::number(busId);
|
||||
return QString::number(index.row());
|
||||
break;
|
||||
case 1: //type
|
||||
case Column::Type:
|
||||
if (conn_p)
|
||||
switch (conn_p->getType()) {
|
||||
case CANCon::KVASER: return "KVASER";
|
||||
@@ -175,38 +173,35 @@ QVariant CANConnectionModel::data(const QModelIndex &index, int role) const
|
||||
}
|
||||
else qDebug() << "Tried to show connection type but connection was NULL";
|
||||
break;
|
||||
case 2: //port
|
||||
case Column::Port:
|
||||
if (conn_p) return conn_p->getPort();
|
||||
else qDebug() << "Tried to show connection port but connection was NULL";
|
||||
break;
|
||||
case 3: //speed
|
||||
case Column::Speed:
|
||||
if(!ret) return QVariant();
|
||||
if (!isSocketCAN) return QString::number(bus.speed);
|
||||
else return QString("N/A");
|
||||
case 4: //Listen Only
|
||||
case Column::ListenOnly:
|
||||
return QVariant();
|
||||
case 5: //Single Wire
|
||||
case Column::SingleWire:
|
||||
return QVariant();
|
||||
case 6: //Status
|
||||
case Column::Active:
|
||||
return QVariant();
|
||||
case 7: //Active
|
||||
case Column::Status:
|
||||
return (conn_p->getStatus()==CANCon::CONNECTED) ? "Connected" : "Not Connected";
|
||||
default: {}
|
||||
}
|
||||
}
|
||||
if (role == Qt::CheckStateRole)
|
||||
{
|
||||
switch (index.column())
|
||||
switch (Column(index.column()))
|
||||
{
|
||||
case 4:
|
||||
case Column::ListenOnly:
|
||||
return (bus.listenOnly) ? Qt::Checked : Qt::Unchecked;
|
||||
break;
|
||||
case 5:
|
||||
case Column::SingleWire:
|
||||
return (bus.singleWire) ? Qt::Checked : Qt::Unchecked;
|
||||
break;
|
||||
case 6:
|
||||
case Column::Active:
|
||||
return (bus.active) ? Qt::Checked : Qt::Unchecked;
|
||||
break;
|
||||
default: {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,8 @@
|
||||
#include <QAbstractTableModel>
|
||||
|
||||
#include "canbus.h"
|
||||
#include "connections/canconnection.h"
|
||||
#include "connectionwindow.h"
|
||||
|
||||
class CANConnection;
|
||||
|
||||
class CANConnectionModel : public QAbstractTableModel
|
||||
{
|
||||
|
||||
@@ -1,31 +1,30 @@
|
||||
#include <QObject>
|
||||
#include <QDebug>
|
||||
#include "serialbusconnection.h"
|
||||
|
||||
#include "canconmanager.h"
|
||||
|
||||
#include <QCanBus>
|
||||
#include <QCanBusFrame>
|
||||
#include <QDateTime>
|
||||
|
||||
#include "socketcan.h"
|
||||
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
/***********************************/
|
||||
/**** class definition ****/
|
||||
/***********************************/
|
||||
|
||||
SocketCan::SocketCan(QString portName) :
|
||||
SerialBusConnection::SerialBusConnection(QString portName) :
|
||||
CANConnection(portName, CANCon::SOCKETCAN, 1, 4000, true),
|
||||
mDev_p(NULL),
|
||||
mTimer(this) /*NB: set connection as parent of timer to manage it from working thread */
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
SocketCan::~SocketCan()
|
||||
SerialBusConnection::~SerialBusConnection()
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
|
||||
void SocketCan::piStarted()
|
||||
void SerialBusConnection::piStarted()
|
||||
{
|
||||
connect(&mTimer, SIGNAL(timeout()), this, SLOT(testConnection()));
|
||||
mTimer.setInterval(1000);
|
||||
@@ -36,7 +35,7 @@ void SocketCan::piStarted()
|
||||
}
|
||||
|
||||
|
||||
void SocketCan::piSuspend(bool pSuspend)
|
||||
void SerialBusConnection::piSuspend(bool pSuspend)
|
||||
{
|
||||
/* update capSuspended */
|
||||
setCapSuspended(pSuspend);
|
||||
@@ -47,19 +46,19 @@ void SocketCan::piSuspend(bool pSuspend)
|
||||
}
|
||||
|
||||
|
||||
void SocketCan::piStop() {
|
||||
void SerialBusConnection::piStop() {
|
||||
mTimer.stop();
|
||||
disconnectDevice();
|
||||
}
|
||||
|
||||
|
||||
bool SocketCan::piGetBusSettings(int pBusIdx, CANBus& pBus)
|
||||
bool SerialBusConnection::piGetBusSettings(int pBusIdx, CANBus& pBus)
|
||||
{
|
||||
return getBusConfig(pBusIdx, pBus);
|
||||
}
|
||||
|
||||
|
||||
void SocketCan::piSetBusSettings(int pBusIdx, CANBus bus)
|
||||
void SerialBusConnection::piSetBusSettings(int pBusIdx, CANBus bus)
|
||||
{
|
||||
CANConStatus stats;
|
||||
/* sanity checks */
|
||||
@@ -67,8 +66,7 @@ void SocketCan::piSetBusSettings(int pBusIdx, CANBus bus)
|
||||
return;
|
||||
|
||||
/* disconnect device if we have one connected */
|
||||
if(mDev_p)
|
||||
disconnectDevice();
|
||||
disconnectDevice();
|
||||
|
||||
/* copy bus config */
|
||||
setBusConfig(0, bus);
|
||||
@@ -78,17 +76,18 @@ void SocketCan::piSetBusSettings(int pBusIdx, CANBus bus)
|
||||
return;
|
||||
|
||||
/* create device */
|
||||
mDev_p = QCanBus::instance()->createDevice("socketcan", getPort());
|
||||
QString errorString;
|
||||
mDev_p = QCanBus::instance()->createDevice("socketcan", getPort(), &errorString);
|
||||
if (!mDev_p) {
|
||||
disconnectDevice();
|
||||
qDebug() << "can't create device";
|
||||
qDebug() << "Error: createDevice(" << getType() << getPort() << "):" << errorString;
|
||||
return;
|
||||
}
|
||||
|
||||
/* connect slots */
|
||||
connect(mDev_p, &QCanBusDevice::errorOccurred, this, &SocketCan::errorReceived);
|
||||
connect(mDev_p, &QCanBusDevice::framesWritten, this, &SocketCan::framesWritten);
|
||||
connect(mDev_p, &QCanBusDevice::framesReceived, this, &SocketCan::framesReceived);
|
||||
connect(mDev_p, &QCanBusDevice::errorOccurred, this, &SerialBusConnection::errorReceived);
|
||||
connect(mDev_p, &QCanBusDevice::framesWritten, this, &SerialBusConnection::framesWritten);
|
||||
connect(mDev_p, &QCanBusDevice::framesReceived, this, &SerialBusConnection::framesReceived);
|
||||
|
||||
/* set configuration */
|
||||
/*if (p.useConfigurationEnabled) {
|
||||
@@ -107,7 +106,7 @@ void SocketCan::piSetBusSettings(int pBusIdx, CANBus bus)
|
||||
}
|
||||
|
||||
|
||||
bool SocketCan::piSendFrame(const CANFrame& pFrame)
|
||||
bool SerialBusConnection::piSendFrame(const CANFrame& pFrame)
|
||||
{
|
||||
/* sanity checks */
|
||||
if(0 != pFrame.bus || pFrame.len>8)
|
||||
@@ -131,16 +130,16 @@ bool SocketCan::piSendFrame(const CANFrame& pFrame)
|
||||
|
||||
|
||||
/* disconnect device */
|
||||
void SocketCan::disconnectDevice() {
|
||||
void SerialBusConnection::disconnectDevice() {
|
||||
if(mDev_p) {
|
||||
mDev_p->disconnectDevice();
|
||||
delete mDev_p;
|
||||
mDev_p = Q_NULLPTR;
|
||||
mDev_p = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SocketCan::errorReceived(QCanBusDevice::CanBusError error) const
|
||||
void SerialBusConnection::errorReceived(QCanBusDevice::CanBusError error) const
|
||||
{
|
||||
switch (error) {
|
||||
case QCanBusDevice::ReadError:
|
||||
@@ -155,13 +154,13 @@ void SocketCan::errorReceived(QCanBusDevice::CanBusError error) const
|
||||
}
|
||||
}
|
||||
|
||||
void SocketCan::framesWritten(qint64 count)
|
||||
void SerialBusConnection::framesWritten(qint64 count)
|
||||
{
|
||||
Q_UNUSED(count);
|
||||
//qDebug() << "Number of frames written:" << count;
|
||||
}
|
||||
|
||||
void SocketCan::framesReceived()
|
||||
void SerialBusConnection::framesReceived()
|
||||
{
|
||||
uint64_t timeBasis = CANConManager::getInstance()->getTimeBasis();
|
||||
|
||||
@@ -211,7 +210,7 @@ void SocketCan::framesReceived()
|
||||
}
|
||||
|
||||
|
||||
void SocketCan::testConnection() {
|
||||
void SerialBusConnection::testConnection() {
|
||||
QCanBusDevice* dev_p = QCanBus::instance()->createDevice("socketcan", getPort());
|
||||
CANConStatus stats;
|
||||
|
||||
@@ -1,23 +1,19 @@
|
||||
#ifndef SocketCan_H
|
||||
#define SocketCan_H
|
||||
#ifndef SERIALBUSCONNECTION_H
|
||||
#define SERIALBUSCONNECTION_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QCanBus>
|
||||
#include <QTimer>
|
||||
#include <qthread.h>
|
||||
|
||||
#include "canframemodel.h"
|
||||
#include "canconnection.h"
|
||||
#include "canconmanager.h"
|
||||
#include "canframemodel.h"
|
||||
|
||||
#include <QCanBusDevice>
|
||||
#include <QTimer>
|
||||
|
||||
class SocketCan : public CANConnection
|
||||
class SerialBusConnection : public CANConnection
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
SocketCan(QString portName);
|
||||
virtual ~SocketCan();
|
||||
SerialBusConnection(QString portName);
|
||||
virtual ~SerialBusConnection();
|
||||
|
||||
protected:
|
||||
|
||||
@@ -37,9 +33,9 @@ private slots:
|
||||
void testConnection();
|
||||
|
||||
protected:
|
||||
QCanBusDevice* mDev_p;
|
||||
QCanBusDevice *mDev_p = nullptr;
|
||||
QTimer mTimer;
|
||||
};
|
||||
|
||||
|
||||
#endif // SocketCan_H
|
||||
#endif // SERIALBUSCONNECTION_H
|
||||
Reference in New Issue
Block a user