MQTT client proxying (#2587)

* WIP on MQTT proxy message queue

* Fix copy paste goof

* Progress on uplink

* Has packets

* Avoid trying to connect if we're proxying

* Pointer correctly

* Remove wifi guards

* Client proxy subscribe

* Fixed method that got bababababorked somehow... personally I blame CoPilot

* Short circuit logic

* Remove canned settings

* Missed some stuff in the move

* Guard pubsub client for non-networked variants

* Has networking guard

* else

* Return statement for fall-thru

* More gaurd removals

* Removed source filters. No wonder I was confused

* Bounding

* Scope guard around else and fix return

* Portduino

* Defs instead

* Move macro up to actually fix portduino

* Size_t

* Unsigned int

* Thread interval

* Protos

* Protobufs ref
This commit is contained in:
Ben Meadors
2023-07-08 20:37:04 -05:00
committed by GitHub
parent da389eb787
commit 6e96216ba3
21 changed files with 339 additions and 103 deletions

View File

@@ -5,15 +5,20 @@
#include "concurrency/OSThread.h"
#include "mesh/Channels.h"
#include "mesh/generated/meshtastic/mqtt.pb.h"
#include <PubSubClient.h>
#if HAS_WIFI
#include <WiFiClient.h>
#define HAS_NETWORKING 1
#if !defined(ARCH_PORTDUINO)
#include <WiFiClientSecure.h>
#endif
#endif
#if HAS_ETHERNET
#include <EthernetClient.h>
#define HAS_NETWORKING 1
#endif
#ifdef HAS_NETWORKING
#include <PubSubClient.h>
#endif
#define MAX_MQTT_QUEUE 16
@@ -35,12 +40,9 @@ class MQTT : private concurrency::OSThread
#if HAS_ETHERNET
EthernetClient mqttClient;
#endif
#if !defined(DEBUG_HEAP_MQTT)
PubSubClient pubSub;
public:
#else
public:
#ifdef HAS_NETWORKING
PubSubClient pubSub;
#endif
MQTT();
@@ -59,7 +61,13 @@ class MQTT : private concurrency::OSThread
*/
void reconnect();
bool connected();
bool isConnectedDirectly();
bool publish(const char *topic, const char *payload, bool retained);
bool publish(const char *topic, const uint8_t *payload, size_t length, const bool retained);
void onClientProxyReceive(meshtastic_MqttClientProxyMessage msg);
protected:
PointerQueue<meshtastic_ServiceEnvelope> mqttQueue;
@@ -80,14 +88,17 @@ class MQTT : private concurrency::OSThread
*/
void sendSubscriptions();
/// Just C glue to call onPublish
/// Callback for direct mqtt subscription messages
static void mqttCallback(char *topic, byte *payload, unsigned int length);
/// Called when a new publish arrives from the MQTT server
void onPublish(char *topic, byte *payload, unsigned int length);
void onReceive(char *topic, byte *payload, size_t length);
/// Called when a new publish arrives from the MQTT server
std::string downstreamPacketToJson(meshtastic_MeshPacket *mp);
std::string meshPacketToJson(meshtastic_MeshPacket *mp);
void publishStatus();
void publishQueuedMessages();
/// Return 0 if sleep is okay, veto sleep if we are connected to pubsub server
// int preflightSleepCb(void *unused = NULL) { return pubSub.connected() ? 1 : 0; }