xref: /openbmc/phosphor-time-manager/manager.hpp (revision c6fe8693f72bdc673c25838c4c8a8a39a6cdea8b)
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;
27415b964fSLei YU         explicit Manager(sdbusplus::bus::bus& bus);
28*c6fe8693SLei YU         Manager(const Manager&) = delete;
29*c6fe8693SLei YU         Manager& operator=(const Manager&) = delete;
30*c6fe8693SLei YU         Manager(Manager&&) = delete;
31*c6fe8693SLei YU         Manager& operator=(Manager&&) = delete;
32415b964fSLei YU 
33415b964fSLei YU         /** @brief Add a listener that will be called
34415b964fSLei YU           * when property is changed
35415b964fSLei YU          **/
36415b964fSLei YU         void addListener(PropertyChangeListner* listener);
37415b964fSLei YU 
38415b964fSLei YU     private:
39415b964fSLei YU         /** @brief Persistent sdbusplus DBus connection */
40415b964fSLei YU         sdbusplus::bus::bus& bus;
41415b964fSLei YU 
42415b964fSLei YU         /** @brief The match of settings property change */
43415b964fSLei YU         sdbusplus::bus::match::match propertyChangeMatch;
44415b964fSLei YU 
45*c6fe8693SLei YU         /** @brief The match of pgood change */
46*c6fe8693SLei YU         sdbusplus::bus::match::match pgoodChangeMatch;
47*c6fe8693SLei YU 
48415b964fSLei YU         /** @brief The container to hold all the listeners */
49415b964fSLei YU         std::set<PropertyChangeListner*> listeners;
50415b964fSLei YU 
51*c6fe8693SLei YU         /** @brief The value to indicate if host is on */
52*c6fe8693SLei YU         bool hostOn = false;
53*c6fe8693SLei YU 
54415b964fSLei YU         /** @brief The current time mode */
55415b964fSLei YU         Mode timeMode;
56415b964fSLei YU 
57415b964fSLei YU         /** @brief The current time owner */
58415b964fSLei YU         Owner timeOwner;
59415b964fSLei YU 
60*c6fe8693SLei YU         /** @brief Check if host is on and update hostOn variable */
61*c6fe8693SLei YU         void checkHostOn();
62*c6fe8693SLei YU 
63415b964fSLei YU         /** @brief Get setting from settingsd service
64415b964fSLei YU          *
65415b964fSLei YU          * @param[in] setting - The string of the setting
66415b964fSLei YU          *
67415b964fSLei YU          * @return The setting value in string
68415b964fSLei YU          */
69415b964fSLei YU         std::string getSettings(const char* setting) const;
70415b964fSLei YU 
71415b964fSLei YU         /** @brief Set current time mode
72415b964fSLei YU          *
73415b964fSLei YU          * @param[in] mode - The string of time mode
74415b964fSLei YU          */
75415b964fSLei YU         void setCurrentTimeMode(const std::string& mode);
76415b964fSLei YU 
77415b964fSLei YU         /** @brief Set current time owner
78415b964fSLei YU          *
79415b964fSLei YU          * @param[in] owner - The string of time owner
80415b964fSLei YU          */
81415b964fSLei YU         void setCurrentTimeOwner(const std::string& owner);
82415b964fSLei YU 
83415b964fSLei YU         /** @brief Notified on settings property changed
84415b964fSLei YU          *
85415b964fSLei YU          * @param[in] key - The name of property that is changed
86415b964fSLei YU          * @param[in] value - The value of the property
87415b964fSLei YU          */
88415b964fSLei YU         void onPropertyChanged(const std::string& key,
89415b964fSLei YU                                const std::string& value);
90415b964fSLei YU 
91*c6fe8693SLei YU         /** @brief Notified on pgood has changed
92*c6fe8693SLei YU          *
93*c6fe8693SLei YU          * @param[in] pgood - The changed pgood value
94*c6fe8693SLei YU          */
95*c6fe8693SLei YU         void onPgoodChanged(bool pgood);
96*c6fe8693SLei YU 
97415b964fSLei YU         /** @brief The static function called on settings property changed
98415b964fSLei YU          *
99415b964fSLei YU          * @param[in] msg - Data associated with subscribed signal
100415b964fSLei YU          * @param[in] userData - Pointer to this object instance
101415b964fSLei YU          * @param[out] retError  - Not used but required with signal API
102415b964fSLei YU          */
103415b964fSLei YU         static int onPropertyChanged(sd_bus_message* msg,
104415b964fSLei YU                                      void* userData,
105415b964fSLei YU                                      sd_bus_error* retError);
106415b964fSLei YU 
107*c6fe8693SLei YU         /** @brief Notified on pgood has changed
108*c6fe8693SLei YU          *
109*c6fe8693SLei YU          * @param[in] msg - Data associated with subscribed signal
110*c6fe8693SLei YU          * @param[in] userData - Pointer to this object instance
111*c6fe8693SLei YU          * @param[out] retError  - Not used but required with signal API
112*c6fe8693SLei YU          */
113*c6fe8693SLei YU         static int onPgoodChanged(sd_bus_message* msg,
114*c6fe8693SLei YU                                   void* userData,
115*c6fe8693SLei YU                                   sd_bus_error* retError);
116*c6fe8693SLei YU 
117415b964fSLei YU         /** @brief Convert a string to enum Mode
118415b964fSLei YU          *
119415b964fSLei YU          * Convert the time mode string to enum.
120415b964fSLei YU          * Valid strings are "NTP", "MANUAL"
121415b964fSLei YU          * If it's not a valid time mode string, return NTP.
122415b964fSLei YU          *
123415b964fSLei YU          * @param[in] mode - The string of time mode
124415b964fSLei YU          *
125415b964fSLei YU          * @return The Mode enum
126415b964fSLei YU          */
127415b964fSLei YU         static Mode convertToMode(const std::string& mode);
128415b964fSLei YU 
129415b964fSLei YU         /** @brief Convert a string to enum Owner
130415b964fSLei YU          *
131415b964fSLei YU          * Convert the time owner string to enum.
132415b964fSLei YU          * Valid strings are "BMC", "HOST", "SPLIT", "BOTH"
133415b964fSLei YU          * If it's not a valid time owner string, return BMC.
134415b964fSLei YU          *
135415b964fSLei YU          * @param[in] owner - The string of time owner
136415b964fSLei YU          *
137415b964fSLei YU          * @return The Owner enum
138415b964fSLei YU          */
139415b964fSLei YU         static Owner convertToOwner(const std::string& owner);
140415b964fSLei YU 
141415b964fSLei YU         using Updater = std::function<void(const std::string&)>;
142415b964fSLei YU 
143415b964fSLei YU         /** @brief Map the property string to functions that shall
144415b964fSLei YU          *  be called when the property is changed
145415b964fSLei YU          */
146415b964fSLei YU         const std::map<std::string, Updater> propertyUpdaters =
147415b964fSLei YU         {
148415b964fSLei YU             {"time_mode", std::bind(&Manager::setCurrentTimeMode,
149415b964fSLei YU                                     this, std::placeholders::_1)},
150415b964fSLei YU             {"time_owner", std::bind(&Manager::setCurrentTimeOwner,
151415b964fSLei YU                                      this, std::placeholders::_1)}
152415b964fSLei YU         };
153415b964fSLei YU 
154415b964fSLei YU         /** @brief The properties that manager shall notify the
155415b964fSLei YU          *  listeners when changed
156415b964fSLei YU          */
157415b964fSLei YU         static const std::set<std::string> managedProperties;
158415b964fSLei YU 
159415b964fSLei YU         /** @brief The map that maps the string to Owners */
160415b964fSLei YU         static const std::map<std::string, Owner> ownerMap;
161415b964fSLei YU };
162415b964fSLei YU 
163415b964fSLei YU }
164415b964fSLei YU }
165