feat(mosq): Add support for on-message callback

This commit is contained in:
David Cermak
2024-12-19 16:36:51 +01:00
parent 6cce87e465
commit cdeab8f517
3 changed files with 15 additions and 0 deletions

View File

@ -101,6 +101,8 @@ void mosq_broker_stop(void)
run = 0; run = 0;
} }
extern mosq_message_cb_t g_mosq_message_callback;
int mosq_broker_run(struct mosq_broker_config *broker_config) 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) { if (broker_config->tls_cfg) {
net__set_tls_config(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; db.config = &config;

View File

@ -13,7 +13,9 @@
#include "util_mosq.h" #include "util_mosq.h"
#include "utlist.h" #include "utlist.h"
#include "lib_load.h" #include "lib_load.h"
#include "mosq_broker.h"
mosq_message_cb_t g_mosq_message_callback = NULL;
int mosquitto_callback_register( int mosquitto_callback_register(
mosquitto_plugin_id_t *identifier, 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) 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; return MOSQ_ERR_SUCCESS;
} }

View File

@ -9,6 +9,7 @@
struct mosquitto__config; 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 * @brief Mosquitto configuration structure
* *
@ -24,6 +25,10 @@ struct mosq_broker_config {
* You can open the respective docs with this idf.py command: * You can open the respective docs with this idf.py command:
* `idf.py docs -sp api-reference/protocols/esp_tls.html` * `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.
*/
}; };
/** /**