blob: e84652c0a64b1be46d6f69e9aa23cc498d89a0f8 [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 YUdebe1d82017-10-13 13:21:37 +080048 /** @brief The match of host state change */
49 std::unique_ptr<sdbusplus::bus::match::match> hostStateChangeMatch;
Lei YUc6fe8692017-02-23 15:12:07 +080050
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 YUdebe1d82017-10-13 13:21:37 +0800138 /** @brief Notified on host state has changed
Lei YUc6fe8692017-02-23 15:12:07 +0800139 *
Lei YUdebe1d82017-10-13 13:21:37 +0800140 * @param[in] msg - sdbusplus dbusmessage
Lei YUc6fe8692017-02-23 15:12:07 +0800141 */
Lei YUdebe1d82017-10-13 13:21:37 +0800142 void onHostStateChanged(sdbusplus::message::message& msg);
143
144 /** @brief Notified on host state has changed
145 *
146 * @param[in] on - Indicate if the host is on or off
147 */
148 void onHostState(bool on);
Lei YUc6fe8692017-02-23 15:12:07 +0800149
Lei YU7f4fca52017-02-23 15:15:51 +0800150 /** @brief Set the property as requested time mode/owner
151 *
152 * @param[in] key - The property name
153 * @param[in] value - The property value
154 */
155 void setPropertyAsRequested(const std::string& key,
156 const std::string& value);
157
158 /** @brief Set the current mode to user requested one
159 * if conditions allow it
160 *
161 * @param[in] mode - The string of time mode
162 */
163 void setRequestedMode(const std::string& mode);
164
165 /** @brief Set the current owner to user requested one
166 * if conditions allow it
167 *
168 * @param[in] owner - The string of time owner
169 */
170 void setRequestedOwner(const std::string& owner);
171
Lei YUa7417132017-02-23 15:24:05 +0800172 /** @brief Update the NTP setting to systemd time service
173 *
174 * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL"
175 */
176 void updateNtpSetting(const std::string& value);
177
Lei YU415b9642017-02-09 11:37:26 +0800178 /** @brief The static function called on settings property changed
179 *
180 * @param[in] msg - Data associated with subscribed signal
181 * @param[in] userData - Pointer to this object instance
182 * @param[out] retError - Not used but required with signal API
183 */
184 static int onPropertyChanged(sd_bus_message* msg,
185 void* userData,
186 sd_bus_error* retError);
187
Lei YU7f4fca52017-02-23 15:15:51 +0800188 /** @brief The string of time mode property */
Lei YU710d49b2017-08-01 17:10:17 +0800189 static constexpr auto PROPERTY_TIME_MODE = "TimeSyncMethod";
Lei YU7f4fca52017-02-23 15:15:51 +0800190
191 /** @brief The string of time owner property */
Lei YU710d49b2017-08-01 17:10:17 +0800192 static constexpr auto PROPERTY_TIME_OWNER = "TimeOwner";
Lei YU7f4fca52017-02-23 15:15:51 +0800193
Lei YU415b9642017-02-09 11:37:26 +0800194 using Updater = std::function<void(const std::string&)>;
195
196 /** @brief Map the property string to functions that shall
197 * be called when the property is changed
198 */
199 const std::map<std::string, Updater> propertyUpdaters =
200 {
Lei YU7f4fca52017-02-23 15:15:51 +0800201 {PROPERTY_TIME_MODE, std::bind(&Manager::setCurrentTimeMode,
202 this, std::placeholders::_1)},
203 {PROPERTY_TIME_OWNER, std::bind(&Manager::setCurrentTimeOwner,
204 this, std::placeholders::_1)}
Lei YU415b9642017-02-09 11:37:26 +0800205 };
206
207 /** @brief The properties that manager shall notify the
208 * listeners when changed
209 */
210 static const std::set<std::string> managedProperties;
211
212 /** @brief The map that maps the string to Owners */
213 static const std::map<std::string, Owner> ownerMap;
Lei YU7f4fca52017-02-23 15:15:51 +0800214
215 /** @brief The file name of saved time mode */
216 static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode";
217
218 /** @brief The file name of saved time owner */
219 static constexpr auto ownerFile = "/var/lib/obmc/saved_time_owner";
Lei YU415b9642017-02-09 11:37:26 +0800220};
221
222}
223}