|
|
@@ -16,7 +16,6 @@
|
|
|
#include "esp_timer.h"
|
|
|
#endif
|
|
|
|
|
|
-
|
|
|
static const char *TAG = "MQTT IF";
|
|
|
|
|
|
std::map<std::string, std::function<void()>>* connectFunktionMap = NULL;
|
|
|
@@ -36,11 +35,11 @@ bool mqtt_connected = false;
|
|
|
esp_mqtt_client_handle_t client = NULL;
|
|
|
std::string uri, client_id, lwt_topic, lwt_connected, lwt_disconnected, user, password, maintopic, domoticz_in_topic;
|
|
|
std::string caCert, clientCert, clientKey;
|
|
|
+bool validateServerCert = true;
|
|
|
int keepalive;
|
|
|
bool SetRetainFlag;
|
|
|
void (*callbackOnConnected)(std::string, bool) = NULL;
|
|
|
|
|
|
-
|
|
|
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)
|
|
|
@@ -95,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) {
|
|
|
std::string topic = "";
|
|
|
switch (event->event_id) {
|
|
|
@@ -197,16 +195,14 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) {
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
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);
|
|
|
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,
|
|
|
std::string _maintopic, std::string _domoticz_in_topic, std::string _lwt, std::string _lwt_connected, std::string _lwt_disconnected,
|
|
|
- std::string _cacertfilename, std::string _clientcertfilename, std::string _clientkeyfilename,
|
|
|
+ std::string _cacertfilename, bool _validateServerCert, std::string _clientcertfilename, std::string _clientkeyfilename,
|
|
|
int _keepalive, bool _SetRetainFlag, void *_callbackOnConnected) {
|
|
|
if ((_mqttURI.length() == 0) || (_maintopic.length() == 0) || (_clientid.length() == 0))
|
|
|
{
|
|
|
@@ -225,25 +221,45 @@ bool MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _us
|
|
|
domoticz_in_topic = _domoticz_in_topic;
|
|
|
callbackOnConnected = ( void (*)(std::string, bool) )(_callbackOnConnected);
|
|
|
|
|
|
- if (_clientcertfilename.length() && _clientkeyfilename.length()){
|
|
|
+ if (_clientcertfilename.length() && _clientkeyfilename.length()) {
|
|
|
std::ifstream cert_ifs(_clientcertfilename);
|
|
|
- std::string cert_content((std::istreambuf_iterator<char>(cert_ifs)), (std::istreambuf_iterator<char>()));
|
|
|
- clientCert = cert_content;
|
|
|
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "using clientCert: " + _clientcertfilename);
|
|
|
+ if (cert_ifs.is_open()) {
|
|
|
+ std::string cert_content((std::istreambuf_iterator<char>(cert_ifs)), (std::istreambuf_iterator<char>()));
|
|
|
+ clientCert = cert_content;
|
|
|
+ cert_ifs.close();
|
|
|
+ LogFile.WriteToFile(ESP_LOG_INFO, TAG, "using clientCert: " + _clientcertfilename);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ LogFile.WriteToFile(ESP_LOG_INFO, TAG, "could not open clientCert: " + _clientcertfilename);
|
|
|
+ }
|
|
|
|
|
|
std::ifstream key_ifs(_clientkeyfilename);
|
|
|
- std::string key_content((std::istreambuf_iterator<char>(key_ifs)), (std::istreambuf_iterator<char>()));
|
|
|
- clientKey = key_content;
|
|
|
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "using clientKey: " + _clientkeyfilename);
|
|
|
+ if (key_ifs.is_open()) {
|
|
|
+ std::string key_content((std::istreambuf_iterator<char>(key_ifs)), (std::istreambuf_iterator<char>()));
|
|
|
+ clientKey = key_content;
|
|
|
+ key_ifs.close();
|
|
|
+ LogFile.WriteToFile(ESP_LOG_INFO, TAG, "using clientKey: " + _clientkeyfilename);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ LogFile.WriteToFile(ESP_LOG_INFO, TAG, "could not open clientKey: " + _clientkeyfilename);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- if (_cacertfilename.length() ){
|
|
|
- std::ifstream ifs(_cacertfilename);
|
|
|
- std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
|
|
|
- caCert = content;
|
|
|
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "using caCert: " + _cacertfilename);
|
|
|
+ if (_cacertfilename.length()) {
|
|
|
+ std::ifstream ca_ifs(_cacertfilename);
|
|
|
+ if (ca_ifs.is_open()) {
|
|
|
+ std::string content((std::istreambuf_iterator<char>(ca_ifs)), (std::istreambuf_iterator<char>()));
|
|
|
+ caCert = content;
|
|
|
+ ca_ifs.close();
|
|
|
+ LogFile.WriteToFile(ESP_LOG_INFO, TAG, "using caCert: " + _cacertfilename);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ LogFile.WriteToFile(ESP_LOG_INFO, TAG, "could not open caCert: " + _cacertfilename);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ validateServerCert = _validateServerCert;
|
|
|
+
|
|
|
if (_user.length() && _password.length()){
|
|
|
user = _user;
|
|
|
password = _password;
|
|
|
@@ -261,7 +277,6 @@ bool MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _us
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
int MQTT_Init() {
|
|
|
if (mqtt_initialized) {
|
|
|
return 0;
|
|
|
@@ -295,18 +310,21 @@ int MQTT_Init() {
|
|
|
mqtt_cfg.session.last_will.msg = lwt_disconnected.c_str();
|
|
|
mqtt_cfg.session.last_will.msg_len = (int)(lwt_disconnected.length());
|
|
|
mqtt_cfg.session.keepalive = keepalive;
|
|
|
- mqtt_cfg.buffer.size = 1536; // size of MQTT send/receive buffer (Default: 1024)
|
|
|
+ mqtt_cfg.buffer.size = 2048; // size of MQTT send/receive buffer
|
|
|
|
|
|
- if (caCert.length()){
|
|
|
+ if (caCert.length()) {
|
|
|
mqtt_cfg.broker.verification.certificate = caCert.c_str();
|
|
|
mqtt_cfg.broker.verification.certificate_len = caCert.length() + 1;
|
|
|
- mqtt_cfg.broker.verification.skip_cert_common_name_check = true;
|
|
|
+
|
|
|
+ // 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;
|
|
|
}
|
|
|
|
|
|
- if (clientCert.length() && clientKey.length()){
|
|
|
+ if (clientCert.length() && clientKey.length()) {
|
|
|
mqtt_cfg.credentials.authentication.certificate = clientCert.c_str();
|
|
|
mqtt_cfg.credentials.authentication.certificate_len = clientCert.length() + 1;
|
|
|
-
|
|
|
+
|
|
|
mqtt_cfg.credentials.authentication.key = clientKey.c_str();
|
|
|
mqtt_cfg.credentials.authentication.key_len = clientKey.length() + 1;
|
|
|
}
|
|
|
@@ -356,7 +374,6 @@ int MQTT_Init() {
|
|
|
|
|
|
}
|
|
|
|
|
|
-
|
|
|
void MQTTdestroy_client(bool _disable = false) {
|
|
|
if (client) {
|
|
|
if (mqtt_connected) {
|
|
|
@@ -374,17 +391,14 @@ void MQTTdestroy_client(bool _disable = false) {
|
|
|
mqtt_configOK = false;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
bool getMQTTisEnabled() {
|
|
|
return mqtt_enabled;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
bool getMQTTisConnected() {
|
|
|
return mqtt_connected;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
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);
|
|
|
@@ -393,7 +407,6 @@ bool mqtt_handler_flow_start(std::string _topic, char* _data, int _data_len)
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
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);
|
|
|
@@ -429,7 +442,6 @@ bool mqtt_handler_set_prevalue(std::string _topic, char* _data, int _data_len)
|
|
|
return ESP_FAIL;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
void MQTTconnected(){
|
|
|
if (mqtt_connected) {
|
|
|
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Connected to broker");
|
|
|
@@ -464,7 +476,6 @@ void MQTTconnected(){
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
void MQTTregisterConnectFunction(std::string name, std::function<void()> func){
|
|
|
ESP_LOGD(TAG, "MQTTregisteronnectFunction %s\r\n", name.c_str());
|
|
|
if (connectFunktionMap == NULL) {
|
|
|
@@ -483,7 +494,6 @@ void MQTTregisterConnectFunction(std::string name, std::function<void()> func){
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
void MQTTunregisterConnectFunction(std::string name){
|
|
|
ESP_LOGD(TAG, "unregisterConnnectFunction %s\r\n", name.c_str());
|
|
|
if ((connectFunktionMap != NULL) && (connectFunktionMap->find(name) != connectFunktionMap->end())) {
|
|
|
@@ -491,7 +501,6 @@ void MQTTunregisterConnectFunction(std::string name){
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
void MQTTregisterSubscribeFunction(std::string topic, std::function<bool(std::string, char*, int)> func){
|
|
|
ESP_LOGD(TAG, "registerSubscribeFunction %s", topic.c_str());
|
|
|
if (subscribeFunktionMap == NULL) {
|
|
|
@@ -506,7 +515,6 @@ void MQTTregisterSubscribeFunction(std::string topic, std::function<bool(std::st
|
|
|
(*subscribeFunktionMap)[topic] = func;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
void MQTTdestroySubscribeFunction(){
|
|
|
if (subscribeFunktionMap != NULL) {
|
|
|
if (mqtt_connected) {
|