ocpp 0.24.1
A C++ implementation of the Open Charge Point Protocol
connectivity_manager.hpp
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2020 - 2024 Pionix GmbH and Contributors to EVerest
3
4#pragma once
5
6#include <ocpp/common/websocket/websocket.hpp>
7#include <ocpp/v2/messages/SetNetworkProfile.hpp>
8#include <ocpp/v2/ocpp_types.hpp>
9
10#include <functional>
11#include <future>
12#include <optional>
13namespace ocpp {
14namespace v2 {
15
16class DeviceModel;
17
18using WebsocketConnectionCallback = std::function<void(
19 int configuration_slot, const NetworkConnectionProfile& network_connection_profile, OcppProtocolVersion version)>;
20using WebsocketConnectionFailedCallback = std::function<void(ConnectionFailedReason reason)>;
21using ConfigureNetworkConnectionProfileCallback = std::function<std::future<ConfigNetworkResult>(
22 const int32_t configuration_slot, const NetworkConnectionProfile& network_connection_profile)>;
23
25public:
27 }
30 virtual void set_websocket_authorization_key(const std::string& authorization_key) = 0;
31
34 virtual void set_websocket_connection_options(const WebsocketConnectionOptions& connection_options) = 0;
35
39
42 virtual void set_websocket_connected_callback(WebsocketConnectionCallback callback) = 0;
43
46 virtual void set_websocket_disconnected_callback(WebsocketConnectionCallback callback) = 0;
47
50 virtual void set_websocket_connection_failed_callback(WebsocketConnectionFailedCallback callback) = 0;
51
54 virtual void
55 set_configure_network_connection_profile_callback(ConfigureNetworkConnectionProfileCallback callback) = 0;
56
60 virtual std::optional<NetworkConnectionProfile>
61 get_network_connection_profile(const int32_t configuration_slot) const = 0;
62
67 virtual std::optional<int32_t> get_priority_from_configuration_slot(const int configuration_slot) const = 0;
68
74 virtual const std::vector<int>& get_network_connection_slots() const = 0;
75
79 virtual bool is_websocket_connected() = 0;
80
85 virtual void connect(std::optional<int32_t> network_profile_slot = std::nullopt) = 0;
86
89 virtual void disconnect() = 0;
90
94 virtual bool send_to_websocket(const std::string& message) = 0;
95
104 virtual void on_network_disconnected(OCPPInterfaceEnum ocpp_interface) = 0;
105
109
112};
113
115private:
117 DeviceModel& device_model;
119 std::shared_ptr<EvseSecurity> evse_security;
121 std::shared_ptr<MessageLogging> logging;
123 std::unique_ptr<Websocket> websocket;
125 std::function<void(const std::string& message)> message_callback;
127 std::optional<WebsocketConnectionCallback> websocket_connected_callback;
129 std::optional<WebsocketConnectionCallback> websocket_disconnected_callback;
131 std::optional<WebsocketConnectionFailedCallback> websocket_connection_failed_callback;
133 std::optional<ConfigureNetworkConnectionProfileCallback> configure_network_connection_profile_callback;
134
135 Everest::SteadyTimer websocket_timer;
136 std::optional<int32_t> pending_configuration_slot;
137 bool wants_to_be_connected;
138 int32_t active_network_configuration_priority;
139 int last_known_security_level;
141 std::vector<SetNetworkProfileRequest> cached_network_connection_profiles;
143 std::vector<int32_t> network_connection_slots;
144 OcppProtocolVersion connected_ocpp_version;
145
146public:
147 ConnectivityManager(DeviceModel& device_model, std::shared_ptr<EvseSecurity> evse_security,
148 std::shared_ptr<MessageLogging> logging,
149 const std::function<void(const std::string& message)>& message_callback);
150
151 void set_websocket_authorization_key(const std::string& authorization_key) override;
152 void set_websocket_connection_options(const WebsocketConnectionOptions& connection_options) override;
154 void set_websocket_connected_callback(WebsocketConnectionCallback callback) override;
155 void set_websocket_disconnected_callback(WebsocketConnectionCallback callback) override;
156 void set_websocket_connection_failed_callback(WebsocketConnectionFailedCallback callback) override;
157 void set_configure_network_connection_profile_callback(ConfigureNetworkConnectionProfileCallback callback) override;
158 std::optional<NetworkConnectionProfile>
159 get_network_connection_profile(const int32_t configuration_slot) const override;
160 std::optional<int32_t> get_priority_from_configuration_slot(const int configuration_slot) const override;
161 const std::vector<int>& get_network_connection_slots() const override;
162 bool is_websocket_connected() override;
163 void connect(std::optional<int32_t> network_profile_slot = std::nullopt) override;
164 void disconnect() override;
165 bool send_to_websocket(const std::string& message) override;
166 void on_network_disconnected(OCPPInterfaceEnum ocpp_interface) override;
168 void confirm_successful_connection() override;
169
170private:
173 void try_connect_websocket();
174
178 std::optional<WebsocketConnectionOptions> get_ws_connection_options(const int32_t configuration_slot);
179
186 std::optional<ConfigNetworkResult>
187 handle_configure_network_connection_profile_callback(int slot, const NetworkConnectionProfile& profile);
188
191 void on_websocket_connected(OcppProtocolVersion protocol);
192
195 void on_websocket_disconnected();
196
200 void on_websocket_stopped_connecting(ocpp::WebsocketCloseReason reason);
201
206 int get_active_network_configuration_slot() const;
207
213 int get_configuration_slot_from_priority(const int priority);
214
220 int get_next_configuration_slot(int32_t configuration_slot);
221
223 void cache_network_connection_profiles();
224
227 void check_cache_for_invalid_security_profiles();
228
231 void remove_network_connection_profiles_below_actual_security_profile();
232};
233
234} // namespace v2
235} // namespace ocpp
Definition: connectivity_manager.hpp:24
virtual const std::vector< int > & get_network_connection_slots() const =0
Get the network connection slots sorted by priority. Each item in the vector contains the configured ...
virtual std::optional< int32_t > get_priority_from_configuration_slot(const int configuration_slot) const =0
Get the priority of the given configuration slot.
virtual void set_websocket_authorization_key(const std::string &authorization_key)=0
Set the websocket authorization_key.
virtual bool is_websocket_connected()=0
Check if the websocket is connected.
virtual void set_websocket_connected_callback(WebsocketConnectionCallback callback)=0
Set the callback that is called when the websocket is connected.
virtual void set_websocket_connection_options_without_reconnect()=0
Set the websocket connection options without triggering a reconnect.
virtual void on_charging_station_certificate_changed()=0
Called when the charging station certificate is changed.
virtual std::optional< NetworkConnectionProfile > get_network_connection_profile(const int32_t configuration_slot) const =0
Gets the cached NetworkConnectionProfile based on the given configuration_slot. This returns the valu...
virtual void disconnect()=0
Disconnect the websocket.
virtual void connect(std::optional< int32_t > network_profile_slot=std::nullopt)=0
Connect to the websocket.
virtual void confirm_successful_connection()=0
Confirms the connection is successful so the security profile requirements can be handled.
virtual void set_websocket_disconnected_callback(WebsocketConnectionCallback callback)=0
Set the callback that is called when the websocket is disconnected.
virtual bool send_to_websocket(const std::string &message)=0
send a message over the websocket
virtual void set_websocket_connection_failed_callback(WebsocketConnectionFailedCallback callback)=0
Set the callback that is called when the websocket could not connect with a specific reason.
virtual void set_websocket_connection_options(const WebsocketConnectionOptions &connection_options)=0
Set the websocket connection_options.
virtual void set_configure_network_connection_profile_callback(ConfigureNetworkConnectionProfileCallback callback)=0
Set the callback that is called to configure a network connection profile when none is configured.
virtual void on_network_disconnected(OCPPInterfaceEnum ocpp_interface)=0
Can be called when a network is disconnected, for example when an ethernet cable is removed.
Definition: connectivity_manager.hpp:114
std::optional< int32_t > get_priority_from_configuration_slot(const int configuration_slot) const override
Get the priority of the given configuration slot.
Definition: connectivity_manager.cpp:94
void set_websocket_authorization_key(const std::string &authorization_key) override
Set the websocket authorization_key.
Definition: connectivity_manager.cpp:36
void on_charging_station_certificate_changed() override
Called when the charging station certificate is changed.
Definition: connectivity_manager.cpp:315
const std::vector< int > & get_network_connection_slots() const override
Get the network connection slots sorted by priority. Each item in the vector contains the configured ...
Definition: connectivity_manager.cpp:112
void confirm_successful_connection() override
Confirms the connection is successful so the security profile requirements can be handled.
Definition: connectivity_manager.cpp:151
void set_websocket_connection_failed_callback(WebsocketConnectionFailedCallback callback) override
Set the callback that is called when the websocket could not connect with a specific reason.
Definition: connectivity_manager.cpp:65
bool send_to_websocket(const std::string &message) override
send a message over the websocket
Definition: connectivity_manager.cpp:292
void set_websocket_connected_callback(WebsocketConnectionCallback callback) override
Set the callback that is called when the websocket is connected.
Definition: connectivity_manager.cpp:57
std::optional< NetworkConnectionProfile > get_network_connection_profile(const int32_t configuration_slot) const override
Gets the cached NetworkConnectionProfile based on the given configuration_slot. This returns the valu...
Definition: connectivity_manager.cpp:75
void set_websocket_connection_options_without_reconnect() override
Set the websocket connection options without triggering a reconnect.
Definition: connectivity_manager.cpp:49
void connect(std::optional< int32_t > network_profile_slot=std::nullopt) override
Connect to the websocket.
Definition: connectivity_manager.cpp:120
bool is_websocket_connected() override
Check if the websocket is connected.
Definition: connectivity_manager.cpp:116
void set_websocket_connection_options(const WebsocketConnectionOptions &connection_options) override
Set the websocket connection_options.
Definition: connectivity_manager.cpp:43
void set_websocket_disconnected_callback(WebsocketConnectionCallback callback) override
Set the callback that is called when the websocket is disconnected.
Definition: connectivity_manager.cpp:61
void disconnect() override
Disconnect the websocket.
Definition: connectivity_manager.cpp:143
void on_network_disconnected(OCPPInterfaceEnum ocpp_interface) override
Can be called when a network is disconnected, for example when an ethernet cable is removed.
Definition: connectivity_manager.cpp:300
void set_configure_network_connection_profile_callback(ConfigureNetworkConnectionProfileCallback callback) override
Set the callback that is called to configure a network connection profile when none is configured.
Definition: connectivity_manager.cpp:69
This class manages access to the device model representation and to the device model interface and pr...
Definition: device_model.hpp:96
Definition: websocket_base.hpp:18
Definition: ocpp_types.hpp:831