From 0e124f8e51ee347c52bb073a2acf130d5ff0c235 Mon Sep 17 00:00:00 2001 From: iranl Date: Sun, 28 Apr 2024 20:01:00 +0200 Subject: [PATCH 1/2] Fix authName --- NetworkLock.cpp | 27 +++++++++++++-------------- NetworkLock.h | 7 ++++--- NetworkOpener.cpp | 25 ++++++++++++------------- NetworkOpener.h | 7 ++++--- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/NetworkLock.cpp b/NetworkLock.cpp index a3e2439..6c57ec5 100644 --- a/NetworkLock.cpp +++ b/NetworkLock.cpp @@ -21,8 +21,6 @@ NetworkLock::NetworkLock(Network* network, Preferences* preferences, char* buffe _configTopics.push_back(mqtt_topic_config_auto_lock); _configTopics.push_back(mqtt_topic_config_single_lock); - memset(authName, 0, sizeof(authName)); - _network->registerMqttReceiver(this); } @@ -325,9 +323,9 @@ void NetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurne } } - json["auth_id"] = authId; - json["auth_name"] = authName; - + json["auth_id"] = _authId; + json["auth_name"] = _authName; + serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_lock_json, _buffer); @@ -372,8 +370,9 @@ void NetworkLock::publishAuthorizationInfo(const std::list& { char str[50]; - bool authFound = false; - memset(authName, 0, sizeof(authName)); + _authId = 0; + _authName = ""; + _authFound = false; JsonDocument json; @@ -385,11 +384,11 @@ void NetworkLock::publishAuthorizationInfo(const std::list& break; } --i; - if((log.loggingType == NukiLock::LoggingType::LockAction || log.loggingType == NukiLock::LoggingType::KeypadAction) && ! authFound) + if((log.loggingType == NukiLock::LoggingType::LockAction || log.loggingType == NukiLock::LoggingType::KeypadAction) && ! _authFound) { - authFound = true; - authId = log.authId; - memcpy(authName, log.name, sizeof(log.name)); + _authFound = true; + _authId = log.authId; + _authName = (char*)log.name; } auto entry = json.add(); @@ -462,10 +461,10 @@ void NetworkLock::publishAuthorizationInfo(const std::list& serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_lock_log, _buffer); - if(authFound) + if(_authFound) { - publishUInt(mqtt_topic_lock_auth_id, authId); - publishString(mqtt_topic_lock_auth_name, authName); + publishUInt(mqtt_topic_lock_auth_id, _authId); + publishString(mqtt_topic_lock_auth_name, _authName); } } diff --git a/NetworkLock.h b/NetworkLock.h index f259f38..a9bf3f2 100644 --- a/NetworkLock.h +++ b/NetworkLock.h @@ -84,9 +84,10 @@ private: String _keypadCommandCode = ""; uint _keypadCommandId = 0; int _keypadCommandEnabled = 1; - uint8_t _queryCommands = 0; - uint32_t authId = 0; - char authName[33]; + uint8_t _queryCommands = 0; + uint32_t _authId = 0; + String _authName = ""; + bool _authFound = false; char* _buffer; size_t _bufferSize; diff --git a/NetworkOpener.cpp b/NetworkOpener.cpp index a0d1d0a..c55cc04 100644 --- a/NetworkOpener.cpp +++ b/NetworkOpener.cpp @@ -17,8 +17,6 @@ NetworkOpener::NetworkOpener(Network* network, Preferences* preferences, char* b _configTopics.push_back(mqtt_topic_config_led_enabled); _configTopics.push_back(mqtt_topic_config_sound_level); - memset(authName, 0, sizeof(authName)); - _network->registerMqttReceiver(this); } @@ -297,8 +295,8 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn publishBool(mqtt_topic_battery_critical, critical); } - json["auth_id"] = authId; - json["auth_name"] = authName; + json["auth_id"] = _authId; + json["auth_name"] = _authName; serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_lock_json, _buffer); @@ -359,8 +357,9 @@ void NetworkOpener::publishAuthorizationInfo(const std::list Date: Thu, 2 May 2024 19:48:34 +0200 Subject: [PATCH 2/2] Fix authName --- NetworkLock.cpp | 16 +++++++++++----- NetworkLock.h | 2 +- NetworkOpener.cpp | 12 +++++++++--- NetworkOpener.h | 2 +- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/NetworkLock.cpp b/NetworkLock.cpp index 6c57ec5..a3a5d4e 100644 --- a/NetworkLock.cpp +++ b/NetworkLock.cpp @@ -20,6 +20,9 @@ NetworkLock::NetworkLock(Network* network, Preferences* preferences, char* buffe _configTopics.push_back(mqtt_topic_config_auto_unlock); _configTopics.push_back(mqtt_topic_config_auto_lock); _configTopics.push_back(mqtt_topic_config_single_lock); + + memset(_authName, 0, sizeof(_authName)); + _authName[0] = '\0'; _network->registerMqttReceiver(this); } @@ -224,7 +227,7 @@ void NetworkLock::onMqttDataReceived(const char* topic, byte* payload, const uns publishString(mqtt_topic_keypad_json_action, "--"); } - + if(comparePrefixedPath(topic, mqtt_topic_timecontrol_action)) { if(strcmp(value, "") == 0 || strcmp(value, "--") == 0) return; @@ -325,7 +328,7 @@ void NetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurne json["auth_id"] = _authId; json["auth_name"] = _authName; - + serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_lock_json, _buffer); @@ -371,7 +374,8 @@ void NetworkLock::publishAuthorizationInfo(const std::list& char str[50]; _authId = 0; - _authName = ""; + memset(_authName, 0, sizeof(_authName)); + _authName[0] = '\0'; _authFound = false; JsonDocument json; @@ -388,14 +392,16 @@ void NetworkLock::publishAuthorizationInfo(const std::list& { _authFound = true; _authId = log.authId; - _authName = (char*)log.name; + int sizeName = sizeof(log.name); + memcpy(_authName, log.name, sizeName); + if(_authName[sizeName - 1] != '\0') _authName[sizeName] = '\0'; } auto entry = json.add(); entry["index"] = log.index; entry["authorizationId"] = log.authId; - entry["authorizationName"] = log.name; + entry["authorizationName"] = _authName; entry["timeYear"] = log.timeStampYear; entry["timeMonth"] = log.timeStampMonth; entry["timeDay"] = log.timeStampDay; diff --git a/NetworkLock.h b/NetworkLock.h index a9bf3f2..f9b1b9a 100644 --- a/NetworkLock.h +++ b/NetworkLock.h @@ -86,7 +86,7 @@ private: int _keypadCommandEnabled = 1; uint8_t _queryCommands = 0; uint32_t _authId = 0; - String _authName = ""; + char _authName[33]; bool _authFound = false; char* _buffer; diff --git a/NetworkOpener.cpp b/NetworkOpener.cpp index c55cc04..600f36f 100644 --- a/NetworkOpener.cpp +++ b/NetworkOpener.cpp @@ -16,6 +16,9 @@ NetworkOpener::NetworkOpener(Network* network, Preferences* preferences, char* b _configTopics.push_back(mqtt_topic_config_button_enabled); _configTopics.push_back(mqtt_topic_config_led_enabled); _configTopics.push_back(mqtt_topic_config_sound_level); + + memset(_authName, 0, sizeof(_authName)); + _authName[0] = '\0'; _network->registerMqttReceiver(this); } @@ -358,7 +361,8 @@ void NetworkOpener::publishAuthorizationInfo(const std::list