Go to the documentation of this file.
21 #include "mqtt-codec/src/message.h"
22 #include "mqtt-codec/src/serialiser.h"
23 #include "mqtt-codec/src/parser.h"
33 #ifndef MQTT_REQUEST_POOL_SIZE
34 #define MQTT_REQUEST_POOL_SIZE 10
37 #define MQTT_CLIENT_CONNECTED bit(1)
39 #define MQTT_FLAG_RETAINED 1
49 MqttClient(
bool withDefaultPayloadParser =
true,
bool autoDestruct =
false);
60 if(seconds < pingRepeatTime) {
71 seconds = std::min(keepAlive, seconds);
72 if(seconds != pingRepeatTime) {
73 pingRepeatTime = seconds;
74 pingTimer.reset(seconds);
133 eventHandlers[type] = handler;
144 this->payloadParser = payloadParser;
157 static uint8_t getFlags(mqtt_qos_t QoS, mqtt_retain_t retain = MQTT_RETAIN_FALSE, mqtt_dup_t dup = MQTT_DUP_FALSE)
159 return (retain + (QoS << 1) + (dup << 3));
169 eventHandlers[MQTT_TYPE_CONNACK] = handler;
180 eventHandlers[MQTT_TYPE_PUBACK] = handler;
181 eventHandlers[MQTT_TYPE_PUBREC] = handler;
191 eventHandlers[MQTT_TYPE_PUBLISH] = handler;
221 virtual bool onTcpReceive(
TcpClient& client,
char* data,
int size);
224 static int staticOnMessageBegin(
void* user_data, mqtt_message_t* message);
225 static int staticOnDataBegin(
void* user_data, mqtt_message_t* message);
226 static int staticOnDataPayload(
void* user_data, mqtt_message_t* message,
const char* data,
size_t length);
227 static int staticOnDataEnd(
void* user_data, mqtt_message_t* message);
228 static int staticOnMessageEnd(
void* user_data, mqtt_message_t* message);
229 int onMessageEnd(mqtt_message_t* message);
236 HandlerMap eventHandlers;
250 mqtt_message_t connectMessage;
251 bool connectQueued =
false;
252 mqtt_message_t* outgoingMessage =
nullptr;
253 mqtt_message_t incomingMessage;
256 mqtt_serialiser_t serialiser;
257 static const mqtt_parser_callbacks_t callbacks;
258 mqtt_parser_t parser;
static uint8_t getFlags(mqtt_qos_t QoS, mqtt_retain_t retain=MQTT_RETAIN_FALSE, mqtt_dup_t dup=MQTT_DUP_FALSE)
Compute the flags value.
Definition: MqttClient.h:157
Base class for read-only stream.
Definition: DataSourceStream.h:45
TcpClientState getConnectionState()
Definition: TcpClient.h:126
void onFinished(TcpClientState finishState) override
void onReadyToSendData(TcpConnectionEvent sourceEvent) override
bool unsubscribe(const String &topic)
Unsubscribe from a topic.
IDataSourceStream * stream
The currently active stream being sent.
Definition: TcpClient.h:165
void setCompleteDelegate(TcpClientCompleteDelegate completeCb=nullptr)
Set or clear the callback for connection close.
Definition: TcpClient.h:97
void setEventHandler(mqtt_type_t type, MqttDelegate handler)
Register a callback function to be invoked on incoming event notification.
Definition: MqttClient.h:131
Class to manage URL instance.
Definition: Url.h:66
void setConnectedHandler(MqttDelegate handler)
Sets a handler to be called after successful MQTT connection.
Definition: MqttClient.h:167
Template class to implement a polled timer.
Definition: PolledTimer.h:67
bool isProcessing()
Definition: TcpClient.h:121
The String class.
Definition: WString.h:136
TcpConnectionEvent
Definition: TcpConnection.h:26
bool publish(const String &topic, const String &message, uint8_t flags=0)
Publish a message.
Definition: MqttPayloadParser.h:29
Definition: MqttClient.h:46
Definition: TcpClient.h:45
void setDisconnectHandler(TcpClientCompleteDelegate handler)
Sets a handler to be called on disconnect from the server.
Definition: MqttClient.h:199
Ssl::Session * getSsl()
Get a pointer to the current SSL session object.
Definition: TcpConnection.h:148
@ eMCS_Ready
Definition: MqttClient.h:31
void setPayloadParser(MqttPayloadParser payloadParser=nullptr)
Sets or clears a payload parser (for PUBLISH messages from the server to us)
Definition: MqttClient.h:142
IpAddress getRemoteIp() const
Definition: TcpConnection.h:102
MqttClient(bool withDefaultPayloadParser=true, bool autoDestruct=false)
void setMessageHandler(MqttDelegate handler)
Sets a handler to be called after receiving a PUBLISH message from the server.
Definition: MqttClient.h:189
bool subscribe(const String &topic)
Subscribe to a topic.
void setPingRepeatTime(uint16_t seconds)
Definition: MqttClient.h:69
bool setWill(const String &topic, const String &message, uint8_t flags=0)
uint16_t getRemotePort() const
Definition: TcpConnection.h:107
TcpClientState
Definition: TcpClient.h:28
bool connect(const Url &url, const String &uniqueClientName)
Connect to a MQTT server.
@ eMCS_SendingData
Definition: MqttClient.h:31
MqttClientState
Definition: MqttClient.h:31
void setKeepAlive(uint16_t seconds)
Sets keep-alive time. That information is sent during connection to the server.
Definition: MqttClient.h:57
Definition: Delegate.h:20
void setSslInitHandler(Ssl::Session::InitDelegate handler)
Set the SSL session initialisation callback.
Definition: TcpConnection.h:125
void setPublishedHandler(MqttDelegate handler)
Sets a handler to be called after receiving confirmation from the server for a published message from...
Definition: MqttClient.h:178