blob: 40b86f111c5e69ca0d4589d35d5552bf14f24b5f [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
Pavithra Barithayaf93c4052023-04-26 23:28:13 -050035 ~BmcEpoch() override;
Lei YU96232822017-01-20 14:05:46 +080036
Pavithra Barithaya06df6542023-04-28 01:18:31 -050037 BmcEpoch(const BmcEpoch&) = delete;
38 BmcEpoch(BmcEpoch&&) = delete;
39 BmcEpoch& operator=(const BmcEpoch&) = delete;
40 BmcEpoch& operator=(BmcEpoch&&) = delete;
41
George Liuf1d54ae2022-09-01 16:54:57 +080042 /** @brief Notified on time mode changed */
43 void onModeChanged(Mode mode) override;
44
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050045 /**
46 * @brief Get value of Elapsed property
47 *
48 * @return The elapsed microseconds since UTC
49 **/
50 uint64_t elapsed() const override;
Lei YU96232822017-01-20 14:05:46 +080051
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050052 /**
53 * @brief Set value of Elapsed property
54 *
55 * @param[in] value - The microseconds since UTC to set
56 * @return The updated elapsed microseconds since UTC
57 **/
58 uint64_t elapsed(uint64_t value) override;
Lei YU7b218792017-02-09 12:10:13 +080059
George Liuf1d54ae2022-09-01 16:54:57 +080060 protected:
61 /** @brief Persistent sdbusplus DBus connection */
62 sdbusplus::bus_t& bus;
63
64 /** @brief The manager to handle OpenBMC time */
65 Manager& manager;
66
67 /** @brief Set current time to system
68 *
69 * This function set the time to system by invoking systemd
70 * org.freedesktop.timedate1's SetTime method.
71 *
72 * @param[in] timeOfDayUsec - Microseconds since UTC
73 *
74 * @return true or false to indicate if it sets time successfully
75 */
76 bool setTime(const std::chrono::microseconds& timeOfDayUsec);
77
78 /** @brief Get current time
79 *
80 * @return Microseconds since UTC
81 */
Pavithra Barithaya864e1732023-04-11 04:30:23 -050082 static std::chrono::microseconds getTime();
George Liuf1d54ae2022-09-01 16:54:57 +080083
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050084 private:
85 /** @brief The fd for time change event */
86 int timeFd = -1;
Lei YU7b218792017-02-09 12:10:13 +080087
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050088 /** @brief Initialize timerFd related resource */
89 void initialize();
Lei YU7b218792017-02-09 12:10:13 +080090
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050091 /** @brief The callback function on system time change
92 *
93 * @param[in] es - Source of the event
94 * @param[in] fd - File descriptor of the timer
95 * @param[in] revents - Not used
96 * @param[in] userdata - User data pointer
97 */
98 static int onTimeChange(sd_event_source* es, int fd, uint32_t revents,
99 void* userdata);
Lei YU7b218792017-02-09 12:10:13 +0800100
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500101 /** @brief The deleter of sd_event_source */
102 std::function<void(sd_event_source*)> sdEventSourceDeleter =
103 [](sd_event_source* p) {
104 if (p)
105 {
106 sd_event_source_unref(p);
107 }
108 };
109 using SdEventSource =
110 std::unique_ptr<sd_event_source, decltype(sdEventSourceDeleter)>;
Lei YU7b218792017-02-09 12:10:13 +0800111
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500112 /** @brief The event source on system time change */
113 SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter};
Lei YU96232822017-01-20 14:05:46 +0800114};
115
Lei YUaf5abc52017-03-07 17:49:17 +0800116} // namespace time
117} // namespace phosphor