forked from espressif/esp-mqtt
support for esp event loop library while keeping backward compatible mode if callback configured
This commit is contained in:
14
README.md
14
README.md
@ -19,9 +19,21 @@
|
|||||||
Please refer to instructions in [ESP-IDF](https://github.com/espressif/esp-idf)
|
Please refer to instructions in [ESP-IDF](https://github.com/espressif/esp-idf)
|
||||||
|
|
||||||
## Documentation
|
## 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
|
## License
|
||||||
|
|
||||||
- MQTT Package - [Stephen Robinson - contiki-mqtt](https://github.com/esar/contiki-mqtt)
|
- MQTT Package - [Stephen Robinson - contiki-mqtt](https://github.com/esar/contiki-mqtt)
|
||||||
- Others [@tuanpmt](https://twitter.com/tuanpmt)
|
- Others [@tuanpmt](https://twitter.com/tuanpmt)
|
||||||
Apache License
|
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
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
|
|
||||||
#include "mqtt_config.h"
|
#include "mqtt_config.h"
|
||||||
|
#include "esp_event.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -85,7 +86,8 @@ typedef esp_err_t (* mqtt_event_callback_t)(esp_mqtt_event_handle_t event);
|
|||||||
* MQTT client configuration structure
|
* MQTT client configuration structure
|
||||||
*/
|
*/
|
||||||
typedef struct {
|
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 *host; /*!< MQTT server domain (ipv4 as string) */
|
||||||
const char *uri; /*!< Complete MQTT broker URI */
|
const char *uri; /*!< Complete MQTT broker URI */
|
||||||
uint32_t port; /*!< MQTT server port */
|
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);
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif //__cplusplus
|
#endif //__cplusplus
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
/* using uri parser */
|
/* using uri parser */
|
||||||
#include "http_parser.h"
|
#include "http_parser.h"
|
||||||
|
#include "esp_event_loop.h"
|
||||||
|
|
||||||
#ifdef MQTT_DISABLE_API_LOCKS
|
#ifdef MQTT_DISABLE_API_LOCKS
|
||||||
# define MQTT_API_LOCK(c)
|
# define MQTT_API_LOCK(c)
|
||||||
@ -27,7 +28,14 @@
|
|||||||
|
|
||||||
static const char *TAG = "MQTT_CLIENT";
|
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;
|
mqtt_connect_info_t *connect_info;
|
||||||
uint8_t *in_buffer;
|
uint8_t *in_buffer;
|
||||||
uint8_t *out_buffer;
|
uint8_t *out_buffer;
|
||||||
@ -45,6 +53,7 @@ typedef struct mqtt_state {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
mqtt_event_callback_t event_handle;
|
mqtt_event_callback_t event_handle;
|
||||||
|
esp_event_loop_handle_t event_loop_handle;
|
||||||
int task_stack;
|
int task_stack;
|
||||||
int task_prio;
|
int task_prio;
|
||||||
char *uri;
|
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) {
|
if (config->event_handle) {
|
||||||
cfg->event_handle = 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) {
|
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) {
|
if (client->config->event_handle) {
|
||||||
return client->config->event_handle(&client->event);
|
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)
|
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);
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user