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" |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 5 | #include "settings.hpp" |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 6 | |
| 7 | #include <sdbusplus/bus.hpp> |
| 8 | #include <sdbusplus/bus/match.hpp> |
| 9 | |
| 10 | #include <set> |
| 11 | #include <string> |
| 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace time |
| 16 | { |
| 17 | |
| 18 | /** @class Manager |
| 19 | * @brief The manager to handle OpenBMC time. |
| 20 | * @details It registers various time related settings and properties signals |
| 21 | * on DBus and handle the changes. |
| 22 | * For certain properties it also notifies the changed events to listeners. |
| 23 | */ |
| 24 | class Manager |
| 25 | { |
| 26 | public: |
| 27 | friend class TestManager; |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 28 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 29 | explicit Manager(sdbusplus::bus::bus& bus); |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 30 | Manager(const Manager&) = delete; |
| 31 | Manager& operator=(const Manager&) = delete; |
| 32 | Manager(Manager&&) = delete; |
| 33 | Manager& operator=(Manager&&) = delete; |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 34 | ~Manager() = default; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 35 | |
| 36 | /** @brief Add a listener that will be called |
| 37 | * when property is changed |
| 38 | **/ |
| 39 | void addListener(PropertyChangeListner* listener); |
| 40 | |
| 41 | private: |
| 42 | /** @brief Persistent sdbusplus DBus connection */ |
| 43 | sdbusplus::bus::bus& bus; |
| 44 | |
| 45 | /** @brief The match of settings property change */ |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 46 | std::vector<sdbusplus::bus::match::match> settingsMatches; |
| 47 | |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 48 | /** @brief The match of host state change */ |
| 49 | std::unique_ptr<sdbusplus::bus::match::match> hostStateChangeMatch; |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 50 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 51 | /** @brief The container to hold all the listeners */ |
| 52 | std::set<PropertyChangeListner*> listeners; |
| 53 | |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 54 | /** @brief Settings objects of intereset */ |
| 55 | settings::Objects settings; |
| 56 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 57 | /** @brief The value to indicate if host is on */ |
| 58 | bool hostOn = false; |
| 59 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 60 | /** @brief The requested time mode when host is on*/ |
| 61 | std::string requestedMode; |
| 62 | |
| 63 | /** @brief The requested time owner when host is on*/ |
| 64 | std::string requestedOwner; |
| 65 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 66 | /** @brief The current time mode */ |
| 67 | Mode timeMode; |
| 68 | |
| 69 | /** @brief The current time owner */ |
| 70 | Owner timeOwner; |
| 71 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 72 | /** @brief Restore saved settings */ |
| 73 | void restoreSettings(); |
| 74 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 75 | /** @brief Check if host is on and update hostOn variable */ |
| 76 | void checkHostOn(); |
| 77 | |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 78 | /** @brief Get setting from settingsd service |
| 79 | * |
| 80 | * @param[in] path - The dbus object path |
| 81 | * @param[in] interface - The dbus interface |
| 82 | * @param[in] setting - The string of the setting |
| 83 | * |
| 84 | * @return The setting value in string |
| 85 | */ |
| 86 | std::string getSetting(const char* path, |
| 87 | const char* interface, |
| 88 | const char* setting) const; |
| 89 | |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 90 | /** @brief Set current time mode from the time mode string |
| 91 | * |
| 92 | * @param[in] mode - The string of time mode |
| 93 | * |
| 94 | * @return - true if the mode is updated |
| 95 | * false if it's the same as before |
| 96 | */ |
| 97 | bool setCurrentTimeMode(const std::string& mode); |
| 98 | |
| 99 | /** @brief Set current time owner from the time owner string |
| 100 | * |
| 101 | * @param[in] owner - The string of time owner |
| 102 | * |
| 103 | * @return - true if the owner is updated |
| 104 | * false if it's the same as before |
| 105 | */ |
| 106 | bool setCurrentTimeOwner(const std::string& owner); |
| 107 | |
| 108 | /** @brief Called on time mode is changed |
| 109 | * |
| 110 | * Notify listeners that time mode is changed and update ntp setting |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 111 | * |
| 112 | * @param[in] mode - The string of time mode |
| 113 | */ |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 114 | void onTimeModeChanged(const std::string& mode); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 115 | |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 116 | /** @brief Called on time owner is changed |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 117 | * |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 118 | * Notify listeners that time owner is changed |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 119 | */ |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 120 | void onTimeOwnerChanged(); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 121 | |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 122 | /** @brief Callback to handle change in a setting |
| 123 | * |
| 124 | * @param[in] msg - sdbusplus dbusmessage |
| 125 | * |
| 126 | * @return 0 on success, < 0 on failure. |
| 127 | */ |
| 128 | int onSettingsChanged(sdbusplus::message::message& msg); |
| 129 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 130 | /** @brief Notified on settings property changed |
| 131 | * |
| 132 | * @param[in] key - The name of property that is changed |
| 133 | * @param[in] value - The value of the property |
| 134 | */ |
| 135 | void onPropertyChanged(const std::string& key, |
| 136 | const std::string& value); |
| 137 | |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 138 | /** @brief Notified on host state has changed |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 139 | * |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 140 | * @param[in] msg - sdbusplus dbusmessage |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 141 | */ |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 142 | void onHostStateChanged(sdbusplus::message::message& msg); |
| 143 | |
| 144 | /** @brief Notified on host state has changed |
| 145 | * |
| 146 | * @param[in] on - Indicate if the host is on or off |
| 147 | */ |
| 148 | void onHostState(bool on); |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 149 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 150 | /** @brief Set the property as requested time mode/owner |
| 151 | * |
| 152 | * @param[in] key - The property name |
| 153 | * @param[in] value - The property value |
| 154 | */ |
| 155 | void setPropertyAsRequested(const std::string& key, |
| 156 | const std::string& value); |
| 157 | |
| 158 | /** @brief Set the current mode to user requested one |
| 159 | * if conditions allow it |
| 160 | * |
| 161 | * @param[in] mode - The string of time mode |
| 162 | */ |
| 163 | void setRequestedMode(const std::string& mode); |
| 164 | |
| 165 | /** @brief Set the current owner to user requested one |
| 166 | * if conditions allow it |
| 167 | * |
| 168 | * @param[in] owner - The string of time owner |
| 169 | */ |
| 170 | void setRequestedOwner(const std::string& owner); |
| 171 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 172 | /** @brief Update the NTP setting to systemd time service |
| 173 | * |
| 174 | * @param[in] value - The time mode value, e.g. "NTP" or "MANUAL" |
| 175 | */ |
| 176 | void updateNtpSetting(const std::string& value); |
| 177 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 178 | /** @brief The static function called on settings property changed |
| 179 | * |
| 180 | * @param[in] msg - Data associated with subscribed signal |
| 181 | * @param[in] userData - Pointer to this object instance |
| 182 | * @param[out] retError - Not used but required with signal API |
| 183 | */ |
| 184 | static int onPropertyChanged(sd_bus_message* msg, |
| 185 | void* userData, |
| 186 | sd_bus_error* retError); |
| 187 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 188 | /** @brief The string of time mode property */ |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 189 | static constexpr auto PROPERTY_TIME_MODE = "TimeSyncMethod"; |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 190 | |
| 191 | /** @brief The string of time owner property */ |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 192 | static constexpr auto PROPERTY_TIME_OWNER = "TimeOwner"; |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 193 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 194 | using Updater = std::function<void(const std::string&)>; |
| 195 | |
| 196 | /** @brief Map the property string to functions that shall |
| 197 | * be called when the property is changed |
| 198 | */ |
| 199 | const std::map<std::string, Updater> propertyUpdaters = |
| 200 | { |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 201 | {PROPERTY_TIME_MODE, std::bind(&Manager::setCurrentTimeMode, |
| 202 | this, std::placeholders::_1)}, |
| 203 | {PROPERTY_TIME_OWNER, std::bind(&Manager::setCurrentTimeOwner, |
| 204 | this, std::placeholders::_1)} |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | /** @brief The properties that manager shall notify the |
| 208 | * listeners when changed |
| 209 | */ |
| 210 | static const std::set<std::string> managedProperties; |
| 211 | |
| 212 | /** @brief The map that maps the string to Owners */ |
| 213 | static const std::map<std::string, Owner> ownerMap; |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 214 | |
| 215 | /** @brief The file name of saved time mode */ |
| 216 | static constexpr auto modeFile = "/var/lib/obmc/saved_time_mode"; |
| 217 | |
| 218 | /** @brief The file name of saved time owner */ |
| 219 | static constexpr auto ownerFile = "/var/lib/obmc/saved_time_owner"; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | } |
| 223 | } |