Skip to content

Commit 1f7d92e

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 1f7d92e

13 files changed

+88
-48
lines changed

include/ocpp/common/websocket/websocket.hpp

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

1111
namespace ocpp {
12+
13+
class WebsocketBase;
14+
struct WebsocketConnectionOptions;
15+
16+
///
17+
/// \brief Reason why a websocket closes its connection
18+
///
19+
enum class WebsocketCloseReason : uint8_t
20+
{
21+
/// Normal closure, meaning that the purpose for which the connection was
22+
/// established has been fulfilled.
23+
Normal = 1,
24+
/// Close the connection with a forced TCP drop.
25+
/**
26+
* This special value requests that the WebSocket connection be closed by
27+
* forcibly dropping the TCP connection. This will leave the other side of
28+
* the connection with a broken connection and some expensive timeouts. this
29+
* should not be done except in extreme cases or in cases of malicious
30+
* remote endpoints.
31+
*/
32+
ForceTcpDrop,
33+
/// The endpoint was "going away", such as a server going down or a browser
34+
/// navigating away from a page.
35+
GoingAway,
36+
/// A dummy value to indicate that the connection was closed abnormally.
37+
/**
38+
* In such a case there was no close frame to extract a value from. This
39+
* value is illegal on the wire.
40+
*/
41+
AbnormalClose,
42+
/// Indicates that the service is restarted. A client may reconnect and if
43+
/// if it chooses to do so, should reconnect using a randomized delay of
44+
/// 5-30s
45+
ServiceRestart
46+
};
47+
1248
///
1349
/// \brief contains a websocket abstraction that can connect to TLS and non-TLS websocket endpoints
1450
///
@@ -18,7 +54,7 @@ class Websocket {
1854
std::unique_ptr<WebsocketBase> websocket;
1955
std::function<void(const int security_profile)> connected_callback;
2056
std::function<void()> disconnected_callback;
21-
std::function<void(const websocketpp::close::status::value reason)> closed_callback;
57+
std::function<void(const WebsocketCloseReason reason)> closed_callback;
2258
std::function<void(const std::string& message)> message_callback;
2359
std::shared_ptr<MessageLogging> logging;
2460

@@ -34,7 +70,7 @@ class Websocket {
3470
void set_connection_options(const WebsocketConnectionOptions& connection_options);
3571

3672
/// \brief disconnect the websocket
37-
void disconnect(websocketpp::close::status::value code);
73+
void disconnect(const WebsocketCloseReason code);
3874

3975
// \brief reconnects the websocket after the delay
4076
void reconnect(std::error_code reason, long delay);
@@ -50,7 +86,7 @@ class Websocket {
5086

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

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

include/ocpp/common/websocket/websocket_base.hpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <websocketpp/config/asio_client.hpp>
1414

1515
#include <ocpp/common/types.hpp>
16+
#include <ocpp/common/websocket/websocket.hpp>
1617
#include <ocpp/common/websocket/websocket_uri.hpp>
1718

1819
namespace ocpp {
@@ -36,6 +37,7 @@ struct WebsocketConnectionOptions {
3637
std::optional<std::string> hostName;
3738
bool verify_csms_common_name;
3839
bool use_tpm_tls;
40+
std::optional<std::string> iface_or_ip; ///< The interface of the connection or the ip address of the interface
3941
};
4042

4143
///
@@ -47,7 +49,7 @@ class WebsocketBase {
4749
WebsocketConnectionOptions connection_options;
4850
std::function<void(const int security_profile)> connected_callback;
4951
std::function<void()> disconnected_callback;
50-
std::function<void(const websocketpp::close::status::value reason)> closed_callback;
52+
std::function<void(const WebsocketCloseReason reason)> closed_callback;
5153
std::function<void(const std::string& message)> message_callback;
5254
websocketpp::lib::shared_ptr<boost::asio::steady_timer> reconnect_timer;
5355
std::unique_ptr<Everest::SteadyTimer> ping_timer;
@@ -101,13 +103,13 @@ class WebsocketBase {
101103
virtual void reconnect(std::error_code reason, long delay) = 0;
102104

103105
/// \brief disconnect the websocket
104-
void disconnect(websocketpp::close::status::value code);
106+
void disconnect(WebsocketCloseReason code);
105107

106108
/// \brief indicates if the websocket is connected
107109
bool is_connected();
108110

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

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

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

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

include/ocpp/common/websocket/websocket_plain.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <websocketpp/client.hpp>
99
#include <websocketpp/config/asio_client.hpp>
1010

11+
#include <ocpp/common/websocket/websocket.hpp>
1112
#include <ocpp/common/websocket/websocket_base.hpp>
1213

1314
namespace ocpp {
@@ -60,7 +61,7 @@ class WebsocketPlain final : public WebsocketBase {
6061
void reconnect(std::error_code reason, long delay) override;
6162

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

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

include/ocpp/common/websocket/websocket_tls.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <websocketpp/config/asio_client.hpp>
88

99
#include <ocpp/common/evse_security.hpp>
10+
#include <ocpp/common/websocket/websocket.hpp>
1011
#include <ocpp/common/websocket/websocket_base.hpp>
1112

1213
namespace ocpp {
@@ -66,7 +67,7 @@ class WebsocketTLS final : public WebsocketBase {
6667
void reconnect(std::error_code reason, long delay) override;
6768

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

7172
/// \brief send a \p message over the websocket
7273
/// \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)