xref: /openbmc/phosphor-time-manager/manager.hpp (revision 7f4fca554b250d40230d5735d1d0ddf9ac6af801)
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;
27*7f4fca55SLei 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 
55*7f4fca55SLei YU         /** @brief The requested time mode when host is on*/
56*7f4fca55SLei YU         std::string requestedMode;
57*7f4fca55SLei YU 
58*7f4fca55SLei YU         /** @brief The requested time owner when host is on*/
59*7f4fca55SLei YU         std::string requestedOwner;
60*7f4fca55SLei 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 
67*7f4fca55SLei YU         /** @brief Restore saved settings */
68*7f4fca55SLei YU         void restoreSettings();
69*7f4fca55SLei YU 
70c6fe8693SLei YU         /** @brief Check if host is on and update hostOn variable */
71c6fe8693SLei YU         void checkHostOn();
72c6fe8693SLei YU 
73415b964fSLei YU         /** @brief Get setting from settingsd service
74415b964fSLei YU          *
75415b964fSLei YU          * @param[in] setting - The string of the setting
76415b964fSLei YU          *
77415b964fSLei YU          * @return The setting value in string
78415b964fSLei YU          */
79415b964fSLei YU         std::string getSettings(const char* setting) const;
80415b964fSLei YU 
81415b964fSLei YU         /** @brief Set current time mode
82415b964fSLei YU          *
83415b964fSLei YU          * @param[in] mode - The string of time mode
84415b964fSLei YU          */
85415b964fSLei YU         void setCurrentTimeMode(const std::string& mode);
86415b964fSLei YU 
87415b964fSLei YU         /** @brief Set current time owner
88415b964fSLei YU          *
89415b964fSLei YU          * @param[in] owner - The string of time owner
90415b964fSLei YU          */
91415b964fSLei YU         void setCurrentTimeOwner(const std::string& owner);
92415b964fSLei YU 
93415b964fSLei YU         /** @brief Notified on settings property changed
94415b964fSLei YU          *
95415b964fSLei YU          * @param[in] key - The name of property that is changed
96415b964fSLei YU          * @param[in] value - The value of the property
97415b964fSLei YU          */
98415b964fSLei YU         void onPropertyChanged(const std::string& key,
99415b964fSLei YU                                const std::string& value);
100415b964fSLei YU 
101c6fe8693SLei YU         /** @brief Notified on pgood has changed
102c6fe8693SLei YU          *
103c6fe8693SLei YU          * @param[in] pgood - The changed pgood value
104c6fe8693SLei YU          */
105c6fe8693SLei YU         void onPgoodChanged(bool pgood);
106c6fe8693SLei YU 
107*7f4fca55SLei YU         /** @brief Set the property as requested time mode/owner
108*7f4fca55SLei YU          *
109*7f4fca55SLei YU          * @param[in] key - The property name
110*7f4fca55SLei YU          * @param[in] value - The property value
111*7f4fca55SLei YU          */
112*7f4fca55SLei YU         void setPropertyAsRequested(const std::string& key,
113*7f4fca55SLei YU                                     const std::string& value);
114*7f4fca55SLei YU 
115*7f4fca55SLei YU         /** @brief Set the current mode to user requested one
116*7f4fca55SLei YU          *  if conditions allow it
117*7f4fca55SLei YU          *
118*7f4fca55SLei YU          * @param[in] mode - The string of time mode
119*7f4fca55SLei YU          */
120*7f4fca55SLei YU         void setRequestedMode(const std::string& mode);
121*7f4fca55SLei YU 
122*7f4fca55SLei YU         /** @brief Set the current owner to user requested one
123*7f4fca55SLei YU          *  if conditions allow it
124*7f4fca55SLei YU          *
125*7f4fca55SLei YU          * @param[in] owner - The string of time owner
126*7f4fca55SLei YU          */
127*7f4fca55SLei YU         void setRequestedOwner(const std::string& owner);
128*7f4fca55SLei YU 
129415b964fSLei YU         /** @brief The static function called on settings property changed
130415b964fSLei YU          *
131415b964fSLei YU          * @param[in] msg - Data associated with subscribed signal
132415b964fSLei YU          * @param[in] userData - Pointer to this object instance
133415b964fSLei YU          * @param[out] retError  - Not used but required with signal API
134415b964fSLei YU          */
135415b964fSLei YU         static int onPropertyChanged(sd_bus_message* msg,
136415b964fSLei YU                                      void* userData,
137415b964fSLei YU                                      sd_bus_error* retError);
138415b964fSLei YU 
139c6fe8693SLei YU         /** @brief Notified on pgood has changed
140c6fe8693SLei YU          *
141c6fe8693SLei YU          * @param[in] msg - Data associated with subscribed signal
142c6fe8693SLei YU          * @param[in] userData - Pointer to this object instance
143c6fe8693SLei YU          * @param[out] retError  - Not used but required with signal API
144c6fe8693SLei YU          */
145c6fe8693SLei YU         static int onPgoodChanged(sd_bus_message* msg,
146c6fe8693SLei YU                                   void* userData,
147c6fe8693SLei YU                                   sd_bus_error* retError);
148c6fe8693SLei YU 
149415b964fSLei YU         /** @brief Convert a string to enum Mode
150415b964fSLei YU          *
151415b964fSLei YU          * Convert the time mode string to enum.
152415b964fSLei YU          * Valid strings are "NTP", "MANUAL"
153415b964fSLei YU          * If it's not a valid time mode string, return NTP.
154415b964fSLei YU          *
155415b964fSLei YU          * @param[in] mode - The string of time mode
156415b964fSLei YU          *
157415b964fSLei YU          * @return The Mode enum
158415b964fSLei YU          */
159415b964fSLei YU         static Mode convertToMode(const std::string& mode);
160415b964fSLei YU 
161415b964fSLei YU         /** @brief Convert a string to enum Owner
162415b964fSLei YU          *
163415b964fSLei YU          * Convert the time owner string to enum.
164415b964fSLei YU          * Valid strings are "BMC", "HOST", "SPLIT", "BOTH"
165415b964fSLei YU          * If it's not a valid time owner string, return BMC.
166415b964fSLei YU          *
167415b964fSLei YU          * @param[in] owner - The string of time owner
168415b964fSLei YU          *
169415b964fSLei YU          * @return The Owner enum
170415b964fSLei YU          */
171415b964fSLei YU         static Owner convertToOwner(const std::string& owner);
172415b964fSLei YU 
173*7f4fca55SLei YU         /** @brief The string of time mode property */
174*7f4fca55SLei YU         static constexpr auto PROPERTY_TIME_MODE = "time_mode";
175*7f4fca55SLei YU 
176*7f4fca55SLei YU         /** @brief The string of time owner property */
177*7f4fca55SLei YU         static constexpr auto PROPERTY_TIME_OWNER = "time_owner";
178*7f4fca55SLei YU 
179415b964fSLei YU         using Updater = std::function<void(const std::string&)>;
180415b964fSLei YU 
181415b964fSLei YU         /** @brief Map the property string to functions that shall
182415b964fSLei YU          *  be called when the property is changed
183415b964fSLei YU          */
184415b964fSLei YU         const std::map<std::string, Updater> propertyUpdaters =
185415b964fSLei YU         {
186*7f4fca55SLei YU             {PROPERTY_TIME_MODE, std::bind(&Manager::setCurrentTimeMode,
187415b964fSLei YU                                            this, std::placeholders::_1)},
188*7f4fca55SLei YU             {PROPERTY_TIME_OWNER, std::bind(&Manager::setCurrentTimeOwner,
189415b964fSLei YU                                             this, std::placeholders::_1)}
190415b964fSLei YU         };
191415b964fSLei YU 
192415b964fSLei YU         /** @brief The properties that manager shall notify the
193415b964fSLei YU          *  listeners when changed
194415b964fSLei YU          */
195415b964fSLei YU         static const std::set<std::string> managedProperties;
196415b964fSLei YU 
197415b964fSLei YU         /** @brief The map that maps the string to Owners */
198415b964fSLei YU         static const std::map<std::string, Owner> ownerMap;
199*7f4fca55SLei YU 
200*7f4fca55SLei YU         /** @brief The file name of saved time mode */
201*7f4fca55SLei YU         static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode";
202*7f4fca55SLei YU 
203*7f4fca55SLei YU         /** @brief The file name of saved time owner */
204*7f4fca55SLei YU         static constexpr auto ownerFile = "/var/lib/obmc/saved_time_owner";
205415b964fSLei YU };
206415b964fSLei YU 
207415b964fSLei YU }
208415b964fSLei YU }
209