Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists_Options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ if(NOT DEFINED LOG_LEVEL)
endif()
add_compile_definitions(LOG_LEVEL=${LOG_LEVEL})

# Logger
option(EXTERNAL_LOGGER "Use an external logger" OFF)
if(EXTERNAL_LOGGER)
add_compile_definitions(EXTERNAL_LOGGER=1)
endif()

# Static library
option(BUILD_STATIC_LIBRARY "Build Open OCPP as a static library" ON)

Expand Down
7 changes: 7 additions & 0 deletions examples/common/DefaultCentralSystemEventsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ DefaultCentralSystemEventsHandler::~DefaultCentralSystemEventsHandler() { }

// ICentralSystemEventsHandler interface

/** @copydoc bool ICentralSystemEventsHandler::acceptConnection(const char*) */
bool DefaultCentralSystemEventsHandler::acceptConnection(const char* ip_address)
{
cout << "Accept connection from [" << ip_address << "]" << endl;
return true;
}

/** @copydoc bool ICentralSystemEventsHandler::checkCredentials(const std::string&, const std::string&) */
bool DefaultCentralSystemEventsHandler::checkCredentials(const std::string& chargepoint_id, const std::string& password)
{
Expand Down
3 changes: 3 additions & 0 deletions examples/common/DefaultCentralSystemEventsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class DefaultCentralSystemEventsHandler : public ocpp::centralsystem::ICentralSy

// ICentralSystemEventsHandler interface

/** @copydoc bool ICentralSystemEventsHandler::acceptConnection(const char*) */
bool acceptConnection(const char* ip_address) override;

/** @copydoc bool ICentralSystemEventsHandler::checkCredentials(const std::string&, const std::string&) */
bool checkCredentials(const std::string& chargepoint_id, const std::string& password) override;

Expand Down
7 changes: 7 additions & 0 deletions examples/common/DefaultLocalControllerEventsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ DefaultLocalControllerEventsHandler::~DefaultLocalControllerEventsHandler() { }

// ILocalControllerEventsHandler interface

/** @copydoc bool ILocalControllerEventsHandler::acceptConnection(const char*) */
bool DefaultLocalControllerEventsHandler::acceptConnection(const char* ip_address)
{
cout << "Accept connection from [" << ip_address << "]" << endl;
return true;
}

/** @copydoc bool ILocalControllerEventsHandler::checkCredentials(const std::string&, const std::string&) */
bool DefaultLocalControllerEventsHandler::checkCredentials(const std::string& chargepoint_id, const std::string& password)
{
Expand Down
3 changes: 3 additions & 0 deletions examples/common/DefaultLocalControllerEventsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class DefaultLocalControllerEventsHandler : public ocpp::localcontroller::ILocal

// ILocalControllerEventsHandler interface

/** @copydoc bool ILocalControllerEventsHandler::acceptConnection(const char*) */
bool acceptConnection(const char* ip_address) override;

/** @copydoc bool ILocalControllerEventsHandler::checkCredentials(const std::string&, const std::string&) */
bool checkCredentials(const std::string& chargepoint_id, const std::string& password) override;

Expand Down
8 changes: 8 additions & 0 deletions src/centralsystem/CentralSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ bool CentralSystem::stop()
return ret;
}

/** @copydoc bool RpcServer::IListener::rpcAcceptConnection(const char*) */
bool CentralSystem::rpcAcceptConnection(const char* ip_address)
{
// Notify connection => no additional processing is done here
// to keep this callback has fast as possible
return m_events_handler.acceptConnection(ip_address);
}

/** @copydoc bool RpcServer::IListener::rpcCheckCredentials(const std::string&, const std::string&, const std::string&) */
bool CentralSystem::rpcCheckCredentials(const std::string& chargepoint_id, const std::string& user, const std::string& password)
{
Expand Down
3 changes: 3 additions & 0 deletions src/centralsystem/CentralSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class CentralSystem : public ICentralSystem, public ocpp::rpc::RpcServer::IListe

// RpcServer::IListener interface

/** @copydoc bool RpcServer::IListener::rpcAcceptConnection(const char*) */
bool rpcAcceptConnection(const char* ip_address) override;

/** @copydoc bool RpcServer::IListener::rpcCheckCredentials(const std::string&, const std::string&, const std::string&) */
bool rpcCheckCredentials(const std::string& chargepoint_id, const std::string& user, const std::string& password) override;

Expand Down
7 changes: 7 additions & 0 deletions src/centralsystem/interface/ICentralSystemEventsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class ICentralSystemEventsHandler
/** @brief Destructor */
virtual ~ICentralSystemEventsHandler() { }

/**
* @brief Called to accept an incoming connection
* @param ip_address IP address of the client
* @return true if the incoming connection must be accepted, false otherwise
*/
virtual bool acceptConnection(const char* ip_address) = 0;

/**
* @brief Called to check the charge point credentials for HTTP basic authentication
* @param chargepoint_id Charge Point identifier
Expand Down
8 changes: 8 additions & 0 deletions src/localcontroller/LocalController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ bool LocalController::stop()
return ret;
}

/** @copydoc bool RpcServer::IListener::rpcAcceptConnection(const char*) */
bool LocalController::rpcAcceptConnection(const char* ip_address)
{
// Notify connection => no additional processing is done here
// to keep this callback has fast as possible
return m_events_handler.acceptConnection(ip_address);
}

/** @copydoc bool RpcServer::IListener::rpcCheckCredentials(const std::string&, const std::string&, const std::string&) */
bool LocalController::rpcCheckCredentials(const std::string& chargepoint_id, const std::string& user, const std::string& password)
{
Expand Down
3 changes: 3 additions & 0 deletions src/localcontroller/LocalController.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class LocalController : public ILocalController, public ocpp::rpc::RpcServer::IL

// RpcServer::IListener interface

/** @copydoc bool RpcServer::IListener::rpcAcceptConnection(const char*) */
bool rpcAcceptConnection(const char* ip_address) override;

/** @copydoc bool RpcServer::IListener::rpcCheckCredentials(const std::string&, const std::string&, const std::string&) */
bool rpcCheckCredentials(const std::string& chargepoint_id, const std::string& user, const std::string& password) override;

Expand Down
7 changes: 7 additions & 0 deletions src/localcontroller/interface/ILocalControllerEventsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class ILocalControllerEventsHandler
/** @brief Destructor */
virtual ~ILocalControllerEventsHandler() { }

/**
* @brief Called to accept an incoming connection
* @param ip_address IP address of the client
* @return true if the incoming connection must be accepted, false otherwise
*/
virtual bool acceptConnection(const char* ip_address) = 0;

/**
* @brief Called to check the charge point credentials for HTTP basic authentication
* @param chargepoint_id Charge Point identifier
Expand Down
13 changes: 8 additions & 5 deletions src/rpc/RpcBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ bool RpcBase::call(const std::string& action,
std::stringstream expected_id;
expected_id << m_transaction_id;
std::shared_ptr<RpcMessage> rpc_message;
auto wait_time = std::chrono::steady_clock().now() + timeout;
auto wait_time = std::chrono::steady_clock().now() + timeout;
do
{
// Compute timeout
Expand Down Expand Up @@ -179,8 +179,11 @@ void RpcBase::stop()
// Check if already started
if (m_rx_thread)
{
// Stop reception thread
// Stop queues
m_results_queue.setEnable(false);
m_requests_queue.setEnable(false);

// Stop reception thread
m_rx_thread->join();
delete m_rx_thread;
m_rx_thread = nullptr;
Expand Down Expand Up @@ -313,7 +316,7 @@ bool RpcBase::decodeCall(const std::string& unique_id,
{
// Add request to the queue
auto msg = std::make_shared<RpcMessage>(unique_id, action.GetString(), rpc_frame, payload);
m_requests_queue.push(msg);
m_requests_queue.push(std::move(msg));

ret = true;
}
Expand All @@ -331,7 +334,7 @@ bool RpcBase::decodeCallResult(const std::string& unique_id, rapidjson::Document
{
// Add result to the queue
auto msg = std::make_shared<RpcMessage>(unique_id, rpc_frame, payload);
m_results_queue.push(msg);
m_results_queue.push(std::move(msg));

ret = true;
}
Expand All @@ -353,7 +356,7 @@ bool RpcBase::decodeCallError(const std::string& unique_id,
{
// Add error to the queue
auto msg = std::make_shared<RpcMessage>(unique_id, rpc_frame, payload, &error, &message);
m_results_queue.push(msg);
m_results_queue.push(std::move(msg));

ret = true;
}
Expand Down
6 changes: 6 additions & 0 deletions src/rpc/RpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ void RpcServer::registerServerListener(IListener& listener)

// IWebsocketServer::IListener interface

/** @copydoc bool IWebsocketServer::IListener::wsAcceptConnection(const char*) */
bool RpcServer::wsAcceptConnection(const char* ip_address)
{
return m_listener->rpcAcceptConnection(ip_address);
}

/** @copydoc bool IWebsocketServer::IListener::wsCheckCredentials(const char*, const std::string&, const std::string&) */
bool RpcServer::wsCheckCredentials(const char* uri, const std::string& user, const std::string& password)
{
Expand Down
10 changes: 10 additions & 0 deletions src/rpc/RpcServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class RpcServer : public ocpp::websockets::IWebsocketServer::IListener

// IWebsocketServer::IListener interface

/** @copydoc bool IWebsocketServer::IListener::wsAcceptConnection(const char*) */
bool wsAcceptConnection(const char* ip_address) override;

/** @copydoc bool IWebsocketServer::IListener::wsCheckCredentials(const char*, const std::string&, const std::string&) */
bool wsCheckCredentials(const char* uri, const std::string& user, const std::string& password) override;

Expand All @@ -82,6 +85,13 @@ class RpcServer : public ocpp::websockets::IWebsocketServer::IListener
/** @brief Destructor */
virtual ~IListener() { }

/**
* @brief Called to accept an incoming connection
* @param ip_address IP address of the client
* @return true if the incoming connection must be accepted, false otherwise
*/
virtual bool rpcAcceptConnection(const char* ip_address) = 0;

/**
* @brief Called to check the user credentials for HTTP basic authentication
* @param chargepoint_id Charge Point identifier
Expand Down
42 changes: 42 additions & 0 deletions src/tools/log/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,47 @@ void Logger::registerLogger(ocpp::database::Database& database, const std::strin
}
}

/** @brief External logging function */
std::function<void(unsigned int, const std::string&)> ExtLogger::m_log_function = [](unsigned int level, const std::string& log_line)
{
// Default function if no external logger has been registered
static std::mutex mutex;

std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::lock_guard<std::mutex> lock(mutex);

std::tm now_tm;
localtime_r(&now, &now_tm);
LOG_OUTPUT << level << " - [" << std::put_time(&now_tm, "%Y-%m-%dT%T") << "] - " << log_line << std::endl;
};

/** @brief Constructor */
ExtLogger::ExtLogger(const char* level_str, unsigned int level, const char* filename, const char* line) : m_log_output(), m_level(level)
{
(void)level_str;
m_log_output << filename << ":" << line << " - ";
}

/** @brief Constructor */
ExtLogger::ExtLogger(const char* name, const char* level_str, unsigned int level, const char* filename, const char* line)
: m_log_output(), m_level(level)
{
(void)name;
(void)level_str;
m_log_output << filename << ":" << line << " - ";
}

/** @brief Destructor */
ExtLogger::~ExtLogger()
{
m_log_function(m_level, m_log_output.str());
}

/** @brief Register an external logging function */
void ExtLogger::registerLogFunction(std::function<void(unsigned int, const std::string&)> log_function)
{
m_log_function = log_function;
}

} // namespace log
} // namespace ocpp
Loading