Skip to content

Commit

Permalink
Enable the connect callback to update the connection state variable
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Suess <[email protected]>
  • Loading branch information
Matthias-NIDEC committed Oct 23, 2023
1 parent 8292d0c commit 023c054
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 26 deletions.
5 changes: 3 additions & 2 deletions include/ocpp/common/websocket/websocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Websocket {
// unique_ptr holds address of base - requires WebSocketBase to have a virtual destructor
std::unique_ptr<WebsocketBase> websocket;
std::function<void(const int security_profile)> connected_callback;
std::function<void(const websocketpp::close::status::value reason)> closed_callback;
std::function<void(const websocketpp::close::status::value reason, const bool doFull)> closed_callback;
std::function<void(const std::string& message)> message_callback;
std::shared_ptr<MessageLogging> logging;

Expand Down Expand Up @@ -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<void(const websocketpp::close::status::value reason)>& callback);
void register_closed_callback(
const std::function<void(const websocketpp::close::status::value reason, const bool doFull)>& callback);

/// \brief register a \p callback that is called when the websocket receives a message
void register_message_callback(const std::function<void(const std::string& message)>& callback);
Expand Down
5 changes: 3 additions & 2 deletions include/ocpp/common/websocket/websocket_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class WebsocketBase {
bool m_is_connected;
WebsocketConnectionOptions connection_options;
std::function<void(const int security_profile)> connected_callback;
std::function<void(const websocketpp::close::status::value reason)> closed_callback;
std::function<void(const websocketpp::close::status::value reason, const bool doFull)> closed_callback;
std::function<void(const std::string& message)> message_callback;
websocketpp::lib::shared_ptr<boost::asio::steady_timer> reconnect_timer;
std::unique_ptr<Everest::SteadyTimer> ping_timer;
Expand Down Expand Up @@ -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<void(const websocketpp::close::status::value reason)>& callback);
void register_closed_callback(
const std::function<void(const websocketpp::close::status::value reason, const bool doFull)>& callback);

/// \brief register a \p callback that is called when the websocket receives a message
void register_message_callback(const std::function<void(const std::string& message)>& callback);
Expand Down
6 changes: 3 additions & 3 deletions lib/ocpp/common/websocket/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ void Websocket::register_connected_callback(const std::function<void(const int s
}

void Websocket::register_closed_callback(
const std::function<void(const websocketpp::close::status::value reason)>& callback) {
const std::function<void(const websocketpp::close::status::value reason, const bool doFull)>& 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);
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/ocpp/common/websocket/websocket_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void WebsocketBase::register_connected_callback(const std::function<void(const i
}

void WebsocketBase::register_closed_callback(
const std::function<void(const websocketpp::close::status::value reason)>& callback) {
const std::function<void(const websocketpp::close::status::value reason)>& callback, const bool doFull) {
this->closed_callback = callback;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/ocpp/common/websocket/websocket_plain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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.";
}
Expand Down
5 changes: 3 additions & 2 deletions lib/ocpp/common/websocket/websocket_tls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.";
}
Expand Down
28 changes: 15 additions & 13 deletions lib/ocpp/v16/charge_point_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
});

Expand Down
2 changes: 1 addition & 1 deletion lib/ocpp/v201/charge_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 023c054

Please sign in to comment.