blob: 228a39dae20286ac4455e1820896cca673f371a9 [file] [log] [blame]
Lei YU96232822017-01-20 14:05:46 +08001#pragma once
2
Lei YU7b218792017-02-09 12:10:13 +08003#include "bmc_time_change_listener.hpp"
Lei YU96232822017-01-20 14:05:46 +08004#include "epoch_base.hpp"
5
Lei YU7b218792017-02-09 12:10:13 +08006#include <chrono>
7
Lei YU96232822017-01-20 14:05:46 +08008namespace phosphor
9{
10namespace time
11{
12
Lei YU7b218792017-02-09 12:10:13 +080013using namespace std::chrono;
14
Lei YU96232822017-01-20 14:05:46 +080015/** @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 */
20class BmcEpoch : public EpochBase
21{
22 public:
23 friend class TestBmcEpoch;
24 BmcEpoch(sdbusplus::bus::bus& bus,
25 const char* objPath);
Lei YU7b218792017-02-09 12:10:13 +080026 ~BmcEpoch();
Lei YU96232822017-01-20 14:05:46 +080027
28 /**
29 * @brief Get value of Elapsed property
30 *
31 * @return The elapsed microseconds since UTC
32 **/
33 uint64_t elapsed() const override;
34
35 /**
36 * @brief Set value of Elapsed property
37 *
38 * @param[in] value - The microseconds since UTC to set
39 * @return The updated elapsed microseconds since UTC
40 **/
41 uint64_t elapsed(uint64_t value) override;
Lei YU7b218792017-02-09 12:10:13 +080042
43 /** @brief Set the listner for bmc time change
44 *
45 * @param[in] listener - The pointer to the listener
46 */
47 void setBmcTimeChangeListener(BmcTimeChangeListener* listener);
48
49 private:
50 /** @brief The fd for time change event */
51 int timeFd = -1;
52
53 /** @brief Initialize timerFd related resource */
54 void initialize();
55
56 /** @brief Notify the listeners that bmc time is changed
57 *
58 * @param[in] time - The epoch time in microseconds to notify
59 */
60 void notifyBmcTimeChange(const microseconds& time);
61
62 /** @brief The callback function on system time change
63 *
64 * @param[in] es - Source of the event
65 * @param[in] fd - File descriptor of the timer
66 * @param[in] revents - Not used
67 * @param[in] userdata - User data pointer
68 */
69 static int onTimeChange(sd_event_source* es, int fd,
70 uint32_t revents, void* userdata);
71
72 /** @brief The reference of sdbusplus bus */
73 sdbusplus::bus::bus& bus;
74
75 /** @brief The deleter of sd_event_source */
76 std::function<void(sd_event_source*)> sdEventSourceDeleter =
77 [] (sd_event_source* p) {
78 if (p)
79 {
80 sd_event_source_unref(p);
81 }
82 };
83 using SdEventSource = std::unique_ptr<sd_event_source,
84 decltype(sdEventSourceDeleter)>;
85
86 /** @brief The event source on system time change */
87 SdEventSource timeChangeEventSource {nullptr, sdEventSourceDeleter};
88
89 /** @brief The listener for bmc time change */
90 BmcTimeChangeListener* timeChangeListener = nullptr;
Lei YU96232822017-01-20 14:05:46 +080091};
92
Lei YUaf5abc52017-03-07 17:49:17 +080093} // namespace time
94} // namespace phosphor