Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Matthew Barth | cc8912e | 2019-01-21 11:35:27 -0600 | [diff] [blame] | 16 | #include "config.h" |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 17 | |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 18 | #include "zone.hpp" |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 19 | |
Matthew Barth | d953bb2 | 2017-08-22 10:45:28 -0500 | [diff] [blame] | 20 | #include "sdbusplus.hpp" |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 21 | #include "utility.hpp" |
| 22 | |
| 23 | #include <cereal/archives/json.hpp> |
| 24 | #include <cereal/cereal.hpp> |
| 25 | #include <phosphor-logging/elog-errors.hpp> |
| 26 | #include <phosphor-logging/elog.hpp> |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 27 | #include <phosphor-logging/lg2.hpp> |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 28 | #include <xyz/openbmc_project/Common/error.hpp> |
| 29 | |
| 30 | #include <chrono> |
Matt Spinler | 2ea9a59 | 2022-04-08 14:36:22 -0500 | [diff] [blame] | 31 | #include <filesystem> |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 32 | #include <fstream> |
| 33 | #include <functional> |
| 34 | #include <stdexcept> |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 35 | |
| 36 | namespace phosphor |
| 37 | { |
| 38 | namespace fan |
| 39 | { |
| 40 | namespace control |
| 41 | { |
| 42 | |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 43 | using namespace std::chrono; |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame] | 44 | using namespace phosphor::fan; |
Matt Spinler | 2ea9a59 | 2022-04-08 14:36:22 -0500 | [diff] [blame] | 45 | namespace fs = std::filesystem; |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 46 | using InternalFailure = |
| 47 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 48 | |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 49 | Zone::Zone(Mode mode, sdbusplus::bus_t& bus, const std::string& path, |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 50 | const sdeventplus::Event& event, const ZoneDefinition& def) : |
Patrick Williams | 0850e31 | 2022-04-05 16:24:52 -0500 | [diff] [blame] | 51 | ThermalObject(bus, path.c_str(), ThermalObject::action::defer_emit), |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 52 | _bus(bus), _path(path), |
Matthew Barth | 766f854 | 2019-01-29 12:44:13 -0600 | [diff] [blame] | 53 | _ifaces({"xyz.openbmc_project.Control.ThermalMode"}), |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 54 | _fullSpeed(std::get<fullSpeedPos>(def)), |
Matthew Barth | 1de6662 | 2017-06-12 13:13:02 -0500 | [diff] [blame] | 55 | _zoneNum(std::get<zoneNumPos>(def)), |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 56 | _defFloorSpeed(std::get<floorSpeedPos>(def)), |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 57 | _defCeilingSpeed(std::get<fullSpeedPos>(def)), |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 58 | _incDelay(std::get<incDelayPos>(def)), |
| 59 | _decInterval(std::get<decIntervalPos>(def)), |
William A. Kennington III | 22c36ab | 2018-10-30 19:50:57 -0700 | [diff] [blame] | 60 | _incTimer(event, std::bind(&Zone::incTimerExpired, this)), |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 61 | _decTimer(event, std::bind(&Zone::decTimerExpired, this)), _eventLoop(event) |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 62 | { |
| 63 | auto& fanDefs = std::get<fanListPos>(def); |
| 64 | |
Matthew Barth | 9f93bd3 | 2020-05-28 16:57:14 -0500 | [diff] [blame] | 65 | for (auto& fanDef : fanDefs) |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 66 | { |
Matthew Barth | 9f93bd3 | 2020-05-28 16:57:14 -0500 | [diff] [blame] | 67 | _fans.emplace_back(std::make_unique<Fan>(bus, fanDef)); |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 68 | } |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 69 | |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 70 | // Do not enable set speed events when in init mode |
Matthew Barth | 93af419 | 2019-01-18 09:30:57 -0600 | [diff] [blame] | 71 | if (mode == Mode::control) |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 72 | { |
Matthew Barth | 1b3e960 | 2019-02-13 11:37:03 -0600 | [diff] [blame] | 73 | // Process any zone handlers defined |
| 74 | for (auto& hand : std::get<handlerPos>(def)) |
| 75 | { |
| 76 | hand(*this); |
| 77 | } |
| 78 | |
Matthew Barth | 9e4db25 | 2019-01-21 13:08:02 -0600 | [diff] [blame] | 79 | // Restore thermal control current mode state |
| 80 | restoreCurrentMode(); |
Matthew Barth | 93af419 | 2019-01-18 09:30:57 -0600 | [diff] [blame] | 81 | |
| 82 | // Emit objects added in control mode only |
| 83 | this->emit_object_added(); |
| 84 | |
Matthew Barth | 2b3db61 | 2017-10-25 10:56:51 -0500 | [diff] [blame] | 85 | // Update target speed to current zone target speed |
| 86 | if (!_fans.empty()) |
| 87 | { |
| 88 | _targetSpeed = _fans.front()->getTargetSpeed(); |
| 89 | } |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 90 | // Setup signal trigger for set speed events |
Matthew Barth | 9f93bd3 | 2020-05-28 16:57:14 -0500 | [diff] [blame] | 91 | for (auto& ssEvent : std::get<setSpeedEventsPos>(def)) |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 92 | { |
Matthew Barth | 9f93bd3 | 2020-05-28 16:57:14 -0500 | [diff] [blame] | 93 | initEvent(ssEvent); |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 94 | } |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 95 | // Start timer for fan speed decreases |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 96 | _decTimer.restart(_decInterval); |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 97 | } |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 100 | void Zone::setSpeed(uint64_t speed) |
| 101 | { |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 102 | if (_isActive) |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 103 | { |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 104 | _targetSpeed = speed; |
| 105 | for (auto& fan : _fans) |
| 106 | { |
| 107 | fan->setSpeed(_targetSpeed); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void Zone::setFullSpeed() |
| 113 | { |
| 114 | if (_fullSpeed != 0) |
| 115 | { |
| 116 | _targetSpeed = _fullSpeed; |
| 117 | for (auto& fan : _fans) |
| 118 | { |
| 119 | fan->setSpeed(_targetSpeed); |
| 120 | } |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 124 | void Zone::setActiveAllow(const Group* group, bool isActiveAllow) |
| 125 | { |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 126 | _active[*(group)] = isActiveAllow; |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 127 | if (!isActiveAllow) |
| 128 | { |
| 129 | _isActive = false; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | // Check all entries are set to allow control active |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 134 | auto actPred = [](const auto& entry) { return entry.second; }; |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 135 | _isActive = std::all_of(_active.begin(), _active.end(), actPred); |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 139 | void Zone::removeService(const Group* group, const std::string& name) |
Matthew Barth | 55dea64 | 2017-11-06 13:34:32 -0600 | [diff] [blame] | 140 | { |
| 141 | try |
| 142 | { |
| 143 | auto& sNames = _services.at(*group); |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 144 | auto it = std::find_if(sNames.begin(), sNames.end(), |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 145 | [&name](const auto& entry) { |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 146 | return name == std::get<namePos>(entry); |
| 147 | }); |
Matthew Barth | 55dea64 | 2017-11-06 13:34:32 -0600 | [diff] [blame] | 148 | if (it != std::end(sNames)) |
| 149 | { |
| 150 | // Remove service name from group |
| 151 | sNames.erase(it); |
| 152 | } |
| 153 | } |
| 154 | catch (const std::out_of_range& oore) |
| 155 | { |
| 156 | // No services for group found |
| 157 | } |
| 158 | } |
| 159 | |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 160 | void Zone::setServiceOwner(const Group* group, const std::string& name, |
Matthew Barth | e59fdf7 | 2017-09-27 09:33:42 -0500 | [diff] [blame] | 161 | const bool hasOwner) |
| 162 | { |
| 163 | try |
| 164 | { |
| 165 | auto& sNames = _services.at(*group); |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 166 | auto it = std::find_if(sNames.begin(), sNames.end(), |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 167 | [&name](const auto& entry) { |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 168 | return name == std::get<namePos>(entry); |
| 169 | }); |
Matthew Barth | e59fdf7 | 2017-09-27 09:33:42 -0500 | [diff] [blame] | 170 | if (it != std::end(sNames)) |
| 171 | { |
| 172 | std::get<hasOwnerPos>(*it) = hasOwner; |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | _services[*group].emplace_back(name, hasOwner); |
| 177 | } |
| 178 | } |
| 179 | catch (const std::out_of_range& oore) |
| 180 | { |
| 181 | _services[*group].emplace_back(name, hasOwner); |
| 182 | } |
| 183 | } |
| 184 | |
Matthew Barth | 480787c | 2017-11-06 11:00:00 -0600 | [diff] [blame] | 185 | void Zone::setServices(const Group* group) |
| 186 | { |
Matthew Barth | 55dea64 | 2017-11-06 13:34:32 -0600 | [diff] [blame] | 187 | // Remove the empty service name if exists |
| 188 | removeService(group, ""); |
Matthew Barth | 480787c | 2017-11-06 11:00:00 -0600 | [diff] [blame] | 189 | for (auto it = group->begin(); it != group->end(); ++it) |
| 190 | { |
| 191 | std::string name; |
| 192 | bool hasOwner = false; |
| 193 | try |
| 194 | { |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 195 | name = getService(std::get<pathPos>(*it), std::get<intfPos>(*it)); |
Matthew Barth | 480787c | 2017-11-06 11:00:00 -0600 | [diff] [blame] | 196 | hasOwner = util::SDBusPlus::callMethodAndRead<bool>( |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 197 | _bus, "org.freedesktop.DBus", "/org/freedesktop/DBus", |
| 198 | "org.freedesktop.DBus", "NameHasOwner", name); |
Matthew Barth | 480787c | 2017-11-06 11:00:00 -0600 | [diff] [blame] | 199 | } |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 200 | catch (const util::DBusMethodError& e) |
Matthew Barth | 480787c | 2017-11-06 11:00:00 -0600 | [diff] [blame] | 201 | { |
| 202 | // Failed to get service name owner state |
| 203 | hasOwner = false; |
| 204 | } |
| 205 | setServiceOwner(group, name, hasOwner); |
| 206 | } |
| 207 | } |
| 208 | |
Matthew Barth | b4a7cb9 | 2017-06-28 15:29:50 -0500 | [diff] [blame] | 209 | void Zone::setFloor(uint64_t speed) |
| 210 | { |
Matthew Barth | 98726c4 | 2017-10-17 10:35:20 -0500 | [diff] [blame] | 211 | // Check all entries are set to allow floor to be set |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 212 | auto pred = [](const auto& entry) { return entry.second; }; |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 213 | auto setFloor = std::all_of(_floorChange.begin(), _floorChange.end(), pred); |
Matthew Barth | 98726c4 | 2017-10-17 10:35:20 -0500 | [diff] [blame] | 214 | if (setFloor) |
Matthew Barth | b4a7cb9 | 2017-06-28 15:29:50 -0500 | [diff] [blame] | 215 | { |
Matthew Barth | 98726c4 | 2017-10-17 10:35:20 -0500 | [diff] [blame] | 216 | _floorSpeed = speed; |
| 217 | // Floor speed above target, update target to floor speed |
| 218 | if (_targetSpeed < _floorSpeed) |
| 219 | { |
| 220 | requestSpeedIncrease(_floorSpeed - _targetSpeed); |
| 221 | } |
Matthew Barth | b4a7cb9 | 2017-06-28 15:29:50 -0500 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 225 | void Zone::requestSpeedIncrease(uint64_t targetDelta) |
| 226 | { |
| 227 | // Only increase speed when delta is higher than |
| 228 | // the current increase delta for the zone and currently under ceiling |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 229 | if (targetDelta > _incSpeedDelta && _targetSpeed < _ceilingSpeed) |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 230 | { |
Matthew Barth | 4e72854 | 2017-09-14 16:47:55 -0500 | [diff] [blame] | 231 | auto requestTarget = getRequestSpeedBase(); |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 232 | requestTarget = (targetDelta - _incSpeedDelta) + requestTarget; |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 233 | _incSpeedDelta = targetDelta; |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 234 | // Target speed can not go above a defined ceiling speed |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 235 | if (requestTarget > _ceilingSpeed) |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 236 | { |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 237 | requestTarget = _ceilingSpeed; |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 238 | } |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 239 | setSpeed(requestTarget); |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 240 | // Retart timer countdown for fan speed increase |
| 241 | _incTimer.restartOnce(_incDelay); |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 242 | } |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void Zone::incTimerExpired() |
| 246 | { |
| 247 | // Clear increase delta when timer expires allowing additional speed |
| 248 | // increase requests or speed decreases to occur |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 249 | _incSpeedDelta = 0; |
| 250 | } |
| 251 | |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 252 | void Zone::requestSpeedDecrease(uint64_t targetDelta) |
| 253 | { |
| 254 | // Only decrease the lowest target delta requested |
| 255 | if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta) |
| 256 | { |
| 257 | _decSpeedDelta = targetDelta; |
| 258 | } |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 259 | } |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 260 | |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 261 | void Zone::decTimerExpired() |
| 262 | { |
Matthew Barth | e4338cd | 2017-12-14 11:14:30 -0600 | [diff] [blame] | 263 | // Check all entries are set to allow a decrease |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 264 | auto pred = [](const auto& entry) { return entry.second; }; |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 265 | auto decAllowed = std::all_of(_decAllowed.begin(), _decAllowed.end(), pred); |
Matthew Barth | e4338cd | 2017-12-14 11:14:30 -0600 | [diff] [blame] | 266 | |
| 267 | // Only decrease speeds when allowed, |
Matthew Barth | d4c001f | 2019-09-19 11:31:20 -0500 | [diff] [blame] | 268 | // a requested decrease speed delta exists, |
Matthew Barth | e4338cd | 2017-12-14 11:14:30 -0600 | [diff] [blame] | 269 | // where no requested increases exist and |
| 270 | // the increase timer is not running |
| 271 | // (i.e. not in the middle of increasing) |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 272 | if (decAllowed && _decSpeedDelta != 0 && _incSpeedDelta == 0 && |
| 273 | !_incTimer.isEnabled()) |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 274 | { |
Matthew Barth | 4e72854 | 2017-09-14 16:47:55 -0500 | [diff] [blame] | 275 | auto requestTarget = getRequestSpeedBase(); |
Matthew Barth | c63973a | 2017-12-08 15:31:35 -0600 | [diff] [blame] | 276 | // Request target speed should not start above ceiling |
| 277 | if (requestTarget > _ceilingSpeed) |
| 278 | { |
| 279 | requestTarget = _ceilingSpeed; |
| 280 | } |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 281 | // Target speed can not go below the defined floor speed |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 282 | if ((requestTarget < _decSpeedDelta) || |
| 283 | (requestTarget - _decSpeedDelta < _floorSpeed)) |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 284 | { |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 285 | requestTarget = _floorSpeed; |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 286 | } |
| 287 | else |
| 288 | { |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 289 | requestTarget = requestTarget - _decSpeedDelta; |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 290 | } |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 291 | setSpeed(requestTarget); |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 292 | } |
| 293 | // Clear decrease delta when timer expires |
| 294 | _decSpeedDelta = 0; |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 295 | // Decrease timer is restarted since its repeating |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 296 | } |
| 297 | |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 298 | void Zone::initEvent(const SetSpeedEvent& event) |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 299 | { |
Matthew Barth | 1b4de26 | 2018-03-06 13:03:16 -0600 | [diff] [blame] | 300 | // Enable event triggers |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 301 | std::for_each( |
| 302 | std::get<triggerPos>(event).begin(), std::get<triggerPos>(event).end(), |
| 303 | [this, &event](const auto& trigger) { |
| 304 | if (!std::get<actionsPos>(event).empty()) |
| 305 | { |
| 306 | std::for_each( |
| 307 | std::get<actionsPos>(event).begin(), |
| 308 | std::get<actionsPos>(event).end(), |
| 309 | [this, &trigger, &event](const auto& action) { |
| 310 | // Default to use group defined with action if exists |
| 311 | if (!std::get<adGroupPos>(action).empty()) |
| 312 | { |
| 313 | trigger(*this, std::get<sseNamePos>(event), |
| 314 | std::get<adGroupPos>(action), |
| 315 | std::get<adActionsPos>(action)); |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | trigger(*this, std::get<sseNamePos>(event), |
| 320 | std::get<groupPos>(event), |
| 321 | std::get<adActionsPos>(action)); |
| 322 | } |
| 323 | }); |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | trigger(*this, std::get<sseNamePos>(event), |
| 328 | std::get<groupPos>(event), {}); |
| 329 | } |
| 330 | }); |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 331 | } |
| 332 | |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 333 | void Zone::removeEvent(const SetSpeedEvent& event) |
| 334 | { |
Matthew Barth | 79cb831 | 2018-11-14 15:20:31 -0600 | [diff] [blame] | 335 | // Remove event signals |
| 336 | auto sigIter = _signalEvents.find(std::get<sseNamePos>(event)); |
| 337 | if (sigIter != _signalEvents.end()) |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 338 | { |
Matthew Barth | 79cb831 | 2018-11-14 15:20:31 -0600 | [diff] [blame] | 339 | auto& signals = sigIter->second; |
| 340 | for (auto it = signals.begin(); it != signals.end(); ++it) |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 341 | { |
Matthew Barth | 33bfe76 | 2018-11-05 11:13:25 -0600 | [diff] [blame] | 342 | removeSignal(it); |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 343 | } |
Matthew Barth | 79cb831 | 2018-11-14 15:20:31 -0600 | [diff] [blame] | 344 | _signalEvents.erase(sigIter); |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 345 | } |
Matthew Barth | 79cb831 | 2018-11-14 15:20:31 -0600 | [diff] [blame] | 346 | |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 347 | // Remove event timers |
| 348 | auto timIter = _timerEvents.find(std::get<sseNamePos>(event)); |
| 349 | if (timIter != _timerEvents.end()) |
Matthew Barth | 33bfe76 | 2018-11-05 11:13:25 -0600 | [diff] [blame] | 350 | { |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 351 | _timerEvents.erase(timIter); |
Matthew Barth | 33bfe76 | 2018-11-05 11:13:25 -0600 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 355 | std::vector<TimerEvent>::iterator Zone::findTimer( |
| 356 | const Group& eventGroup, const std::vector<Action>& eventActions, |
| 357 | std::vector<TimerEvent>& eventTimers) |
Matthew Barth | bfb1a56 | 2017-10-05 17:03:40 -0500 | [diff] [blame] | 358 | { |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 359 | for (auto it = eventTimers.begin(); it != eventTimers.end(); ++it) |
Matthew Barth | bfb1a56 | 2017-10-05 17:03:40 -0500 | [diff] [blame] | 360 | { |
William A. Kennington III | 0420a93 | 2018-10-30 19:53:16 -0700 | [diff] [blame] | 361 | const auto& teEventData = *std::get<timerEventDataPos>(*it); |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 362 | if (std::get<eventGroupPos>(teEventData) == eventGroup && |
| 363 | std::get<eventActionsPos>(teEventData).size() == |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 364 | eventActions.size()) |
Matthew Barth | bfb1a56 | 2017-10-05 17:03:40 -0500 | [diff] [blame] | 365 | { |
| 366 | // TODO openbmc/openbmc#2328 - Use the action function target |
| 367 | // for comparison |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 368 | auto actsEqual = [](const auto& a1, const auto& a2) { |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 369 | return a1.target_type().name() == a2.target_type().name(); |
| 370 | }; |
| 371 | if (std::equal(eventActions.begin(), eventActions.end(), |
Matthew Barth | bfb1a56 | 2017-10-05 17:03:40 -0500 | [diff] [blame] | 372 | std::get<eventActionsPos>(teEventData).begin(), |
| 373 | actsEqual)) |
| 374 | { |
| 375 | return it; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 380 | return eventTimers.end(); |
Matthew Barth | bfb1a56 | 2017-10-05 17:03:40 -0500 | [diff] [blame] | 381 | } |
| 382 | |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 383 | void Zone::addTimer(const std::string& name, const Group& group, |
| 384 | const std::vector<Action>& actions, const TimerConf& tConf) |
William A. Kennington III | 94fe1a0 | 2018-10-30 19:00:27 -0700 | [diff] [blame] | 385 | { |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 386 | auto eventData = std::make_unique<EventData>(group, "", nullptr, actions); |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 387 | Timer timer( |
William A. Kennington III | 94fe1a0 | 2018-10-30 19:00:27 -0700 | [diff] [blame] | 388 | _eventLoop, |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 389 | std::bind(&Zone::timerExpired, this, |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 390 | std::cref(std::get<Group>(*eventData)), |
| 391 | std::cref(std::get<std::vector<Action>>(*eventData)))); |
| 392 | if (std::get<TimerType>(tConf) == TimerType::repeating) |
William A. Kennington III | 94fe1a0 | 2018-10-30 19:00:27 -0700 | [diff] [blame] | 393 | { |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 394 | timer.restart(std::get<intervalPos>(tConf)); |
William A. Kennington III | 94fe1a0 | 2018-10-30 19:00:27 -0700 | [diff] [blame] | 395 | } |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 396 | else if (std::get<TimerType>(tConf) == TimerType::oneshot) |
| 397 | { |
| 398 | timer.restartOnce(std::get<intervalPos>(tConf)); |
| 399 | } |
| 400 | else |
| 401 | { |
| 402 | throw std::invalid_argument("Invalid Timer Type"); |
| 403 | } |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 404 | _timerEvents[name].emplace_back(std::move(eventData), std::move(timer)); |
William A. Kennington III | 94fe1a0 | 2018-10-30 19:00:27 -0700 | [diff] [blame] | 405 | } |
| 406 | |
William A. Kennington III | c0c5f07 | 2018-10-30 19:11:01 -0700 | [diff] [blame] | 407 | void Zone::timerExpired(const Group& eventGroup, |
| 408 | const std::vector<Action>& eventActions) |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame] | 409 | { |
Matthew Barth | f9201ab | 2017-09-11 16:07:58 -0500 | [diff] [blame] | 410 | // Perform the actions |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 411 | std::for_each(eventActions.begin(), eventActions.end(), |
| 412 | [this, &eventGroup](const auto& action) { |
| 413 | action(*this, eventGroup); |
| 414 | }); |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame] | 415 | } |
| 416 | |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 417 | void Zone::handleEvent(sdbusplus::message_t& msg, const EventData* eventData) |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 418 | { |
| 419 | // Handle the callback |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 420 | std::get<eventHandlerPos> (*eventData)(_bus, msg, *this); |
Matthew Barth | f9201ab | 2017-09-11 16:07:58 -0500 | [diff] [blame] | 421 | // Perform the actions |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 422 | std::for_each(std::get<eventActionsPos>(*eventData).begin(), |
| 423 | std::get<eventActionsPos>(*eventData).end(), |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 424 | [this, &eventData](const auto& action) { |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 425 | action(*this, std::get<eventGroupPos>(*eventData)); |
| 426 | }); |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 427 | } |
| 428 | |
Matthew Barth | a603ed0 | 2018-01-19 16:56:26 -0600 | [diff] [blame] | 429 | const std::string& Zone::getService(const std::string& path, |
| 430 | const std::string& intf) |
| 431 | { |
| 432 | // Retrieve service from cache |
| 433 | auto srvIter = _servTree.find(path); |
| 434 | if (srvIter != _servTree.end()) |
| 435 | { |
| 436 | for (auto& serv : srvIter->second) |
| 437 | { |
Patrick Williams | 5e15c3b | 2023-10-20 11:18:11 -0500 | [diff] [blame] | 438 | auto it = std::find_if( |
| 439 | serv.second.begin(), serv.second.end(), |
| 440 | [&intf](const auto& interface) { return intf == interface; }); |
Matthew Barth | a603ed0 | 2018-01-19 16:56:26 -0600 | [diff] [blame] | 441 | if (it != std::end(serv.second)) |
| 442 | { |
| 443 | // Service found |
| 444 | return serv.first; |
| 445 | } |
| 446 | } |
| 447 | // Interface not found in cache, add and return |
| 448 | return addServices(path, intf, 0); |
| 449 | } |
| 450 | else |
| 451 | { |
| 452 | // Path not found in cache, add and return |
| 453 | return addServices(path, intf, 0); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | const std::string& Zone::addServices(const std::string& path, |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 458 | const std::string& intf, int32_t depth) |
Matthew Barth | a603ed0 | 2018-01-19 16:56:26 -0600 | [diff] [blame] | 459 | { |
| 460 | static const std::string empty = ""; |
| 461 | auto it = _servTree.end(); |
| 462 | |
| 463 | // Get all subtree objects for the given interface |
| 464 | auto objects = util::SDBusPlus::getSubTree(_bus, "/", intf, depth); |
| 465 | // Add what's returned to the cache of path->services |
| 466 | for (auto& pIter : objects) |
| 467 | { |
| 468 | auto pathIter = _servTree.find(pIter.first); |
| 469 | if (pathIter != _servTree.end()) |
| 470 | { |
| 471 | // Path found in cache |
| 472 | for (auto& sIter : pIter.second) |
| 473 | { |
| 474 | auto servIter = pathIter->second.find(sIter.first); |
| 475 | if (servIter != pathIter->second.end()) |
| 476 | { |
| 477 | // Service found in cache |
| 478 | for (auto& iIter : sIter.second) |
| 479 | { |
Matthew Barth | e8b340b | 2018-11-09 10:05:34 -0600 | [diff] [blame] | 480 | if (std::find(servIter->second.begin(), |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 481 | servIter->second.end(), iIter) == |
| 482 | servIter->second.end()) |
Matthew Barth | e8b340b | 2018-11-09 10:05:34 -0600 | [diff] [blame] | 483 | { |
| 484 | // Add interface to cache |
| 485 | servIter->second.emplace_back(iIter); |
| 486 | } |
Matthew Barth | a603ed0 | 2018-01-19 16:56:26 -0600 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | else |
| 490 | { |
| 491 | // Service not found in cache |
| 492 | pathIter->second.insert(sIter); |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | else |
| 497 | { |
| 498 | _servTree.insert(pIter); |
| 499 | } |
| 500 | // When the paths match, since a single interface constraint is given, |
| 501 | // that is the service to return |
| 502 | if (path == pIter.first) |
| 503 | { |
| 504 | it = _servTree.find(pIter.first); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | if (it != _servTree.end()) |
| 509 | { |
| 510 | return it->second.begin()->first; |
| 511 | } |
| 512 | |
| 513 | return empty; |
| 514 | } |
| 515 | |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 516 | auto Zone::getPersisted(const std::string& intf, const std::string& prop) |
Matthew Barth | 70b2e7d | 2019-02-18 11:03:07 -0600 | [diff] [blame] | 517 | { |
| 518 | auto persisted = false; |
| 519 | |
| 520 | auto it = _persisted.find(intf); |
| 521 | if (it != _persisted.end()) |
| 522 | { |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 523 | return std::any_of(it->second.begin(), it->second.end(), |
Matthew Barth | 9f93bd3 | 2020-05-28 16:57:14 -0500 | [diff] [blame] | 524 | [&prop](const auto& p) { return prop == p; }); |
Matthew Barth | 70b2e7d | 2019-02-18 11:03:07 -0600 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | return persisted; |
| 528 | } |
| 529 | |
Matthew Barth | 6faf894 | 2019-01-22 09:26:09 -0600 | [diff] [blame] | 530 | std::string Zone::current(std::string value) |
| 531 | { |
Matthew Barth | b390df1 | 2019-02-14 15:18:27 -0600 | [diff] [blame] | 532 | auto current = ThermalObject::current(); |
| 533 | std::transform(value.begin(), value.end(), value.begin(), toupper); |
| 534 | |
Matthew Barth | 221c90c | 2019-02-15 10:35:50 -0600 | [diff] [blame] | 535 | auto supported = ThermalObject::supported(); |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 536 | auto isSupported = |
| 537 | std::any_of(supported.begin(), supported.end(), [&value](auto& s) { |
| 538 | std::transform(s.begin(), s.end(), s.begin(), toupper); |
| 539 | return value == s; |
| 540 | }); |
Matthew Barth | 221c90c | 2019-02-15 10:35:50 -0600 | [diff] [blame] | 541 | |
| 542 | if (value != current && isSupported) |
Matthew Barth | 6faf894 | 2019-01-22 09:26:09 -0600 | [diff] [blame] | 543 | { |
| 544 | current = ThermalObject::current(value); |
Matthew Barth | 70b2e7d | 2019-02-18 11:03:07 -0600 | [diff] [blame] | 545 | if (getPersisted("xyz.openbmc_project.Control.ThermalMode", "Current")) |
| 546 | { |
| 547 | saveCurrentMode(); |
| 548 | } |
Matthew Barth | b390df1 | 2019-02-14 15:18:27 -0600 | [diff] [blame] | 549 | // Trigger event(s) for current mode property change |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 550 | auto eData = _objects[_path]["xyz.openbmc_project.Control.ThermalMode"] |
Matthew Barth | baea6c3 | 2019-01-29 14:20:19 -0600 | [diff] [blame] | 551 | ["Current"]; |
| 552 | if (eData != nullptr) |
| 553 | { |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 554 | sdbusplus::message_t nullMsg{nullptr}; |
Matthew Barth | baea6c3 | 2019-01-29 14:20:19 -0600 | [diff] [blame] | 555 | handleEvent(nullMsg, eData); |
| 556 | } |
Matthew Barth | 6faf894 | 2019-01-22 09:26:09 -0600 | [diff] [blame] | 557 | } |
Matthew Barth | b390df1 | 2019-02-14 15:18:27 -0600 | [diff] [blame] | 558 | |
Matthew Barth | 6faf894 | 2019-01-22 09:26:09 -0600 | [diff] [blame] | 559 | return current; |
| 560 | } |
| 561 | |
Matthew Barth | cc8912e | 2019-01-21 11:35:27 -0600 | [diff] [blame] | 562 | void Zone::saveCurrentMode() |
| 563 | { |
| 564 | fs::path path{CONTROL_PERSIST_ROOT_PATH}; |
| 565 | // Append zone and property description |
| 566 | path /= std::to_string(_zoneNum); |
| 567 | path /= "CurrentMode"; |
| 568 | std::ofstream ofs(path.c_str(), std::ios::binary); |
| 569 | cereal::JSONOutputArchive oArch(ofs); |
| 570 | oArch(ThermalObject::current()); |
| 571 | } |
| 572 | |
Matthew Barth | 9e4db25 | 2019-01-21 13:08:02 -0600 | [diff] [blame] | 573 | void Zone::restoreCurrentMode() |
| 574 | { |
Matthew Barth | a2bed6e | 2019-02-14 16:07:00 -0600 | [diff] [blame] | 575 | auto current = ThermalObject::current(); |
Matthew Barth | 9e4db25 | 2019-01-21 13:08:02 -0600 | [diff] [blame] | 576 | fs::path path{CONTROL_PERSIST_ROOT_PATH}; |
| 577 | path /= std::to_string(_zoneNum); |
| 578 | path /= "CurrentMode"; |
| 579 | fs::create_directories(path.parent_path()); |
| 580 | |
| 581 | try |
| 582 | { |
| 583 | if (fs::exists(path)) |
| 584 | { |
| 585 | std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary); |
| 586 | cereal::JSONInputArchive iArch(ifs); |
| 587 | iArch(current); |
| 588 | } |
| 589 | } |
Patrick Williams | ddb773b | 2021-10-06 11:24:49 -0500 | [diff] [blame] | 590 | catch (const std::exception& e) |
Matthew Barth | 9e4db25 | 2019-01-21 13:08:02 -0600 | [diff] [blame] | 591 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 592 | lg2::error("Exception restoring current zone mode: {ERROR}", "ERROR", |
| 593 | e); |
Matthew Barth | 9e4db25 | 2019-01-21 13:08:02 -0600 | [diff] [blame] | 594 | fs::remove(path); |
Matthew Barth | a2bed6e | 2019-02-14 16:07:00 -0600 | [diff] [blame] | 595 | current = ThermalObject::current(); |
Matthew Barth | 9e4db25 | 2019-01-21 13:08:02 -0600 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | this->current(current); |
| 599 | } |
| 600 | |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 601 | } // namespace control |
| 602 | } // namespace fan |
| 603 | } // namespace phosphor |