Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 1 | #include "manager.hpp" |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 2 | #include "utils.hpp" |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 3 | |
| 4 | #include <phosphor-logging/log.hpp> |
| 5 | |
| 6 | namespace rules = sdbusplus::bus::match::rules; |
| 7 | |
| 8 | namespace // anonymous |
| 9 | { |
| 10 | constexpr auto SETTINGS_SERVICE = "org.openbmc.settings.Host"; |
| 11 | constexpr auto SETTINGS_PATH = "/org/openbmc/settings/host0"; |
| 12 | constexpr auto SETTINGS_INTERFACE = "org.openbmc.settings.Host"; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 13 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 14 | // TODO: Use new settings in xyz.openbmc_project |
| 15 | const auto MATCH_PROPERTY_CHANGE = |
| 16 | rules::type::signal() + |
| 17 | rules::member("PropertiesChanged") + |
| 18 | rules::path("/org/openbmc/settings/host0") + |
| 19 | rules::interface("org.freedesktop.DBus.Properties"); |
| 20 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 21 | const auto MATCH_PGOOD_CHANGE = |
| 22 | rules::type::signal() + |
| 23 | rules::member("PropertiesChanged") + |
| 24 | rules::path("/org/openbmc/control/power0") + |
| 25 | rules::interface("org.freedesktop.DBus.Properties"); |
| 26 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 27 | constexpr auto POWER_SERVICE = "org.openbmc.control.Power"; |
| 28 | constexpr auto POWER_PATH = "/org/openbmc/control/power0"; |
| 29 | constexpr auto POWER_INTERFACE = POWER_SERVICE; |
| 30 | constexpr auto PGOOD_STR = "pgood"; |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 31 | |
| 32 | constexpr auto SYSTEMD_TIME_SERVICE = "org.freedesktop.timedate1"; |
| 33 | constexpr auto SYSTEMD_TIME_PATH = "/org/freedesktop/timedate1"; |
| 34 | constexpr auto SYSTEMD_TIME_INTERFACE = SYSTEMD_TIME_SERVICE; |
| 35 | constexpr auto METHOD_SET_NTP = "SetNTP"; |
| 36 | |
| 37 | constexpr auto OBMC_NETWORK_SERVICE = "org.openbmc.NetworkManager"; |
| 38 | constexpr auto OBMC_NETWORK_PATH = "/org/openbmc/NetworkManager/Interface"; |
| 39 | constexpr auto OBMC_NETWORK_INTERFACE = OBMC_NETWORK_SERVICE; |
| 40 | constexpr auto METHOD_UPDATE_USE_NTP = "UpdateUseNtpField"; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | namespace phosphor |
| 44 | { |
| 45 | namespace time |
| 46 | { |
| 47 | |
| 48 | using namespace phosphor::logging; |
| 49 | |
| 50 | const std::set<std::string> |
| 51 | Manager::managedProperties = {PROPERTY_TIME_MODE, PROPERTY_TIME_OWNER}; |
| 52 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 53 | Manager::Manager(sdbusplus::bus::bus& bus) |
| 54 | : bus(bus), |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 55 | propertyChangeMatch(bus, MATCH_PROPERTY_CHANGE, onPropertyChanged, this), |
| 56 | pgoodChangeMatch(bus, MATCH_PGOOD_CHANGE, onPgoodChanged, this) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 57 | { |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 58 | checkHostOn(); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 59 | |
| 60 | // Restore settings from persistent storage |
| 61 | restoreSettings(); |
| 62 | |
| 63 | // Check the settings daemon to process the new settings |
| 64 | onPropertyChanged(PROPERTY_TIME_MODE, getSettings(PROPERTY_TIME_MODE)); |
| 65 | onPropertyChanged(PROPERTY_TIME_OWNER, getSettings(PROPERTY_TIME_OWNER)); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 66 | |
| 67 | checkDhcpNtp(); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void Manager::addListener(PropertyChangeListner* listener) |
| 71 | { |
| 72 | // Notify listener about the initial value |
| 73 | listener->onModeChanged(timeMode); |
| 74 | listener->onOwnerChanged(timeOwner); |
| 75 | |
| 76 | listeners.insert(listener); |
| 77 | } |
| 78 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 79 | void Manager::restoreSettings() |
| 80 | { |
| 81 | auto mode = utils::readData<std::string>(modeFile); |
| 82 | if (!mode.empty()) |
| 83 | { |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame^] | 84 | timeMode = utils::strToMode(mode); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 85 | } |
| 86 | auto owner = utils::readData<std::string>(ownerFile); |
| 87 | if (!owner.empty()) |
| 88 | { |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame^] | 89 | timeOwner = utils::strToOwner(owner); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 93 | void Manager::checkHostOn() |
| 94 | { |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 95 | int pgood = utils::getProperty<int>(bus, |
| 96 | POWER_SERVICE, |
| 97 | POWER_PATH, |
| 98 | POWER_INTERFACE, |
| 99 | PGOOD_STR); |
| 100 | hostOn = static_cast<bool>(pgood); |
| 101 | } |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 102 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 103 | void Manager::checkDhcpNtp() |
| 104 | { |
| 105 | std::string useDhcpNtp = utils::getProperty<std::string>( |
| 106 | bus, |
| 107 | SETTINGS_SERVICE, |
| 108 | SETTINGS_PATH, |
| 109 | SETTINGS_INTERFACE, |
| 110 | PROPERTY_DHCP_NTP); |
| 111 | updateDhcpNtpSetting(useDhcpNtp); |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 112 | } |
| 113 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 114 | void Manager::onPropertyChanged(const std::string& key, |
| 115 | const std::string& value) |
| 116 | { |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 117 | if (hostOn) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 118 | { |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 119 | // If host is on, set the values as requested time mode/owner. |
| 120 | // And when host becomes off, notify the listners. |
| 121 | setPropertyAsRequested(key, value); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 122 | } |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 123 | else |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 124 | { |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 125 | // If host is off, notify listners |
| 126 | if (key == PROPERTY_TIME_MODE) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 127 | { |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 128 | setCurrentTimeMode(value); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 129 | onTimeModeChanged(value); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 130 | } |
| 131 | else if (key == PROPERTY_TIME_OWNER) |
| 132 | { |
| 133 | setCurrentTimeOwner(value); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 134 | onTimeOwnerChanged(); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | int Manager::onPropertyChanged(sd_bus_message* msg, |
| 140 | void* userData, |
| 141 | sd_bus_error* retError) |
| 142 | { |
| 143 | using properties = std::map < std::string, |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 144 | sdbusplus::message::variant<std::string> >; |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 145 | auto m = sdbusplus::message::message(msg); |
| 146 | // message type: sa{sv}as |
| 147 | std::string ignore; |
| 148 | properties props; |
| 149 | m.read(ignore, props); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 150 | auto manager = static_cast<Manager*>(userData); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 151 | for (const auto& item : props) |
| 152 | { |
| 153 | if (managedProperties.find(item.first) != managedProperties.end()) |
| 154 | { |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 155 | // For managed properties, notify listeners |
| 156 | manager->onPropertyChanged( |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 157 | item.first, item.second.get<std::string>()); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 158 | } |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 159 | else if (item.first == PROPERTY_DHCP_NTP) |
| 160 | { |
| 161 | // For other manager interested properties, handle specifically |
| 162 | manager->updateDhcpNtpSetting(item.second.get<std::string>()); |
| 163 | } |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 164 | } |
| 165 | return 0; |
| 166 | } |
| 167 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 168 | void Manager::setPropertyAsRequested(const std::string& key, |
| 169 | const std::string& value) |
| 170 | { |
| 171 | if (key == PROPERTY_TIME_MODE) |
| 172 | { |
| 173 | setRequestedMode(value); |
| 174 | } |
| 175 | else if (key == PROPERTY_TIME_OWNER) |
| 176 | { |
| 177 | setRequestedOwner(value); |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | // The key shall be already the supported one |
| 182 | // TODO: use elog API |
| 183 | assert(false); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void Manager::setRequestedMode(const std::string& mode) |
| 188 | { |
| 189 | requestedMode = mode; |
| 190 | } |
| 191 | |
| 192 | void Manager::setRequestedOwner(const std::string& owner) |
| 193 | { |
| 194 | requestedOwner = owner; |
| 195 | } |
| 196 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 197 | void Manager::updateNtpSetting(const std::string& value) |
| 198 | { |
| 199 | bool isNtp = (value == "NTP"); |
| 200 | auto method = bus.new_method_call(SYSTEMD_TIME_SERVICE, |
| 201 | SYSTEMD_TIME_PATH, |
| 202 | SYSTEMD_TIME_INTERFACE, |
| 203 | METHOD_SET_NTP); |
| 204 | method.append(isNtp, false); // isNtp: 'true/false' means Enable/Disable |
| 205 | // 'false' meaning no policy-kit |
| 206 | |
| 207 | if (bus.call(method)) |
| 208 | { |
| 209 | log<level::INFO>("Updated NTP setting", |
| 210 | entry("ENABLED:%d", isNtp)); |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | log<level::ERR>("Failed to update NTP setting"); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void Manager::updateDhcpNtpSetting(const std::string& useDhcpNtp) |
| 219 | { |
| 220 | auto method = bus.new_method_call(OBMC_NETWORK_SERVICE, |
| 221 | OBMC_NETWORK_PATH, |
| 222 | OBMC_NETWORK_INTERFACE, |
| 223 | METHOD_UPDATE_USE_NTP); |
| 224 | method.append(useDhcpNtp); |
| 225 | |
| 226 | if (bus.call(method)) |
| 227 | { |
| 228 | log<level::INFO>("Updated use ntp field", |
| 229 | entry("USENTPFIELD:%s", useDhcpNtp.c_str())); |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | log<level::ERR>("Failed to update UseNtpField"); |
| 234 | } |
| 235 | } |
| 236 | |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 237 | void Manager::onPgoodChanged(bool pgood) |
| 238 | { |
| 239 | hostOn = pgood; |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 240 | if (hostOn) |
| 241 | { |
| 242 | return; |
| 243 | } |
| 244 | if (!requestedMode.empty()) |
| 245 | { |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 246 | if (setCurrentTimeMode(requestedMode)) |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 247 | { |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 248 | onTimeModeChanged(requestedMode); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 249 | } |
| 250 | setRequestedMode({}); // Clear requested mode |
| 251 | } |
| 252 | if (!requestedOwner.empty()) |
| 253 | { |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 254 | if (setCurrentTimeOwner(requestedOwner)) |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 255 | { |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 256 | onTimeOwnerChanged(); |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 257 | } |
| 258 | setRequestedOwner({}); // Clear requested owner |
| 259 | } |
Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | int Manager::onPgoodChanged(sd_bus_message* msg, |
| 263 | void* userData, |
| 264 | sd_bus_error* retError) |
| 265 | { |
| 266 | using properties = std::map < std::string, |
| 267 | sdbusplus::message::variant<int> >; |
| 268 | auto m = sdbusplus::message::message(msg); |
| 269 | // message type: sa{sv}as |
| 270 | std::string ignore; |
| 271 | properties props; |
| 272 | m.read(ignore, props); |
| 273 | for (const auto& item : props) |
| 274 | { |
| 275 | if (item.first == PGOOD_STR) |
| 276 | { |
| 277 | static_cast<Manager*>(userData) |
| 278 | ->onPgoodChanged(static_cast<bool>(item.second.get<int>())); |
| 279 | } |
| 280 | } |
| 281 | return 0; |
| 282 | } |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 283 | |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 284 | bool Manager::setCurrentTimeMode(const std::string& mode) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 285 | { |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame^] | 286 | auto newMode = utils::strToMode(mode); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 287 | if (newMode != timeMode) |
| 288 | { |
| 289 | log<level::INFO>("Time mode is changed", |
| 290 | entry("MODE=%s", mode.c_str())); |
| 291 | timeMode = newMode; |
| 292 | utils::writeData(modeFile, mode); |
| 293 | return true; |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | return false; |
| 298 | } |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 299 | } |
| 300 | |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 301 | bool Manager::setCurrentTimeOwner(const std::string& owner) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 302 | { |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame^] | 303 | auto newOwner = utils::strToOwner(owner); |
Lei YU | a5003ce | 2017-02-24 15:35:25 +0800 | [diff] [blame] | 304 | if (newOwner != timeOwner) |
| 305 | { |
| 306 | log<level::INFO>("Time owner is changed", |
| 307 | entry("OWNER=%s", owner.c_str())); |
| 308 | timeOwner = newOwner; |
| 309 | utils::writeData(ownerFile, owner); |
| 310 | return true; |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | return false; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | void Manager::onTimeModeChanged(const std::string& mode) |
| 319 | { |
| 320 | for (const auto listener : listeners) |
| 321 | { |
| 322 | listener->onModeChanged(timeMode); |
| 323 | } |
| 324 | // When time_mode is updated, update the NTP setting |
| 325 | updateNtpSetting(mode); |
| 326 | } |
| 327 | |
| 328 | void Manager::onTimeOwnerChanged() |
| 329 | { |
| 330 | for (const auto& listener : listeners) |
| 331 | { |
| 332 | listener->onOwnerChanged(timeOwner); |
| 333 | } |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | std::string Manager::getSettings(const char* value) const |
| 337 | { |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 338 | return utils::getProperty<std::string>( |
| 339 | bus, |
| 340 | SETTINGS_SERVICE, |
| 341 | SETTINGS_PATH, |
| 342 | SETTINGS_INTERFACE, |
| 343 | value); |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 344 | } |
| 345 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 346 | } |
| 347 | } |