#include <MqttClient.h>

Inheritance diagram for MqttClient:
[legend]
Collaboration diagram for MqttClient:
[legend]

Public Member Functions

 MqttClient (bool withDefaultPayloadParser=true, bool autoDestruct=false)
 
 ~MqttClient ()
 
void setKeepAlive (uint16_t seconds)
 Sets keep-alive time. That information is sent during connection to the server. More...
 
void setPingRepeatTime (unsigned seconds)
 
bool setWill (const String &topic, const String &message, uint8_t flags=0)
 
bool connect (const Url &url, const String &uniqueClientName)
 Connect to a MQTT server. More...
 
bool publish (const String &topic, const String &message, uint8_t flags=0)
 
bool publish (const String &topic, IDataSourceStream *stream, uint8_t flags=0)
 
bool subscribe (const String &topic)
 
bool unsubscribe (const String &topic)
 
void setEventHandler (mqtt_type_t type, MqttDelegate handler)
 
void setPayloadParser (MqttPayloadParser payloadParser=nullptr)
 Sets or clears a payload parser (for PUBLISH messages from the server to us) More...
 
void setConnectedHandler (MqttDelegate handler)
 Sets a handler to be called after successful MQTT connection. More...
 
void setPublishedHandler (MqttDelegate handler)
 Sets a handler to be called after receiving confirmation from the server for a published message from the client. More...
 
void setMessageHandler (MqttDelegate handler)
 Sets a handler to be called after receiving a PUBLISH message from the server. More...
 
void setDisconnectHandler (TcpClientCompleteDelegate handler)
 Sets a handler to be called on disconnect from the server. More...
 
bool setWill (const String &topic, const String &message, int QoS, bool retained=false)
 
bool publishWithQoS (const String &topic, const String &message, int QoS, bool retained=false, MqttMessageDeliveredCallback onDelivery=nullptr)
 
void setCallback (MqttStringSubscriptionCallback subscriptionCallback=nullptr)
 Provide a function to be called when a message is received from the broker. More...
 

Protected Member Functions

void onReadyToSendData (TcpConnectionEvent sourceEvent) override
 
void onFinished (TcpClientState finishState) override
 
bool connect (const String &server, int port, bool useSsl=false) override
 
bool connect (IpAddress addr, uint16_t port, bool useSsl=false) override
 
void close () override
 
void setReceiveDelegate (TcpClientDataDelegate receiveCb=nullptr)
 Set or clear the callback for received data. More...
 
void setCompleteDelegate (TcpClientCompleteDelegate completeCb=nullptr)
 Set or clear the callback for connection close. More...
 
bool send (const char *data, uint16_t len, bool forceCloseAfterSent=false)
 
bool sendString (const String &data, bool forceCloseAfterSent=false)
 
bool isProcessing ()
 
TcpClientState getConnectionState ()
 
void setCloseAfterSent (bool ignoreIncomingData=false)
 
err_t onConnected (err_t err) override
 
err_t onReceive (pbuf *buf) override
 
err_t onSent (uint16_t len) override
 
void onError (err_t err) override
 
void pushAsyncPart ()
 
void freeStreams ()
 
void setBuffer (ReadWriteStream *stream)
 
