blob: 2d5d131f8548467c744525bdfb7bc60620b9aa36 [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"
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05004
Lei YU415b9642017-02-09 11:37:26 +08005#include "property_change_listener.hpp"
Lei YU710d49b2017-08-01 17:10:17 +08006#include "settings.hpp"
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05007#include "types.hpp"
Lei YU415b9642017-02-09 11:37:26 +08008
9#include <sdbusplus/bus.hpp>
10#include <sdbusplus/bus/match.hpp>
Lei YU415b9642017-02-09 11:37:26 +080011#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{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050027 public:
28 friend class TestManager;
Lei YU7f4fca52017-02-23 15:15:51 +080029
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050030 explicit Manager(sdbusplus::bus::bus& bus);
31 Manager(const Manager&) = delete;
32 Manager& operator=(const Manager&) = delete;
33 Manager(Manager&&) = delete;
34 Manager& operator=(Manager&&) = delete;
35 ~Manager() = default;
Lei YU415b9642017-02-09 11:37:26 +080036
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050037 /** @brief Add a listener that will be called
38 * when property is changed
39 **/
40 void addListener(PropertyChangeListner* listener);
Lei YU415b9642017-02-09 11:37:26 +080041
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050042 private:
43 /** @brief Persistent sdbusplus DBus connection */
44 sdbusplus::bus::bus& bus;
Lei YU415b9642017-02-09 11:37:26 +080045
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050046 /** @brief The match of settings property change */
47 std::vector<sdbusplus::bus::match::match> settingsMatches;
Lei YU710d49b2017-08-01 17:10:17 +080048
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050049 /** @brief The match of host state change */
50 std::unique_ptr<sdbusplus::bus::match::match> hostStateChangeMatch;
Lei YUc6fe8692017-02-23 15:12:07 +080051
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050052 /** @brief The container to hold all the listeners */
53 std::set<PropertyChangeListner*> listeners;
Lei YU415b9642017-02-09 11:37:26 +080054
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050055 /** @brief Settings objects of intereset */
56 settings::Objects settings;
Lei YU710d49b2017-08-01 17:10:17 +080057
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050058 /** @brief The value to indicate if host is on */
59 bool hostOn = false;
Lei YUc6fe8692017-02-23 15:12:07 +080060
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050061 /** @brief The requested time mode when host is on*/
62 std::string requestedMode;
Lei YU7f4fca52017-02-23 15:15:51 +080063
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050064 /** @brief The current time mode */
65 Mode timeMode = DEFAULT_TIME_MODE;
Lei YU415b9642017-02-09 11:37:26 +080066
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050067 /** @brief Restore saved settings */
68 void restoreSettings();
Lei YU7f4fca52017-02-23 15:15:51 +080069
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050070 /** @brief Check if host is on and update hostOn variable */
71 void checkHostOn();
Lei YUc6fe8692017-02-23 15:12:07 +080072
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050073 /** @brief Get setting from settingsd service
74 *
75 * @param[in] path - The dbus object path
76 * @param[in] interface - The dbus interface
77 * @param[in] setting - The string of the setting
78 *
79 * @return The setting value in string
80 */
81 std::string getSetting(const char* path, const char* interface,
82 const char* setting) const;
Lei YU710d49b2017-08-01 17:10:17 +080083
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050084 /** @brief Set current time mode from the time mode string
85 *
86 * @param[in] mode - The string of time mode
87 *
88 * @return - true if the mode is updated
89 * false if it's the same as before
90 */
91 bool setCurrentTimeMode(const std::string& mode);
Lei YUa5003ce2017-02-24 15:35:25 +080092
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050093 /** @brief Called on time mode is changed
94 *
95 * Notify listeners that time mode is changed and update ntp setting
96 *
97 * @param[in] mode - The string of time mode
98 */
99 void onTimeModeChanged(const std::string& mode);
Lei YU415b9642017-02-09 11:37:26 +0800100
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500101 /** @brief Callback to handle change in a setting
102 *
103 * @param[in] msg - sdbusplus dbusmessage
104 *
105 * @return 0 on success, < 0 on failure.
106 */
107 int onSettingsChanged(sdbusplus::message::message& msg);
Lei YU710d49b2017-08-01 17:10:17 +0800108
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500109 /** @brief Notified on settings property changed
110 *
111 * @param[in] key - The name of property that is changed
112 * @param[in] value - The value of the property
113 */
114 void onPropertyChanged(const std::string& key, const std::string& value);
Lei YU415b9642017-02-09 11:37:26 +0800115
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500116 /** @brief Notified on host state has changed
117 *
118 * @param[in] msg - sdbusplus dbusmessage
119 */
120 void onHostStateChanged(sdbusplus::message::message& msg);
Lei YUdebe1d82017-10-13 13:21:37 +0800121
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500122 /** @brief Notified on host state has changed
123 *
124 * @param[in] on - Indicate if the host is on or off
125 */
126 void onHostState(bool on);
Lei YUc6fe8692017-02-23 15:12:07 +0800127
George Liu3c2f4492020-04-12 11:35:57 +0800128 /** @brief Set the property as requested time mode
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500129 *
130 * @param[in] key - The property name
131 * @param[in] value - The property value
132 */
133 void setPropertyAsRequested(const std::string& key,
134 const std::string& value);
Lei YU7f4fca52017-02-23 15:15:51 +0800135
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500136 /** @brief Set the current mode to user requested one
137 * if conditions allow it
138 *
139 * @param[in] mode - The string of time mode
140 */
141 void setRequestedMode(const std::string& mode);
Lei YU7f4fca52017-02-23 15:15:51 +0800142
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500143 /** @brief Update the NTP setting to systemd time service
144 *
145 * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL"
146 */
147 void updateNtpSetting(const std::string& value);
Lei YUa7417132017-02-23 15:24:05 +0800148
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500149 /** @brief The static function called on settings property changed
150 *
151 * @param[in] msg - Data associated with subscribed signal
152 * @param[in] userData - Pointer to this object instance
153 * @param[out] retError - Not used but required with signal API
154 */
155 static int onPropertyChanged(sd_bus_message* msg, void* userData,
156 sd_bus_error* retError);
Lei YU415b9642017-02-09 11:37:26 +0800157
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500158 /** @brief The string of time mode property */
159 static constexpr auto PROPERTY_TIME_MODE = "TimeSyncMethod";
Lei YU7f4fca52017-02-23 15:15:51 +0800160
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500161 using Updater = std::function<void(const std::string&)>;
Lei YU415b9642017-02-09 11:37:26 +0800162
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500163 /** @brief Map the property string to functions that shall
164 * be called when the property is changed
165 */
166 const std::map<std::string, Updater> propertyUpdaters = {
167 {PROPERTY_TIME_MODE,
George Liu3c2f4492020-04-12 11:35:57 +0800168 std::bind(&Manager::setCurrentTimeMode, this, std::placeholders::_1)}};
Lei YU415b9642017-02-09 11:37:26 +0800169
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500170 /** @brief The properties that manager shall notify the
171 * listeners when changed
172 */
173 static const std::set<std::string> managedProperties;
Lei YU415b9642017-02-09 11:37:26 +0800174
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500175 /** @brief The file name of saved time mode */
176 static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode";
Lei YU415b9642017-02-09 11:37:26 +0800177};
178
Gunnar Millsab4cc6a2018-09-14 14:42:39 -0500179} // namespace time
180} // namespace phosphor