From 06b933c3ba57bd7dfe6f920f8ae388f7d38e437e Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Fri, 23 Oct 2020 22:49:16 +0100 Subject: [PATCH] Add tick event to plugin interface. --- include/mosquitto_broker.h | 12 ++++++++++++ src/loop.c | 1 + src/mosquitto_broker_internal.h | 4 +++- src/plugin.c | 26 ++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/include/mosquitto_broker.h b/include/mosquitto_broker.h index 0f6ee3a3..e66cc9ed 100644 --- a/include/mosquitto_broker.h +++ b/include/mosquitto_broker.h @@ -55,6 +55,7 @@ enum mosquitto_plugin_event { MOSQ_EVT_CONTROL = 6, MOSQ_EVT_MESSAGE = 7, MOSQ_EVT_PSK_KEY = 8, + MOSQ_EVT_TICK = 9, }; /* Data for the MOSQ_EVT_RELOAD event */ @@ -141,6 +142,17 @@ struct mosquitto_evt_message { }; +/* Data for the MOSQ_EVT_TICK event */ +struct mosquitto_evt_tick { + void *future; + long now_ns; + long next_ns; + int now_s; + int next_s; + void *future2[4]; +}; + + /* Callback definition */ typedef int (*MOSQ_FUNC_generic_callback)(int, void *, void *); diff --git a/src/loop.c b/src/loop.c index b4f27d2b..28cf785f 100644 --- a/src/loop.c +++ b/src/loop.c @@ -229,6 +229,7 @@ int mosquitto_main_loop(struct mosquitto_db *db, mosq_sock_t *listensock, int li } } #endif + plugin__handle_tick(db); } mux__cleanup(db); diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index 54f65cc3..1e9588f6 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -230,11 +230,12 @@ struct plugin__callbacks{ struct mosquitto__callback *reload; struct mosquitto__callback *acl_check; struct mosquitto__callback *basic_auth; - struct mosquitto__callback *psk_key; struct mosquitto__callback *ext_auth_start; struct mosquitto__callback *ext_auth_continue; struct mosquitto__callback *control; struct mosquitto__callback *message; + struct mosquitto__callback *tick; + struct mosquitto__callback *psk_key; }; struct mosquitto__security_options { @@ -797,6 +798,7 @@ void listener__set_defaults(struct mosquitto__listener *listener); int plugin__load_v5(struct mosquitto__listener *listener, struct mosquitto__auth_plugin *plugin, struct mosquitto_opt *auth_options, int auth_option_count, void *lib); int plugin__handle_message(struct mosquitto_db *db, struct mosquitto *context, struct mosquitto_msg_store *stored); void LIB_ERROR(void); +void plugin__handle_tick(struct mosquitto_db *db); /* ============================================================ * Property related functions diff --git a/src/plugin.c b/src/plugin.c index 54cbd373..e83b429b 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -155,6 +155,26 @@ int plugin__handle_message(struct mosquitto_db *db, struct mosquitto *context, s } +void plugin__handle_tick(struct mosquitto_db *db) +{ + struct mosquitto_evt_tick event_data; + struct mosquitto__callback *cb_base; + struct mosquitto__security_options *opts; + + // FIXME - set now_s and now_ns to avoid need for multiple time lookups + if(db->config->per_listener_settings){ + // FIXME - iterate over all listeners + }else{ + opts = &db->config->security_options; + memset(&event_data, 0, sizeof(event_data)); + + DL_FOREACH(opts->plugin_callbacks.message, cb_base){ + cb_base->cb(MOSQ_EVT_TICK, &event_data, cb_base->userdata); + } + } +} + + int mosquitto_callback_register( mosquitto_plugin_id_t *identifier, int event, @@ -200,6 +220,9 @@ int mosquitto_callback_register( case MOSQ_EVT_MESSAGE: cb_base = &security_options->plugin_callbacks.message; break; + case MOSQ_EVT_TICK: + cb_base = &security_options->plugin_callbacks.tick; + break; default: return MOSQ_ERR_INVAL; break; @@ -264,6 +287,9 @@ int mosquitto_callback_unregister( case MOSQ_EVT_MESSAGE: cb_base = &security_options->plugin_callbacks.message; break; + case MOSQ_EVT_TICK: + cb_base = &security_options->plugin_callbacks.tick; + break; default: return MOSQ_ERR_INVAL; break;