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         explicit Manager(sdbusplus::bus::bus& bus);
28 
29         /** @brief Add a listener that will be called
30           * when property is changed
31          **/
32         void addListener(PropertyChangeListner* listener);
33 
34     private:
35         /** @brief Persistent sdbusplus DBus connection */
36         sdbusplus::bus::bus& bus;
37 
38         /** @brief The match of settings property change */
39         sdbusplus::bus::match::match propertyChangeMatch;
40 
41         /** @brief The container to hold all the listeners */
42         std::set<PropertyChangeListner*> listeners;
43 
44         /** @brief The current time mode */
45         Mode timeMode;
46 
47         /** @brief The current time owner */
48         Owner timeOwner;
49 
50         /** @brief Get setting from settingsd service
51          *
52          * @param[in] setting - The string of the setting
53          *
54          * @return The setting value in string
55          */
56         std::string getSettings(const char* setting) const;
57 
58         /** @brief Set current time mode
59          *
60          * @param[in] mode - The string of time mode
61          */
62         void setCurrentTimeMode(const std::string& mode);
63 
64         /** @brief Set current time owner
65          *
66          * @param[in] owner - The string of time owner
67          */
68         void setCurrentTimeOwner(const std::string& owner);
69 
70         /** @brief Notified on settings property changed
71          *
72          * @param[in] key - The name of property that is changed
73          * @param[in] value - The value of the property
74          */
75         void onPropertyChanged(const std::string& key,
76                                const std::string& value);
77 
78         /** @brief The static function called on settings property changed
79          *
80          * @param[in] msg - Data associated with subscribed signal
81          * @param[in] userData - Pointer to this object instance
82          * @param[out] retError  - Not used but required with signal API
83          */
84         static int onPropertyChanged(sd_bus_message* msg,
85                                      void* userData,
86                                      sd_bus_error* retError);
87 
88         /** @brief Convert a string to enum Mode
89          *
90          * Convert the time mode string to enum.
91          * Valid strings are "NTP", "MANUAL"
92          * If it's not a valid time mode string, return NTP.
93          *
94          * @param[in] mode - The string of time mode
95          *
96          * @return The Mode enum
97          */
98         static Mode convertToMode(const std::string& mode);
99 
100         /** @brief Convert a string to enum Owner
101          *
102          * Convert the time owner string to enum.
103          * Valid strings are "BMC", "HOST", "SPLIT", "BOTH"
104          * If it's not a valid time owner string, return BMC.
105          *
106          * @param[in] owner - The string of time owner
107          *
108          * @return The Owner enum
109          */
110         static Owner convertToOwner(const std::string& owner);
111 
112         using Updater = std::function<void(const std::string&)>;
113 
114         /** @brief Map the property string to functions that shall
115          *  be called when the property is changed
116          */
117         const std::map<std::string, Updater> propertyUpdaters =
118         {
119             {"time_mode", std::bind(&Manager::setCurrentTimeMode,
120                                     this, std::placeholders::_1)},
121             {"time_owner", std::bind(&Manager::setCurrentTimeOwner,
122                                      this, std::placeholders::_1)}
123         };
124 
125         /** @brief The properties that manager shall notify the
126          *  listeners when changed
127          */
128         static const std::set<std::string> managedProperties;
129 
130         /** @brief The map that maps the string to Owners */
131         static const std::map<std::string, Owner> ownerMap;
132 };
133 
134 }
135 }
136