/*! * * Copyright (c) 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 DCSSimController.h * \author (original) Stephen Quong * \date (original) 30-Jul-2026 * */ #pragma once #include #include #include #include #include #include #include #include "MqttPacket.h" /*! * \brief QTcpServer that upgrades accepted connections to server-side TLS. * \details Plain QTcpServer while no TLS material is set. With material set, * each accepted descriptor becomes a QSslSocket and the handshake is * started before the connection is surfaced. */ class SslTcpServer : public QTcpServer { Q_OBJECT public: /*! * \brief Server-side TLS material; TLS is disabled while cert is null. */ struct Tls { QSslCertificate cert; ///< Server certificate. QSslKey key; ///< Server private key. QList caCerts; ///< When set, clients must present a certificate these sign (mTLS). }; using QTcpServer::QTcpServer; void setTls(const Tls &tls); protected: void incomingConnection(qintptr socketDescriptor) override; private: Tls _tls; }; /*! * \brief Diality Cloud System (DCS) simulator */ class DCSSimController : public QObject { Q_OBJECT public: /*! * \brief TLS configuration, all PEM; TLS is off while certPath is empty. */ struct TlsConfig { QString certPath; ///< Server certificate. QString keyPath; ///< Server private key. QString caPath; ///< When set, clients must present a certificate this bundle signs (mTLS). }; explicit DCSSimController(quint16 port, const TlsConfig &tls = {}, QObject *parent = nullptr); bool listen(); private Q_SLOTS: void onNewConnection(); void onReadyRead(); void onDisconnected(); private: bool initTls(); void handlePacket(const Mqtt::Packet &packet); void handleConnectPacket(const Mqtt::Packet &packet); void handlePublishPacket(const Mqtt::Packet &packet); void dumpPayload(const QByteArray &payload); SslTcpServer _server; QPointer _client; QByteArray _rxBuf; quint16 _port; TlsConfig _tlsConfig; quint64 _publishCount = 0; };