ocpp 0.24.1
A C++ implementation of the Open Charge Point Protocol
websocket_libwebsockets.hpp
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest
3#ifndef OCPP_WEBSOCKET_TLS_TPM_HPP
4#define OCPP_WEBSOCKET_TLS_TPM_HPP
5
6#include <ocpp/common/evse_security.hpp>
7#include <ocpp/common/safe_queue.hpp>
8#include <ocpp/common/websocket/websocket_base.hpp>
9
10#include <memory>
11#include <optional>
12#include <string>
13
14struct ssl_ctx_st;
15
16namespace ocpp {
17
18struct ConnectionData;
19struct WebsocketMessage;
20
23public:
25 explicit WebsocketLibwebsockets(const WebsocketConnectionOptions& connection_options,
26 std::shared_ptr<EvseSecurity> evse_security);
27
29
30 void set_connection_options(const WebsocketConnectionOptions& connection_options) override;
31
32 bool start_connecting() override;
33
34 void reconnect(long delay) override;
35
36 void close(const WebsocketCloseReason code, const std::string& reason) override;
37
38 bool send(const std::string& message) override;
39
40 void ping() override;
41
46
47public:
48 int process_callback(void* wsi_ptr, int callback_reason, void* user, void* in, size_t len);
49
50private:
51 bool is_trying_to_connect_internal();
52 void close_internal(const WebsocketCloseReason code, const std::string& reason);
53
54private:
57 bool initialize_connection_options(std::shared_ptr<ConnectionData>& new_connection_data);
58
59 bool tls_init(struct ssl_ctx_st* ctx, const std::string& path_chain, const std::string& path_key, bool custom_key,
60 std::optional<std::string>& password);
61
63 void thread_websocket_client_loop(std::shared_ptr<ConnectionData> local_data);
64
67 void thread_websocket_message_recv_loop(std::shared_ptr<ConnectionData> local_data);
68
70 void thread_deferred_callback_queue();
71
73 void on_conn_connected(ConnectionData* conn_data);
74
76 void on_conn_close(ConnectionData* conn_data);
77
79 void on_conn_fail(ConnectionData* conn_data);
80
82 void on_conn_writable();
83
85 void on_conn_message(std::string&& message);
86
88 void request_write();
89
90 void poll_message(const std::shared_ptr<WebsocketMessage>& msg);
91
93 void push_deferred_callback(const std::function<void()>& callback);
94
95 // \brief Safely closes the already running connection threads
96 void safe_close_threads();
97
99 void clear_all_queues();
100
101private:
102 std::shared_ptr<EvseSecurity> evse_security;
103
104 // Connection related data
105 Everest::SteadyTimer reconnect_timer_tpm;
106 std::unique_ptr<std::thread> websocket_thread;
107 std::shared_ptr<ConnectionData> conn_data;
108
109 // Queue of outgoing messages, notify thread only when we remove messages
111
112 std::unique_ptr<std::thread> recv_message_thread;
113 SafeQueue<std::string> recv_message_queue;
114 std::string recv_buffered_message;
115
116 std::unique_ptr<std::thread> deferred_callback_thread;
117 SafeQueue<std::function<void()>> deferred_callback_queue;
118 std::atomic_bool stop_deferred_handler;
119
120 OcppProtocolVersion connected_ocpp_version;
121};
122
123} // namespace ocpp
124#endif // OCPP_WEBSOCKET_HPP
Thread safe message queue. Holds a conditional variable that can be waited upon. Will take up the wai...
Definition: safe_queue.hpp:15
contains a websocket abstraction
Definition: websocket_base.hpp:46
Experimental libwebsockets TLS connection.
Definition: websocket_libwebsockets.hpp:22
WebsocketLibwebsockets(const WebsocketConnectionOptions &connection_options, std::shared_ptr< EvseSecurity > evse_security)
Creates a new Websocket object with the providede connection_options.
Definition: websocket_libwebsockets.cpp:339
bool start_connecting() override
Starts the connection attempts. It will init the websocket processing thread.
Definition: websocket_libwebsockets.cpp:943
void reconnect(long delay) override
reconnect the websocket after the delay
Definition: websocket_libwebsockets.cpp:1029
bool is_trying_to_connect()
Indicates if the websocket has a valid connection data and is trying to connect/reconnect internally ...
Definition: websocket_libwebsockets.cpp:932
bool send(const std::string &message) override
send a message over the websocket
Definition: websocket_libwebsockets.cpp:1158
void close(const WebsocketCloseReason code, const std::string &reason) override
closes the websocket
Definition: websocket_libwebsockets.cpp:1000
void set_connection_options(const WebsocketConnectionOptions &connection_options) override
sets this connection_options to the given connection_options and resets the connection_attempts
Definition: websocket_libwebsockets.cpp:378
void ping() override
send a websocket ping
Definition: websocket_libwebsockets.cpp:1173
Current connection data, sets the internal state of the.
Definition: websocket_libwebsockets.cpp:81
Definition: websocket_base.hpp:18