Skip to content

Commit

Permalink
Merge pull request #193 from c-jimenez/fix/config_case_insensitive
Browse files Browse the repository at this point in the history
[chargepoint] Handle configurations keys name as case insensitive
  • Loading branch information
c-jimenez authored Mar 10, 2024
2 parents db379c0 + 5027159 commit 033c4cf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/chargepoint/ChargePoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ void ChargePoint::rcpMessageSent(const std::string& msg)
void ChargePoint::configurationValueChanged(const std::string& key)
{
// Check configuration key
if (key == "AuthorizationKey")
if (key == "authorizationkey")
{
// Reconnect with new authorization key
if (m_ocpp_config.securityProfile() != 3)
Expand All @@ -1001,7 +1001,7 @@ void ChargePoint::configurationValueChanged(const std::string& key)

m_security_manager.logSecurityEvent(SECEVT_RECONFIG_SECURITY_PARAMETER, "AuthorizationKey");
}
else if (key == "SecurityProfile")
else if (key == "securityprofile")
{
// Reconnect with new profile
LOG_INFO << "SecurityProfile modified, reconnect with new security profile";
Expand Down
15 changes: 9 additions & 6 deletions src/chargepoint/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ ConfigManager::~ConfigManager() { }
/** @copydoc void registerCheckFunction(const std::string&, ConfigurationValueCheckFunc) */
void ConfigManager::registerCheckFunction(const std::string& key, ConfigurationValueCheckFunc func)
{
m_specific_checks[key] = func;
auto lower_case_key = ocpp::helpers::tolower(key);
m_specific_checks[lower_case_key] = func;
}

/** @copydoc void IConfigManager::registerConfigChangedListener(const std::string&, IConfigChangedListener&) */
void ConfigManager::registerConfigChangedListener(const std::string& key, IConfigChangedListener& listener)
{
m_listeners[key] = &listener;
auto lower_case_key = ocpp::helpers::tolower(key);
m_listeners[lower_case_key] = &listener;
}

/** @copydoc bool GenericMessageHandler<RequestType, ResponseType>::handleMessage(const RequestType& request,
Expand Down Expand Up @@ -93,10 +95,11 @@ bool ConfigManager::handleMessage(const ocpp::messages::ChangeConfigurationReq&
response.status = ConfigurationStatus::Accepted;

// Specific check
auto it = m_specific_checks.find(request.key);
auto lower_case_key = ocpp::helpers::tolower(request.key);
auto it = m_specific_checks.find(lower_case_key);
if (it != m_specific_checks.end())
{
response.status = it->second(request.key, request.value);
response.status = it->second(lower_case_key, request.value);
}
if (response.status == ConfigurationStatus::Accepted)
{
Expand All @@ -105,10 +108,10 @@ bool ConfigManager::handleMessage(const ocpp::messages::ChangeConfigurationReq&
if (response.status == ConfigurationStatus::Accepted)
{
// Notify change
auto iter = m_listeners.find(request.key);
auto iter = m_listeners.find(lower_case_key);
if (iter != m_listeners.end())
{
iter->second->configurationValueChanged(request.key);
iter->second->configurationValueChanged(lower_case_key);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/tools/helpers/StringHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ along with OpenOCPP. If not, see <http://www.gnu.org/licenses/>.

#include "StringHelpers.h"

#include <algorithm>
#include <iomanip>
#include <sstream>

Expand All @@ -29,6 +30,14 @@ namespace helpers
/** @brief Space */
const std::string SPACE_STRING = " ";

/** @brief Helper function to convert a string to lowercase */
std::string tolower(const std::string& str)
{
std::string ret = str;
std::transform(ret.begin(), ret.end(), ret.begin(), [](unsigned char c) { return std::tolower(c); });
return ret;
}

/** @brief Helper function to trim a string */
std::string& trim(std::string& str, const std::string& chars)
{
Expand Down
8 changes: 8 additions & 0 deletions src/tools/helpers/StringHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ namespace helpers
/** @brief Space */
extern const std::string SPACE_STRING;

/**
* @brief Helper function to convert a string to lowercase
* /!\ Works only on ASCII strings /!\
* @param str String to convert
* @return Reference to the converted string
*/
std::string tolower(const std::string& str);

/**
* @brief Helper function to trim a string
* @param str String to trim
Expand Down

0 comments on commit 033c4cf

Please sign in to comment.