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