blob: 4b75ad3d3eafb50b512663651198d9eaccbe03a4 [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
Carol Wang71230ef2020-02-18 17:39:49 +08005#include <phosphor-logging/log.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -05006#include <sdbusplus/bus.hpp>
Carol Wang6a5db3d2020-02-21 10:12:01 +08007#include <sdeventplus/event.hpp>
8#include <sdeventplus/utility/timer.hpp>
Carol Wang71230ef2020-02-18 17:39:49 +08009#include <xyz/openbmc_project/State/ScheduledHostTransition/server.hpp>
10
Carol Wang4ca6f3f2020-02-19 16:28:59 +080011class TestScheduledHostTransition;
12
Carol Wang71230ef2020-02-18 17:39:49 +080013namespace phosphor
14{
15namespace state
16{
17namespace manager
18{
19
Carol Wang6a5db3d2020-02-21 10:12:01 +080020using Transition =
21 sdbusplus::xyz::openbmc_project::State::server::Host::Transition;
Carol Wang71230ef2020-02-18 17:39:49 +080022using ScheduledHostTransitionInherit = sdbusplus::server::object::object<
23 sdbusplus::xyz::openbmc_project::State::server::ScheduledHostTransition>;
24
25/** @class ScheduledHostTransition
26 * @brief Scheduled host transition implementation.
27 * @details A concrete implementation for
28 * xyz.openbmc_project.State.ScheduledHostTransition
29 */
30class ScheduledHostTransition : public ScheduledHostTransitionInherit
31{
32 public:
Carol Wang6a5db3d2020-02-21 10:12:01 +080033 ScheduledHostTransition(sdbusplus::bus::bus& bus, const char* objPath,
34 const sdeventplus::Event& event) :
Carol Wang1dbbef42020-03-09 11:51:23 +080035 ScheduledHostTransitionInherit(bus, objPath, true),
Carol Wangef7abe12020-02-25 16:19:34 +080036 bus(bus), event(event),
Carol Wang6a5db3d2020-02-21 10:12:01 +080037 timer(event, std::bind(&ScheduledHostTransition::callback, this))
Carol Wang71230ef2020-02-18 17:39:49 +080038 {
Carol Wangef7abe12020-02-25 16:19:34 +080039 initialize();
Carol Wang1dbbef42020-03-09 11:51:23 +080040
41 restoreScheduledValues();
42
43 // We deferred this until we could get our property correct
44 this->emit_object_added();
Carol Wang71230ef2020-02-18 17:39:49 +080045 }
46
Carol Wangef7abe12020-02-25 16:19:34 +080047 ~ScheduledHostTransition();
Carol Wang71230ef2020-02-18 17:39:49 +080048
49 /**
50 * @brief Handle with scheduled time
51 *
52 * @param[in] value - The seconds since epoch
53 * @return The time for the transition. It is the same as the input value if
54 * it is set successfully. Otherwise, it won't return value, but throw an
55 * error.
56 **/
57 uint64_t scheduledTime(uint64_t value) override;
Carol Wang4ca6f3f2020-02-19 16:28:59 +080058
59 private:
60 friend class TestScheduledHostTransition;
Carol Wang6a5db3d2020-02-21 10:12:01 +080061
62 /** @brief sdbusplus bus client connection */
63 sdbusplus::bus::bus& bus;
64
Carol Wangef7abe12020-02-25 16:19:34 +080065 /** @brief sdbusplus event */
66 const sdeventplus::Event& event;
67
Carol Wang6a5db3d2020-02-21 10:12:01 +080068 /** @brief Timer used for host transition with seconds */
69 sdeventplus::utility::Timer<sdeventplus::ClockId::RealTime> timer;
70
Carol Wangef7abe12020-02-25 16:19:34 +080071 /** @brief The fd for time change event */
72 int timeFd = -1;
73
Carol Wang4ca6f3f2020-02-19 16:28:59 +080074 /** @brief Get current time
75 *
76 * @return - return current epoch time
77 */
78 std::chrono::seconds getTime();
Carol Wang6a5db3d2020-02-21 10:12:01 +080079
80 /** @brief Implement host transition
81 *
82 * @return - Does not return anything. Error will result in exception
83 * being thrown
84 */
85 void hostTransition();
86
87 /** @brief Used by the timer to do host transition */
88 void callback();
Carol Wangef7abe12020-02-25 16:19:34 +080089
90 /** @brief Initialize timerFd related resource */
91 void initialize();
92
93 /** @brief The callback function on system time change
94 *
95 * @param[in] es - Source of the event
96 * @param[in] fd - File descriptor of the timer
97 * @param[in] revents - Not used
98 * @param[in] userdata - User data pointer
99 */
100 static int onTimeChange(sd_event_source* es, int fd, uint32_t revents,
101 void* userdata);
102
103 /** @brief The deleter of sd_event_source */
104 std::function<void(sd_event_source*)> sdEventSourceDeleter =
105 [](sd_event_source* p) {
106 if (p)
107 {
108 sd_event_source_unref(p);
109 }
110 };
111
112 using SdEventSource =
113 std::unique_ptr<sd_event_source, decltype(sdEventSourceDeleter)>;
114
115 /** @brief The event source on system time change */
116 SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter};
117
118 /** @brief Handle with the process when bmc time is changed*/
119 void handleTimeUpdates();
Carol Wang1dbbef42020-03-09 11:51:23 +0800120
121 /** @brief Serialize the scheduled values */
122 void serializeScheduledValues();
123
124 /** @brief Deserialize the scheduled values
125 *
126 * @param[out] time - Deserialized scheduled time
127 * @param[out] trans - Deserialized requested transition
128 *
129 * @return bool - true if successful, false otherwise
130 */
131 bool deserializeScheduledValues(uint64_t& time, Transition& trans);
132
133 /** @brief Restore scheduled time and requested transition from persisted
134 * file */
135 void restoreScheduledValues();
Carol Wang71230ef2020-02-18 17:39:49 +0800136};
137} // namespace manager
138} // namespace state
Carol Wang4ca6f3f2020-02-19 16:28:59 +0800139} // namespace phosphor