-
Notifications
You must be signed in to change notification settings - Fork 22
/
scheduled_host_transition.hpp
143 lines (115 loc) · 4.2 KB
/
scheduled_host_transition.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#pragma once
#include "config.h"
#include <sdbusplus/bus.hpp>
#include <sdeventplus/event.hpp>
#include <sdeventplus/utility/timer.hpp>
#include <xyz/openbmc_project/State/Host/server.hpp>
#include <xyz/openbmc_project/State/ScheduledHostTransition/server.hpp>
namespace phosphor
{
namespace state
{
namespace manager
{
class TestScheduledHostTransition;
using Transition =
sdbusplus::server::xyz::openbmc_project::state::Host::Transition;
using ScheduledHostTransitionInherit = sdbusplus::server::object_t<
sdbusplus::server::xyz::openbmc_project::state::ScheduledHostTransition>;
/** @class ScheduledHostTransition
* @brief Scheduled host transition implementation.
* @details A concrete implementation for
* xyz.openbmc_project.State.ScheduledHostTransition
*/
class ScheduledHostTransition : public ScheduledHostTransitionInherit
{
public:
ScheduledHostTransition(sdbusplus::bus_t& bus, const char* objPath,
size_t id, const sdeventplus::Event& event) :
ScheduledHostTransitionInherit(
bus, objPath, ScheduledHostTransition::action::defer_emit),
bus(bus), id(id), event(event),
timer(event, [this](auto&) { callback(); })
{
initialize();
restoreScheduledValues();
// We deferred this until we could get our property correct
this->emit_object_added();
}
~ScheduledHostTransition() override;
/**
* @brief Handle with scheduled time
*
* @param[in] value - The seconds since epoch
* @return The time for the transition. It is the same as the input value if
* it is set successfully. Otherwise, it won't return value, but throw an
* error.
**/
uint64_t scheduledTime(uint64_t value) override;
private:
friend class TestScheduledHostTransition;
/** @brief sdbusplus bus client connection */
sdbusplus::bus_t& bus;
/** @brief Host id. **/
const size_t id = 0;
/** @brief sdbusplus event */
const sdeventplus::Event& event;
/** @brief Timer used for host transition with seconds */
sdeventplus::utility::Timer<sdeventplus::ClockId::RealTime> timer;
/** @brief The fd for time change event */
int timeFd = -1;
/** @brief Get current time
*
* @return - return current epoch time
*/
static std::chrono::seconds getTime();
/** @brief Implement host transition
*
* @return - Does not return anything. Error will result in exception
* being thrown
*/
void hostTransition();
/** @brief Used by the timer to do host transition */
void callback();
/** @brief Initialize timerFd related resource */
void initialize();
/** @brief The callback function on system time change
*
* @param[in] es - Source of the event
* @param[in] fd - File descriptor of the timer
* @param[in] revents - Not used
* @param[in] userdata - User data pointer
*/
static int onTimeChange(sd_event_source* es, int fd, uint32_t revents,
void* userdata);
/** @brief The deleter of sd_event_source */
std::function<void(sd_event_source*)> sdEventSourceDeleter =
[](sd_event_source* p) {
if (p != nullptr)
{
sd_event_source_unref(p);
}
};
using SdEventSource =
std::unique_ptr<sd_event_source, decltype(sdEventSourceDeleter)>;
/** @brief The event source on system time change */
SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter};
/** @brief Handle with the process when bmc time is changed*/
void handleTimeUpdates();
/** @brief Serialize the scheduled values */
void serializeScheduledValues();
/** @brief Deserialize the scheduled values
*
* @param[out] time - Deserialized scheduled time
* @param[out] trans - Deserialized requested transition
*
* @return bool - true if successful, false otherwise
*/
static bool deserializeScheduledValues(uint64_t& time, Transition& trans);
/** @brief Restore scheduled time and requested transition from persisted
* file */
void restoreScheduledValues();
};
} // namespace manager
} // namespace state
} // namespace phosphor