Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 1 | #include "manager.hpp" |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 2 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 3 | #include "utils.hpp" |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 4 | |
Lei YU | 86d8041 | 2017-07-12 13:12:12 +0800 | [diff] [blame] | 5 | #include <phosphor-logging/elog-errors.hpp> |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 6 | #include <phosphor-logging/elog.hpp> |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 7 | #include <phosphor-logging/log.hpp> |
Lei YU | 86d8041 | 2017-07-12 13:12:12 +0800 | [diff] [blame] | 8 | #include <xyz/openbmc_project/Common/error.hpp> |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 9 | #include <xyz/openbmc_project/State/Host/server.hpp> |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 10 | |
| 11 | namespace rules = sdbusplus::bus::match::rules; |
| 12 | |
| 13 | namespace // anonymous |
| 14 | { |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 15 | constexpr auto HOST_CURRENT_STATE = "CurrentHostState"; |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 16 | |
| 17 | constexpr auto SYSTEMD_TIME_SERVICE = "org.freedesktop.timedate1"; |
| 18 | constexpr auto SYSTEMD_TIME_PATH = "/org/freedesktop/timedate1"; |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 19 | constexpr auto SYSTEMD_TIME_INTERFACE = "org.freedesktop.timedate1"; |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 20 | constexpr auto METHOD_SET_NTP = "SetNTP"; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 21 | } // namespace |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace time |
| 26 | { |
| 27 | |
| 28 | using namespace phosphor::logging; |
| 29 | |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 30 | const std::set<std::string> Manager::managedProperties = {PROPERTY_TIME_MODE}; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 31 | |
Brad Bishop | 4e84539 | 2018-12-18 18:13:12 -0500 | [diff] [blame] | 32 | Manager::Manager(sdbusplus::bus::bus& bus) : bus(bus), settings(bus) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 33 | { |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 34 | using namespace sdbusplus::bus::match::rules; |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 35 | hostStateChangeMatch = |
| 36 | std::make_unique<decltype(hostStateChangeMatch)::element_type>( |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 37 | bus, propertiesChanged(settings.hostState, settings::hostStateIntf), |
| 38 | std::bind(std::mem_fn(&Manager::onHostStateChanged), this, |
| 39 | std::placeholders::_1)); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 40 | settingsMatches.emplace_back( |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 41 | bus, propertiesChanged(settings.timeSyncMethod, settings::timeSyncIntf), |
| 42 | std::bind(std::mem_fn(&Manager::onSettingsChanged), this, |
| 43 | std::placeholders::_1)); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 44 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 45 | checkHostOn(); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 46 | |
| 47 | // Restore settings from persistent storage |
| 48 | restoreSettings(); |
| 49 | |
| 50 | // Check the settings daemon to process the new settings |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 51 | auto mode = getSetting(settings.timeSyncMethod.c_str(), |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 52 | settings::timeSyncIntf, PROPERTY_TIME_MODE); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 53 | |
| 54 | onPropertyChanged(PROPERTY_TIME_MODE, mode); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void Manager::addListener(PropertyChangeListner* listener) |
| 58 | { |
| 59 | // Notify listener about the initial value |
| 60 | listener->onModeChanged(timeMode); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 61 | |
| 62 | listeners.insert(listener); |
| 63 | } |
| 64 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 65 | void Manager::restoreSettings() |
| 66 | { |
| 67 | auto mode = utils::readData<std::string>(modeFile); |
| 68 | if (!mode.empty()) |
| 69 | { |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 70 | timeMode = utils::strToMode(mode); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 71 | } |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 72 | } |
| 73 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 74 | void Manager::checkHostOn() |
| 75 | { |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 76 | using Host = sdbusplus::xyz::openbmc_project::State::server::Host; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 77 | auto hostService = utils::getService(bus, settings.hostState.c_str(), |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 78 | settings::hostStateIntf); |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 79 | auto stateStr = utils::getProperty<std::string>( |
| 80 | bus, hostService.c_str(), settings.hostState.c_str(), |
| 81 | settings::hostStateIntf, HOST_CURRENT_STATE); |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 82 | auto state = Host::convertHostStateFromString(stateStr); |
| 83 | hostOn = (state == Host::HostState::Running); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 84 | } |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 85 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 86 | void Manager::onPropertyChanged(const std::string& key, |
| 87 | const std::string& value) |
| 88 | { |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 89 | if (hostOn) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 90 | { |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 91 | // If host is on, set the values as requested time mode. |
Gunnar Mills | 7f25c53 | 2017-10-25 20:45:28 -0500 | [diff] [blame] | 92 | // And when host becomes off, notify the listeners. |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 93 | setPropertyAsRequested(key, value); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 94 | } |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 95 | else |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 96 | { |
Gunnar Mills | 7f25c53 | 2017-10-25 20:45:28 -0500 | [diff] [blame] | 97 | // If host is off, notify listeners |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 98 | if (key == PROPERTY_TIME_MODE) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 99 | { |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 100 | setCurrentTimeMode(value); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 101 | onTimeModeChanged(value); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 102 | } |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 106 | int Manager::onSettingsChanged(sdbusplus::message::message& msg) |
| 107 | { |
| 108 | using Interface = std::string; |
| 109 | using Property = std::string; |
| 110 | using Value = std::string; |
Patrick Williams | c09ac3f | 2020-05-13 18:01:29 -0500 | [diff] [blame] | 111 | using Properties = std::map<Property, std::variant<Value>>; |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 112 | |
| 113 | Interface interface; |
| 114 | Properties properties; |
| 115 | |
| 116 | msg.read(interface, properties); |
| 117 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 118 | for (const auto& p : properties) |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 119 | { |
Patrick Williams | 5b746c7 | 2020-05-13 11:49:35 -0500 | [diff] [blame] | 120 | onPropertyChanged(p.first, std::get<std::string>(p.second)); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 126 | void Manager::setPropertyAsRequested(const std::string& key, |
| 127 | const std::string& value) |
| 128 | { |
| 129 | if (key == PROPERTY_TIME_MODE) |
| 130 | { |
| 131 | setRequestedMode(value); |
| 132 | } |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 133 | else |
| 134 | { |
| 135 | // The key shall be already the supported one |
Lei YU | 86d8041 | 2017-07-12 13:12:12 +0800 | [diff] [blame] | 136 | using InvalidArgumentError = |
| 137 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 138 | using namespace xyz::openbmc_project::Common; |
| 139 | elog<InvalidArgumentError>( |
| 140 | InvalidArgument::ARGUMENT_NAME(key.c_str()), |
| 141 | InvalidArgument::ARGUMENT_VALUE(value.c_str())); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | void Manager::setRequestedMode(const std::string& mode) |
| 146 | { |
| 147 | requestedMode = mode; |
| 148 | } |
| 149 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 150 | void Manager::updateNtpSetting(const std::string& value) |
| 151 | { |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 152 | bool isNtp = |
| 153 | (value == "xyz.openbmc_project.Time.Synchronization.Method.NTP"); |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 154 | auto method = bus.new_method_call(SYSTEMD_TIME_SERVICE, SYSTEMD_TIME_PATH, |
| 155 | SYSTEMD_TIME_INTERFACE, METHOD_SET_NTP); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 156 | method.append(isNtp, false); // isNtp: 'true/false' means Enable/Disable |
| 157 | // 'false' meaning no policy-kit |
| 158 | |
Lei YU | 89efe6e | 2018-07-24 10:38:01 +0800 | [diff] [blame] | 159 | try |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 160 | { |
Lei YU | 89efe6e | 2018-07-24 10:38:01 +0800 | [diff] [blame] | 161 | bus.call_noreply(method); |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 162 | log<level::INFO>("Updated NTP setting", entry("ENABLED=%d", isNtp)); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 163 | } |
Lei YU | 89efe6e | 2018-07-24 10:38:01 +0800 | [diff] [blame] | 164 | catch (const sdbusplus::exception::SdBusError& ex) |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 165 | { |
Lei YU | 89efe6e | 2018-07-24 10:38:01 +0800 | [diff] [blame] | 166 | log<level::ERR>("Failed to update NTP setting", |
| 167 | entry("ERR=%s", ex.what())); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 171 | void Manager::onHostStateChanged(sdbusplus::message::message& msg) |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 172 | { |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 173 | using Interface = std::string; |
| 174 | using Property = std::string; |
| 175 | using Value = std::string; |
Patrick Williams | c09ac3f | 2020-05-13 18:01:29 -0500 | [diff] [blame] | 176 | using Properties = std::map<Property, std::variant<Value>>; |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 177 | using Host = sdbusplus::xyz::openbmc_project::State::server::Host; |
| 178 | |
| 179 | Interface interface; |
| 180 | Properties properties; |
| 181 | |
| 182 | msg.read(interface, properties); |
| 183 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 184 | for (const auto& p : properties) |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 185 | { |
| 186 | if (p.first == HOST_CURRENT_STATE) |
| 187 | { |
William A. Kennington III | 1f1d8e0 | 2018-11-06 16:06:34 -0800 | [diff] [blame] | 188 | auto state = Host::convertHostStateFromString( |
Patrick Williams | 5b746c7 | 2020-05-13 11:49:35 -0500 | [diff] [blame] | 189 | std::get<std::string>(p.second)); |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 190 | onHostState(state == Host::HostState::Running); |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void Manager::onHostState(bool on) |
| 197 | { |
| 198 | hostOn = on; |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 199 | if (hostOn) |
| 200 | { |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 201 | log<level::INFO>("Changing time settings is *deferred* now"); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 202 | return; |
| 203 | } |
Lei YU | debe1d8 | 2017-10-13 13:21:37 +0800 | [diff] [blame] | 204 | log<level::INFO>("Changing time settings allowed now"); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 205 | if (!requestedMode.empty()) |
| 206 | { |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 207 | if (setCurrentTimeMode(requestedMode)) |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 208 | { |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 209 | onTimeModeChanged(requestedMode); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 210 | } |
| 211 | setRequestedMode({}); // Clear requested mode |
| 212 | } |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 213 | } |
| 214 | |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 215 | bool Manager::setCurrentTimeMode(const std::string& mode) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 216 | { |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 217 | auto newMode = utils::strToMode(mode); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 218 | if (newMode != timeMode) |
| 219 | { |
| 220 | log<level::INFO>("Time mode is changed", |
| 221 | entry("MODE=%s", mode.c_str())); |
| 222 | timeMode = newMode; |
| 223 | utils::writeData(modeFile, mode); |
| 224 | return true; |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | return false; |
| 229 | } |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 230 | } |
| 231 | |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 232 | void Manager::onTimeModeChanged(const std::string& mode) |
| 233 | { |
| 234 | for (const auto listener : listeners) |
| 235 | { |
| 236 | listener->onModeChanged(timeMode); |
| 237 | } |
| 238 | // When time_mode is updated, update the NTP setting |
| 239 | updateNtpSetting(mode); |
| 240 | } |
| 241 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 242 | std::string Manager::getSetting(const char* path, const char* interface, |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 243 | const char* setting) const |
| 244 | { |
| 245 | std::string settingManager = utils::getService(bus, path, interface); |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 246 | return utils::getProperty<std::string>(bus, settingManager.c_str(), path, |
| 247 | interface, setting); |
Lei YU | 710d49b | 2017-08-01 17:10:17 +0800 | [diff] [blame] | 248 | } |
| 249 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 250 | } // namespace time |
| 251 | } // namespace phosphor |