ocpp 0.24.1
A C++ implementation of the Open Charge Point Protocol
websocket_base.hpp
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest
3#ifndef OCPP_WEBSOCKET_BASE_HPP
4#define OCPP_WEBSOCKET_BASE_HPP
5
6#include <functional>
7#include <memory>
8#include <mutex>
9#include <thread>
10
11#include <everest/timer.hpp>
12
13#include <ocpp/common/types.hpp>
14#include <ocpp/common/websocket/websocket_uri.hpp>
15
16namespace ocpp {
17
19 std::vector<OcppProtocolVersion> ocpp_versions; // List of allowed protocols ordered by preference
20 Uri csms_uri; // the URI of the CSMS
21 int security_profile; // FIXME: change type to `SecurityProfile`
22 std::optional<std::string> authorization_key;
23 int retry_backoff_random_range_s;
24 int retry_backoff_repeat_times;
25 int retry_backoff_wait_minimum_s;
26 int max_connection_attempts;
27 std::string supported_ciphers_12;
28 std::string supported_ciphers_13;
29 int ping_interval_s;
30 std::string ping_payload;
31 int pong_timeout_s;
32 bool use_ssl_default_verify_paths;
33 std::optional<bool> additional_root_certificate_check;
34 std::optional<std::string> hostName;
35 bool verify_csms_common_name;
36 bool use_tpm_tls;
37 bool verify_csms_allow_wildcards;
38 std::optional<std::string> iface; // Optional interface where the socket is created. Only usable for libwebsocket
39 bool enable_tls_keylog = false;
40 std::optional<std::filesystem::path> keylog_file;
41};
42
47protected:
48 std::atomic_bool m_is_connected;
49 WebsocketConnectionOptions connection_options;
50 std::function<void(OcppProtocolVersion protocol)> connected_callback;
51 std::function<void()> disconnected_callback;
52 std::function<void(const WebsocketCloseReason reason)> stopped_connecting_callback;
53 std::function<void(const std::string& message)> message_callback;
54 std::function<void(ConnectionFailedReason)> connection_failed_callback;
55 std::shared_ptr<boost::asio::steady_timer> reconnect_timer;
56 std::unique_ptr<Everest::SteadyTimer> ping_timer;
57 std::mutex reconnect_mutex;
58 std::mutex connection_mutex;
59 std::atomic_int reconnect_backoff_ms;
60 std::atomic_int connection_attempts;
61 std::atomic_bool shutting_down;
62 std::atomic_bool reconnecting;
63
66 bool initialized();
67
69 std::optional<std::string> getAuthorizationHeader();
70
72 void log_on_fail(const std::error_code& ec, const boost::system::error_code& transport_ec, const int http_status);
73
77
78 // \brief cancels the reconnect timer
79 void cancel_reconnect_timer();
80
82 virtual void ping() = 0;
83
85 void on_pong_timeout(std::string msg);
86
87public:
90 explicit WebsocketBase();
91 virtual ~WebsocketBase();
92
96 virtual bool start_connecting() = 0;
97
99 virtual void set_connection_options(const WebsocketConnectionOptions& connection_options) = 0;
100 void set_connection_options_base(const WebsocketConnectionOptions& connection_options);
101
103 virtual void reconnect(long delay) = 0;
104
106 void disconnect(const WebsocketCloseReason code);
107
109 bool is_connected();
110
112 virtual void close(const WebsocketCloseReason code, const std::string& reason) = 0;
113
115 void register_connected_callback(const std::function<void(OcppProtocolVersion protocol)>& callback);
116
118 void register_disconnected_callback(const std::function<void()>& callback);
119
122 void register_stopped_connecting_callback(const std::function<void(const WebsocketCloseReason reason)>& callback);
123
125 void register_message_callback(const std::function<void(const std::string& message)>& callback);
126
128 void register_connection_failed_callback(const std::function<void(ConnectionFailedReason)>& callback);
129
132 virtual bool send(const std::string& message) = 0;
133
135 void set_websocket_ping_interval(int32_t interval_s);
136
138 void set_authorization_key(const std::string& authorization_key);
139};
140
141} // namespace ocpp
142#endif // OCPP_WEBSOCKET_BASE_HPP
Definition: websocket_uri.hpp:14
contains a websocket abstraction
Definition: websocket_base.hpp:46
void log_on_fail(const std::error_code &ec, const boost::system::error_code &transport_ec, const int http_status)
Logs websocket connection error.
Definition: websocket_base.cpp:123
void register_connection_failed_callback(const std::function< void(ConnectionFailedReason)> &callback)
register a callback that is called when the websocket could not connect with a specific reason
Definition: websocket_base.cpp:56
void register_connected_callback(const std::function< void(OcppProtocolVersion protocol)> &callback)
register a callback that is called when the websocket is connected successfully
Definition: websocket_base.cpp:39
void set_authorization_key(const std::string &authorization_key)
set the authorization_key of the connection_options
Definition: websocket_base.cpp:172
long get_reconnect_interval()
Calculates and returns the reconnect interval based on int retry_backoff_random_range_s,...
Definition: websocket_base.cpp:132
void register_stopped_connecting_callback(const std::function< void(const WebsocketCloseReason reason)> &callback)
register a callback that is called when the websocket connection has been closed and will not attempt...
Definition: websocket_base.cpp:47
bool initialized()
Indicates if the required callbacks are registered.
Definition: websocket_base.cpp:60
void disconnect(const WebsocketCloseReason code)
disconnect the websocket
Definition: websocket_base.cpp:77
void register_message_callback(const std::function< void(const std::string &message)> &callback)
register a callback that is called when the websocket receives a message
Definition: websocket_base.cpp:52
virtual void set_connection_options(const WebsocketConnectionOptions &connection_options)=0
sets this connection_options to the given connection_options and resets the connection_attempts
WebsocketBase()
Creates a new WebsocketBase object. The connection_options must be initialised with set_connection_op...
Definition: websocket_base.cpp:10
virtual void reconnect(long delay)=0
reconnect the websocket after the delay
std::optional< std::string > getAuthorizationHeader()
getter for authorization header for connection with basic authentication
Definition: websocket_base.cpp:106
virtual bool send(const std::string &message)=0
send a message over the websocket
void register_disconnected_callback(const std::function< void()> &callback)
register a callback that is called when the websocket connection is disconnected
Definition: websocket_base.cpp:43
virtual void ping()=0
send a websocket ping
void on_pong_timeout(std::string msg)
Called when a websocket pong timeout is received.
Definition: websocket_base.cpp:176
void set_websocket_ping_interval(int32_t interval_s)
starts a timer that sends a websocket ping at the given interval_s
Definition: websocket_base.cpp:162
virtual bool start_connecting()=0
Starts the connection attempts. It will init the websocket processing thread.
virtual void close(const WebsocketCloseReason code, const std::string &reason)=0
closes the websocket
bool is_connected()
indicates if the websocket is connected
Definition: websocket_base.cpp:102
Definition: websocket_base.hpp:18
bool enable_tls_keylog
If set to true enables logging of TLS secrets to the keylog_file.
Definition: websocket_base.hpp:39
std::optional< std::filesystem::path > keylog_file
Optional path to a keylog file.
Definition: websocket_base.hpp:40