ESPHome  2022.12.8
mqtt_backend_idf.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP_IDF
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 class MQTTBackendIDF final : public MQTTBackend {
16  public:
17  static const size_t MQTT_BUFFER_SIZE = 4096;
18 
19  void set_keep_alive(uint16_t keep_alive) final { this->keep_alive_ = keep_alive; }
20  void set_client_id(const char *client_id) final { this->client_id_ = client_id; }
21  void set_clean_session(bool clean_session) final { this->clean_session_ = clean_session; }
22 
23  void set_credentials(const char *username, const char *password) final {
24  if (username)
25  this->username_ = username;
26  if (password)
27  this->password_ = password;
28  }
29  void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) final {
30  if (topic)
31  this->lwt_topic_ = topic;
32  this->lwt_qos_ = qos;
33  if (payload)
34  this->lwt_message_ = payload;
35  this->lwt_retain_ = retain;
36  }
37  void set_server(network::IPAddress ip, uint16_t port) final {
38  this->host_ = ip.str();
39  this->port_ = port;
40  }
41  void set_server(const char *host, uint16_t port) final {
42  this->host_ = host;
43  this->port_ = port;
44  }
45  void set_on_connect(std::function<on_connect_callback_t> &&callback) final {
46  this->on_connect_.add(std::move(callback));
47  }
48  void set_on_disconnect(std::function<on_disconnect_callback_t> &&callback) final {
49  this->on_disconnect_.add(std::move(callback));
50  }
51  void set_on_subscribe(std::function<on_subscribe_callback_t> &&callback) final {
52  this->on_subscribe_.add(std::move(callback));
53  }
54  void set_on_unsubscribe(std::function<on_unsubscribe_callback_t> &&callback) final {
55  this->on_unsubscribe_.add(std::move(callback));
56  }
57  void set_on_message(std::function<on_message_callback_t> &&callback) final {
58  this->on_message_.add(std::move(callback));
59  }
60  void set_on_publish(std::function<on_publish_user_callback_t> &&callback) final {
61  this->on_publish_.add(std::move(callback));
62  }
63  bool connected() const final { return this->is_connected_; }
64 
65  void connect() final {
66  if (!is_initalized_) {
67  if (initialize_()) {
68  esp_mqtt_client_start(handler_.get());
69  }
70  }
71  }
72  void disconnect() final {
73  if (is_initalized_)
74  esp_mqtt_client_disconnect(handler_.get());
75  }
76 
77  bool subscribe(const char *topic, uint8_t qos) final {
78  return esp_mqtt_client_subscribe(handler_.get(), topic, qos) != -1;
79  }
80  bool unsubscribe(const char *topic) final { return esp_mqtt_client_unsubscribe(handler_.get(), topic) != -1; }
81 
82  bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain) final {
83 #if defined(USE_MQTT_IDF_ENQUEUE)
84  // use the non-blocking version
85  // it can delay sending a couple of seconds but won't block
86  return esp_mqtt_client_enqueue(handler_.get(), topic, payload, length, qos, retain, true) != -1;
87 #else
88  // might block for several seconds, either due to network timeout (10s)
89  // or if publishing payloads longer than internal buffer (due to message fragmentation)
90  return esp_mqtt_client_publish(handler_.get(), topic, payload, length, qos, retain) != -1;
91 #endif
92  }
94 
95  void loop() final;
96 
97  void set_ca_certificate(const std::string &cert) { ca_certificate_ = cert; }
98  void set_skip_cert_cn_check(bool skip_check) { skip_cert_cn_check_ = skip_check; }
99 
100  protected:
101  bool initialize_();
102  void mqtt_event_handler_(const esp_mqtt_event_t &event);
103  static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data);
104 
106  void operator()(esp_mqtt_client *client_handler) { esp_mqtt_client_destroy(client_handler); }
107  };
108  using ClientHandler_ = std::unique_ptr<esp_mqtt_client, MqttClientDeleter>;
110 
111  bool is_connected_{false};
112  bool is_initalized_{false};
113 
114  esp_mqtt_client_config_t mqtt_cfg_{};
115 
116  std::string host_;
117  uint16_t port_;
118  std::string username_;
119  std::string password_;
120  std::string lwt_topic_;
121  std::string lwt_message_;
122  uint8_t lwt_qos_;
124  std::string client_id_;
125  uint16_t keep_alive_;
128  bool skip_cert_cn_check_{false};
129 
130  // callbacks
137  std::queue<esp_mqtt_event_t> mqtt_events_;
138 };
139 
140 } // namespace mqtt
141 } // namespace esphome
142 
143 #endif
void set_on_connect(std::function< on_connect_callback_t > &&callback) final
CallbackManager< on_subscribe_callback_t > on_subscribe_
void set_on_unsubscribe(std::function< on_unsubscribe_callback_t > &&callback) final
void set_on_message(std::function< on_message_callback_t > &&callback) final
void operator()(esp_mqtt_client *client_handler)
void set_server(network::IPAddress ip, uint16_t port) final
void mqtt_event_handler_(const esp_mqtt_event_t &event)
CallbackManager< on_unsubscribe_callback_t > on_unsubscribe_
optional< std::string > ca_certificate_
void set_clean_session(bool clean_session) final
CallbackManager< on_publish_user_callback_t > on_publish_
CallbackManager< on_message_callback_t > on_message_
void set_credentials(const char *username, const char *password) final
bool unsubscribe(const char *topic) final
CallbackManager< on_connect_callback_t > on_connect_
void set_skip_cert_cn_check(bool skip_check)
void set_on_publish(std::function< on_publish_user_callback_t > &&callback) final
bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain) final
esp_mqtt_client_config_t mqtt_cfg_
void set_on_disconnect(std::function< on_disconnect_callback_t > &&callback) final
void set_keep_alive(uint16_t keep_alive) final
bool subscribe(const char *topic, uint8_t qos) final
Definition: a4988.cpp:4
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
static - Dispatch event to instance method
void set_ca_certificate(const std::string &cert)
void set_client_id(const char *client_id) final
std::queue< esp_mqtt_event_t > mqtt_events_
void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) final
void set_on_subscribe(std::function< on_subscribe_callback_t > &&callback) final
void set_server(const char *host, uint16_t port) final
CallbackManager< on_disconnect_callback_t > on_disconnect_
static const size_t MQTT_BUFFER_SIZE
virtual bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain)=0
std::unique_ptr< esp_mqtt_client, MqttClientDeleter > ClientHandler_