xref: /openbmc/phosphor-time-manager/manager.hpp (revision 415b964f063f81cd8ec3d1ffc3955be51de06690)
1*415b964fSLei YU #pragma once
2*415b964fSLei YU 
3*415b964fSLei YU #include "types.hpp"
4*415b964fSLei YU #include "property_change_listener.hpp"
5*415b964fSLei YU 
6*415b964fSLei YU #include <sdbusplus/bus.hpp>
7*415b964fSLei YU #include <sdbusplus/bus/match.hpp>
8*415b964fSLei YU 
9*415b964fSLei YU #include <set>
10*415b964fSLei YU #include <string>
11*415b964fSLei YU 
12*415b964fSLei YU namespace phosphor
13*415b964fSLei YU {
14*415b964fSLei YU namespace time
15*415b964fSLei YU {
16*415b964fSLei YU 
17*415b964fSLei YU /** @class Manager
18*415b964fSLei YU  *  @brief The manager to handle OpenBMC time.
19*415b964fSLei YU  *  @details It registers various time related settings and properties signals
20*415b964fSLei YU  *  on DBus and handle the changes.
21*415b964fSLei YU  *  For certain properties it also notifies the changed events to listeners.
22*415b964fSLei YU  */
23*415b964fSLei YU class Manager
24*415b964fSLei YU {
25*415b964fSLei YU     public:
26*415b964fSLei YU         friend class TestManager;
27*415b964fSLei YU         explicit Manager(sdbusplus::bus::bus& bus);
28*415b964fSLei YU 
29*415b964fSLei YU         /** @brief Add a listener that will be called
30*415b964fSLei YU           * when property is changed
31*415b964fSLei YU          **/
32*415b964fSLei YU         void addListener(PropertyChangeListner* listener);
33*415b964fSLei YU 
34*415b964fSLei YU     private:
35*415b964fSLei YU         /** @brief Persistent sdbusplus DBus connection */
36*415b964fSLei YU         sdbusplus::bus::bus& bus;
37*415b964fSLei YU 
38*415b964fSLei YU         /** @brief The match of settings property change */
39*415b964fSLei YU         sdbusplus::bus::match::match propertyChangeMatch;
40*415b964fSLei YU 
41*415b964fSLei YU         /** @brief The container to hold all the listeners */
42*415b964fSLei YU         std::set<PropertyChangeListner*> listeners;
43*415b964fSLei YU 
44*415b964fSLei YU         /** @brief The current time mode */
45*415b964fSLei YU         Mode timeMode;
46*415b964fSLei YU 
47*415b964fSLei YU         /** @brief The current time owner */
48*415b964fSLei YU         Owner timeOwner;
49*415b964fSLei YU 
50*415b964fSLei YU         /** @brief Get setting from settingsd service
51*415b964fSLei YU          *
52*415b964fSLei YU          * @param[in] setting - The string of the setting
53*415b964fSLei YU          *
54*415b964fSLei YU          * @return The setting value in string
55*415b964fSLei YU          */
56*415b964fSLei YU         std::string getSettings(const char* setting) const;
57*415b964fSLei YU 
58*415b964fSLei YU         /** @brief Set current time mode
59*415b964fSLei YU          *
60*415b964fSLei YU          * @param[in] mode - The string of time mode
61*415b964fSLei YU          */
62*415b964fSLei YU         void setCurrentTimeMode(const std::string& mode);
63*415b964fSLei YU 
64*415b964fSLei YU         /** @brief Set current time owner
65*415b964fSLei YU          *
66*415b964fSLei YU          * @param[in] owner - The string of time owner
67*415b964fSLei YU          */
68*415b964fSLei YU         void setCurrentTimeOwner(const std::string& owner);
69*415b964fSLei YU 
70*415b964fSLei YU         /** @brief Notified on settings property changed
71*415b964fSLei YU          *
72*415b964fSLei YU          * @param[in] key - The name of property that is changed
73*415b964fSLei YU          * @param[in] value - The value of the property
74*415b964fSLei YU          */
75*415b964fSLei YU         void onPropertyChanged(const std::string& key,
76*415b964fSLei YU                                const std::string& value);
77*415b964fSLei YU 
78*415b964fSLei YU         /** @brief The static function called on settings property changed
79*415b964fSLei YU          *
80*415b964fSLei YU          * @param[in] msg - Data associated with subscribed signal
81*415b964fSLei YU          * @param[in] userData - Pointer to this object instance
82*415b964fSLei YU          * @param[out] retError  - Not used but required with signal API
83*415b964fSLei YU          */
84*415b964fSLei YU         static int onPropertyChanged(sd_bus_message* msg,
85*415b964fSLei YU                                      void* userData,
86*415b964fSLei YU                                      sd_bus_error* retError);
87*415b964fSLei YU 
88*415b964fSLei YU         /** @brief Convert a string to enum Mode
89*415b964fSLei YU          *
90*415b964fSLei YU          * Convert the time mode string to enum.
91*415b964fSLei YU          * Valid strings are "NTP", "MANUAL"
92*415b964fSLei YU          * If it's not a valid time mode string, return NTP.
93*415b964fSLei YU          *
94*415b964fSLei YU          * @param[in] mode - The string of time mode
95*415b964fSLei YU          *
96*415b964fSLei YU          * @return The Mode enum
97*415b964fSLei YU          */
98*415b964fSLei YU         static Mode convertToMode(const std::string& mode);
99*415b964fSLei YU 
100*415b964fSLei YU         /** @brief Convert a string to enum Owner
101*415b964fSLei YU          *
102*415b964fSLei YU          * Convert the time owner string to enum.
103*415b964fSLei YU          * Valid strings are "BMC", "HOST", "SPLIT", "BOTH"
104*415b964fSLei YU          * If it's not a valid time owner string, return BMC.
105*415b964fSLei YU          *
106*415b964fSLei YU          * @param[in] owner - The string of time owner
107*415b964fSLei YU          *
108*415b964fSLei YU          * @return The Owner enum
109*415b964fSLei YU          */
110*415b964fSLei YU         static Owner convertToOwner(const std::string& owner);
111*415b964fSLei YU 
112*415b964fSLei YU         using Updater = std::function<void(const std::string&)>;
113*415b964fSLei YU 
114*415b964fSLei YU         /** @brief Map the property string to functions that shall
115*415b964fSLei YU          *  be called when the property is changed
116*415b964fSLei YU          */
117*415b964fSLei YU         const std::map<std::string, Updater> propertyUpdaters =
118*415b964fSLei YU         {
119*415b964fSLei YU             {"time_mode", std::bind(&Manager::setCurrentTimeMode,
120*415b964fSLei YU                                     this, std::placeholders::_1)},
121*415b964fSLei YU             {"time_owner", std::bind(&Manager::setCurrentTimeOwner,
122*415b964fSLei YU                                      this, std::placeholders::_1)}
123*415b964fSLei YU         };
124*415b964fSLei YU 
125*415b964fSLei YU         /** @brief The properties that manager shall notify the
126*415b964fSLei YU          *  listeners when changed
127*415b964fSLei YU          */
128*415b964fSLei YU         static const std::set<std::string> managedProperties;
129*415b964fSLei YU 
130*415b964fSLei YU         /** @brief The map that maps the string to Owners */
131*415b964fSLei YU         static const std::map<std::string, Owner> ownerMap;
132*415b964fSLei YU };
133*415b964fSLei YU 
134*415b964fSLei YU }
135*415b964fSLei YU }
136