blob: 1966f63bcfae65b43a030aadf3f89a277db8f481 [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>
Patrick Williams0c2432d2023-08-23 15:50:34 -05008#include <xyz/openbmc_project/State/Host/server.hpp>
Carol Wang71230ef2020-02-18 17:39:49 +08009#include <xyz/openbmc_project/State/ScheduledHostTransition/server.hpp>
10
11namespace phosphor
12{
13namespace state
14{
15namespace manager
16{
17
Pavithra Barithaya31584d22024-06-20 09:12:03 -050018class TestScheduledHostTransition;
19
Carol Wang6a5db3d2020-02-21 10:12:01 +080020using Transition =
Patrick Williams7e969cb2023-08-23 16:24:23 -050021 sdbusplus::server::xyz::openbmc_project::state::Host::Transition;
Patrick Williamsf053e6f2022-07-22 19:26:54 -050022using ScheduledHostTransitionInherit = sdbusplus::server::object_t<
Patrick Williams7e969cb2023-08-23 16:24:23 -050023 sdbusplus::server::xyz::openbmc_project::state::ScheduledHostTransition>;
Carol Wang71230ef2020-02-18 17:39:49 +080024
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:
Patrick Williamsf053e6f2022-07-22 19:26:54 -050033 ScheduledHostTransition(sdbusplus::bus_t& bus, const char* objPath,
Patrick Williams211d9722022-04-07 21:24:33 -050034 size_t id, const sdeventplus::Event& event) :
Patrick Williams76070742022-04-05 15:20:12 -050035 ScheduledHostTransitionInherit(
36 bus, objPath, ScheduledHostTransition::action::defer_emit),
Patrick Williams211d9722022-04-07 21:24:33 -050037 bus(bus), id(id), event(event),
William A. Kennington IIIbd28f022022-11-22 17:11:49 -080038 timer(event, [this](auto&) { callback(); })
Carol Wang71230ef2020-02-18 17:39:49 +080039 {
Carol Wangef7abe12020-02-25 16:19:34 +080040 initialize();
Carol Wang1dbbef42020-03-09 11:51:23 +080041
42 restoreScheduledValues();
43
44 // We deferred this until we could get our property correct
45 this->emit_object_added();
Carol Wang71230ef2020-02-18 17:39:49 +080046 }
47
Pavithra Barithaya44acab72024-06-21 08:11:13 -050048 ~ScheduledHostTransition() override;
Carol Wang71230ef2020-02-18 17:39:49 +080049
50 /**
51 * @brief Handle with scheduled time
52 *
53 * @param[in] value - The seconds since epoch
54 * @return The time for the transition. It is the same as the input value if
55 * it is set successfully. Otherwise, it won't return value, but throw an
56 * error.
57 **/
58 uint64_t scheduledTime(uint64_t value) override;
Carol Wang4ca6f3f2020-02-19 16:28:59 +080059
60 private:
61 friend class TestScheduledHostTransition;
Carol Wang6a5db3d2020-02-21 10:12:01 +080062
63 /** @brief sdbusplus bus client connection */
Patrick Williamsf053e6f2022-07-22 19:26:54 -050064 sdbusplus::bus_t& bus;
Carol Wang6a5db3d2020-02-21 10:12:01 +080065
Patrick Williams211d9722022-04-07 21:24:33 -050066 /** @brief Host id. **/
67 const size_t id = 0;
68
Carol Wangef7abe12020-02-25 16:19:34 +080069 /** @brief sdbusplus event */
70 const sdeventplus::Event& event;
71
Carol Wang6a5db3d2020-02-21 10:12:01 +080072 /** @brief Timer used for host transition with seconds */
73 sdeventplus::utility::Timer<sdeventplus::ClockId::RealTime> timer;
74
Carol Wangef7abe12020-02-25 16:19:34 +080075 /** @brief The fd for time change event */
76 int timeFd = -1;
77
Carol Wang4ca6f3f2020-02-19 16:28:59 +080078 /** @brief Get current time
79 *
80 * @return - return current epoch time
81 */
Pavithra Barithayaa1c0e5c2024-06-21 12:39:38 -050082 static std::chrono::seconds getTime();
Carol Wang6a5db3d2020-02-21 10:12:01 +080083
84 /** @brief Implement host transition
85 *
86 * @return - Does not return anything. Error will result in exception
87 * being thrown
88 */
89 void hostTransition();
90
91 /** @brief Used by the timer to do host transition */
92 void callback();
Carol Wangef7abe12020-02-25 16:19:34 +080093
94 /** @brief Initialize timerFd related resource */
95 void initialize();
96
97 /** @brief The callback function on system time change
98 *
99 * @param[in] es - Source of the event
100 * @param[in] fd - File descriptor of the timer
101 * @param[in] revents - Not used
102 * @param[in] userdata - User data pointer
103 */
104 static int onTimeChange(sd_event_source* es, int fd, uint32_t revents,
105 void* userdata);
106
107 /** @brief The deleter of sd_event_source */
108 std::function<void(sd_event_source*)> sdEventSourceDeleter =
109 [](sd_event_source* p) {
Patrick Williams1b2c3c02024-08-16 15:20:29 -0400110 if (p != nullptr)
111 {
112 sd_event_source_unref(p);
113 }
114 };
Carol Wangef7abe12020-02-25 16:19:34 +0800115
116 using SdEventSource =
117 std::unique_ptr<sd_event_source, decltype(sdEventSourceDeleter)>;
118
119 /** @brief The event source on system time change */
120 SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter};
121
122 /** @brief Handle with the process when bmc time is changed*/
123 void handleTimeUpdates();
Carol Wang1dbbef42020-03-09 11:51:23 +0800124
125 /** @brief Serialize the scheduled values */
126 void serializeScheduledValues();
127
128 /** @brief Deserialize the scheduled values
129 *
130 * @param[out] time - Deserialized scheduled time
131 * @param[out] trans - Deserialized requested transition
132 *
133 * @return bool - true if successful, false otherwise
134 */
Pavithra Barithayaa1c0e5c2024-06-21 12:39:38 -0500135 static bool deserializeScheduledValues(uint64_t& time, Transition& trans);
Carol Wang1dbbef42020-03-09 11:51:23 +0800136
137 /** @brief Restore scheduled time and requested transition from persisted
138 * file */
139 void restoreScheduledValues();
Carol Wang71230ef2020-02-18 17:39:49 +0800140};
141} // namespace manager
142} // namespace state
Carol Wang4ca6f3f2020-02-19 16:28:59 +0800143} // namespace phosphor