|
|
@@ -11,6 +11,9 @@
|
|
|
#include "MainFlowControl.h"
|
|
|
#include "cJSON.h"
|
|
|
#include "../../include/defines.h"
|
|
|
+#include <mutex>
|
|
|
+#include <new>
|
|
|
+#include <utility>
|
|
|
|
|
|
#if DEBUG_DETAIL_ON
|
|
|
#include "esp_timer.h"
|
|
|
@@ -18,8 +21,10 @@
|
|
|
|
|
|
static const char *TAG = "MQTT IF";
|
|
|
|
|
|
-std::map<std::string, std::function<void()>>* connectFunktionMap = NULL;
|
|
|
+std::map<std::string, std::function<void()>>* connectFunktionMap = NULL;
|
|
|
std::map<std::string, std::function<bool(std::string, char*, int)>>* subscribeFunktionMap = NULL;
|
|
|
+static std::mutex connectFunctionMutex;
|
|
|
+static std::mutex subscribeFunctionMutex;
|
|
|
|
|
|
int failedOnRound = -1;
|
|
|
int MQTTReconnectCnt = 0;
|
|
|
@@ -136,13 +141,18 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) {
|
|
|
ESP_LOGD(TAG, "TOPIC=%.*s", event->topic_len, event->topic);
|
|
|
ESP_LOGD(TAG, "DATA=%.*s", event->data_len, event->data);
|
|
|
topic.assign(event->topic, event->topic_len);
|
|
|
- if (subscribeFunktionMap != NULL) {
|
|
|
- if (subscribeFunktionMap->find(topic) != subscribeFunktionMap->end()) {
|
|
|
- ESP_LOGD(TAG, "call subcribe function for topic %s", topic.c_str());
|
|
|
- (*subscribeFunktionMap)[topic](topic, event->data, event->data_len);
|
|
|
+ {
|
|
|
+ // Keep callback targets alive until invocation finishes. Unregister waits for this lock.
|
|
|
+ std::lock_guard<std::mutex> lock(subscribeFunctionMutex);
|
|
|
+ if (subscribeFunktionMap != NULL) {
|
|
|
+ const auto handler = subscribeFunktionMap->find(topic);
|
|
|
+ if (handler != subscribeFunktionMap->end()) {
|
|
|
+ ESP_LOGD(TAG, "call subcribe function for topic %s", topic.c_str());
|
|
|
+ handler->second(topic, event->data, event->data_len);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ ESP_LOGW(TAG, "no handler available\r\n");
|
|
|
}
|
|
|
- } else {
|
|
|
- ESP_LOGW(TAG, "no handler available\r\n");
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
@@ -446,10 +456,14 @@ void MQTTconnected(){
|
|
|
if (mqtt_connected) {
|
|
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Connected to broker");
|
|
|
|
|
|
- if (connectFunktionMap != NULL) {
|
|
|
- for(std::map<std::string, std::function<void()>>::iterator it = connectFunktionMap->begin(); it != connectFunktionMap->end(); ++it) {
|
|
|
- it->second();
|
|
|
- ESP_LOGD(TAG, "call connect function %s", it->first.c_str());
|
|
|
+ {
|
|
|
+ // Callback owners cannot be destroyed until a connect callback has returned.
|
|
|
+ std::lock_guard<std::mutex> lock(connectFunctionMutex);
|
|
|
+ if (connectFunktionMap != NULL) {
|
|
|
+ for(std::map<std::string, std::function<void()>>::iterator it = connectFunktionMap->begin(); it != connectFunktionMap->end(); ++it) {
|
|
|
+ it->second();
|
|
|
+ ESP_LOGD(TAG, "call connect function %s", it->first.c_str());
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -462,10 +476,13 @@ void MQTTconnected(){
|
|
|
std::function<bool(std::string topic, char* data, int data_len)> subHandler2 = mqtt_handler_set_prevalue;
|
|
|
MQTTregisterSubscribeFunction(maintopic + "/ctrl/set_prevalue", subHandler2); // subcribe to maintopic/ctrl/set_prevalue
|
|
|
|
|
|
- if (subscribeFunktionMap != NULL) {
|
|
|
- for(std::map<std::string, std::function<bool(std::string, char*, int)>>::iterator it = subscribeFunktionMap->begin(); it != subscribeFunktionMap->end(); ++it) {
|
|
|
- int msg_id = esp_mqtt_client_subscribe(client, it->first.c_str(), 0);
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "topic " + it->first + " subscribe successful");
|
|
|
+ {
|
|
|
+ std::lock_guard<std::mutex> lock(subscribeFunctionMutex);
|
|
|
+ if (subscribeFunktionMap != NULL && client != NULL) {
|
|
|
+ for(std::map<std::string, std::function<bool(std::string, char*, int)>>::iterator it = subscribeFunktionMap->begin(); it != subscribeFunktionMap->end(); ++it) {
|
|
|
+ const int msg_id = esp_mqtt_client_subscribe(client, it->first.c_str(), 0);
|
|
|
+ LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "topic " + it->first + " subscribe requested, msg_id=" + std::to_string(msg_id));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -477,47 +494,78 @@ void MQTTconnected(){
|
|
|
}
|
|
|
|
|
|
void MQTTregisterConnectFunction(std::string name, std::function<void()> func){
|
|
|
- ESP_LOGD(TAG, "MQTTregisteronnectFunction %s\r\n", name.c_str());
|
|
|
+ ESP_LOGD(TAG, "MQTTregisterConnectFunction %s\r\n", name.c_str());
|
|
|
+ std::lock_guard<std::mutex> lock(connectFunctionMutex);
|
|
|
if (connectFunktionMap == NULL) {
|
|
|
- connectFunktionMap = new std::map<std::string, std::function<void()>>();
|
|
|
+ connectFunktionMap = new (std::nothrow) std::map<std::string, std::function<void()>>();
|
|
|
+ if (connectFunktionMap == NULL) {
|
|
|
+ ESP_LOGE(TAG, "Unable to allocate MQTT connect callback map");
|
|
|
+ return;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- if ((*connectFunktionMap)[name] != NULL) {
|
|
|
- ESP_LOGW(TAG, "connect function %s already registred", name.c_str());
|
|
|
+ if (connectFunktionMap->find(name) != connectFunktionMap->end()) {
|
|
|
+ ESP_LOGW(TAG, "connect function %s already registered", name.c_str());
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- (*connectFunktionMap)[name] = func;
|
|
|
-
|
|
|
+ connectFunktionMap->emplace(name, std::move(func));
|
|
|
if (mqtt_connected) {
|
|
|
- func();
|
|
|
+ connectFunktionMap->find(name)->second();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void MQTTunregisterConnectFunction(std::string name){
|
|
|
- ESP_LOGD(TAG, "unregisterConnnectFunction %s\r\n", name.c_str());
|
|
|
- if ((connectFunktionMap != NULL) && (connectFunktionMap->find(name) != connectFunktionMap->end())) {
|
|
|
+ ESP_LOGD(TAG, "unregisterConnectFunction %s\r\n", name.c_str());
|
|
|
+ std::lock_guard<std::mutex> lock(connectFunctionMutex);
|
|
|
+ if (connectFunktionMap != NULL) {
|
|
|
connectFunktionMap->erase(name);
|
|
|
+ if (connectFunktionMap->empty()) {
|
|
|
+ delete connectFunktionMap;
|
|
|
+ connectFunktionMap = NULL;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void MQTTregisterSubscribeFunction(std::string topic, std::function<bool(std::string, char*, int)> func){
|
|
|
ESP_LOGD(TAG, "registerSubscribeFunction %s", topic.c_str());
|
|
|
+ std::lock_guard<std::mutex> lock(subscribeFunctionMutex);
|
|
|
if (subscribeFunktionMap == NULL) {
|
|
|
- subscribeFunktionMap = new std::map<std::string, std::function<bool(std::string, char*, int)>>();
|
|
|
+ subscribeFunktionMap = new (std::nothrow) std::map<std::string, std::function<bool(std::string, char*, int)>>();
|
|
|
+ if (subscribeFunktionMap == NULL) {
|
|
|
+ ESP_LOGE(TAG, "Unable to allocate MQTT subscription callback map");
|
|
|
+ return;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- if ((*subscribeFunktionMap)[topic] != NULL) {
|
|
|
+ if (subscribeFunktionMap->find(topic) != subscribeFunktionMap->end()) {
|
|
|
ESP_LOGW(TAG, "topic %s already registered for subscription", topic.c_str());
|
|
|
return;
|
|
|
}
|
|
|
+ subscribeFunktionMap->emplace(topic, std::move(func));
|
|
|
+}
|
|
|
+
|
|
|
+void MQTTunregisterSubscribeFunction(std::string topic) {
|
|
|
+ std::lock_guard<std::mutex> lock(subscribeFunctionMutex);
|
|
|
+ if (subscribeFunktionMap == NULL) return;
|
|
|
+ const auto handler = subscribeFunktionMap->find(topic);
|
|
|
+ if (handler == subscribeFunktionMap->end()) return;
|
|
|
|
|
|
- (*subscribeFunktionMap)[topic] = func;
|
|
|
+ if (mqtt_connected && client != NULL) {
|
|
|
+ const int msg_id = esp_mqtt_client_unsubscribe(client, topic.c_str());
|
|
|
+ ESP_LOGD(TAG, "topic %s unsubscribe requested, msg_id=%d", topic.c_str(), msg_id);
|
|
|
+ }
|
|
|
+ subscribeFunktionMap->erase(handler);
|
|
|
+ if (subscribeFunktionMap->empty()) {
|
|
|
+ delete subscribeFunktionMap;
|
|
|
+ subscribeFunktionMap = NULL;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void MQTTdestroySubscribeFunction(){
|
|
|
+ std::lock_guard<std::mutex> lock(subscribeFunctionMutex);
|
|
|
if (subscribeFunktionMap != NULL) {
|
|
|
- if (mqtt_connected) {
|
|
|
+ if (mqtt_connected && client != NULL) {
|
|
|
for(std::map<std::string, std::function<bool(std::string, char*, int)>>::iterator it = subscribeFunktionMap->begin(); it != subscribeFunktionMap->end(); ++it) {
|
|
|
int msg_id = esp_mqtt_client_unsubscribe(client, it->first.c_str());
|
|
|
ESP_LOGD(TAG, "topic %s unsubscribe successful, msg_id=%d", it->first.c_str(), msg_id);
|