Skip to content

Commit 7a4c2ec

Browse files
authored
Merge pull request #5 from c-jimenez/dev/websocket_ping_interval
Dev/websocket ping interval
2 parents f7e73c8 + cc9406d commit 7a4c2ec

File tree

11 files changed

+89
-56
lines changed

11 files changed

+89
-56
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ In the "Owner" column, "S" means that the configuration key behavior is handled
9898
| TransactionMessageAttempts | S | None |
9999
| TransactionMessageRetryInterval | S | None |
100100
| UnlockConnectorOnEVSideDisconnect | U | None |
101-
| WebSocketPingInterval | S | Not implemented yet, websocket ping interval is set to 3s |
101+
| WebSocketPingInterval | S | Reboot required |
102102
| LocalAuthListEnabled | S | None |
103103
| LocalAuthListMaxLength | S | None |
104104
| SendLocalListMaxLength | S | None |

src/chargepoint/ChargePoint.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,11 @@ bool ChargePoint::doConnect()
806806
credentials.skip_server_name_check = m_stack_config.tlsSkipServerNameCheck();
807807

808808
// Start connection process
809-
return m_rpc_client->start(
810-
connection_url, credentials, m_stack_config.connectionTimeout().count(), m_stack_config.retryInterval().count());
809+
return m_rpc_client->start(connection_url,
810+
credentials,
811+
m_stack_config.connectionTimeout(),
812+
m_stack_config.retryInterval(),
813+
m_ocpp_config.webSocketPingInterval());
811814
}
812815

813816
/** @brief Specific configuration check for parameter : AuthorizationKey */

src/rpc/IRpcClient.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ class IRpcClient
4646
* @param credentials Credentials to use
4747
* @param connect_timeout Connection timeout in ms
4848
* @param retry_interval Retry interval in ms when connection cannot be established (0 = no retry)
49+
* @param ping_interval Interval between 2 websocket PING messages when the socket is idle
4950
* @return true if the client has been started, false otherwise
5051
*/
5152
virtual bool start(const std::string& url,
5253
const ocpp::websockets::IWebsocketClient::Credentials& credentials,
53-
unsigned int connect_timeout = 5000u,
54-
unsigned int retry_interval = 5000u) = 0;
54+
std::chrono::milliseconds connect_timeout = std::chrono::seconds(5),
55+
std::chrono::milliseconds retry_interval = std::chrono::seconds(5),
56+
std::chrono::milliseconds ping_interval = std::chrono::seconds(5)) = 0;
5557

