From eeebd0215c3a93c403bed71e615cf0570144b110 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 18 Dec 2018 16:43:08 +0100 Subject: [PATCH] support for esp event loop library while keeping backward compatible mode if callback configured --- README.md | 14 +++++++++++++- include/mqtt_client.h | 17 ++++++++++++++++- mqtt_client.c | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index abe4822..6a6fe3e 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,21 @@ Please refer to instructions in [ESP-IDF](https://github.com/espressif/esp-idf) ## Documentation -Please refer to the standard [ESP-IDF](https://github.com/espressif/esp-idf) documentation here https://github.com/espressif/esp-idf/blob/master/docs/en/api-reference/protocols/mqtt.rst + +* Please refer to the standard [ESP-IDF](https://github.com/espressif/esp-idf), documentation for the latest version: https://docs.espressif.com/projects/esp-idf/ + +* Documentation of ESP-MQTT API: https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/protocols/mqtt.html ## License + - MQTT Package - [Stephen Robinson - contiki-mqtt](https://github.com/esar/contiki-mqtt) - Others [@tuanpmt](https://twitter.com/tuanpmt) Apache License + +## Older IDF verisons + +For [ESP-IDF](https://github.com/espressif/esp-idf) versions prior to IDFv3.2, please clone as a component of [ESP-IDF](https://github.com/espressif/esp-idf): +``` +git submodule add https://github.com/espressif/esp-mqtt.git components/espmqtt +``` +and checkout the [ESP-MQTT_FOR_IDF_3.1](https://github.com/espressif/esp-mqtt/tree/ESP-MQTT_FOR_IDF_3.1) tag diff --git a/include/mqtt_client.h b/include/mqtt_client.h index cb06132..8b05d0b 100644 --- a/include/mqtt_client.h +++ b/include/mqtt_client.h @@ -13,6 +13,7 @@ #include "esp_err.h" #include "mqtt_config.h" +#include "esp_event.h" #ifdef __cplusplus extern "C" { @@ -85,7 +86,8 @@ typedef esp_err_t (* mqtt_event_callback_t)(esp_mqtt_event_handle_t event); * MQTT client configuration structure */ typedef struct { - mqtt_event_callback_t event_handle; /*!< handle for MQTT events */ + mqtt_event_callback_t event_handle; /*!< handle for MQTT events as a callback in legacy mode */ + esp_event_loop_handle_t event_loop_handle; /*!< handle for MQTT event loop library */ const char *host; /*!< MQTT server domain (ipv4 as string) */ const char *uri; /*!< Complete MQTT broker URI */ uint32_t port; /*!< MQTT server port */ @@ -237,6 +239,19 @@ esp_err_t esp_mqtt_client_destroy(esp_mqtt_client_handle_t client); */ esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_client_config_t *config); +/** + * @brief Registers mqtt event + * + * @param client mqtt client handle + * @param event event type + * @param event_handler hanlder callback + * @param event_handler_arg handlers context + * + * @return ESP_ERR_NO_MEM if failed to allocate + * ESP_OK on success + */ +esp_err_t esp_mqtt_client_register_event(esp_mqtt_client_handle_t client, esp_mqtt_event_id_t event, esp_event_handler_t event_handler, void* event_handler_arg); + #ifdef __cplusplus } #endif //__cplusplus diff --git a/mqtt_client.c b/mqtt_client.c index 6afae73..db86724 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -12,6 +12,7 @@ /* using uri parser */ #include "http_parser.h" +#include "esp_event_loop.h" #ifdef MQTT_DISABLE_API_LOCKS # define MQTT_API_LOCK(c) @@ -27,7 +28,14 @@ static const char *TAG = "MQTT_CLIENT"; -typedef struct mqtt_state { +/** + * @brief Define of MQTT Event base + * + */ +ESP_EVENT_DEFINE_BASE(MQTT_EVENTS); + +typedef struct mqtt_state +{ mqtt_connect_info_t *connect_info; uint8_t *in_buffer; uint8_t *out_buffer; @@ -45,6 +53,7 @@ typedef struct mqtt_state { typedef struct { mqtt_event_callback_t event_handle; + esp_event_loop_handle_t event_loop_handle; int task_stack; int task_prio; char *uri; @@ -214,6 +223,12 @@ esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_cl if (config->event_handle) { cfg->event_handle = config->event_handle; + } else { + esp_event_loop_args_t no_task_loop = { + .queue_size = 1, + .task_name = NULL, + }; + esp_event_loop_create(&no_task_loop, &cfg->event_loop_handle); } if (config->refresh_connection_after_ms) { @@ -540,8 +555,10 @@ static esp_err_t esp_mqtt_dispatch_event(esp_mqtt_client_handle_t client) if (client->config->event_handle) { return client->config->event_handle(&client->event); + } else { + esp_event_post_to(client->config->event_loop_handle, MQTT_EVENTS, client->event.event_id, &client->event, sizeof(client->event), portMAX_DELAY); + return esp_event_loop_run(client->config->event_loop_handle, 0); } - return ESP_FAIL; } static esp_err_t deliver_publish(esp_mqtt_client_handle_t client) @@ -1289,3 +1306,15 @@ cannot_publish: } +esp_err_t esp_mqtt_client_register_event(esp_mqtt_client_handle_t client, esp_mqtt_event_id_t event, esp_event_handler_t event_handler, void* event_handler_arg) +{ + if (client == NULL) { + return ESP_ERR_INVALID_ARG; + } + if (client->config->event_handle) { + ESP_LOGW(TAG, "Registering event loop while event callback is not null, clearing callback"); + client->config->event_handle = NULL; + } + + return esp_event_handler_register_with(client->config->event_loop_handle, MQTT_EVENTS, event, event_handler, event_handler_arg); +}