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