diff --git a/components/mosquitto/port/broker.c b/components/mosquitto/port/broker.c index 62cfb29bc..f78f65179 100644 --- a/components/mosquitto/port/broker.c +++ b/components/mosquitto/port/broker.c @@ -101,6 +101,8 @@ void mosq_broker_stop(void) run = 0; } +extern mosq_message_cb_t g_mosq_message_callback; + int mosq_broker_run(struct mosq_broker_config *broker_config) { @@ -125,6 +127,9 @@ int mosq_broker_run(struct mosq_broker_config *broker_config) if (broker_config->tls_cfg) { net__set_tls_config(broker_config->tls_cfg); } + if (broker_config->handle_message_cb) { + g_mosq_message_callback = broker_config->handle_message_cb; + } db.config = &config; diff --git a/components/mosquitto/port/callbacks.c b/components/mosquitto/port/callbacks.c index cf6e5f9a2..dc0f2ad1b 100644 --- a/components/mosquitto/port/callbacks.c +++ b/components/mosquitto/port/callbacks.c @@ -13,7 +13,9 @@ #include "util_mosq.h" #include "utlist.h" #include "lib_load.h" +#include "mosq_broker.h" +mosq_message_cb_t g_mosq_message_callback = NULL; int mosquitto_callback_register( mosquitto_plugin_id_t *identifier, @@ -44,5 +46,8 @@ void plugin__handle_disconnect(struct mosquitto *context, int reason) int plugin__handle_message(struct mosquitto *context, struct mosquitto_msg_store *stored) { + if (g_mosq_message_callback) { + g_mosq_message_callback(context->id, stored->topic, stored->payload, stored->payloadlen, stored->qos, stored->retain); + } return MOSQ_ERR_SUCCESS; } diff --git a/components/mosquitto/port/include/mosq_broker.h b/components/mosquitto/port/include/mosq_broker.h index 362943557..d9b858b8c 100644 --- a/components/mosquitto/port/include/mosq_broker.h +++ b/components/mosquitto/port/include/mosq_broker.h @@ -9,6 +9,7 @@ struct mosquitto__config; +typedef void (*mosq_message_cb_t)(char *client, char *topic, char *data, int len, int qos, int retain); /** * @brief Mosquitto configuration structure * @@ -24,6 +25,10 @@ struct mosq_broker_config { * You can open the respective docs with this idf.py command: * `idf.py docs -sp api-reference/protocols/esp_tls.html` */ + void (*handle_message_cb)(char *client, char *topic, char *data, int len, int qos, int retain); /*!< + * On message callback. If configured, user function is called + * whenever mosquitto processes a message. + */ }; /**