5658
/**
5759
* @brief Stop the client

src/rpc/RpcClient.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,21 @@ RpcClient::~RpcClient()
5959

6060
// IRpcClient interface
6161

62-
/** @copydoc bool IRpcClient::start(const std::string& , const IWebsocketClient::Credentials&, unsigned int, unsigned int) */
62+
/** @copydoc bool IRpcClient::start(const std::string&, const std::string&, const Credentials&,
63+
* std::chrono::milliseconds, std::chrono::milliseconds, std::chrono::milliseconds) */
6364
bool RpcClient::start(const std::string& url,
6465
const ocpp::websockets::IWebsocketClient::Credentials& credentials,
65-
unsigned int connect_timeout,
66-
unsigned int retry_interval)
66+
std::chrono::milliseconds connect_timeout,
67+
std::chrono::milliseconds retry_interval,
68+
std::chrono::milliseconds ping_interval)
6769
{
6870
bool ret = false;
6971

7072
// Check if already started
7173
if (!m_rx_thread)
7274
{
7375
// Connect to websocket
74-
ret = m_websocket.connect(url, m_protocol, credentials, connect_timeout, retry_interval);
76+
ret = m_websocket.connect(url, m_protocol, credentials, connect_timeout, retry_interval, ping_interval);
7577
if (ret)
7678
{
7779
// Initialize transaction id sequence

src/rpc/RpcClient.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ class RpcClient : public IRpcClient, public ocpp::websockets::IWebsocketClient::
4545

4646
// IRpcClient interface
4747

48-
/** @copydoc bool IRpcClient::start(const std::string& , const IWebsocketClient::Credentials&, unsigned int, unsigned int) */
48+
/** @copydoc bool IRpcClient::start(const std::string&, const std::string&, const Credentials&,
49+
* std::chrono::milliseconds, std::chrono::milliseconds, std::chrono::milliseconds) */
4950
bool start(const std::string& url,
5051
const ocpp::websockets::IWebsocketClient::Credentials& credentials,
51-
unsigned int connect_timeout = 5000u,
52-
unsigned int retry_interval = 5000u) override;
52+
std::chrono::milliseconds connect_timeout = std::chrono::seconds(5),
53+
std::chrono::milliseconds retry_interval = std::chrono::seconds(5),
54+
std::chrono::milliseconds ping_interval = std::chrono::seconds(5)) override;
5355

5456
/** @copydoc bool IRpcClient::stop() */
5557
bool stop() override;

src/websockets/IWebsocketClient.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ along with OpenOCPP. If not, see <http://www.gnu.org/licenses/>.
1919
#ifndef IWEBSOCKETCLIENT_H
2020
#define IWEBSOCKETCLIENT_H
2121

22+
#include <chrono>
2223
#include <string>
2324

2425
namespace ocpp
@@ -42,15 +43,17 @@ class IWebsocketClient
4243
* @param url URL to connect to
4344
* @param protocol Name of the protocol to use
4445
* @param credentials Credentials to use
45-
* @param connect_timeout Connection timeout in ms
46-
* @param retry_interval Retry interval in ms when connection cannot be established (0 = no retry)
46+
* @param connect_timeout Connection timeout
47+
* @param retry_interval Retry interval when connection cannot be established (0 = no retry)
48+
* @param ping_interval Interval between 2 websocket PING messages when the socket is idle
4749
* @return true if the connexion process has been started, false otherwise
4850
*/
49-
virtual bool connect(const std::string& url,
50-
const std::string& protocol,
51-
const Credentials& credentials,
52-
unsigned int connect_timeout = 5000u,
53-
unsigned int retry_interval = 5000u) = 0;
51+
virtual bool connect(const std::string& url,
52+
const std::string& protocol,
53+
const Credentials& credentials,
54+
std::chrono::milliseconds connect_timeout = std::chrono::seconds(5),
55+
std::chrono::milliseconds retry_interval = std::chrono::seconds(5),
56+
std::chrono::milliseconds ping_interval = std::chrono::seconds(5)) = 0;
5457

5558
/**
5659
* @brief Disconnect the client

src/websockets/libwebsockets/LibWebsocketClient.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ LibWebsocketClient::LibWebsocketClient()
3737
m_thread(nullptr),
3838
m_end(false),
3939
m_retry_interval(0),
40+
m_ping_interval(0),
4041
m_connection_error_notified(false),
4142
m_url(),
4243
m_protocol(""),
@@ -57,12 +58,14 @@ LibWebsocketClient::~LibWebsocketClient()
5758
disconnect();
5859
}
5960

60-
/** @copydoc bool IWebsocketClient::connect(const std::string&, const std::string&, const Credentials&, unsigned int, unsigned int) */
61-
bool LibWebsocketClient::connect(const std::string& url,
62-
const std::string& protocol,
63-
const Credentials& credentials,
64-
unsigned int connect_timeout,
65-
unsigned int retry_interval)
61+
/** @copydoc bool IWebsocketClient::connect(const std::string&, const std::string&, const Credentials&,
62+
* std::chrono::milliseconds, std::chrono::milliseconds, std::chrono::milliseconds) */
63+
bool LibWebsocketClient::connect(const std::string& url,
64+
const std::string& protocol,
65+
const Credentials& credentials,
66+
std::chrono::milliseconds connect_timeout,
67+
std::chrono::milliseconds retry_interval,
68+
std::chrono::milliseconds ping_interval)
6669
{
6770
bool ret = false;
6871

@@ -83,7 +86,7 @@ bool LibWebsocketClient::connect(const std::string& url,
8386
info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
8487
info.port = CONTEXT_PORT_NO_LISTEN;
8588
info.protocols = protocols;
86-
info.timeout_secs = connect_timeout / 1000u;
89+
info.timeout_secs = static_cast<unsigned int>(std::chrono::duration_cast<std::chrono::seconds>(connect_timeout).count());
8790
m_credentials = credentials;
8891
if (!m_credentials.tls12_cipher_list.empty())
8992
{
@@ -110,11 +113,12 @@ bool LibWebsocketClient::connect(const std::string& url,
110113
m_end = false;
111114
m_connection_error_notified = false;
112115
m_connected = false;
113-
m_retry_interval = retry_interval;
114-
m_retry_count = 0;
115-
m_protocol = protocol;
116-
m_thread = new std::thread(std::bind(&LibWebsocketClient::process, this));
117-
ret = true;
116+
m_retry_interval = static_cast<uint32_t>(retry_interval.count());
117+
m_ping_interval = static_cast<uint16_t>(std::chrono::duration_cast<std::chrono::seconds>(ping_interval).count());
118+
m_retry_count = 0;
119+
m_protocol = protocol;
120+
m_thread = new std::thread(std::bind(&LibWebsocketClient::process, this));
121+
ret = true;
118122
}
119123
}
120124
}
@@ -213,8 +217,8 @@ void LibWebsocketClient::connectCallback(struct lws_sorted_usec_list* sul)
213217
.retry_ms_table_count = 1,
214218
.conceal_count = 1,
215219

216-
.secs_since_valid_ping = 3, /* force PINGs after secs idle */
217-
.secs_since_valid_hangup = 10, /* hangup after secs idle */
220+
.secs_since_valid_ping = client->m_ping_interval, /* force PINGs after secs idle */
221+
.secs_since_valid_hangup = static_cast<uint16_t>(2u * client->m_ping_interval), /* hangup after secs idle */
218222

219223
.jitter_percent = 20,
220224
};

src/websockets/libwebsockets/LibWebsocketClient.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ class LibWebsocketClient : public IWebsocketClient
4141
/** @brief Destructor */
4242
virtual ~LibWebsocketClient();
4343

44-
/** @copydoc bool IWebsocketClient::connect(const std::string&, const std::string&, const Credentials&, unsigned int, unsigned int) */
45-
bool connect(const std::string& url,
46-
const std::string& protocol,
47-
const Credentials& credentials,
48-
unsigned int connect_timeout = 5000u,
49-
unsigned int retry_interval = 5000u) override;
44+
/** @copydoc bool IWebsocketClient::connect(const std::string&, const std::string&, const Credentials&,
45+
* std::chrono::milliseconds, std::chrono::milliseconds, std::chrono::milliseconds) */
46+
bool connect(const std::string& url,
47+
const std::string& protocol,
48+
const Credentials& credentials,
49+
std::chrono::milliseconds connect_timeout = std::chrono::seconds(5),
50+
std::chrono::milliseconds retry_interval = std::chrono::seconds(5),
51+
std::chrono::milliseconds ping_interval = std::chrono::seconds(5)) override;
5052

5153
/** @copydoc bool IWebsocketClient::disconnect() */
5254
bool disconnect() override;
@@ -91,6 +93,8 @@ class LibWebsocketClient : public IWebsocketClient
9193
bool m_end;
9294
/** @brief Retry interval in ms */
9395
uint32_t m_retry_interval;
96+
/** @brief PING interval in s */
97+
uint16_t m_ping_interval;
9498
/** @brief Indicate if the connection error has been notified at least once */
9599
bool m_connection_error_notified;
96100
/** @brief Connection URL */

src/websockets/stubs/WebsocketClientStub.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ WebsocketClientStub::WebsocketClientStub()
3333
m_credentials(),
3434
m_connect_timeout(0),
3535
m_retry_interval(0),
36+
m_ping_interval(0),
3637
m_disconnect_called(false),
3738
m_is_connected(false),
3839
m_send_called(false),
@@ -52,19 +53,22 @@ WebsocketClientStub::~WebsocketClientStub()
5253

5354
/// IWebsocketClient interface
5455

55-
/** @copydoc bool IWebsocketClient::connect(const std::string&, const std::string&, const Credentials&, unsigned int, unsigned int) */
56-
bool WebsocketClientStub::connect(const std::string& url,
57-
const std::string& protocol,
58-
const Credentials& credentials,
59-
unsigned int connect_timeout,
60-
unsigned int retry_interval)
56+
/** @copydoc bool IWebsocketClient::connect(const std::string&, const std::string&, const Credentials&,
57+
* std::chrono::milliseconds, std::chrono::milliseconds, std::chrono::milliseconds) */
58+
bool WebsocketClientStub::connect(const std::string& url,
59+
const std::string& protocol,
60+
const Credentials& credentials,
61+
std::chrono::milliseconds connect_timeout,
62+
std::chrono::milliseconds retry_interval,
63+
std::chrono::milliseconds ping_interval)
6164
{
6265
m_connect_called = true;
6366
m_url = url;
6467
m_protocol = protocol;
6568
m_credentials = credentials;
66-
m_connect_timeout = connect_timeout;
67-
m_retry_interval = retry_interval;
69+
m_connect_timeout = static_cast<unsigned int>(connect_timeout.count());
70+
m_retry_interval = static_cast<unsigned int>(retry_interval.count());
71+
m_ping_interval = static_cast<unsigned int>(ping_interval.count());
6872

6973
return returnValue();
7074
}

src/websockets/stubs/WebsocketClientStub.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ class WebsocketClientStub : public IWebsocketClient
4040

4141
/// IWebsocketClient interface
4242

43-
/** @copydoc bool IWebsocketClient::connect(const std::string&, const std::string&, const Credentials&, unsigned int, unsigned int) */
44-
bool connect(const std::string& url,
45-
const std::string& protocol,
46-
const Credentials& credentials,
47-
unsigned int connect_timeout,
48-
unsigned int retry_interval) override;
43+
/** @copydoc bool IWebsocketClient::connect(const std::string&, const std::string&, const Credentials&,
44+
* std::chrono::milliseconds, std::chrono::milliseconds, std::chrono::milliseconds) */
45+
bool connect(const std::string& url,
46+
const std::string& protocol,
47+
const Credentials& credentials,
48+
std::chrono::milliseconds connect_timeout,
49+
std::chrono::milliseconds retry_interval,
50+
std::chrono::milliseconds ping_interval) override;
4951

5052
/** @copydoc bool IWebsocketClient::disconnect() */
5153
bool disconnect() override;
@@ -93,6 +95,7 @@ class WebsocketClientStub : public IWebsocketClient
9395
const Credentials& credentials() const { return m_credentials; }
9496
unsigned int connectTimeout() const { return m_connect_timeout; }
9597
unsigned int retryInterval() const { return m_retry_interval; }
98+
unsigned int pingInterval() const { return m_ping_interval; }
9699
bool disconnectCalled() const { return m_disconnect_called; }
97100
bool sendCalled() const { return m_send_called; }
98101
const uint8_t* sentData() const { return m_sent_data; }
@@ -111,6 +114,8 @@ class WebsocketClientStub : public IWebsocketClient
111114
unsigned int m_connect_timeout;
112115
/** @brief Retry interval */
113116
unsigned int m_retry_interval;
117+
/** @brief PING interval */
118+
unsigned int m_ping_interval;
114119
/** @brief Indicate if the disconnect() function has been called */
115120
bool m_disconnect_called;
116121
/** @brief Connection state */

0 commit comments

Comments
 (0)