using topic matcher for audit topic

This commit is contained in:
Wolfgang Hottgenroth 2020-06-17 18:45:03 +02:00
parent c48702db3a
commit 44884a58ac
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
3 changed files with 26 additions and 21 deletions

View File

@ -1,3 +1,4 @@
# mqttauditing file # mqttauditing file
mqttBroker = "172.16.2.16:1883" mqttBroker = "172.16.2.16:1883"
mqttAuditTopic = "Iot/MqttAuditing/#"

View File

@ -8,6 +8,8 @@
#include <stdbool.h> #include <stdbool.h>
#include "handleType.h" #include "handleType.h"
#include "ringbuffer.h" #include "ringbuffer.h"
#include "mqtttopicmatcher.h"
const char MQTT_BROKER_KEY[] = "mqttBroker"; 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; commonThreadHandle_t *handle = (commonThreadHandle_t*)kontext;
mqttThreadContext_t *context = (mqttThreadContext_t*)handle->context; mqttThreadContext_t *context = (mqttThreadContext_t*)handle->context;
printf("Topic: %s, TopicLen: %d\n", topicName, topicLen);
char* payload = message->payload; 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 (strcmp(topicName, context->watchdogTopic) == 0) {
if (strncmp(topicName, context->watchdogTopic, curTopicLen) == 0) { // printf("Watchdog signal received\n");
printf("Watchdog signal received\n");
context->watchdogCounter += 1; context->watchdogCounter += 1;
} else if (strncmp(topicName, context->commandTopic, curTopicLen) == 0) { }
if (strcmp(topicName, context->commandTopic) == 0) {
printf("Command received\n"); printf("Command received\n");
if (strcmp(payload, "stop") == 0) { if (strcmp(payload, "stop") == 0) {
context->stopSignal = true; context->stopSignal = true;
printf("Stop command received\n"); 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"); printf("Audit message received\n");
} }

View File

@ -8,9 +8,12 @@ int cmpTopicWithWildcard(char *subscribedTopic, char *receivedTopic) {
char s; char s;
char r; 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; state = e_START;
enum { r_NOK = -1, r_OK = 0, r_INVALID = 99 } res;
res = r_INVALID;
while (1) { while (1) {
s = *(subscribedTopic + s_idx); s = *(subscribedTopic + s_idx);
r = *(receivedTopic + r_idx); r = *(receivedTopic + r_idx);
@ -22,7 +25,7 @@ int cmpTopicWithWildcard(char *subscribedTopic, char *receivedTopic) {
switch (state) { switch (state) {
case e_START: case e_START:
if (s == '#') { if (s == '#') {
state = e_OK; res = r_OK;
break; break;
} else if (s == '+') { } else if (s == '+') {
state = e_WAIT_FOR_DELIMITER_IN_S; state = e_WAIT_FOR_DELIMITER_IN_S;
@ -40,12 +43,12 @@ int cmpTopicWithWildcard(char *subscribedTopic, char *receivedTopic) {
r_idx++; r_idx++;
break; break;
} else { } else {
state = e_NOK; res = r_NOK;
break; break;
} }
case e_DELIMITER: case e_DELIMITER:
if (s == '#') { if (s == '#') {
state = e_OK; res = r_OK;
break; break;
} else if (s == '+') { } else if (s == '+') {
state = e_WAIT_FOR_DELIMITER_IN_S; state = e_WAIT_FOR_DELIMITER_IN_S;
@ -58,12 +61,12 @@ int cmpTopicWithWildcard(char *subscribedTopic, char *receivedTopic) {
r_idx++; r_idx++;
break; break;
} else { } else {
state = e_NOK; res = r_NOK;
break; break;
} }
case e_WAIT_FOR_DELIMITER_IN_S: case e_WAIT_FOR_DELIMITER_IN_S:
if (s != '/') { if (s != '/') {
state = e_NOK; res = r_NOK;
break; break;
} else if ((s == '/') && (r == '/')) { } else if ((s == '/') && (r == '/')) {
state = e_PART; state = e_PART;
@ -75,7 +78,7 @@ int cmpTopicWithWildcard(char *subscribedTopic, char *receivedTopic) {
r_idx++; r_idx++;
break; break;
} else { } else {
state = e_NOK; res = r_NOK;
break; break;
} }
case e_WAIT_FOR_DELIMITER_IN_R: case e_WAIT_FOR_DELIMITER_IN_R:
@ -90,7 +93,7 @@ int cmpTopicWithWildcard(char *subscribedTopic, char *receivedTopic) {
} }
case e_PART: case e_PART:
if (s == '#') { if (s == '#') {
state = e_OK; res = r_OK;
break; break;
} else if (s == '+') { } else if (s == '+') {
state = e_WAIT_FOR_DELIMITER_IN_S; state = e_WAIT_FOR_DELIMITER_IN_S;
@ -103,15 +106,13 @@ int cmpTopicWithWildcard(char *subscribedTopic, char *receivedTopic) {
r_idx++; r_idx++;
break; break;
} else { } else {
state = e_NOK; res = r_NOK;
break; break;
} }
} }
if (state == e_OK) { if (res != r_INVALID) {
return 0; return res;
} else if (state == e_NOK) {
return -1;
} }
} }
} }