Add topic modification example plugin.
parent
1c6d74f208
commit
7697406d3a
@ -0,0 +1,24 @@
|
||||
add_library(mosquitto_topic_modification MODULE
|
||||
mosquitto_topic_modification.c
|
||||
)
|
||||
|
||||
target_include_directories(mosquitto_topic_modification PRIVATE
|
||||
"${OPENSSL_INCLUDE_DIR}"
|
||||
"${STDBOOL_H_PATH}"
|
||||
"${STDINT_H_PATH}"
|
||||
"${mosquitto_SOURCE_DIR}"
|
||||
"${mosquitto_SOURCE_DIR}/include"
|
||||
)
|
||||
|
||||
link_directories(${mosquitto_SOURCE_DIR})
|
||||
|
||||
set_target_properties(mosquitto_topic_modification PROPERTIES
|
||||
PREFIX ""
|
||||
POSITION_INDEPENDENT_CODE 1
|
||||
)
|
||||
if(WIN32)
|
||||
target_link_libraries(mosquitto_topic_modification mosquitto)
|
||||
endif()
|
||||
|
||||
# Don't install, these are example plugins only.
|
||||
#install(TARGETS mosquitto_topic_modification RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
|
@ -0,0 +1,28 @@
|
||||
include ../../../config.mk
|
||||
|
||||
.PHONY : all binary check clean reallyclean test install uninstall
|
||||
|
||||
PLUGIN_NAME=mosquitto_topic_modification
|
||||
PLUGIN_CFLAGS+=-I../../../include -I../../../
|
||||
|
||||
all : binary
|
||||
|
||||
binary : ${PLUGIN_NAME}.so
|
||||
|
||||
${PLUGIN_NAME}.so : ${PLUGIN_NAME}.c
|
||||
$(CROSS_COMPILE)$(CC) $(PLUGIN_CPPFLAGS) $(PLUGIN_CFLAGS) $(PLUGIN_LDFLAGS) -fPIC -shared $< -o $@
|
||||
|
||||
reallyclean : clean
|
||||
clean:
|
||||
-rm -f *.o ${PLUGIN_NAME}.so *.gcda *.gcno
|
||||
|
||||
check: test
|
||||
test:
|
||||
|
||||
install: ${PLUGIN_NAME}.so
|
||||
# Don't install, these are examples only.
|
||||
#$(INSTALL) -d "${DESTDIR}$(libdir)"
|
||||
#$(INSTALL) ${STRIP_OPTS} ${PLUGIN_NAME}.so "${DESTDIR}${libdir}/${PLUGIN_NAME}.so"
|
||||
|
||||
uninstall :
|
||||
-rm -f "${DESTDIR}${libdir}/${PLUGIN_NAME}.so"
|
@ -0,0 +1,99 @@
|
||||
/*
|
||||
Copyright (c) 2020 Roger Light <roger@atchoo.org>
|
||||
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License 2.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
The Eclipse Public License is available at
|
||||
https://www.eclipse.org/legal/epl-2.0/
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
|
||||
|
||||
Contributors:
|
||||
Roger Light - initial implementation and documentation.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This is an *example* plugin which demonstrates how to modify the topic of
|
||||
* a message after it is received by the broker and before it is sent on to
|
||||
* other clients.
|
||||
*
|
||||
* You should be very sure of what you are doing before making use of this feature.
|
||||
*
|
||||
* Compile with:
|
||||
* gcc -I<path to mosquitto-repo/include> -fPIC -shared mosquitto_topic_modification.c -o mosquitto_topic_modification.so
|
||||
*
|
||||
* Use in config with:
|
||||
*
|
||||
* plugin /path/to/mosquitto_topic_modification.so
|
||||
*
|
||||
* Note that this only works on Mosquitto 2.0 or later.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mosquitto_broker.h"
|
||||
#include "mosquitto_plugin.h"
|
||||
#include "mosquitto.h"
|
||||
#include "mqtt_protocol.h"
|
||||
|
||||
#define PLUGIN_NAME "topic-modification"
|
||||
#define PLUGIN_VERSION "1.0"
|
||||
|
||||
#define UNUSED(A) (void)(A)
|
||||
|
||||
static mosquitto_plugin_id_t *mosq_pid = NULL;
|
||||
|
||||
static int callback_message(int event, void *event_data, void *userdata)
|
||||
{
|
||||
struct mosquitto_evt_message *ed = event_data;
|
||||
bool result;
|
||||
|
||||
UNUSED(event);
|
||||
UNUSED(userdata);
|
||||
|
||||
/* This simply removes "/uplink" from the end of every matching topic. You
|
||||
* can of course do much more complicated message processing if needed. */
|
||||
|
||||
mosquitto_topic_matches_sub("device/+/data/uplink", ed->topic, &result);
|
||||
|
||||
if(result){
|
||||
ed->topic[strlen(ed->topic) - strlen("/uplink")] = '\0';
|
||||
}
|
||||
|
||||
return MOSQ_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
int mosquitto_plugin_version(int supported_version_count, const int *supported_versions)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i=0; i<supported_version_count; i++){
|
||||
if(supported_versions[i] == 5){
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, struct mosquitto_opt *opts, int opt_count)
|
||||
{
|
||||
UNUSED(user_data);
|
||||
UNUSED(opts);
|
||||
UNUSED(opt_count);
|
||||
|
||||
mosq_pid = identifier;
|
||||
return mosquitto_callback_register(mosq_pid, MOSQ_EVT_MESSAGE, callback_message, NULL, NULL);
|
||||
}
|
||||
|
||||
int mosquitto_plugin_cleanup(void *user_data, struct mosquitto_opt *opts, int opt_count)
|
||||
{
|
||||
UNUSED(user_data);
|
||||
UNUSED(opts);
|
||||
UNUSED(opt_count);
|
||||
|
||||
return mosquitto_callback_unregister(mosq_pid, MOSQ_EVT_MESSAGE, callback_message, NULL);
|
||||
}
|
@ -0,0 +1 @@
|
||||
plugin ./mosquitto_topic_modification.so
|
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
../../../src/mosquitto -c test.conf -v
|
Loading…
Reference in New Issue