xref: /openbmc/phosphor-time-manager/manager.hpp (revision 1cd4248d4afa664b94411dd57e7e686f944dfafc)
1415b964fSLei YU #pragma once
2415b964fSLei YU 
3*1cd4248dSLei YU #include "config.h"
4415b964fSLei YU #include "types.hpp"
5415b964fSLei YU #include "property_change_listener.hpp"
6710d49beSLei YU #include "settings.hpp"
7415b964fSLei YU 
8415b964fSLei YU #include <sdbusplus/bus.hpp>
9415b964fSLei YU #include <sdbusplus/bus/match.hpp>
10415b964fSLei YU 
11415b964fSLei YU #include <set>
12415b964fSLei YU #include <string>
13415b964fSLei YU 
14415b964fSLei YU namespace phosphor
15415b964fSLei YU {
16415b964fSLei YU namespace time
17415b964fSLei YU {
18415b964fSLei YU 
19415b964fSLei YU /** @class Manager
20415b964fSLei YU  *  @brief The manager to handle OpenBMC time.
21415b964fSLei YU  *  @details It registers various time related settings and properties signals
22415b964fSLei YU  *  on DBus and handle the changes.
23415b964fSLei YU  *  For certain properties it also notifies the changed events to listeners.
24415b964fSLei YU  */
25415b964fSLei YU class Manager
26415b964fSLei YU {
27415b964fSLei YU     public:
28415b964fSLei YU         friend class TestManager;
297f4fca55SLei YU 
30415b964fSLei YU         explicit Manager(sdbusplus::bus::bus& bus);
31c6fe8693SLei YU         Manager(const Manager&) = delete;
32c6fe8693SLei YU         Manager& operator=(const Manager&) = delete;
33c6fe8693SLei YU         Manager(Manager&&) = delete;
34c6fe8693SLei YU         Manager& operator=(Manager&&) = delete;
35710d49beSLei YU         ~Manager() = default;
36415b964fSLei YU 
37415b964fSLei YU         /** @brief Add a listener that will be called
38415b964fSLei YU           * when property is changed
39415b964fSLei YU          **/
40415b964fSLei YU         void addListener(PropertyChangeListner* listener);
41415b964fSLei YU 
42415b964fSLei YU     private:
43415b964fSLei YU         /** @brief Persistent sdbusplus DBus connection */
44415b964fSLei YU         sdbusplus::bus::bus& bus;
45415b964fSLei YU 
46415b964fSLei YU         /** @brief The match of settings property change */
47710d49beSLei YU         std::vector<sdbusplus::bus::match::match> settingsMatches;
48710d49beSLei YU 
49debe1d8fSLei YU         /** @brief The match of host state change */
50debe1d8fSLei YU         std::unique_ptr<sdbusplus::bus::match::match> hostStateChangeMatch;
51c6fe8693SLei YU 
52415b964fSLei YU         /** @brief The container to hold all the listeners */
53415b964fSLei YU         std::set<PropertyChangeListner*> listeners;
54415b964fSLei YU 
55710d49beSLei YU         /** @brief Settings objects of intereset */
56710d49beSLei YU         settings::Objects settings;
57710d49beSLei YU 
58c6fe8693SLei YU         /** @brief The value to indicate if host is on */
59c6fe8693SLei YU         bool hostOn = false;
60c6fe8693SLei YU 
617f4fca55SLei YU         /** @brief The requested time mode when host is on*/
627f4fca55SLei YU         std::string requestedMode;
637f4fca55SLei YU 
647f4fca55SLei YU         /** @brief The requested time owner when host is on*/
657f4fca55SLei YU         std::string requestedOwner;
667f4fca55SLei YU 
67415b964fSLei YU         /** @brief The current time mode */
68*1cd4248dSLei YU         Mode timeMode = DEFAULT_TIME_MODE;
69415b964fSLei YU 
70415b964fSLei YU         /** @brief The current time owner */
71*1cd4248dSLei YU         Owner timeOwner = DEFAULT_TIME_OWNER;
72415b964fSLei YU 
737f4fca55SLei YU         /** @brief Restore saved settings */
747f4fca55SLei YU         void restoreSettings();
757f4fca55SLei YU 
76c6fe8693SLei YU         /** @brief Check if host is on and update hostOn variable */
77c6fe8693SLei YU         void checkHostOn();
78c6fe8693SLei YU 
79710d49beSLei YU         /** @brief Get setting from settingsd service
80710d49beSLei YU          *
81710d49beSLei YU          * @param[in] path - The dbus object path
82710d49beSLei YU          * @param[in] interface - The dbus interface
83710d49beSLei YU          * @param[in] setting - The string of the setting
84710d49beSLei YU          *
85710d49beSLei YU          * @return The setting value in string
86710d49beSLei YU          */
87710d49beSLei YU         std::string getSetting(const char* path,
88710d49beSLei YU                                const char* interface,
89710d49beSLei YU                                const char* setting) const;
90710d49beSLei YU 
91a5003cebSLei YU         /** @brief Set current time mode from the time mode string
92a5003cebSLei YU          *
93a5003cebSLei YU          * @param[in] mode - The string of time mode
94a5003cebSLei YU          *
95a5003cebSLei YU          * @return - true if the mode is updated
96a5003cebSLei YU          *           false if it's the same as before
97a5003cebSLei YU          */
98a5003cebSLei YU         bool setCurrentTimeMode(const std::string& mode);
99a5003cebSLei YU 
100a5003cebSLei YU         /** @brief Set current time owner from the time owner string
101a5003cebSLei YU          *
102a5003cebSLei YU          * @param[in] owner - The string of time owner
103a5003cebSLei YU          *
104a5003cebSLei YU          * @return - true if the owner is updated
105a5003cebSLei YU          *           false if it's the same as before
106a5003cebSLei YU          */
107a5003cebSLei YU         bool setCurrentTimeOwner(const std::string& owner);
108a5003cebSLei YU 
109a5003cebSLei YU         /** @brief Called on time mode is changed
110a5003cebSLei YU          *
111a5003cebSLei YU          * Notify listeners that time mode is changed and update ntp setting
112415b964fSLei YU          *
113415b964fSLei YU          * @param[in] mode - The string of time mode
114415b964fSLei YU          */
115a5003cebSLei YU         void onTimeModeChanged(const std::string& mode);
116415b964fSLei YU 
117a5003cebSLei YU         /** @brief Called on time owner is changed
118415b964fSLei YU          *
119a5003cebSLei YU          * Notify listeners that time owner is changed
120415b964fSLei YU          */
121a5003cebSLei YU         void onTimeOwnerChanged();
122415b964fSLei YU 
123710d49beSLei YU         /** @brief Callback to handle change in a setting
124710d49beSLei YU          *
125710d49beSLei YU          *  @param[in] msg - sdbusplus dbusmessage
126710d49beSLei YU          *
127710d49beSLei YU          *  @return 0 on success, < 0 on failure.
128710d49beSLei YU          */
129710d49beSLei YU         int onSettingsChanged(sdbusplus::message::message& msg);
130710d49beSLei YU 
131415b964fSLei YU         /** @brief Notified on settings property changed
132415b964fSLei YU          *
133415b964fSLei YU          * @param[in] key - The name of property that is changed
134415b964fSLei YU          * @param[in] value - The value of the property
135415b964fSLei YU          */
136415b964fSLei YU         void onPropertyChanged(const std::string& key,
137415b964fSLei YU                                const std::string& value);
138415b964fSLei YU 
139debe1d8fSLei YU         /** @brief Notified on host state has changed
140c6fe8693SLei YU          *
141debe1d8fSLei YU          * @param[in] msg - sdbusplus dbusmessage
142c6fe8693SLei YU          */
143debe1d8fSLei YU         void onHostStateChanged(sdbusplus::message::message& msg);
144debe1d8fSLei YU 
145debe1d8fSLei YU         /** @brief Notified on host state has changed
146debe1d8fSLei YU          *
147debe1d8fSLei YU          * @param[in] on - Indicate if the host is on or off
148debe1d8fSLei YU          */
149debe1d8fSLei YU         void onHostState(bool on);
150c6fe8693SLei YU 
1517f4fca55SLei YU         /** @brief Set the property as requested time mode/owner
1527f4fca55SLei YU          *
1537f4fca55SLei YU          * @param[in] key - The property name
1547f4fca55SLei YU          * @param[in] value - The property value
1557f4fca55SLei YU          */
1567f4fca55SLei YU         void setPropertyAsRequested(const std::string& key,
1577f4fca55SLei YU                                     const std::string& value);
1587f4fca55SLei YU 
1597f4fca55SLei YU         /** @brief Set the current mode to user requested one
1607f4fca55SLei YU          *  if conditions allow it
1617f4fca55SLei YU          *
1627f4fca55SLei YU          * @param[in] mode - The string of time mode
1637f4fca55SLei YU          */
1647f4fca55SLei YU         void setRequestedMode(const std::string& mode);
1657f4fca55SLei YU 
1667f4fca55SLei YU         /** @brief Set the current owner to user requested one
1677f4fca55SLei YU          *  if conditions allow it
1687f4fca55SLei YU          *
1697f4fca55SLei YU          * @param[in] owner - The string of time owner
1707f4fca55SLei YU          */
1717f4fca55SLei YU         void setRequestedOwner(const std::string& owner);
1727f4fca55SLei YU 
173a741713cSLei YU         /** @brief Update the NTP setting to systemd time service
174a741713cSLei YU          *
175a741713cSLei YU          * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL"
176a741713cSLei YU          */
177a741713cSLei YU         void updateNtpSetting(const std::string& value);
178a741713cSLei YU 
179415b964fSLei YU         /** @brief The static function called on settings property changed
180415b964fSLei YU          *
181415b964fSLei YU          * @param[in] msg - Data associated with subscribed signal
182415b964fSLei YU          * @param[in] userData - Pointer to this object instance
183415b964fSLei YU          * @param[out] retError  - Not used but required with signal API
184415b964fSLei YU          */
185415b964fSLei YU         static int onPropertyChanged(sd_bus_message* msg,
186415b964fSLei YU                                      void* userData,
187415b964fSLei YU                                      sd_bus_error* retError);
188415b964fSLei YU 
1897f4fca55SLei YU         /** @brief The string of time mode property */
190710d49beSLei YU         static constexpr auto PROPERTY_TIME_MODE = "TimeSyncMethod";
1917f4fca55SLei YU 
1927f4fca55SLei YU         /** @brief The string of time owner property */
193710d49beSLei YU         static constexpr auto PROPERTY_TIME_OWNER = "TimeOwner";
1947f4fca55SLei YU 
195415b964fSLei YU         using Updater = std::function<void(const std::string&)>;
196415b964fSLei YU 
197415b964fSLei YU         /** @brief Map the property string to functions that shall
198415b964fSLei YU          *  be called when the property is changed
199415b964fSLei YU          */
200415b964fSLei YU         const std::map<std::string, Updater> propertyUpdaters =
201415b964fSLei YU         {
2027f4fca55SLei YU             {PROPERTY_TIME_MODE, std::bind(&Manager::setCurrentTimeMode,
203415b964fSLei YU                                            this, std::placeholders::_1)},
2047f4fca55SLei YU             {PROPERTY_TIME_OWNER, std::bind(&Manager::setCurrentTimeOwner,
205415b964fSLei YU                                             this, std::placeholders::_1)}
206415b964fSLei YU         };
207415b964fSLei YU 
208415b964fSLei YU         /** @brief The properties that manager shall notify the
209415b964fSLei YU          *  listeners when changed
210415b964fSLei YU          */
211415b964fSLei YU         static const std::set<std::string> managedProperties;
212415b964fSLei YU 
213415b964fSLei YU         /** @brief The map that maps the string to Owners */
214415b964fSLei YU         static const std::map<std::string, Owner> ownerMap;
2157f4fca55SLei YU 
2167f4fca55SLei YU         /** @brief The file name of saved time mode */
2177f4fca55SLei YU         static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode";
2187f4fca55SLei YU 
2197f4fca55SLei YU         /** @brief The file name of saved time owner */
2207f4fca55SLei YU         static constexpr auto ownerFile = "/var/lib/obmc/saved_time_owner";
221415b964fSLei YU };
222415b964fSLei YU 
223415b964fSLei YU }
224415b964fSLei YU }
225