-
-
Notifications
You must be signed in to change notification settings - Fork 894
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
Accept wildcards in mqtt subscription prefix #1497
base: development
Are you sure you want to change the base?
Changes from 2 commits
e9446b2
45cb263
b36c846
aeb62d2
4a8bb44
75e131b
23eb210
305992b
72f713a
fea7b0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,14 +111,30 @@ 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. | ||
// 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the increase in ram/flash for this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The savepointer (4 bytes?) is stored on stack I guess. 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++; | ||
} | ||
//GATEWAY_DEBUG(PSTR("GWT:TPS:Index=%d, mqttstr=%s, prefixtoken=%s\n"), index, str,strPrefix); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove dead code |
||
switch (index) { | ||
case 0: | ||
// Node id | ||
|
@@ -163,5 +179,5 @@ bool protocolMQTT2MyMessage(MyMessage &message, char *topic, uint8_t *payload, | |
} | ||
} | ||
// Return true if input valid | ||
return (index == 5); | ||
return (index == 4); | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.