mirror of
https://github.com/meshtastic/firmware.git
synced 2026-01-28 12:42:00 +00:00
add a .clang-format file (#9154)
This commit is contained in:
1399
src/mqtt/MQTT.cpp
1399
src/mqtt/MQTT.cpp
File diff suppressed because it is too large
Load Diff
139
src/mqtt/MQTT.h
139
src/mqtt/MQTT.h
@@ -27,115 +27,114 @@
|
||||
#define MAX_MQTT_QUEUE 16
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* 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.
|
||||
*/
|
||||
class MQTT : private concurrency::OSThread
|
||||
{
|
||||
public:
|
||||
MQTT();
|
||||
class MQTT : private concurrency::OSThread {
|
||||
public:
|
||||
MQTT();
|
||||
|
||||
/**
|
||||
* Publish a packet on the global MQTT server.
|
||||
* @param mp_encrypted the encrypted packet to publish
|
||||
* @param mp_decoded the decrypted packet to publish
|
||||
* @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 - because no way to find a global channel ID.
|
||||
*/
|
||||
void onSend(const meshtastic_MeshPacket &mp_encrypted, const meshtastic_MeshPacket &mp_decoded, ChannelIndex chIndex);
|
||||
/**
|
||||
* Publish a packet on the global MQTT server.
|
||||
* @param mp_encrypted the encrypted packet to publish
|
||||
* @param mp_decoded the decrypted packet to publish
|
||||
* @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 - because no way to find a global channel ID.
|
||||
*/
|
||||
void onSend(const meshtastic_MeshPacket &mp_encrypted, const meshtastic_MeshPacket &mp_decoded, ChannelIndex chIndex);
|
||||
|
||||
bool isConnectedDirectly();
|
||||
bool isConnectedDirectly();
|
||||
|
||||
bool publish(const char *topic, const char *payload, bool retained);
|
||||
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);
|
||||
bool publish(const char *topic, const uint8_t *payload, size_t length, const bool retained);
|
||||
|
||||
void onClientProxyReceive(meshtastic_MqttClientProxyMessage msg);
|
||||
void onClientProxyReceive(meshtastic_MqttClientProxyMessage msg);
|
||||
|
||||
bool isEnabled() { return this->enabled; };
|
||||
bool isEnabled() { return this->enabled; };
|
||||
|
||||
void start() { setIntervalFromNow(0); };
|
||||
void start() { setIntervalFromNow(0); };
|
||||
|
||||
bool isUsingDefaultServer() { return isConfiguredForDefaultServer; }
|
||||
bool isUsingDefaultRootTopic() { return isConfiguredForDefaultRootTopic; }
|
||||
bool isUsingDefaultServer() { return isConfiguredForDefaultServer; }
|
||||
bool isUsingDefaultRootTopic() { return isConfiguredForDefaultRootTopic; }
|
||||
|
||||
/// Validate the meshtastic_ModuleConfig_MQTTConfig.
|
||||
static bool isValidConfig(const meshtastic_ModuleConfig_MQTTConfig &config) { return isValidConfig(config, nullptr); }
|
||||
/// Validate the meshtastic_ModuleConfig_MQTTConfig.
|
||||
static bool isValidConfig(const meshtastic_ModuleConfig_MQTTConfig &config) { return isValidConfig(config, nullptr); }
|
||||
|
||||
protected:
|
||||
struct QueueEntry {
|
||||
std::string topic;
|
||||
std::basic_string<uint8_t> envBytes; // binary/pb_encode_to_bytes ServiceEnvelope
|
||||
};
|
||||
PointerQueue<QueueEntry> mqttQueue;
|
||||
protected:
|
||||
struct QueueEntry {
|
||||
std::string topic;
|
||||
std::basic_string<uint8_t> envBytes; // binary/pb_encode_to_bytes ServiceEnvelope
|
||||
};
|
||||
PointerQueue<QueueEntry> mqttQueue;
|
||||
|
||||
int reconnectCount = 0;
|
||||
bool isConfiguredForDefaultServer = true;
|
||||
bool isConfiguredForDefaultRootTopic = true;
|
||||
int reconnectCount = 0;
|
||||
bool isConfiguredForDefaultServer = true;
|
||||
bool isConfiguredForDefaultRootTopic = true;
|
||||
|
||||
virtual int32_t runOnce() override;
|
||||
virtual int32_t runOnce() override;
|
||||
|
||||
#ifndef PIO_UNIT_TESTING
|
||||
private:
|
||||
private:
|
||||
#endif
|
||||
#if HAS_WIFI
|
||||
using MQTTClient = WiFiClient;
|
||||
using MQTTClient = WiFiClient;
|
||||
#if __has_include(<WiFiClientSecure.h>)
|
||||
using MQTTClientTLS = WiFiClientSecure;
|
||||
using MQTTClientTLS = WiFiClientSecure;
|
||||
#define MQTT_SUPPORTS_TLS 1
|
||||
#endif
|
||||
#elif HAS_ETHERNET
|
||||
using MQTTClient = EthernetClient;
|
||||
using MQTTClient = EthernetClient;
|
||||
#else
|
||||
using MQTTClient = void;
|
||||
using MQTTClient = void;
|
||||
#endif
|
||||
|
||||
#if HAS_NETWORKING
|
||||
std::unique_ptr<MQTTClient> mqttClient;
|
||||
std::unique_ptr<MQTTClient> mqttClient;
|
||||
#if MQTT_SUPPORTS_TLS
|
||||
MQTTClientTLS mqttClientTLS;
|
||||
MQTTClientTLS mqttClientTLS;
|
||||
#endif
|
||||
PubSubClient pubSub;
|
||||
explicit MQTT(std::unique_ptr<MQTTClient> mqttClient);
|
||||
PubSubClient pubSub;
|
||||
explicit MQTT(std::unique_ptr<MQTTClient> mqttClient);
|
||||
#endif
|
||||
|
||||
std::string cryptTopic = "/2/e/"; // msh/2/e/CHANNELID/NODEID
|
||||
std::string jsonTopic = "/2/json/"; // msh/2/json/CHANNELID/NODEID
|
||||
std::string mapTopic = "/2/map/"; // For protobuf-encoded MapReport messages
|
||||
std::string cryptTopic = "/2/e/"; // msh/2/e/CHANNELID/NODEID
|
||||
std::string jsonTopic = "/2/json/"; // msh/2/json/CHANNELID/NODEID
|
||||
std::string mapTopic = "/2/map/"; // For protobuf-encoded MapReport messages
|
||||
|
||||
// For map reporting (only applies when enabled)
|
||||
const uint32_t default_map_position_precision = 14; // defaults to max. offset of ~1459m
|
||||
uint32_t last_report_to_map = 0;
|
||||
uint32_t map_position_precision = default_map_position_precision;
|
||||
uint32_t map_publish_interval_msecs = default_map_publish_interval_secs * 1000;
|
||||
// For map reporting (only applies when enabled)
|
||||
const uint32_t default_map_position_precision = 14; // defaults to max. offset of ~1459m
|
||||
uint32_t last_report_to_map = 0;
|
||||
uint32_t map_position_precision = default_map_position_precision;
|
||||
uint32_t map_publish_interval_msecs = default_map_publish_interval_secs * 1000;
|
||||
|
||||
/** Attempt to connect to server if necessary
|
||||
*/
|
||||
void reconnect();
|
||||
/** Attempt to connect to server if necessary
|
||||
*/
|
||||
void reconnect();
|
||||
|
||||
/** Tell the server what subscriptions we want (based on channels.downlink_enabled)
|
||||
*/
|
||||
void sendSubscriptions();
|
||||
/** Tell the server what subscriptions we want (based on channels.downlink_enabled)
|
||||
*/
|
||||
void sendSubscriptions();
|
||||
|
||||
/// Callback for direct mqtt subscription messages
|
||||
static void mqttCallback(char *topic, byte *payload, unsigned int length);
|
||||
/// Callback for direct mqtt subscription messages
|
||||
static void mqttCallback(char *topic, byte *payload, unsigned int length);
|
||||
|
||||
static bool isValidConfig(const meshtastic_ModuleConfig_MQTTConfig &config, MQTTClient *client);
|
||||
static bool isValidConfig(const meshtastic_ModuleConfig_MQTTConfig &config, MQTTClient *client);
|
||||
|
||||
/// Called when a new publish arrives from the MQTT server
|
||||
void onReceive(char *topic, byte *payload, size_t length);
|
||||
/// Called when a new publish arrives from the MQTT server
|
||||
void onReceive(char *topic, byte *payload, size_t length);
|
||||
|
||||
void publishQueuedMessages();
|
||||
void publishQueuedMessages();
|
||||
|
||||
void publishNodeInfo();
|
||||
void publishNodeInfo();
|
||||
|
||||
// Check if we should report unencrypted information about our node for consumption by a map
|
||||
void perhapsReportToMap();
|
||||
// Check if we should report unencrypted information about our node for consumption by a map
|
||||
void perhapsReportToMap();
|
||||
|
||||
/// 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; }
|
||||
/// 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; }
|
||||
};
|
||||
|
||||
void mqttInit();
|
||||
|
||||
@@ -4,20 +4,16 @@
|
||||
|
||||
DecodedServiceEnvelope::DecodedServiceEnvelope(const uint8_t *payload, size_t length)
|
||||
: meshtastic_ServiceEnvelope(meshtastic_ServiceEnvelope_init_default),
|
||||
validDecode(pb_decode_from_bytes(payload, length, &meshtastic_ServiceEnvelope_msg, this))
|
||||
{
|
||||
}
|
||||
validDecode(pb_decode_from_bytes(payload, length, &meshtastic_ServiceEnvelope_msg, this)) {}
|
||||
|
||||
DecodedServiceEnvelope::DecodedServiceEnvelope(DecodedServiceEnvelope &&other)
|
||||
: meshtastic_ServiceEnvelope(meshtastic_ServiceEnvelope_init_zero), validDecode(other.validDecode)
|
||||
{
|
||||
std::swap(packet, other.packet);
|
||||
std::swap(channel_id, other.channel_id);
|
||||
std::swap(gateway_id, other.gateway_id);
|
||||
: meshtastic_ServiceEnvelope(meshtastic_ServiceEnvelope_init_zero), validDecode(other.validDecode) {
|
||||
std::swap(packet, other.packet);
|
||||
std::swap(channel_id, other.channel_id);
|
||||
std::swap(gateway_id, other.gateway_id);
|
||||
}
|
||||
|
||||
DecodedServiceEnvelope::~DecodedServiceEnvelope()
|
||||
{
|
||||
if (validDecode)
|
||||
pb_release(&meshtastic_ServiceEnvelope_msg, this);
|
||||
DecodedServiceEnvelope::~DecodedServiceEnvelope() {
|
||||
if (validDecode)
|
||||
pb_release(&meshtastic_ServiceEnvelope_msg, this);
|
||||
}
|
||||
@@ -4,10 +4,10 @@
|
||||
|
||||
// meshtastic_ServiceEnvelope that automatically releases dynamically allocated memory when it goes out of scope.
|
||||
struct DecodedServiceEnvelope : public meshtastic_ServiceEnvelope {
|
||||
DecodedServiceEnvelope(const uint8_t *payload, size_t length);
|
||||
DecodedServiceEnvelope(DecodedServiceEnvelope &) = delete;
|
||||
DecodedServiceEnvelope(DecodedServiceEnvelope &&);
|
||||
~DecodedServiceEnvelope();
|
||||
// Clients must check that this is true before using.
|
||||
const bool validDecode;
|
||||
DecodedServiceEnvelope(const uint8_t *payload, size_t length);
|
||||
DecodedServiceEnvelope(DecodedServiceEnvelope &) = delete;
|
||||
DecodedServiceEnvelope(DecodedServiceEnvelope &&);
|
||||
~DecodedServiceEnvelope();
|
||||
// Clients must check that this is true before using.
|
||||
const bool validDecode;
|
||||
};
|
||||
Reference in New Issue
Block a user