ocpp 0.24.1
A C++ implementation of the Open Charge Point Protocol
ocsp_updater.hpp
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest
3
4#ifndef OCPP_OCSP_UPDATER_HPP
5#define OCPP_OCSP_UPDATER_HPP
6
7#include <chrono>
8#include <condition_variable>
9#include <mutex>
10#include <stdexcept>
11#include <thread>
12
13#include <ocpp/common/call_types.hpp>
14#include <ocpp/common/evse_security.hpp>
15#include <ocpp/v2/messages/GetCertificateStatus.hpp>
16
17namespace ocpp::v2 {
18
19class OcspUpdateFailedException : public std::exception {
20public:
21 [[nodiscard]] const char* what() const noexcept override {
22 return this->reason.c_str();
23 }
24
25 explicit OcspUpdateFailedException(std::string reason, bool can_be_retried) :
26 reason(std::move(reason)), _allows_retry(can_be_retried) {
27 }
28
29 [[nodiscard]] bool allows_retry() const {
30 return _allows_retry;
31 }
32
33private:
34 std::string reason;
35 const bool _allows_retry;
36};
37
38typedef std::function<GetCertificateStatusResponse(GetCertificateStatusRequest)> cert_status_func;
39
40// Forward declarations to avoid include loops
41class ChargePoint;
43
45public:
46 virtual ~OcspUpdaterInterface() {
47 }
48 virtual void start() = 0;
49 virtual void stop() = 0;
50 // Wake up the updater thread and tell it to update
51 // Used e.g. when a new charging station cert was just installed
52 virtual void trigger_ocsp_cache_update() = 0;
53};
54
56public:
57 OcspUpdater() = delete;
58 OcspUpdater(std::shared_ptr<EvseSecurity> evse_security, cert_status_func get_cert_status_from_csms,
59 std::chrono::seconds ocsp_cache_update_interval = std::chrono::hours(167),
60 std::chrono::seconds ocsp_cache_update_retry_interval = std::chrono::hours(24));
61 virtual ~OcspUpdater() {
62 }
63
64 void start() override;
65 void stop() override;
66
67 void trigger_ocsp_cache_update() override;
68
69private:
70 // Updater thread responsible for executing the updates
71 std::thread updater_thread;
72
73 // This mutex guards access to everything below it, INCLUDING explicit_update_trigger
74 // - The updater thread always holds the lock, except when it's waiting on explicit_update_trigger
75 // - The lib needs to hold the lock to notify the explicit_update_trigger (this guarantees it wakes up the worker)
76 std::mutex update_ocsp_cache_lock;
77 // Condition variable used to wake up the updater thread
78 std::condition_variable explicit_update_trigger;
79 // Deadline by which libocpp must automatically trigger an OCSP cache update
80 std::chrono::time_point<std::chrono::steady_clock> update_deadline;
81 std::shared_ptr<EvseSecurity> evse_security;
82 // Set this when starting and stopping the updater thread
83 bool running;
84
85 // This function captures a pointer to a ChargePoint, which has to remain valid.
86 // The OcspUpdater class is part of the ChargePoint, and thus it cannot outlive the ChargePoint.
87 cert_status_func get_cert_status_from_csms;
88
89 // Timing constants
90 const std::chrono::seconds ocsp_cache_update_interval;
91 const std::chrono::seconds ocsp_cache_update_retry_interval;
92
93 // Running loop of the OCSP updater thread
94 void updater_thread_loop();
95 // Helper method, only called within updater_thread_loop().
96 void execute_ocsp_update();
97};
98
99} // namespace ocpp::v2
100
101#endif // OCPP_OCSP_UPDATER_HPP
Class implements OCPP2.0.1 Charging Station.
Definition: charge_point.hpp:334
Definition: ocsp_updater.hpp:19
Definition: ocsp_updater.hpp:44
Definition: ocsp_updater.hpp:55
Definition: charge_point.hpp:54
Contains a OCPP GetCertificateStatus message.
Definition: GetCertificateStatus.hpp:19
Contains a OCPP GetCertificateStatusResponse message.
Definition: GetCertificateStatus.hpp:39