From fadf58e42d9e39f782354e68f81febf600367b45 Mon Sep 17 00:00:00 2001 From: Krzysztof Strehlau Date: Sun, 23 Jun 2024 12:14:25 +0200 Subject: [PATCH] Little cleanuo --- src/ksf/comp/ksMqttConnector.cpp | 24 +++++++----------------- src/ksf/misc/ksCertUtils.cpp | 2 ++ 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/ksf/comp/ksMqttConnector.cpp b/src/ksf/comp/ksMqttConnector.cpp index b87ab91..7499003 100644 --- a/src/ksf/comp/ksMqttConnector.cpp +++ b/src/ksf/comp/ksMqttConnector.cpp @@ -69,41 +69,30 @@ namespace ksf::comps { auto secureClient{std::make_unique()}; certFingerprint = std::make_unique(); - if (certFingerprint->setup(secureClient.get(), fingerprint)) - { - #if ESP32 - constexpr auto handshakeTimeoutSec{KSF_MQTT_TIMEOUT_MS/KSF_ONE_SEC_MS}; - secureClient->setHandshakeTimeout(handshakeTimeoutSec); - #endif - netClientUq = std::move(secureClient); - } - } - else - { - netClientUq = std::make_unique(); } + else netClientUq = std::make_unique(); /* Whoops, it looks like fingerprint validation failed. */ if (!netClientUq) return; - /* Set MQTT timeouts. */ + /* Set socket timeouts. */ netClientUq->setTimeout(KSF_MQTT_TIMEOUT_MS); #if ESP32 netClientUq->setConnectionTimeout(KSF_MQTT_TIMEOUT_MS); #endif - mqttClientUq = std::make_unique(*netClientUq.get()); - + /* Load MQTT parameters. */ this->login = std::move(login); this->password = std::move(password); this->prefix = std::move(prefix); this->broker = std::move(broker); - - /* Load MQTT port. */ ksf::from_chars(port, portNumber); + + /* Create MQTT client. */ + mqttClientUq = std::make_unique(*netClientUq.get()); } void ksMqttConnector::mqttConnectedInternal() @@ -180,6 +169,7 @@ namespace ksf::comps out += PSTR("[MQTT] Connecting to MQTT broker..."); }); #endif + /* If host is an IP Address, use it. Otherwise use domain name. */ if (IPAddress serverIP; serverIP.fromString(this->broker.c_str())) netClientUq->connect(serverIP, portNumber); else diff --git a/src/ksf/misc/ksCertUtils.cpp b/src/ksf/misc/ksCertUtils.cpp index 5b9fcc2..2446d78 100644 --- a/src/ksf/misc/ksCertUtils.cpp +++ b/src/ksf/misc/ksCertUtils.cpp @@ -54,6 +54,8 @@ namespace ksf { if (fingerprintToBytes(fingerprint, fingerprintBytes, sizeof(fingerprintBytes))) { + constexpr auto handshakeTimeoutSec{KSF_MQTT_TIMEOUT_MS/KSF_ONE_SEC_MS}; + clientSecure->setHandshakeTimeout(KSF_MQTT_TIMEOUT_MS/KSF_ONE_SEC_MS); clientSecure->setInsecure(); return true; }