2021-04-03 06:54:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "configuration.h"
|
|
|
|
|
2021-04-03 14:27:06 +00:00
|
|
|
#include "concurrency/OSThread.h"
|
2021-04-04 01:20:37 +00:00
|
|
|
#include "mesh/Channels.h"
|
2021-04-03 06:54:10 +00:00
|
|
|
#include <PubSubClient.h>
|
|
|
|
#include <WiFiClient.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Our wrapper/singleton for sending/receiving MQTT "udp" packets. This object isolates the MQTT protocol implementation from
|
|
|
|
* the two components that use it: MQTTPlugin and MQTTSimInterface.
|
|
|
|
*/
|
2021-04-03 14:27:06 +00:00
|
|
|
class MQTT : private concurrency::OSThread
|
2021-04-03 06:54:10 +00:00
|
|
|
{
|
|
|
|
// supposedly the current version is busted:
|
|
|
|
// http://www.iotsharing.com/2017/08/how-to-use-esp32-mqtts-with-mqtts-mosquitto-broker-tls-ssl.html
|
|
|
|
// WiFiClientSecure wifiClient;
|
|
|
|
WiFiClient mqttClient;
|
|
|
|
PubSubClient pubSub;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MQTT();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Publish a packet on the glboal MQTT server.
|
2021-04-04 01:20:37 +00:00
|
|
|
* 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.
|
2021-04-03 06:54:10 +00:00
|
|
|
*/
|
2021-04-04 01:20:37 +00:00
|
|
|
void onSend(const MeshPacket &mp, ChannelIndex chIndex);
|
2021-04-03 06:54:10 +00:00
|
|
|
|
2021-04-03 14:27:06 +00:00
|
|
|
protected:
|
|
|
|
virtual int32_t runOnce();
|
|
|
|
|
2021-04-03 06:54:10 +00:00
|
|
|
private:
|
2021-04-04 23:48:46 +00:00
|
|
|
/** return true if we have a channel that wants uplink/downlink
|
|
|
|
*/
|
|
|
|
bool wantsLink() const;
|
|
|
|
|
|
|
|
/** Attempt to connect to server if necessary
|
|
|
|
*/
|
|
|
|
void reconnect();
|
2021-04-05 00:42:52 +00:00
|
|
|
|
|
|
|
/** Tell the server what subscriptions we want (based on channels.downlink_enabled)
|
|
|
|
*/
|
|
|
|
void sendSubscriptions();
|
|
|
|
|
|
|
|
/// Just C glue to call onPublish
|
|
|
|
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);
|
2021-04-03 06:54:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void mqttInit();
|
|
|
|
|
|
|
|
extern MQTT *mqtt;
|