blob: 3e24f8909b896939b515d84e9993fdc1dbdf965f [file] [log] [blame]
Carol Wang71230ef2020-02-18 17:39:49 +08001#pragma once
2
Andrew Geisslere426b582020-05-28 12:40:55 -05003#include "config.h"
4
Andrew Geisslere426b582020-05-28 12:40:55 -05005#include <sdbusplus/bus.hpp>
Carol Wang6a5db3d2020-02-21 10:12:01 +08006#include <sdeventplus/event.hpp>
7#include <sdeventplus/utility/timer.hpp>
Carol Wang71230ef2020-02-18 17:39:49 +08008#include <xyz/openbmc_project/State/ScheduledHostTransition/server.hpp>
9
Carol Wang4ca6f3f2020-02-19 16:28:59 +080010class TestScheduledHostTransition;
11
Carol Wang71230ef2020-02-18 17:39:49 +080012namespace phosphor
13{
14namespace state
15{
16namespace manager
17{
18
Carol Wang6a5db3d2020-02-21 10:12:01 +080019using Transition =
20 sdbusplus::xyz::openbmc_project::State::server::Host::Transition;
Carol Wang71230ef2020-02-18 17:39:49 +080021using ScheduledHostTransitionInherit = sdbusplus::server::object::object<
22 sdbusplus::xyz::openbmc_project::State::server::ScheduledHostTransition>;
23
24/** @class ScheduledHostTransition
25 * @brief Scheduled host transition implementation.
26 * @details A concrete implementation for
27 * xyz.openbmc_project.State.ScheduledHostTransition
28 */
29class ScheduledHostTransition : public ScheduledHostTransitionInherit
30{
31 public:
Carol Wang6a5db3d2020-02-21 10:12:01 +080032 ScheduledHostTransition(sdbusplus::bus::bus& bus, const char* objPath,
33 const sdeventplus::Event& event) :
Carol Wang1dbbef42020-03-09 11:51:23 +080034 ScheduledHostTransitionInherit(bus, objPath, true),
Carol Wangef7abe12020-02-25 16:19:34 +080035 bus(bus), event(event),
Carol Wang6a5db3d2020-02-21 10:12:01 +080036 timer(event, std::bind(&ScheduledHostTransition::callback, this))
Carol Wang71230ef2020-02-18 17:39:49 +080037 {
Carol Wangef7abe12020-02-25 16:19:34 +080038 initialize();
Carol Wang1dbbef42020-03-09 11:51:23 +080039
40 restoreScheduledValues();
41
42 // We deferred this until we could get our property correct
43 this->emit_object_added();
Carol Wang71230ef2020-02-18 17:39:49 +080044 }
45
Carol Wangef7abe12020-02-25 16:19:34 +080046 ~ScheduledHostTransition();
Carol Wang71230ef2020-02-18 17:39:49 +080047
48 /**
49 * @brief Handle with scheduled time
50 *
51 * @param[in] value - The seconds since epoch
52 * @return The time for the transition. It is the same as the input value if
53 * it is set successfully. Otherwise, it won't return value, but throw an
54 * error.
55 **/
56 uint64_t scheduledTime(uint64_t value) override;
Carol Wang4ca6f3f2020-02-19 16:28:59 +080057
58 private:
59 friend class TestScheduledHostTransition;
Carol Wang6a5db3d2020-02-21 10:12:01 +080060
61 /** @brief sdbusplus bus client connection */
62 sdbusplus::bus::bus& bus;
63
Carol Wangef7abe12020-02-25 16:19:34 +080064 /** @brief sdbusplus event */
65 const sdeventplus::Event& event;
66
Carol Wang6a5db3d2020-02-21 10:12:01 +080067 /** @brief Timer used for host transition with seconds */
68 sdeventplus::utility::Timer<sdeventplus::ClockId::RealTime> timer;
69
Carol Wangef7abe12020-02-25 16:19:34 +080070 /** @brief The fd for time change event */
71 int timeFd = -1;
72
Carol Wang4ca6f3f2020-02-19 16:28:59 +080073 /** @brief Get current time
74 *
75 * @return - return current epoch time
76 */
77 std::chrono::seconds getTime();
Carol Wang6a5db3d2020-02-21 10:12:01 +080078
79 /** @brief Implement host transition
80 *
81 * @return - Does not return anything. Error will result in exception
82 * being thrown
83 */
84 void hostTransition();
85
86 /** @brief Used by the timer to do host transition */
87 void callback();
Carol Wangef7abe12020-02-25 16:19:34 +080088
89 /** @brief Initialize timerFd related resource */
90 void initialize();
91
92 /** @brief The callback function on system time change
93 *
94 * @param[in] es - Source of the event
95 * @param[in] fd - File descriptor of the timer
96 * @param[in] revents - Not used
97 * @param[in] userdata - User data pointer
98 */
99 static int onTimeChange(sd_event_source* es, int fd, uint32_t revents,
100 void* userdata);
101
102 /** @brief The deleter of sd_event_source */
103 std::function<void(sd_event_source*)> sdEventSourceDeleter =
104 [](sd_event_source* p) {
105 if (p)
106 {
107 sd_event_source_unref(p);
108 }
109 };
110
111 using SdEventSource =
112 std::unique_ptr<sd_event_source, decltype(sdEventSourceDeleter)>;
113
114 /** @brief The event source on system time change */
115 SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter};
116
117 /** @brief Handle with the process when bmc time is changed*/
118 void handleTimeUpdates();
Carol Wang1dbbef42020-03-09 11:51:23 +0800119
120 /** @brief Serialize the scheduled values */
121 void serializeScheduledValues();
122
123 /** @brief Deserialize the scheduled values
124 *
125 * @param[out] time - Deserialized scheduled time
126 * @param[out] trans - Deserialized requested transition
127 *
128 * @return bool - true if successful, false otherwise
129 */
130 bool deserializeScheduledValues(uint64_t& time, Transition& trans);
131
132 /** @brief Restore scheduled time and requested transition from persisted
133 * file */
134 void restoreScheduledValues();
Carol Wang71230ef2020-02-18 17:39:49 +0800135};
136} // namespace manager
137} // namespace state
Carol Wang4ca6f3f2020-02-19 16:28:59 +0800138} // namespace phosphor