Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ping task #1647

Merged
merged 9 commits into from
Jul 9, 2024
4 changes: 2 additions & 2 deletions core/include/core/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class SCOPY_CORE_EXPORT Device
virtual QString param() = 0;
virtual QString displayParam() = 0;
virtual QWidget *icon() = 0;
;

virtual QWidget *page() = 0;
;

virtual QList<ToolMenuEntry *> toolList() = 0;
virtual void init() = 0;
virtual void preload() = 0;
Expand Down
4 changes: 4 additions & 0 deletions core/include/core/deviceimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public Q_SLOTS:
void loadPages();
void loadToolList();
void loadBadges();
void setPingPlugin(Plugin *plugin);
void bindPing();
void unbindPing();

protected:
PluginManager *p;
Expand All @@ -76,6 +79,7 @@ public Q_SLOTS:
QWidget *m_icon;
QWidget *m_page;
QPushButton *connbtn, *discbtn;
Plugin *m_pingPlugin = nullptr;
};
} // namespace scopy

Expand Down
37 changes: 37 additions & 0 deletions core/src/deviceimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,40 @@ void DeviceImpl::loadBadges()
connect(this, &DeviceImpl::connected, warningHover, &HoverWidget::hide);
}

void DeviceImpl::setPingPlugin(Plugin *plugin)
{
if(!m_pingPlugin && plugin->pingTask()) {
m_pingPlugin = plugin;
}
}

void DeviceImpl::bindPing()
{
if(!m_pingPlugin) {
return;
}
for(auto &&p : m_connectedPlugins) {
connect(dynamic_cast<QObject *>(p), SIGNAL(pausePingTask(bool)), dynamic_cast<QObject *>(m_pingPlugin),
SLOT(onPausePingTask(bool)));
}
connect(m_pingPlugin->pingTask(), &PingTask::pingFailed, this, &DeviceImpl::disconnectDev);
m_pingPlugin->startPingTask();
}

void DeviceImpl::unbindPing()
{
if(!m_pingPlugin) {
return;
}
for(auto &&p : m_connectedPlugins) {
disconnect(dynamic_cast<QObject *>(p), SIGNAL(pausePingTask(bool)),
dynamic_cast<QObject *>(m_pingPlugin), SLOT(onPausePingTask(bool)));
}
m_pingPlugin->stopPingTask();
disconnect(m_pingPlugin->pingTask(), &PingTask::pingFailed, this, &DeviceImpl::disconnectDev);
m_pingPlugin = nullptr;
}

void DeviceImpl::onConnectionFailed() { disconnectDev(); }

