blob: 21cf54089e1d923cba7db2e1b656296007c56f84 [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 reference of sdbusplus bus */
59 sdbusplus::bus::bus& bus;
Lei YU7b218792017-02-09 12:10:13 +080060
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050061 /** @brief The deleter of sd_event_source */
62 std::function<void(sd_event_source*)> sdEventSourceDeleter =
63 [](sd_event_source* p) {
64 if (p)
65 {
66 sd_event_source_unref(p);
67 }
68 };
69 using SdEventSource =
70 std::unique_ptr<sd_event_source, decltype(sdEventSourceDeleter)>;
Lei YU7b218792017-02-09 12:10:13 +080071
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050072 /** @brief The event source on system time change */
73 SdEventSource timeChangeEventSource{nullptr, sdEventSourceDeleter};
Lei YU96232822017-01-20 14:05:46 +080074};
75
Lei YUaf5abc52017-03-07 17:49:17 +080076} // namespace time
77} // namespace phosphor