blob: fd5e3f48ba545f359ee6225e82e10a44a58ef18e [file] [log] [blame]
Lei YU96232822017-01-20 14:05:46 +08001#pragma once
2
George Liuf1d54ae2022-09-01 16:54:57 +08003#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 YU96232822017-01-20 14:05:46 +08008
Lei YU7b218792017-02-09 12:10:13 +08009#include <chrono>
10
Lei YU96232822017-01-20 14:05:46 +080011namespace phosphor
12{
13namespace time
14{
15
Lei YU7b218792017-02-09 12:10:13 +080016using namespace std::chrono;
17
George Liuf1d54ae2022-09-01 16:54:57 +080018using EpochTimeIntf = sdbusplus::server::object_t<
19 sdbusplus::xyz::openbmc_project::Time::server::EpochTime>;
20
Lei YU96232822017-01-20 14:05:46 +080021/** @class BmcEpoch
22 * @brief OpenBMC BMC EpochTime implementation.
George Liuf1d54ae2022-09-01 16:54:57 +080023 * @details A concrete implementation for
24 * xyz.openbmc_project.Time.EpochTime DBus API for BMC's epoch time.
Lei YU96232822017-01-20 14:05:46 +080025 */
George Liuf1d54ae2022-09-01 16:54:57 +080026class BmcEpoch : public EpochTimeIntf, public PropertyChangeListner
Lei YU96232822017-01-20 14:05:46 +080027{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050028 public:
George Liuf1d54ae2022-09-01 16:54:57 +080029 BmcEpoch(sdbusplus::bus_t& bus, const char* objPath, Manager& manager) :
30 EpochTimeIntf(bus, objPath), bus(bus), manager(manager)
31 {
32 initialize();
33 }
34
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050035 ~BmcEpoch();
Lei YU96232822017-01-20 14:05:46 +080036
George Liuf1d54ae2022-09-01 16:54:57 +080037 /** @brief Notified on time mode changed */
38 void onModeChanged(Mode mode) override;
39
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050040 /**
41 * @brief Get value of Elapsed property
42 *
43 * @return The elapsed microseconds since UTC
44 **/
45 uint64_t elapsed() const override;
Lei YU96232822017-01-20 14:05:46 +080046
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050047 /**
48 * @brief Set value of Elapsed property
49 *
50 * @param[in] value - The microseconds since UTC to set
51 * @return The updated elapsed microseconds since UTC
52 **/
53 uint64_t elapsed(uint64_t value) override;
Lei YU7b218792017-02-09 12:10:13 +080054
George Liuf1d54ae2022-09-01 16:54:57 +080055 protected:
56 /** @brief Persistent sdbusplus DBus connection */
57 sdbusplus::bus_t& bus;
58
59 /** @brief The manager to handle OpenBMC time */
60 Manager& manager;
61
62 /** @brief Set current time to system
63 *
64 * This function set the time to system by invoking systemd
65 * org.freedesktop.timedate1's SetTime method.
66 *
67 * @param[in] timeOfDayUsec - Microseconds since UTC
68 *
69 * @return true or false to indicate if it sets time successfully
70 */
71 bool setTime(const std::chrono::microseconds& timeOfDayUsec);
72
73 /** @brief Get current time
74 *
75 * @return Microseconds since UTC
76 */
Pavithra Barithaya864e1732023-04-11 04:30:23 -050077 static std::chrono::microseconds getTime();
George Liuf1d54ae2022-09-01 16:54:57 +080078
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050079 private:
80 /** @brief The fd for time change event */
81 int timeFd = -1;
Lei YU7b218792017-02-09 12:10:13 +080082
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050083 /** @brief Initialize timerFd related resource */
84 void initialize();
Lei YU7b218792017-02-09 12:10:13 +080085
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050086 /** @brief The callback function on system time change
87 *
88 * @param[in] es - Source of the event
89 * @param[in] fd - File descriptor of the timer
90 * @param[in] revents - Not used
91 * @param[in] userdata - User data pointer
92 */
93 static int onTimeChange(sd_event_source* es, int fd, uint32_t revents,
94 void* userdata);
Lei YU7b218792017-02-09 12:10:13 +080095
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050096 /** @brief The deleter of sd_event_source */
97 std::function<void(sd_event_source*)> sdEventSourceDeleter =
98 [](sd_event_source* p) {
99 if (p)
100 {
101 sd_event_source_unref(p);
102 }
103 };
104 using SdEventSource =
105 std::unique_ptr<sd_event_source, decltype(sdEventSourceDeleter)>;
Lei YU7b218792017-02-09 12:10:13 +0800106
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500107 /** @brief The event source on system time change */
108 SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter};
Lei YU96232822017-01-20 14:05:46 +0800109};
110
Lei YUaf5abc52017-03-07 17:49:17 +0800111} // namespace time
112} // namespace phosphor