12#ifdef USE_MQTT_CERT_BUNDLE
13#include "esp_crt_bundle.h"
50#ifdef USE_MQTT_CERT_BUNDLE
51 mqtt_cfg_.broker.verification.crt_bundle_attach = esp_crt_bundle_attach;
60 mqtt_cfg_.broker.address.transport = MQTT_TRANSPORT_OVER_WS;
63 mqtt_cfg_.broker.address.transport = MQTT_TRANSPORT_OVER_WSS;
68 mqtt_cfg_.broker.address.transport = MQTT_TRANSPORT_OVER_SSL;
70 mqtt_cfg_.broker.address.transport = MQTT_TRANSPORT_OVER_TCP;
77 auto *mqtt_client = esp_mqtt_client_init(&
mqtt_cfg_);
82#if defined(USE_MQTT_IDF_ENQUEUE)
92 ESP_LOGE(TAG,
"Failed to create MQTT task");
103 ESP_LOGE(TAG,
"Failed to init client");
113 if (event ==
nullptr)
115 const auto event_id =
event->event_id;
122 if (
event_id == MQTT_EVENT_CONNECTED ||
event_id == MQTT_EVENT_DISCONNECTED ||
128 uint16_t inbound_dropped = this->
mqtt_event_queue_.get_and_reset_dropped_count();
129 if (inbound_dropped > 0) {
130 ESP_LOGW(TAG,
"Dropped %u inbound MQTT events", inbound_dropped);
133#if defined(USE_MQTT_IDF_ENQUEUE)
145 uint16_t dropped = this->
mqtt_queue_.get_and_reset_dropped_count();
155 ESP_LOGV(TAG,
"Event dispatched from event loop event_id=%d", event.event_id);
156 switch (event.event_id) {
157 case MQTT_EVENT_BEFORE_CONNECT:
158 ESP_LOGV(TAG,
"MQTT_EVENT_BEFORE_CONNECT");
161 case MQTT_EVENT_CONNECTED:
162 ESP_LOGV(TAG,
"MQTT_EVENT_CONNECTED");
166#if defined(USE_MQTT_IDF_ENQUEUE)
173 case MQTT_EVENT_DISCONNECTED:
174 ESP_LOGV(TAG,
"MQTT_EVENT_DISCONNECTED");
177#if defined(USE_MQTT_IDF_ENQUEUE)
184 case MQTT_EVENT_SUBSCRIBED:
185 ESP_LOGV(TAG,
"MQTT_EVENT_SUBSCRIBED, msg_id=%d", event.msg_id);
188 event.data.empty() ? SUBACK_FAILURE :
static_cast<uint8_t
>(event.data[0]));
190 case MQTT_EVENT_UNSUBSCRIBED:
191 ESP_LOGV(TAG,
"MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event.msg_id);
194 case MQTT_EVENT_PUBLISHED:
195 ESP_LOGV(TAG,
"MQTT_EVENT_PUBLISHED, msg_id=%d", event.msg_id);
198 case MQTT_EVENT_DATA: {
199 if (!event.topic.empty()) {
206 ESP_LOGV(TAG,
"MQTT_EVENT_DATA %s", this->
cached_topic_.c_str());
208 event.current_data_offset, event.total_data_len);
210 case MQTT_EVENT_ERROR:
211 ESP_LOGE(TAG,
"MQTT_EVENT_ERROR");
212 if (event.error_handle.error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
213 ESP_LOGE(TAG,
"Last esp-tls error: 0x%x, tls stack error: 0x%x, socket errno: %d (%s)",
214 event.error_handle.esp_tls_last_esp_err, event.error_handle.esp_tls_stack_err,
215 event.error_handle.esp_transport_sock_errno, strerror(event.error_handle.esp_transport_sock_errno));
216 }
else if (event.error_handle.error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) {
217 ESP_LOGE(TAG,
"Connection refused error: 0x%x", event.error_handle.connect_return_code);
219 ESP_LOGE(TAG,
"Unknown error type: 0x%x", event.error_handle.error_type);
223 ESP_LOGV(TAG,
"Other event id:%d", event.event_id);
232 if (instance ==
nullptr)
234 const auto *mqtt_event =
static_cast<esp_mqtt_event_t *
>(event_data);
236 bool listened =
true;
238 case MQTT_EVENT_CONNECTED:
241 case MQTT_EVENT_SUBSCRIBED:
244 case MQTT_EVENT_UNSUBSCRIBED:
247 case MQTT_EVENT_PUBLISHED: {
249 auto msg_id =
static_cast<uint16_t
>(mqtt_event->msg_id);
256 case MQTT_EVENT_DATA:
258 if (mqtt_event->total_data_len > MQTT_MAX_PAYLOAD_SIZE && mqtt_event->current_data_offset != 0)
267 if (event !=
nullptr) {
268 event->populate(*mqtt_event);
277 ESP_LOGV(TAG,
"Ack event_id=%" PRId32
", msg_id=%d",
event_id, mqtt_event->msg_id);
279 if (
event_id == MQTT_EVENT_SUBSCRIBED) {
282 instance->
subacks_.
add(mqtt_event->data_len > 0 ?
static_cast<uint8_t
>(mqtt_event->data[0]) : SUBACK_FAILURE);
283 }
else if (!listened) {
293 int msg_id = esp_mqtt_client_publish(this->
handler_.get(), message.
topic.c_str(),
message.payload.c_str(),
304#if defined(USE_MQTT_IDF_ENQUEUE)
310 ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
314 while ((elem = this_mqtt->
mqtt_queue_.pop()) !=
nullptr) {
316 switch (elem->
type) {
318 if (esp_mqtt_client_subscribe(this_mqtt->
handler_.get(), elem->
topic, elem->
qos) == -1)
323 esp_mqtt_client_unsubscribe(this_mqtt->
handler_.get(), elem->
topic);
332 ESP_LOGE(TAG,
"Invalid operation type from MQTT queue");
360 elem->retain = retain;
363 if (!elem->set_data(topic, payload,
len)) {
void wake_loop_threadsafe()
Wake the main event loop from another thread or callback.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
Sets a flag for the guard's scope and restores its previous value on exit.
EventPool< struct QueueElement, MQTT_QUEUE_LENGTH - 1 > mqtt_outbound_pool_
CallbackManager< on_connect_callback_t > on_connect_
CallbackManager< on_disconnect_callback_t > on_disconnect_
uint16_t subscribes_sent_
SUBSCRIBEs sent on this connection.
std::atomic< uint16_t > acked_msg_id_
uint16_t subscribes_lost_at_connect_
CallbackManager< on_message_callback_t > on_message_
std::atomic< uint16_t > tracked_msg_id_
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
const char * cl_certificate_
static constexpr uint32_t EVENT_DRAIN_BUDGET_MS
CallbackManager< on_subscribe_callback_t > on_subscribe_
static void esphome_mqtt_task(void *params)
EventPool< Event, MQTT_EVENT_POOL_SIZE > mqtt_event_pool_
bool enqueue_(MqttQueueTypeT type, const char *topic, int qos=0, bool retain=false, const char *payload=NULL, size_t len=0)
const char * ca_certificate_
bool publish_tracked(const MQTTMessage &message) final
void count_lost_subscribe_()
static constexpr size_t TASK_STACK_SIZE_TLS
CallbackManager< on_unsubscribe_callback_t > on_unsubscribe_
std::atomic< uint8_t > tracked_acked_
std::string cached_topic_
esp_mqtt_client_config_t mqtt_cfg_
TaskHandle_t task_handle_
static constexpr size_t TASK_STACK_SIZE
CallbackManager< on_publish_user_callback_t > on_publish_
static constexpr uint32_t DROP_LOG_INTERVAL_MS
NotifyingLockFreeQueue< struct QueueElement, MQTT_QUEUE_LENGTH > mqtt_queue_
static constexpr uint8_t MQTT_EVENT_POOL_SIZE
uint32_t last_dropped_log_time_
void mqtt_event_handler_(const Event &event)
std::atomic< uint16_t > subscribes_lost_
static constexpr ssize_t TASK_PRIORITY
LockFreeQueue< Event, MQTT_EVENT_QUEUE_LENGTH > mqtt_event_queue_
void add(uint8_t return_code)
const LogString * message
ESPHOME_LOG_TAG(TAG, "alarm_control_panel")
@ MQTT_QUEUE_TYPE_SUBSCRIBE
@ MQTT_QUEUE_TYPE_UNSUBSCRIBE
@ MQTT_QUEUE_TYPE_PUBLISH
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
internal struct for MQTT messages.