blob: f9ee5111decdc30cf971ab14c12ecb1946fab54e [file] [log] [blame]
Lei YU415b9642017-02-09 11:37:26 +08001#pragma once
2
3#include "types.hpp"
4#include "property_change_listener.hpp"
Lei YU710d49b2017-08-01 17:10:17 +08005#include "settings.hpp"
Lei YU415b9642017-02-09 11:37:26 +08006
7#include <sdbusplus/bus.hpp>
8#include <sdbusplus/bus/match.hpp>
9
10#include <set>
11#include <string>
12
13namespace phosphor
14{
15namespace time
16{
17
18/** @class Manager
19 * @brief The manager to handle OpenBMC time.
20 * @details It registers various time related settings and properties signals
21 * on DBus and handle the changes.
22 * For certain properties it also notifies the changed events to listeners.
23 */
24class Manager
25{
26 public:
27 friend class TestManager;
Lei YU7f4fca52017-02-23 15:15:51 +080028
Lei YU415b9642017-02-09 11:37:26 +080029 explicit Manager(sdbusplus::bus::bus& bus);
Lei YUc6fe8692017-02-23 15:12:07 +080030 Manager(const Manager&) = delete;
31 Manager& operator=(const Manager&) = delete;
32 Manager(Manager&&) = delete;
33 Manager& operator=(Manager&&) = delete;
Lei YU710d49b2017-08-01 17:10:17 +080034 ~Manager() = default;
Lei YU415b9642017-02-09 11:37:26 +080035
36 /** @brief Add a listener that will be called
37 * when property is changed
38 **/
39 void addListener(PropertyChangeListner* listener);
40
41 private:
42 /** @brief Persistent sdbusplus DBus connection */
43 sdbusplus::bus::bus& bus;
44
45 /** @brief The match of settings property change */
Lei YU710d49b2017-08-01 17:10:17 +080046 std::vector<sdbusplus::bus::match::match> settingsMatches;
47
Lei YUc6fe8692017-02-23 15:12:07 +080048 /** @brief The match of pgood change */
49 sdbusplus::bus::match::match pgoodChangeMatch;
50
Lei YU415b9642017-02-09 11:37:26 +080051 /** @brief The container to hold all the listeners */
52 std::set<PropertyChangeListner*> listeners;
53
Lei YU710d49b2017-08-01 17:10:17 +080054 /** @brief Settings objects of intereset */
55 settings::Objects settings;
56
Lei YUc6fe8692017-02-23 15:12:07 +080057 /** @brief The value to indicate if host is on */
58 bool hostOn = false;
59
Lei YU7f4fca52017-02-23 15:15:51 +080060 /** @brief The requested time mode when host is on*/
61 std::string requestedMode;
62
63 /** @brief The requested time owner when host is on*/
64 std::string requestedOwner;
65
Lei YU415b9642017-02-09 11:37:26 +080066 /** @brief The current time mode */
67 Mode timeMode;
68
69 /** @brief The current time owner */
70 Owner timeOwner;
71
Lei YU7f4fca52017-02-23 15:15:51 +080072 /** @brief Restore saved settings */
73 void restoreSettings();
74
Lei YUc6fe8692017-02-23 15:12:07 +080075 /** @brief Check if host is on and update hostOn variable */
76 void checkHostOn();
77
Lei YU710d49b2017-08-01 17:10:17 +080078 /** @brief Get setting from settingsd service
79 *
80 * @param[in] path - The dbus object path
81 * @param[in] interface - The dbus interface
82 * @param[in] setting - The string of the setting
83 *
84 * @return The setting value in string
85 */
86 std::string getSetting(const char* path,
87 const char* interface,
88 const char* setting) const;
89
Lei YUa5003ce2017-02-24 15:35:25 +080090 /** @brief Set current time mode from the time mode string
91 *
92 * @param[in] mode - The string of time mode
93 *
94 * @return - true if the mode is updated
95 * false if it's the same as before
96 */
97 bool setCurrentTimeMode(const std::string& mode);
98
99 /** @brief Set current time owner from the time owner string
100 *
101 * @param[in] owner - The string of time owner
102 *
103 * @return - true if the owner is updated
104 * false if it's the same as before
105 */
106 bool setCurrentTimeOwner(const std::string& owner);
107
108 /** @brief Called on time mode is changed
109 *
110 * Notify listeners that time mode is changed and update ntp setting
Lei YU415b9642017-02-09 11:37:26 +0800111 *
112 * @param[in] mode - The string of time mode
113 */
Lei YUa5003ce2017-02-24 15:35:25 +0800114 void onTimeModeChanged(const std::string& mode);
Lei YU415b9642017-02-09 11:37:26 +0800115
Lei YUa5003ce2017-02-24 15:35:25 +0800116 /** @brief Called on time owner is changed
Lei YU415b9642017-02-09 11:37:26 +0800117 *
Lei YUa5003ce2017-02-24 15:35:25 +0800118 * Notify listeners that time owner is changed
Lei YU415b9642017-02-09 11:37:26 +0800119 */
Lei YUa5003ce2017-02-24 15:35:25 +0800120 void onTimeOwnerChanged();
Lei YU415b9642017-02-09 11:37:26 +0800121
Lei YU710d49b2017-08-01 17:10:17 +0800122 /** @brief Callback to handle change in a setting
123 *
124 * @param[in] msg - sdbusplus dbusmessage
125 *
126 * @return 0 on success, < 0 on failure.
127 */
128 int onSettingsChanged(sdbusplus::message::message& msg);
129
Lei YU415b9642017-02-09 11:37:26 +0800130 /** @brief Notified on settings property changed
131 *
132 * @param[in] key - The name of property that is changed
133 * @param[in] value - The value of the property
134 */
135 void onPropertyChanged(const std::string& key,
136 const std::string& value);
137
Lei YUc6fe8692017-02-23 15:12:07 +0800138 /** @brief Notified on pgood has changed
139 *
140 * @param[in] pgood - The changed pgood value
141 */
142 void onPgoodChanged(bool pgood);
143
Lei YU7f4fca52017-02-23 15:15:51 +0800144 /** @brief Set the property as requested time mode/owner
145 *
146 * @param[in] key - The property name
147 * @param[in] value - The property value
148 */
149 void setPropertyAsRequested(const std::string& key,
150 const std::string& value);
151
152 /** @brief Set the current mode to user requested one
153 * if conditions allow it
154 *
155 * @param[in] mode - The string of time mode
156 */
157 void setRequestedMode(const std::string& mode);
158
159 /** @brief Set the current owner to user requested one
160 * if conditions allow it
161 *
162 * @param[in] owner - The string of time owner
163 */
164 void setRequestedOwner(const std::string& owner);
165
Lei YUa7417132017-02-23 15:24:05 +0800166 /** @brief Update the NTP setting to systemd time service
167 *
168 * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL"
169 */
170 void updateNtpSetting(const std::string& value);
171
Lei YU415b9642017-02-09 11:37:26 +0800172 /** @brief The static function called on settings property changed
173 *
174 * @param[in] msg - Data associated with subscribed signal
175 * @param[in] userData - Pointer to this object instance
176 * @param[out] retError - Not used but required with signal API
177 */
178 static int onPropertyChanged(sd_bus_message* msg,
179 void* userData,
180 sd_bus_error* retError);
181
Lei YUc6fe8692017-02-23 15:12:07 +0800182 /** @brief Notified on pgood has changed
183 *
184 * @param[in] msg - Data associated with subscribed signal
185 * @param[in] userData - Pointer to this object instance
186 * @param[out] retError - Not used but required with signal API
187 */
188 static int onPgoodChanged(sd_bus_message* msg,
189 void* userData,
190 sd_bus_error* retError);
191
Lei YU7f4fca52017-02-23 15:15:51 +0800192 /** @brief The string of time mode property */
Lei YU710d49b2017-08-01 17:10:17 +0800193 static constexpr auto PROPERTY_TIME_MODE = "TimeSyncMethod";
Lei YU7f4fca52017-02-23 15:15:51 +0800194
195 /** @brief The string of time owner property */
Lei YU710d49b2017-08-01 17:10:17 +0800196 static constexpr auto PROPERTY_TIME_OWNER = "TimeOwner";
Lei YU7f4fca52017-02-23 15:15:51 +0800197
Lei YU415b9642017-02-09 11:37:26 +0800198 using Updater = std::function<void(const std::string&)>;
199
200 /** @brief Map the property string to functions that shall
201 * be called when the property is changed
202 */
203 const std::map<std::string, Updater> propertyUpdaters =
204 {
Lei YU7f4fca52017-02-23 15:15:51 +0800205 {PROPERTY_TIME_MODE, std::bind(&Manager::setCurrentTimeMode,
206 this, std::placeholders::_1)},
207 {PROPERTY_TIME_OWNER, std::bind(&Manager::setCurrentTimeOwner,
208 this, std::placeholders::_1)}
Lei YU415b9642017-02-09 11:37:26 +0800209 };
210
211 /** @brief The properties that manager shall notify the
212 * listeners when changed
213 */
214 static const std::set<std::string> managedProperties;
215
216 /** @brief The map that maps the string to Owners */
217 static const std::map<std::string, Owner> ownerMap;
Lei YU7f4fca52017-02-23 15:15:51 +0800218
219 /** @brief The file name of saved time mode */
220 static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode";
221
222 /** @brief The file name of saved time owner */
223 static constexpr auto ownerFile = "/var/lib/obmc/saved_time_owner";
Lei YU415b9642017-02-09 11:37:26 +0800224};
225
226}
227}