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; 277f4fca55SLei YU 28415b964fSLei YU explicit Manager(sdbusplus::bus::bus& bus); 29c6fe8693SLei YU Manager(const Manager&) = delete; 30c6fe8693SLei YU Manager& operator=(const Manager&) = delete; 31c6fe8693SLei YU Manager(Manager&&) = delete; 32c6fe8693SLei YU Manager& operator=(Manager&&) = delete; 33415b964fSLei YU 34415b964fSLei YU /** @brief Add a listener that will be called 35415b964fSLei YU * when property is changed 36415b964fSLei YU **/ 37415b964fSLei YU void addListener(PropertyChangeListner* listener); 38415b964fSLei YU 39415b964fSLei YU private: 40415b964fSLei YU /** @brief Persistent sdbusplus DBus connection */ 41415b964fSLei YU sdbusplus::bus::bus& bus; 42415b964fSLei YU 43415b964fSLei YU /** @brief The match of settings property change */ 44415b964fSLei YU sdbusplus::bus::match::match propertyChangeMatch; 45415b964fSLei YU 46c6fe8693SLei YU /** @brief The match of pgood change */ 47c6fe8693SLei YU sdbusplus::bus::match::match pgoodChangeMatch; 48c6fe8693SLei YU 49415b964fSLei YU /** @brief The container to hold all the listeners */ 50415b964fSLei YU std::set<PropertyChangeListner*> listeners; 51415b964fSLei YU 52c6fe8693SLei YU /** @brief The value to indicate if host is on */ 53c6fe8693SLei YU bool hostOn = false; 54c6fe8693SLei YU 557f4fca55SLei YU /** @brief The requested time mode when host is on*/ 567f4fca55SLei YU std::string requestedMode; 577f4fca55SLei YU 587f4fca55SLei YU /** @brief The requested time owner when host is on*/ 597f4fca55SLei YU std::string requestedOwner; 607f4fca55SLei YU 61415b964fSLei YU /** @brief The current time mode */ 62415b964fSLei YU Mode timeMode; 63415b964fSLei YU 64415b964fSLei YU /** @brief The current time owner */ 65415b964fSLei YU Owner timeOwner; 66415b964fSLei YU 677f4fca55SLei YU /** @brief Restore saved settings */ 687f4fca55SLei YU void restoreSettings(); 697f4fca55SLei YU 70c6fe8693SLei YU /** @brief Check if host is on and update hostOn variable */ 71c6fe8693SLei YU void checkHostOn(); 72c6fe8693SLei YU 73a741713cSLei YU /** @brief Check if use_dhcp_ntp is used and update NTP setting */ 74a741713cSLei YU void checkDhcpNtp(); 75a741713cSLei YU 76415b964fSLei YU /** @brief Get setting from settingsd service 77415b964fSLei YU * 78415b964fSLei YU * @param[in] setting - The string of the setting 79415b964fSLei YU * 80415b964fSLei YU * @return The setting value in string 81415b964fSLei YU */ 82415b964fSLei YU std::string getSettings(const char* setting) const; 83415b964fSLei YU 84*a5003cebSLei YU /** @brief Set current time mode from the time mode string 85*a5003cebSLei YU * 86*a5003cebSLei YU * @param[in] mode - The string of time mode 87*a5003cebSLei YU * 88*a5003cebSLei YU * @return - true if the mode is updated 89*a5003cebSLei YU * false if it's the same as before 90*a5003cebSLei YU */ 91*a5003cebSLei YU bool setCurrentTimeMode(const std::string& mode); 92*a5003cebSLei YU 93*a5003cebSLei YU /** @brief Set current time owner from the time owner string 94*a5003cebSLei YU * 95*a5003cebSLei YU * @param[in] owner - The string of time owner 96*a5003cebSLei YU * 97*a5003cebSLei YU * @return - true if the owner is updated 98*a5003cebSLei YU * false if it's the same as before 99*a5003cebSLei YU */ 100*a5003cebSLei YU bool setCurrentTimeOwner(const std::string& owner); 101*a5003cebSLei YU 102*a5003cebSLei YU /** @brief Called on time mode is changed 103*a5003cebSLei YU * 104*a5003cebSLei YU * Notify listeners that time mode is changed and update ntp setting 105415b964fSLei YU * 106415b964fSLei YU * @param[in] mode - The string of time mode 107415b964fSLei YU */ 108*a5003cebSLei YU void onTimeModeChanged(const std::string& mode); 109415b964fSLei YU 110*a5003cebSLei YU /** @brief Called on time owner is changed 111415b964fSLei YU * 112*a5003cebSLei YU * Notify listeners that time owner is changed 113415b964fSLei YU */ 114*a5003cebSLei YU void onTimeOwnerChanged(); 115415b964fSLei YU 116415b964fSLei YU /** @brief Notified on settings property changed 117415b964fSLei YU * 118415b964fSLei YU * @param[in] key - The name of property that is changed 119415b964fSLei YU * @param[in] value - The value of the property 120415b964fSLei YU */ 121415b964fSLei YU void onPropertyChanged(const std::string& key, 122415b964fSLei YU const std::string& value); 123415b964fSLei YU 124c6fe8693SLei YU /** @brief Notified on pgood has changed 125c6fe8693SLei YU * 126c6fe8693SLei YU * @param[in] pgood - The changed pgood value 127c6fe8693SLei YU */ 128c6fe8693SLei YU void onPgoodChanged(bool pgood); 129c6fe8693SLei YU 1307f4fca55SLei YU /** @brief Set the property as requested time mode/owner 1317f4fca55SLei YU * 1327f4fca55SLei YU * @param[in] key - The property name 1337f4fca55SLei YU * @param[in] value - The property value 1347f4fca55SLei YU */ 1357f4fca55SLei YU void setPropertyAsRequested(const std::string& key, 1367f4fca55SLei YU const std::string& value); 1377f4fca55SLei YU 1387f4fca55SLei YU /** @brief Set the current mode to user requested one 1397f4fca55SLei YU * if conditions allow it 1407f4fca55SLei YU * 1417f4fca55SLei YU * @param[in] mode - The string of time mode 1427f4fca55SLei YU */ 1437f4fca55SLei YU void setRequestedMode(const std::string& mode); 1447f4fca55SLei YU 1457f4fca55SLei YU /** @brief Set the current owner to user requested one 1467f4fca55SLei YU * if conditions allow it 1477f4fca55SLei YU * 1487f4fca55SLei YU * @param[in] owner - The string of time owner 1497f4fca55SLei YU */ 1507f4fca55SLei YU void setRequestedOwner(const std::string& owner); 1517f4fca55SLei YU 152a741713cSLei YU /** @brief Update the NTP setting to systemd time service 153a741713cSLei YU * 154a741713cSLei YU * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL" 155a741713cSLei YU */ 156a741713cSLei YU void updateNtpSetting(const std::string& value); 157a741713cSLei YU 158a741713cSLei YU /** @brief Update dhcp_ntp setting to OpenBMC network service 159a741713cSLei YU * 160a741713cSLei YU * @param[in] value - The use_dhcp_ntp value, e.g. "yes" or "no" 161a741713cSLei YU */ 162a741713cSLei YU void updateDhcpNtpSetting(const std::string& useDhcpNtp); 163a741713cSLei YU 164415b964fSLei YU /** @brief The static function called on settings property changed 165415b964fSLei YU * 166415b964fSLei YU * @param[in] msg - Data associated with subscribed signal 167415b964fSLei YU * @param[in] userData - Pointer to this object instance 168415b964fSLei YU * @param[out] retError - Not used but required with signal API 169415b964fSLei YU */ 170415b964fSLei YU static int onPropertyChanged(sd_bus_message* msg, 171415b964fSLei YU void* userData, 172415b964fSLei YU sd_bus_error* retError); 173415b964fSLei YU 174c6fe8693SLei YU /** @brief Notified on pgood has changed 175c6fe8693SLei YU * 176c6fe8693SLei YU * @param[in] msg - Data associated with subscribed signal 177c6fe8693SLei YU * @param[in] userData - Pointer to this object instance 178c6fe8693SLei YU * @param[out] retError - Not used but required with signal API 179c6fe8693SLei YU */ 180c6fe8693SLei YU static int onPgoodChanged(sd_bus_message* msg, 181c6fe8693SLei YU void* userData, 182c6fe8693SLei YU sd_bus_error* retError); 183c6fe8693SLei YU 184415b964fSLei YU /** @brief Convert a string to enum Mode 185415b964fSLei YU * 186415b964fSLei YU * Convert the time mode string to enum. 187415b964fSLei YU * Valid strings are "NTP", "MANUAL" 188415b964fSLei YU * If it's not a valid time mode string, return NTP. 189415b964fSLei YU * 190415b964fSLei YU * @param[in] mode - The string of time mode 191415b964fSLei YU * 192415b964fSLei YU * @return The Mode enum 193415b964fSLei YU */ 194415b964fSLei YU static Mode convertToMode(const std::string& mode); 195415b964fSLei YU 196415b964fSLei YU /** @brief Convert a string to enum Owner 197415b964fSLei YU * 198415b964fSLei YU * Convert the time owner string to enum. 199415b964fSLei YU * Valid strings are "BMC", "HOST", "SPLIT", "BOTH" 200415b964fSLei YU * If it's not a valid time owner string, return BMC. 201415b964fSLei YU * 202415b964fSLei YU * @param[in] owner - The string of time owner 203415b964fSLei YU * 204415b964fSLei YU * @return The Owner enum 205415b964fSLei YU */ 206415b964fSLei YU static Owner convertToOwner(const std::string& owner); 207415b964fSLei YU 2087f4fca55SLei YU /** @brief The string of time mode property */ 2097f4fca55SLei YU static constexpr auto PROPERTY_TIME_MODE = "time_mode"; 2107f4fca55SLei YU 2117f4fca55SLei YU /** @brief The string of time owner property */ 2127f4fca55SLei YU static constexpr auto PROPERTY_TIME_OWNER = "time_owner"; 2137f4fca55SLei YU 214a741713cSLei YU /** @brief The string of use dhcp ntp property */ 215a741713cSLei YU static constexpr auto PROPERTY_DHCP_NTP = "use_dhcp_ntp"; 216a741713cSLei YU 217415b964fSLei YU using Updater = std::function<void(const std::string&)>; 218415b964fSLei YU 219415b964fSLei YU /** @brief Map the property string to functions that shall 220415b964fSLei YU * be called when the property is changed 221415b964fSLei YU */ 222415b964fSLei YU const std::map<std::string, Updater> propertyUpdaters = 223415b964fSLei YU { 2247f4fca55SLei YU {PROPERTY_TIME_MODE, std::bind(&Manager::setCurrentTimeMode, 225415b964fSLei YU this, std::placeholders::_1)}, 2267f4fca55SLei YU {PROPERTY_TIME_OWNER, std::bind(&Manager::setCurrentTimeOwner, 227415b964fSLei YU this, std::placeholders::_1)} 228415b964fSLei YU }; 229415b964fSLei YU 230415b964fSLei YU /** @brief The properties that manager shall notify the 231415b964fSLei YU * listeners when changed 232415b964fSLei YU */ 233415b964fSLei YU static const std::set<std::string> managedProperties; 234415b964fSLei YU 235415b964fSLei YU /** @brief The map that maps the string to Owners */ 236415b964fSLei YU static const std::map<std::string, Owner> ownerMap; 2377f4fca55SLei YU 2387f4fca55SLei YU /** @brief The file name of saved time mode */ 2397f4fca55SLei YU static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode"; 2407f4fca55SLei YU 2417f4fca55SLei YU /** @brief The file name of saved time owner */ 2427f4fca55SLei YU static constexpr auto ownerFile = "/var/lib/obmc/saved_time_owner"; 243415b964fSLei YU }; 244415b964fSLei YU 245415b964fSLei YU } 246415b964fSLei YU } 247