mqtt: send packets after they are encrypted

This commit is contained in:
Kevin Hester
2021-04-04 09:20:37 +08:00
parent 638cec7f25
commit d19af8b83d
7 changed files with 59 additions and 57 deletions

View File

@@ -6,6 +6,7 @@
#include "configuration.h"
#include "main.h"
#include "mesh-pb-constants.h"
#include "mqtt/MQTT.h"
#include "plugins/RoutingPlugin.h"
/**
@@ -187,7 +188,7 @@ ErrorCode Router::send(MeshPacket *p)
assert(p->which_payloadVariant == MeshPacket_encrypted_tag ||
p->which_payloadVariant == MeshPacket_decoded_tag); // I _think_ all packets should have a payload by now
// First convert from protobufs to raw bytes
// If the packet is not yet encrypted, do so now
if (p->which_payloadVariant == MeshPacket_decoded_tag) {
static uint8_t bytes[MAX_RHPACKETLEN]; // we have to use a scratch buffer because a union
@@ -202,7 +203,8 @@ ErrorCode Router::send(MeshPacket *p)
// printBytes("plaintext", bytes, numbytes);
auto hash = channels.setActiveByIndex(p->channel);
ChannelIndex chIndex = p->channel; // keep as a local because we are about to change it
auto hash = channels.setActiveByIndex(chIndex);
if (hash < 0) {
// No suitable channel could be found for sending
abortSendAndNak(Routing_Error_NO_CHANNEL, p);
@@ -217,6 +219,9 @@ ErrorCode Router::send(MeshPacket *p)
memcpy(p->encrypted.bytes, bytes, numbytes);
p->encrypted.size = numbytes;
p->which_payloadVariant = MeshPacket_encrypted_tag;
if (mqtt)
mqtt->onSend(*p, chIndex);
}
assert(iface); // This should have been detected already in sendLocal (or we just received a packet from outside)

View File

@@ -1,7 +1,7 @@
#include "MQTT.h"
#include "MQTTPlugin.h"
#include "NodeDB.h"
#include "main.h"
#include "mesh/Channels.h"
#include "mesh/generated/mqtt.pb.h"
#include <WiFi.h>
#include <assert.h>
@@ -27,7 +27,6 @@ void mqttInit()
DEBUG_MSG("WiFi is not connected, can not start MQTT\n");
else {
new MQTT();
new MQTTPlugin();
}
}
@@ -74,13 +73,13 @@ int32_t MQTT::runOnce()
return 20;
}
void MQTT::publish(const MeshPacket &mp)
void MQTT::onSend(const MeshPacket &mp, ChannelIndex chIndex)
{
// don't bother sending if not connected...
if (pubSub.connected()) {
// FIXME - check uplink enabled
const char *channelId = "fixmechan";
const char *channelId = channels.getName(chIndex); // FIXME, for now we just use the human name for the channel
ServiceEnvelope env = ServiceEnvelope_init_default;
env.channel_id = (char *)channelId;

View File

@@ -3,6 +3,7 @@
#include "configuration.h"
#include "concurrency/OSThread.h"
#include "mesh/Channels.h"
#include <PubSubClient.h>
#include <WiFiClient.h>
@@ -23,8 +24,13 @@ class MQTT : private concurrency::OSThread
/**
* Publish a packet on the glboal MQTT server.
* This hook must be called **after** the packet is encrypted (including the channel being changed to a hash).
* @param chIndex the index of the channel for this message
*
* Note: for messages we are forwarding on the mesh that we can't find the channel for (because we don't have the keys), we
* can not forward those messages to the cloud - becuase no way to find a global channel ID.
*/
void publish(const MeshPacket &mp);
void onSend(const MeshPacket &mp, ChannelIndex chIndex);
protected:
virtual int32_t runOnce();

View File

@@ -1,18 +0,0 @@
#include "MQTTPlugin.h"
#include "MQTT.h"
#include "MeshService.h"
#include "NodeDB.h"
#include "Router.h"
#include "configuration.h"
#include "main.h"
MQTTPlugin::MQTTPlugin() : MeshPlugin("mqtt")
{
isPromiscuous = true; // We always want to update our nodedb, even if we are sniffing on others
}
bool MQTTPlugin::handleReceived(const MeshPacket &mp)
{
mqtt->publish(mp);
return false; // never claim handled
}

View File

@@ -1,17 +0,0 @@
#pragma once
#include "MeshPlugin.h"
/**
* NodeInfo plugin for sending/receiving NodeInfos into the mesh
*/
class MQTTPlugin : public MeshPlugin
{
public:
MQTTPlugin();
protected:
/** We sniff all packets */
virtual bool handleReceived(const MeshPacket &mp);
virtual bool wantPacket(const MeshPacket *p) { return true; }
};