QList<Plugin *> DeviceImpl::plugins() const { return m_plugins; }
Expand Down Expand Up @@ -280,6 +314,7 @@ void DeviceImpl::connectDev()
p->loadSettings(s);
}
m_connectedPlugins.push_back(p);
setPingPlugin(p);
} else {
disconnectDevice = p->metadata().value("disconnectDevOnConnectFailure").toBool();
if(disconnectDevice) {
Expand All @@ -293,6 +328,7 @@ void DeviceImpl::connectDev()
connbtn->hide();
discbtn->show();
discbtn->setFocus();
bindPing();
Q_EMIT connected();
}
qInfo(CAT_BENCHMARK) << this->displayName() << " device connection took: " << timer.elapsed() << "ms";
Expand All @@ -303,6 +339,7 @@ void DeviceImpl::disconnectDev()
QElapsedTimer pluginTimer;
QElapsedTimer timer;
timer.start();
unbindPing();
connbtn->show();
discbtn->hide();
Preferences *pref = Preferences::GetInstance();
Expand Down
1 change: 0 additions & 1 deletion core/src/devicemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "devicefactory.h"
#include "deviceimpl.h"
#include "deviceloader.h"
#include "iiodeviceimpl.h"
#include "pluginbase/statusbarmanager.h"

#include <QDebug>
Expand Down
34 changes: 34 additions & 0 deletions iioutil/include/iioutil/cmdqpingtask.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef SWIOTPINGTASK_H
#define SWIOTPINGTASK_H

#include <iio.h>

#include <QThread>

#include "command.h"
#include "connection.h"
#include "pingtask.h"

namespace scopy {
class SCOPY_IIOUTIL_EXPORT CmdQPingTask : public PingTask
{
Q_OBJECT
public:
CmdQPingTask(Connection *conn, QObject *parent = nullptr);
CmdQPingTask(Connection *conn, QString pingDevice, QObject *parent = nullptr);
~CmdQPingTask();
void run() override;
bool ping() override;

protected:
QString m_pingDevice = "";
Connection *c;

private Q_SLOTS:
void getTriggerCommandFinished(scopy::Command *cmd);

private:
const int MS_TO_WAIT = 2000;
};
} // namespace scopy
#endif // SWIOTPINGTASK_H
4 changes: 4 additions & 0 deletions iioutil/include/iioutil/commandqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <QThread>
#include <QThreadPool>
#include <QTime>

#include <deque>

Expand All @@ -25,6 +26,8 @@ class SCOPY_IIOUTIL_EXPORT CommandQueue : public QObject
void wait();
void requestStop();
void runCmd();
QTime lastCmdTime() const;

private Q_SLOTS:
void resolveNext(scopy::Command *cmd);

Expand All @@ -34,6 +37,7 @@ private Q_SLOTS:
std::mutex m_enqueueMutex;
std::atomic<bool> m_running;
QThreadPool m_commandExecThreadPool;
QTime m_lastCmdTime;
};
} // namespace scopy
#endif // IIOCOMMANDQUEUE_H
15 changes: 4 additions & 11 deletions iioutil/include/iioutil/iiopingtask.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
#ifndef IIOPINGTASK_H
#define IIOPINGTASK_H

#include "pingtask.h"
#include "scopy-iioutil_export.h"

#include <iio.h>

#include <QThread>

namespace scopy {
/**
* @brief The IIOPingTask class
* IIOPingTask verifies IIO connection and emits pingSuccess/pingFailed
*/
class SCOPY_IIOUTIL_EXPORT IIOPingTask : public QThread
class SCOPY_IIOUTIL_EXPORT IIOPingTask : public PingTask
{
Q_OBJECT
public:
IIOPingTask(iio_context *c, QObject *parent = nullptr);
~IIOPingTask();
virtual void run() override;

static bool ping(iio_context *ctx);

Q_SIGNALS:
void pingSuccess();
void pingFailed();
virtual bool ping() override;

protected:
iio_context *c;
bool enabled;
iio_context *m_ctx;
};
} // namespace scopy
#endif // IIOPINGTASK_H
24 changes: 24 additions & 0 deletions iioutil/include/iioutil/pingtask.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef PINGTASK_H
#define PINGTASK_H

#include "scopy-iioutil_export.h"
#include <QThread>

namespace scopy {
class SCOPY_IIOUTIL_EXPORT PingTask : public QThread
{
Q_OBJECT
public:
PingTask(QObject *parent);
~PingTask();

virtual void run() override;
virtual bool ping() = 0;

Q_SIGNALS:
void pingSuccess();
void pingFailed();
};
} // namespace scopy

#endif // PINGTASK_H
71 changes: 71 additions & 0 deletions iioutil/src/cmdqpingtask.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "cmdqpingtask.h"

#include <QLoggingCategory>
#include "iiocommand/iiodevicegettrigger.h"

Q_LOGGING_CATEGORY(CAT_CMDQPING, "CmdQPingTask")

using namespace scopy;
CmdQPingTask::CmdQPingTask(Connection *conn, QObject *parent)
: PingTask(parent)
{
c = conn;
}

CmdQPingTask::CmdQPingTask(Connection *conn, QString pingDevice, QObject *parent)
: PingTask(parent)
{
c = conn;
m_pingDevice = pingDevice;
}

CmdQPingTask::~CmdQPingTask() {}

void CmdQPingTask::run()
{
if(!c) {
Q_EMIT pingFailed();
return;
}
int lastCmdInterval = c->commandQueue()->lastCmdTime().msecsTo(QTime::currentTime());
if(lastCmdInterval < MS_TO_WAIT) {
return;
}
bool pingStatus = ping();
if(!pingStatus) {
Q_EMIT pingFailed();
return;
}
}

