diff --git a/connections/newconnectiondialog.cpp b/connections/newconnectiondialog.cpp index 85163e2..9121fe9 100644 --- a/connections/newconnectiondialog.cpp +++ b/connections/newconnectiondialog.cpp @@ -200,7 +200,7 @@ void NewConnectionDialog::selectRemote() void NewConnectionDialog::selectKayak() { - ui->lPort->setText("Available Bus(ses):"); + ui->lPort->setText("Available Bus(@can://:):"); ui->lblDeviceType->setHidden(true); ui->cbDeviceType->setHidden(true); diff --git a/connections/socketcand.cpp b/connections/socketcand.cpp index 6ef8975..4e73311 100644 --- a/connections/socketcand.cpp +++ b/connections/socketcand.cpp @@ -14,33 +14,59 @@ SocketCANd::SocketCANd(QString portName) : mTimer(this) /*NB: set this as parent of timer to manage it from working thread */ { - mTimer.setInterval(10000); //tick every 10 seconds + mTimer.setInterval(2000); //tick every 2 seconds mTimer.setSingleShot(false); //keep ticking connect(&mTimer, SIGNAL(timeout()), this, SLOT(checkConnection())); sendDebug("SocketCANd()"); - //tcpClient = nullptr; hostCanIDs = portName.left(portName.indexOf("@")).split(','); - QString hostIPandPort = portName.mid(portName.indexOf("can://")+6, portName.length() - portName.indexOf("can://") - 7); //7 is lenght of 'can://' and ')' + QString hostIPandPort = portName.mid(portName.indexOf("can://")+6, portName.length() - portName.indexOf("can://") - 6); //6 is lenght of 'can://' hostIP = QHostAddress(hostIPandPort.left(hostIPandPort.indexOf(":"))); hostPort = (hostIPandPort.right(hostIPandPort.length() - hostIPandPort.lastIndexOf(":") -1)).toInt(); - QVarLengthArray tcpClient(0); mNumBuses = hostCanIDs.length(); mBusData.resize(mNumBuses); + + // initial nullptr clients + tcpClient = QVarLengthArray(mNumBuses, nullptr); + reconnecting = false; + setStatus(CANCon::NOT_CONNECTED); + CANConStatus stats; + stats.conStatus = getStatus(); + stats.numHardwareBuses = mNumBuses; + emit status(stats); for (int i = 0; i < mNumBuses; i++) { rx_state.append(IDLE); unprocessedData.append(""); - } + CANBus bus_info; + bus_info.setActive(true); + bus_info.setListenOnly(false); + bus_info.setSpeed(500000); + setBusConfig(i, bus_info); + } } SocketCANd::~SocketCANd() { stop(); + for (int i = 0; i < tcpClient.length(); i++) + { + if (tcpClient[i]) + { + if (tcpClient[i]->isOpen()) + { + tcpClient[i]->close(); + } + tcpClient[i]->disconnect(); + delete tcpClient[i]; + tcpClient[i] = nullptr; + } + } + tcpClient.clear(); sendDebug("~SocketCANd()"); } @@ -88,6 +114,8 @@ void SocketCANd::sendStringToTCP(const char* data, int busNum) void SocketCANd::piStarted() { connectDevice(); + checkConnection(); + mTimer.start(); } @@ -144,7 +172,8 @@ bool SocketCANd::piSendFrame(const CANFrame& frame) framesRapid++; if (tcpClient[busNum] && !tcpClient[busNum]->isOpen()) return false; - //if (!isConnected) return false; + // rx not ready + if(rx_state[busNum] != RAWMODE) return false; // Doesn't make sense to send an error frame // to an adapter @@ -173,18 +202,13 @@ bool SocketCANd::piSendFrame(const CANFrame& frame) void SocketCANd::connectDevice() { - QSettings settings; - - /* disconnect device */ - /* avoiding this solution since it removes connection selection in case of connection check */ - //if(tcpClient) - // disconnectDevice(); - - // only resetting tcpClient for (int i = 0; i < tcpClient.length(); i++) { if (tcpClient[i]) { + if(tcpClient[i]->state() == QAbstractSocket::ConnectedState || tcpClient[i]->state() == QAbstractSocket::ConnectingState) + continue; + if (tcpClient[i]->isOpen()) { tcpClient[i]->close(); @@ -193,27 +217,12 @@ void SocketCANd::connectDevice() delete tcpClient[i]; tcpClient[i] = nullptr; } - } - tcpClient.clear(); - - mTimer.start(); - - //sendDebug("TCP Connection to a Kayak device"); - for (int i = 0; i < mNumBuses; i++) - { rx_state[i] = IDLE; - tcpClient.append(new QTcpSocket()); + tcpClient[i] = new QTcpSocket(); tcpClient[i]->connectToHost(hostIP, hostPort); - //connect(tcpClient[i], SIGNAL(readyRead()), this, SLOT(readTCPData())); connect(tcpClient[i], SIGNAL(readyRead()), this, SLOT(invokeReadTCPData())); - sendDebug("Created TCP Socket to Kayak device " + hostCanIDs.at(i)); + sendDebug("Created TCP Socket to Kayak device " + hostCanIDs.at(i) + ", host: " + hostIP.toString() + ":" + QString::number(hostPort)); } - setStatus(CANCon::CONNECTED); - CANConStatus stats; - stats.conStatus = getStatus(); - stats.numHardwareBuses = mNumBuses; - //signalSender = qobject_cast(QObject::sender()); - if (!reconnecting) emit status(stats); } void SocketCANd::deviceConnected(int busNum) @@ -227,18 +236,27 @@ void SocketCANd::deviceConnected(int busNum) void SocketCANd::checkConnection() { - int bufSize = 0; + bool connected = tcpClient.length() > 0; for (int i = 0; i < tcpClient.length(); i++) { - bufSize += tcpClient[i]->readBufferSize(); - //sendDebug("Buffer size: " + QString::number(bufSize)); + connected &= tcpClient[i] && tcpClient[i]->state() == QAbstractSocket::ConnectedState; } - if (bufSize == 0) + if (!connected) { reconnecting = true; + setStatus(CANCon::NOT_CONNECTED); connectDevice(); - sendDebug("Reconnecting to TCP Host " + hostIP.toString()); + sendDebug("Reconnecting to TCP Host " + hostIP.toString() + ":" + QString::number(hostPort)); + } else { + setStatus(CANCon::CONNECTED); + if (reconnecting) { + CANConStatus stats; + stats.conStatus = getStatus(); + stats.numHardwareBuses = mNumBuses; + emit status(stats); + } + reconnecting = false; } } @@ -349,7 +367,6 @@ void SocketCANd::disconnectDevice() { tcpClient[i] = nullptr; } } - tcpClient.clear(); setStatus(CANCon::NOT_CONNECTED); CANConStatus stats; @@ -378,11 +395,6 @@ void SocketCANd::readTCPData(int busNum) void SocketCANd::procRXData(QString data, int busNum) { - if (data != "") - { - mTimer.stop(); - mTimer.start(); - } switch (rx_state.at(busNum)) { case IDLE: