Skip to content

Commit 944e2aa

Browse files
committed
Changes on websocket interface to use a new defined enum.
Signed-off-by: Maaike Zijderveld <m.zijderveld@alfen.nl>
1 parent 9c659e8 commit 944e2aa

14 files changed

+85
-48
lines changed

include/ocpp/common/types.hpp

+32
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,38 @@ enum class MessageDirection {
602602
ChargingStationToCSMS
603603
};
604604

605+
///
606+
/// \brief Reason why a websocket closes its connection
607+
///
608+
enum class WebsocketCloseReason : uint8_t
609+
{
610+
/// Normal closure, meaning that the purpose for which the connection was
611+
/// established has been fulfilled.
612+
Normal = 1,
613+
/// Close the connection with a forced TCP drop.
614+
/**
615+
* This special value requests that the WebSocket connection be closed by
616+
* forcibly dropping the TCP connection. This will leave the other side of
617+
* the connection with a broken connection and some expensive timeouts. this
618+
* should not be done except in extreme cases or in cases of malicious
619+
* remote endpoints.
620+
*/
621+
ForceTcpDrop,
622+
/// The endpoint was "going away", such as a server going down or a browser
623+
/// navigating away from a page.
624+
GoingAway,
625+
/// A dummy value to indicate that the connection was closed abnormally.
626+
/**
627+
* In such a case there was no close frame to extract a value from. This
628+
* value is illegal on the wire.
629+
*/
630+
AbnormalClose,
631+
/// Indicates that the service is restarted. A client may reconnect and if
632+
/// if it chooses to do so, should reconnect using a randomized delay of
633+
/// 5-30s
634+
ServiceRestart
635+
};
636+
605637
} // namespace ocpp
606638

607639
#endif

include/ocpp/common/websocket/websocket.hpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <ocpp/common/websocket/websocket_tls.hpp>
1010

1111
namespace ocpp {
12+
13+
class WebsocketBase;
14+
struct WebsocketConnectionOptions;
15+
1216
///
1317
/// \brief contains a websocket abstraction that can connect to TLS and non-TLS websocket endpoints
1418
///
@@ -18,7 +22,7 @@ class Websocket {
1822
std::unique_ptr<WebsocketBase> websocket;
1923
std::function<void(const int security_profile)> connected_callback;
2024
std::function<void()> disconnected_callback;
21-
std::function<void(const websocketpp::close::status::value reason)> closed_callback;
25+
std::function<void(const WebsocketCloseReason reason)> closed_callback;
2226
std::function<void(const std::string& message)> message_callback;
2327
std::shared_ptr<MessageLogging> logging;
2428

@@ -34,7 +38,7 @@ class Websocket {
3438
void set_connection_options(const WebsocketConnectionOptions& connection_options);
3539

3640
/// \brief disconnect the websocket
37-
void disconnect(websocketpp::close::status::value code);
41+
void disconnect(const WebsocketCloseReason code);
3842

3943
// \brief reconnects the websocket after the delay
4044
void reconnect(std::error_code reason, long delay);
@@ -50,7 +54,7 @@ class Websocket {
5054

5155
/// \brief register a \p callback that is called when the websocket connection has been closed and will not attempt
5256
/// to reconnect
53-
void register_closed_callback(const std::function<void(const websocketpp::close::status::value reason)>& callback);
57+
void register_closed_callback(const std::function<void(const WebsocketCloseReason reason)>& callback);
5458

5559
/// \brief register a \p callback that is called when the websocket receives a message
5660
void register_message_callback(const std::function<void(const std::string& message)>& callback);

include/ocpp/common/websocket/websocket_base.hpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct WebsocketConnectionOptions {
3636
std::optional<std::string> hostName;
3737
bool verify_csms_common_name;
3838
bool use_tpm_tls;
39+
std::optional<std::string> iface_or_ip; ///< The interface of the connection or the ip address of the interface
3940
};
4041

4142
///
@@ -47,7 +48,7 @@ class WebsocketBase {
4748
WebsocketConnectionOptions connection_options;
4849
std::function<void(const int security_profile)> connected_callback;
4950
std::function<void()> disconnected_callback;
50-
std::function<void(const websocketpp::close::status::value reason)> closed_callback;
51+
std::function<void(const WebsocketCloseReason reason)> closed_callback;
5152
std::function<void(const std::string& message)> message_callback;
5253
websocketpp::lib::shared_ptr<boost::asio::steady_timer> reconnect_timer;
5354
std::unique_ptr<Everest::SteadyTimer> ping_timer;
@@ -101,13 +102,13 @@ class WebsocketBase {
101102
virtual void reconnect(std::error_code reason, long delay) = 0;
102103

103104
/// \brief disconnect the websocket
104-
void disconnect(websocketpp::close::status::value code);
105+
void disconnect(WebsocketCloseReason code);
105106

106107
/// \brief indicates if the websocket is connected
107108
bool is_connected();
108109

109110
/// \brief closes the websocket
110-
virtual void close(websocketpp::close::status::value code, const std::string& reason) = 0;
111+
virtual void close(WebsocketCloseReason code, const std::string& reason) = 0;
111112

112113
/// \brief register a \p callback that is called when the websocket is connected successfully
113114
void register_connected_callback(const std::function<void(const int security_profile)>& callback);
@@ -117,7 +118,7 @@ class WebsocketBase {
117118

118119
/// \brief register a \p callback that is called when the websocket connection has been closed and will not attempt
119120
/// to reconnect
120-
void register_closed_callback(const std::function<void(const websocketpp::close::status::value reason)>& callback);
121+
void register_closed_callback(const std::function<void(const WebsocketCloseReason reason)>& callback);
121122

122123
/// \brief register a \p callback that is called when the websocket receives a message
123124
void register_message_callback(const std::function<void(const std::string& message)>& callback);

include/ocpp/common/websocket/websocket_plain.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class WebsocketPlain final : public WebsocketBase {
6060
void reconnect(std::error_code reason, long delay) override;
6161

6262
/// \brief Closes a plaintext websocket connection
63-
void close(websocketpp::close::status::value code, const std::string& reason) override;
63+
void close(WebsocketCloseReason code, const std::string& reason) override;
6464

6565
/// \brief send a \p message over the websocket
6666
/// \returns true if the message was sent successfully

include/ocpp/common/websocket/websocket_tls.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class WebsocketTLS final : public WebsocketBase {
6666
void reconnect(std::error_code reason, long delay) override;
6767

6868
/// \brief closes the websocket
69-
void close(websocketpp::close::status::value code, const std::string& reason) override;
69+
void close(WebsocketCloseReason code, const std::string& reason) override;
7070

7171
/// \brief send a \p message over the websocket
7272
/// \returns true if the message was sent successfully

include/ocpp/common/websocket/websocket_tls_tpm.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class WebsocketTlsTPM final : public WebsocketBase {
3333
void reconnect(std::error_code reason, long delay) override;
3434

3535
/// \brief closes the websocket
36-
void close(websocketpp::close::status::value code, const std::string& reason) override;
36+
void close(WebsocketCloseReason code, const std::string& reason) override;
3737

3838
/// \brief send a \p message over the websocket
3939
/// \returns true if the message was sent successfully

include/ocpp/v201/charge_point.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ class ChargePoint : ocpp::ChargingStationBase {
588588

589589
/// \brief Disconnects the the websocket connection to the CSMS if it is connected
590590
/// \param code Optional websocket close status code (default: normal).
591-
void disconnect_websocket(websocketpp::close::status::value code = websocketpp::close::status::normal);
591+
void disconnect_websocket(WebsocketCloseReason code = WebsocketCloseReason::Normal);
592592

593593
/// \brief Chargepoint notifies about new firmware update status firmware_update_status. This function should be
594594
/// called during a Firmware Update to indicate the current firmware_update_status.

lib/ocpp/common/websocket/websocket.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void Websocket::set_connection_options(const WebsocketConnectionOptions& connect
4242
this->websocket->set_connection_options(connection_options);
4343
}
4444

45-
void Websocket::disconnect(websocketpp::close::status::value code) {
45+
void Websocket::disconnect(const WebsocketCloseReason code) {
4646
this->logging->sys("Disconnecting");
4747
this->websocket->disconnect(code);
4848
}
@@ -75,10 +75,10 @@ void Websocket::register_disconnected_callback(const std::function<void()>& call
7575
}
7676

7777
void Websocket::register_closed_callback(
78-
const std::function<void(const websocketpp::close::status::value reason)>& callback) {
78+
const std::function<void(const WebsocketCloseReason reason)>& callback) {
7979
this->closed_callback = callback;
8080
this->websocket->register_closed_callback(
81-
[this](const websocketpp::close::status::value reason) { this->closed_callback(reason); });
81+
[this](const WebsocketCloseReason reason) { this->closed_callback(reason); });
8282
}
8383

8484
void Websocket::register_message_callback(const std::function<void(const std::string& message)>& callback) {

lib/ocpp/common/websocket/websocket_base.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void WebsocketBase::register_disconnected_callback(const std::function<void()>&
4343
}
4444

4545
void WebsocketBase::register_closed_callback(
46-
const std::function<void(const websocketpp::close::status::value reason)>& callback) {
46+
const std::function<void(const WebsocketCloseReason reason)>& callback) {
4747
this->closed_callback = callback;
4848
}
4949

@@ -68,12 +68,12 @@ bool WebsocketBase::initialized() {
6868
return true;
6969
}
7070

71-
void WebsocketBase::disconnect(websocketpp::close::status::value code) {
71+
void WebsocketBase::disconnect(WebsocketCloseReason code) {
7272
if (!this->initialized()) {
7373
EVLOG_error << "Cannot disconnect a websocket that was not initialized";
7474
return;
7575
}
76-
if (code == websocketpp::close::status::normal) {
76+
if (code == WebsocketCloseReason::Normal) {
7777
this->shutting_down = true;
7878
}
7979
if (this->reconnect_timer) {
@@ -162,7 +162,7 @@ void WebsocketBase::on_pong_timeout(websocketpp::connection_hdl hdl, std::string
162162
if (!this->reconnecting) {
163163
EVLOG_info << "Reconnecting because of a pong timeout after " << this->connection_options.pong_timeout_s << "s";
164164
this->reconnecting = true;
165-
this->close(websocketpp::close::status::going_away, "Pong timeout");
165+
this->close(WebsocketCloseReason::GoingAway, "Pong timeout");
166166
}
167167
}
168168

lib/ocpp/common/websocket/websocket_plain.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ bool WebsocketPlain::connect() {
5858
if (this->m_is_connected) {
5959
try {
6060
EVLOG_info << "Closing websocket connection before reconnecting";
61-
this->ws_client.close(this->handle, websocketpp::close::status::normal, "");
61+
this->ws_client.close(this->handle, WebsocketCloseReason::Normal, "");
6262
} catch (std::exception& e) {
6363
EVLOG_error << "Error on plain close: " << e.what();
6464
}
@@ -107,7 +107,7 @@ void WebsocketPlain::reconnect(std::error_code reason, long delay) {
107107
if (this->m_is_connected) {
108108
try {
109109
EVLOG_info << "Closing websocket connection before reconnecting";
110-
this->ws_client.close(this->handle, websocketpp::close::status::normal, "");
110+
this->ws_client.close(this->handle, WebsocketCloseReason::Normal, "");
111111
} catch (std::exception& e) {
112112
EVLOG_error << "Error on plain close: " << e.what();
113113
}
@@ -125,7 +125,7 @@ void WebsocketPlain::reconnect(std::error_code reason, long delay) {
125125
// TODO(kai): complete error handling, especially making sure that a reconnect is only attempted in reasonable
126126
// circumstances
127127
switch (reason.value()) {
128-
case websocketpp::close::status::force_tcp_drop:
128+
case WebsocketCloseReason::ForceTcpDrop:
129129
/* code */
130130
break;
131131

@@ -223,7 +223,7 @@ void WebsocketPlain::on_close_plain(client* c, websocketpp::connection_hdl hdl)
223223
<< websocketpp::close::status::get_string(con->get_remote_close_code())
224224
<< "), reason: " << con->get_remote_close_reason();
225225
// dont reconnect on normal code
226-
if (con->get_remote_close_code() != websocketpp::close::status::normal) {
226+
if (con->get_remote_close_code() != WebsocketCloseReason::Normal) {
227227
this->reconnect(error_code, this->get_reconnect_interval());
228228
} else {
229229
this->closed_callback(con->get_remote_close_code());
@@ -246,11 +246,11 @@ void WebsocketPlain::on_fail_plain(client* c, websocketpp::connection_hdl hdl) {
246246
this->connection_attempts <= this->connection_options.max_connection_attempts) {
247247
this->reconnect(ec, this->get_reconnect_interval());
248248
} else {
249-
this->close(websocketpp::close::status::normal, "Connection failed");
249+
this->close(WebsocketCloseReason::Normal, "Connection failed");
250250
}
251251
}
252252

253-
void WebsocketPlain::close(websocketpp::close::status::value code, const std::string& reason) {
253+
void WebsocketPlain::close(WebsocketCloseReason code, const std::string& reason) {
254254
EVLOG_info << "Closing plain websocket.";
255255
websocketpp::lib::error_code ec;
256256
this->cancel_reconnect_timer();
@@ -260,7 +260,7 @@ void WebsocketPlain::close(websocketpp::close::status::value code, const std::st
260260
if (ec) {
261261
EVLOG_error << "Error initiating close of plain websocket: " << ec.message();
262262
// on_close_plain won't be called here so we have to call the closed_callback manually
263-
this->closed_callback(websocketpp::close::status::abnormal_close);
263+
this->closed_callback(WebsocketCloseReason::AbnormalClose);
264264
} else {
265265
EVLOG_info << "Closed plain websocket successfully.";
266266
}

lib/ocpp/common/websocket/websocket_tls.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ bool WebsocketTLS::connect() {
154154
if (this->m_is_connected) {
155155
try {
156156
EVLOG_info << "Closing websocket connection before reconnecting";
157-
this->wss_client.close(this->handle, websocketpp::close::status::normal, "");
157+
this->wss_client.close(this->handle, WebsocketCloseReason::Normal, "");
158158
} catch (std::exception& e) {
159159
EVLOG_error << "Error on TLS close: " << e.what();
160160
}
@@ -202,7 +202,7 @@ void WebsocketTLS::reconnect(std::error_code reason, long delay) {
202202
if (this->m_is_connected) {
203203
try {
204204
EVLOG_info << "Closing websocket connection before reconnecting";
205-
this->wss_client.close(this->handle, websocketpp::close::status::normal, "");
205+
this->wss_client.close(this->handle, WebsocketCloseReason::Normal, "");
206206
} catch (std::exception& e) {
207207
EVLOG_error << "Error on plain close: " << e.what();
208208
}
@@ -220,7 +220,7 @@ void WebsocketTLS::reconnect(std::error_code reason, long delay) {
220220
// TODO(kai): complete error handling, especially making sure that a reconnect is only attempted in reasonable
221221
// circumstances
222222
switch (reason.value()) {
223-
case websocketpp::close::status::force_tcp_drop:
223+
case WebsocketCloseReason::ForceTcpDrop:
224224
/* code */
225225
break;
226226

@@ -407,7 +407,7 @@ void WebsocketTLS::on_close_tls(tls_client* c, websocketpp::connection_hdl hdl)
407407
<< websocketpp::close::status::get_string(con->get_remote_close_code())
408408
<< "), reason: " << con->get_remote_close_reason();
409409
// dont reconnect on normal close
410-
if (con->get_remote_close_code() != websocketpp::close::status::normal) {
410+
if (con->get_remote_close_code() != WebsocketCloseReason::Normal) {
411411
this->reconnect(error_code, this->get_reconnect_interval());
412412
} else {
413413
this->closed_callback(con->get_remote_close_code());
@@ -431,11 +431,11 @@ void WebsocketTLS::on_fail_tls(tls_client* c, websocketpp::connection_hdl hdl) {
431431
this->connection_attempts <= this->connection_options.max_connection_attempts) {
432432
this->reconnect(ec, this->get_reconnect_interval());
433433
} else {
434-
this->close(websocketpp::close::status::normal, "Connection failed");
434+
this->close(WebsocketCloseReason::Normal, "Connection failed");
435435
}
436436
}
437437

438-
void WebsocketTLS::close(websocketpp::close::status::value code, const std::string& reason) {
438+
void WebsocketTLS::close(WebsocketCloseReason code, const std::string& reason) {
439439

440440
EVLOG_info << "Closing TLS websocket.";
441441

@@ -448,7 +448,7 @@ void WebsocketTLS::close(websocketpp::close::status::value code, const std::stri
448448
if (ec) {
449449
EVLOG_error << "Error initiating close of TLS websocket: " << ec.message();
450450
// on_close_tls wont be called here so we have to call the closed_callback manually
451-
this->closed_callback(websocketpp::close::status::abnormal_close);
451+
this->closed_callback(WebsocketCloseReason::AbnormalClose);
452452
} else {
453453
EVLOG_info << "Closed TLS websocket successfully.";
454454
}

lib/ocpp/common/websocket/websocket_tls_tpm.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ bool WebsocketTlsTPM::connect() {
509509
// close connection before reconnecting
510510
if (this->m_is_connected) {
511511
EVLOG_info << "Closing websocket connection before reconnecting";
512-
this->close(websocketpp::close::status::abnormal_close, "Reconnect");
512+
this->close(WebsocketCloseReason::AbnormalClose, "Reconnect");
513513
}
514514

515515
{
@@ -535,7 +535,7 @@ void WebsocketTlsTPM::reconnect(std::error_code reason, long delay) {
535535
std::lock_guard<std::mutex> lk(this->reconnect_mutex);
536536
if (this->m_is_connected) {
537537
EVLOG_info << "Closing websocket connection before reconnecting";
538-
this->close(websocketpp::close::status::abnormal_close, "Reconnect");
538+
this->close(WebsocketCloseReason::AbnormalClose, "Reconnect");
539539
}
540540

541541
if (!this->reconnect_timer_tpm) {
@@ -550,7 +550,7 @@ void WebsocketTlsTPM::reconnect(std::error_code reason, long delay) {
550550
}
551551
}
552552

553-
void WebsocketTlsTPM::close(websocketpp::close::status::value code, const std::string& reason) {
553+
void WebsocketTlsTPM::close(WebsocketCloseReason code, const std::string& reason) {
554554
EVLOG_info << "Closing TLS TPM websocket with reason: " << reason;
555555

556556
{
@@ -572,7 +572,7 @@ void WebsocketTlsTPM::close(websocketpp::close::status::value code, const std::s
572572
}
573573

574574
this->m_is_connected = false;
575-
this->closed_callback(websocketpp::close::status::normal);
575+
this->closed_callback(WebsocketCloseReason::Normal);
576576
}
577577

578578
void WebsocketTlsTPM::on_conn_connected() {
@@ -593,7 +593,7 @@ void WebsocketTlsTPM::on_conn_close() {
593593
this->disconnected_callback();
594594
this->cancel_reconnect_timer();
595595

596-
this->closed_callback(websocketpp::close::status::normal);
596+
this->closed_callback(WebsocketCloseReason::Normal);
597597
}
598598

599599
void WebsocketTlsTPM::on_conn_fail() {
@@ -612,7 +612,7 @@ void WebsocketTlsTPM::on_conn_fail() {
612612
this->reconnect(std::error_code(), this->get_reconnect_interval());
613613
} else {
614614
EVLOG_info << "Closed TLS websocket, reconnect attempts exhausted";
615-
this->close(websocketpp::close::status::normal, "Connection failed");
615+
this->close(WebsocketCloseReason::Normal, "Connection failed");
616616
}
617617
}
618618

0 commit comments

Comments
 (0)