From 328f4ec3293ce7340d579f96c99701d7f6a50a93 Mon Sep 17 00:00:00 2001 From: Dominik Kuhn Date: Tue, 20 Aug 2024 09:55:44 +0200 Subject: [PATCH] =?UTF-8?q?=C2=96adding=20publish=20function=20to=20wamo?= =?UTF-8?q?=20plugin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeSettings.json | 7 ++++++- plugins/wamo/CMakeLists.txt | 9 ++++++--- plugins/wamo/wamo.c | 27 +++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CMakeSettings.json b/CMakeSettings.json index 91f357d9..d0d6a43a 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -48,8 +48,13 @@ }, { "name": "CJSON_LIBRARY", - "value": "C:/Users/d.kuhn/Projekte/cJSON/cjson/out/build/x64-Debug/cjson.lib", + "value": "C:\\Users\\d.kuhn\\Projekte\\cJSON\\cjson\\out\\build\\x64-Debug\\cjson.lib", "type": "FILEPATH" + }, + { + "name": "WITH_STATIC_LIBRARIES", + "value": "False", + "type": "BOOL" } ] } diff --git a/plugins/wamo/CMakeLists.txt b/plugins/wamo/CMakeLists.txt index 470eb681..aa6f5953 100644 --- a/plugins/wamo/CMakeLists.txt +++ b/plugins/wamo/CMakeLists.txt @@ -27,18 +27,21 @@ if (CJSON_FOUND ) include_directories(${CLIENT_INC}) link_directories(${CLIENT_DIR} ${mosquitto_SOURCE_DIR}) - add_library(wamo MODULE + add_library(wamo MODULE json_help.c json_help.h wamo.c) - + set_target_properties(wamo PROPERTIES POSITION_INDEPENDENT_CODE 1 ) set_target_properties(wamo PROPERTIES PREFIX "") + set_target_properties(wamo PROPERTIES IMPORTED_IMPLIB ${CJSON_LIBRARIES}) - target_link_libraries(wamo ${CJSON_LIBRARIES}) + target_link_libraries(wamo ${CJSON_LIBRARIES}) if(WIN32) + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + set_target_properties(wamo PROPERTIES IMPORTED_IMPLIB mosquitto) target_link_libraries(wamo mosquitto) install(TARGETS wamo DESTINATION "${CMAKE_INSTALL_BINDIR}") diff --git a/plugins/wamo/wamo.c b/plugins/wamo/wamo.c index a5740a04..b71b57c4 100644 --- a/plugins/wamo/wamo.c +++ b/plugins/wamo/wamo.c @@ -33,6 +33,7 @@ Contributors: * Note that this only works on Mosquitto 2.0 or later. */ #include +#include #include #include "mosquitto_broker.h" @@ -50,6 +51,8 @@ static cJSON* subscribedTopics = NULL; static int callback_control(int event, void* event_data, void* userdata) { struct mosquitto_evt_acl_check* ed = event_data; + char* payload = NULL; + uint32_t payload_len; UNUSED(event); UNUSED(userdata); @@ -64,14 +67,38 @@ static int callback_control(int event, void* event_data, void* userdata) json_create_array(subscribedTopics, topic); json_add_id_to_array(subscribedTopics, topic, client_id); char* json_string = cJSON_Print(subscribedTopics); + payload = cJSON_PrintUnformatted(subscribedTopics); mosquitto_log_printf(MOSQ_LOG_INFO, "wamo: client with id %s subscribed to topic %s", client_id, topic); mosquitto_log_printf(MOSQ_LOG_INFO, "wamo: subscribed topics %s", json_string); + + if (payload == NULL) return MOSQ_ERR_MALFORMED_PACKET; + + payload_len = strlen(payload); + if (payload_len > MQTT_MAX_PAYLOAD) { + free(payload); + return MOSQ_ERR_PAYLOAD_SIZE; + } + mosquitto_log_printf(MOSQ_LOG_INFO, "wamo: DEBUG MESSAGE BEFORE PUBLISHING ..."); + + mosquitto_broker_publish(NULL, "mqtt/subscriptions", + (int)payload_len, payload, 0, true, NULL); } else if (access == MOSQ_ACL_UNSUBSCRIBE) { json_del_id_from_array(subscribedTopics, topic, client_id); char* json_string = cJSON_Print(subscribedTopics); + payload = cJSON_PrintUnformatted(subscribedTopics); mosquitto_log_printf(MOSQ_LOG_INFO, "wamo: client with id %s unscribed to topic %s", client_id, topic); mosquitto_log_printf(MOSQ_LOG_INFO, "wamo: subscribed topics %s", json_string); + + if (payload == NULL) return MOSQ_ERR_MALFORMED_PACKET; + + payload_len = strlen(payload); + if (payload_len > MQTT_MAX_PAYLOAD) { + free(payload); + return MOSQ_ERR_PAYLOAD_SIZE; + } + mosquitto_broker_publish(NULL, "mqtt/subscriptions", + (int)payload_len, payload, 0, true, NULL); }