blob: b47a39b21c8bb3d846f0ffec5161033b2ba7c1c5 [file] [log] [blame]
Lei YU96232822017-01-20 14:05:46 +08001#pragma once
2
3#include "epoch_base.hpp"
4
Lei YU7b218792017-02-09 12:10:13 +08005#include <chrono>
6
Lei YU96232822017-01-20 14:05:46 +08007namespace phosphor
8{
9namespace time
10{
11
Lei YU7b218792017-02-09 12:10:13 +080012using namespace std::chrono;
13
Lei YU96232822017-01-20 14:05:46 +080014/** @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 */
19class BmcEpoch : public EpochBase
20{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050021 public:
22 friend class TestBmcEpoch;
23 BmcEpoch(sdbusplus::bus::bus& bus, const char* objPath);
24 ~BmcEpoch();
Lei YU96232822017-01-20 14:05:46 +080025
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050026 /**
27 * @brief Get value of Elapsed property
28 *
29 * @return The elapsed microseconds since UTC
30 **/
31 uint64_t elapsed() const override;
Lei YU96232822017-01-20 14:05:46 +080032
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050033 /**
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;
Lei YU7b218792017-02-09 12:10:13 +080040
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050041 private:
42 /** @brief The fd for time change event */
43 int timeFd = -1;
Lei YU7b218792017-02-09 12:10:13 +080044
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050045 /** @brief Initialize timerFd related resource */
46 void initialize();
Lei YU7b218792017-02-09 12:10:13 +080047
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050048 /** @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);
Lei YU7b218792017-02-09 12:10:13 +080057
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050058 /** @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)>;
Lei YU7b218792017-02-09 12:10:13 +080068
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050069 /** @brief The event source on system time change */
70 SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter};
Lei YU96232822017-01-20 14:05:46 +080071};
72
Lei YUaf5abc52017-03-07 17:49:17 +080073} // namespace time
74} // namespace phosphor