From 44884a58aceee134096a7cad2e5f3c37d691a65a Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Wed, 17 Jun 2020 18:45:03 +0200 Subject: [PATCH] using topic matcher for audit topic --- mqttauditing.cfg | 3 ++- mqttreceiver.c | 17 ++++++++++------- mqtttopicmatcher.c | 27 ++++++++++++++------------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/mqttauditing.cfg b/mqttauditing.cfg index e009afc..b37f5e0 100644 --- a/mqttauditing.cfg +++ b/mqttauditing.cfg @@ -1,3 +1,4 @@ # mqttauditing file -mqttBroker = "172.16.2.16:1883" \ No newline at end of file +mqttBroker = "172.16.2.16:1883" +mqttAuditTopic = "Iot/MqttAuditing/#" \ No newline at end of file diff --git a/mqttreceiver.c b/mqttreceiver.c index 4c84a70..c355249 100644 --- a/mqttreceiver.c +++ b/mqttreceiver.c @@ -8,6 +8,8 @@ #include #include "handleType.h" #include "ringbuffer.h" +#include "mqtttopicmatcher.h" + const char MQTT_BROKER_KEY[] = "mqttBroker"; @@ -47,21 +49,22 @@ int on_message(void *kontext, char *topicName, int topicLen, MQTTClient_message commonThreadHandle_t *handle = (commonThreadHandle_t*)kontext; mqttThreadContext_t *context = (mqttThreadContext_t*)handle->context; - printf("Topic: %s, TopicLen: %d\n", topicName, topicLen); char* payload = message->payload; - printf("Received operation: %s\n", payload); + printf("Topic: %s, TopicLen: %d, Payload: %s\n", topicName, topicLen, payload); - int curTopicLen = (topicLen == 0) ? strlen(topicName) : topicLen; - if (strncmp(topicName, context->watchdogTopic, curTopicLen) == 0) { - printf("Watchdog signal received\n"); + if (strcmp(topicName, context->watchdogTopic) == 0) { + // printf("Watchdog signal received\n"); context->watchdogCounter += 1; - } else if (strncmp(topicName, context->commandTopic, curTopicLen) == 0) { + } + if (strcmp(topicName, context->commandTopic) == 0) { printf("Command received\n"); if (strcmp(payload, "stop") == 0) { context->stopSignal = true; printf("Stop command received\n"); } - } else if (strncmp(topicName, context->auditTopic, curTopicLen) == 0) { + } + + if (cmpTopicWithWildcard((char*)context->auditTopic, topicName) == 0) { printf("Audit message received\n"); } diff --git a/mqtttopicmatcher.c b/mqtttopicmatcher.c index a219684..71833bf 100644 --- a/mqtttopicmatcher.c +++ b/mqtttopicmatcher.c @@ -8,9 +8,12 @@ int cmpTopicWithWildcard(char *subscribedTopic, char *receivedTopic) { char s; char r; - enum { e_START, e_PART, e_DELIMITER, e_WAIT_FOR_DELIMITER_IN_S, e_WAIT_FOR_DELIMITER_IN_R, e_OK, e_NOK } state; + enum { e_START, e_PART, e_DELIMITER, e_WAIT_FOR_DELIMITER_IN_S, e_WAIT_FOR_DELIMITER_IN_R } state; state = e_START; + enum { r_NOK = -1, r_OK = 0, r_INVALID = 99 } res; + res = r_INVALID; + while (1) { s = *(subscribedTopic + s_idx); r = *(receivedTopic + r_idx); @@ -22,7 +25,7 @@ int cmpTopicWithWildcard(char *subscribedTopic, char *receivedTopic) { switch (state) { case e_START: if (s == '#') { - state = e_OK; + res = r_OK; break; } else if (s == '+') { state = e_WAIT_FOR_DELIMITER_IN_S; @@ -40,12 +43,12 @@ int cmpTopicWithWildcard(char *subscribedTopic, char *receivedTopic) { r_idx++; break; } else { - state = e_NOK; + res = r_NOK; break; } case e_DELIMITER: if (s == '#') { - state = e_OK; + res = r_OK; break; } else if (s == '+') { state = e_WAIT_FOR_DELIMITER_IN_S; @@ -58,12 +61,12 @@ int cmpTopicWithWildcard(char *subscribedTopic, char *receivedTopic) { r_idx++; break; } else { - state = e_NOK; + res = r_NOK; break; } case e_WAIT_FOR_DELIMITER_IN_S: if (s != '/') { - state = e_NOK; + res = r_NOK; break; } else if ((s == '/') && (r == '/')) { state = e_PART; @@ -75,7 +78,7 @@ int cmpTopicWithWildcard(char *subscribedTopic, char *receivedTopic) { r_idx++; break; } else { - state = e_NOK; + res = r_NOK; break; } case e_WAIT_FOR_DELIMITER_IN_R: @@ -90,7 +93,7 @@ int cmpTopicWithWildcard(char *subscribedTopic, char *receivedTopic) { } case e_PART: if (s == '#') { - state = e_OK; + res = r_OK; break; } else if (s == '+') { state = e_WAIT_FOR_DELIMITER_IN_S; @@ -103,15 +106,13 @@ int cmpTopicWithWildcard(char *subscribedTopic, char *receivedTopic) { r_idx++; break; } else { - state = e_NOK; + res = r_NOK; break; } } - if (state == e_OK) { - return 0; - } else if (state == e_NOK) { - return -1; + if (res != r_INVALID) { + return res; } } }