blob: 3256f594466a6d48c1865ee19b87fa7c6ef17c81 [file] [log] [blame]
Lei YU415b9642017-02-09 11:37:26 +08001#pragma once
2
Lei YU1cd42482018-07-24 10:40:59 +08003#include "config.h"
Lei YU415b9642017-02-09 11:37:26 +08004#include "types.hpp"
5#include "property_change_listener.hpp"
Lei YU710d49b2017-08-01 17:10:17 +08006#include "settings.hpp"
Lei YU415b9642017-02-09 11:37:26 +08007
8#include <sdbusplus/bus.hpp>
9#include <sdbusplus/bus/match.hpp>
10
11#include <set>
12#include <string>
13
14namespace phosphor
15{
16namespace time
17{
18
19/** @class Manager
20 * @brief The manager to handle OpenBMC time.
21 * @details It registers various time related settings and properties signals
22 * on DBus and handle the changes.
23 * For certain properties it also notifies the changed events to listeners.
24 */
25class Manager
26{
27 public:
28 friend class TestManager;
Lei YU7f4fca52017-02-23 15:15:51 +080029
Lei YU415b9642017-02-09 11:37:26 +080030 explicit Manager(sdbusplus::bus::bus& bus);
Lei YUc6fe8692017-02-23 15:12:07 +080031 Manager(const Manager&) = delete;
32 Manager& operator=(const Manager&) = delete;
33 Manager(Manager&&) = delete;
34 Manager& operator=(Manager&&) = delete;
Lei YU710d49b2017-08-01 17:10:17 +080035 ~Manager() = default;
Lei YU415b9642017-02-09 11:37:26 +080036
37 /** @brief Add a listener that will be called
38 * when property is changed
39 **/
40 void addListener(PropertyChangeListner* listener);
41
42 private:
43 /** @brief Persistent sdbusplus DBus connection */
44 sdbusplus::bus::bus& bus;
45
46 /** @brief The match of settings property change */
Lei YU710d49b2017-08-01 17:10:17 +080047 std::vector<sdbusplus::bus::match::match> settingsMatches;
48
Lei YUdebe1d82017-10-13 13:21:37 +080049 /** @brief The match of host state change */
50 std::unique_ptr<sdbusplus::bus::match::match> hostStateChangeMatch;
Lei YUc6fe8692017-02-23 15:12:07 +080051
Lei YU415b9642017-02-09 11:37:26 +080052 /** @brief The container to hold all the listeners */
53 std::set<PropertyChangeListner*> listeners;
54
Lei YU710d49b2017-08-01 17:10:17 +080055 /** @brief Settings objects of intereset */
56 settings::Objects settings;
57
Lei YUc6fe8692017-02-23 15:12:07 +080058 /** @brief The value to indicate if host is on */
59 bool hostOn = false;
60
Lei YU7f4fca52017-02-23 15:15:51 +080061 /** @brief The requested time mode when host is on*/
62 std::string requestedMode;
63
64 /** @brief The requested time owner when host is on*/
65 std::string requestedOwner;
66
Lei YU415b9642017-02-09 11:37:26 +080067 /** @brief The current time mode */
Lei YU1cd42482018-07-24 10:40:59 +080068 Mode timeMode = DEFAULT_TIME_MODE;
Lei YU415b9642017-02-09 11:37:26 +080069
70 /** @brief The current time owner */
Lei YU1cd42482018-07-24 10:40:59 +080071 Owner timeOwner = DEFAULT_TIME_OWNER;
Lei YU415b9642017-02-09 11:37:26 +080072
Lei YU7f4fca52017-02-23 15:15:51 +080073 /** @brief Restore saved settings */
74 void restoreSettings();
75
Lei YUc6fe8692017-02-23 15:12:07 +080076 /** @brief Check if host is on and update hostOn variable */
77 void checkHostOn();
78
Lei YU710d49b2017-08-01 17:10:17 +080079 /** @brief Get setting from settingsd service
80 *
81 * @param[in] path - The dbus object path
82 * @param[in] interface - The dbus interface
83 * @param[in] setting - The string of the setting
84 *
85 * @return The setting value in string
86 */
87 std::string getSetting(const char* path,
88 const char* interface,
89 const char* setting) const;
90
Lei YUa5003ce2017-02-24 15:35:25 +080091 /** @brief Set current time mode from the time mode string
92 *
93 * @param[in] mode - The string of time mode
94 *
95 * @return - true if the mode is updated
96 * false if it's the same as before
97 */
98 bool setCurrentTimeMode(const std::string& mode);
99
100 /** @brief Set current time owner from the time owner string
101 *
102 * @param[in] owner - The string of time owner
103 *
104 * @return - true if the owner is updated
105 * false if it's the same as before
106 */
107 bool setCurrentTimeOwner(const std::string& owner);
108
109 /** @brief Called on time mode is changed
110 *
111 * Notify listeners that time mode is changed and update ntp setting
Lei YU415b9642017-02-09 11:37:26 +0800112 *
113 * @param[in] mode - The string of time mode
114 */
Lei YUa5003ce2017-02-24 15:35:25 +0800115 void onTimeModeChanged(const std::string& mode);
Lei YU415b9642017-02-09 11:37:26 +0800116
Lei YUa5003ce2017-02-24 15:35:25 +0800117 /** @brief Called on time owner is changed
Lei YU415b9642017-02-09 11:37:26 +0800118 *
Lei YUa5003ce2017-02-24 15:35:25 +0800119 * Notify listeners that time owner is changed
Lei YU415b9642017-02-09 11:37:26 +0800120 */
Lei YUa5003ce2017-02-24 15:35:25 +0800121 void onTimeOwnerChanged();
Lei YU415b9642017-02-09 11:37:26 +0800122
Lei YU710d49b2017-08-01 17:10:17 +0800123 /** @brief Callback to handle change in a setting
124 *
125 * @param[in] msg - sdbusplus dbusmessage
126 *
127 * @return 0 on success, < 0 on failure.
128 */
129 int onSettingsChanged(sdbusplus::message::message& msg);
130
Lei YU415b9642017-02-09 11:37:26 +0800131 /** @brief Notified on settings property changed
132 *
133 * @param[in] key - The name of property that is changed
134 * @param[in] value - The value of the property
135 */
136 void onPropertyChanged(const std::string& key,
137 const std::string& value);
138
Lei YUdebe1d82017-10-13 13:21:37 +0800139 /** @brief Notified on host state has changed
Lei YUc6fe8692017-02-23 15:12:07 +0800140 *
Lei YUdebe1d82017-10-13 13:21:37 +0800141 * @param[in] msg - sdbusplus dbusmessage
Lei YUc6fe8692017-02-23 15:12:07 +0800142 */
Lei YUdebe1d82017-10-13 13:21:37 +0800143 void onHostStateChanged(sdbusplus::message::message& msg);
144
145 /** @brief Notified on host state has changed
146 *
147 * @param[in] on - Indicate if the host is on or off
148 */
149 void onHostState(bool on);
Lei YUc6fe8692017-02-23 15:12:07 +0800150
Lei YU7f4fca52017-02-23 15:15:51 +0800151 /** @brief Set the property as requested time mode/owner
152 *
153 * @param[in] key - The property name
154 * @param[in] value - The property value
155 */
156 void setPropertyAsRequested(const std::string& key,
157 const std::string& value);
158
159 /** @brief Set the current mode to user requested one
160 * if conditions allow it
161 *
162 * @param[in] mode - The string of time mode
163 */
164 void setRequestedMode(const std::string& mode);
165
166 /** @brief Set the current owner to user requested one
167 * if conditions allow it
168 *
169 * @param[in] owner - The string of time owner
170 */
171 void setRequestedOwner(const std::string& owner);
172
Lei YUa7417132017-02-23 15:24:05 +0800173 /** @brief Update the NTP setting to systemd time service
174 *
175 * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL"
176 */
177 void updateNtpSetting(const std::string& value);
178
Lei YU415b9642017-02-09 11:37:26 +0800179 /** @brief The static function called on settings property changed
180 *
181 * @param[in] msg - Data associated with subscribed signal
182 * @param[in] userData - Pointer to this object instance
183 * @param[out] retError - Not used but required with signal API
184 */
185 static int onPropertyChanged(sd_bus_message* msg,
186 void* userData,
187 sd_bus_error* retError);
188
Lei YU7f4fca52017-02-23 15:15:51 +0800189 /** @brief The string of time mode property */
Lei YU710d49b2017-08-01 17:10:17 +0800190 static constexpr auto PROPERTY_TIME_MODE = "TimeSyncMethod";
Lei YU7f4fca52017-02-23 15:15:51 +0800191
192 /** @brief The string of time owner property */
Lei YU710d49b2017-08-01 17:10:17 +0800193 static constexpr auto PROPERTY_TIME_OWNER = "TimeOwner";
Lei YU7f4fca52017-02-23 15:15:51 +0800194
Lei YU415b9642017-02-09 11:37:26 +0800195 using Updater = std::function<void(const std::string&)>;
196
197 /** @brief Map the property string to functions that shall
198 * be called when the property is changed
199 */
200 const std::map<std::string, Updater> propertyUpdaters =
201 {
Lei YU7f4fca52017-02-23 15:15:51 +0800202 {PROPERTY_TIME_MODE, std::bind(&Manager::setCurrentTimeMode,
203 this, std::placeholders::_1)},
204 {PROPERTY_TIME_OWNER, std::bind(&Manager::setCurrentTimeOwner,
205 this, std::placeholders::_1)}
Lei YU415b9642017-02-09 11:37:26 +0800206 };
207
208 /** @brief The properties that manager shall notify the
209 * listeners when changed
210 */
211 static const std::set<std::string> managedProperties;
212
213 /** @brief The map that maps the string to Owners */
214 static const std::map<std::string, Owner> ownerMap;
Lei YU7f4fca52017-02-23 15:15:51 +0800215
216 /** @brief The file name of saved time mode */
217 static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode";
218
219 /** @brief The file name of saved time owner */
220 static constexpr auto ownerFile = "/var/lib/obmc/saved_time_owner";
Lei YU415b9642017-02-09 11:37:26 +0800221};
222
223}
224}