diff --git a/qt/FZ3TCPClient/QTCPClient.pro.user b/qt/FZ3TCPClient/QTCPClient.pro.user index 939f937768f0bac0d86f4d070254c08c71e96559..5b61e63014ac8b6d494a5086189205d789b27d95 100644 --- a/qt/FZ3TCPClient/QTCPClient.pro.user +++ b/qt/FZ3TCPClient/QTCPClient.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -342,9 +342,314 @@ 1 + + ProjectExplorer.Project.Target.1 + + FZ3 + FZ3 + {ef77da34-607a-4188-93e6-fe38b806747e} + 0 + 0 + 0 + + /home/hossein/projects/fz3/qt/build-QTCPClient-FZ3-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + /home/hossein/projects/fz3/qt/build-QTCPClient-FZ3-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + true + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + /home/hossein/projects/fz3/qt/build-QTCPClient-FZ3-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + true + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + + true + Check for free disk space + + RemoteLinux.CheckForFreeDiskSpaceStep + + + + + / + 5242880 + + + + + true + Kill current application instance + + RemoteLinux.KillAppStep + + + + + + + + + true + Upload files via SFTP + + RemoteLinux.DirectUploadStep + + + + + false + true + + + + 3 + Deploy + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy to Remote Linux Host + + DeployToGenericLinux + + 1 + + + dwarf + + cpu-cycles + + + 250 + -F + true + 4096 + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + kcachegrind + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 1 + + + QTCPClient (on FZ3) + RemoteLinuxRunConfiguration:/home/hossein/projects/fz3/qt/FZ3TCPClient/QTCPClient.pro + 1 + + false + + 3768 + false + true + false + false + true + false + + + :0 + + 1 + + ProjectExplorer.Project.TargetCount - 1 + 2 ProjectExplorer.Project.Updater.FileVersion diff --git a/qt/FZ3TCPClient/mainwindow.cpp b/qt/FZ3TCPClient/mainwindow.cpp index 54512346d9d02de43387afb1d9dd566617930538..815c571d18a88bbf628ef5dbed22566e74e86759 100644 --- a/qt/FZ3TCPClient/mainwindow.cpp +++ b/qt/FZ3TCPClient/mainwindow.cpp @@ -1,178 +1,89 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::MainWindow) +MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), + ui(new Ui::MainWindow) { - ui->setupUi(this); - socket = new QTcpSocket(this); - - connect(this, &MainWindow::newMessage, this, &MainWindow::displayMessage); - connect(socket, &QTcpSocket::readyRead, this, &MainWindow::readSocket); - connect(socket, &QTcpSocket::disconnected, this, &MainWindow::discardSocket); - - socket->connectToHost(QHostAddress::LocalHost,8080); - - if(socket->waitForConnected()) - ui->statusBar->showMessage("Connected to Server"); - else{ - QMessageBox::critical(this,"QTCPClient", QString("The following error occurred: %1.").arg(socket->errorString())); - exit(EXIT_FAILURE); - } + ui->setupUi(this); + socket = new QTcpSocket(this); + + connect(this, &MainWindow::newMessage, this, &MainWindow::displayMessage); + connect(socket, &QTcpSocket::readyRead, this, &MainWindow::readSocket); + connect(socket, &QTcpSocket::disconnected, this, &MainWindow::discardSocket); + + socket->connectToHost(QHostAddress::LocalHost, 1992); + + if (socket->waitForConnected()) + ui->statusBar->showMessage("Connected to Server"); + else + { + QMessageBox::critical(this, "QTCPClient", QString("The following error occurred: %1.").arg(socket->errorString())); + exit(EXIT_FAILURE); + } } MainWindow::~MainWindow() { - if(socket->isOpen()) - socket->close(); - delete ui; + if (socket->isOpen()) + socket->close(); + delete ui; } void MainWindow::readSocket() { - QByteArray buffer; - - QDataStream socketStream(socket); - socketStream.setVersion(QDataStream::Qt_5_13); - - socketStream.startTransaction(); - socketStream >> buffer; - - if(!socketStream.commitTransaction()) - { - QString message = QString("%1 :: Waiting for more data to come..").arg(socket->socketDescriptor()); - emit newMessage(message); - return; - } - - QString header = buffer.mid(0,128); - QString fileType = header.split(",")[0].split(":")[1]; - - buffer = buffer.mid(128); - - if(fileType=="attachment"){ - QString fileName = header.split(",")[1].split(":")[1]; - QString ext = fileName.split(".")[1]; - QString size = header.split(",")[2].split(":")[1].split(";")[0]; - - if (QMessageBox::Yes == QMessageBox::question(this, "QTCPServer", QString("You are receiving an attachment from sd:%1 of size: %2 bytes, called %3. Do you want to accept it?").arg(socket->socketDescriptor()).arg(size).arg(fileName))) - { - QString filePath = QFileDialog::getSaveFileName(this, tr("Save File"), QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)+"/"+fileName, QString("File (*.%1)").arg(ext)); - - QFile file(filePath); - if(file.open(QIODevice::WriteOnly)){ - file.write(buffer); - QString message = QString("INFO :: Attachment from sd:%1 successfully stored on disk under the path %2").arg(socket->socketDescriptor()).arg(QString(filePath)); - emit newMessage(message); - }else - QMessageBox::critical(this,"QTCPServer", "An error occurred while trying to write the attachment."); - }else{ - QString message = QString("INFO :: Attachment from sd:%1 discarded").arg(socket->socketDescriptor()); - emit newMessage(message); - } - }else if(fileType=="message"){ - QString message = QString("%1 :: %2").arg(socket->socketDescriptor()).arg(QString::fromStdString(buffer.toStdString())); - emit newMessage(message); - } + QByteArray buffer; + QByteArray fullBuffer; + bool footerPacketRecieved = 0; + while (!footerPacketRecieved) + { + while (socket->bytesAvailable() > 0) + { + buffer.append(socket->readAll()); + fullBuffer = fullBuffer + buffer; + } + if (buffer.right(16) == "A5A5A5A5A5A5A5A5") + { + footerPacketRecieved = 1; + } + } + QString file_time = QTime::currentTime().toString("hh:mm:ss"); + QString filePath = "/tmp/fz3_data/sensor_data_" + file_time + ".bin"; + QFile file(filePath); + if (file.open(QIODevice::WriteOnly)) + { + file.write(fullBuffer); + QString message = QString("INFO :: Attachment from sd:%1 successfully stored on disk under the path %2").arg(socket->socketDescriptor()).arg(QString(filePath)); + emit newMessage(message); + } } void MainWindow::discardSocket() { - socket->deleteLater(); - socket=nullptr; + socket->deleteLater(); + socket = nullptr; - ui->statusBar->showMessage("Disconnected!"); + ui->statusBar->showMessage("Disconnected!"); } void MainWindow::displayError(QAbstractSocket::SocketError socketError) { - switch (socketError) { - case QAbstractSocket::RemoteHostClosedError: - break; - case QAbstractSocket::HostNotFoundError: - QMessageBox::information(this, "QTCPClient", "The host was not found. Please check the host name and port settings."); - break; - case QAbstractSocket::ConnectionRefusedError: - QMessageBox::information(this, "QTCPClient", "The connection was refused by the peer. Make sure QTCPServer is running, and check that the host name and port settings are correct."); - break; - default: - QMessageBox::information(this, "QTCPClient", QString("The following error occurred: %1.").arg(socket->errorString())); - break; - } -} - -void MainWindow::on_pushButton_sendMessage_clicked() -{ - if(socket) - { - if(socket->isOpen()) - { - QString str = ui->lineEdit_message->text(); - - QDataStream socketStream(socket); - socketStream.setVersion(QDataStream::Qt_5_13); - - QByteArray header; - header.prepend(QString("fileType:message,fileName:null,fileSize:%1;").arg(str.size()).toUtf8()); - header.resize(128); - - QByteArray byteArray = str.toUtf8(); - byteArray.prepend(header); - - socketStream << byteArray; - - ui->lineEdit_message->clear(); - } - else - QMessageBox::critical(this,"QTCPClient","Socket doesn't seem to be opened"); - } - else - QMessageBox::critical(this,"QTCPClient","Not connected"); -} - -void MainWindow::on_pushButton_sendAttachment_clicked() -{ - if(socket) - { - if(socket->isOpen()) - { - QString filePath = QFileDialog::getOpenFileName(this, ("Select an attachment"), QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), ("File (*.json *.txt *.png *.jpg *.jpeg)")); - - if(filePath.isEmpty()){ - QMessageBox::critical(this,"QTCPClient","You haven't selected any attachment!"); - return; - } - - QFile m_file(filePath); - if(m_file.open(QIODevice::ReadOnly)){ - - QFileInfo fileInfo(m_file.fileName()); - QString fileName(fileInfo.fileName()); - - QDataStream socketStream(socket); - socketStream.setVersion(QDataStream::Qt_5_13); - - QByteArray header; - header.prepend(QString("fileType:attachment,fileName:%1,fileSize:%2;").arg(fileName).arg(m_file.size()).toUtf8()); - header.resize(128); - - QByteArray byteArray = m_file.readAll(); - byteArray.prepend(header); - - socketStream.setVersion(QDataStream::Qt_5_13); - socketStream << byteArray; - }else - QMessageBox::critical(this,"QTCPClient","Attachment is not readable!"); - } - else - QMessageBox::critical(this,"QTCPClient","Socket doesn't seem to be opened"); - } - else - QMessageBox::critical(this,"QTCPClient","Not connected"); + switch (socketError) + { + case QAbstractSocket::RemoteHostClosedError: + break; + case QAbstractSocket::HostNotFoundError: + QMessageBox::information(this, "QTCPClient", "The host was not found. Please check the host name and port settings."); + break; + case QAbstractSocket::ConnectionRefusedError: + QMessageBox::information(this, "QTCPClient", "The connection was refused by the peer. Make sure QTCPServer is running, and check that the host name and port settings are correct."); + break; + default: + QMessageBox::information(this, "QTCPClient", QString("The following error occurred: %1.").arg(socket->errorString())); + break; + } } -void MainWindow::displayMessage(const QString& str) +void MainWindow::displayMessage(const QString &str) { - ui->textBrowser_receivedMessages->append(str); + ui->textBrowser_receivedMessages->append(str); } diff --git a/qt/FZ3TCPClient/mainwindow.h b/qt/FZ3TCPClient/mainwindow.h index 2feee9562cf3e1bfe63a787e023ef2cf7ed575d6..98d479adfb888b4947f5e0728a2b964cb81bc1ad 100644 --- a/qt/FZ3TCPClient/mainwindow.h +++ b/qt/FZ3TCPClient/mainwindow.h @@ -12,32 +12,34 @@ #include #include #include +#include +#include -namespace Ui { -class MainWindow; +namespace Ui +{ + class MainWindow; } class MainWindow : public QMainWindow { - Q_OBJECT + Q_OBJECT public: - explicit MainWindow(QWidget *parent = nullptr); - ~MainWindow(); + explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow(); signals: - void newMessage(QString); + void newMessage(QString); private slots: - void readSocket(); - void discardSocket(); - void displayError(QAbstractSocket::SocketError socketError); + void readSocket(); + void discardSocket(); + void displayError(QAbstractSocket::SocketError socketError); + + void displayMessage(const QString &str); - void displayMessage(const QString& str); - void on_pushButton_sendMessage_clicked(); - void on_pushButton_sendAttachment_clicked(); private: - Ui::MainWindow *ui; + Ui::MainWindow *ui; - QTcpSocket* socket; + QTcpSocket *socket; }; #endif // MAINWINDOW_H diff --git a/qt/FZ3TCPClient/mainwindow.ui b/qt/FZ3TCPClient/mainwindow.ui index 57a5f9329a6face08149d28c85fc076eeb424134..126286d9a95877838498656e82c285d2498ae272 100644 --- a/qt/FZ3TCPClient/mainwindow.ui +++ b/qt/FZ3TCPClient/mainwindow.ui @@ -6,8 +6,8 @@ 0 0 - 369 - 305 + 373 + 368 @@ -18,49 +18,22 @@ - + - - - - - - - - - Send Message - - - - - - - - - - Send Attachment - - - - - - - - 0 - 0 - - - - - - - - + + + + + + + log console: + + @@ -69,8 +42,8 @@ 0 0 - 369 - 21 + 373 + 22 diff --git a/qt/FZ3TCPServer/QTCPServer.pro.user b/qt/FZ3TCPServer/QTCPServer.pro.user index 8467d3140729855dccfdde3d462d9a44dcd3951a..ec103c6a1247c8cd6e97b86957f616b9efa845bd 100644 --- a/qt/FZ3TCPServer/QTCPServer.pro.user +++ b/qt/FZ3TCPServer/QTCPServer.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -8,7 +8,7 @@ ProjectExplorer.Project.ActiveTarget - 0 + 1 ProjectExplorer.Project.EditorSettings @@ -386,9 +386,270 @@ 1 + + ProjectExplorer.Project.Target.1 + + Desktop Qt 5.13.2 GCC 64bit + Desktop Qt 5.13.2 GCC 64bit + qt.qt5.5132.gcc_64_kit + 0 + 0 + 0 + + /home/hossein/projects/fz3/qt/build-QTCPServer-Desktop_Qt_5_13_2_GCC_64bit-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + /home/hossein/projects/fz3/qt/build-QTCPServer-Desktop_Qt_5_13_2_GCC_64bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + true + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + /home/hossein/projects/fz3/qt/build-QTCPServer-Desktop_Qt_5_13_2_GCC_64bit-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + true + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + 0 + Deploy + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy Configuration + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + dwarf + + cpu-cycles + + + 250 + -F + true + 4096 + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + kcachegrind + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + QTCPServer + + Qt4ProjectManager.Qt4RunConfiguration:/home/hossein/projects/fz3/qt/FZ3TCPServer/QTCPServer.pro + + 3768 + false + true + true + false + false + true + + /home/hossein/projects/fz3/qt/build-QTCPServer-Desktop_Qt_5_13_2_GCC_64bit-Debug + + 1 + + ProjectExplorer.Project.TargetCount - 1 + 2 ProjectExplorer.Project.Updater.FileVersion diff --git a/qt/FZ3TCPServer/mainwindow.cpp b/qt/FZ3TCPServer/mainwindow.cpp index 85682e81d68cd8ef2dcbd8de6d53e96bb3318a8c..6ab717a515e6b50f685f4110e06b942624899f38 100644 --- a/qt/FZ3TCPServer/mainwindow.cpp +++ b/qt/FZ3TCPServer/mainwindow.cpp @@ -3,380 +3,294 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { - ui->setupUi(this); - ui->pushButton_init_dma->setEnabled(false); - m_server = new QTcpServer(); - - if(m_server->listen(QHostAddress::Any, 8080)) - { - connect(this, &MainWindow::newMessage, this, &MainWindow::displayMessage); - connect(m_server, &QTcpServer::newConnection, this, &MainWindow::newConnection); - ui->statusBar->showMessage("Server is listening..."); - } - else - { - QMessageBox::critical(this,"QTCPServer",QString("Unable to start the server: %1.").arg(m_server->errorString())); - exit(EXIT_FAILURE); - } + ui->setupUi(this); + ui->pushButton_init_dma->setEnabled(false); + m_server = new QTcpServer(); + + if (m_server->listen(QHostAddress::Any, 1992)) + { + connect(this, &MainWindow::newMessage, this, &MainWindow::displayMessage); + connect(m_server, &QTcpServer::newConnection, this, &MainWindow::newConnection); + ui->statusBar->showMessage("Server is listening..."); + } + else + { + QMessageBox::critical(this, "QTCPServer", QString("Unable to start the server: %1.").arg(m_server->errorString())); + exit(EXIT_FAILURE); + } } MainWindow::~MainWindow() { - foreach (QTcpSocket* socket, connection_set) - { - socket->close(); - socket->deleteLater(); - } + foreach (QTcpSocket *socket, connection_set) + { + socket->close(); + socket->deleteLater(); + } - m_server->close(); - m_server->deleteLater(); + m_server->close(); + m_server->deleteLater(); - delete ui; + delete ui; } void MainWindow::newConnection() { - while (m_server->hasPendingConnections()) - appendToSocketList(m_server->nextPendingConnection()); + while (m_server->hasPendingConnections()) + appendToSocketList(m_server->nextPendingConnection()); } -void MainWindow::appendToSocketList(QTcpSocket* socket) +void MainWindow::appendToSocketList(QTcpSocket *socket) { - connection_set.insert(socket); - connect(socket, &QTcpSocket::readyRead, this, &MainWindow::readSocket); - connect(socket, &QTcpSocket::disconnected, this, &MainWindow::discardSocket); - connect(socket, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError); - ui->comboBox_receiver->addItem(QString::number(socket->socketDescriptor())); - displayMessage(QString("INFO :: Client with sockd:%1 has just entered the room").arg(socket->socketDescriptor())); - ui->pushButton_init_dma->setEnabled(true); -} - -void MainWindow::readSocket() -{ - QTcpSocket* socket = reinterpret_cast(sender()); - - QByteArray buffer; - - QDataStream socketStream(socket); - socketStream.setVersion(QDataStream::Qt_5_13); - - socketStream.startTransaction(); - socketStream >> buffer; - - if(!socketStream.commitTransaction()) - { - QString message = QString("%1 :: Waiting for more data to come..").arg(socket->socketDescriptor()); - emit newMessage(message); - return; - } - - QString header = buffer.mid(0,128); - QString fileType = header.split(",")[0].split(":")[1]; - - buffer = buffer.mid(128); - - if(fileType=="attachment"){ - QString fileName = header.split(",")[1].split(":")[1]; - QString ext = fileName.split(".")[1]; - QString size = header.split(",")[2].split(":")[1].split(";")[0]; - - if (QMessageBox::Yes == QMessageBox::question(this, "QTCPServer", QString("You are receiving an attachment from sd:%1 of size: %2 bytes, called %3. Do you want to accept it?").arg(socket->socketDescriptor()).arg(size).arg(fileName))) - { - QString filePath = QFileDialog::getSaveFileName(this, tr("Save File"), QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)+"/"+fileName, QString("File (*.%1)").arg(ext)); - - QFile file(filePath); - if(file.open(QIODevice::WriteOnly)){ - file.write(buffer); - QString message = QString("INFO :: Attachment from sd:%1 successfully stored on disk under the path %2").arg(socket->socketDescriptor()).arg(QString(filePath)); - emit newMessage(message); - }else - QMessageBox::critical(this,"QTCPServer", "An error occurred while trying to write the attachment."); - }else{ - QString message = QString("INFO :: Attachment from sd:%1 discarded").arg(socket->socketDescriptor()); - emit newMessage(message); - } - }else if(fileType=="message"){ - QString message = QString("%1 :: %2").arg(socket->socketDescriptor()).arg(QString::fromStdString(buffer.toStdString())); - emit newMessage(message); - } + connection_set.insert(socket); + connect(socket, &QTcpSocket::disconnected, this, &MainWindow::discardSocket); + ui->comboBox_receiver->addItem(QString::number(socket->socketDescriptor())); + displayMessage(QString("INFO :: Client with sockd:%1 has just entered the room").arg(socket->socketDescriptor())); + ui->pushButton_init_dma->setEnabled(true); } void MainWindow::discardSocket() { - QTcpSocket* socket = reinterpret_cast(sender()); - QSet::iterator it = connection_set.find(socket); - if (it != connection_set.end()){ - displayMessage(QString("INFO :: A client has just left the room").arg(socket->socketDescriptor())); - connection_set.remove(*it); - } - refreshComboBox(); - - socket->deleteLater(); + QTcpSocket *socket = reinterpret_cast(sender()); + QSet::iterator it = connection_set.find(socket); + if (it != connection_set.end()) + { + displayMessage(QString("INFO :: A client has just left the room").arg(socket->socketDescriptor())); + connection_set.remove(*it); + } + refreshComboBox(); + + socket->deleteLater(); } void MainWindow::displayError(QAbstractSocket::SocketError socketError) { - switch (socketError) { - case QAbstractSocket::RemoteHostClosedError: - break; - case QAbstractSocket::HostNotFoundError: - QMessageBox::information(this, "QTCPServer", "The host was not found. Please check the host name and port settings."); - break; - case QAbstractSocket::ConnectionRefusedError: - QMessageBox::information(this, "QTCPServer", "The connection was refused by the peer. Make sure QTCPServer is running, and check that the host name and port settings are correct."); - break; - default: - QTcpSocket* socket = qobject_cast(sender()); - QMessageBox::information(this, "QTCPServer", QString("The following error occurred: %1.").arg(socket->errorString())); - break; - } + switch (socketError) + { + case QAbstractSocket::RemoteHostClosedError: + break; + case QAbstractSocket::HostNotFoundError: + QMessageBox::information(this, "QTCPServer", "The host was not found. Please check the host name and port settings."); + break; + case QAbstractSocket::ConnectionRefusedError: + QMessageBox::information(this, "QTCPServer", "The connection was refused by the peer. Make sure QTCPServer is running, and check that the host name and port settings are correct."); + break; + default: + QTcpSocket *socket = qobject_cast(sender()); + QMessageBox::information(this, "QTCPServer", QString("The following error occurred: %1.").arg(socket->errorString())); + break; + } } -void MainWindow::on_pushButton_sendMessage_clicked() +void MainWindow::displayMessage(const QString &str) { - QString receiver = ui->comboBox_receiver->currentText(); - - foreach (QTcpSocket* socket,connection_set) - { - if(socket->socketDescriptor() == receiver.toLongLong()) - { - sendMessage(socket); - break; - } - } - ui->lineEdit_message->clear(); + ui->textBrowser_receivedMessages->append(str); } - -void MainWindow::on_pushButton_sendAttachment_clicked() +void MainWindow::refreshComboBox() { - QString receiver = ui->comboBox_receiver->currentText(); - - QString filePath = QFileDialog::getOpenFileName(this, ("Select an attachment"), QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), ("File (*.json *.txt *.png *.jpg *.jpeg)")); - - if(filePath.isEmpty()){ - QMessageBox::critical(this,"QTCPClient","You haven't selected any attachment!"); - return; - } - - foreach (QTcpSocket* socket,connection_set) - { - if(socket->socketDescriptor() == receiver.toLongLong()) - { - sendAttachment(socket, filePath); - break; - } - } - ui->lineEdit_message->clear(); + ui->comboBox_receiver->clear(); + foreach (QTcpSocket *socket, connection_set) + ui->comboBox_receiver->addItem(QString::number(socket->socketDescriptor())); } -void MainWindow::sendMessage(QTcpSocket* socket) +void MainWindow::init_dma() { - if(socket) - { - if(socket->isOpen()) - { - QString str = ui->lineEdit_message->text(); - - QDataStream socketStream(socket); - socketStream.setVersion(QDataStream::Qt_5_13); - - QByteArray header; - header.prepend(QString("fileType:message,fileName:null,fileSize:%1;").arg(str.size()).toUtf8()); - header.resize(128); - - QByteArray byteArray = str.toUtf8(); - byteArray.prepend(header); - - socketStream.setVersion(QDataStream::Qt_5_13); - socketStream << byteArray; - } - else - QMessageBox::critical(this,"QTCPServer","Socket doesn't seem to be opened"); - } - else - QMessageBox::critical(this,"QTCPServer","Not connected"); + int tx_size_init = 10; + int rx_size_init = 10; + tx_channel = 0; + rx_channel = 1; + tx_size = MIB_TO_BYTE(tx_size_init); + rx_size = MIB_TO_BYTE(rx_size_init); + + LastLogQstring = "AXI DMA Parameters:"; + ui->textBrowser_receivedMessages->append(LastLogQstring); + std::cout << LastLogQstring.toStdString() << std::endl; + LastLogQstring = "Transmit Buffer Size:" + QString::number(BYTE_TO_MIB(tx_size)) + " MiB"; + ui->textBrowser_receivedMessages->append(LastLogQstring); + std::cout << LastLogQstring.toStdString() << std::endl; + ; + LastLogQstring = "Receive Buffer Size:" + QString::number(BYTE_TO_MIB(rx_size)) + " MiB"; + ui->textBrowser_receivedMessages->append(LastLogQstring); + std::cout << LastLogQstring.toStdString() << std::endl; + // setup axis switch + XDma_lb_axis_switch loop_back_sw; + const char *loop_back_sw_name = "dma_lb_axis_switch"; + int sw_init_status = 0; + sw_init_status = XDma_lb_axis_switch_Initialize(&loop_back_sw, loop_back_sw_name); + if (sw_init_status != 0) + { + LastLogQstring = "AXI DMA sw initialize failed with code:" + QString::number(BYTE_TO_MIB(sw_init_status)); + ui->textBrowser_receivedMessages->append(LastLogQstring); + std::cout << LastLogQstring.toStdString() << std::endl; + } + else + { + LastLogQstring = "AXI DMA sw initialized"; + ui->textBrowser_receivedMessages->append(LastLogQstring); + std::cout << LastLogQstring.toStdString() << std::endl; + } + XDma_lb_axis_switch_Set_dma_loopback_en(&loop_back_sw, 1); + LastLogQstring = "AXI DMA sw loopback enabled"; + ui->textBrowser_receivedMessages->append(LastLogQstring); + std::cout << LastLogQstring.toStdString() << std::endl; + + // Initialize the AXI DMA device + axidma_dev = axidma_init(); + if (axidma_dev == NULL) + { + LastLogQstring = "Failed to initialize the AXI DMA device."; + ui->textBrowser_receivedMessages->append(LastLogQstring); + std::cout << LastLogQstring.toStdString() << std::endl; + rc = 1; + } + + // Map memory regions for the transmit and receive buffers + tx_buf = static_cast(axidma_malloc(axidma_dev, tx_size)); + if (tx_buf == NULL) + { + LastLogQstring = "Unable to allocate transmit buffer from the AXI DMA device."; + ui->textBrowser_receivedMessages->append(LastLogQstring); + std::cout << LastLogQstring.toStdString() << std::endl; + rc = -1; + axidma_destroy(axidma_dev); + } + rx_buf = static_cast(axidma_malloc(axidma_dev, rx_size)); + if (rx_buf == NULL) + { + LastLogQstring = "Unable to allocate receive buffer from the AXI DMA device"; + ui->textBrowser_receivedMessages->append(LastLogQstring); + std::cout << LastLogQstring.toStdString() << std::endl; + rc = -1; + axidma_free(axidma_dev, tx_buf, tx_size); + axidma_destroy(axidma_dev); + } + + // Get all the transmit and receive channels + tx_chans = axidma_get_dma_tx(axidma_dev); + rx_chans = axidma_get_dma_rx(axidma_dev); + + if (tx_chans->len < 1) + { + LastLogQstring = "Error: No transmit channels were found."; + ui->textBrowser_receivedMessages->append(LastLogQstring); + std::cout << LastLogQstring.toStdString() << std::endl; + rc = -ENODEV; + axidma_free(axidma_dev, rx_buf, rx_size); + axidma_free(axidma_dev, tx_buf, tx_size); + axidma_destroy(axidma_dev); + } + if (rx_chans->len < 1) + { + LastLogQstring = "Error: No receive channels were found."; + ui->textBrowser_receivedMessages->append(LastLogQstring); + std::cout << LastLogQstring.toStdString() << std::endl; + rc = -ENODEV; + axidma_free(axidma_dev, rx_buf, rx_size); + axidma_free(axidma_dev, tx_buf, tx_size); + axidma_destroy(axidma_dev); + } + + /* If the user didn't specify the channels, we assume that the transmit and + * receive channels are the lowest numbered ones. */ + if (tx_channel == -1 && rx_channel == -1) + { + tx_channel = tx_chans->data[0]; + rx_channel = rx_chans->data[0]; + } + LastLogQstring = "Using transmit channel " + QString::number(tx_channel) + " and receive channel " + QString::number(rx_channel); + ui->textBrowser_receivedMessages->append(LastLogQstring); + std::cout << LastLogQstring.toStdString() << std::endl; + + XDma_lb_axis_switch_Set_dma_loopback_en(&loop_back_sw, 0); + XDma_lb_axis_switch_Release(&loop_back_sw); + LastLogQstring = "AXI DMA sw loopback disabled"; + ui->textBrowser_receivedMessages->append(LastLogQstring); + std::cout << LastLogQstring.toStdString() << std::endl; + ui->pushButton_init_dma->setEnabled(false); + LastLogQstring = "DMA test DONE"; + ui->textBrowser_receivedMessages->append(LastLogQstring); + std::cout << LastLogQstring.toStdString() << std::endl; + + QString receiver = "DMA initialize DONE"; } -void MainWindow::sendAttachment(QTcpSocket* socket, QString filePath) +void MainWindow::on_pushButton_init_dma_clicked() { - if(socket) - { - if(socket->isOpen()) - { - QFile m_file(filePath); - if(m_file.open(QIODevice::ReadOnly)){ - - QFileInfo fileInfo(m_file.fileName()); - QString fileName(fileInfo.fileName()); - - QDataStream socketStream(socket); - socketStream.setVersion(QDataStream::Qt_5_13); - - QByteArray header; - header.prepend(QString("fileType:attachment,fileName:%1,fileSize:%2;").arg(fileName).arg(m_file.size()).toUtf8()); - header.resize(128); - - QByteArray byteArray = m_file.readAll(); - byteArray.prepend(header); - - socketStream << byteArray; - }else - QMessageBox::critical(this,"QTCPClient","Couldn't open the attachment!"); - } - else - QMessageBox::critical(this,"QTCPServer","Socket doesn't seem to be opened"); - } - else - QMessageBox::critical(this,"QTCPServer","Not connected"); + init_dma(); } -void MainWindow::displayMessage(const QString& str) +void MainWindow::on_pushButton_sendData_clicked() { - ui->textBrowser_receivedMessages->append(str); -} -void MainWindow::refreshComboBox(){ - ui->comboBox_receiver->clear(); - foreach(QTcpSocket* socket, connection_set) - ui->comboBox_receiver->addItem(QString::number(socket->socketDescriptor())); + QString receiver = ui->comboBox_receiver->currentText(); + + QByteArray fileData; + for (int i = 0; i < 10000; i++) + { + fileData.prepend(QByteArray::fromHex("41")); + } + for (int i = 0; i < 10000; i++) + { + fileData.append(QByteArray::fromHex("42")); + } + for (int i = 0; i < 10000; i++) + { + fileData.append(QByteArray::fromHex("43")); + } + + foreach (QTcpSocket *socket, connection_set) + { + if (socket->socketDescriptor() == receiver.toLongLong()) + { + sendDataToClient(socket, &fileData, fileData.size()); + break; + } + } } -void MainWindow::on_Init_dma_clicked() +void MainWindow::sendDataToClient(QTcpSocket *socket, QByteArray *fileDataPtr, unsigned long long fileDataSize) { - init_dma(); + if (socket) + { + if (socket->isOpen()) + { + int64_t bytes; + QByteArray data = *fileDataPtr; + QString tmpHeader = "fileType:fz3_data: "; + QByteArray header = tmpHeader.toLocal8Bit(); + bytes = socket->write(header); + while (socket->waitForBytesWritten()) + { + usleep(10); + } + + int packetSize = 100000; + unsigned long long numberOfPacket = fileDataSize / packetSize; + unsigned long long currentPacket = 0; + + while (currentPacket < numberOfPacket) + { + bytes = socket->write(data.left(packetSize)); + while (socket->waitForBytesWritten()) + { + usleep(10); + } + data.remove(0, packetSize); + currentPacket++; + } + + QString tmpFooter = "A5A5A5A5A5A5A5A5"; + QByteArray footer = tmpFooter.toLocal8Bit(); + bytes = socket->write(footer); + while (socket->waitForBytesWritten()) + { + usleep(10); + } + } + else + QMessageBox::critical(this, "QTCPServer", "Socket doesn't seem to be opened"); + } + else + QMessageBox::critical(this, "QTCPServer", "Not connected"); } - -void MainWindow::init_dma() -{ - int tx_size_init = 10; - int rx_size_init = 10; - tx_channel = 0; - rx_channel = 1; - tx_size = MIB_TO_BYTE(tx_size_init); - rx_size = MIB_TO_BYTE(rx_size_init); - - LastLogQstring = "AXI DMA Parameters:"; - ui->textBrowser_receivedMessages->append(LastLogQstring); - std::cout << LastLogQstring.toStdString() << std::endl; - LastLogQstring = "Transmit Buffer Size:" + QString::number(BYTE_TO_MIB(tx_size)) + " MiB"; - ui->textBrowser_receivedMessages->append(LastLogQstring); - std::cout << LastLogQstring.toStdString() << std::endl; - ; - LastLogQstring = "Receive Buffer Size:" + QString::number(BYTE_TO_MIB(rx_size)) + " MiB"; - ui->textBrowser_receivedMessages->append(LastLogQstring); - std::cout << LastLogQstring.toStdString() << std::endl; - // setup axis switch - XDma_lb_axis_switch loop_back_sw; - const char *loop_back_sw_name = "dma_lb_axis_switch"; - int sw_init_status = 0; - sw_init_status = XDma_lb_axis_switch_Initialize(&loop_back_sw, loop_back_sw_name); - if (sw_init_status != 0) - { - LastLogQstring = "AXI DMA sw initialize failed with code:" + QString::number(BYTE_TO_MIB(sw_init_status)) ; - ui->textBrowser_receivedMessages->append(LastLogQstring); - std::cout << LastLogQstring.toStdString() << std::endl; - } - else - { - LastLogQstring = "AXI DMA sw initialized"; - ui->textBrowser_receivedMessages->append(LastLogQstring); - std::cout << LastLogQstring.toStdString() << std::endl; - - } - XDma_lb_axis_switch_Set_dma_loopback_en(&loop_back_sw, 1); - LastLogQstring = "AXI DMA sw loopback enabled"; - ui->textBrowser_receivedMessages->append(LastLogQstring); - std::cout << LastLogQstring.toStdString() << std::endl; - - - // Initialize the AXI DMA device - axidma_dev = axidma_init(); - if (axidma_dev == NULL) - { - LastLogQstring = "Failed to initialize the AXI DMA device."; - ui->textBrowser_receivedMessages->append(LastLogQstring); - std::cout << LastLogQstring.toStdString() << std::endl; - rc = 1; - } - - // Map memory regions for the transmit and receive buffers - tx_buf = static_cast(axidma_malloc(axidma_dev, tx_size)); - if (tx_buf == NULL) - { - LastLogQstring = "Unable to allocate transmit buffer from the AXI DMA device."; - ui->textBrowser_receivedMessages->append(LastLogQstring); - std::cout << LastLogQstring.toStdString() << std::endl; - rc = -1; - axidma_destroy(axidma_dev); - } - rx_buf = static_cast(axidma_malloc(axidma_dev, rx_size)); - if (rx_buf == NULL) - { - LastLogQstring = "Unable to allocate receive buffer from the AXI DMA device"; - ui->textBrowser_receivedMessages->append(LastLogQstring); - std::cout << LastLogQstring.toStdString() << std::endl; - rc = -1; - axidma_free(axidma_dev, tx_buf, tx_size); - axidma_destroy(axidma_dev); - } - - // Get all the transmit and receive channels - tx_chans = axidma_get_dma_tx(axidma_dev); - rx_chans = axidma_get_dma_rx(axidma_dev); - - if (tx_chans->len < 1) - { - LastLogQstring = "Error: No transmit channels were found."; - ui->textBrowser_receivedMessages->append(LastLogQstring); - std::cout << LastLogQstring.toStdString() << std::endl; - rc = -ENODEV; - axidma_free(axidma_dev, rx_buf, rx_size); - axidma_free(axidma_dev, tx_buf, tx_size); - axidma_destroy(axidma_dev); - } - if (rx_chans->len < 1) - { - LastLogQstring = "Error: No receive channels were found."; - ui->textBrowser_receivedMessages->append(LastLogQstring); - std::cout << LastLogQstring.toStdString() << std::endl; - rc = -ENODEV; - axidma_free(axidma_dev, rx_buf, rx_size); - axidma_free(axidma_dev, tx_buf, tx_size); - axidma_destroy(axidma_dev); - } - - /* If the user didn't specify the channels, we assume that the transmit and - * receive channels are the lowest numbered ones. */ - if (tx_channel == -1 && rx_channel == -1) - { - tx_channel = tx_chans->data[0]; - rx_channel = rx_chans->data[0]; - } - LastLogQstring = "Using transmit channel " + QString::number(tx_channel) + " and receive channel " + QString::number(rx_channel); - ui->textBrowser_receivedMessages->append(LastLogQstring); - std::cout << LastLogQstring.toStdString() << std::endl; - - - XDma_lb_axis_switch_Set_dma_loopback_en(&loop_back_sw, 0); - XDma_lb_axis_switch_Release(&loop_back_sw); - LastLogQstring = "AXI DMA sw loopback disabled"; - ui->textBrowser_receivedMessages->append(LastLogQstring); - std::cout << LastLogQstring.toStdString() << std::endl; - ui->pushButton_init_dma->setEnabled(false); - LastLogQstring = "DMA test DONE"; - ui->textBrowser_receivedMessages->append(LastLogQstring); - std::cout << LastLogQstring.toStdString() << std::endl; - - QString receiver = "DMA initialize DONE"; - - foreach (QTcpSocket* socket,connection_set) - { - if(socket->socketDescriptor() == receiver.toLongLong()) - { - sendMessage(socket); - break; - } - } -} - diff --git a/qt/FZ3TCPServer/mainwindow.h b/qt/FZ3TCPServer/mainwindow.h index f75359cc5f652efd1751883c4b16a9224b9d2a9f..456e8dca9e6dc25f512f2a5f55db718647e00457 100644 --- a/qt/FZ3TCPServer/mainwindow.h +++ b/qt/FZ3TCPServer/mainwindow.h @@ -18,68 +18,61 @@ #include #include // Strlen function -#include // Flags for open() +#include // Flags for open() #include // Open() system call #include // Types for open() #include // Mmap system call #include // IOCTL system call -#include // Close() system call +#include // Close() system call #include // Timing functions and definitions -#include // Option parsing -#include // Error codes +#include // Option parsing +#include // Error codes #include "AXIDMA/libaxidma.h" // Interface to the AXI DMA -#include "AXIDMA/util.h" // Miscellaneous utilities +#include "AXIDMA/util.h" // Miscellaneous utilities #include "AXIDMA/conversion.h" // Miscellaneous conversion utilities #include "AXIDMA/axidma_benchmark.h" #include "AXIDMA/xdma_lb_axis_switch.h" -namespace Ui { -class MainWindow; +namespace Ui +{ + class MainWindow; } class MainWindow : public QMainWindow { - Q_OBJECT + Q_OBJECT public: - explicit MainWindow(QWidget *parent = nullptr); - ~MainWindow(); + explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow(); signals: - void newMessage(QString); + void newMessage(QString); private slots: - void newConnection(); - void appendToSocketList(QTcpSocket* socket); - - void readSocket(); - void discardSocket(); - void displayError(QAbstractSocket::SocketError socketError); - - void displayMessage(const QString& str); - void sendMessage(QTcpSocket* socket); - void sendAttachment(QTcpSocket* socket, QString filePath); - - void on_pushButton_sendMessage_clicked(); - void on_pushButton_sendAttachment_clicked(); - - void refreshComboBox(); - void on_Init_dma_clicked(); - void init_dma(); + void newConnection(); + void appendToSocketList(QTcpSocket *socket); + void discardSocket(); + void displayError(QAbstractSocket::SocketError socketError); + void displayMessage(const QString &str); + void sendDataToClient(QTcpSocket *socket, QByteArray *fileDataPtr,unsigned long long fileDataSize); + void refreshComboBox(); + void init_dma(); + void on_pushButton_init_dma_clicked(); + void on_pushButton_sendData_clicked(); private: - Ui::MainWindow *ui; - QTcpServer* m_server; - QSet connection_set; - int rc; - int tx_channel, rx_channel; - int num_transfers = 10; - size_t tx_size, rx_size; - char *tx_buf, *rx_buf; - axidma_dev_t axidma_dev; - const array_t *tx_chans, *rx_chans; - QString LastLogQstring; - + Ui::MainWindow *ui; + QTcpServer *m_server; + QSet connection_set; + int rc; + int tx_channel, rx_channel; + int num_transfers = 10; + size_t tx_size, rx_size; + char *tx_buf, *rx_buf; + axidma_dev_t axidma_dev; + const array_t *tx_chans, *rx_chans; + QString LastLogQstring; }; #endif // MAINWINDOW_H diff --git a/qt/FZ3TCPServer/mainwindow.ui b/qt/FZ3TCPServer/mainwindow.ui index 8908ce4c88d88166ee77c35620e97e682f3d9768..5eb5c03bc8035e7cdee43c3d12e50337a1445145 100644 --- a/qt/FZ3TCPServer/mainwindow.ui +++ b/qt/FZ3TCPServer/mainwindow.ui @@ -18,20 +18,35 @@ - - - - Initialize and test DMA + + + + + 110 + 0 + + + + Broadcast + + - - + + + + + + + + + - Send Attachment + capture sensor data false @@ -45,62 +60,11 @@ - - - - - - - - - - - - - - - - - 150 - 0 - - - - - - - - - - - - 0 - 0 - - - - - - - Send Message - - - - - - - - - - 110 - 0 - + + + + Initialize and test DMA - - - Broadcast - -