From 023c0540daefb4dece20b9d532cf438f6abe4047 Mon Sep 17 00:00:00 2001 From: Matthias Suess Date: Mon, 23 Oct 2023 16:48:17 +0200 Subject: [PATCH] Enable the connect callback to update the connection state variable Signed-off-by: Matthias Suess --- include/ocpp/common/websocket/websocket.hpp | 5 ++-- .../ocpp/common/websocket/websocket_base.hpp | 5 ++-- lib/ocpp/common/websocket/websocket.cpp | 6 ++-- lib/ocpp/common/websocket/websocket_base.cpp | 2 +- lib/ocpp/common/websocket/websocket_plain.cpp | 5 ++-- lib/ocpp/common/websocket/websocket_tls.cpp | 5 ++-- lib/ocpp/v16/charge_point_impl.cpp | 28 ++++++++++--------- lib/ocpp/v201/charge_point.cpp | 2 +- 8 files changed, 32 insertions(+), 26 deletions(-) diff --git a/include/ocpp/common/websocket/websocket.hpp b/include/ocpp/common/websocket/websocket.hpp index 95cfbb31b..26c765544 100644 --- a/include/ocpp/common/websocket/websocket.hpp +++ b/include/ocpp/common/websocket/websocket.hpp @@ -17,7 +17,7 @@ class Websocket { // unique_ptr holds address of base - requires WebSocketBase to have a virtual destructor std::unique_ptr websocket; std::function connected_callback; - std::function closed_callback; + std::function closed_callback; std::function message_callback; std::shared_ptr logging; @@ -46,7 +46,8 @@ class Websocket { /// \brief register a \p callback that is called when the websocket connection has been closed and will not attempt /// to reconnect - void register_closed_callback(const std::function& callback); + void register_closed_callback( + const std::function& callback); /// \brief register a \p callback that is called when the websocket receives a message void register_message_callback(const std::function& callback); diff --git a/include/ocpp/common/websocket/websocket_base.hpp b/include/ocpp/common/websocket/websocket_base.hpp index 542f17977..3a080ec86 100644 --- a/include/ocpp/common/websocket/websocket_base.hpp +++ b/include/ocpp/common/websocket/websocket_base.hpp @@ -45,7 +45,7 @@ class WebsocketBase { bool m_is_connected; WebsocketConnectionOptions connection_options; std::function connected_callback; - std::function closed_callback; + std::function closed_callback; std::function message_callback; websocketpp::lib::shared_ptr reconnect_timer; std::unique_ptr ping_timer; @@ -111,7 +111,8 @@ class WebsocketBase { /// \brief register a \p callback that is called when the websocket connection has been closed and will not attempt /// to reconnect - void register_closed_callback(const std::function& callback); + void register_closed_callback( + const std::function& callback); /// \brief register a \p callback that is called when the websocket receives a message void register_message_callback(const std::function& callback); diff --git a/lib/ocpp/common/websocket/websocket.cpp b/lib/ocpp/common/websocket/websocket.cpp index 8ddcb8169..599460a8f 100644 --- a/lib/ocpp/common/websocket/websocket.cpp +++ b/lib/ocpp/common/websocket/websocket.cpp @@ -59,11 +59,11 @@ void Websocket::register_connected_callback(const std::function& callback) { + const std::function& callback) { this->closed_callback = callback; - this->websocket->register_closed_callback([this](const websocketpp::close::status::value reason) { + this->websocket->register_closed_callback([this](const websocketpp::close::status::value reason, const bool doFull) { this->logging->sys("Disconnected"); - this->closed_callback(reason); + this->closed_callback(reason, doFull); }); } diff --git a/lib/ocpp/common/websocket/websocket_base.cpp b/lib/ocpp/common/websocket/websocket_base.cpp index 282434869..2818395ed 100644 --- a/lib/ocpp/common/websocket/websocket_base.cpp +++ b/lib/ocpp/common/websocket/websocket_base.cpp @@ -38,7 +38,7 @@ void WebsocketBase::register_connected_callback(const std::function& callback) { + const std::function& callback, const bool doFull) { this->closed_callback = callback; } diff --git a/lib/ocpp/common/websocket/websocket_plain.cpp b/lib/ocpp/common/websocket/websocket_plain.cpp index df7b5bf46..d453e7ed4 100644 --- a/lib/ocpp/common/websocket/websocket_plain.cpp +++ b/lib/ocpp/common/websocket/websocket_plain.cpp @@ -201,9 +201,10 @@ void WebsocketPlain::on_close_plain(client* c, websocketpp::connection_hdl hdl) << "), reason: " << con->get_remote_close_reason(); // dont reconnect on service restart code if (con->get_remote_close_code() != websocketpp::close::status::normal) { + this->closed_callback(con->get_remote_close_code(), false); this->reconnect(error_code, this->get_reconnect_interval()); } else { - this->closed_callback(con->get_remote_close_code()); + this->closed_callback(con->get_remote_close_code(), true); } } @@ -234,7 +235,7 @@ void WebsocketPlain::close(websocketpp::close::status::value code, const std::st if (ec) { EVLOG_error << "Error initiating close of plain websocket: " << ec.message(); // on_close_plain won't be called here so we have to call the closed_callback manually - this->closed_callback(websocketpp::close::status::abnormal_close); + this->closed_callback(websocketpp::close::status::abnormal_close, true); } else { EVLOG_info << "Closed plain websocket successfully."; } diff --git a/lib/ocpp/common/websocket/websocket_tls.cpp b/lib/ocpp/common/websocket/websocket_tls.cpp index c209179b5..dfae46217 100644 --- a/lib/ocpp/common/websocket/websocket_tls.cpp +++ b/lib/ocpp/common/websocket/websocket_tls.cpp @@ -291,9 +291,10 @@ void WebsocketTLS::on_close_tls(tls_client* c, websocketpp::connection_hdl hdl) << "), reason: " << con->get_remote_close_reason(); // dont reconnect on normal close if (con->get_remote_close_code() != websocketpp::close::status::normal) { + this->closed_callback(con->get_remote_close_code(), false); this->reconnect(error_code, this->get_reconnect_interval()); } else { - this->closed_callback(con->get_remote_close_code()); + this->closed_callback(con->get_remote_close_code(), true); } } void WebsocketTLS::on_fail_tls(tls_client* c, websocketpp::connection_hdl hdl) { @@ -328,7 +329,7 @@ void WebsocketTLS::close(websocketpp::close::status::value code, const std::stri if (ec) { EVLOG_error << "Error initiating close of TLS websocket: " << ec.message(); // on_close_tls wont be called here so we have to call the closed_callback manually - this->closed_callback(websocketpp::close::status::abnormal_close); + this->closed_callback(websocketpp::close::status::abnormal_close, true); } else { EVLOG_info << "Closed TLS websocket successfully."; } diff --git a/lib/ocpp/v16/charge_point_impl.cpp b/lib/ocpp/v16/charge_point_impl.cpp index 2fbb53aa0..26c7a8bff 100644 --- a/lib/ocpp/v16/charge_point_impl.cpp +++ b/lib/ocpp/v16/charge_point_impl.cpp @@ -153,22 +153,24 @@ void ChargePointImpl::init_websocket() { this->message_queue->resume(); // this->connected_callback(); // }); - this->websocket->register_closed_callback([this](const websocketpp::close::status::value reason) { + this->websocket->register_closed_callback([this](const websocketpp::close::status::value reason, const bool doFull) { if (this->connection_state_changed_callback != nullptr) { this->connection_state_changed_callback(false); } - this->message_queue->pause(); // - if (this->ocsp_request_timer != nullptr) { - this->ocsp_request_timer->stop(); - } - if (this->switch_security_profile_callback != nullptr) { - this->switch_security_profile_callback(); - } - if (this->client_certificate_timer != nullptr) { - this->client_certificate_timer->stop(); - } - if (this->v2g_certificate_timer != nullptr) { - this->v2g_certificate_timer->stop(); + if (doFull) { + this->message_queue->pause(); // + if (this->ocsp_request_timer != nullptr) { + this->ocsp_request_timer->stop(); + } + if (this->switch_security_profile_callback != nullptr) { + this->switch_security_profile_callback(); + } + if (this->client_certificate_timer != nullptr) { + this->client_certificate_timer->stop(); + } + if (this->v2g_certificate_timer != nullptr) { + this->v2g_certificate_timer->stop(); + } } }); diff --git a/lib/ocpp/v201/charge_point.cpp b/lib/ocpp/v201/charge_point.cpp index e78d61e48..2c4e49009 100644 --- a/lib/ocpp/v201/charge_point.cpp +++ b/lib/ocpp/v201/charge_point.cpp @@ -577,7 +577,7 @@ void ChargePoint::init_websocket() { }); this->websocket->register_closed_callback( - [this, connection_options, configuration_slot](const websocketpp::close::status::value reason) { + [this, connection_options, configuration_slot](const websocketpp::close::status::value reason, const bool doFull) { EVLOG_warning << "Failed to connect to NetworkConfigurationPriority: " << this->network_configuration_priority + 1 << " which is configurationSlot: " << configuration_slot;