Skip to content

Commit

Permalink
feat(mosq): Add support for on-message callback
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Dec 19, 2024
1 parent 6cce87e commit cdeab8f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions components/mosquitto/port/broker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{

Expand All @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions components/mosquitto/port/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions components/mosquitto/port/include/mosq_broker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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.
*/
};

/**
Expand Down

0 comments on commit cdeab8f

Please sign in to comment.