Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 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; |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 27 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 28 | explicit Manager(sdbusplus::bus::bus& bus); |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 29 | Manager(const Manager&) = delete; |
| 30 | Manager& operator=(const Manager&) = delete; |
| 31 | Manager(Manager&&) = delete; |
| 32 | Manager& operator=(Manager&&) = delete; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 33 | |
| 34 | /** @brief Add a listener that will be called |
| 35 | * when property is changed |
| 36 | **/ |
| 37 | void addListener(PropertyChangeListner* listener); |
| 38 | |
| 39 | private: |
| 40 | /** @brief Persistent sdbusplus DBus connection */ |
| 41 | sdbusplus::bus::bus& bus; |
| 42 | |
| 43 | /** @brief The match of settings property change */ |
| 44 | sdbusplus::bus::match::match propertyChangeMatch; |
| 45 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 46 | /** @brief The match of pgood change */ |
| 47 | sdbusplus::bus::match::match pgoodChangeMatch; |
| 48 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 49 | /** @brief The container to hold all the listeners */ |
| 50 | std::set<PropertyChangeListner*> listeners; |
| 51 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 52 | /** @brief The value to indicate if host is on */ |
| 53 | bool hostOn = false; |
| 54 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 55 | /** @brief The requested time mode when host is on*/ |
| 56 | std::string requestedMode; |
| 57 | |
| 58 | /** @brief The requested time owner when host is on*/ |
| 59 | std::string requestedOwner; |
| 60 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 61 | /** @brief The current time mode */ |
| 62 | Mode timeMode; |
| 63 | |
| 64 | /** @brief The current time owner */ |
| 65 | Owner timeOwner; |
| 66 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 67 | /** @brief Restore saved settings */ |
| 68 | void restoreSettings(); |
| 69 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 70 | /** @brief Check if host is on and update hostOn variable */ |
| 71 | void checkHostOn(); |
| 72 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 73 | /** @brief Check if use_dhcp_ntp is used and update NTP setting */ |
| 74 | void checkDhcpNtp(); |
| 75 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 76 | /** @brief Get setting from settingsd service |
| 77 | * |
| 78 | * @param[in] setting - The string of the setting |
| 79 | * |
| 80 | * @return The setting value in string |
| 81 | */ |
| 82 | std::string getSettings(const char* setting) const; |
| 83 | |
| 84 | /** @brief Set current time mode |
| 85 | * |
| 86 | * @param[in] mode - The string of time mode |
| 87 | */ |
| 88 | void setCurrentTimeMode(const std::string& mode); |
| 89 | |
| 90 | /** @brief Set current time owner |
| 91 | * |
| 92 | * @param[in] owner - The string of time owner |
| 93 | */ |
| 94 | void setCurrentTimeOwner(const std::string& owner); |
| 95 | |
| 96 | /** @brief Notified on settings property changed |
| 97 | * |
| 98 | * @param[in] key - The name of property that is changed |
| 99 | * @param[in] value - The value of the property |
| 100 | */ |
| 101 | void onPropertyChanged(const std::string& key, |
| 102 | const std::string& value); |
| 103 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 104 | /** @brief Notified on pgood has changed |
| 105 | * |
| 106 | * @param[in] pgood - The changed pgood value |
| 107 | */ |
| 108 | void onPgoodChanged(bool pgood); |
| 109 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 110 | /** @brief Set the property as requested time mode/owner |
| 111 | * |
| 112 | * @param[in] key - The property name |
| 113 | * @param[in] value - The property value |
| 114 | */ |
| 115 | void setPropertyAsRequested(const std::string& key, |
| 116 | const std::string& value); |
| 117 | |
| 118 | /** @brief Set the current mode to user requested one |
| 119 | * if conditions allow it |
| 120 | * |
| 121 | * @param[in] mode - The string of time mode |
| 122 | */ |
| 123 | void setRequestedMode(const std::string& mode); |
| 124 | |
| 125 | /** @brief Set the current owner to user requested one |
| 126 | * if conditions allow it |
| 127 | * |
| 128 | * @param[in] owner - The string of time owner |
| 129 | */ |
| 130 | void setRequestedOwner(const std::string& owner); |
| 131 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 132 | /** @brief Update the NTP setting to systemd time service |
| 133 | * |
| 134 | * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL" |
| 135 | */ |
| 136 | void updateNtpSetting(const std::string& value); |
| 137 | |
| 138 | /** @brief Update dhcp_ntp setting to OpenBMC network service |
| 139 | * |
| 140 | * @param[in] value - The use_dhcp_ntp value, e.g. "yes" or "no" |
| 141 | */ |
| 142 | void updateDhcpNtpSetting(const std::string& useDhcpNtp); |
| 143 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 144 | /** @brief The static function called on settings property changed |
| 145 | * |
| 146 | * @param[in] msg - Data associated with subscribed signal |
| 147 | * @param[in] userData - Pointer to this object instance |
| 148 | * @param[out] retError - Not used but required with signal API |
| 149 | */ |
| 150 | static int onPropertyChanged(sd_bus_message* msg, |
| 151 | void* userData, |
| 152 | sd_bus_error* retError); |
| 153 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 154 | /** @brief Notified on pgood has changed |
| 155 | * |
| 156 | * @param[in] msg - Data associated with subscribed signal |
| 157 | * @param[in] userData - Pointer to this object instance |
| 158 | * @param[out] retError - Not used but required with signal API |
| 159 | */ |
| 160 | static int onPgoodChanged(sd_bus_message* msg, |
| 161 | void* userData, |
| 162 | sd_bus_error* retError); |
| 163 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 164 | /** @brief Convert a string to enum Mode |
| 165 | * |
| 166 | * Convert the time mode string to enum. |
| 167 | * Valid strings are "NTP", "MANUAL" |
| 168 | * If it's not a valid time mode string, return NTP. |
| 169 | * |
| 170 | * @param[in] mode - The string of time mode |
| 171 | * |
| 172 | * @return The Mode enum |
| 173 | */ |
| 174 | static Mode convertToMode(const std::string& mode); |
| 175 | |
| 176 | /** @brief Convert a string to enum Owner |
| 177 | * |
| 178 | * Convert the time owner string to enum. |
| 179 | * Valid strings are "BMC", "HOST", "SPLIT", "BOTH" |
| 180 | * If it's not a valid time owner string, return BMC. |
| 181 | * |
| 182 | * @param[in] owner - The string of time owner |
| 183 | * |
| 184 | * @return The Owner enum |
| 185 | */ |
| 186 | static Owner convertToOwner(const std::string& owner); |
| 187 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 188 | /** @brief The string of time mode property */ |
| 189 | static constexpr auto PROPERTY_TIME_MODE = "time_mode"; |
| 190 | |
| 191 | /** @brief The string of time owner property */ |
| 192 | static constexpr auto PROPERTY_TIME_OWNER = "time_owner"; |
| 193 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 194 | /** @brief The string of use dhcp ntp property */ |
| 195 | static constexpr auto PROPERTY_DHCP_NTP = "use_dhcp_ntp"; |
| 196 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 197 | using Updater = std::function<void(const std::string&)>; |
| 198 | |
| 199 | /** @brief Map the property string to functions that shall |
| 200 | * be called when the property is changed |
| 201 | */ |
| 202 | const std::map<std::string, Updater> propertyUpdaters = |
| 203 | { |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 204 | {PROPERTY_TIME_MODE, std::bind(&Manager::setCurrentTimeMode, |
| 205 | this, std::placeholders::_1)}, |
| 206 | {PROPERTY_TIME_OWNER, std::bind(&Manager::setCurrentTimeOwner, |
| 207 | this, std::placeholders::_1)} |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | /** @brief The properties that manager shall notify the |
| 211 | * listeners when changed |
| 212 | */ |
| 213 | static const std::set<std::string> managedProperties; |
| 214 | |
| 215 | /** @brief The map that maps the string to Owners */ |
| 216 | static const std::map<std::string, Owner> ownerMap; |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 217 | |
| 218 | /** @brief The file name of saved time mode */ |
| 219 | static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode"; |
| 220 | |
| 221 | /** @brief The file name of saved time owner */ |
| 222 | static constexpr auto ownerFile = "/var/lib/obmc/saved_time_owner"; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | } |
| 226 | } |