processing a published valued on a subscribed topic is currently running in a task that is not the task executing the main loop(). that's because the espMqttClient(Secure) was constructed without arguments, which selects the constructor with two arguments priority and core, both of which have default values. that constructor selects espMqttClientTypes::UseInternalTask::YES, causing a task to be created in which context the MQTT client loop is executed. MQTT subscribers assume they are running in the same context as the main loop(). most code assumes exactly that. as the scheduler is preemptive and very little (none at all?) code is interlocked, we have to make sure to meet the programmer's expectations. this changeset calls the MQTT client loop in the context of the main loop() and enforces the use of espMqttClientTypes::UseInternalTask::NO.
44 lines
1.3 KiB
C++
44 lines
1.3 KiB
C++
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
#pragma once
|
|
|
|
#include "NetworkSettings.h"
|
|
#include <MqttSubscribeParser.h>
|
|
#include <Ticker.h>
|
|
#include <espMqttClient.h>
|
|
|
|
class MqttSettingsClass {
|
|
public:
|
|
MqttSettingsClass();
|
|
void init();
|
|
void loop();
|
|
void performReconnect();
|
|
bool getConnected();
|
|
void publish(const String& subtopic, const String& payload);
|
|
void publishGeneric(const String& topic, const String& payload, bool retain, uint8_t qos = 0);
|
|
|
|
void subscribe(const String& topic, uint8_t qos, const espMqttClientTypes::OnMessageCallback& cb);
|
|
void unsubscribe(const String& topic);
|
|
|
|
String getPrefix();
|
|
|
|
private:
|
|
void NetworkEvent(network_event event);
|
|
|
|
void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason);
|
|
void onMqttConnect(bool sessionPresent);
|
|
void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);
|
|
|
|
void performConnect();
|
|
void performDisconnect();
|
|
|
|
void createMqttClientObject();
|
|
|
|
MqttClient* mqttClient = nullptr;
|
|
String clientId;
|
|
String willTopic;
|
|
Ticker mqttReconnectTimer;
|
|
MqttSubscribeParser _mqttSubscribeParser;
|
|
bool _verboseLogging = true;
|
|
};
|
|
|
|
extern MqttSettingsClass MqttSettings; |