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 examples/common/DefaultCentralSystemEventsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ bool DefaultCentralSystemEventsHandler::acceptConnection(const char* ip_address)
return true;
}

/** @copydoc void ICentralSystemEventsHandler::clientFailedToConnect(const char*) */
void DefaultCentralSystemEventsHandler::clientFailedToConnect(const char* ip_address)
{
cout << "Client [" << ip_address << "] failed to connect" << endl;
}

/** @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 @@ -48,6 +48,9 @@ class DefaultCentralSystemEventsHandler : public ocpp::centralsystem::ICentralSy
/** @copydoc bool ICentralSystemEventsHandler::acceptConnection(const char*) */
bool acceptConnection(const char* ip_address) override;

/** @copydoc void ICentralSystemEventsHandler::clientFailedToConnect(const char*) */
void clientFailedToConnect(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
6 changes: 6 additions & 0 deletions examples/common/DefaultLocalControllerEventsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ bool DefaultLocalControllerEventsHandler::acceptConnection(const char* ip_addres
return true;
}

/** @copydoc void ILocalControllerEventsHandler::clientFailedToConnect(const char*) */
void DefaultLocalControllerEventsHandler::clientFailedToConnect(const char* ip_address)
{
cout << "Client [" << ip_address << "] failed to connect" << endl;
}

/** @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 @@ -46,6 +46,9 @@ class DefaultLocalControllerEventsHandler : public ocpp::localcontroller::ILocal
/** @copydoc bool ILocalControllerEventsHandler::acceptConnection(const char*) */
bool acceptConnection(const char* ip_address) override;

/** @copydoc void ILocalControllerEventsHandler::clientFailedToConnect(const char*) */
void clientFailedToConnect(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 @@ -300,6 +300,14 @@ void CentralSystem::rpcClientConnected(const std::string& chargepoint_id, std::s
m_events_handler.chargePointConnected(chargepoint);
}

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

/** @copydoc void RpcServer::IListener::rpcServerError() */
void CentralSystem::rpcServerError()
{
Expand Down
3 changes: 3 additions & 0 deletions src/centralsystem/CentralSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class CentralSystem : public ICentralSystem, public ocpp::rpc::RpcServer::IListe
/** @copydoc void RpcServer::IListener::rpcClientConnected(const std::string&, std::shared_ptr<Client>) */
void rpcClientConnected(const std::string& chargepoint_id, std::shared_ptr<ocpp::rpc::RpcServer::Client> client) override;

/** @copydoc void RpcServer::IListener::rpcClientFailedToConnect(const char*) */
void rpcClientFailedToConnect(const char* ip_address) override;

/** @copydoc void RpcServer::IListener::rpcServerError() */
void rpcServerError() override;

Expand Down
6 changes: 6 additions & 0 deletions src/centralsystem/interface/ICentralSystemEventsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class ICentralSystemEventsHandler
*/
virtual bool acceptConnection(const char* ip_address) = 0;

/**
* @brief Called when connection fails to established
* @param ip_address IP address of the client
*/
virtual void clientFailedToConnect(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 @@ -313,6 +313,14 @@ void LocalController::rpcClientConnected(const std::string& chargepoint_id, std:
m_events_handler.chargePointConnected(chargepoint);
}

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

/** @copydoc void RpcServer::IListener::rpcServerError() */
void LocalController::rpcServerError()
{
Expand Down
3 changes: 3 additions & 0 deletions src/localcontroller/LocalController.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class LocalController : public ILocalController, public ocpp::rpc::RpcServer::IL
/** @copydoc void RpcServer::IListener::rpcClientConnected(const std::string&, std::shared_ptr<Client>) */
void rpcClientConnected(const std::string& chargepoint_id, std::shared_ptr<ocpp::rpc::RpcServer::Client> client) override;

/** @copydoc void RpcServer::IListener::rpcClientFailedToConnect(const char*) */
void rpcClientFailedToConnect(const char* ip_address) override;

/** @copydoc void RpcServer::IListener::rpcServerError() */
void rpcServerError() override;

Expand Down
6 changes: 6 additions & 0 deletions src/localcontroller/interface/ILocalControllerEventsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class ILocalControllerEventsHandler
*/
virtual bool acceptConnection(const char* ip_address) = 0;

/**
* @brief Called when connection fails to established
* @param ip_address IP address of the client
*/
virtual void clientFailedToConnect(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/rpc/RpcBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@ void RpcBase::stop()
}
}

/** @brief Process the websocket disconnection event */
void RpcBase::processDisconnected()
{
// Disable queues
m_requests_queue.setEnable(false);
m_results_queue.setEnable(false);
}

/** @brief Process received data */
void RpcBase::processReceivedData(const void* data, size_t size)
{
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/RpcBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ class RpcBase : public IRpc
void start();
/** @brief Stop RPC operations */
void stop();
/** @brief Process the websocket disconnection event */
void processDisconnected();
/** @brief Process received data */
void processReceivedData(const void* data, size_t size);
/** @brief Get the RPC listener */
Expand Down
10 changes: 10 additions & 0 deletions src/rpc/RpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ void RpcServer::wsClientConnected(const char* uri, std::shared_ptr<ocpp::websock
m_listener->rpcClientConnected(chargepoint_id, rpc_client);
}

/** @copydoc void IWebsocketServer::IListener::wsClientFailedToConnect(const char*) */
void RpcServer::wsClientFailedToConnect(const char* ip_address)
{
m_listener->rpcClientFailedToConnect(ip_address);
}

/** @copydoc void IWebsocketServer::IListener::wsServerError() */
void RpcServer::wsServerError()
{
Expand Down Expand Up @@ -173,6 +179,10 @@ bool RpcServer::Client::isConnected() const
/** @brief void IWebsocketServer::IClient::IListener::wsClientDisconnected() */
void RpcServer::Client::wsClientDisconnected()
{
// Process disconnection event
processDisconnected();

// Notify listener
rpcListener()->rpcDisconnected();
}

Expand Down
9 changes: 9 additions & 0 deletions src/rpc/RpcServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class RpcServer : public ocpp::websockets::IWebsocketServer::IListener
/** @copydoc void IWebsocketServer::IListener::wsClientConnected(const char*, std::shared_ptr<IClient>) */
void wsClientConnected(const char* uri, std::shared_ptr<ocpp::websockets::IWebsocketServer::IClient> client) override;

/** @copydoc void IWebsocketServer::IListener::wsClientFailedToConnect(const char*) */
void wsClientFailedToConnect(const char* ip_address) override;

/** @copydoc void IWebsocketServer::IListener::wsServerError() */
void wsServerError() override;

Expand Down Expand Up @@ -111,6 +114,12 @@ class RpcServer : public ocpp::websockets::IWebsocketServer::IListener
*/
virtual void rpcClientConnected(const std::string& chargepoint_id, std::shared_ptr<Client> client) = 0;

/**
* @brief Called when connection fails to established
* @param ip_address IP address of the client
*/
virtual void rpcClientFailedToConnect(const char* ip_address) = 0;

/** @brief Called on critical error */
virtual void rpcServerError() = 0;
};
Expand Down
6 changes: 6 additions & 0 deletions src/websockets/IWebsocketServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ class IWebsocketServer
*/
virtual void wsClientConnected(const char* uri, std::shared_ptr<IClient> client) = 0;

/**
* @brief Called when connection fails to established
* @param ip_address IP address of the client
*/
virtual void wsClientFailedToConnect(const char* ip_address) = 0;

/** @brief Called on critical error */
virtual void wsServerError() = 0;
};
Expand Down
45 changes: 40 additions & 5 deletions src/websockets/libwebsockets/LibWebsocketServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ LibWebsocketServer::LibWebsocketServer()
m_wsi(nullptr),
m_retry_policy(),
m_protocols(),
m_connecting_ip_address(nullptr),
m_clients()
{
}
Expand Down Expand Up @@ -278,18 +279,33 @@ int LibWebsocketServer::eventCallback(struct lws* wsi, enum lws_callback_reasons
struct lws_filter_network_conn_args* filter = reinterpret_cast<struct lws_filter_network_conn_args*>(user);

// Get client IP address
char ip_address[64];
lws_sa46_write_numeric_address(reinterpret_cast<lws_sockaddr46*>(&filter->cli_addr), ip_address, sizeof(ip_address));
// => Save the current IP address so it can be retrieved
// in the LWS_CALLBACK_WSI_CREATE calls which happens
// during the same event loop processing
server->m_connecting_ip_address = new char[64u];
lws_sa46_write_numeric_address(reinterpret_cast<lws_sockaddr46*>(&filter->cli_addr), server->m_connecting_ip_address, 64u);

// Notify user
if (!server->m_listener->wsAcceptConnection(ip_address))
if (!server->m_listener->wsAcceptConnection(server->m_connecting_ip_address))
{
// Disconnect
ret = -1;

// Release memory
delete[] server->m_connecting_ip_address;
}
}
break;

case LWS_CALLBACK_WSI_CREATE:
{
// Set client IP address
// => Must be done here to ensure that the event loop is still working
// with the same client as in the LWS_CALLBACK_FILTER_NETWORK_CONNECTION call
lws_set_wsi_user(wsi, server->m_connecting_ip_address);
}
break;

case LWS_CALLBACK_HTTP_CONFIRM_UPGRADE:
{
// Check selected protocol
Expand Down Expand Up @@ -418,8 +434,7 @@ int LibWebsocketServer::eventCallback(struct lws* wsi, enum lws_callback_reasons
case LWS_CALLBACK_ESTABLISHED:
{
// Get client IP address
char ip_address[64];
lws_get_peer_simple(wsi, ip_address, sizeof(ip_address));
char* ip_address = reinterpret_cast<char*>(lws_wsi_user(wsi));

// Instanciate a new client
std::shared_ptr<IClient> client(new Client(wsi, ip_address));
Expand Down Expand Up @@ -455,10 +470,30 @@ int LibWebsocketServer::eventCallback(struct lws* wsi, enum lws_callback_reasons
{
client->m_listener->wsClientDisconnected();
}
}
}
break;

case LWS_CALLBACK_WSI_DESTROY:
{
// Get client IP address
char* ip_address = reinterpret_cast<char*>(lws_wsi_user(wsi));

// Get corresponding client
auto iter_client = server->m_clients.find(wsi);
if (iter_client != server->m_clients.end())
{
// Remove client
server->m_clients.erase(iter_client);
}
else
{
// Connection failed to be established
server->m_listener->wsClientFailedToConnect(ip_address);
}

// Release memory
delete[] ip_address;
}
break;

Expand Down
2 changes: 2 additions & 0 deletions src/websockets/libwebsockets/LibWebsocketServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ class LibWebsocketServer : public IWebsocketServer
lws_retry_bo_t m_retry_policy;
/** @brief Protocols */
std::array<struct lws_protocols, 2u> m_protocols;
/** @brief IP address of the currently connecting client */
char* m_connecting_ip_address;

/** @brief Connected clients */
std::unordered_map<struct lws*, std::shared_ptr<IClient>> m_clients;
Expand Down