Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 3 | #include "manager.hpp" |
| 4 | #include "property_change_listener.hpp" |
| 5 | |
| 6 | #include <sdbusplus/bus.hpp> |
| 7 | #include <xyz/openbmc_project/Time/EpochTime/server.hpp> |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 8 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 9 | #include <chrono> |
| 10 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 11 | namespace phosphor |
| 12 | { |
| 13 | namespace time |
| 14 | { |
| 15 | |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 16 | using EpochTimeIntf = sdbusplus::server::object_t< |
| 17 | sdbusplus::xyz::openbmc_project::Time::server::EpochTime>; |
| 18 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 19 | /** @class BmcEpoch |
| 20 | * @brief OpenBMC BMC EpochTime implementation. |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 21 | * @details A concrete implementation for |
| 22 | * xyz.openbmc_project.Time.EpochTime DBus API for BMC's epoch time. |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 23 | */ |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 24 | class BmcEpoch : public EpochTimeIntf, public PropertyChangeListner |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 25 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 26 | public: |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 27 | BmcEpoch(sdbusplus::bus_t& bus, const char* objPath, Manager& manager) : |
| 28 | EpochTimeIntf(bus, objPath), bus(bus), manager(manager) |
| 29 | { |
| 30 | initialize(); |
| 31 | } |
| 32 | |
Pavithra Barithaya | f93c405 | 2023-04-26 23:28:13 -0500 | [diff] [blame] | 33 | ~BmcEpoch() override; |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 34 | |
Pavithra Barithaya | 06df654 | 2023-04-28 01:18:31 -0500 | [diff] [blame] | 35 | BmcEpoch(const BmcEpoch&) = delete; |
| 36 | BmcEpoch(BmcEpoch&&) = delete; |
| 37 | BmcEpoch& operator=(const BmcEpoch&) = delete; |
| 38 | BmcEpoch& operator=(BmcEpoch&&) = delete; |
| 39 | |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 40 | /** @brief Notified on time mode changed */ |
| 41 | void onModeChanged(Mode mode) override; |
| 42 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 43 | /** |
| 44 | * @brief Get value of Elapsed property |
| 45 | * |
| 46 | * @return The elapsed microseconds since UTC |
| 47 | **/ |
| 48 | uint64_t elapsed() const override; |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 49 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 50 | /** |
| 51 | * @brief Set value of Elapsed property |
| 52 | * |
| 53 | * @param[in] value - The microseconds since UTC to set |
| 54 | * @return The updated elapsed microseconds since UTC |
| 55 | **/ |
| 56 | uint64_t elapsed(uint64_t value) override; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 57 | |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 58 | protected: |
| 59 | /** @brief Persistent sdbusplus DBus connection */ |
| 60 | sdbusplus::bus_t& bus; |
| 61 | |
| 62 | /** @brief The manager to handle OpenBMC time */ |
| 63 | Manager& manager; |
| 64 | |
| 65 | /** @brief Set current time to system |
| 66 | * |
| 67 | * This function set the time to system by invoking systemd |
| 68 | * org.freedesktop.timedate1's SetTime method. |
| 69 | * |
| 70 | * @param[in] timeOfDayUsec - Microseconds since UTC |
| 71 | * |
| 72 | * @return true or false to indicate if it sets time successfully |
| 73 | */ |
| 74 | bool setTime(const std::chrono::microseconds& timeOfDayUsec); |
| 75 | |
| 76 | /** @brief Get current time |
| 77 | * |
| 78 | * @return Microseconds since UTC |
| 79 | */ |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 80 | static std::chrono::microseconds getTime(); |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 81 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 82 | private: |
| 83 | /** @brief The fd for time change event */ |
| 84 | int timeFd = -1; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 85 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 86 | /** @brief Initialize timerFd related resource */ |
| 87 | void initialize(); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 88 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 89 | /** @brief The callback function on system time change |
| 90 | * |
| 91 | * @param[in] es - Source of the event |
| 92 | * @param[in] fd - File descriptor of the timer |
| 93 | * @param[in] revents - Not used |
| 94 | * @param[in] userdata - User data pointer |
| 95 | */ |
| 96 | static int onTimeChange(sd_event_source* es, int fd, uint32_t revents, |
| 97 | void* userdata); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 98 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 99 | /** @brief The deleter of sd_event_source */ |
| 100 | std::function<void(sd_event_source*)> sdEventSourceDeleter = |
| 101 | [](sd_event_source* p) { |
Patrick Williams | c0e77cf | 2024-08-16 15:20:46 -0400 | [diff] [blame] | 102 | if (p) |
| 103 | { |
| 104 | sd_event_source_unref(p); |
| 105 | } |
| 106 | }; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 107 | using SdEventSource = |
| 108 | std::unique_ptr<sd_event_source, decltype(sdEventSourceDeleter)>; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 109 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 110 | /** @brief The event source on system time change */ |
| 111 | SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter}; |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 112 | }; |
| 113 | |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 114 | } // namespace time |
| 115 | } // namespace phosphor |