initial support for socketcan

This commit is contained in:
canpadawan
2016-06-08 11:30:44 +02:00
parent 839027eee1
commit 97db24b698
4 changed files with 218 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
#include "canbus.h"
CAN_Bus::CAN_Bus()
{
}
+11
View File
@@ -0,0 +1,11 @@
#ifndef CAN_BUS_H
#define CAN_BUS_H
class CAN_Bus
{
public:
CAN_Bus();
};
#endif // CAN_BUS_H
+157
View File
@@ -0,0 +1,157 @@
#include <QObject>
#include <QDebug>
#include <QCanBusFrame>
#include "canframemodel.h"
#include "canconnection.h"
#include "socketcanconnection.h"
/***********************************/
/**** nested class ****/
/***********************************/
BUSConfig::BUSConfig():isConfigured(false){}
void BUSConfig::reset() {
isConfigured = false;
}
bool BUSConfig::operator==(CANBus& bus) {
return isConfigured && speed == bus.speed &&
listenOnly == bus.listenOnly && active == bus.active;
}
void BUSConfig::operator=(CANBus& bus) {
isConfigured = true;
speed = bus.speed;
listenOnly = bus.listenOnly;
active = bus.active;
}
/***********************************/
/**** class definition ****/
/***********************************/
SocketCanConnection::SocketCanConnection(CANFrameModel *model, int base) : CANConnection(model, base), mDev_p(NULL)
{
qDebug() << "SocketCanConnection()";
qRegisterMetaType<CANFrame>("CANFrame");
}
SocketCanConnection::~SocketCanConnection()
{
qDebug() << "~SocketCanConnection()";
/* stop device */
}
void SocketCanConnection::updateBusSettings(CANBus bus)
{
qDebug()<<"updateBusSettings";
if(mConf == bus) return;
/* disconnect device if we have one connected */
if(mDev_p) disconnect();
/* if bus is not active we are done */
if(!bus.active) return;
/* create device */
mDev_p = QCanBus::instance()->createDevice("socketcan", portName);
if (!mDev_p) {
qDebug() << "can't create device";
return;
}
/* connect slots */
connect(mDev_p, &QCanBusDevice::errorOccurred, this, &SocketCanConnection::errorReceived);
connect(mDev_p, &QCanBusDevice::framesReceived, this, &SocketCanConnection::framesReceived);
connect(mDev_p, &QCanBusDevice::framesWritten, this, &SocketCanConnection::framesWritten);
/* set configuration */
/*if (p.useConfigurationEnabled) {
foreach (const SettingsDialog::ConfigurationItem &item, p.configurations)
mDev->setConfigurationParameter(item.first, item.second);
}*/
/* connect device */
if (!mDev_p->connectDevice()) {
disconnect();
qDebug() << "can't connect device";
}
}
/***********************************/
/**** private methods ****/
/***********************************/
/* connect device */
void SocketCanConnection::disconnect() {
if(mDev_p) {
mDev_p->disconnectDevice();
delete mDev_p;
mDev_p = Q_NULLPTR;
}
}
void SocketCanConnection::errorReceived(QCanBusDevice::CanBusError error) const
{
switch (error) {
case QCanBusDevice::ReadError:
case QCanBusDevice::WriteError:
case QCanBusDevice::ConnectionError:
case QCanBusDevice::ConfigurationError:
case QCanBusDevice::UnknownError:
qWarning() << mDev_p->errorString();
default:
break;
}
}
void SocketCanConnection::framesWritten(qint64 count)
{
qDebug() << "Number of frames written:" << count;
}
void SocketCanConnection::framesReceived()
{
/* sanity checks */
if(!mDev_p) return;
/* read frame */
while(true)
{
const QCanBusFrame recFrame = mDev_p->readFrame();
/* exit case */
if(!recFrame.isValid()) return;
if(!recFrame.payload().isEmpty() &&
recFrame.payload().length()<=8)
{
CANFrame frame;
frame.len = recFrame.payload().length();
frame.bus = 0;
memcpy(frame.data, recFrame.payload().data(), frame.len);
frame.extended = false;
frame.ID = recFrame.frameId();
frame.isReceived = true;
frame.timestamp = recFrame.timeStamp().microSeconds();
/* send frame */
QMetaObject::invokeMethod(model, "addFrame",
Qt::QueuedConnection,
Q_ARG(CANFrame, frame),
Q_ARG(bool, false));
}
else {
qDebug() << "invalid frame";
}
}
}
+44
View File
@@ -0,0 +1,44 @@
#ifndef SOCKETCANCONNECTION_H
#define SOCKETCANCONNECTION_H
#include <QObject>
#include <QCanBus>
#include "canframemodel.h"
#include "canconnection.h"
class BUSConfig {
public:
BUSConfig();
void reset();
bool operator ==(CANBus&);
void operator =(CANBus&);
bool isConfigured;
int speed;
bool listenOnly;
bool active;
};
class SocketCanConnection : public CANConnection
{
Q_OBJECT
public:
SocketCanConnection(CANFrameModel *, int);
virtual ~SocketCanConnection() override;
public slots:
virtual void updateBusSettings(CANBus) override;
private:
void errorReceived(QCanBusDevice::CanBusError error) const;
void framesWritten(qint64 count);
void framesReceived();
void disconnect();
QCanBusDevice* mDev_p;
BUSConfig mConf;
};
#endif // SOCKETCANCONNECTION_H