1 #pragma once
2 
3 #include "types.hpp"
4 #include "property_change_listener.hpp"
5 
6 #include <sdbusplus/bus.hpp>
7 #include <sdbusplus/bus/match.hpp>
8 
9 #include <set>
10 #include <string>
11 
12 namespace phosphor
13 {
14 namespace time
15 {
16 
17 /** @class Manager
18  *  @brief The manager to handle OpenBMC time.
19  *  @details It registers various time related settings and properties signals
20  *  on DBus and handle the changes.
21  *  For certain properties it also notifies the changed events to listeners.
22  */
23 class Manager
24 {
25     public:
26         friend class TestManager;
27 
28         explicit Manager(sdbusplus::bus::bus& bus);
29         Manager(const Manager&) = delete;
30         Manager& operator=(const Manager&) = delete;
31         Manager(Manager&&) = delete;
32         Manager& operator=(Manager&&) = delete;
33 
34         /** @brief Add a listener that will be called
35           * when property is changed
36          **/
37         void addListener(PropertyChangeListner* listener);
38 
39     private:
40         /** @brief Persistent sdbusplus DBus connection */
41         sdbusplus::bus::bus& bus;
42 
43         /** @brief The match of settings property change */
44         sdbusplus::bus::match::match propertyChangeMatch;
45 
46         /** @brief The match of pgood change */
47         sdbusplus::bus::match::match pgoodChangeMatch;
48 
49         /** @brief The container to hold all the listeners */
50         std::set<PropertyChangeListner*> listeners;
51 
52         /** @brief The value to indicate if host is on */
53         bool hostOn = false;
54 
55         /** @brief The requested time mode when host is on*/
56         std::string requestedMode;
57 
58         /** @brief The requested time owner when host is on*/
59         std::string requestedOwner;
60 
61         /** @brief The current time mode */
62         Mode timeMode;
63 
64         /** @brief The current time owner */
65         Owner timeOwner;
66 
67         /** @brief Restore saved settings */
68         void restoreSettings();
69 
70         /** @brief Check if host is on and update hostOn variable */
71         void checkHostOn();
72 
73         /** @brief Get setting from settingsd service
74          *
75          * @param[in] setting - The string of the setting
76          *
77          * @return The setting value in string
78          */
79         std::string getSettings(const char* setting) const;
80 
81         /** @brief Set current time mode
82          *
83          * @param[in] mode - The string of time mode
84          */
85         void setCurrentTimeMode(const std::string& mode);
86 
87         /** @brief Set current time owner
88          *
89          * @param[in] owner - The string of time owner
90          */
91         void setCurrentTimeOwner(const std::string& owner);
92 
93         /** @brief Notified on settings property changed
94          *
95          * @param[in] key - The name of property that is changed
96          * @param[in] value - The value of the property
97          */
98         void onPropertyChanged(const std::string& key,
99                                const std::string& value);
100 
101         /** @brief Notified on pgood has changed
102          *
103          * @param[in] pgood - The changed pgood value
104          */
105         void onPgoodChanged(bool pgood);
106 
107         /** @brief Set the property as requested time mode/owner
108          *
109          * @param[in] key - The property name
110          * @param[in] value - The property value
111          */
112         void setPropertyAsRequested(const std::string& key,
113                                     const std::string& value);
114 
115         /** @brief Set the current mode to user requested one
116          *  if conditions allow it
117          *
118          * @param[in] mode - The string of time mode
119          */
120         void setRequestedMode(const std::string& mode);
121 
122         /** @brief Set the current owner to user requested one
123          *  if conditions allow it
124          *
125          * @param[in] owner - The string of time owner
126          */
127         void setRequestedOwner(const std::string& owner);
128 
129         /** @brief The static function called on settings property changed
130          *
131          * @param[in] msg - Data associated with subscribed signal
132          * @param[in] userData - Pointer to this object instance
133          * @param[out] retError  - Not used but required with signal API
134          */
135         static int onPropertyChanged(sd_bus_message* msg,
136                                      void* userData,
137                                      sd_bus_error* retError);
138 
139         /** @brief Notified on pgood has changed
140          *
141          * @param[in] msg - Data associated with subscribed signal
142          * @param[in] userData - Pointer to this object instance
143          * @param[out] retError  - Not used but required with signal API
144          */
145         static int onPgoodChanged(sd_bus_message* msg,
146                                   void* userData,
147                                   sd_bus_error* retError);
148 
149         /** @brief Convert a string to enum Mode
150          *
151          * Convert the time mode string to enum.
152          * Valid strings are "NTP", "MANUAL"
153          * If it's not a valid time mode string, return NTP.
154          *
155          * @param[in] mode - The string of time mode
156          *
157          * @return The Mode enum
158          */
159         static Mode convertToMode(const std::string& mode);
160 
161         /** @brief Convert a string to enum Owner
162          *
163          * Convert the time owner string to enum.
164          * Valid strings are "BMC", "HOST", "SPLIT", "BOTH"
165          * If it's not a valid time owner string, return BMC.
166          *
167          * @param[in] owner - The string of time owner
168          *
169          * @return The Owner enum
170          */
171         static Owner convertToOwner(const std::string& owner);
172 
173         /** @brief The string of time mode property */
174         static constexpr auto PROPERTY_TIME_MODE = "time_mode";
175 
176         /** @brief The string of time owner property */
177         static constexpr auto PROPERTY_TIME_OWNER = "time_owner";
178 
179         using Updater = std::function<void(const std::string&)>;
180 
181         /** @brief Map the property string to functions that shall
182          *  be called when the property is changed
183          */
184         const std::map<std::string, Updater> propertyUpdaters =
185         {
186             {PROPERTY_TIME_MODE, std::bind(&Manager::setCurrentTimeMode,
187                                            this, std::placeholders::_1)},
188             {PROPERTY_TIME_OWNER, std::bind(&Manager::setCurrentTimeOwner,
189                                             this, std::placeholders::_1)}
190         };
191 
192         /** @brief The properties that manager shall notify the
193          *  listeners when changed
194          */
195         static const std::set<std::string> managedProperties;
196 
197         /** @brief The map that maps the string to Owners */
198         static const std::map<std::string, Owner> ownerMap;
199 
200         /** @brief The file name of saved time mode */
201         static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode";
202 
203         /** @brief The file name of saved time owner */
204         static constexpr auto ownerFile = "/var/lib/obmc/saved_time_owner";
205 };
206 
207 }
208 }
209