Index: LeahiRt/LeahiRtController.cpp =================================================================== diff -u -r401d8ad0e5c070f9c6ecb41ed3ba25ba3d1c69a0 -r808df723cf9d1b0097796815e5229abfa18f2d8f --- LeahiRt/LeahiRtController.cpp (.../LeahiRtController.cpp) (revision 401d8ad0e5c070f9c6ecb41ed3ba25ba3d1c69a0) +++ LeahiRt/LeahiRtController.cpp (.../LeahiRtController.cpp) (revision 808df723cf9d1b0097796815e5229abfa18f2d8f) @@ -1,20 +1,44 @@ +/*! + * + * Copyright (c) 2024-2026 Diality Inc. - All Rights Reserved. + * \copyright + * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN + * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. + * + * \file LeahiRtController.cpp + * \author (original) Stephen Quong + * \date (original) 24-May-2026 + * + */ #include #include #include "LeahiRtController.h" #include "LeahiMsgDefs.h" +/*! + * \brief LeahiRtController::LeahiRtController + * \details Constructor. Starts the CAN interface and wires the frame→dispatcher→ + * completed-message pipeline. + * \param configPath - path to the settings file + * \param parent - optional QObject parent + */ LeahiRtController::LeahiRtController(const QString &configPath, QObject *parent) : QObject(parent), _settings(configPath, QSettings::IniFormat), _canInterface(), _canThread(this), - _msgBuilder(this) + _dispatcher(this) { _canInterface.init(_canThread); connect(&_canInterface, &Can::CanInterface::didFrameReceive, this, &LeahiRtController::onFrameReceive); + connect(&_dispatcher, &Can::MessageDispatcher::didActionReceive, this, &LeahiRtController::onMessageReceive); } +/*! + * \brief LeahiRtController::~LeahiRtController + * \details Destructor. Stops and joins the CAN and Agent worker threads. + */ LeahiRtController::~LeahiRtController() { _canThread.quit(); @@ -24,21 +48,50 @@ _agentThread.wait(); } +/*! + * \brief LeahiRtController::connectToAgent + * \details Initialises the AgentInterface using the socket path and reconnect + * interval from the settings file. + */ void LeahiRtController::connectToAgent() { const QString socketPath = _settings.value("Socket/LocalSocketName", "/tmp/leahi_rt.sock").toString(); const int reconnectIntervalMs = _settings.value("Socket/ReconnectIntervalMs", 5000).toInt(); _agentInterface.init(socketPath, reconnectIntervalMs, _agentThread); } +/*! + * \brief LeahiRtController::onFrameReceive + * \details Unpacks a CAN frame and feeds it to the dispatcher, which reassembles + * multi-frame messages per CAN id. + * \param frame - the received CAN frame + */ void LeahiRtController::onFrameReceive(const QCanBusFrame frame) { - const Can::CanId canId = Can::CanId(frame.frameId()); - Can::Message &msg = _messages[canId]; - if (_msgBuilder.buildMessage(frame.payload(), msg, canId) && msg.isComplete()) { - qDebug().noquote() << QString("Received message with MsgId=0x%1").arg(QString("%1").arg(msg.msgId, 4, 16, QChar('0')).toUpper()); - const QByteArray payload = leahi::canMessageToProtobufByteArray(QDateTime::currentDateTime(), QStringLiteral("test_device"), msg); - _agentInterface.send(AgentMessage::MsgId::ClinicalData, _txSequence++, payload); - msg.clear(); - } + _dispatcher.onFrameReceive(Can::CanId(frame.frameId()), frame.payload()); } + +/*! + * \brief LeahiRtController::onMessageReceive + * \details Converts a fully reassembled message to protobuf and forwards it to + * the Agent. + * \param msg - the reassembled message + */ +void LeahiRtController::onMessageReceive(const Can::Message &msg) +{ + Can::Message &cachedMsg = _msgCache[msg.msgId]; + qDebug().noquote() << QString("Received message with MsgId=0x%1").arg(QString("%1").arg(msg.msgId, 4, 16, QChar('0')).toUpper()); + + // only send the message if the data is different + // SQ if (cachedMsg.data != msg.data) { + // SQ if (msg.msgId == leahi::MSG_ID_TD_OP_MODE_DATA) { + const QByteArray payload = leahi::canMessageToProtobufByteArray( + QDateTime::currentDateTime(), + QStringLiteral("test_device"), + msg); + _agentInterface.send(AgentMessage::MsgId::ClinicalData, _txSequence++, payload); + // SQ } + // SQ } + cachedMsg = msg; + msg.dump(); +}