Skip to content

Commit cdeab8f

Browse files
committed
feat(mosq): Add support for on-message callback
1 parent 6cce87e commit cdeab8f

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

components/mosquitto/port/broker.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ void mosq_broker_stop(void)
101101
run = 0;
102102
}
103103

104+
extern mosq_message_cb_t g_mosq_message_callback;
105+
104106
int mosq_broker_run(struct mosq_broker_config *broker_config)
105107
{
106108

@@ -125,6 +127,9 @@ int mosq_broker_run(struct mosq_broker_config *broker_config)
125127
if (broker_config->tls_cfg) {
126128
net__set_tls_config(broker_config->tls_cfg);
127129
}
130+
if (broker_config->handle_message_cb) {
131+
g_mosq_message_callback = broker_config->handle_message_cb;
132+
}
128133

129134
db.config = &config;
130135

components/mosquitto/port/callbacks.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#include "util_mosq.h"
1414
#include "utlist.h"
1515
#include "lib_load.h"
16+
#include "mosq_broker.h"
1617

18+
mosq_message_cb_t g_mosq_message_callback = NULL;
1719

1820
int mosquitto_callback_register(
1921
mosquitto_plugin_id_t *identifier,
@@ -44,5 +46,8 @@ void plugin__handle_disconnect(struct mosquitto *context, int reason)
4446

4547
int plugin__handle_message(struct mosquitto *context, struct mosquitto_msg_store *stored)
4648
{
49+
if (g_mosq_message_callback) {
50+
g_mosq_message_callback(context->id, stored->topic, stored->payload, stored->payloadlen, stored->qos, stored->retain);
51+
}
4752
return MOSQ_ERR_SUCCESS;
4853
}

components/mosquitto/port/include/mosq_broker.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
struct mosquitto__config;
1111

12+
typedef void (*mosq_message_cb_t)(char *client, char *topic, char *data, int len, int qos, int retain);
1213
/**
1314
* @brief Mosquitto configuration structure
1415
*
@@ -24,6 +25,10 @@ struct mosq_broker_config {
2425
* You can open the respective docs with this idf.py command:
2526
* `idf.py docs -sp api-reference/protocols/esp_tls.html`
2627
*/
28+
void (*handle_message_cb)(char *client, char *topic, char *data, int len, int qos, int retain); /*!<
29+
* On message callback. If configured, user function is called
30+
* whenever mosquitto processes a message.
31+
*/
2732
};
2833

2934
/**

0 commit comments

Comments
 (0)