Skip to content

Commit

Permalink
pqm: add a callback for the PQM ping task and handle the ping interna…
Browse files Browse the repository at this point in the history
…lly.

In order to avoid thread concurrency over the serial connection while
acquiring data and also pinging the board, we need to handle the ping
procedure internally. This can only be reproduced on Windows (my guess is
that the different implementation of libserialport for Windows vs UNIX
is the cause for this). Serial port calls return -995 at some point (which
is the error code for no I/O access. After this happens there is no
recovery for the serial connection and the plugin disconnects from the
device. This happens after a random amount of time.

This commit is a temporary workaround due to demand of users. But there is
a task in progress for development and investigation on the IIOPingTask
which should handle the plugin callback for the ping procedure but
should also move the ping management in the DeviceManager rather than
having it inside each plugin.

Signed-off-by: AlexandraTrifan <[email protected]>
  • Loading branch information
AlexandraTrifan authored and andreidanila1 committed Jul 2, 2024
1 parent 6097841 commit 6d26a9f
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 12 deletions.
3 changes: 2 additions & 1 deletion iioutil/include/iioutil/iiopingtask.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SCOPY_IIOUTIL_EXPORT IIOPingTask : public QThread
IIOPingTask(iio_context *c, QObject *parent = nullptr);
~IIOPingTask();
virtual void run() override;

void setCallback(const std::function<bool(void)> callback);
static bool ping(iio_context *ctx);

Q_SIGNALS:
Expand All @@ -29,6 +29,7 @@ class SCOPY_IIOUTIL_EXPORT IIOPingTask : public QThread
protected:
iio_context *c;
bool enabled;
std::function<bool(void)> m_callback;
};
} // namespace scopy
#endif // IIOPINGTASK_H
14 changes: 12 additions & 2 deletions iioutil/src/iiopingtask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ using namespace scopy;
IIOPingTask::IIOPingTask(iio_context *c, QObject *parent)
: QThread(parent)
, c(c)
, m_callback(nullptr)
{}

IIOPingTask::~IIOPingTask() {}

void IIOPingTask::run()
{

bool ret;
enabled = true;
bool ret = ping(c);
if (m_callback) {
ret = m_callback();
} else {
ret = ping(c);
}

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

void IIOPingTask::setCallback(const std::function<bool()> callback)
{
m_callback = callback;
}

bool IIOPingTask::ping(iio_context *ctx)
{
auto dev = iio_context_get_device(ctx, 0);
Expand Down
4 changes: 4 additions & 0 deletions plugins/pqm/include/pqm/acquisitionmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class AcquisitionManager : public QObject
~AcquisitionManager();

public Q_SLOTS:
bool ping();
void toolEnabled(bool en, QString toolName);
void setConfigAttr(QMap<QString, QMap<QString, QString>>);
Q_SIGNALS:
Expand All @@ -38,6 +39,7 @@ private Q_SLOTS:
bool readPqmAttributes();
bool readBufferedData();
void setData(QMap<QString, QMap<QString, QString>>);
bool pingInternal();

iio_context *m_ctx;
iio_buffer *m_buffer;
Expand All @@ -52,6 +54,8 @@ private Q_SLOTS:

bool m_attrHaveBeenRead = false;
bool m_buffHaveBeenRead = false;

std::atomic<bool> m_pingStatus;
};
} // namespace scopy::pqm

Expand Down
2 changes: 1 addition & 1 deletion plugins/pqm/include/pqm/pqmcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PqmController : public QObject
PqmController(QString uri, QObject *parent = nullptr);
~PqmController();

void startPingTask(iio_context *ctx);
void startPingTask(iio_context *ctx, const std::function<bool(void)> callback);
void stopPingTask();

Q_SIGNALS:
Expand Down
32 changes: 27 additions & 5 deletions plugins/pqm/src/acquisitionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ AcquisitionManager::AcquisitionManager(iio_context *ctx, QObject *parent)
: QObject(parent)
, m_ctx(ctx)
, m_buffer(nullptr)
, m_pingStatus(true)
{
m_readFw = new QFutureWatcher<void>(this);
m_setFw = new QFutureWatcher<void>(this);
Expand Down Expand Up @@ -50,6 +51,24 @@ AcquisitionManager::~AcquisitionManager()
m_pqmAttr.clear();
}

bool AcquisitionManager::pingInternal()
{
auto dev = iio_context_get_device(m_ctx, 0);
const iio_device *test_device = nullptr;

int ret = iio_device_get_trigger(dev, &test_device);

if(ret < 0 && ret != -ENOENT) {
return false;
}
return true;
}

bool AcquisitionManager::ping()
{
return m_pingStatus;
}

void AcquisitionManager::enableBufferChnls(iio_device *dev)
{
int chnlsNo = iio_device_get_channels_count(dev);
Expand Down Expand Up @@ -86,11 +105,14 @@ void AcquisitionManager::futureReadData()
void AcquisitionManager::readData()
{
mutex.lock();
if(m_tools["rms"] || m_tools["harmonics"] || m_tools["settings"]) {
m_attrHaveBeenRead = readPqmAttributes();
}
if(m_tools["waveform"]) {
m_buffHaveBeenRead = readBufferedData();
m_pingStatus = pingInternal();
if(m_pingStatus) {
if(m_tools["rms"] || m_tools["harmonics"] || m_tools["settings"]) {
m_attrHaveBeenRead = readPqmAttributes();
}
if(m_tools["waveform"]) {
m_buffHaveBeenRead = readBufferedData();
}
}
mutex.unlock();
}
Expand Down
3 changes: 2 additions & 1 deletion plugins/pqm/src/pqmcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ PqmController::PqmController(QString uri, QObject *parent)

PqmController::~PqmController() { stopPingTask(); }

void PqmController::startPingTask(iio_context *ctx)
void PqmController::startPingTask(iio_context *ctx, const std::function<bool(void)> callback)
{
if(!ctx) {
qWarning(CAT_PQM_CONTROLLER) << "The context is unavailable!";
return;
}
m_pingTask = new IIOPingTask(ctx);
m_pingTask->setCallback(callback);
m_pingTimer = new CyclicalTask(m_pingTask);
connect(m_pingTask, &IIOPingTask::pingFailed, this, &PqmController::pingFailed);
connect(m_pingTask, &IIOPingTask::pingSuccess, this, &PqmController::pingSuccess);
Expand Down
5 changes: 3 additions & 2 deletions plugins/pqm/src/pqmplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ bool PQMPlugin::onConnect()
}
struct iio_context *ctx = conn->context();
connect(m_pqmController, &PqmController::pingFailed, this, &PQMPlugin::disconnectDevice);
m_pqmController->startPingTask(ctx);

m_acqManager = new AcquisitionManager(ctx, this);
auto fp = std::bind(&AcquisitionManager::ping, m_acqManager);
m_pqmController->startPingTask(ctx, fp);


RmsInstrument *rms = new RmsInstrument();
m_toolList[0]->setTool(rms);
Expand Down

0 comments on commit 6d26a9f

Please sign in to comment.