Skip to content

Accept wildcards in mqtt subscription prefix #1497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions MySensors-Arduino.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/bin" />
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
3 changes: 1 addition & 2 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -577,10 +577,9 @@ if [ -z "${SOC}" ]; then
info=($(detect_machine))
SOC=${info[0]}
TYPE=${info[1]}
CPU=${info[2]}
printf " ${OK} machine detected: SoC=${SOC}, Type=${TYPE}, CPU=${CPU}.\n"
fi

CPU=$(eval "uname -m 2>/dev/null")
if [ -z "${CPUFLAGS}" ]; then
CPUFLAGS=$(gcc_cpu_flags "${SOC}" "${CPU}")
fi
Expand Down
23 changes: 19 additions & 4 deletions core/MyProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,29 @@ bool protocolMQTT2MyMessage(MyMessage &message, char *topic, uint8_t *payload,
const unsigned int length)
{
char *str, *p;
uint8_t index = 0;
int8_t index = -1;
message.setSender(GATEWAY_ADDRESS);
message.setLast(GATEWAY_ADDRESS);
message.setEcho(false);
for (str = strtok_r(topic + strlen(MY_MQTT_SUBSCRIBE_TOPIC_PREFIX) + 1, "/", &p);

// The subscription prefix can contain wildcards, but only the + wildcard.
Copy link
Member

@Yveaux Yveaux Aug 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is only the single-level + wildcard supported?
If we add support for wildcards, users will expect to be able to use multi-level # wildcards too.
If you parse right-to-left for the mysensors message field you could support any wildcard.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MQTT multi-level wildcard (#) is only allowed at the end of the topic. Because the prefix is always followed by other levels it cannot contain the # wildcard, although "my" code wouldn't complain about it.

The code only checks the number of / characters.
The syntactical check is left to the MQTT broker. That was already the case and I think that is right. I have browsed the MySensors code and checked all code where the PREFIX is used and none of needed a change.

// e.g. subscription prefix "+/mysensors", actual prefix "foo/mysensors"
// To fetch only the part after the mqtt prefix, discard as many
// mqtt levels (between / characters) as in the subscription prefix.
// Make a copy of the subscribe prefix because strtok is allowed to change the string.
char subscribeTopicPrefix[strlen(MY_MQTT_SUBSCRIBE_TOPIC_PREFIX) + 1];
strcpy(subscribeTopicPrefix,MY_MQTT_SUBSCRIBE_TOPIC_PREFIX);
char *strPrefix, *savePointerPrefix;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the increase in ram/flash for this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The savepointer (4 bytes?) is stored on stack I guess.
The increase in ram (stack use?) for the copy of the prefix string will be the length of the prefix plus 1.
The default value for the prefix is mygateway1-in, length is 13, so it will use 14 bytes on the stack. Your comment made me think about this memory usage. Actually I think malloc/free would be safer. Or maybe choose in runtime between stack and malloc depending on the prefix length? The length of the string is known at compile time, but I don't think that will help. I am not an expert in this subject.

Flash use? Is that the code length? The answer is: I don't know.

I compiled and ran it only on a raspberry pi 2, not on an Arduino.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In core/MyGatewayTransportMQTTClient.cpp, method reconnectMQTT, there is also a local variable inTopic with a length of the prefix plus 10. So I think it is acceptable to have it as a stack variable.

for (str = strtok_r(topic, "/", &p),
strPrefix = strtok_r(subscribeTopicPrefix,"/", &savePointerPrefix);
str && index < 5;
str = strtok_r(NULL, "/", &p), index++
str = strtok_r(NULL, "/", &p),
strPrefix = strtok_r(NULL, "/", &savePointerPrefix)
) {
// Increment index (init value is -1) if we have consumed the mqtt subscription prefix
if (!strPrefix) {
index++;
}
switch (index) {
case 0:
// Node id
Expand Down Expand Up @@ -163,5 +178,5 @@ bool protocolMQTT2MyMessage(MyMessage &message, char *topic, uint8_t *payload,
}
}
// Return true if input valid
return (index == 5);
return (index == 4);
}