bool CmdQPingTask::ping()
{
auto dev = (!m_pingDevice.isEmpty()) ? iio_context_find_device(c->context(), m_pingDevice.toStdString().c_str())
: iio_context_find_device(c->context(), 0);

if(!dev) {
qWarning(CAT_CMDQPING) << "Device not found!";
return false;
}
Command *getTriggerCommand = new IioDeviceGetTrigger(dev, nullptr);
connect(getTriggerCommand, &scopy::Command::finished, this, &CmdQPingTask::getTriggerCommandFinished,
Qt::QueuedConnection);
c->commandQueue()->enqueue(getTriggerCommand);
return true;
}

void CmdQPingTask::getTriggerCommandFinished(scopy::Command *cmd)
{
IioDeviceGetTrigger *tcmd = dynamic_cast<IioDeviceGetTrigger *>(cmd);
if(!tcmd) {
return;
}
int ret = tcmd->getReturnCode();
if(ret >= 0 || ret == -ENOENT) {
Q_EMIT pingSuccess();
} else {
Q_EMIT pingFailed();
}
}

#include "moc_cmdqpingtask.cpp"
7 changes: 6 additions & 1 deletion iioutil/src/commandqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Q_LOGGING_CATEGORY(CAT_COMMANDQUEUE, "CommandQueue");
CommandQueue::CommandQueue(QObject *parent)
: QObject(parent)
, m_running(false)
{}
{
m_lastCmdTime = QTime::currentTime();
}

CommandQueue::~CommandQueue()
{
Expand Down Expand Up @@ -44,6 +46,7 @@ void CommandQueue::start()

void CommandQueue::resolveNext(scopy::Command *cmd)
{
m_lastCmdTime = QTime::currentTime();
m_commandQueue.pop_front(); // also delete/disconnect
qDebug(CAT_COMMANDQUEUE) << "delete " << cmd;
disconnect(cmd, &Command::finished, this, &CommandQueue::resolveNext);
Expand All @@ -56,6 +59,8 @@ void CommandQueue::resolveNext(scopy::Command *cmd)
}
}

QTime CommandQueue::lastCmdTime() const { return m_lastCmdTime; }

void CommandQueue::runCmd()
{
std::lock_guard<std::mutex> lock(m_commandMutex);
Expand Down
4 changes: 3 additions & 1 deletion iioutil/src/cyclicaltask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ void CyclicalTask::stop()
{
if(enabled) {
qDebug(CAT_CYCLICALTASK) << "Stopping scanner thread";
task->requestInterruption();
t->stop();
if(!task->isFinished()) {
task->wait(THREAD_FINISH_TIMEOUT);
}
enabled = false;
}
}
Expand Down
12 changes: 5 additions & 7 deletions iioutil/src/iiopingtask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@

using namespace scopy;
IIOPingTask::IIOPingTask(iio_context *c, QObject *parent)
: QThread(parent)
, c(c)
: PingTask(parent)
, m_ctx(c)
{}

IIOPingTask::~IIOPingTask() {}

void IIOPingTask::run()
{

enabled = true;
bool ret = ping(c);
bool ret = ping();

if(isInterruptionRequested())
return;
Expand All @@ -24,9 +22,9 @@ void IIOPingTask::run()
Q_EMIT pingFailed();
}

bool IIOPingTask::ping(iio_context *ctx)
bool IIOPingTask::ping()
{
auto dev = iio_context_get_device(ctx, 0);
auto dev = iio_context_get_device(m_ctx, 0);
const iio_device *test_device = nullptr;

int ret = iio_device_get_trigger(dev, &test_device);
Expand Down
12 changes: 12 additions & 0 deletions iioutil/src/pingtask.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "pingtask.h"

using namespace scopy;
PingTask::PingTask(QObject *parent)
: QThread(parent)
{}

PingTask::~PingTask() {}

void PingTask::run() {}

#include "moc_pingtask.cpp"
Loading
Loading