Commit 7d9a7549 authored by Grégory JANISZEWSKI's avatar Grégory JANISZEWSKI

Make CanOpenProtocolManager handling of SDOs thread-safe (use mutexes)

parent 233408e2
......@@ -97,8 +97,10 @@ CanOpenSdoTransfer::CanOpenSdoTransfer (void)
, toggleBit (false)
, finished (false)
, sendFrame (false)
, waitingFrame (false)
, useCrc (false)
, checksum (0x0000)
, dirty (false)
{ }
void CanOpenSdoTransfer::createBuffer (const CanOpenDataLen len) {
......@@ -107,6 +109,18 @@ void CanOpenSdoTransfer::createBuffer (const CanOpenDataLen len) {
memset (buffer.data (), 0x00, len);
}
bool CanOpenSdoTransfer::isDirty (void) const {
return dirty;
}
void CanOpenSdoTransfer::markDirty (void) {
dirty = true;
}
void CanOpenSdoTransfer::markClean (void) {
dirty = false;
}
/*** CAN OPEN SDO TRANSFERS QUEUE ***/
CanOpenSdoTransferQueue::CanOpenSdoTransferQueue (const CanOpenNodeId nodeId, const CanOpenSdoMode mode)
......@@ -132,6 +146,38 @@ void CanOpenSdoTransferQueue::restartTimer (void) {
timer->start (timeout);
}
void CanOpenSdoTransferQueue::addPendingTransfer (CanOpenSdoTransfer * transfer) {
QMutexLocker locker (&mutex);
if (currentTransfer == Q_NULLPTR && transfersQueue.isEmpty ()) {
currentTransfer = transfer;
transfer->markDirty ();
}
else {
transfersQueue.enqueue (transfer);
}
}
CanOpenSdoTransfer * CanOpenSdoTransferQueue::getCurrentTransfer (void) {
QMutexLocker locker (&mutex);
return currentTransfer;
}
void CanOpenSdoTransferQueue::prepareNextTransfer (void) {
QMutexLocker locker (&mutex);
if (currentTransfer == Q_NULLPTR && !transfersQueue.isEmpty ()) {
currentTransfer = transfersQueue.dequeue ();
currentTransfer->markDirty ();
}
}
void CanOpenSdoTransferQueue::resetCurrentTransfer (void) {
QMutexLocker locker (&mutex);
if (currentTransfer != Q_NULLPTR) {
delete currentTransfer;
currentTransfer = Q_NULLPTR;
}
}
/*** CAN OPEN PDO CONFIG/MAPPING CACHE ***/
CanOpenPdoConfigCache::CanOpenPdoConfigCache (void)
......
......@@ -7,6 +7,7 @@
#include <QString>
#include <QStringBuilder>
#include <QTimer>
#include <QMutex>
#include "QtCAN.h"
#include "CanMessage.h"
......@@ -610,6 +611,7 @@ public:
struct QTCAN_CANOPEN_EXPORT CanOpenSdoTransfer {
explicit CanOpenSdoTransfer (void);
QMutex mutex;
CanOpenSdoOperation operation;
CanOpenSdoMode mode;
CanOpenSdoMethod method;
......@@ -626,17 +628,26 @@ struct QTCAN_CANOPEN_EXPORT CanOpenSdoTransfer {
bool toggleBit;
bool finished;
bool sendFrame;
bool waitingFrame;
bool useCrc;
quint16 checksum;
QByteArray buffer;
void createBuffer (const CanOpenDataLen len);
bool isDirty (void) const;
void markDirty (void);
void markClean (void);
private:
bool dirty;
};
struct QTCAN_CANOPEN_EXPORT CanOpenSdoTransferQueue {
explicit CanOpenSdoTransferQueue (const CanOpenNodeId nodeId = 0x00,
const CanOpenSdoMode mode = CanOpenSdoModes::SdoNeiter);
QMutex mutex;
int timeout;
CanOpenNodeId nodeId;
CanOpenSdoMode mode;
......@@ -646,6 +657,11 @@ struct QTCAN_CANOPEN_EXPORT CanOpenSdoTransferQueue {
void killTimer (void);
void restartTimer (void);
void addPendingTransfer (CanOpenSdoTransfer * transfer);
CanOpenSdoTransfer * getCurrentTransfer (void);
void prepareNextTransfer (void);
void resetCurrentTransfer (void);
};
struct QTCAN_CANOPEN_EXPORT CanOpenLssAction {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -122,6 +122,8 @@ protected slots:
void onHeartBeatIntervalChanged (void);
void onHeartbeatConsumerChanged (void);
void onSdoClockTick (void);
void onDiag (const int level, const QString & description);
void handleNMT (CanMessage * msg, const CanOpenNodeId nodeId, const bool isRtr);
......@@ -136,7 +138,7 @@ protected slots:
void refreshPdoMapping (void);
void enqueueSdoTransfer (CanOpenSdoTransfer * transfer);
void processSdoTransfer (const CanOpenNodeId nodeId);
void processSdoTransfer (CanOpenSdoTransfer * transfer);
void enqueueLssAction (CanOpenLssAction * action);
void processLssAction (void);
......@@ -153,6 +155,7 @@ private:
CanOpenNodeId m_localNodeId;
CanOpenHeartBeatState m_localNodeState;
CanOpenNetPosition m_localNetworkPos;
QTimer * m_sdoTimer;
QTimer * m_syncTimer;
QTimer * m_heartBeatTimer;
CanDriver * m_driver;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment