ESPHome  2024.7.1
mqtt_backend_esp32.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP32
4 
5 #include <string>
6 #include <queue>
7 #include <mqtt_client.h>
9 #include "esphome/core/helpers.h"
10 #include "mqtt_backend.h"
11 
12 namespace esphome {
13 namespace mqtt {
14 
15 struct Event {
16  esp_mqtt_event_id_t event_id;
17  std::vector<char> data;
20  std::string topic;
21  int msg_id;
22  bool retain;
23  int qos;
24  bool dup;
26  esp_mqtt_error_codes_t error_handle;
27 
28  // Construct from esp_mqtt_event_t
29  // Any pointer values that are unsafe to keep are converted to safe copies
30  Event(const esp_mqtt_event_t &event)
31  : event_id(event.event_id),
32  data(event.data, event.data + event.data_len),
33  total_data_len(event.total_data_len),
34  current_data_offset(event.current_data_offset),
35  topic(event.topic, event.topic_len),
36  msg_id(event.msg_id),
37  retain(event.retain),
38  qos(event.qos),
39  dup(event.dup),
40  session_present(event.session_present),
41  error_handle(*event.error_handle) {}
42 };
43 
44 class MQTTBackendESP32 final : public MQTTBackend {
45  public:
46  static const size_t MQTT_BUFFER_SIZE = 4096;
47 
48  void set_keep_alive(uint16_t keep_alive) final { this->keep_alive_ = keep_alive; }
49  void set_client_id(const char *client_id) final { this->client_id_ = client_id; }
50  void set_clean_session(bool clean_session) final { this->clean_session_ = clean_session; }
51 
52  void set_credentials(const char *username, const char *password) final {
53  if (username)
54  this->username_ = username;
55  if (password)
56  this->password_ = password;
57  }
58  void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) final {
59  if (topic)
60  this->lwt_topic_ = topic;
61  this->lwt_qos_ = qos;
62  if (payload)
63  this->lwt_message_ = payload;
64  this->lwt_retain_ = retain;
65  }
66  void set_server(network::IPAddress ip, uint16_t port) final {
67  this->host_ = ip.str();
68  this->port_ = port;
69  }
70  void set_server(const char *host, uint16_t port) final {
71  this->host_ = host;
72  this->port_ = port;
73  }
74  void set_on_connect(std::function<on_connect_callback_t> &&callback) final {
75  this->on_connect_.add(std::move(callback));
76  }
77  void set_on_disconnect(std::function<on_disconnect_callback_t> &&callback) final {
78  this->on_disconnect_.add(std::move(callback));
79  }
80  void set_on_subscribe(std::function<on_subscribe_callback_t> &&callback) final {
81  this->on_subscribe_.add(std::move(callback));
82  }
83  void set_on_unsubscribe(std::function<on_unsubscribe_callback_t> &&callback) final {
84  this->on_unsubscribe_.add(std::move(callback));
85  }
86  void set_on_message(std::function<on_message_callback_t> &&callback) final {
87  this->on_message_.add(std::move(callback));
88  }
89  void set_on_publish(std::function<on_publish_user_callback_t> &&callback) final {
90  this->on_publish_.add(std::move(callback));
91  }
92  bool connected() const final { return this->is_connected_; }
93 
94  void connect() final {
95  if (!is_initalized_) {
96  if (initialize_()) {
97  esp_mqtt_client_start(handler_.get());
98  }
99  }
100  }
101  void disconnect() final {
102  if (is_initalized_)
103  esp_mqtt_client_disconnect(handler_.get());
104  }
105 
106  bool subscribe(const char *topic, uint8_t qos) final {
107  return esp_mqtt_client_subscribe(handler_.get(), topic, qos) != -1;
108  }
109  bool unsubscribe(const char *topic) final { return esp_mqtt_client_unsubscribe(handler_.get(), topic) != -1; }
110 
111  bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain) final {
112 #if defined(USE_MQTT_IDF_ENQUEUE)
113  // use the non-blocking version
114  // it can delay sending a couple of seconds but won't block
115  return esp_mqtt_client_enqueue(handler_.get(), topic, payload, length, qos, retain, true) != -1;
116 #else
117  // might block for several seconds, either due to network timeout (10s)
118  // or if publishing payloads longer than internal buffer (due to message fragmentation)
119  return esp_mqtt_client_publish(handler_.get(), topic, payload, length, qos, retain) != -1;
120 #endif
121  }
122  using MQTTBackend::publish;
123 
124  void loop() final;
125 
126  void set_ca_certificate(const std::string &cert) { ca_certificate_ = cert; }
127  void set_cl_certificate(const std::string &cert) { cl_certificate_ = cert; }
128  void set_cl_key(const std::string &key) { cl_key_ = key; }
129  void set_skip_cert_cn_check(bool skip_check) { skip_cert_cn_check_ = skip_check; }
130 
131  protected:
132  bool initialize_();
133  void mqtt_event_handler_(const Event &event);
134  static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data);
135 
137  void operator()(esp_mqtt_client *client_handler) { esp_mqtt_client_destroy(client_handler); }
138  };
139  using ClientHandler_ = std::unique_ptr<esp_mqtt_client, MqttClientDeleter>;
141 
142  bool is_connected_{false};
143  bool is_initalized_{false};
144 
145  esp_mqtt_client_config_t mqtt_cfg_{};
146 
147  std::string host_;
148  uint16_t port_;
149  std::string username_;
150  std::string password_;
151  std::string lwt_topic_;
152  std::string lwt_message_;
153  uint8_t lwt_qos_;
155  std::string client_id_;
156  uint16_t keep_alive_;
161  bool skip_cert_cn_check_{false};
162 
163  // callbacks
170  std::queue<Event> mqtt_events_;
171 };
172 
173 } // namespace mqtt
174 } // namespace esphome
175 
176 #endif
void operator()(esp_mqtt_client *client_handler)
void loop()
CallbackManager< on_subscribe_callback_t > on_subscribe_
void set_on_disconnect(std::function< on_disconnect_callback_t > &&callback) final
void set_on_publish(std::function< on_publish_user_callback_t > &&callback) final
void set_server(const char *host, uint16_t port) final
Event(const esp_mqtt_event_t &event)
bool unsubscribe(const char *topic) final
void set_server(network::IPAddress ip, uint16_t port) final
optional< std::string > ca_certificate_
void set_on_subscribe(std::function< on_subscribe_callback_t > &&callback) final
void set_client_id(const char *client_id) final
CallbackManager< on_unsubscribe_callback_t > on_unsubscribe_
void set_cl_key(const std::string &key)
void set_ca_certificate(const std::string &cert)
std::unique_ptr< esp_mqtt_client, MqttClientDeleter > ClientHandler_
void set_credentials(const char *username, const char *password) final
void set_keep_alive(uint16_t keep_alive) final
esp_mqtt_error_codes_t error_handle
void set_clean_session(bool clean_session) final
std::vector< char > data
void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) final
void set_on_unsubscribe(std::function< on_unsubscribe_callback_t > &&callback) final
void set_skip_cert_cn_check(bool skip_check)
bool subscribe(const char *topic, uint8_t qos) final
CallbackManager< on_connect_callback_t > on_connect_
void set_on_message(std::function< on_message_callback_t > &&callback) final
esp_mqtt_event_id_t event_id
bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain) final
CallbackManager< on_message_callback_t > on_message_
CallbackManager< on_publish_user_callback_t > on_publish_
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_cl_certificate(const std::string &cert)
uint8_t event_id
Definition: tt21100.cpp:15
CallbackManager< on_disconnect_callback_t > on_disconnect_
optional< std::string > cl_certificate_
optional< std::string > cl_key_
void set_on_connect(std::function< on_connect_callback_t > &&callback) final
virtual bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain)=0