int writeString (const char *data, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
 
int writeString (const String &data, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
 
virtual int write (const char *data, int len, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
 Base write operation. More...
 
int write (IDataSourceStream *stream)
 
uint16_t getAvailableWriteSize ()
 
void flush ()
 
void setTimeOut (uint16_t waitTimeOut)
 
IpAddress getRemoteIp () const
 
uint16_t getRemotePort () const
 
void setDestroyedDelegate (TcpConnectionDestroyedDelegate destroyedDelegate)
 Sets a callback to be called when the object instance is destroyed. More...
 
void setSslInitHandler (Ssl::Session::InitDelegate handler)
 Set the SSL session initialisation callback. More...
 
bool setSslConnection (Ssl::Connection *connection)
 
Ssl::SessiongetSsl ()
 Get a pointer to the current SSL session object. More...
 
void initialize (tcp_pcb *pcb)
 
bool internalConnect (IpAddress addr, uint16_t port)
 
bool sslCreateSession ()
 
virtual void sslInitSession (Ssl::Session &session)
 Override in inherited classes to perform custom session initialisation. More...
 
virtual err_t onPoll ()
 
void trySend (TcpConnectionEvent event)
 
err_t internalOnConnected (err_t err)
 
err_t internalOnReceive (pbuf *p, err_t err)
 
err_t internalOnSent (uint16_t len)
 
err_t internalOnPoll ()
 
void internalOnError (err_t err)
 
void internalOnDnsResponse (const char *name, LWIP_IP_ADDR_T *ipaddr, int port)
 

Protected Attributes

ReadWriteStreambuffer = nullptr
 Used internally to buffer arbitrary data via send() methods. More...
 
IDataSourceStreamstream = nullptr
 The currently active stream being sent. More...
 
tcp_pcb * tcp = nullptr
 
uint16_t sleep = 0
 
uint16_t timeOut = USHRT_MAX
 By default a TCP connection does not have a time out. More...
 
bool canSend = true
 
bool autoSelfDestruct = true
 
Ssl::Sessionssl = nullptr
 
Ssl::Session::InitDelegate sslInit
 
bool useSsl = false
 

Constructor & Destructor Documentation

◆ MqttClient()

MqttClient::MqttClient ( bool  withDefaultPayloadParser = true,
bool  autoDestruct = false 
)

◆ ~MqttClient()

MqttClient::~MqttClient ( )

Member Function Documentation

◆ close()

void TcpClient::close ( )
overridevirtualinherited

Reimplemented from TcpConnection.

◆ connect() [1/3]

bool TcpClient::connect ( const String server,
int  port,
bool  useSsl = false 
)
overridevirtualinherited

Reimplemented from TcpConnection.

◆ connect() [2/3]

bool TcpClient::connect ( IpAddress  addr,
uint16_t  port,
bool  useSsl = false 
)
overridevirtualinherited

Reimplemented from TcpConnection.

◆ connect() [3/3]

bool MqttClient::connect ( const Url url,
const String uniqueClientName 
)

Connect to a MQTT server.

Parameters
urlURL in the form "mqtt://user:password@server:port" or "mqtts://user:password@server:port"
uniqueClientName
Return values
bool

◆ flush()

void TcpConnection::flush ( )
inherited

◆ freeStreams()

void TcpClient::freeStreams ( )
protectedinherited

◆ getAvailableWriteSize()

uint16_t TcpConnection::getAvailableWriteSize ( )
inlineinherited

◆ getConnectionState()

TcpClientState TcpClient::getConnectionState ( )
inlineinherited

◆ getRemoteIp()

IpAddress TcpConnection::getRemoteIp ( ) const
inlineinherited

◆ getRemotePort()

uint16_t TcpConnection::getRemotePort ( ) const
inlineinherited

◆ getSsl()

Ssl::Session* TcpConnection::getSsl ( )
inlineinherited

Get a pointer to the current SSL session object.

Note that this is typically used so we can query properties of an established session. If you need to change session parameters this must be done via setSslInitHandler.

◆ initialize()

void TcpConnection::initialize ( tcp_pcb *  pcb)
protectedinherited

◆ internalConnect()

bool TcpConnection::internalConnect ( IpAddress  addr,
uint16_t  port 
)
protectedinherited

◆ internalOnConnected()

err_t TcpConnection::internalOnConnected ( err_t  err)
protectedinherited

◆ internalOnDnsResponse()

void TcpConnection::internalOnDnsResponse ( const char *  name,
LWIP_IP_ADDR_T ipaddr,
int  port 
)
protectedinherited

◆ internalOnError()

void TcpConnection::internalOnError ( err_t  err)
protectedinherited

◆ internalOnPoll()

err_t TcpConnection::internalOnPoll ( )
protectedinherited

◆ internalOnReceive()

err_t TcpConnection::internalOnReceive ( pbuf *  p,
err_t  err 
)
protectedinherited

◆ internalOnSent()

err_t TcpConnection::internalOnSent ( uint16_t  len)
protectedinherited

◆ isProcessing()

bool TcpClient::isProcessing ( )
inlineinherited

◆ onConnected()

err_t TcpClient::onConnected ( err_t  err)
overrideprotectedvirtualinherited

Reimplemented from TcpConnection.

◆ onError()

void TcpClient::onError ( err_t  err)
overrideprotectedvirtualinherited

Reimplemented from TcpConnection.

◆ onFinished()

void MqttClient::onFinished ( TcpClientState  finishState)
overrideprotectedvirtual

Reimplemented from TcpClient.

◆ onPoll()

virtual err_t TcpConnection::onPoll ( )
protectedvirtualinherited

◆ onReadyToSendData()

void MqttClient::onReadyToSendData ( TcpConnectionEvent  sourceEvent)
overrideprotectedvirtual

Reimplemented from TcpConnection.

◆ onReceive()

err_t TcpClient::onReceive ( pbuf *  buf)
overrideprotectedvirtualinherited

Reimplemented from TcpConnection.

◆ onSent()

err_t TcpClient::onSent ( uint16_t  len)
overrideprotectedvirtualinherited

Reimplemented from TcpConnection.

◆ publish() [1/2]

bool MqttClient::publish ( const String topic,
const String message,
uint8_t  flags = 0 
)

◆ publish() [2/2]

bool MqttClient::publish ( const String topic,
IDataSourceStream stream,
uint8_t  flags = 0 
)

◆ publishWithQoS()

bool MqttClient::publishWithQoS ( const String topic,
const String message,
int  QoS,
bool  retained = false,
MqttMessageDeliveredCallback  onDelivery = nullptr 
)
inline

bool publish(String& topic, String& message, bool retained = false) Use publish(const String& topic, const String& message, uint8_t flags = 0) instead.

Todo:
deprecate: Use publish(const String& topic, const String& message, uint8_t flags = 0) instead. If you want to have a callback that should be triggered on successful delivery of messages then use setEventHandler(MQTT_TYPE_PUBACK, youCallback) instead.

◆ pushAsyncPart()

void TcpClient::pushAsyncPart ( )
protectedinherited

◆ send()

bool TcpClient::send ( const char *  data,
uint16_t  len,
bool  forceCloseAfterSent = false 
)
inherited

◆ sendString()

bool TcpClient::sendString ( const String data,
bool  forceCloseAfterSent = false 
)
inlineinherited

◆ setBuffer()

void TcpClient::setBuffer ( ReadWriteStream stream)
protectedinherited

◆ setCallback()

void MqttClient::setCallback ( MqttStringSubscriptionCallback  subscriptionCallback = nullptr)
inline

Provide a function to be called when a message is received from the broker.

Todo:
deprecate: Use setEventHandler(MQTT_TYPE_PUBLISH, MqttDelegate handler) instead.

◆ setCloseAfterSent()

void TcpClient::setCloseAfterSent ( bool  ignoreIncomingData = false)
inlineinherited

Schedules the connection to get closed after the data is sent

Parameters
ignoreIncomingDatawhen that flag is set the connection will start ignoring incoming data.

◆ setCompleteDelegate()

void TcpClient::setCompleteDelegate ( TcpClientCompleteDelegate  completeCb = nullptr)
inlineinherited

Set or clear the callback for connection close.

Parameters
completeCbcallback delegate or nullptr

◆ setConnectedHandler()

void MqttClient::setConnectedHandler ( MqttDelegate  handler)
inline

Sets a handler to be called after successful MQTT connection.

Parameters
handler

◆ setDestroyedDelegate()

void TcpConnection::setDestroyedDelegate ( TcpConnectionDestroyedDelegate  destroyedDelegate)
inlineinherited

Sets a callback to be called when the object instance is destroyed.

Parameters
destroyedDelegate

◆ setDisconnectHandler()

void MqttClient::setDisconnectHandler ( TcpClientCompleteDelegate  handler)
inline

Sets a handler to be called on disconnect from the server.

Parameters
handler

◆ setEventHandler()

void MqttClient::setEventHandler ( mqtt_type_t  type,
MqttDelegate  handler 
)
inline

◆ setKeepAlive()

void MqttClient::setKeepAlive ( uint16_t  seconds)
inline

Sets keep-alive time. That information is sent during connection to the server.

Parameters
seconds

◆ setMessageHandler()

void MqttClient::setMessageHandler ( MqttDelegate  handler)
inline

Sets a handler to be called after receiving a PUBLISH message from the server.

Parameters
handler

◆ setPayloadParser()

void MqttClient::setPayloadParser ( MqttPayloadParser  payloadParser = nullptr)
inline

Sets or clears a payload parser (for PUBLISH messages from the server to us)

Note
We no longer have size limitation for incoming or outgoing messages but in order to prevent running out of memory we have a "sane" payload parser that will read up to 1K of payload

◆ setPingRepeatTime()

void MqttClient::setPingRepeatTime ( unsigned  seconds)

Sets the interval in which to ping the remote server if there was no activity

Parameters
seconds

◆ setPublishedHandler()

void MqttClient::setPublishedHandler ( MqttDelegate  handler)
inline

Sets a handler to be called after receiving confirmation from the server for a published message from the client.

Parameters
handler

◆ setReceiveDelegate()

void TcpClient::setReceiveDelegate ( TcpClientDataDelegate  receiveCb = nullptr)
inlineinherited

Set or clear the callback for received data.

Parameters
receiveCbcallback delegate or nullptr

◆ setSslConnection()

bool TcpConnection::setSslConnection ( Ssl::Connection connection)
inlineinherited

◆ setSslInitHandler()

void TcpConnection::setSslInitHandler ( Ssl::Session::InitDelegate  handler)
inlineinherited

Set the SSL session initialisation callback.

Parameters
handler

◆ setTimeOut()

void TcpConnection::setTimeOut ( uint16_t  waitTimeOut)
inherited

◆ setWill() [1/2]

bool MqttClient::setWill ( const String topic,
const String message,
uint8_t  flags = 0 
)

Sets last will and testament

Parameters
topic
message
flagsQoS, retain, etc flags
Return values
bool

◆ setWill() [2/2]

bool MqttClient::setWill ( const String topic,
const String message,
int  QoS,
bool  retained = false 
)
inline

◆ sslCreateSession()

bool TcpConnection::sslCreateSession ( )
protectedinherited

◆ sslInitSession()

virtual void TcpConnection::sslInitSession ( Ssl::Session session)
inlineprotectedvirtualinherited

Override in inherited classes to perform custom session initialisation.

Called when TCP connection is established before initiating handshake.

Reimplemented in HttpClientConnection.

◆ subscribe()

bool MqttClient::subscribe ( const String topic)

◆ trySend()

void TcpConnection::trySend ( TcpConnectionEvent  event)
inlineprotectedinherited

◆ unsubscribe()

bool MqttClient::unsubscribe ( const String topic)

◆ write() [1/2]

virtual int TcpConnection::write ( const char *  data,
int  len,
uint8_t  apiflags = TCP_WRITE_FLAG_COPY 
)
virtualinherited

Base write operation.

Parameters
data
len
apiflagsTCP_WRITE_FLAG_COPY, TCP_WRITE_FLAG_MORE
Return values
int-1 on error

Reimplemented in FtpDataStream.

◆ write() [2/2]

int TcpConnection::write ( IDataSourceStream stream)
inherited

◆ writeString() [1/2]

int TcpConnection::writeString ( const char *  data,
uint8_t  apiflags = TCP_WRITE_FLAG_COPY 
)
inlineinherited

◆ writeString() [2/2]

int TcpConnection::writeString ( const String data,
uint8_t  apiflags = TCP_WRITE_FLAG_COPY 
)
inlineinherited

Member Data Documentation

◆ autoSelfDestruct

bool TcpConnection::autoSelfDestruct = true
protectedinherited

◆ buffer

ReadWriteStream* TcpClient::buffer = nullptr
protectedinherited

Used internally to buffer arbitrary data via send() methods.

◆ canSend

bool TcpConnection::canSend = true
protectedinherited

◆ sleep

uint16_t TcpConnection::sleep = 0
protectedinherited

◆ ssl

Ssl::Session* TcpConnection::ssl = nullptr
protectedinherited

◆ sslInit

Ssl::Session::InitDelegate TcpConnection::sslInit
protectedinherited

◆ stream

IDataSourceStream* TcpClient::stream = nullptr
protectedinherited

The currently active stream being sent.

◆ tcp

tcp_pcb* TcpConnection::tcp = nullptr
protectedinherited

◆ timeOut

uint16_t TcpConnection::timeOut = USHRT_MAX
protectedinherited

By default a TCP connection does not have a time out.

◆ useSsl

bool TcpConnection::useSsl = false
protectedinherited

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