xref: /openbmc/phosphor-time-manager/manager.hpp (revision a741713c3edcf19569d02bce1c76c53f76a61e13)
1415b964fSLei YU #pragma once
2415b964fSLei YU 
3415b964fSLei YU #include "types.hpp"
4415b964fSLei YU #include "property_change_listener.hpp"
5415b964fSLei YU 
6415b964fSLei YU #include <sdbusplus/bus.hpp>
7415b964fSLei YU #include <sdbusplus/bus/match.hpp>
8415b964fSLei YU 
9415b964fSLei YU #include <set>
10415b964fSLei YU #include <string>
11415b964fSLei YU 
12415b964fSLei YU namespace phosphor
13415b964fSLei YU {
14415b964fSLei YU namespace time
15415b964fSLei YU {
16415b964fSLei YU 
17415b964fSLei YU /** @class Manager
18415b964fSLei YU  *  @brief The manager to handle OpenBMC time.
19415b964fSLei YU  *  @details It registers various time related settings and properties signals
20415b964fSLei YU  *  on DBus and handle the changes.
21415b964fSLei YU  *  For certain properties it also notifies the changed events to listeners.
22415b964fSLei YU  */
23415b964fSLei YU class Manager
24415b964fSLei YU {
25415b964fSLei YU     public:
26415b964fSLei YU         friend class TestManager;
277f4fca55SLei YU 
28415b964fSLei YU         explicit Manager(sdbusplus::bus::bus& bus);
29c6fe8693SLei YU         Manager(const Manager&) = delete;
30c6fe8693SLei YU         Manager& operator=(const Manager&) = delete;
31c6fe8693SLei YU         Manager(Manager&&) = delete;
32c6fe8693SLei YU         Manager& operator=(Manager&&) = delete;
33415b964fSLei YU 
34415b964fSLei YU         /** @brief Add a listener that will be called
35415b964fSLei YU           * when property is changed
36415b964fSLei YU          **/
37415b964fSLei YU         void addListener(PropertyChangeListner* listener);
38415b964fSLei YU 
39415b964fSLei YU     private:
40415b964fSLei YU         /** @brief Persistent sdbusplus DBus connection */
41415b964fSLei YU         sdbusplus::bus::bus& bus;
42415b964fSLei YU 
43415b964fSLei YU         /** @brief The match of settings property change */
44415b964fSLei YU         sdbusplus::bus::match::match propertyChangeMatch;
45415b964fSLei YU 
46c6fe8693SLei YU         /** @brief The match of pgood change */
47c6fe8693SLei YU         sdbusplus::bus::match::match pgoodChangeMatch;
48c6fe8693SLei YU 
49415b964fSLei YU         /** @brief The container to hold all the listeners */
50415b964fSLei YU         std::set<PropertyChangeListner*> listeners;
51415b964fSLei YU 
52c6fe8693SLei YU         /** @brief The value to indicate if host is on */
53c6fe8693SLei YU         bool hostOn = false;
54c6fe8693SLei YU 
557f4fca55SLei YU         /** @brief The requested time mode when host is on*/
567f4fca55SLei YU         std::string requestedMode;
577f4fca55SLei YU 
587f4fca55SLei YU         /** @brief The requested time owner when host is on*/
597f4fca55SLei YU         std::string requestedOwner;
607f4fca55SLei YU 
61415b964fSLei YU         /** @brief The current time mode */
62415b964fSLei YU         Mode timeMode;
63415b964fSLei YU 
64415b964fSLei YU         /** @brief The current time owner */
65415b964fSLei YU         Owner timeOwner;
66415b964fSLei YU 
677f4fca55SLei YU         /** @brief Restore saved settings */
687f4fca55SLei YU         void restoreSettings();
697f4fca55SLei YU 
70c6fe8693SLei YU         /** @brief Check if host is on and update hostOn variable */
71c6fe8693SLei YU         void checkHostOn();
72c6fe8693SLei YU 
73*a741713cSLei YU         /** @brief Check if use_dhcp_ntp is used and update NTP setting */
74*a741713cSLei YU         void checkDhcpNtp();
75*a741713cSLei YU 
76415b964fSLei YU         /** @brief Get setting from settingsd service
77415b964fSLei YU          *
78415b964fSLei YU          * @param[in] setting - The string of the setting
79415b964fSLei YU          *
80415b964fSLei YU          * @return The setting value in string
81415b964fSLei YU          */
82415b964fSLei YU         std::string getSettings(const char* setting) const;
83415b964fSLei YU 
84415b964fSLei YU         /** @brief Set current time mode
85415b964fSLei YU          *
86415b964fSLei YU          * @param[in] mode - The string of time mode
87415b964fSLei YU          */
88415b964fSLei YU         void setCurrentTimeMode(const std::string& mode);
89415b964fSLei YU 
90415b964fSLei YU         /** @brief Set current time owner
91415b964fSLei YU          *
92415b964fSLei YU          * @param[in] owner - The string of time owner
93415b964fSLei YU          */
94415b964fSLei YU         void setCurrentTimeOwner(const std::string& owner);
95415b964fSLei YU 
96415b964fSLei YU         /** @brief Notified on settings property changed
97415b964fSLei YU          *
98415b964fSLei YU          * @param[in] key - The name of property that is changed
99415b964fSLei YU          * @param[in] value - The value of the property
100415b964fSLei YU          */
101415b964fSLei YU         void onPropertyChanged(const std::string& key,
102415b964fSLei YU                                const std::string& value);
103415b964fSLei YU 
104c6fe8693SLei YU         /** @brief Notified on pgood has changed
105c6fe8693SLei YU          *
106c6fe8693SLei YU          * @param[in] pgood - The changed pgood value
107c6fe8693SLei YU          */
108c6fe8693SLei YU         void onPgoodChanged(bool pgood);
109c6fe8693SLei YU 
1107f4fca55SLei YU         /** @brief Set the property as requested time mode/owner
1117f4fca55SLei YU          *
1127f4fca55SLei YU          * @param[in] key - The property name
1137f4fca55SLei YU          * @param[in] value - The property value
1147f4fca55SLei YU          */
1157f4fca55SLei YU         void setPropertyAsRequested(const std::string& key,
1167f4fca55SLei YU                                     const std::string& value);
1177f4fca55SLei YU 
1187f4fca55SLei YU         /** @brief Set the current mode to user requested one
1197f4fca55SLei YU          *  if conditions allow it
1207f4fca55SLei YU          *
1217f4fca55SLei YU          * @param[in] mode - The string of time mode
1227f4fca55SLei YU          */
1237f4fca55SLei YU         void setRequestedMode(const std::string& mode);
1247f4fca55SLei YU 
1257f4fca55SLei YU         /** @brief Set the current owner to user requested one
1267f4fca55SLei YU          *  if conditions allow it
1277f4fca55SLei YU          *
1287f4fca55SLei YU          * @param[in] owner - The string of time owner
1297f4fca55SLei YU          */
1307f4fca55SLei YU         void setRequestedOwner(const std::string& owner);
1317f4fca55SLei YU 
132*a741713cSLei YU         /** @brief Update the NTP setting to systemd time service
133*a741713cSLei YU          *
134*a741713cSLei YU          * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL"
135*a741713cSLei YU          */
136*a741713cSLei YU         void updateNtpSetting(const std::string& value);
137*a741713cSLei YU 
138*a741713cSLei YU         /** @brief Update dhcp_ntp setting to OpenBMC network service
139*a741713cSLei YU          *
140*a741713cSLei YU          * @param[in] value - The use_dhcp_ntp value, e.g. "yes" or "no"
141*a741713cSLei YU          */
142*a741713cSLei YU         void updateDhcpNtpSetting(const std::string& useDhcpNtp);
143*a741713cSLei YU 
144415b964fSLei YU         /** @brief The static function called on settings property changed
145415b964fSLei YU          *
146415b964fSLei YU          * @param[in] msg - Data associated with subscribed signal
147415b964fSLei YU          * @param[in] userData - Pointer to this object instance
148415b964fSLei YU          * @param[out] retError  - Not used but required with signal API
149415b964fSLei YU          */
150415b964fSLei YU         static int onPropertyChanged(sd_bus_message* msg,
151415b964fSLei YU                                      void* userData,
152415b964fSLei YU                                      sd_bus_error* retError);
153415b964fSLei YU 
154c6fe8693SLei YU         /** @brief Notified on pgood has changed
155c6fe8693SLei YU          *
156c6fe8693SLei YU          * @param[in] msg - Data associated with subscribed signal
157c6fe8693SLei YU          * @param[in] userData - Pointer to this object instance
158c6fe8693SLei YU          * @param[out] retError  - Not used but required with signal API
159c6fe8693SLei YU          */
160c6fe8693SLei YU         static int onPgoodChanged(sd_bus_message* msg,
161c6fe8693SLei YU                                   void* userData,
162c6fe8693SLei YU                                   sd_bus_error* retError);
163c6fe8693SLei YU 
164415b964fSLei YU         /** @brief Convert a string to enum Mode
165415b964fSLei YU          *
166415b964fSLei YU          * Convert the time mode string to enum.
167415b964fSLei YU          * Valid strings are "NTP", "MANUAL"
168415b964fSLei YU          * If it's not a valid time mode string, return NTP.
169415b964fSLei YU          *
170415b964fSLei YU          * @param[in] mode - The string of time mode
171415b964fSLei YU          *
172415b964fSLei YU          * @return The Mode enum
173415b964fSLei YU          */
174415b964fSLei YU         static Mode convertToMode(const std::string& mode);
175415b964fSLei YU 
176415b964fSLei YU         /** @brief Convert a string to enum Owner
177415b964fSLei YU          *
178415b964fSLei YU          * Convert the time owner string to enum.
179415b964fSLei YU          * Valid strings are "BMC", "HOST", "SPLIT", "BOTH"
180415b964fSLei YU          * If it's not a valid time owner string, return BMC.
181415b964fSLei YU          *
182415b964fSLei YU          * @param[in] owner - The string of time owner
183415b964fSLei YU          *
184415b964fSLei YU          * @return The Owner enum
185415b964fSLei YU          */
186415b964fSLei YU         static Owner convertToOwner(const std::string& owner);
187415b964fSLei YU 
1887f4fca55SLei YU         /** @brief The string of time mode property */
1897f4fca55SLei YU         static constexpr auto PROPERTY_TIME_MODE = "time_mode";
1907f4fca55SLei YU 
1917f4fca55SLei YU         /** @brief The string of time owner property */
1927f4fca55SLei YU         static constexpr auto PROPERTY_TIME_OWNER = "time_owner";
1937f4fca55SLei YU 
194*a741713cSLei YU         /** @brief The string of use dhcp ntp property */
195*a741713cSLei YU         static constexpr auto PROPERTY_DHCP_NTP = "use_dhcp_ntp";
196*a741713cSLei YU 
197415b964fSLei YU         using Updater = std::function<void(const std::string&)>;
198415b964fSLei YU 
199415b964fSLei YU         /** @brief Map the property string to functions that shall
200415b964fSLei YU          *  be called when the property is changed
201415b964fSLei YU          */
202415b964fSLei YU         const std::map<std::string, Updater> propertyUpdaters =
203415b964fSLei YU         {
2047f4fca55SLei YU             {PROPERTY_TIME_MODE, std::bind(&Manager::setCurrentTimeMode,
205415b964fSLei YU                                            this, std::placeholders::_1)},
2067f4fca55SLei YU             {PROPERTY_TIME_OWNER, std::bind(&Manager::setCurrentTimeOwner,
207415b964fSLei YU                                             this, std::placeholders::_1)}
208415b964fSLei YU         };
209415b964fSLei YU 
210415b964fSLei YU         /** @brief The properties that manager shall notify the
211415b964fSLei YU          *  listeners when changed
212415b964fSLei YU          */
213415b964fSLei YU         static const std::set<std::string> managedProperties;
214415b964fSLei YU 
215415b964fSLei YU         /** @brief The map that maps the string to Owners */
216415b964fSLei YU         static const std::map<std::string, Owner> ownerMap;
2177f4fca55SLei YU 
2187f4fca55SLei YU         /** @brief The file name of saved time mode */
2197f4fca55SLei YU         static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode";
2207f4fca55SLei YU 
2217f4fca55SLei YU         /** @brief The file name of saved time owner */
2227f4fca55SLei YU         static constexpr auto ownerFile = "/var/lib/obmc/saved_time_owner";
223415b964fSLei YU };
224415b964fSLei YU 
225415b964fSLei YU }
226415b964fSLei YU }
227