ESPHome  2024.10.2
mqtt_backend.h
Go to the documentation of this file.
1 #pragma once
2 #include "esphome/core/defines.h"
3 #ifdef USE_MQTT
4 #include <string>
5 #include <map>
7 #include "esphome/core/helpers.h"
8 
9 namespace esphome {
10 namespace mqtt {
11 
12 enum class MQTTClientDisconnectReason : int8_t {
13  TCP_DISCONNECTED = 0,
21 };
22 
24 struct MQTTMessage {
25  std::string topic;
26  std::string payload;
27  uint8_t qos;
28  bool retain;
29 };
30 
31 class MQTTBackend {
32  public:
33  using on_connect_callback_t = void(bool session_present);
35  using on_subscribe_callback_t = void(uint16_t packet_id, uint8_t qos);
36  using on_unsubscribe_callback_t = void(uint16_t packet_id);
37  using on_message_callback_t = void(const char *topic, const char *payload, size_t len, size_t index, size_t total);
38  using on_publish_user_callback_t = void(uint16_t packet_id);
39 
40  virtual void set_keep_alive(uint16_t keep_alive) = 0;
41  virtual void set_client_id(const char *client_id) = 0;
42  virtual void set_clean_session(bool clean_session) = 0;
43  virtual void set_credentials(const char *username, const char *password) = 0;
44  virtual void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) = 0;
45  virtual void set_server(network::IPAddress ip, uint16_t port) = 0;
46  virtual void set_server(const char *host, uint16_t port) = 0;
47  virtual void set_on_connect(std::function<on_connect_callback_t> &&callback) = 0;
48  virtual void set_on_disconnect(std::function<on_disconnect_callback_t> &&callback) = 0;
49  virtual void set_on_subscribe(std::function<on_subscribe_callback_t> &&callback) = 0;
50  virtual void set_on_unsubscribe(std::function<on_unsubscribe_callback_t> &&callback) = 0;
51  virtual void set_on_message(std::function<on_message_callback_t> &&callback) = 0;
52  virtual void set_on_publish(std::function<on_publish_user_callback_t> &&callback) = 0;
53  virtual bool connected() const = 0;
54  virtual void connect() = 0;
55  virtual void disconnect() = 0;
56  virtual bool subscribe(const char *topic, uint8_t qos) = 0;
57  virtual bool unsubscribe(const char *topic) = 0;
58  virtual bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain) = 0;
59 
60  virtual bool publish(const MQTTMessage &message) {
61  return publish(message.topic.c_str(), message.payload.c_str(), message.payload.length(), message.qos,
62  message.retain);
63  }
64 
65  // called from MQTTClient::loop()
66  virtual void loop() {}
67 };
68 
69 } // namespace mqtt
70 } // namespace esphome
71 #endif
internal struct for MQTT messages.
Definition: mqtt_backend.h:24
void(uint16_t packet_id) on_unsubscribe_callback_t
Definition: mqtt_backend.h:36
void(const char *topic, const char *payload, size_t len, size_t index, size_t total) on_message_callback_t
Definition: mqtt_backend.h:37
void(MQTTClientDisconnectReason reason) on_disconnect_callback_t
Definition: mqtt_backend.h:34
void(uint16_t packet_id) on_publish_user_callback_t
Definition: mqtt_backend.h:38
void(bool session_present) on_connect_callback_t
Definition: mqtt_backend.h:33
void(uint16_t packet_id, uint8_t qos) on_subscribe_callback_t
Definition: mqtt_backend.h:35
std::string size_t len
Definition: helpers.h:292
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
virtual bool publish(const MQTTMessage &message)
Definition: mqtt_backend.h:60
uint8_t qos
QoS. Only for last will testaments.
Definition: mqtt_backend.h:27