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 | |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame^] | 84 | /** @brief Set current time mode from the time mode string |
| 85 | * |
| 86 | * @param[in] mode - The string of time mode |
| 87 | * |
| 88 | * @return - true if the mode is updated |
| 89 | * false if it's the same as before |
| 90 | */ |
| 91 | bool setCurrentTimeMode(const std::string& mode); |
| 92 | |
| 93 | /** @brief Set current time owner from the time owner string |
| 94 | * |
| 95 | * @param[in] owner - The string of time owner |
| 96 | * |
| 97 | * @return - true if the owner is updated |
| 98 | * false if it's the same as before |
| 99 | */ |
| 100 | bool setCurrentTimeOwner(const std::string& owner); |
| 101 | |
| 102 | /** @brief Called on time mode is changed |
| 103 | * |
| 104 | * Notify listeners that time mode is changed and update ntp setting |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 105 | * |
| 106 | * @param[in] mode - The string of time mode |
| 107 | */ |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame^] | 108 | void onTimeModeChanged(const std::string& mode); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 109 | |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame^] | 110 | /** @brief Called on time owner is changed |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 111 | * |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame^] | 112 | * Notify listeners that time owner is changed |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 113 | */ |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame^] | 114 | void onTimeOwnerChanged(); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 115 | |
| 116 | /** @brief Notified on settings property changed |
| 117 | * |
| 118 | * @param[in] key - The name of property that is changed |
| 119 | * @param[in] value - The value of the property |
| 120 | */ |
| 121 | void onPropertyChanged(const std::string& key, |
| 122 | const std::string& value); |
| 123 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 124 | /** @brief Notified on pgood has changed |
| 125 | * |
| 126 | * @param[in] pgood - The changed pgood value |
| 127 | */ |
| 128 | void onPgoodChanged(bool pgood); |
| 129 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 130 | /** @brief Set the property as requested time mode/owner |
| 131 | * |
| 132 | * @param[in] key - The property name |
| 133 | * @param[in] value - The property value |
| 134 | */ |
| 135 | void setPropertyAsRequested(const std::string& key, |
| 136 | const std::string& value); |
| 137 | |
| 138 | /** @brief Set the current mode to user requested one |
| 139 | * if conditions allow it |
| 140 | * |
| 141 | * @param[in] mode - The string of time mode |
| 142 | */ |
| 143 | void setRequestedMode(const std::string& mode); |
| 144 | |
| 145 | /** @brief Set the current owner to user requested one |
| 146 | * if conditions allow it |
| 147 | * |
| 148 | * @param[in] owner - The string of time owner |
| 149 | */ |
| 150 | void setRequestedOwner(const std::string& owner); |
| 151 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 152 | /** @brief Update the NTP setting to systemd time service |
| 153 | * |
| 154 | * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL" |
| 155 | */ |
| 156 | void updateNtpSetting(const std::string& value); |
| 157 | |
| 158 | /** @brief Update dhcp_ntp setting to OpenBMC network service |
| 159 | * |
| 160 | * @param[in] value - The use_dhcp_ntp value, e.g. "yes" or "no" |
| 161 | */ |
| 162 | void updateDhcpNtpSetting(const std::string& useDhcpNtp); |
| 163 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 164 | /** @brief The static function called on settings property changed |
| 165 | * |
| 166 | * @param[in] msg - Data associated with subscribed signal |
| 167 | * @param[in] userData - Pointer to this object instance |
| 168 | * @param[out] retError - Not used but required with signal API |
| 169 | */ |
| 170 | static int onPropertyChanged(sd_bus_message* msg, |
| 171 | void* userData, |
| 172 | sd_bus_error* retError); |
| 173 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 174 | /** @brief Notified on pgood has changed |
| 175 | * |
| 176 | * @param[in] msg - Data associated with subscribed signal |
| 177 | * @param[in] userData - Pointer to this object instance |
| 178 | * @param[out] retError - Not used but required with signal API |
| 179 | */ |
| 180 | static int onPgoodChanged(sd_bus_message* msg, |
| 181 | void* userData, |
| 182 | sd_bus_error* retError); |
| 183 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 184 | /** @brief Convert a string to enum Mode |
| 185 | * |
| 186 | * Convert the time mode string to enum. |
| 187 | * Valid strings are "NTP", "MANUAL" |
| 188 | * If it's not a valid time mode string, return NTP. |
| 189 | * |
| 190 | * @param[in] mode - The string of time mode |
| 191 | * |
| 192 | * @return The Mode enum |
| 193 | */ |
| 194 | static Mode convertToMode(const std::string& mode); |
| 195 | |
| 196 | /** @brief Convert a string to enum Owner |
| 197 | * |
| 198 | * Convert the time owner string to enum. |
| 199 | * Valid strings are "BMC", "HOST", "SPLIT", "BOTH" |
| 200 | * If it's not a valid time owner string, return BMC. |
| 201 | * |
| 202 | * @param[in] owner - The string of time owner |
| 203 | * |
| 204 | * @return The Owner enum |
| 205 | */ |
| 206 | static Owner convertToOwner(const std::string& owner); |
| 207 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 208 | /** @brief The string of time mode property */ |
| 209 | static constexpr auto PROPERTY_TIME_MODE = "time_mode"; |
| 210 | |
| 211 | /** @brief The string of time owner property */ |
| 212 | static constexpr auto PROPERTY_TIME_OWNER = "time_owner"; |
| 213 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 214 | /** @brief The string of use dhcp ntp property */ |
| 215 | static constexpr auto PROPERTY_DHCP_NTP = "use_dhcp_ntp"; |
| 216 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 217 | using Updater = std::function<void(const std::string&)>; |
| 218 | |
| 219 | /** @brief Map the property string to functions that shall |
| 220 | * be called when the property is changed |
| 221 | */ |
| 222 | const std::map<std::string, Updater> propertyUpdaters = |
| 223 | { |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 224 | {PROPERTY_TIME_MODE, std::bind(&Manager::setCurrentTimeMode, |
| 225 | this, std::placeholders::_1)}, |
| 226 | {PROPERTY_TIME_OWNER, std::bind(&Manager::setCurrentTimeOwner, |
| 227 | this, std::placeholders::_1)} |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | /** @brief The properties that manager shall notify the |
| 231 | * listeners when changed |
| 232 | */ |
| 233 | static const std::set<std::string> managedProperties; |
| 234 | |
| 235 | /** @brief The map that maps the string to Owners */ |
| 236 | static const std::map<std::string, Owner> ownerMap; |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 237 | |
| 238 | /** @brief The file name of saved time mode */ |
| 239 | static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode"; |
| 240 | |
| 241 | /** @brief The file name of saved time owner */ |
| 242 | static constexpr auto ownerFile = "/var/lib/obmc/saved_time_owner"; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | } |
| 246 | } |