blob: 29748a6c935bc9d0400f485e077d98a07ded6e70 [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
George Liuf1d54ae2022-09-01 16:54:57 +080016using EpochTimeIntf = sdbusplus::server::object_t<
17 sdbusplus::xyz::openbmc_project::Time::server::EpochTime>;
18
Lei YU96232822017-01-20 14:05:46 +080019/** @class BmcEpoch
20 * @brief OpenBMC BMC EpochTime implementation.
George Liuf1d54ae2022-09-01 16:54:57 +080021 * @details A concrete implementation for
22 * xyz.openbmc_project.Time.EpochTime DBus API for BMC's epoch time.
Lei YU96232822017-01-20 14:05:46 +080023 */
George Liuf1d54ae2022-09-01 16:54:57 +080024class BmcEpoch : public EpochTimeIntf, public PropertyChangeListner
Lei YU96232822017-01-20 14:05:46 +080025{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050026 public:
George Liuf1d54ae2022-09-01 16:54:57 +080027 BmcEpoch(sdbusplus::bus_t& bus, const char* objPath, Manager& manager) :
28 EpochTimeIntf(bus, objPath), bus(bus), manager(manager)
29 {
30 initialize();
31 }
32
Pavithra Barithayaf93c4052023-04-26 23:28:13 -050033 ~BmcEpoch() override;
Lei YU96232822017-01-20 14:05:46 +080034
Pavithra Barithaya06df6542023-04-28 01:18:31 -050035 BmcEpoch(const BmcEpoch&) = delete;
36 BmcEpoch(BmcEpoch&&) = delete;
37 BmcEpoch& operator=(const BmcEpoch&) = delete;
38 BmcEpoch& operator=(BmcEpoch&&) = delete;
39
George Liuf1d54ae2022-09-01 16:54:57 +080040 /** @brief Notified on time mode changed */
41 void onModeChanged(Mode mode) override;
42
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050043 /**
44 * @brief Get value of Elapsed property
45 *
46 * @return The elapsed microseconds since UTC
47 **/
48 uint64_t elapsed() const override;
Lei YU96232822017-01-20 14:05:46 +080049
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050050 /**
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 YU7b218792017-02-09 12:10:13 +080057
George Liuf1d54ae2022-09-01 16:54:57 +080058 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 Barithaya864e1732023-04-11 04:30:23 -050080 static std::chrono::microseconds getTime();
George Liuf1d54ae2022-09-01 16:54:57 +080081
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050082 private:
83 /** @brief The fd for time change event */
84 int timeFd = -1;
Lei YU7b218792017-02-09 12:10:13 +080085
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050086 /** @brief Initialize timerFd related resource */
87 void initialize();
Lei YU7b218792017-02-09 12:10:13 +080088
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050089 /** @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 YU7b218792017-02-09 12:10:13 +080098
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050099 /** @brief The deleter of sd_event_source */
100 std::function<void(sd_event_source*)> sdEventSourceDeleter =
101 [](sd_event_source* p) {
Patrick Williamsea220362023-05-10 07:50:45 -0500102 if (p)
103 {
104 sd_event_source_unref(p);
105 }
106 };
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500107 using SdEventSource =
108 std::unique_ptr<sd_event_source, decltype(sdEventSourceDeleter)>;
Lei YU7b218792017-02-09 12:10:13 +0800109
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500110 /** @brief The event source on system time change */
111 SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter};
Lei YU96232822017-01-20 14:05:46 +0800112};
113
Lei YUaf5abc52017-03-07 17:49:17 +0800114} // namespace time
115} // namespace phosphor