severals changes to adapt to new connection model and connection API

This commit is contained in:
canpadawan
2016-06-15 18:44:45 +02:00
parent fd672de5c5
commit ed1b9b082b
2 changed files with 193 additions and 137 deletions
+175 -120
View File
@@ -2,8 +2,7 @@
#include "connectionwindow.h" #include "connectionwindow.h"
#include "ui_connectionwindow.h" #include "ui_connectionwindow.h"
#include "socketcanconnection.h" #include "connections/canconfactory.h"
ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) : ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) :
@@ -26,10 +25,11 @@ ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) :
ui->tableConnections->setColumnWidth(0, 50); ui->tableConnections->setColumnWidth(0, 50);
ui->tableConnections->setColumnWidth(1, 110); ui->tableConnections->setColumnWidth(1, 110);
ui->tableConnections->setColumnWidth(2, 110); ui->tableConnections->setColumnWidth(2, 110);
ui->tableConnections->setColumnWidth(3, 150); ui->tableConnections->setColumnWidth(3, 110);
ui->tableConnections->setColumnWidth(4, 75); ui->tableConnections->setColumnWidth(4, 75);
ui->tableConnections->setColumnWidth(5, 75); ui->tableConnections->setColumnWidth(5, 75);
ui->tableConnections->setColumnWidth(6, 75); ui->tableConnections->setColumnWidth(6, 75);
ui->tableConnections->setColumnWidth(7, 75);
int temp = settings->value("Main/DefaultConnectionType", 0).toInt(); int temp = settings->value("Main/DefaultConnectionType", 0).toInt();
@@ -40,7 +40,6 @@ ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) :
ui->ckSingleWire->setChecked(settings->value("Main/SingleWireMode", false).toBool()); ui->ckSingleWire->setChecked(settings->value("Main/SingleWireMode", false).toBool());
ui->cbSpeed->addItem(tr("<Default>")); ui->cbSpeed->addItem(tr("<Default>"));
ui->cbSpeed->addItem(tr("Disabled"));
ui->cbSpeed->addItem(tr("125000")); ui->cbSpeed->addItem(tr("125000"));
ui->cbSpeed->addItem(tr("250000")); ui->cbSpeed->addItem(tr("250000"));
ui->cbSpeed->addItem(tr("500000")); ui->cbSpeed->addItem(tr("500000"));
@@ -66,10 +65,19 @@ ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) :
connect(ui->btnActivateAll, &QPushButton::clicked, this, &ConnectionWindow::handleEnableAll); connect(ui->btnActivateAll, &QPushButton::clicked, this, &ConnectionWindow::handleEnableAll);
connect(ui->btnDeactivateAll, &QPushButton::clicked, this, &ConnectionWindow::handleDisableAll); connect(ui->btnDeactivateAll, &QPushButton::clicked, this, &ConnectionWindow::handleDisableAll);
connect(ui->btnRemoveBus, &QPushButton::clicked, this, &ConnectionWindow::handleRemoveConn); connect(ui->btnRemoveBus, &QPushButton::clicked, this, &ConnectionWindow::handleRemoveConn);
qDebug() << "Serial worker thread starting";
connect(&mTicker, SIGNAL(timeout()), this, SLOT(refreshCanList()));
mTicker.setInterval(250); //tick 4 times a second (TODO: make this configurable?) */
mTicker.setSingleShot(false);
mTicker.start();
} }
ConnectionWindow::~ConnectionWindow() ConnectionWindow::~ConnectionWindow()
{ {
mTicker.stop();
delete settings; delete settings;
delete ui; delete ui;
} }
@@ -81,6 +89,20 @@ void ConnectionWindow::showEvent(QShowEvent* event)
handleConnTypeChanged(); handleConnTypeChanged();
} }
void ConnectionWindow::refreshCanList() {
QList<CANConnection*>::iterator conn_p;
QList<CANConnection*>& conns = connModel->getConnections();
CANFrame* frame_p = NULL;
for (conn_p = conns.begin(); conn_p != conns.end(); ++conn_p) {
while( (frame_p = (*conn_p)->getQueue().peek() ) ) {
canModel->addFrame(*frame_p, true);
(*conn_p)->getQueue().dequeue();
}
}
}
void ConnectionWindow::handleNewConn() void ConnectionWindow::handleNewConn()
{ {
ui->tableConnections->selectionModel()->clearSelection(); ui->tableConnections->selectionModel()->clearSelection();
@@ -88,91 +110,101 @@ void ConnectionWindow::handleNewConn()
handleConnSelectionChanged(); handleConnSelectionChanged();
} }
void ConnectionWindow::setSuspendAll(bool pSuspend)
{
qDebug() << "setSuspendAll";
QList<CANConnection*>::iterator iter;
QList<CANConnection*>& conns = connModel->getConnections();
for (iter = conns.begin(); iter != conns.end(); ++iter)
(*iter)->suspend(pSuspend);
}
void ConnectionWindow::setActiveAll(bool pActive)
{
QList<CANConnection*>::iterator iter;
QList<CANConnection*>& conns = connModel->getConnections();
CANBus bus;
for (iter = conns.begin(); iter != conns.end(); ++iter) {
for(int i=0 ; i<(*iter)->getNumBuses() ; i++) {
if( (*iter)->getBusSettings(i, bus) ) {
bus.active = pActive;
(*iter)->setBusSettings(i, bus);
}
}
}
}
void ConnectionWindow::handleEnableAll() void ConnectionWindow::handleEnableAll()
{ {
setActiveAll(true);
} }
void ConnectionWindow::handleDisableAll() void ConnectionWindow::handleDisableAll()
{ {
setActiveAll(false);
} }
void ConnectionWindow::handleConnTypeChanged() void ConnectionWindow::handleConnTypeChanged()
{ {
if (ui->rbGVRET->isChecked()) getSerialPorts(); if (ui->rbGVRET->isChecked()) selectSerial();
if (ui->rbKvaser->isChecked()) getKvaserPorts(); if (ui->rbKvaser->isChecked()) selectKvaser();
if (ui->rbSocketCAN->isChecked()) getSocketcanPorts(); if (ui->rbSocketCAN->isChecked()) selectSocketCan();
} }
void ConnectionWindow::connectionSuccess(CANConnection *conn)
/* status */
void ConnectionWindow::connectionStatus(CANCon::status pStatus)
{ {
#if 0 qDebug() << "Connectionstatus changed";
CANBus bus; connModel->refreshView();
bus.active = true;
bus.busNum = conn->getBusBase();
bus.connection = conn;
bus.listenOnly = ui->ckListenOnly->isChecked();
bus.singleWire = ui->ckSingleWire->isChecked();
if (ui->cbSpeed->currentIndex() < 1) bus.speed = 0; //default speed
else if (ui->cbSpeed->currentIndex() == 1)
{
bus.speed = 0;
bus.active = false;
}
else bus.speed = ui->cbSpeed->currentText().toInt();
connModel->addBus(bus);
int numBuses = conn->getNumBuses();
for (int i = 1; i < numBuses; i++)
{
bus.active = false;
bus.listenOnly = false;
bus.singleWire = false;
bus.speed = 250000;
bus.busNum = conn->getBusBase() + i;
bus.connection = conn;
connModel->addBus(bus);
qDebug() << "Added bus " << bus.busNum;
}
#endif
} }
void ConnectionWindow::handleOKButton() void ConnectionWindow::handleOKButton()
{ {
QString conn;
int connType = 0;
int whichRow = ui->tableConnections->selectionModel()->currentIndex().row(); int whichRow = ui->tableConnections->selectionModel()->currentIndex().row();
qDebug() << "OK pressed. Row " << whichRow;
CANConnection* conn_p = NULL;
if (whichRow > -1) if (whichRow > -1)
{ {
//load settings from GUI into appropriate CANBus entry and then send it off to the appropriate int busId;
//CANConnection object CANBus bus;
CANBus *bus = connModel->getBus(whichRow); bool ret;
bus->setListenOnly(ui->ckListenOnly->isChecked());
bus->setSingleWire(ui->ckSingleWire->isChecked()); conn_p = connModel->getAtIdx(whichRow, busId);
bus->setEnabled(ui->ckEnabled->isChecked()); if(!conn_p) return;
if (ui->cbSpeed->currentIndex() == 1)
ret = conn_p->getBusSettings(busId, bus);
if(!ret) return;
bus.setListenOnly(ui->ckListenOnly->isChecked());
bus.setSingleWire(ui->ckSingleWire->isChecked());
bus.setEnabled(ui->ckEnabled->isChecked());
if (ui->cbSpeed->currentIndex() == 0)
{ {
bus->speed = 0; bus.speed = 0;
bus->setEnabled(false); bus.setEnabled(false);
} }
else if (ui->cbSpeed->currentIndex() > 1) else if (ui->cbSpeed->currentIndex() >= 1)
{ {
bus->setSpeed(ui->cbSpeed->currentText().toInt()); bus.setSpeed(ui->cbSpeed->currentText().toInt());
} }
//call through signal/slot interface without using connect /* update bus settings */
QMetaObject::invokeMethod(bus->getContainer()->getRef(), "updateBusSettings", conn_p->setBusSettings(busId, bus);
Qt::QueuedConnection,
Q_ARG(CANBus, *bus));
connModel->refreshView(); connModel->refreshView();
} }
else //new connection else //new connection
{ {
#if 0
if (ui->rbGVRET->isChecked()) if (ui->rbGVRET->isChecked())
{ {
SerialWorker *serial = new SerialWorker(canModel, connModel->rowCount()); SerialWorker *serial = new SerialWorker(canModel, connModel->rowCount());
connect(serial, SIGNAL(busStatus(int,int,int)), this, SLOT(receiveBusStatus(int,int,int))); connect(serial, SIGNAL(busStatus(int,int,int)), this, SLOT(receiveBusStatus(int,int,int)));
connect(serial, SIGNAL(connectionSuccess(CANConnection*)), this, SLOT(connectionSuccess(CANConnection*))); connect(serial, SIGNAL(connectionSuccess(CANConnection*)), this, SLOT(connectionSuccess(CANConnection*)));
@@ -215,55 +247,43 @@ void ConnectionWindow::handleOKButton()
QMetaObject::invokeMethod(serial, "updatePortName", QMetaObject::invokeMethod(serial, "updatePortName",
Qt::QueuedConnection, Qt::QueuedConnection,
Q_ARG(QString, ui->cbPort->currentText())); Q_ARG(QString, ui->cbPort->currentText()));
} #endif
else if (ui->rbKvaser->isChecked())
{
} /* create connection */
else if (ui->rbSocketCAN->isChecked()) conn_p = CanConFactory::create(getConnectionType(), getPortName());
{ if(!conn_p)
/* TODO: sanity checks here */ return;
/* text, port, etc, ... */
/* factorize code with other probes */
/* create connection */ /* connect signal */
SocketCanConnection* conn = new SocketCanConnection(canModel, 0); /* what is base? (second param) */ connect(conn_p, SIGNAL(status(CANCon::status)),
this, SLOT(connectionStatus(CANCon::status)));
/* set port name */ /*TODO add return value and checks */
conn->updatePortName(ui->lePort->text()); conn_p->start();
/* add connection to a container (this creates the thread) */
CANConnectionContainer* container = new CANConnectionContainer(conn);
for (int i=0 ; i<conn_p->getNumBuses() ; i++) {
/* set bus configuration */ /* set bus configuration */
CANBus bus; CANBus bus;
bus.active = ui->ckEnabled->isChecked(); bus.active = ui->ckEnabled->isChecked();
bus.busNum = 0; // not used
bus.container = container;
bus.listenOnly = ui->ckListenOnly->isChecked(); bus.listenOnly = ui->ckListenOnly->isChecked();
bus.singleWire = ui->ckSingleWire->isChecked(); bus.singleWire = ui->ckSingleWire->isChecked();
if (ui->cbSpeed->currentIndex() < 1) bus.speed = 0; //default speed if (ui->cbSpeed->currentIndex() < 1) bus.speed = 0;
else if (ui->cbSpeed->currentIndex() == 1)
{
bus.speed = 0;
bus.active = false;
}
else bus.speed = ui->cbSpeed->currentText().toInt(); else bus.speed = ui->cbSpeed->currentText().toInt();
/* add bus to model */ /* update bus settings */
connModel->addBus(bus); conn_p->setBusSettings(i, bus);
/* set bus settings (send message to thread) */
QMetaObject::invokeMethod(conn, "updateBusSettings",
Qt::QueuedConnection,
Q_ARG(CANBus, bus));
} }
/* add connection to model */
connModel->add(conn_p);
} }
} }
void ConnectionWindow::receiveBusStatus(int bus, int speed, int status) void ConnectionWindow::receiveBusStatus(int bus, int speed, int status)
{ {
#if 0
qDebug() << "bus " << bus << " speed " << speed << " status " << status; qDebug() << "bus " << bus << " speed " << speed << " status " << status;
CANBus *busRef = connModel->getBus(bus); CANBus *busRef = connModel->getBus(bus);
if (status & 40) busRef->setSpeed(speed); if (status & 40) busRef->setSpeed(speed);
@@ -280,6 +300,7 @@ void ConnectionWindow::receiveBusStatus(int bus, int speed, int status)
busRef->setListenOnly((status & 4)?true:false); busRef->setListenOnly((status & 4)?true:false);
} }
connModel->refreshView(); connModel->refreshView();
#endif
} }
void ConnectionWindow::handleConnSelectionChanged() void ConnectionWindow::handleConnSelectionChanged()
@@ -289,10 +310,13 @@ void ConnectionWindow::handleConnSelectionChanged()
if (selIdx == -1) if (selIdx == -1)
{ {
ui->btnOK->setText(tr("Create New Connection")); ui->btnOK->setText(tr("Create New Connection"));
ui->cbPort->setEnabled(true); ui->cbPort->setEnabled(true);
ui->rbGVRET->setEnabled(true); ui->rbGVRET->setEnabled(true);
ui->rbKvaser->setEnabled(true); ui->rbKvaser->setEnabled(true);
ui->rbSocketCAN->setEnabled(true); ui->rbSocketCAN->setEnabled(true);
ui->cbSpeed->setEnabled(false);
ui->cbPort->setCurrentIndex(0); ui->cbPort->setCurrentIndex(0);
ui->ckListenOnly->setChecked(false); ui->ckListenOnly->setChecked(false);
ui->ckSingleWire->setChecked(false); ui->ckSingleWire->setChecked(false);
@@ -300,25 +324,39 @@ void ConnectionWindow::handleConnSelectionChanged()
} }
else else
{ {
int busId;
bool ret;
CANBus bus;
CANConnection* conn_p = connModel->getAtIdx(selIdx, busId);
if(!conn_p) return;
ret = conn_p->getBusSettings(busId, bus);
if(!ret) return;
ui->btnOK->setText(tr("Update Connection Settings")); ui->btnOK->setText(tr("Update Connection Settings"));
ui->cbPort->setEnabled(false); ui->cbPort->setEnabled(false);
ui->rbGVRET->setEnabled(false); ui->rbGVRET->setEnabled(false);
ui->rbKvaser->setEnabled(false); ui->rbKvaser->setEnabled(false);
ui->rbSocketCAN->setEnabled(false); ui->rbSocketCAN->setEnabled(false);
CANBus *bus = connModel->getBus(selIdx); ui->cbSpeed->setEnabled(true);
if (bus->getContainer()->getRef()->getConnTypeName() == "GVRET") ui->rbGVRET->setChecked(true);
if (bus->getContainer()->getRef()->getConnTypeName() == "KVASER") ui->rbKvaser->setChecked(true); switch(conn_p->getType()) {
if (bus->getContainer()->getRef()->getConnTypeName() == "SOCKETCAN") ui->rbSocketCAN->setChecked(true); case CANCon::GVRET_SERIAL: ui->rbGVRET->setChecked(true); break;
ui->ckListenOnly->setChecked(bus->isListenOnly()); case CANCon::KVASER: ui->rbKvaser->setChecked(true); break;
ui->ckSingleWire->setChecked(bus->isSingleWire()); case CANCon::SOCKETCAN: ui->rbSocketCAN->setChecked(true); break;
ui->ckEnabled->setChecked(bus->isActive()); default: {}
int speed = bus->getSpeed(); }
ui->ckListenOnly->setChecked(bus.isListenOnly());
ui->ckSingleWire->setChecked(bus.isSingleWire());
ui->ckEnabled->setChecked(bus.isActive());
int speed = bus.getSpeed();
setSpeed(speed); setSpeed(speed);
} }
} }
/*TODO: rename the 3 following functions */
void ConnectionWindow::getSerialPorts() void ConnectionWindow::selectSerial()
{ {
/* set combobox page visible */ /* set combobox page visible */
ui->stPort->setCurrentWidget(ui->cbPage); ui->stPort->setCurrentWidget(ui->cbPage);
@@ -333,7 +371,7 @@ void ConnectionWindow::getSerialPorts()
} }
} }
void ConnectionWindow::getKvaserPorts() void ConnectionWindow::selectKvaser()
{ {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
/* set combobox page visible */ /* set combobox page visible */
@@ -341,7 +379,7 @@ void ConnectionWindow::getKvaserPorts()
#endif #endif
} }
void ConnectionWindow::getSocketcanPorts() void ConnectionWindow::selectSocketCan()
{ {
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
/* set edit text page visible */ /* set edit text page visible */
@@ -394,16 +432,30 @@ int ConnectionWindow::getSpeed()
QString ConnectionWindow::getPortName() QString ConnectionWindow::getPortName()
{ {
return (ui->cbPort->currentText()); switch( getConnectionType() ) {
case CANCon::GVRET_SERIAL:
case CANCon::KVASER:
return ui->cbPort->currentText();
case CANCon::SOCKETCAN:
return ui->lePort->text();
default:
qDebug() << "getPortName: can't get port";
}
return "";
} }
ConnectionType::ConnectionType ConnectionWindow::getConnectionType() CANCon::type ConnectionWindow::getConnectionType()
{ {
if (ui->rbGVRET->isChecked()) return ConnectionType::GVRET_SERIAL; if (ui->rbGVRET->isChecked()) return CANCon::GVRET_SERIAL;
if (ui->rbKvaser->isChecked()) return ConnectionType::KVASER; if (ui->rbKvaser->isChecked()) return CANCon::KVASER;
if (ui->rbSocketCAN->isChecked()) return ConnectionType::SOCKETCAN; if (ui->rbSocketCAN->isChecked()) return CANCon::SOCKETCAN;
qDebug() << "getConnectionType: error";
return CANCon::NONE;
} }
void ConnectionWindow::setSWMode(bool mode) void ConnectionWindow::setSWMode(bool mode)
{ {
ui->ckSingleWire->setChecked(mode); ui->ckSingleWire->setChecked(mode);
@@ -417,22 +469,21 @@ bool ConnectionWindow::getSWMode()
void ConnectionWindow::handleRemoveConn() void ConnectionWindow::handleRemoveConn()
{ {
#if 0
int selIdx = ui->tableConnections->selectionModel()->currentIndex().row(); int selIdx = ui->tableConnections->selectionModel()->currentIndex().row();
if (selIdx <=0) return; if (selIdx <0) return;
CANBus* bus = conModel->getBus(selIdx);
if(!bus) return;
/* remove connection to model (this creates the thread) */
connModel->removeConnection(bus->connection);
/* remove connection and reset model */
conModel->removeBus(selIdx);
qDebug() << "remove connection at index: " << selIdx; qDebug() << "remove connection at index: " << selIdx;
selIdx int busId;
#endif CANConnection* conn_p = connModel->getAtIdx(selIdx, busId);
if(!conn_p) return;
/* remove connection from model */
connModel->remove(conn_p);
/* stop and delete connection */
conn_p->stop();
delete conn_p;
} }
void ConnectionWindow::handleRevert() void ConnectionWindow::handleRevert()
@@ -442,21 +493,25 @@ void ConnectionWindow::handleRevert()
void ConnectionWindow::sendFrame(const CANFrame *frame) void ConnectionWindow::sendFrame(const CANFrame *frame)
{ {
#if 0
CANBus *bus = connModel->getBus(frame->bus); CANBus *bus = connModel->getBus(frame->bus);
if (bus == NULL) return; if (bus == NULL) return;
QMetaObject::invokeMethod(bus->getContainer()->getRef(), "sendFrame", QMetaObject::invokeMethod(bus->getContainer()->getRef(), "sendFrame",
Qt::QueuedConnection, Qt::QueuedConnection,
Q_ARG(const CANFrame *, frame)); Q_ARG(const CANFrame *, frame));
#endif
} }
void ConnectionWindow::sendFrameBatch(const QList<CANFrame> *frames) void ConnectionWindow::sendFrameBatch(const QList<CANFrame> *frames)
{ {
#if 0
if (frames->count() == 0) return; if (frames->count() == 0) return;
CANBus *bus = connModel->getBus(frames->at(0).bus); CANBus *bus = connModel->getBus(frames->at(0).bus);
if (bus == NULL) return; if (bus == NULL) return;
QMetaObject::invokeMethod(bus->getContainer()->getRef(), "sendFrameBatch", QMetaObject::invokeMethod(bus->getContainer()->getRef(), "sendFrameBatch",
Qt::QueuedConnection, Qt::QueuedConnection,
Q_ARG(const QList<CANFrame> *, frames)); Q_ARG(const QList<CANFrame> *, frames));
#endif
} }
bool ConnectionWindow::isSocketCanAvailable() bool ConnectionWindow::isSocketCanAvailable()
+18 -17
View File
@@ -1,14 +1,18 @@
#ifndef CONNECTIONWINDOW_H #ifndef CONNECTIONWINDOW_H
#define CONNECTIONWINDOW_H #define CONNECTIONWINDOW_H
#include "canconnectionmodel.h"
#include <QSerialPortInfo> #include <QSerialPortInfo>
#include <QDialog> #include <QDialog>
#include <QDebug> #include <QDebug>
#include <QSettings> #include <QSettings>
#include "canconnection.h" #include <QTimer>
#include "serialworker.h" //#include "canconnection_old.h"
//#include "serialworker.h"
#include "canconnectionmodel.h"
#include "canframemodel.h"
class CANConnectionModel; class CANConnectionModel;
@@ -16,15 +20,6 @@ namespace Ui {
class ConnectionWindow; class ConnectionWindow;
} }
namespace ConnectionType
{
enum ConnectionType
{
GVRET_SERIAL,
KVASER,
SOCKETCAN
};
}
class ConnectionWindow : public QDialog class ConnectionWindow : public QDialog
{ {
@@ -36,7 +31,7 @@ public:
void showEvent(QShowEvent *); void showEvent(QShowEvent *);
int getSpeed(); int getSpeed();
QString getPortName(); //name of port to connect to QString getPortName(); //name of port to connect to
ConnectionType::ConnectionType getConnectionType(); CANCon::type getConnectionType();
bool getSWMode(); bool getSWMode();
signals: signals:
@@ -48,6 +43,8 @@ public slots:
void setSWMode(bool mode); void setSWMode(bool mode);
void sendFrame(const CANFrame *); void sendFrame(const CANFrame *);
void sendFrameBatch(const QList<CANFrame> *); void sendFrameBatch(const QList<CANFrame> *);
void setSuspendAll(bool);
private slots: private slots:
void handleOKButton(); void handleOKButton();
@@ -59,7 +56,9 @@ private slots:
void handleRevert(); void handleRevert();
void handleNewConn(); void handleNewConn();
void receiveBusStatus(int bus, int speed, int status); void receiveBusStatus(int bus, int speed, int status);
void connectionSuccess(CANConnection *conn); void connectionStatus(CANCon::status);
void refreshCanList();
private: private:
Ui::ConnectionWindow *ui; Ui::ConnectionWindow *ui;
@@ -67,12 +66,14 @@ private:
QSettings *settings; QSettings *settings;
CANConnectionModel *connModel; CANConnectionModel *connModel;
CANFrameModel *canModel; CANFrameModel *canModel;
QTimer mTicker;
void getSerialPorts(); void selectSerial();
void getKvaserPorts(); void selectKvaser();
void getSocketcanPorts(); void selectSocketCan();
bool isSocketCanAvailable(); bool isSocketCanAvailable();
void setActiveAll(bool pActive);
}; };
#endif // CONNECTIONWINDOW_H #endif // CONNECTIONWINDOW_H