summaryrefslogtreecommitdiffstats
path: root/bmc_epoch.hpp
blob: 96c14b7c22724e8b25d5ac79734f29c167c14b9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once

#include "bmc_time_change_listener.hpp"
#include "epoch_base.hpp"

#include <chrono>

namespace phosphor
{
namespace time
{

using namespace std::chrono;

/** @class BmcEpoch
 *  @brief OpenBMC BMC EpochTime implementation.
 *  @details A concrete implementation for xyz.openbmc_project.Time.EpochTime
 *  DBus API for BMC's epoch time.
 */
class BmcEpoch : public EpochBase
{
  public:
    friend class TestBmcEpoch;
    BmcEpoch(sdbusplus::bus::bus& bus, const char* objPath);
    ~BmcEpoch();

    /**
     * @brief Get value of Elapsed property
     *
     * @return The elapsed microseconds since UTC
     **/
    uint64_t elapsed() const override;

    /**
     * @brief Set value of Elapsed property
     *
     * @param[in] value - The microseconds since UTC to set
     * @return The updated elapsed microseconds since UTC
     **/
    uint64_t elapsed(uint64_t value) override;

    /** @brief Set the listner for bmc time change
     *
     * @param[in] listener - The pointer to the listener
     */
    void setBmcTimeChangeListener(BmcTimeChangeListener* listener);

  private:
    /** @brief The fd for time change event */
    int timeFd = -1;

    /** @brief Initialize timerFd related resource */
    void initialize();

    /** @brief Notify the listeners that bmc time is changed
     *
     * @param[in] time - The epoch time in microseconds to notify
     */
    void notifyBmcTimeChange(const microseconds& time);

    /** @brief The callback function on system time change
     *
     * @param[in] es - Source of the event
     * @param[in] fd - File descriptor of the timer
     * @param[in] revents - Not used
     * @param[in] userdata - User data pointer
     */
    static int onTimeChange(sd_event_source* es, int fd, uint32_t revents,
                            void* userdata);

    /** @brief The reference of sdbusplus bus */
    sdbusplus::bus::bus& bus;

    /** @brief The deleter of sd_event_source */
    std::function<void(sd_event_source*)> sdEventSourceDeleter =
        [](sd_event_source* p) {
            if (p)
            {
                sd_event_source_unref(p);
            }
        };
    using SdEventSource =
        std::unique_ptr<sd_event_source, decltype(sdEventSourceDeleter)>;

    /** @brief The event source on system time change */
    SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter};

    /** @brief The listener for bmc time change */
    BmcTimeChangeListener* timeChangeListener = nullptr;
};

} // namespace time
} // namespace phosphor
OpenPOWER on IntegriCloud