Skip to content

Commit

Permalink
Add unit tests for MQTT (#5724)
Browse files Browse the repository at this point in the history
* Add unit tests for MQTT

* Test received fields
  • Loading branch information
esev authored Jan 7, 2025
1 parent 3537406 commit 27fbfd0
Show file tree
Hide file tree
Showing 7 changed files with 876 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/mesh/MeshService.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class MeshService
void sendToPhone(meshtastic_MeshPacket *p);

/// Send an MQTT message to the phone for client proxying
void sendMqttMessageToClientProxy(meshtastic_MqttClientProxyMessage *m);
virtual void sendMqttMessageToClientProxy(meshtastic_MqttClientProxyMessage *m);

/// Send a ClientNotification to the phone
void sendClientNotification(meshtastic_ClientNotification *cn);
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/NodeDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class NodeDB
return &meshNodes->at(x);
}

meshtastic_NodeInfoLite *getMeshNode(NodeNum n);
virtual meshtastic_NodeInfoLite *getMeshNode(NodeNum n);
size_t getNumMeshNodes() { return numMeshNodes; }

// returns true if the maximum number of nodes is reached or we are running low on memory
Expand Down
1 change: 1 addition & 0 deletions src/mesh/RadioLibInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ void RadioLibInterface::handleReceiveInterrupt()
// nodes.
meshtastic_MeshPacket *mp = packetPool.allocZeroed();

// Keep the assigned fields in sync with src/mqtt/MQTT.cpp:onReceiveProto
mp->from = radioBuffer.header.from;
mp->to = radioBuffer.header.to;
mp->id = radioBuffer.header.id;
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/Router.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Router : protected concurrency::OSThread
* RadioInterface calls this to queue up packets that have been received from the radio. The router is now responsible for
* freeing the packet
*/
void enqueueReceivedMessage(meshtastic_MeshPacket *p);
virtual void enqueueReceivedMessage(meshtastic_MeshPacket *p);

/**
* Send a packet on a suitable interface. This routine will
Expand Down
3 changes: 2 additions & 1 deletion src/modules/RoutingModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class RoutingModule : public ProtobufModule<meshtastic_Routing>
*/
RoutingModule();

void sendAckNak(meshtastic_Routing_Error err, NodeNum to, PacketId idFrom, ChannelIndex chIndex, uint8_t hopLimit = 0);
virtual void sendAckNak(meshtastic_Routing_Error err, NodeNum to, PacketId idFrom, ChannelIndex chIndex,
uint8_t hopLimit = 0);

// Given the hopStart and hopLimit upon reception of a request, return the hop limit to use for the response
uint8_t getHopLimitForResponse(uint8_t hopStart, uint8_t hopLimit);
Expand Down
18 changes: 14 additions & 4 deletions src/mqtt/MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,22 @@ inline void onReceiveProto(char *topic, byte *payload, size_t length)
return;
}
LOG_INFO("Received MQTT topic %s, len=%u", topic, length);
if (e.packet->hop_limit > HOP_MAX || e.packet->hop_start > HOP_MAX) {
LOG_INFO("Invalid hop_limit(%u) or hop_start(%u)", e.packet->hop_limit, e.packet->hop_start);
return;
}

UniquePacketPoolPacket p = packetPool.allocUniqueCopy(*e.packet);
UniquePacketPoolPacket p = packetPool.allocUniqueZeroed();
p->from = e.packet->from;
p->to = e.packet->to;
p->id = e.packet->id;
p->channel = e.packet->channel;
p->hop_limit = e.packet->hop_limit;
p->hop_start = e.packet->hop_start;
p->want_ack = e.packet->want_ack;
p->via_mqtt = true; // Mark that the packet was received via MQTT
// Unset received SNR/RSSI which might have been added by the MQTT gateway
p->rx_snr = 0;
p->rx_rssi = 0;
p->which_payload_variant = e.packet->which_payload_variant;
memcpy(&p->decoded, &e.packet->decoded, std::max(sizeof(p->decoded), sizeof(p->encrypted)));

if (p->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
if (moduleConfig.mqtt.encryption_enabled) {
Expand Down
Loading

0 comments on commit 27fbfd0

Please sign in to comment.