mirror of
https://github.com/jomjol/AI-on-the-edge-device.git
synced 2025-12-06 11:36:51 +03:00
Update interface_mqtt.cpp
This commit is contained in:
@@ -16,7 +16,6 @@
|
|||||||
#include "esp_timer.h"
|
#include "esp_timer.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static const char *TAG = "MQTT IF";
|
static const char *TAG = "MQTT IF";
|
||||||
|
|
||||||
std::map<std::string, std::function<void()>>* connectFunktionMap = NULL;
|
std::map<std::string, std::function<void()>>* connectFunktionMap = NULL;
|
||||||
@@ -41,7 +40,6 @@ int keepalive;
|
|||||||
bool SetRetainFlag;
|
bool SetRetainFlag;
|
||||||
void (*callbackOnConnected)(std::string, bool) = NULL;
|
void (*callbackOnConnected)(std::string, bool) = NULL;
|
||||||
|
|
||||||
|
|
||||||
bool MQTTPublish(std::string _key, std::string _content, int qos, bool retained_flag)
|
bool MQTTPublish(std::string _key, std::string _content, int qos, bool retained_flag)
|
||||||
{
|
{
|
||||||
if (!mqtt_enabled) { // MQTT sevice not started / configured (MQTT_Init not called before)
|
if (!mqtt_enabled) { // MQTT sevice not started / configured (MQTT_Init not called before)
|
||||||
@@ -96,7 +94,6 @@ bool MQTTPublish(std::string _key, std::string _content, int qos, bool retained_
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) {
|
static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) {
|
||||||
std::string topic = "";
|
std::string topic = "";
|
||||||
switch (event->event_id) {
|
switch (event->event_id) {
|
||||||
@@ -198,13 +195,11 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) {
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
|
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
|
||||||
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, (int)event_id);
|
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, (int)event_id);
|
||||||
mqtt_event_handler_cb((esp_mqtt_event_handle_t) event_data);
|
mqtt_event_handler_cb((esp_mqtt_event_handle_t) event_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password,
|
bool MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password,
|
||||||
std::string _maintopic, std::string _domoticz_in_topic, std::string _lwt, std::string _lwt_connected, std::string _lwt_disconnected,
|
std::string _maintopic, std::string _domoticz_in_topic, std::string _lwt, std::string _lwt_connected, std::string _lwt_disconnected,
|
||||||
std::string _cacertfilename, bool _validateServerCert, std::string _clientcertfilename, std::string _clientkeyfilename,
|
std::string _cacertfilename, bool _validateServerCert, std::string _clientcertfilename, std::string _clientkeyfilename,
|
||||||
@@ -264,7 +259,6 @@ bool MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _us
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int MQTT_Init() {
|
int MQTT_Init() {
|
||||||
if (mqtt_initialized) {
|
if (mqtt_initialized) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -300,19 +294,38 @@ int MQTT_Init() {
|
|||||||
mqtt_cfg.session.keepalive = keepalive;
|
mqtt_cfg.session.keepalive = keepalive;
|
||||||
mqtt_cfg.buffer.size = 2048; // size of MQTT send/receive buffer
|
mqtt_cfg.buffer.size = 2048; // size of MQTT send/receive buffer
|
||||||
|
|
||||||
|
#if MQTT_ENABLE_SSL
|
||||||
if (caCert.length()){
|
if (caCert.length()){
|
||||||
mqtt_cfg.broker.verification.certificate = caCert.c_str();
|
mqtt_cfg.broker.verification.certificate = caCert.c_str();
|
||||||
mqtt_cfg.broker.verification.certificate_len = caCert.length() + 1;
|
// darf nur bei DER_CERTIFICATES gesetzt werden, wenn PEM_CERTIFICATES muß = 0 sein ??????????????????????????
|
||||||
|
// siehe: .platformio\packages\framework-espidf\components\mqtt\esp-mqtt\mqtt_client.c
|
||||||
|
// static esp_err_t esp_mqtt_set_cert_key_data()
|
||||||
|
// mqtt_cfg.broker.verification.certificate_len = caCert.length() + 1;
|
||||||
|
mqtt_cfg.broker.verification.certificate_len = 0;
|
||||||
|
|
||||||
|
#if defined(MQTT_SUPPORTED_FEATURE_SKIP_CRT_CMN_NAME_CHECK)
|
||||||
|
// Skip any validation of server certificate CN field, this reduces the
|
||||||
|
// security of TLS and makes the *MQTT* client susceptible to MITM attacks
|
||||||
mqtt_cfg.broker.verification.skip_cert_common_name_check = !validateServerCert;
|
mqtt_cfg.broker.verification.skip_cert_common_name_check = !validateServerCert;
|
||||||
|
#endif // end MQTT_SUPPORTED_FEATURE_SKIP_CRT_CMN_NAME_CHECK
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clientCert.length() && clientKey.length()){
|
if (clientCert.length() && clientKey.length()){
|
||||||
mqtt_cfg.credentials.authentication.certificate = clientCert.c_str();
|
mqtt_cfg.credentials.authentication.certificate = clientCert.c_str();
|
||||||
mqtt_cfg.credentials.authentication.certificate_len = clientCert.length() + 1;
|
// darf nur bei DER_CERTIFICATES gesetzt werden, wenn PEM_CERTIFICATES muß = 0 sein ??????????????????????????
|
||||||
|
// siehe: .platformio\packages\framework-espidf\components\mqtt\esp-mqtt\mqtt_client.c
|
||||||
|
// static esp_err_t esp_mqtt_set_cert_key_data()
|
||||||
|
// mqtt_cfg.credentials.authentication.certificate_len = clientCert.length() + 1;
|
||||||
|
mqtt_cfg.credentials.authentication.certificate_len = 0;
|
||||||
|
|
||||||
mqtt_cfg.credentials.authentication.key = clientKey.c_str();
|
mqtt_cfg.credentials.authentication.key = clientKey.c_str();
|
||||||
mqtt_cfg.credentials.authentication.key_len = clientKey.length() + 1;
|
// darf nur bei DER_CERTIFICATES gesetzt werden, wenn PEM_CERTIFICATES muß = 0 sein ??????????????????????????
|
||||||
|
// siehe: .platformio\packages\framework-espidf\components\mqtt\esp-mqtt\mqtt_client.c
|
||||||
|
// static esp_err_t esp_mqtt_set_cert_key_data()
|
||||||
|
// mqtt_cfg.credentials.authentication.key_len = clientKey.length() + 1;
|
||||||
|
mqtt_cfg.credentials.authentication.key_len = 0;
|
||||||
}
|
}
|
||||||
|
#endif // end MQTT_ENABLE_SSL
|
||||||
|
|
||||||
if (user.length() && password.length()){
|
if (user.length() && password.length()){
|
||||||
mqtt_cfg.credentials.username = user.c_str();
|
mqtt_cfg.credentials.username = user.c_str();
|
||||||
@@ -359,7 +372,6 @@ int MQTT_Init() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MQTTdestroy_client(bool _disable = false) {
|
void MQTTdestroy_client(bool _disable = false) {
|
||||||
if (client) {
|
if (client) {
|
||||||
if (mqtt_connected) {
|
if (mqtt_connected) {
|
||||||
@@ -377,17 +389,14 @@ void MQTTdestroy_client(bool _disable = false) {
|
|||||||
mqtt_configOK = false;
|
mqtt_configOK = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool getMQTTisEnabled() {
|
bool getMQTTisEnabled() {
|
||||||
return mqtt_enabled;
|
return mqtt_enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool getMQTTisConnected() {
|
bool getMQTTisConnected() {
|
||||||
return mqtt_connected;
|
return mqtt_connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool mqtt_handler_flow_start(std::string _topic, char* _data, int _data_len)
|
bool mqtt_handler_flow_start(std::string _topic, char* _data, int _data_len)
|
||||||
{
|
{
|
||||||
ESP_LOGD(TAG, "Handler called: topic %s, data %.*s", _topic.c_str(), _data_len, _data);
|
ESP_LOGD(TAG, "Handler called: topic %s, data %.*s", _topic.c_str(), _data_len, _data);
|
||||||
@@ -396,7 +405,6 @@ bool mqtt_handler_flow_start(std::string _topic, char* _data, int _data_len)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool mqtt_handler_set_prevalue(std::string _topic, char* _data, int _data_len)
|
bool mqtt_handler_set_prevalue(std::string _topic, char* _data, int _data_len)
|
||||||
{
|
{
|
||||||
//ESP_LOGD(TAG, "Handler called: topic %s, data %.*s", _topic.c_str(), _data_len, _data);
|
//ESP_LOGD(TAG, "Handler called: topic %s, data %.*s", _topic.c_str(), _data_len, _data);
|
||||||
@@ -432,7 +440,6 @@ bool mqtt_handler_set_prevalue(std::string _topic, char* _data, int _data_len)
|
|||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MQTTconnected(){
|
void MQTTconnected(){
|
||||||
if (mqtt_connected) {
|
if (mqtt_connected) {
|
||||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Connected to broker");
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Connected to broker");
|
||||||
@@ -467,7 +474,6 @@ void MQTTconnected(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MQTTregisterConnectFunction(std::string name, std::function<void()> func){
|
void MQTTregisterConnectFunction(std::string name, std::function<void()> func){
|
||||||
ESP_LOGD(TAG, "MQTTregisteronnectFunction %s\r\n", name.c_str());
|
ESP_LOGD(TAG, "MQTTregisteronnectFunction %s\r\n", name.c_str());
|
||||||
if (connectFunktionMap == NULL) {
|
if (connectFunktionMap == NULL) {
|
||||||
@@ -486,7 +492,6 @@ void MQTTregisterConnectFunction(std::string name, std::function<void()> func){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MQTTunregisterConnectFunction(std::string name){
|
void MQTTunregisterConnectFunction(std::string name){
|
||||||
ESP_LOGD(TAG, "unregisterConnnectFunction %s\r\n", name.c_str());
|
ESP_LOGD(TAG, "unregisterConnnectFunction %s\r\n", name.c_str());
|
||||||
if ((connectFunktionMap != NULL) && (connectFunktionMap->find(name) != connectFunktionMap->end())) {
|
if ((connectFunktionMap != NULL) && (connectFunktionMap->find(name) != connectFunktionMap->end())) {
|
||||||
@@ -494,7 +499,6 @@ void MQTTunregisterConnectFunction(std::string name){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MQTTregisterSubscribeFunction(std::string topic, std::function<bool(std::string, char*, int)> func){
|
void MQTTregisterSubscribeFunction(std::string topic, std::function<bool(std::string, char*, int)> func){
|
||||||
ESP_LOGD(TAG, "registerSubscribeFunction %s", topic.c_str());
|
ESP_LOGD(TAG, "registerSubscribeFunction %s", topic.c_str());
|
||||||
if (subscribeFunktionMap == NULL) {
|
if (subscribeFunktionMap == NULL) {
|
||||||
@@ -509,7 +513,6 @@ void MQTTregisterSubscribeFunction(std::string topic, std::function<bool(std::st
|
|||||||
(*subscribeFunktionMap)[topic] = func;
|
(*subscribeFunktionMap)[topic] = func;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MQTTdestroySubscribeFunction(){
|
void MQTTdestroySubscribeFunction(){
|
||||||
if (subscribeFunktionMap != NULL) {
|
if (subscribeFunktionMap != NULL) {
|
||||||
if (mqtt_connected) {
|
if (mqtt_connected) {
|
||||||
|
|||||||
Reference in New Issue
Block a user