xref: /openbmc/phosphor-time-manager/manager.hpp (revision ab4cc6a5585436a29b120429abaa48b416f8edb7)
1415b964fSLei YU #pragma once
2415b964fSLei YU 
31cd4248dSLei YU #include "config.h"
4*ab4cc6a5SGunnar Mills 
5415b964fSLei YU #include "property_change_listener.hpp"
6710d49beSLei YU #include "settings.hpp"
7*ab4cc6a5SGunnar Mills #include "types.hpp"
8415b964fSLei YU 
9415b964fSLei YU #include <sdbusplus/bus.hpp>
10415b964fSLei YU #include <sdbusplus/bus/match.hpp>
11415b964fSLei YU #include <set>
12415b964fSLei YU #include <string>
13415b964fSLei YU 
14415b964fSLei YU namespace phosphor
15415b964fSLei YU {
16415b964fSLei YU namespace time
17415b964fSLei YU {
18415b964fSLei YU 
19415b964fSLei YU /** @class Manager
20415b964fSLei YU  *  @brief The manager to handle OpenBMC time.
21415b964fSLei YU  *  @details It registers various time related settings and properties signals
22415b964fSLei YU  *  on DBus and handle the changes.
23415b964fSLei YU  *  For certain properties it also notifies the changed events to listeners.
24415b964fSLei YU  */
25415b964fSLei YU class Manager
26415b964fSLei YU {
27415b964fSLei YU   public:
28415b964fSLei YU     friend class TestManager;
297f4fca55SLei YU 
30415b964fSLei YU     explicit Manager(sdbusplus::bus::bus& bus);
31c6fe8693SLei YU     Manager(const Manager&) = delete;
32c6fe8693SLei YU     Manager& operator=(const Manager&) = delete;
33c6fe8693SLei YU     Manager(Manager&&) = delete;
34c6fe8693SLei YU     Manager& operator=(Manager&&) = delete;
35710d49beSLei YU     ~Manager() = default;
36415b964fSLei YU 
37415b964fSLei YU     /** @brief Add a listener that will be called
38415b964fSLei YU      * when property is changed
39415b964fSLei YU      **/
40415b964fSLei YU     void addListener(PropertyChangeListner* listener);
41415b964fSLei YU 
42415b964fSLei YU   private:
43415b964fSLei YU     /** @brief Persistent sdbusplus DBus connection */
44415b964fSLei YU     sdbusplus::bus::bus& bus;
45415b964fSLei YU 
46415b964fSLei YU     /** @brief The match of settings property change */
47710d49beSLei YU     std::vector<sdbusplus::bus::match::match> settingsMatches;
48710d49beSLei YU 
49debe1d8fSLei YU     /** @brief The match of host state change */
50debe1d8fSLei YU     std::unique_ptr<sdbusplus::bus::match::match> hostStateChangeMatch;
51c6fe8693SLei YU 
52415b964fSLei YU     /** @brief The container to hold all the listeners */
53415b964fSLei YU     std::set<PropertyChangeListner*> listeners;
54415b964fSLei YU 
55710d49beSLei YU     /** @brief Settings objects of intereset */
56710d49beSLei YU     settings::Objects settings;
57710d49beSLei YU 
58c6fe8693SLei YU     /** @brief The value to indicate if host is on */
59c6fe8693SLei YU     bool hostOn = false;
60c6fe8693SLei YU 
617f4fca55SLei YU     /** @brief The requested time mode when host is on*/
627f4fca55SLei YU     std::string requestedMode;
637f4fca55SLei YU 
647f4fca55SLei YU     /** @brief The requested time owner when host is on*/
657f4fca55SLei YU     std::string requestedOwner;
667f4fca55SLei YU 
67415b964fSLei YU     /** @brief The current time mode */
681cd4248dSLei YU     Mode timeMode = DEFAULT_TIME_MODE;
69415b964fSLei YU 
70415b964fSLei YU     /** @brief The current time owner */
711cd4248dSLei YU     Owner timeOwner = DEFAULT_TIME_OWNER;
72415b964fSLei YU 
737f4fca55SLei YU     /** @brief Restore saved settings */
747f4fca55SLei YU     void restoreSettings();
757f4fca55SLei YU 
76c6fe8693SLei YU     /** @brief Check if host is on and update hostOn variable */
77c6fe8693SLei YU     void checkHostOn();
78c6fe8693SLei YU 
79710d49beSLei YU     /** @brief Get setting from settingsd service
80710d49beSLei YU      *
81710d49beSLei YU      * @param[in] path - The dbus object path
82710d49beSLei YU      * @param[in] interface - The dbus interface
83710d49beSLei YU      * @param[in] setting - The string of the setting
84710d49beSLei YU      *
85710d49beSLei YU      * @return The setting value in string
86710d49beSLei YU      */
87*ab4cc6a5SGunnar Mills     std::string getSetting(const char* path, const char* interface,
88710d49beSLei YU                            const char* setting) const;
89710d49beSLei YU 
90a5003cebSLei YU     /** @brief Set current time mode from the time mode string
91a5003cebSLei YU      *
92a5003cebSLei YU      * @param[in] mode - The string of time mode
93a5003cebSLei YU      *
94a5003cebSLei YU      * @return - true if the mode is updated
95a5003cebSLei YU      *           false if it's the same as before
96a5003cebSLei YU      */
97a5003cebSLei YU     bool setCurrentTimeMode(const std::string& mode);
98a5003cebSLei YU 
99a5003cebSLei YU     /** @brief Set current time owner from the time owner string
100a5003cebSLei YU      *
101a5003cebSLei YU      * @param[in] owner - The string of time owner
102a5003cebSLei YU      *
103a5003cebSLei YU      * @return - true if the owner is updated
104a5003cebSLei YU      *           false if it's the same as before
105a5003cebSLei YU      */
106a5003cebSLei YU     bool setCurrentTimeOwner(const std::string& owner);
107a5003cebSLei YU 
108a5003cebSLei YU     /** @brief Called on time mode is changed
109a5003cebSLei YU      *
110a5003cebSLei YU      * Notify listeners that time mode is changed and update ntp setting
111415b964fSLei YU      *
112415b964fSLei YU      * @param[in] mode - The string of time mode
113415b964fSLei YU      */
114a5003cebSLei YU     void onTimeModeChanged(const std::string& mode);
115415b964fSLei YU 
116a5003cebSLei YU     /** @brief Called on time owner is changed
117415b964fSLei YU      *
118a5003cebSLei YU      * Notify listeners that time owner is changed
119415b964fSLei YU      */
120a5003cebSLei YU     void onTimeOwnerChanged();
121415b964fSLei YU 
122710d49beSLei YU     /** @brief Callback to handle change in a setting
123710d49beSLei YU      *
124710d49beSLei YU      *  @param[in] msg - sdbusplus dbusmessage
125710d49beSLei YU      *
126710d49beSLei YU      *  @return 0 on success, < 0 on failure.
127710d49beSLei YU      */
128710d49beSLei YU     int onSettingsChanged(sdbusplus::message::message& msg);
129710d49beSLei YU 
130415b964fSLei YU     /** @brief Notified on settings property changed
131415b964fSLei YU      *
132415b964fSLei YU      * @param[in] key - The name of property that is changed
133415b964fSLei YU      * @param[in] value - The value of the property
134415b964fSLei YU      */
135*ab4cc6a5SGunnar Mills     void onPropertyChanged(const std::string& key, const std::string& value);
136415b964fSLei YU 
137debe1d8fSLei YU     /** @brief Notified on host state has changed
138c6fe8693SLei YU      *
139debe1d8fSLei YU      * @param[in] msg - sdbusplus dbusmessage
140c6fe8693SLei YU      */
141debe1d8fSLei YU     void onHostStateChanged(sdbusplus::message::message& msg);
142debe1d8fSLei YU 
143debe1d8fSLei YU     /** @brief Notified on host state has changed
144debe1d8fSLei YU      *
145debe1d8fSLei YU      * @param[in] on - Indicate if the host is on or off
146debe1d8fSLei YU      */
147debe1d8fSLei YU     void onHostState(bool on);
148c6fe8693SLei YU 
1497f4fca55SLei YU     /** @brief Set the property as requested time mode/owner
1507f4fca55SLei YU      *
1517f4fca55SLei YU      * @param[in] key - The property name
1527f4fca55SLei YU      * @param[in] value - The property value
1537f4fca55SLei YU      */
1547f4fca55SLei YU     void setPropertyAsRequested(const std::string& key,
1557f4fca55SLei YU                                 const std::string& value);
1567f4fca55SLei YU 
1577f4fca55SLei YU     /** @brief Set the current mode to user requested one
1587f4fca55SLei YU      *  if conditions allow it
1597f4fca55SLei YU      *
1607f4fca55SLei YU      * @param[in] mode - The string of time mode
1617f4fca55SLei YU      */
1627f4fca55SLei YU     void setRequestedMode(const std::string& mode);
1637f4fca55SLei YU 
1647f4fca55SLei YU     /** @brief Set the current owner to user requested one
1657f4fca55SLei YU      *  if conditions allow it
1667f4fca55SLei YU      *
1677f4fca55SLei YU      * @param[in] owner - The string of time owner
1687f4fca55SLei YU      */
1697f4fca55SLei YU     void setRequestedOwner(const std::string& owner);
1707f4fca55SLei YU 
171a741713cSLei YU     /** @brief Update the NTP setting to systemd time service
172a741713cSLei YU      *
173a741713cSLei YU      * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL"
174a741713cSLei YU      */
175a741713cSLei YU     void updateNtpSetting(const std::string& value);
176a741713cSLei YU 
177415b964fSLei YU     /** @brief The static function called on settings property changed
178415b964fSLei YU      *
179415b964fSLei YU      * @param[in] msg - Data associated with subscribed signal
180415b964fSLei YU      * @param[in] userData - Pointer to this object instance
181415b964fSLei YU      * @param[out] retError  - Not used but required with signal API
182415b964fSLei YU      */
183*ab4cc6a5SGunnar Mills     static int onPropertyChanged(sd_bus_message* msg, void* userData,
184415b964fSLei YU                                  sd_bus_error* retError);
185415b964fSLei YU 
1867f4fca55SLei YU     /** @brief The string of time mode property */
187710d49beSLei YU     static constexpr auto PROPERTY_TIME_MODE = "TimeSyncMethod";
1887f4fca55SLei YU 
1897f4fca55SLei YU     /** @brief The string of time owner property */
190710d49beSLei YU     static constexpr auto PROPERTY_TIME_OWNER = "TimeOwner";
1917f4fca55SLei YU 
192415b964fSLei YU     using Updater = std::function<void(const std::string&)>;
193415b964fSLei YU 
194415b964fSLei YU     /** @brief Map the property string to functions that shall
195415b964fSLei YU      *  be called when the property is changed
196415b964fSLei YU      */
197*ab4cc6a5SGunnar Mills     const std::map<std::string, Updater> propertyUpdaters = {
198*ab4cc6a5SGunnar Mills         {PROPERTY_TIME_MODE,
199*ab4cc6a5SGunnar Mills          std::bind(&Manager::setCurrentTimeMode, this, std::placeholders::_1)},
200*ab4cc6a5SGunnar Mills         {PROPERTY_TIME_OWNER, std::bind(&Manager::setCurrentTimeOwner, this,
201*ab4cc6a5SGunnar Mills                                         std::placeholders::_1)}};
202415b964fSLei YU 
203415b964fSLei YU     /** @brief The properties that manager shall notify the
204415b964fSLei YU      *  listeners when changed
205415b964fSLei YU      */
206415b964fSLei YU     static const std::set<std::string> managedProperties;
207415b964fSLei YU 
208415b964fSLei YU     /** @brief The map that maps the string to Owners */
209415b964fSLei YU     static const std::map<std::string, Owner> ownerMap;
2107f4fca55SLei YU 
2117f4fca55SLei YU     /** @brief The file name of saved time mode */
2127f4fca55SLei YU     static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode";
2137f4fca55SLei YU 
2147f4fca55SLei YU     /** @brief The file name of saved time owner */
2157f4fca55SLei YU     static constexpr auto ownerFile = "/var/lib/obmc/saved_time_owner";
216415b964fSLei YU };
217415b964fSLei YU 
218*ab4cc6a5SGunnar Mills } // namespace time
219*ab4cc6a5SGunnar Mills } // namespace phosphor
220