JBHaMqttDiscovery
Loading...
Searching...
No Matches
MqttTransport Class Referenceabstract

Abstract MQTT transport interface. More...

#include <MqttTransport.h>

Inheritance diagram for MqttTransport:
AsyncMqttClientTransport PubSubClientTransport

Public Member Functions

virtual bool connected () const =0
 Check whether the MQTT client is currently connected.
virtual bool publish (const char *topic, const uint8_t *payload, size_t len, bool retained, uint8_t qos)=0
 Publish an MQTT message.
virtual void setOnConnect (void(*cb)(void *), void *ctx)=0
 Register a callback invoked when the MQTT connection is established.
virtual void setServer (const char *host, uint16_t port, const char *user=nullptr, const char *pass=nullptr)=0
 Set MQTT server and credentials.
virtual void setServer (const std::string &host, uint16_t port, const std::string &user="", const std::string &pass="")=0
 Set MQTT server and credentials (std::string version).
virtual void tick ()
 Periodic processing hook (optional).
void setLogger (JBLogger *logger)
 Set the logger for this transport.

Protected Attributes

JBLogger * log = nullptr

Detailed Description

Abstract MQTT transport interface.

This interface decouples the Home Assistant Discovery logic from the underlying MQTT client implementation.

It supports both:

  • synchronous / polling MQTT clients (e.g. PubSubClient)
  • asynchronous / event-driven MQTT clients (e.g. AsyncMqttClient)

The transport is responsible only for:

  • reporting connection state
  • publishing MQTT messages
  • notifying when a connection is (re)established

It does NOT:

  • manage Wi-Fi
  • manage MQTT configuration
  • perform polling (except via tick() for sync clients)

Member Function Documentation

◆ connected()

virtual bool MqttTransport::connected ( ) const
pure virtual

Check whether the MQTT client is currently connected.

Returns
true if connected, false otherwise

Implemented in AsyncMqttClientTransport, and PubSubClientTransport.

◆ publish()

virtual bool MqttTransport::publish ( const char * topic,
const uint8_t * payload,
size_t len,
bool retained,
uint8_t qos )
pure virtual

Publish an MQTT message.

Parameters
topicMQTT topic (null-terminated string)
payloadPointer to payload bytes (may be nullptr)
lenPayload length in bytes
retainedWhether the message should be retained
qosRequested QoS level (best-effort for some clients)
Returns
true if the publish request was accepted, false otherwise
Note
Passing payload == nullptr publishes an empty payload, which is required for Home Assistant entity removal.

Implemented in AsyncMqttClientTransport, and PubSubClientTransport.

◆ setLogger()

void MqttTransport::setLogger ( JBLogger * logger)
inline

Set the logger for this transport.

Parameters
loggerPointer to a JBLogger instance.

◆ setOnConnect()

virtual void MqttTransport::setOnConnect ( void(* cb )(void *),
void * ctx )
pure virtual

Register a callback invoked when the MQTT connection is established.

For asynchronous transports, this is wired directly to the MQTT client's onConnect handler.

For synchronous transports, this callback is invoked when a rising-edge connection transition is detected via tick().

Parameters
cbCallback function
ctxUser context pointer passed back to the callback

Implemented in AsyncMqttClientTransport, and PubSubClientTransport.

◆ setServer() [1/2]

virtual void MqttTransport::setServer ( const char * host,
uint16_t port,
const char * user = nullptr,
const char * pass = nullptr )
pure virtual

Set MQTT server and credentials.

Parameters
hostMQTT host/IP
portMQTT port
userMQTT username (optional)
passMQTT password (optional)

Implemented in AsyncMqttClientTransport, and PubSubClientTransport.

◆ setServer() [2/2]

virtual void MqttTransport::setServer ( const std::string & host,
uint16_t port,
const std::string & user = "",
const std::string & pass = "" )
pure virtual

Set MQTT server and credentials (std::string version).

Parameters
hostMQTT host/IP
portMQTT port
userMQTT username
passMQTT password

Implemented in AsyncMqttClientTransport, and PubSubClientTransport.

◆ tick()

virtual void MqttTransport::tick ( )
inlinevirtual

Periodic processing hook (optional).

This is required only for synchronous MQTT clients that do not provide a native connection callback.

The host firmware should call this regularly (e.g. from loop()) if using such a transport.

Reimplemented in PubSubClientTransport.


The documentation for this class was generated from the following file: