xref: /openbmc/phosphor-time-manager/manager.hpp (revision 710d49be2fc33f22e8f4a7ed43774fd9293d083f)
1415b964fSLei YU #pragma once
2415b964fSLei YU 
3415b964fSLei YU #include "types.hpp"
4415b964fSLei YU #include "property_change_listener.hpp"
5*710d49beSLei YU #include "settings.hpp"
6415b964fSLei YU 
7415b964fSLei YU #include <sdbusplus/bus.hpp>
8415b964fSLei YU #include <sdbusplus/bus/match.hpp>
9415b964fSLei YU 
10415b964fSLei YU #include <set>
11415b964fSLei YU #include <string>
12415b964fSLei YU 
13415b964fSLei YU namespace phosphor
14415b964fSLei YU {
15415b964fSLei YU namespace time
16415b964fSLei YU {
17415b964fSLei YU 
18415b964fSLei YU /** @class Manager
19415b964fSLei YU  *  @brief The manager to handle OpenBMC time.
20415b964fSLei YU  *  @details It registers various time related settings and properties signals
21415b964fSLei YU  *  on DBus and handle the changes.
22415b964fSLei YU  *  For certain properties it also notifies the changed events to listeners.
23415b964fSLei YU  */
24415b964fSLei YU class Manager
25415b964fSLei YU {
26415b964fSLei YU     public:
27415b964fSLei YU         friend class TestManager;
287f4fca55SLei YU 
29415b964fSLei YU         explicit Manager(sdbusplus::bus::bus& bus);
30c6fe8693SLei YU         Manager(const Manager&) = delete;
31c6fe8693SLei YU         Manager& operator=(const Manager&) = delete;
32c6fe8693SLei YU         Manager(Manager&&) = delete;
33c6fe8693SLei YU         Manager& operator=(Manager&&) = delete;
34*710d49beSLei YU         ~Manager() = default;
35415b964fSLei YU 
36415b964fSLei YU         /** @brief Add a listener that will be called
37415b964fSLei YU           * when property is changed
38415b964fSLei YU          **/
39415b964fSLei YU         void addListener(PropertyChangeListner* listener);
40415b964fSLei YU 
41415b964fSLei YU     private:
42415b964fSLei YU         /** @brief Persistent sdbusplus DBus connection */
43415b964fSLei YU         sdbusplus::bus::bus& bus;
44415b964fSLei YU 
45415b964fSLei YU         /** @brief The match of settings property change */
46*710d49beSLei YU         // TODO: This is to be removed when all properties are handled in
47*710d49beSLei YU         // new settings daemon
48415b964fSLei YU         sdbusplus::bus::match::match propertyChangeMatch;
49415b964fSLei YU 
50*710d49beSLei YU         /** @brief The match of settings property change */
51*710d49beSLei YU         std::vector<sdbusplus::bus::match::match> settingsMatches;
52*710d49beSLei YU 
53c6fe8693SLei YU         /** @brief The match of pgood change */
54c6fe8693SLei YU         sdbusplus::bus::match::match pgoodChangeMatch;
55c6fe8693SLei YU 
56415b964fSLei YU         /** @brief The container to hold all the listeners */
57415b964fSLei YU         std::set<PropertyChangeListner*> listeners;
58415b964fSLei YU 
59*710d49beSLei YU         /** @brief Settings objects of intereset */
60*710d49beSLei YU         settings::Objects settings;
61*710d49beSLei YU 
62c6fe8693SLei YU         /** @brief The value to indicate if host is on */
63c6fe8693SLei YU         bool hostOn = false;
64c6fe8693SLei YU 
657f4fca55SLei YU         /** @brief The requested time mode when host is on*/
667f4fca55SLei YU         std::string requestedMode;
677f4fca55SLei YU 
687f4fca55SLei YU         /** @brief The requested time owner when host is on*/
697f4fca55SLei YU         std::string requestedOwner;
707f4fca55SLei YU 
71415b964fSLei YU         /** @brief The current time mode */
72415b964fSLei YU         Mode timeMode;
73415b964fSLei YU 
74415b964fSLei YU         /** @brief The current time owner */
75415b964fSLei YU         Owner timeOwner;
76415b964fSLei YU 
777f4fca55SLei YU         /** @brief Restore saved settings */
787f4fca55SLei YU         void restoreSettings();
797f4fca55SLei YU 
80c6fe8693SLei YU         /** @brief Check if host is on and update hostOn variable */
81c6fe8693SLei YU         void checkHostOn();
82c6fe8693SLei YU 
83a741713cSLei YU         /** @brief Check if use_dhcp_ntp is used and update NTP setting */
84a741713cSLei YU         void checkDhcpNtp();
85a741713cSLei YU 
86415b964fSLei YU         /** @brief Get setting from settingsd service
87415b964fSLei YU          *
88415b964fSLei YU          * @param[in] setting - The string of the setting
89415b964fSLei YU          *
90415b964fSLei YU          * @return The setting value in string
91415b964fSLei YU          */
92415b964fSLei YU         std::string getSettings(const char* setting) const;
93415b964fSLei YU 
94*710d49beSLei YU         /** @brief Get setting from settingsd service
95*710d49beSLei YU          *
96*710d49beSLei YU          * @param[in] path - The dbus object path
97*710d49beSLei YU          * @param[in] interface - The dbus interface
98*710d49beSLei YU          * @param[in] setting - The string of the setting
99*710d49beSLei YU          *
100*710d49beSLei YU          * @return The setting value in string
101*710d49beSLei YU          */
102*710d49beSLei YU         std::string getSetting(const char* path,
103*710d49beSLei YU                                const char* interface,
104*710d49beSLei YU                                const char* setting) const;
105*710d49beSLei YU 
106a5003cebSLei YU         /** @brief Set current time mode from the time mode string
107a5003cebSLei YU          *
108a5003cebSLei YU          * @param[in] mode - The string of time mode
109a5003cebSLei YU          *
110a5003cebSLei YU          * @return - true if the mode is updated
111a5003cebSLei YU          *           false if it's the same as before
112a5003cebSLei YU          */
113a5003cebSLei YU         bool setCurrentTimeMode(const std::string& mode);
114a5003cebSLei YU 
115a5003cebSLei YU         /** @brief Set current time owner from the time owner string
116a5003cebSLei YU          *
117a5003cebSLei YU          * @param[in] owner - The string of time owner
118a5003cebSLei YU          *
119a5003cebSLei YU          * @return - true if the owner is updated
120a5003cebSLei YU          *           false if it's the same as before
121a5003cebSLei YU          */
122a5003cebSLei YU         bool setCurrentTimeOwner(const std::string& owner);
123a5003cebSLei YU 
124a5003cebSLei YU         /** @brief Called on time mode is changed
125a5003cebSLei YU          *
126a5003cebSLei YU          * Notify listeners that time mode is changed and update ntp setting
127415b964fSLei YU          *
128415b964fSLei YU          * @param[in] mode - The string of time mode
129415b964fSLei YU          */
130a5003cebSLei YU         void onTimeModeChanged(const std::string& mode);
131415b964fSLei YU 
132a5003cebSLei YU         /** @brief Called on time owner is changed
133415b964fSLei YU          *
134a5003cebSLei YU          * Notify listeners that time owner is changed
135415b964fSLei YU          */
136a5003cebSLei YU         void onTimeOwnerChanged();
137415b964fSLei YU 
138*710d49beSLei YU         /** @brief Callback to handle change in a setting
139*710d49beSLei YU          *
140*710d49beSLei YU          *  @param[in] msg - sdbusplus dbusmessage
141*710d49beSLei YU          *
142*710d49beSLei YU          *  @return 0 on success, < 0 on failure.
143*710d49beSLei YU          */
144*710d49beSLei YU         int onSettingsChanged(sdbusplus::message::message& msg);
145*710d49beSLei YU 
146415b964fSLei YU         /** @brief Notified on settings property changed
147415b964fSLei YU          *
148415b964fSLei YU          * @param[in] key - The name of property that is changed
149415b964fSLei YU          * @param[in] value - The value of the property
150415b964fSLei YU          */
151415b964fSLei YU         void onPropertyChanged(const std::string& key,
152415b964fSLei YU                                const std::string& value);
153415b964fSLei YU 
154c6fe8693SLei YU         /** @brief Notified on pgood has changed
155c6fe8693SLei YU          *
156c6fe8693SLei YU          * @param[in] pgood - The changed pgood value
157c6fe8693SLei YU          */
158c6fe8693SLei YU         void onPgoodChanged(bool pgood);
159c6fe8693SLei YU 
1607f4fca55SLei YU         /** @brief Set the property as requested time mode/owner
1617f4fca55SLei YU          *
1627f4fca55SLei YU          * @param[in] key - The property name
1637f4fca55SLei YU          * @param[in] value - The property value
1647f4fca55SLei YU          */
1657f4fca55SLei YU         void setPropertyAsRequested(const std::string& key,
1667f4fca55SLei YU                                     const std::string& value);
1677f4fca55SLei YU 
1687f4fca55SLei YU         /** @brief Set the current mode to user requested one
1697f4fca55SLei YU          *  if conditions allow it
1707f4fca55SLei YU          *
1717f4fca55SLei YU          * @param[in] mode - The string of time mode
1727f4fca55SLei YU          */
1737f4fca55SLei YU         void setRequestedMode(const std::string& mode);
1747f4fca55SLei YU 
1757f4fca55SLei YU         /** @brief Set the current owner to user requested one
1767f4fca55SLei YU          *  if conditions allow it
1777f4fca55SLei YU          *
1787f4fca55SLei YU          * @param[in] owner - The string of time owner
1797f4fca55SLei YU          */
1807f4fca55SLei YU         void setRequestedOwner(const std::string& owner);
1817f4fca55SLei YU 
182a741713cSLei YU         /** @brief Update the NTP setting to systemd time service
183a741713cSLei YU          *
184a741713cSLei YU          * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL"
185a741713cSLei YU          */
186a741713cSLei YU         void updateNtpSetting(const std::string& value);
187a741713cSLei YU 
188a741713cSLei YU         /** @brief Update dhcp_ntp setting to OpenBMC network service
189a741713cSLei YU          *
190a741713cSLei YU          * @param[in] value - The use_dhcp_ntp value, e.g. "yes" or "no"
191a741713cSLei YU          */
192a741713cSLei YU         void updateDhcpNtpSetting(const std::string& useDhcpNtp);
193a741713cSLei YU 
194415b964fSLei YU         /** @brief The static function called on settings property changed
195415b964fSLei YU          *
196415b964fSLei YU          * @param[in] msg - Data associated with subscribed signal
197415b964fSLei YU          * @param[in] userData - Pointer to this object instance
198415b964fSLei YU          * @param[out] retError  - Not used but required with signal API
199415b964fSLei YU          */
200415b964fSLei YU         static int onPropertyChanged(sd_bus_message* msg,
201415b964fSLei YU                                      void* userData,
202415b964fSLei YU                                      sd_bus_error* retError);
203415b964fSLei YU 
204c6fe8693SLei YU         /** @brief Notified on pgood has changed
205c6fe8693SLei YU          *
206c6fe8693SLei YU          * @param[in] msg - Data associated with subscribed signal
207c6fe8693SLei YU          * @param[in] userData - Pointer to this object instance
208c6fe8693SLei YU          * @param[out] retError  - Not used but required with signal API
209c6fe8693SLei YU          */
210c6fe8693SLei YU         static int onPgoodChanged(sd_bus_message* msg,
211c6fe8693SLei YU                                   void* userData,
212c6fe8693SLei YU                                   sd_bus_error* retError);
213c6fe8693SLei YU 
2147f4fca55SLei YU         /** @brief The string of time mode property */
215*710d49beSLei YU         static constexpr auto PROPERTY_TIME_MODE = "TimeSyncMethod";
2167f4fca55SLei YU 
2177f4fca55SLei YU         /** @brief The string of time owner property */
218*710d49beSLei YU         static constexpr auto PROPERTY_TIME_OWNER = "TimeOwner";
2197f4fca55SLei YU 
220a741713cSLei YU         /** @brief The string of use dhcp ntp property */
221a741713cSLei YU         static constexpr auto PROPERTY_DHCP_NTP = "use_dhcp_ntp";
222a741713cSLei YU 
223415b964fSLei YU         using Updater = std::function<void(const std::string&)>;
224415b964fSLei YU 
225415b964fSLei YU         /** @brief Map the property string to functions that shall
226415b964fSLei YU          *  be called when the property is changed
227415b964fSLei YU          */
228415b964fSLei YU         const std::map<std::string, Updater> propertyUpdaters =
229415b964fSLei YU         {
2307f4fca55SLei YU             {PROPERTY_TIME_MODE, std::bind(&Manager::setCurrentTimeMode,
231415b964fSLei YU                                            this, std::placeholders::_1)},
2327f4fca55SLei YU             {PROPERTY_TIME_OWNER, std::bind(&Manager::setCurrentTimeOwner,
233415b964fSLei YU                                             this, std::placeholders::_1)}
234415b964fSLei YU         };
235415b964fSLei YU 
236415b964fSLei YU         /** @brief The properties that manager shall notify the
237415b964fSLei YU          *  listeners when changed
238415b964fSLei YU          */
239415b964fSLei YU         static const std::set<std::string> managedProperties;
240415b964fSLei YU 
241415b964fSLei YU         /** @brief The map that maps the string to Owners */
242415b964fSLei YU         static const std::map<std::string, Owner> ownerMap;
2437f4fca55SLei YU 
2447f4fca55SLei YU         /** @brief The file name of saved time mode */
2457f4fca55SLei YU         static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode";
2467f4fca55SLei YU 
2477f4fca55SLei YU         /** @brief The file name of saved time owner */
2487f4fca55SLei YU         static constexpr auto ownerFile = "/var/lib/obmc/saved_time_owner";
249415b964fSLei YU };
250415b964fSLei YU 
251415b964fSLei YU }
252415b964fSLei YU }
253