blob: a17ca3ba21fd1ab37c7600635d7ac2a14dfc7ae9 [file] [log] [blame]
Carol Wang71230ef2020-02-18 17:39:49 +08001#pragma once
2
3#include <sdbusplus/bus.hpp>
4#include <phosphor-logging/log.hpp>
Carol Wang6a5db3d2020-02-21 10:12:01 +08005#include <sdeventplus/event.hpp>
6#include <sdeventplus/utility/timer.hpp>
Carol Wang71230ef2020-02-18 17:39:49 +08007#include <xyz/openbmc_project/State/ScheduledHostTransition/server.hpp>
Carol Wang6a5db3d2020-02-21 10:12:01 +08008#include "config.h"
Carol Wang71230ef2020-02-18 17:39:49 +08009
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) :
34 ScheduledHostTransitionInherit(bus, objPath),
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 Wang71230ef2020-02-18 17:39:49 +080039 }
40
Carol Wangef7abe12020-02-25 16:19:34 +080041 ~ScheduledHostTransition();
Carol Wang71230ef2020-02-18 17:39:49 +080042
43 /**
44 * @brief Handle with scheduled time
45 *
46 * @param[in] value - The seconds since epoch
47 * @return The time for the transition. It is the same as the input value if
48 * it is set successfully. Otherwise, it won't return value, but throw an
49 * error.
50 **/
51 uint64_t scheduledTime(uint64_t value) override;
Carol Wang4ca6f3f2020-02-19 16:28:59 +080052
53 private:
54 friend class TestScheduledHostTransition;
Carol Wang6a5db3d2020-02-21 10:12:01 +080055
56 /** @brief sdbusplus bus client connection */
57 sdbusplus::bus::bus& bus;
58
Carol Wangef7abe12020-02-25 16:19:34 +080059 /** @brief sdbusplus event */
60 const sdeventplus::Event& event;
61
Carol Wang6a5db3d2020-02-21 10:12:01 +080062 /** @brief Timer used for host transition with seconds */
63 sdeventplus::utility::Timer<sdeventplus::ClockId::RealTime> timer;
64
Carol Wangef7abe12020-02-25 16:19:34 +080065 /** @brief The fd for time change event */
66 int timeFd = -1;
67
Carol Wang4ca6f3f2020-02-19 16:28:59 +080068 /** @brief Get current time
69 *
70 * @return - return current epoch time
71 */
72 std::chrono::seconds getTime();
Carol Wang6a5db3d2020-02-21 10:12:01 +080073
74 /** @brief Implement host transition
75 *
76 * @return - Does not return anything. Error will result in exception
77 * being thrown
78 */
79 void hostTransition();
80
81 /** @brief Used by the timer to do host transition */
82 void callback();
Carol Wangef7abe12020-02-25 16:19:34 +080083
84 /** @brief Initialize timerFd related resource */
85 void initialize();
86
87 /** @brief The callback function on system time change
88 *
89 * @param[in] es - Source of the event
90 * @param[in] fd - File descriptor of the timer
91 * @param[in] revents - Not used
92 * @param[in] userdata - User data pointer
93 */
94 static int onTimeChange(sd_event_source* es, int fd, uint32_t revents,
95 void* userdata);
96
97 /** @brief The deleter of sd_event_source */
98 std::function<void(sd_event_source*)> sdEventSourceDeleter =
99 [](sd_event_source* p) {
100 if (p)
101 {
102 sd_event_source_unref(p);
103 }
104 };
105
106 using SdEventSource =
107 std::unique_ptr<sd_event_source, decltype(sdEventSourceDeleter)>;
108
109 /** @brief The event source on system time change */
110 SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter};
111
112 /** @brief Handle with the process when bmc time is changed*/
113 void handleTimeUpdates();
Carol Wang71230ef2020-02-18 17:39:49 +0800114};
115} // namespace manager
116} // namespace state
Carol Wang4ca6f3f2020-02-19 16:28:59 +0800117} // namespace phosphor