Doxygen with github
Loading...
Searching...
No Matches
ksMqttConnector.h
1/*
2 * Copyright (c) 2020-2026, Krzysztof Strehlau
3 *
4 * This file is a part of the ksIotFrameworkLib IoT library.
5 * All licensing information can be found inside LICENSE.md file.
6 *
7 * https://github.com/cziter15/ksIotFrameworkLib/blob/master/LICENSE
8 */
9
10#pragma once
11
12#include <cstdint>
13#include <string>
14#include <string_view>
15
16#include "../ksComponent.h"
17#include "../evt/ksEvent.h"
18#include "../misc/ksSimpleTimer.h"
19#include "../misc/ksDomainQuery.h"
20
21#include "../misc/ksPinnedTlsClient.h"
22
23class PubSubClient;
24
25namespace ksf::comps
26{
27 class ksWifiConnector;
28
38 {
39 KSF_RTTI_DECLARATIONS(ksMqttConnector, ksComponent)
40
41 protected:
42#if APP_LOG_ENABLED
43 ksApplication* app{nullptr};
44#endif
45
47 std::unique_ptr<misc::ksNetworkClient_t> netClientUq;
48 std::unique_ptr<PubSubClient> mqttClientUq;
49 std::weak_ptr<ksWifiConnector> wifiConnWp;
51
53 uint32_t reconnectCounter{0};
54
55 struct
56 {
59 bool wasConnected : 1;
60 }
61 bitflags = {true, false, true};
62
63 std::string login;
64 std::string password;
65 std::string prefix;
66 uint16_t portNumber{1883};
67
73 bool connectToBroker();
74
81
88 void mqttMessageInternal(const char* topic, const uint8_t* payload, uint32_t length);
89
90 public:
91 enum class QosLevel
92 {
93 QOS_AT_LEAST_ONCE,
94 QOS_EXACTLY_ONCE
95 };
96
97 DECLARE_KS_EVENT(onDeviceMessage, const std::string_view&, const std::string_view&)
98 DECLARE_KS_EVENT(onAnyMessage, const std::string_view&, const std::string_view&)
99
100 DECLARE_KS_EVENT(onConnected)
101 DECLARE_KS_EVENT(onDisconnected)
102
110
114 virtual ~ksMqttConnector();
115
122 bool init(ksApplication* app) override;
123
132 bool postInit(ksApplication* app) override;
133
138 bool loop(ksApplication* app) override;
139
144 bool isConnected() const;
145
150 uint32_t getConnectionTimeSeconds() const;
151
156 uint32_t getReconnectCounter() const { return reconnectCounter; }
157
164 void subscribe(const std::string& topic, bool skipDevicePrefix = false, ksMqttConnector::QosLevel = ksMqttConnector::QosLevel::QOS_AT_LEAST_ONCE);
165
171 void unsubscribe(const std::string& topic, bool skipDevicePrefix = false);
172
180 void publish(const std::string& topic, const std::string& payload, bool retain = false, bool skipDevicePrefix = false);
181
190 void setupConnection(std::string broker, const std::string& port, std::string login, std::string password, std::string prefix, const std::string& fingerprint);
191 };
192}
A component responsible for managing MQTT connections.
Definition ksMqttConnector.h:38
std::weak_ptr< ksWifiConnector > wifiConnWp
Weak pointer to WiFi connector.
Definition ksMqttConnector.h:49
std::string prefix
Saved MQTT prefix.
Definition ksMqttConnector.h:65
misc::ksDomainQuery domainResolver
Domain query used to resolve MQTT broker address.
Definition ksMqttConnector.h:46
misc::ksSimpleTimer reconnectTimer
Timer that counts time between reconnection attempts.
Definition ksMqttConnector.h:50
void subscribe(const std::string &topic, bool skipDevicePrefix=false, ksMqttConnector::QosLevel=ksMqttConnector::QosLevel::QOS_AT_LEAST_ONCE)
Subscribes to MQTT topic.
Definition ksMqttConnector.cpp:136
uint32_t getReconnectCounter() const
Retrieves MQTT reconnect counter.
Definition ksMqttConnector.h:156
bool init(ksApplication *app) override
Instantiates the MQTT connector component.
Definition ksMqttConnector.cpp:43
uint64_t lastSuccessConnectionTime
Time of connection to MQTT broker in seconds.
Definition ksMqttConnector.h:52
std::unique_ptr< PubSubClient > mqttClientUq
Shared pointer to PubSubClient used to connect to MQTT.
Definition ksMqttConnector.h:48
void setupConnection(std::string broker, const std::string &port, std::string login, std::string password, std::string prefix, const std::string &fingerprint)
Sets up MQTT connection.
Definition ksMqttConnector.cpp:67
std::shared_ptr< ksf::evt::ksEvent< const std::string_view &, const std::string_view & > > onAnyMessage
onAnyMessage event that user can bind to.
Definition ksMqttConnector.h:98
bool loop(ksApplication *app) override
Executes MQTT connection logic.
Definition ksMqttConnector.cpp:241
std::string login
Saved MQTT login.
Definition ksMqttConnector.h:63
bool usePersistentSession
Use persistent session or not.
Definition ksMqttConnector.h:58
uint32_t getConnectionTimeSeconds() const
Retrieves connection time in seconds.
Definition ksMqttConnector.cpp:294
bool sendConnectionStatus
Send connection status to MQTT or not.
Definition ksMqttConnector.h:57
void unsubscribe(const std::string &topic, bool skipDevicePrefix=false)
Unsubscribes the MQTT topic.
Definition ksMqttConnector.cpp:148
bool isConnected() const
Retrieves connection state.
Definition ksMqttConnector.cpp:289
std::shared_ptr< ksf::evt::ksEvent<> > onDisconnected
onDisconnected event that user can bind to.
Definition ksMqttConnector.h:101
void publish(const std::string &topic, const std::string &payload, bool retain=false, bool skipDevicePrefix=false)
Publishes a message to the MQTT topic.
Definition ksMqttConnector.cpp:159
std::unique_ptr< misc::ksNetworkClient_t > netClientUq
Network client owned by the MQTT connector.
Definition ksMqttConnector.h:47
uint16_t portNumber
Saved MQTT port number.
Definition ksMqttConnector.h:66
void mqttConnectedInternal()
Connects to the MQTT broker (internal function).
Definition ksMqttConnector.cpp:98
bool connectToBroker()
Connects to the MQTT broker.
Definition ksMqttConnector.cpp:183
bool postInit(ksApplication *app) override
Method that handles component post-initialization.
Definition ksMqttConnector.cpp:61
std::shared_ptr< ksf::evt::ksEvent<> > onConnected
onConnected event that user can bind to.
Definition ksMqttConnector.h:100
void mqttMessageInternal(const char *topic, const uint8_t *payload, uint32_t length)
Called when MQTT message arrives (internal function).
Definition ksMqttConnector.cpp:105
std::string password
Saved MQTT password.
Definition ksMqttConnector.h:64
uint32_t reconnectCounter
MQTT reconnection counter.
Definition ksMqttConnector.h:53
bool wasConnected
True if connected in previous loop.
Definition ksMqttConnector.h:59
std::shared_ptr< ksf::evt::ksEvent< const std::string_view &, const std::string_view & > > onDeviceMessage
onDeviceMessage event that user can bind to.
Definition ksMqttConnector.h:97
A class that serves as the base for user-defined applications.
Definition ksApplication.h:46
Base component class.
Definition ksComponent.h:36
Class for resolving domain names to IP addresses using DNS queries.
Definition ksDomainQuery.h:38
A simple timer class that does not rely on component architecture.
Definition ksSimpleTimer.h:27