ocpp 0.24.1
A C++ implementation of the Open Charge Point Protocol
websocket.hpp
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest
3#ifndef OCPP_WEBSOCKET_HPP
4#define OCPP_WEBSOCKET_HPP
5
6#include <ocpp/common/evse_security.hpp>
7#include <ocpp/common/ocpp_logging.hpp>
8
9#include <ocpp/common/websocket/websocket_base.hpp>
10
11namespace ocpp {
15class Websocket {
16private:
17 // unique_ptr holds address of base - requires WebSocketBase to have a virtual destructor
18 std::unique_ptr<WebsocketBase> websocket;
19 std::function<void(OcppProtocolVersion protocol)> connected_callback;
20 std::function<void()> disconnected_callback;
21 std::function<void(const WebsocketCloseReason reason)> stopped_connecting_callback;
22 std::function<void(const std::string& message)> message_callback;
23 std::shared_ptr<MessageLogging> logging;
24
25public:
27 explicit Websocket(const WebsocketConnectionOptions& connection_options,
28 std::shared_ptr<EvseSecurity> evse_security, std::shared_ptr<MessageLogging> logging);
29 ~Websocket();
30
34 bool start_connecting();
35
36 void set_connection_options(const WebsocketConnectionOptions& connection_options);
37
39 void disconnect(const WebsocketCloseReason code);
40
41 // \brief reconnects the websocket after the delay
42 void reconnect(long delay);
43
45 bool is_connected();
46
48 void register_connected_callback(const std::function<void(OcppProtocolVersion protocol)>& callback);
49
51 void register_disconnected_callback(const std::function<void()>& callback);
52
55 void register_stopped_connecting_callback(const std::function<void(const WebsocketCloseReason)>& callback);
56
58 void register_message_callback(const std::function<void(const std::string& message)>& callback);
59
61 void register_connection_failed_callback(const std::function<void(ConnectionFailedReason)>& callback);
62
65 bool send(const std::string& message);
66
68 void set_websocket_ping_interval(int32_t interval_s);
69
71 void set_authorization_key(const std::string& authorization_key);
72};
73
74} // namespace ocpp
75#endif // OCPP_WEBSOCKET_HPP
contains a websocket abstraction that can connect to TLS and non-TLS websocket endpoints
Definition: websocket.hpp:15
void set_websocket_ping_interval(int32_t interval_s)
set the websocket ping interval interval_s in seconds
Definition: websocket.cpp:88
void register_stopped_connecting_callback(const std::function< void(const WebsocketCloseReason)> &callback)
register a callback that is called when the websocket connection has been stopped and will not attemp...
Definition: websocket.cpp:66
bool is_connected()
indicates if the websocket is connected
Definition: websocket.cpp:44
Websocket(const WebsocketConnectionOptions &connection_options, std::shared_ptr< EvseSecurity > evse_security, std::shared_ptr< MessageLogging > logging)
Creates a new Websocket object with the provided connection_options.
Definition: websocket.cpp:16
void register_disconnected_callback(const std::function< void()> &callback)
register a callback that is called when the websocket connection is disconnected
Definition: websocket.cpp:57
bool start_connecting()
Starts the connection attempts. It will init the websocket processing thread.
Definition: websocket.cpp:25
bool send(const std::string &message)
send a message over the websocket
Definition: websocket.cpp:83
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.cpp:48
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.cpp:79
void set_authorization_key(const std::string &authorization_key)
set the authorization_key of the connection_options
Definition: websocket.cpp:93
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.cpp:73
void disconnect(const WebsocketCloseReason code)
disconnect the websocket
Definition: websocket.cpp:34
Definition: websocket_base.hpp:18