blob: 7edb30f142d99f293db6629b97151d12874c000a [file] [log] [blame]
Lei YU415b9642017-02-09 11:37:26 +08001#pragma once
2
3#include "types.hpp"
4#include "property_change_listener.hpp"
5
6#include <sdbusplus/bus.hpp>
7#include <sdbusplus/bus/match.hpp>
8
9#include <set>
10#include <string>
11
12namespace phosphor
13{
14namespace time
15{
16
17/** @class Manager
18 * @brief The manager to handle OpenBMC time.
19 * @details It registers various time related settings and properties signals
20 * on DBus and handle the changes.
21 * For certain properties it also notifies the changed events to listeners.
22 */
23class Manager
24{
25 public:
26 friend class TestManager;
Lei YU7f4fca52017-02-23 15:15:51 +080027
Lei YU415b9642017-02-09 11:37:26 +080028 explicit Manager(sdbusplus::bus::bus& bus);
Lei YUc6fe8692017-02-23 15:12:07 +080029 Manager(const Manager&) = delete;
30 Manager& operator=(const Manager&) = delete;
31 Manager(Manager&&) = delete;
32 Manager& operator=(Manager&&) = delete;
Lei YU415b9642017-02-09 11:37:26 +080033
34 /** @brief Add a listener that will be called
35 * when property is changed
36 **/
37 void addListener(PropertyChangeListner* listener);
38
39 private:
40 /** @brief Persistent sdbusplus DBus connection */
41 sdbusplus::bus::bus& bus;
42
43 /** @brief The match of settings property change */
44 sdbusplus::bus::match::match propertyChangeMatch;
45
Lei YUc6fe8692017-02-23 15:12:07 +080046 /** @brief The match of pgood change */
47 sdbusplus::bus::match::match pgoodChangeMatch;
48
Lei YU415b9642017-02-09 11:37:26 +080049 /** @brief The container to hold all the listeners */
50 std::set<PropertyChangeListner*> listeners;
51
Lei YUc6fe8692017-02-23 15:12:07 +080052 /** @brief The value to indicate if host is on */
53 bool hostOn = false;
54
Lei YU7f4fca52017-02-23 15:15:51 +080055 /** @brief The requested time mode when host is on*/
56 std::string requestedMode;
57
58 /** @brief The requested time owner when host is on*/
59 std::string requestedOwner;
60
Lei YU415b9642017-02-09 11:37:26 +080061 /** @brief The current time mode */
62 Mode timeMode;
63
64 /** @brief The current time owner */
65 Owner timeOwner;
66
Lei YU7f4fca52017-02-23 15:15:51 +080067 /** @brief Restore saved settings */
68 void restoreSettings();
69
Lei YUc6fe8692017-02-23 15:12:07 +080070 /** @brief Check if host is on and update hostOn variable */
71 void checkHostOn();
72
Lei YU415b9642017-02-09 11:37:26 +080073 /** @brief Get setting from settingsd service
74 *
75 * @param[in] setting - The string of the setting
76 *
77 * @return The setting value in string
78 */
79 std::string getSettings(const char* setting) const;
80
81 /** @brief Set current time mode
82 *
83 * @param[in] mode - The string of time mode
84 */
85 void setCurrentTimeMode(const std::string& mode);
86
87 /** @brief Set current time owner
88 *
89 * @param[in] owner - The string of time owner
90 */
91 void setCurrentTimeOwner(const std::string& owner);
92
93 /** @brief Notified on settings property changed
94 *
95 * @param[in] key - The name of property that is changed
96 * @param[in] value - The value of the property
97 */
98 void onPropertyChanged(const std::string& key,
99 const std::string& value);
100
Lei YUc6fe8692017-02-23 15:12:07 +0800101 /** @brief Notified on pgood has changed
102 *
103 * @param[in] pgood - The changed pgood value
104 */
105 void onPgoodChanged(bool pgood);
106
Lei YU7f4fca52017-02-23 15:15:51 +0800107 /** @brief Set the property as requested time mode/owner
108 *
109 * @param[in] key - The property name
110 * @param[in] value - The property value
111 */
112 void setPropertyAsRequested(const std::string& key,
113 const std::string& value);
114
115 /** @brief Set the current mode to user requested one
116 * if conditions allow it
117 *
118 * @param[in] mode - The string of time mode
119 */
120 void setRequestedMode(const std::string& mode);
121
122 /** @brief Set the current owner to user requested one
123 * if conditions allow it
124 *
125 * @param[in] owner - The string of time owner
126 */
127 void setRequestedOwner(const std::string& owner);
128
Lei YU415b9642017-02-09 11:37:26 +0800129 /** @brief The static function called on settings property changed
130 *
131 * @param[in] msg - Data associated with subscribed signal
132 * @param[in] userData - Pointer to this object instance
133 * @param[out] retError - Not used but required with signal API
134 */
135 static int onPropertyChanged(sd_bus_message* msg,
136 void* userData,
137 sd_bus_error* retError);
138
Lei YUc6fe8692017-02-23 15:12:07 +0800139 /** @brief Notified on pgood has changed
140 *
141 * @param[in] msg - Data associated with subscribed signal
142 * @param[in] userData - Pointer to this object instance
143 * @param[out] retError - Not used but required with signal API
144 */
145 static int onPgoodChanged(sd_bus_message* msg,
146 void* userData,
147 sd_bus_error* retError);
148
Lei YU415b9642017-02-09 11:37:26 +0800149 /** @brief Convert a string to enum Mode
150 *
151 * Convert the time mode string to enum.
152 * Valid strings are "NTP", "MANUAL"
153 * If it's not a valid time mode string, return NTP.
154 *
155 * @param[in] mode - The string of time mode
156 *
157 * @return The Mode enum
158 */
159 static Mode convertToMode(const std::string& mode);
160
161 /** @brief Convert a string to enum Owner
162 *
163 * Convert the time owner string to enum.
164 * Valid strings are "BMC", "HOST", "SPLIT", "BOTH"
165 * If it's not a valid time owner string, return BMC.
166 *
167 * @param[in] owner - The string of time owner
168 *
169 * @return The Owner enum
170 */
171 static Owner convertToOwner(const std::string& owner);
172
Lei YU7f4fca52017-02-23 15:15:51 +0800173 /** @brief The string of time mode property */
174 static constexpr auto PROPERTY_TIME_MODE = "time_mode";
175
176 /** @brief The string of time owner property */
177 static constexpr auto PROPERTY_TIME_OWNER = "time_owner";
178
Lei YU415b9642017-02-09 11:37:26 +0800179 using Updater = std::function<void(const std::string&)>;
180
181 /** @brief Map the property string to functions that shall
182 * be called when the property is changed
183 */
184 const std::map<std::string, Updater> propertyUpdaters =
185 {
Lei YU7f4fca52017-02-23 15:15:51 +0800186 {PROPERTY_TIME_MODE, std::bind(&Manager::setCurrentTimeMode,
187 this, std::placeholders::_1)},
188 {PROPERTY_TIME_OWNER, std::bind(&Manager::setCurrentTimeOwner,
189 this, std::placeholders::_1)}
Lei YU415b9642017-02-09 11:37:26 +0800190 };
191
192 /** @brief The properties that manager shall notify the
193 * listeners when changed
194 */
195 static const std::set<std::string> managedProperties;
196
197 /** @brief The map that maps the string to Owners */
198 static const std::map<std::string, Owner> ownerMap;
Lei YU7f4fca52017-02-23 15:15:51 +0800199
200 /** @brief The file name of saved time mode */
201 static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode";
202
203 /** @brief The file name of saved time owner */
204 static constexpr auto ownerFile = "/var/lib/obmc/saved_time_owner";
Lei YU415b9642017-02-09 11:37:26 +0800205};
206
207}
208}