Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 3 | #include "config.h" |
| 4 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 5 | #include <sdbusplus/bus.hpp> |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 6 | #include <sdeventplus/event.hpp> |
| 7 | #include <sdeventplus/utility/timer.hpp> |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 8 | #include <xyz/openbmc_project/State/ScheduledHostTransition/server.hpp> |
| 9 | |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 10 | class TestScheduledHostTransition; |
| 11 | |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace state |
| 15 | { |
| 16 | namespace manager |
| 17 | { |
| 18 | |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 19 | using Transition = |
| 20 | sdbusplus::xyz::openbmc_project::State::server::Host::Transition; |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 21 | using 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 | */ |
| 29 | class ScheduledHostTransition : public ScheduledHostTransitionInherit |
| 30 | { |
| 31 | public: |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 32 | ScheduledHostTransition(sdbusplus::bus::bus& bus, const char* objPath, |
Patrick Williams | 211d972 | 2022-04-07 21:24:33 -0500 | [diff] [blame] | 33 | size_t id, const sdeventplus::Event& event) : |
Patrick Williams | 7607074 | 2022-04-05 15:20:12 -0500 | [diff] [blame] | 34 | ScheduledHostTransitionInherit( |
| 35 | bus, objPath, ScheduledHostTransition::action::defer_emit), |
Patrick Williams | 211d972 | 2022-04-07 21:24:33 -0500 | [diff] [blame] | 36 | bus(bus), id(id), event(event), |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 37 | timer(event, std::bind(&ScheduledHostTransition::callback, this)) |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 38 | { |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 39 | initialize(); |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame] | 40 | |
| 41 | restoreScheduledValues(); |
| 42 | |
| 43 | // We deferred this until we could get our property correct |
| 44 | this->emit_object_added(); |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 45 | } |
| 46 | |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 47 | ~ScheduledHostTransition(); |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 48 | |
| 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 Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | friend class TestScheduledHostTransition; |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 61 | |
| 62 | /** @brief sdbusplus bus client connection */ |
| 63 | sdbusplus::bus::bus& bus; |
| 64 | |
Patrick Williams | 211d972 | 2022-04-07 21:24:33 -0500 | [diff] [blame] | 65 | /** @brief Host id. **/ |
| 66 | const size_t id = 0; |
| 67 | |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 68 | /** @brief sdbusplus event */ |
| 69 | const sdeventplus::Event& event; |
| 70 | |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 71 | /** @brief Timer used for host transition with seconds */ |
| 72 | sdeventplus::utility::Timer<sdeventplus::ClockId::RealTime> timer; |
| 73 | |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 74 | /** @brief The fd for time change event */ |
| 75 | int timeFd = -1; |
| 76 | |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 77 | /** @brief Get current time |
| 78 | * |
| 79 | * @return - return current epoch time |
| 80 | */ |
| 81 | std::chrono::seconds getTime(); |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 82 | |
| 83 | /** @brief Implement host transition |
| 84 | * |
| 85 | * @return - Does not return anything. Error will result in exception |
| 86 | * being thrown |
| 87 | */ |
| 88 | void hostTransition(); |
| 89 | |
| 90 | /** @brief Used by the timer to do host transition */ |
| 91 | void callback(); |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 92 | |
| 93 | /** @brief Initialize timerFd related resource */ |
| 94 | void initialize(); |
| 95 | |
| 96 | /** @brief The callback function on system time change |
| 97 | * |
| 98 | * @param[in] es - Source of the event |
| 99 | * @param[in] fd - File descriptor of the timer |
| 100 | * @param[in] revents - Not used |
| 101 | * @param[in] userdata - User data pointer |
| 102 | */ |
| 103 | static int onTimeChange(sd_event_source* es, int fd, uint32_t revents, |
| 104 | void* userdata); |
| 105 | |
| 106 | /** @brief The deleter of sd_event_source */ |
| 107 | std::function<void(sd_event_source*)> sdEventSourceDeleter = |
| 108 | [](sd_event_source* p) { |
| 109 | if (p) |
| 110 | { |
| 111 | sd_event_source_unref(p); |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | using SdEventSource = |
| 116 | std::unique_ptr<sd_event_source, decltype(sdEventSourceDeleter)>; |
| 117 | |
| 118 | /** @brief The event source on system time change */ |
| 119 | SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter}; |
| 120 | |
| 121 | /** @brief Handle with the process when bmc time is changed*/ |
| 122 | void handleTimeUpdates(); |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame] | 123 | |
| 124 | /** @brief Serialize the scheduled values */ |
| 125 | void serializeScheduledValues(); |
| 126 | |
| 127 | /** @brief Deserialize the scheduled values |
| 128 | * |
| 129 | * @param[out] time - Deserialized scheduled time |
| 130 | * @param[out] trans - Deserialized requested transition |
| 131 | * |
| 132 | * @return bool - true if successful, false otherwise |
| 133 | */ |
| 134 | bool deserializeScheduledValues(uint64_t& time, Transition& trans); |
| 135 | |
| 136 | /** @brief Restore scheduled time and requested transition from persisted |
| 137 | * file */ |
| 138 | void restoreScheduledValues(); |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 139 | }; |
| 140 | } // namespace manager |
| 141 | } // namespace state |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 142 | } // namespace phosphor |