MQTT JSON downlink fixes (#3183)

* Fix getting channel name from MQTT topic

* Allow specifying channel index in JSON field "channel" for downlink
Still requires JSON message to be published to channel named "mqtt"

* Make non-breaking if someone adds another slash
This commit is contained in:
GUVWAF 2024-02-08 22:43:24 +01:00 committed by GitHub
parent f4151a7108
commit a40b4e4d69
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -52,11 +52,10 @@ void MQTT::onReceive(char *topic, byte *payload, size_t length)
JSONObject json;
json = json_value->AsObject();
// parse the channel name from the topic string
char *ptr = strtok(topic, "/");
for (int i = 0; i < 3; i++) {
ptr = strtok(NULL, "/");
}
// parse the channel name from the topic string by looking for "json/"
const char *jsonSlash = "json/";
char *ptr = strstr(topic, jsonSlash) + sizeof(jsonSlash) + 1; // set pointer to after "json/"
ptr = strtok(ptr, "/") ? strtok(ptr, "/") : ptr; // if another "/" was added, parse string up to that character
meshtastic_Channel sendChannel = channels.getByName(ptr);
// We allow downlink JSON packets only on a channel named "mqtt"
if (strncasecmp(channels.getGlobalId(sendChannel.index), Channels::mqttChannel, strlen(Channels::mqttChannel)) == 0 &&
@ -70,7 +69,9 @@ void MQTT::onReceive(char *topic, byte *payload, size_t length)
// construct protobuf data packet using TEXT_MESSAGE, send it to the mesh
meshtastic_MeshPacket *p = router->allocForSending();
p->decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP;
p->channel = sendChannel.index;
if (json.find("channel") != json.end() && json["channel"]->IsNumber() &&
(json["channel"]->AsNumber() < channels.getNumChannels()))
p->channel = json["channel"]->AsNumber();
if (json.find("to") != json.end() && json["to"]->IsNumber())
p->to = json["to"]->AsNumber();
if (jsonPayloadStr.length() <= sizeof(p->decoded.payload.bytes)) {
@ -98,7 +99,9 @@ void MQTT::onReceive(char *topic, byte *payload, size_t length)
// construct protobuf data packet using POSITION, send it to the mesh
meshtastic_MeshPacket *p = router->allocForSending();
p->decoded.portnum = meshtastic_PortNum_POSITION_APP;
p->channel = sendChannel.index;
if (json.find("channel") != json.end() && json["channel"]->IsNumber() &&
(json["channel"]->AsNumber() < channels.getNumChannels()))
p->channel = json["channel"]->AsNumber();
if (json.find("to") != json.end() && json["to"]->IsNumber())
p->to = json["to"]->AsNumber();
p->decoded.payload.size =