–adding publish function to wamo plugin

This commit is contained in:
Dominik Kuhn 2024-08-20 09:55:44 +02:00
parent 323a37331c
commit 328f4ec329
3 changed files with 39 additions and 4 deletions

View File

@ -48,8 +48,13 @@
}, },
{ {
"name": "CJSON_LIBRARY", "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" "type": "FILEPATH"
},
{
"name": "WITH_STATIC_LIBRARIES",
"value": "False",
"type": "BOOL"
} }
] ]
} }

View File

@ -27,18 +27,21 @@ if (CJSON_FOUND )
include_directories(${CLIENT_INC}) include_directories(${CLIENT_INC})
link_directories(${CLIENT_DIR} ${mosquitto_SOURCE_DIR}) link_directories(${CLIENT_DIR} ${mosquitto_SOURCE_DIR})
add_library(wamo MODULE add_library(wamo MODULE
json_help.c json_help.c
json_help.h json_help.h
wamo.c) wamo.c)
set_target_properties(wamo PROPERTIES set_target_properties(wamo PROPERTIES
POSITION_INDEPENDENT_CODE 1 POSITION_INDEPENDENT_CODE 1
) )
set_target_properties(wamo PROPERTIES PREFIX "") 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) if(WIN32)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set_target_properties(wamo PROPERTIES IMPORTED_IMPLIB mosquitto)
target_link_libraries(wamo mosquitto) target_link_libraries(wamo mosquitto)
install(TARGETS wamo install(TARGETS wamo
DESTINATION "${CMAKE_INSTALL_BINDIR}") DESTINATION "${CMAKE_INSTALL_BINDIR}")

View File

@ -33,6 +33,7 @@ Contributors:
* Note that this only works on Mosquitto 2.0 or later. * Note that this only works on Mosquitto 2.0 or later.
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "mosquitto_broker.h" #include "mosquitto_broker.h"
@ -50,6 +51,8 @@ static cJSON* subscribedTopics = NULL;
static int callback_control(int event, void* event_data, void* userdata) static int callback_control(int event, void* event_data, void* userdata)
{ {
struct mosquitto_evt_acl_check* ed = event_data; struct mosquitto_evt_acl_check* ed = event_data;
char* payload = NULL;
uint32_t payload_len;
UNUSED(event); UNUSED(event);
UNUSED(userdata); UNUSED(userdata);
@ -64,14 +67,38 @@ static int callback_control(int event, void* event_data, void* userdata)
json_create_array(subscribedTopics, topic); json_create_array(subscribedTopics, topic);
json_add_id_to_array(subscribedTopics, topic, client_id); json_add_id_to_array(subscribedTopics, topic, client_id);
char* json_string = cJSON_Print(subscribedTopics); 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: client with id %s subscribed to topic %s", client_id, topic);
mosquitto_log_printf(MOSQ_LOG_INFO, "wamo: subscribed topics %s", json_string); 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) { else if (access == MOSQ_ACL_UNSUBSCRIBE) {
json_del_id_from_array(subscribedTopics, topic, client_id); json_del_id_from_array(subscribedTopics, topic, client_id);
char* json_string = cJSON_Print(subscribedTopics); 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: client with id %s unscribed to topic %s", client_id, topic);
mosquitto_log_printf(MOSQ_LOG_INFO, "wamo: subscribed topics %s", json_string); 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);
} }