1 #pragma once
2 
3 #include "bmc_time_change_listener.hpp"
4 #include "epoch_base.hpp"
5 
6 #include <chrono>
7 
8 namespace phosphor
9 {
10 namespace time
11 {
12 
13 using namespace std::chrono;
14 
15 /** @class BmcEpoch
16  *  @brief OpenBMC BMC EpochTime implementation.
17  *  @details A concrete implementation for xyz.openbmc_project.Time.EpochTime
18  *  DBus API for BMC's epoch time.
19  */
20 class BmcEpoch : public EpochBase
21 {
22   public:
23     friend class TestBmcEpoch;
24     BmcEpoch(sdbusplus::bus::bus& bus, const char* objPath);
25     ~BmcEpoch();
26 
27     /**
28      * @brief Get value of Elapsed property
29      *
30      * @return The elapsed microseconds since UTC
31      **/
32     uint64_t elapsed() const override;
33 
34     /**
35      * @brief Set value of Elapsed property
36      *
37      * @param[in] value - The microseconds since UTC to set
38      * @return The updated elapsed microseconds since UTC
39      **/
40     uint64_t elapsed(uint64_t value) override;
41 
42     /** @brief Set the listner for bmc time change
43      *
44      * @param[in] listener - The pointer to the listener
45      */
46     void setBmcTimeChangeListener(BmcTimeChangeListener* listener);
47 
48   private:
49     /** @brief The fd for time change event */
50     int timeFd = -1;
51 
52     /** @brief Initialize timerFd related resource */
53     void initialize();
54 
55     /** @brief Notify the listeners that bmc time is changed
56      *
57      * @param[in] time - The epoch time in microseconds to notify
58      */
59     void notifyBmcTimeChange(const microseconds& time);
60 
61     /** @brief The callback function on system time change
62      *
63      * @param[in] es - Source of the event
64      * @param[in] fd - File descriptor of the timer
65      * @param[in] revents - Not used
66      * @param[in] userdata - User data pointer
67      */
68     static int onTimeChange(sd_event_source* es, int fd, uint32_t revents,
69                             void* userdata);
70 
71     /** @brief The reference of sdbusplus bus */
72     sdbusplus::bus::bus& bus;
73 
74     /** @brief The deleter of sd_event_source */
75     std::function<void(sd_event_source*)> sdEventSourceDeleter =
76         [](sd_event_source* p) {
77             if (p)
78             {
79                 sd_event_source_unref(p);
80             }
81         };
82     using SdEventSource =
83         std::unique_ptr<sd_event_source, decltype(sdEventSourceDeleter)>;
84 
85     /** @brief The event source on system time change */
86     SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter};
87 
88     /** @brief The listener for bmc time change */
89     BmcTimeChangeListener* timeChangeListener = nullptr;
90 };
91 
92 } // namespace time
93 } // namespace phosphor
94