blob: 96c14b7c22724e8b25d5ac79734f29c167c14b9d [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{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050022 public:
23 friend class TestBmcEpoch;
24 BmcEpoch(sdbusplus::bus::bus& bus, const char* objPath);
25 ~BmcEpoch();
Lei YU96232822017-01-20 14:05:46 +080026
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050027 /**
28 * @brief Get value of Elapsed property
29 *
30 * @return The elapsed microseconds since UTC
31 **/
32 uint64_t elapsed() const override;
Lei YU96232822017-01-20 14:05:46 +080033
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050034 /**
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;
Lei YU7b218792017-02-09 12:10:13 +080041
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050042 /** @brief Set the listner for bmc time change
43 *
44 * @param[in] listener - The pointer to the listener
45 */
46 void setBmcTimeChangeListener(BmcTimeChangeListener* listener);
Lei YU7b218792017-02-09 12:10:13 +080047
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050048 private:
49 /** @brief The fd for time change event */
50 int timeFd = -1;
Lei YU7b218792017-02-09 12:10:13 +080051
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050052 /** @brief Initialize timerFd related resource */
53 void initialize();
Lei YU7b218792017-02-09 12:10:13 +080054
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050055 /** @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);
Lei YU7b218792017-02-09 12:10:13 +080060
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050061 /** @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);
Lei YU7b218792017-02-09 12:10:13 +080070
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050071 /** @brief The reference of sdbusplus bus */
72 sdbusplus::bus::bus& bus;
Lei YU7b218792017-02-09 12:10:13 +080073
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050074 /** @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)>;
Lei YU7b218792017-02-09 12:10:13 +080084
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050085 /** @brief The event source on system time change */
86 SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter};
Lei YU7b218792017-02-09 12:10:13 +080087
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050088 /** @brief The listener for bmc time change */
89 BmcTimeChangeListener* timeChangeListener = nullptr;
Lei YU96232822017-01-20 14:05:46 +080090};
91
Lei YUaf5abc52017-03-07 17:49:17 +080092} // namespace time
93} // namespace phosphor