blob: fc505d0d201d15f4d61113f54fafe21bc5bc79ca [file] [log] [blame]
Lei YU2f9c0cc2017-01-20 14:02:03 +08001#pragma once
2
3#include <sdbusplus/bus.hpp>
4#include <xyz/openbmc_project/Time/EpochTime/server.hpp>
5
6#include <chrono>
7
8namespace phosphor
9{
10namespace time
11{
12
13/** @class EpochBase
14 * @brief Base class for OpenBMC EpochTime implementation.
15 * @details A base class that implements xyz.openbmc_project.Time.EpochTime
16 * DBus API for epoch time.
17 */
18class EpochBase : public sdbusplus::server::object::object <
19 sdbusplus::xyz::openbmc_project::Time::server::EpochTime >
20{
21 public:
22 friend class TestEpochBase;
23
24 /** @brief Supported time modes
25 * NTP Time sourced by Network Time Server
26 * MANUAL User of the system need to set the time
27 */
28 enum class Mode
29 {
30 NTP,
31 MANUAL,
32 };
33
34 /** @brief Supported time owners
35 * BMC Time source may be NTP or MANUAL but it has to be set natively
36 * on the BMC. Meaning, host can not set the time. What it also
37 * means is that when BMC gets IPMI_SET_SEL_TIME, then its ignored.
38 * similarly, when BMC gets IPMI_GET_SEL_TIME, then the BMC's time
39 * is returned.
40 *
41 * HOST Its only IPMI_SEL_SEL_TIME that will set the time on BMC.
42 * Meaning, IPMI_GET_SEL_TIME and request to get BMC time will
43 * result in same value.
44 *
45 * SPLIT Both BMC and HOST will maintain their individual clocks but then
46 * the time information is stored in BMC. BMC can have either NTP
47 * or MANUAL as it's source of time and will set the time directly
48 * on the BMC. When IPMI_SET_SEL_TIME is received, then the delta
49 * between that and BMC's time is calculated and is stored.
50 * When BMC reads the time, the current time is returned.
51 * When IPMI_GET_SEL_TIME is received, BMC's time is retrieved and
52 * then the delta offset is factored in prior to returning.
53 *
54 * BOTH: BMC's time is set with whoever that sets the time. Similarly,
55 * BMC's time is returned to whoever that asks the time.
56 */
57 enum class Owner
58 {
59 BMC,
60 HOST,
61 SPLIT,
62 BOTH,
63 };
64
65 EpochBase(sdbusplus::bus::bus& bus,
66 const char* objPath);
67
68 protected:
69 /** @brief Persistent sdbusplus DBus connection */
70 sdbusplus::bus::bus& bus;
71
72 /** @brief The current time mode */
73 Mode timeMode;
74
75 /** @brief The current time owner */
76 Owner timeOwner;
77
78 /** @brief Set current time to system
79 *
80 * This function set the time to system by invoking systemd
81 * org.freedesktop.timedate1's SetTime method.
82 *
83 * @param[in] timeOfDayUsec - Microseconds since UTC
84 */
85 void setTime(const std::chrono::microseconds& timeOfDayUsec);
86
87 /** @brief Get current time
88 *
89 * @return Microseconds since UTC
90 */
91 std::chrono::microseconds getTime() const;
92
93 /** @brief Convert a string to enum Mode
94 *
95 * Convert the time mode string to enum.
96 * Valid strings are "NTP", "MANUAL"
97 * If it's not a valid time mode string, return NTP.
98 *
99 * @param[in] mode - The string of time mode
100 *
101 * @return The Mode enum
102 */
103 static Mode convertToMode(const std::string& mode);
104
105 /** @brief Convert a string to enum Owner
106 *
107 * Convert the time owner string to enum.
108 * Valid strings are "BMC", "HOST", "SPLIT", "BOTH"
109 * If it's not a valid time owner string, return BMC.
110 *
111 * @param[in] owner - The string of time owner
112 *
113 * @return The Owner enum
114 */
115 static Owner convertToOwner(const std::string& owner);
116
117 private:
118 /** @brief Initialize the time mode and owner */
119 void initialize();
120
121 /** @brief Set current time mode
122 *
123 * @param[in] mode - The string of time mode
124 */
125 void setCurrentTimeMode(const std::string& mode);
126
127 /** @brief Set current time owner
128 *
129 * @param[in] owner - The string of time owner
130 */
131 void setCurrentTimeOwner(const std::string& owner);
132
133 /** @brief Get setting value from settings manager
134 *
135 * @param[in] setting - The string of the setting to get
136 *
137 * @return The value of the setting
138 */
139 std::string getSettings(const char* setting) const;
140
141 /** @brief The map maps the string key to enum Owner */
142 static const std::map<std::string, Owner> ownerMap;
143};
144
145} // namespace time
146} // namespace phosphor