From 18b1a86a5658ed8082b20719c0ef01f21e3a96c2 Mon Sep 17 00:00:00 2001 From: canpadawan Date: Wed, 8 Jun 2016 11:45:17 +0200 Subject: [PATCH] under heavy work... --- canconnectionmodel.cpp | 31 ++++----- canconnectionmodel.h | 13 ++-- connectionwindow.cpp | 148 ++++++++++++++++++++++++++++------------- connectionwindow.h | 5 +- connectionwindow.ui | 68 ++++++++++++++++++- mainwindow.cpp | 5 +- 6 files changed, 194 insertions(+), 76 deletions(-) diff --git a/canconnectionmodel.cpp b/canconnectionmodel.cpp index 8510f8d..7ec8ca7 100644 --- a/canconnectionmodel.cpp +++ b/canconnectionmodel.cpp @@ -71,8 +71,8 @@ QVariant CANConnectionModel::data(const QModelIndex &index, int role) const return QVariant(); if (role == Qt::DisplayRole) { - CAN_Bus bus = buses[index.row()]; - CANConnection *conn = bus.connection; + CANBus bus = buses[index.row()]; + CANConnection *conn = bus.getContainer()->getRef(); switch (index.column()) { case 0: //bus @@ -109,35 +109,32 @@ QVariant CANConnectionModel::data(const QModelIndex &index, int role) const return QVariant(); } -void CANConnectionModel::addConnection(CANConnection *conn) -{ - CAN_Bus bus; - CANConnectionContainer *cont = new CANConnectionContainer(conn); - connections.append(cont); -} -void CANConnectionModel::addBus(CAN_Bus &bus) +void CANConnectionModel::addBus(CANBus &bus) { beginResetModel(); buses.append(bus); endResetModel(); } -CAN_Bus* CANConnectionModel::getBus(int bus) +void CANConnectionModel::removeBus(int busIdx) +{ + if (busIdx>=0 && busIdx < buses.count()) { + beginResetModel(); + buses.removeAt(busIdx); + endResetModel(); + } +} + +CANBus* CANConnectionModel::getBus(int bus) { if (bus < 0) return NULL; if (bus >= buses.count()) return NULL; return &buses[bus]; } -CANConnection* CANConnectionModel::getConnection(int conn) -{ - if (conn < 0) return NULL; - if (conn >= connections.count()) return NULL; - return connections[conn]->getRef(); -} -CAN_Bus* CANConnectionModel::findBusByNum(int bus) +CANBus* CANConnectionModel::findBusByNum(int bus) { for (int i = 0; i < buses.count(); i++) { diff --git a/canconnectionmodel.h b/canconnectionmodel.h index 1e0410e..22dacdc 100644 --- a/canconnectionmodel.h +++ b/canconnectionmodel.h @@ -2,6 +2,7 @@ #define CANCONNECTIONMODEL_H #include "canconnection.h" +#include "canbus.h" #include "canconnectioncontainer.h" #include @@ -23,17 +24,19 @@ public: QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - void addConnection(CANConnection *conn); - void addBus(CAN_Bus &bus); - CAN_Bus* getBus(int bus); + void addConnection(CANConnectionContainer *conn); + void removeConnection(CANConnection*); + void addBus(CANBus &bus); + void removeBus(int busIdx); + CANBus* getBus(int bus); CANConnection* getConnection(int conn); void refreshView(); private: QList connections; - QList buses; + QList buses; - CAN_Bus *findBusByNum(int bus); + CANBus *findBusByNum(int bus); }; #endif // CANCONNECTIONMODEL_H diff --git a/connectionwindow.cpp b/connectionwindow.cpp index 1ee5c74..8014093 100644 --- a/connectionwindow.cpp +++ b/connectionwindow.cpp @@ -1,6 +1,10 @@ +#include + #include "connectionwindow.h" #include "ui_connectionwindow.h" -#include +#include "socketcanconnection.h" + + ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) : QDialog(parent), @@ -10,7 +14,7 @@ ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) : settings = new QSettings(); - qRegisterMetaType("CAN_Bus *"); + qRegisterMetaType("CANBus"); qRegisterMetaType("const CANFrame *"); qRegisterMetaType *>("const QList *"); @@ -44,7 +48,7 @@ ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) : ui->cbSpeed->addItem(tr("33333")); #ifdef Q_OS_LINUX - ui->rbSocketCAN->setEnabled(true); + ui->rbSocketCAN->setEnabled(isSocketCanAvailable()); #endif #ifdef Q_OS_WIN @@ -57,6 +61,7 @@ ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) : connect(ui->rbSocketCAN, &QAbstractButton::toggled, this, &ConnectionWindow::handleConnTypeChanged); connect(ui->btnRevert, &QPushButton::clicked, this, &ConnectionWindow::handleRevert); connect(ui->tableConnections->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ConnectionWindow::handleConnSelectionChanged); + connect(connModel, &QAbstractItemModel::modelReset, this, &ConnectionWindow::handleConnSelectionChanged); connect(ui->btnNewConn, &QPushButton::clicked, this, &ConnectionWindow::handleNewConn); connect(ui->btnActivateAll, &QPushButton::clicked, this, &ConnectionWindow::handleEnableAll); connect(ui->btnDeactivateAll, &QPushButton::clicked, this, &ConnectionWindow::handleDisableAll); @@ -102,7 +107,8 @@ void ConnectionWindow::handleConnTypeChanged() void ConnectionWindow::connectionSuccess(CANConnection *conn) { - CAN_Bus bus; +#if 0 + CANBus bus; bus.active = true; bus.busNum = conn->getBusBase(); bus.connection = conn; @@ -130,6 +136,7 @@ void ConnectionWindow::connectionSuccess(CANConnection *conn) connModel->addBus(bus); qDebug() << "Added bus " << bus.busNum; } +#endif } void ConnectionWindow::handleOKButton() @@ -141,9 +148,9 @@ void ConnectionWindow::handleOKButton() if (whichRow > -1) { - //load settings from GUI into appropriate CAN_Bus entry and then send it off to the appropriate + //load settings from GUI into appropriate CANBus entry and then send it off to the appropriate //CANConnection object - CAN_Bus *bus = connModel->getBus(whichRow); + CANBus *bus = connModel->getBus(whichRow); bus->setListenOnly(ui->ckListenOnly->isChecked()); bus->setSingleWire(ui->ckSingleWire->isChecked()); bus->setEnabled(ui->ckEnabled->isChecked()); @@ -157,9 +164,9 @@ void ConnectionWindow::handleOKButton() bus->setSpeed(ui->cbSpeed->currentText().toInt()); } //call through signal/slot interface without using connect - QMetaObject::invokeMethod(bus->connection, "updateBusSettings", + QMetaObject::invokeMethod(bus->getContainer()->getRef(), "updateBusSettings", Qt::QueuedConnection, - Q_ARG(CAN_Bus *, bus)); + Q_ARG(CANBus, *bus)); connModel->refreshView(); } else //new connection @@ -169,14 +176,15 @@ void ConnectionWindow::handleOKButton() SerialWorker *serial = new SerialWorker(canModel, connModel->rowCount()); connect(serial, SIGNAL(busStatus(int,int,int)), this, SLOT(receiveBusStatus(int,int,int))); connect(serial, SIGNAL(connectionSuccess(CANConnection*)), this, SLOT(connectionSuccess(CANConnection*))); - connModel->addConnection(serial); + CANConnectionContainer* container = new CANConnectionContainer(serial); + qDebug() << "Setup initial connection object"; - CAN_Bus bus; + CANBus bus; bus.active = ui->ckEnabled->isChecked(); bus.busNum = serial->getBusBase(); - bus.connection = serial; + bus.container = container; bus.listenOnly = ui->ckListenOnly->isChecked(); bus.singleWire = ui->ckSingleWire->isChecked(); @@ -187,6 +195,7 @@ void ConnectionWindow::handleOKButton() bus.active = false; } else bus.speed = ui->cbSpeed->currentText().toInt(); + connModel->addBus(bus); int numBuses = serial->getNumBuses(); @@ -197,7 +206,7 @@ void ConnectionWindow::handleOKButton() bus.singleWire = false; bus.speed = 250000; bus.busNum = serial->getBusBase() + i; - bus.connection = serial; + bus.container = container; connModel->addBus(bus); qDebug() << "Added bus " << bus.busNum; } @@ -213,7 +222,42 @@ void ConnectionWindow::handleOKButton() } else if (ui->rbSocketCAN->isChecked()) { + /* TODO: sanity checks here */ + /* text, port, etc, ... */ + /* factorize code with other probes */ + /* create connection */ + SocketCanConnection* conn = new SocketCanConnection(canModel, 0); /* what is base? (second param) */ + + /* set port name */ + conn->updatePortName(ui->lePort->text()); + + /* add connection to a container (this creates the thread) */ + CANConnectionContainer* container = new CANConnectionContainer(conn); + + /* set bus configuration */ + CANBus bus; + bus.active = ui->ckEnabled->isChecked(); + bus.busNum = 0; // not used + bus.container = container; + 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(); + + /* add bus to model */ + connModel->addBus(bus); + + /* set bus settings (send message to thread) */ + QMetaObject::invokeMethod(conn, "updateBusSettings", + Qt::QueuedConnection, + Q_ARG(CANBus, bus)); } } } @@ -221,7 +265,7 @@ void ConnectionWindow::handleOKButton() void ConnectionWindow::receiveBusStatus(int bus, int speed, int status) { qDebug() << "bus " << bus << " speed " << speed << " status " << status; - CAN_Bus *busRef = connModel->getBus(bus); + CANBus *busRef = connModel->getBus(bus); if (status & 40) busRef->setSpeed(speed); if (status & 8) //update enabled status { @@ -261,10 +305,10 @@ void ConnectionWindow::handleConnSelectionChanged() ui->rbGVRET->setEnabled(false); ui->rbKvaser->setEnabled(false); ui->rbSocketCAN->setEnabled(false); - CAN_Bus *bus = connModel->getBus(selIdx); - if (bus->connection->getConnTypeName() == "GVRET") ui->rbGVRET->setChecked(true); - if (bus->connection->getConnTypeName() == "KVASER") ui->rbKvaser->setChecked(true); - if (bus->connection->getConnTypeName() == "SOCKETCAN") ui->rbSocketCAN->setChecked(true); + CANBus *bus = connModel->getBus(selIdx); + if (bus->getContainer()->getRef()->getConnTypeName() == "GVRET") ui->rbGVRET->setChecked(true); + if (bus->getContainer()->getRef()->getConnTypeName() == "KVASER") ui->rbKvaser->setChecked(true); + if (bus->getContainer()->getRef()->getConnTypeName() == "SOCKETCAN") ui->rbSocketCAN->setChecked(true); ui->ckListenOnly->setChecked(bus->isListenOnly()); ui->ckSingleWire->setChecked(bus->isSingleWire()); ui->ckEnabled->setChecked(bus->isActive()); @@ -273,8 +317,12 @@ void ConnectionWindow::handleConnSelectionChanged() } } +/*TODO: rename the 3 following functions */ void ConnectionWindow::getSerialPorts() { + /* set combobox page visible */ + ui->stPort->setCurrentWidget(ui->cbPage); + ui->cbPort->clear(); ports = QSerialPortInfo::availablePorts(); @@ -287,26 +335,17 @@ void ConnectionWindow::getSerialPorts() void ConnectionWindow::getKvaserPorts() { - +#ifdef Q_OS_WIN + /* set combobox page visible */ + ui->stPort->setCurrentWidget(ui->cbPage); +#endif } void ConnectionWindow::getSocketcanPorts() { #ifdef Q_OS_LINUX - QList interfaces = QNetworkInterface::allInterfaces(); - QString interfaceName; - - ui->cbPort->clear(); - - foreach (QNetworkInterface interface, interfaces) - { - interfaceName = interface.name().toLower(); - qDebug() << "Interface: " << interface.name(); - if (interfaceName.contains("can")) - { - ui->cbPort->addItem(interfaceName); - } - } + /* set edit text page visible */ + ui->stPort->setCurrentWidget(ui->etPage); #endif } @@ -378,7 +417,22 @@ bool ConnectionWindow::getSWMode() void ConnectionWindow::handleRemoveConn() { +#if 0 + int selIdx = ui->tableConnections->selectionModel()->currentIndex().row(); + 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; + + selIdx +#endif } void ConnectionWindow::handleRevert() @@ -386,21 +440,11 @@ void ConnectionWindow::handleRevert() } -void ConnectionWindow::handleEnableAll() -{ - -} - -void ConnectionWindow::handleDisableAll() -{ - -} - void ConnectionWindow::sendFrame(const CANFrame *frame) { - CAN_Bus *bus = connModel->getBus(frame->bus); + CANBus *bus = connModel->getBus(frame->bus); if (bus == NULL) return; - QMetaObject::invokeMethod(bus->connection, "sendFrame", + QMetaObject::invokeMethod(bus->getContainer()->getRef(), "sendFrame", Qt::QueuedConnection, Q_ARG(const CANFrame *, frame)); } @@ -408,9 +452,21 @@ void ConnectionWindow::sendFrame(const CANFrame *frame) void ConnectionWindow::sendFrameBatch(const QList *frames) { if (frames->count() == 0) return; - CAN_Bus *bus = connModel->getBus(frames->at(0).bus); + CANBus *bus = connModel->getBus(frames->at(0).bus); if (bus == NULL) return; - QMetaObject::invokeMethod(bus->connection, "sendFrameBatch", + QMetaObject::invokeMethod(bus->getContainer()->getRef(), "sendFrameBatch", Qt::QueuedConnection, Q_ARG(const QList *, frames)); } + +bool ConnectionWindow::isSocketCanAvailable() +{ +#ifdef Q_OS_LINUX + foreach (const QByteArray &backend, QCanBus::instance()->plugins()) { + if (backend == "socketcan") { + return true; + } + } +#endif + return false; +} diff --git a/connectionwindow.h b/connectionwindow.h index 6d91216..5b0d9c0 100644 --- a/connectionwindow.h +++ b/connectionwindow.h @@ -40,7 +40,7 @@ public: bool getSWMode(); signals: - void updateBusSettings(CAN_Bus *bus); + void updateBusSettings(CANBus *bus); void updatePortName(QString port); public slots: @@ -58,8 +58,6 @@ private slots: void handleDisableAll(); void handleRevert(); void handleNewConn(); - void handleEnableAll(); - void handleDisableAll(); void receiveBusStatus(int bus, int speed, int status); void connectionSuccess(CANConnection *conn); @@ -74,6 +72,7 @@ private: void getSerialPorts(); void getKvaserPorts(); void getSocketcanPorts(); + bool isSocketCanAvailable(); }; #endif // CONNECTIONWINDOW_H diff --git a/connectionwindow.ui b/connectionwindow.ui index f157991..f3a5b39 100644 --- a/connectionwindow.ui +++ b/connectionwindow.ui @@ -6,8 +6,8 @@ 0 0 - 1009 - 433 + 956 + 527 @@ -129,7 +129,69 @@ - + + + + 0 + 0 + + + + + 0 + 22 + + + + Qt::LeftToRight + + + 0 + + + + + 0 + 0 + + + + Qt::LeftToRight + + + + + + + 0 + 0 + + + + + 341 + 16777215 + + + + + + + + + + + + + 0 + 0 + + + + + + + diff --git a/mainwindow.cpp b/mainwindow.cpp index 07aa433..68e27c0 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -47,7 +47,8 @@ MainWindow::MainWindow(QWidget *parent) : this->setWindowTitle("Savvy CAN V" + QString::number(VERSION)); - model = new CANFrameModel(); + model = new CANFrameModel(this); // set parent to mainwindow to prevent canframemodel to change thread (might be done by setModel but just in case) + ui->canFramesView->setModel(model); readSettings(); @@ -510,7 +511,7 @@ void MainWindow::gotFrames(int framesSinceLastUpdate) void MainWindow::addFrameToDisplay(CANFrame &frame, bool autoRefresh = false) { - model->addFrame(frame, autoRefresh); + model->addFrame(frame, autoRefresh); if (autoRefresh) { if (ui->cbAutoScroll->isChecked()) ui->canFramesView->scrollToBottom();