blob: 3fb8cda1305c3c843f39a00da6fc91b28239cc75 [file] [log] [blame]
Matt Spinler7f88fe62017-04-10 14:39:02 -05001/**
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 Barthcc8912e2019-01-21 11:35:27 -060016#include "config.h"
Matthew Barth3e1bb272020-05-26 11:09:21 -050017
Matt Spinler7f88fe62017-04-10 14:39:02 -050018#include "zone.hpp"
Matthew Barth3e1bb272020-05-26 11:09:21 -050019
Matthew Barthd953bb22017-08-22 10:45:28 -050020#include "sdbusplus.hpp"
Matthew Barth3e1bb272020-05-26 11:09:21 -050021#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 Hadi64b5ac22025-04-04 23:54:53 +000027#include <phosphor-logging/lg2.hpp>
Matthew Barth3e1bb272020-05-26 11:09:21 -050028#include <xyz/openbmc_project/Common/error.hpp>
29
30#include <chrono>
Matt Spinler2ea9a592022-04-08 14:36:22 -050031#include <filesystem>
Matthew Barth3e1bb272020-05-26 11:09:21 -050032#include <fstream>
33#include <functional>
34#include <stdexcept>
Matt Spinler7f88fe62017-04-10 14:39:02 -050035
36namespace phosphor
37{
38namespace fan
39{
40namespace control
41{
42
Matthew Barth8600d9a2017-06-23 14:38:05 -050043using namespace std::chrono;
Matthew Barth90149802017-08-15 10:51:37 -050044using namespace phosphor::fan;
Matt Spinler2ea9a592022-04-08 14:36:22 -050045namespace fs = std::filesystem;
Matthew Barth3e1bb272020-05-26 11:09:21 -050046using InternalFailure =
47 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Matt Spinler7f88fe62017-04-10 14:39:02 -050048
Patrick Williamscb356d42022-07-22 19:26:53 -050049Zone::Zone(Mode mode, sdbusplus::bus_t& bus, const std::string& path,
Matthew Barth3e1bb272020-05-26 11:09:21 -050050 const sdeventplus::Event& event, const ZoneDefinition& def) :
Patrick Williams0850e312022-04-05 16:24:52 -050051 ThermalObject(bus, path.c_str(), ThermalObject::action::defer_emit),
Matthew Barth3e1bb272020-05-26 11:09:21 -050052 _bus(bus), _path(path),
Matthew Barth766f8542019-01-29 12:44:13 -060053 _ifaces({"xyz.openbmc_project.Control.ThermalMode"}),
Matt Spinler7f88fe62017-04-10 14:39:02 -050054 _fullSpeed(std::get<fullSpeedPos>(def)),
Matthew Barth1de66622017-06-12 13:13:02 -050055 _zoneNum(std::get<zoneNumPos>(def)),
Matthew Barthe0ca13e2017-06-13 16:29:09 -050056 _defFloorSpeed(std::get<floorSpeedPos>(def)),
Matthew Barth8600d9a2017-06-23 14:38:05 -050057 _defCeilingSpeed(std::get<fullSpeedPos>(def)),
Matthew Bartha9561842017-06-29 11:43:45 -050058 _incDelay(std::get<incDelayPos>(def)),
59 _decInterval(std::get<decIntervalPos>(def)),
William A. Kennington III22c36ab2018-10-30 19:50:57 -070060 _incTimer(event, std::bind(&Zone::incTimerExpired, this)),
Matthew Barth3e1bb272020-05-26 11:09:21 -050061 _decTimer(event, std::bind(&Zone::decTimerExpired, this)), _eventLoop(event)
Matt Spinler7f88fe62017-04-10 14:39:02 -050062{
63 auto& fanDefs = std::get<fanListPos>(def);
64
Matthew Barth9f93bd32020-05-28 16:57:14 -050065 for (auto& fanDef : fanDefs)
Matt Spinler7f88fe62017-04-10 14:39:02 -050066 {
Matthew Barth9f93bd32020-05-28 16:57:14 -050067 _fans.emplace_back(std::make_unique<Fan>(bus, fanDef));
Matt Spinler7f88fe62017-04-10 14:39:02 -050068 }
Matthew Barth38a93a82017-05-11 14:12:27 -050069
Matthew Barth14184132017-05-19 14:37:30 -050070 // Do not enable set speed events when in init mode
Matthew Barth93af4192019-01-18 09:30:57 -060071 if (mode == Mode::control)
Matthew Barth17d1fe22017-05-11 15:00:36 -050072 {
Matthew Barth1b3e9602019-02-13 11:37:03 -060073 // Process any zone handlers defined
74 for (auto& hand : std::get<handlerPos>(def))
75 {
76 hand(*this);
77 }
78
Matthew Barth9e4db252019-01-21 13:08:02 -060079 // Restore thermal control current mode state
80 restoreCurrentMode();
Matthew Barth93af4192019-01-18 09:30:57 -060081
82 // Emit objects added in control mode only
83 this->emit_object_added();
84
Matthew Barth2b3db612017-10-25 10:56:51 -050085 // Update target speed to current zone target speed
86 if (!_fans.empty())
87 {
88 _targetSpeed = _fans.front()->getTargetSpeed();
89 }
Matthew Barthccc77702017-07-28 13:43:04 -050090 // Setup signal trigger for set speed events
Matthew Barth9f93bd32020-05-28 16:57:14 -050091 for (auto& ssEvent : std::get<setSpeedEventsPos>(def))
Matthew Barthccc77702017-07-28 13:43:04 -050092 {
Matthew Barth9f93bd32020-05-28 16:57:14 -050093 initEvent(ssEvent);
Matthew Barthccc77702017-07-28 13:43:04 -050094 }
Matthew Barth8600d9a2017-06-23 14:38:05 -050095 // Start timer for fan speed decreases
William A. Kennington III8fd879f2018-10-30 19:49:29 -070096 _decTimer.restart(_decInterval);
Matthew Barth38a93a82017-05-11 14:12:27 -050097 }
Matt Spinler7f88fe62017-04-10 14:39:02 -050098}
99
Matt Spinler7f88fe62017-04-10 14:39:02 -0500100void Zone::setSpeed(uint64_t speed)
101{
Matthew Barth60b00762017-08-15 13:39:06 -0500102 if (_isActive)
Matt Spinler7f88fe62017-04-10 14:39:02 -0500103 {
Matthew Barth60b00762017-08-15 13:39:06 -0500104 _targetSpeed = speed;
105 for (auto& fan : _fans)
106 {
107 fan->setSpeed(_targetSpeed);
108 }
109 }
110}
111
112void Zone::setFullSpeed()
113{
114 if (_fullSpeed != 0)
115 {
116 _targetSpeed = _fullSpeed;
117 for (auto& fan : _fans)
118 {
119 fan->setSpeed(_targetSpeed);
120 }
Matt Spinler7f88fe62017-04-10 14:39:02 -0500121 }
122}
123
Matthew Barth861d77c2017-05-22 14:18:25 -0500124void Zone::setActiveAllow(const Group* group, bool isActiveAllow)
125{
Matthew Barth60b00762017-08-15 13:39:06 -0500126 _active[*(group)] = isActiveAllow;
Matthew Barth861d77c2017-05-22 14:18:25 -0500127 if (!isActiveAllow)
128 {
129 _isActive = false;
130 }
131 else
132 {
133 // Check all entries are set to allow control active
Patrick Williams61b73292023-05-10 07:50:12 -0500134 auto actPred = [](const auto& entry) { return entry.second; };
Matthew Barth3e1bb272020-05-26 11:09:21 -0500135 _isActive = std::all_of(_active.begin(), _active.end(), actPred);
Matthew Barth861d77c2017-05-22 14:18:25 -0500136 }
137}
138
Matthew Barth3e1bb272020-05-26 11:09:21 -0500139void Zone::removeService(const Group* group, const std::string& name)
Matthew Barth55dea642017-11-06 13:34:32 -0600140{
141 try
142 {
143 auto& sNames = _services.at(*group);
Matthew Barth3e1bb272020-05-26 11:09:21 -0500144 auto it = std::find_if(sNames.begin(), sNames.end(),
Patrick Williams61b73292023-05-10 07:50:12 -0500145 [&name](const auto& entry) {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400146 return name == std::get<namePos>(entry);
147 });
Matthew Barth55dea642017-11-06 13:34:32 -0600148 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 Barth3e1bb272020-05-26 11:09:21 -0500160void Zone::setServiceOwner(const Group* group, const std::string& name,
Matthew Barthe59fdf72017-09-27 09:33:42 -0500161 const bool hasOwner)
162{
163 try
164 {
165 auto& sNames = _services.at(*group);
Matthew Barth3e1bb272020-05-26 11:09:21 -0500166 auto it = std::find_if(sNames.begin(), sNames.end(),
Patrick Williams61b73292023-05-10 07:50:12 -0500167 [&name](const auto& entry) {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400168 return name == std::get<namePos>(entry);
169 });
Matthew Barthe59fdf72017-09-27 09:33:42 -0500170 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 Barth480787c2017-11-06 11:00:00 -0600185void Zone::setServices(const Group* group)
186{
Matthew Barth55dea642017-11-06 13:34:32 -0600187 // Remove the empty service name if exists
188 removeService(group, "");
Matthew Barth480787c2017-11-06 11:00:00 -0600189 for (auto it = group->begin(); it != group->end(); ++it)
190 {
191 std::string name;
192 bool hasOwner = false;
193 try
194 {
Matthew Barth3e1bb272020-05-26 11:09:21 -0500195 name = getService(std::get<pathPos>(*it), std::get<intfPos>(*it));
Matthew Barth480787c2017-11-06 11:00:00 -0600196 hasOwner = util::SDBusPlus::callMethodAndRead<bool>(
Matthew Barth3e1bb272020-05-26 11:09:21 -0500197 _bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
198 "org.freedesktop.DBus", "NameHasOwner", name);
Matthew Barth480787c2017-11-06 11:00:00 -0600199 }
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500200 catch (const util::DBusMethodError& e)
Matthew Barth480787c2017-11-06 11:00:00 -0600201 {
202 // Failed to get service name owner state
203 hasOwner = false;
204 }
205 setServiceOwner(group, name, hasOwner);
206 }
207}
208
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500209void Zone::setFloor(uint64_t speed)
210{
Matthew Barth98726c42017-10-17 10:35:20 -0500211 // Check all entries are set to allow floor to be set
Patrick Williams61b73292023-05-10 07:50:12 -0500212 auto pred = [](const auto& entry) { return entry.second; };
Matthew Barth3e1bb272020-05-26 11:09:21 -0500213 auto setFloor = std::all_of(_floorChange.begin(), _floorChange.end(), pred);
Matthew Barth98726c42017-10-17 10:35:20 -0500214 if (setFloor)
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500215 {
Matthew Barth98726c42017-10-17 10:35:20 -0500216 _floorSpeed = speed;
217 // Floor speed above target, update target to floor speed
218 if (_targetSpeed < _floorSpeed)
219 {
220 requestSpeedIncrease(_floorSpeed - _targetSpeed);
221 }
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500222 }
223}
224
Matthew Barth240397b2017-06-22 11:23:30 -0500225void 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 Barth3e1bb272020-05-26 11:09:21 -0500229 if (targetDelta > _incSpeedDelta && _targetSpeed < _ceilingSpeed)
Matthew Barth240397b2017-06-22 11:23:30 -0500230 {
Matthew Barth4e728542017-09-14 16:47:55 -0500231 auto requestTarget = getRequestSpeedBase();
Matthew Barth60b00762017-08-15 13:39:06 -0500232 requestTarget = (targetDelta - _incSpeedDelta) + requestTarget;
Matthew Barth240397b2017-06-22 11:23:30 -0500233 _incSpeedDelta = targetDelta;
Matthew Barth240397b2017-06-22 11:23:30 -0500234 // Target speed can not go above a defined ceiling speed
Matthew Barth60b00762017-08-15 13:39:06 -0500235 if (requestTarget > _ceilingSpeed)
Matthew Barth240397b2017-06-22 11:23:30 -0500236 {
Matthew Barth60b00762017-08-15 13:39:06 -0500237 requestTarget = _ceilingSpeed;
Matthew Barth240397b2017-06-22 11:23:30 -0500238 }
Matthew Barth60b00762017-08-15 13:39:06 -0500239 setSpeed(requestTarget);
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700240 // Retart timer countdown for fan speed increase
241 _incTimer.restartOnce(_incDelay);
Matthew Barth240397b2017-06-22 11:23:30 -0500242 }
Matthew Barth1ee48f22017-06-27 15:14:48 -0500243}
244
245void Zone::incTimerExpired()
246{
247 // Clear increase delta when timer expires allowing additional speed
248 // increase requests or speed decreases to occur
Matthew Barth240397b2017-06-22 11:23:30 -0500249 _incSpeedDelta = 0;
250}
251
Matthew Barth0ce99d82017-06-22 15:07:29 -0500252void 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 Barth8600d9a2017-06-23 14:38:05 -0500259}
Matthew Barth0ce99d82017-06-22 15:07:29 -0500260
Matthew Barth8600d9a2017-06-23 14:38:05 -0500261void Zone::decTimerExpired()
262{
Matthew Barthe4338cd2017-12-14 11:14:30 -0600263 // Check all entries are set to allow a decrease
Patrick Williams61b73292023-05-10 07:50:12 -0500264 auto pred = [](const auto& entry) { return entry.second; };
Matthew Barth3e1bb272020-05-26 11:09:21 -0500265 auto decAllowed = std::all_of(_decAllowed.begin(), _decAllowed.end(), pred);
Matthew Barthe4338cd2017-12-14 11:14:30 -0600266
267 // Only decrease speeds when allowed,
Matthew Barthd4c001f2019-09-19 11:31:20 -0500268 // a requested decrease speed delta exists,
Matthew Barthe4338cd2017-12-14 11:14:30 -0600269 // where no requested increases exist and
270 // the increase timer is not running
271 // (i.e. not in the middle of increasing)
Matthew Barth3e1bb272020-05-26 11:09:21 -0500272 if (decAllowed && _decSpeedDelta != 0 && _incSpeedDelta == 0 &&
273 !_incTimer.isEnabled())
Matthew Barth0ce99d82017-06-22 15:07:29 -0500274 {
Matthew Barth4e728542017-09-14 16:47:55 -0500275 auto requestTarget = getRequestSpeedBase();
Matthew Barthc63973a2017-12-08 15:31:35 -0600276 // Request target speed should not start above ceiling
277 if (requestTarget > _ceilingSpeed)
278 {
279 requestTarget = _ceilingSpeed;
280 }
Matthew Barth0ce99d82017-06-22 15:07:29 -0500281 // Target speed can not go below the defined floor speed
Matthew Barth60b00762017-08-15 13:39:06 -0500282 if ((requestTarget < _decSpeedDelta) ||
283 (requestTarget - _decSpeedDelta < _floorSpeed))
Matthew Barth0ce99d82017-06-22 15:07:29 -0500284 {
Matthew Barth60b00762017-08-15 13:39:06 -0500285 requestTarget = _floorSpeed;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500286 }
287 else
288 {
Matthew Barth60b00762017-08-15 13:39:06 -0500289 requestTarget = requestTarget - _decSpeedDelta;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500290 }
Matthew Barth60b00762017-08-15 13:39:06 -0500291 setSpeed(requestTarget);
Matthew Barth0ce99d82017-06-22 15:07:29 -0500292 }
293 // Clear decrease delta when timer expires
294 _decSpeedDelta = 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500295 // Decrease timer is restarted since its repeating
Matthew Barth0ce99d82017-06-22 15:07:29 -0500296}
297
Matthew Barthccc77702017-07-28 13:43:04 -0500298void Zone::initEvent(const SetSpeedEvent& event)
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500299{
Matthew Barth1b4de262018-03-06 13:03:16 -0600300 // Enable event triggers
Patrick Williamsdfddd642024-08-16 15:21:51 -0400301 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 Barth1bf0ce42017-06-23 16:16:30 -0500331}
332
Matthew Barthf6b76d82017-08-04 12:58:02 -0500333void Zone::removeEvent(const SetSpeedEvent& event)
334{
Matthew Barth79cb8312018-11-14 15:20:31 -0600335 // Remove event signals
336 auto sigIter = _signalEvents.find(std::get<sseNamePos>(event));
337 if (sigIter != _signalEvents.end())
Matthew Barthf6b76d82017-08-04 12:58:02 -0500338 {
Matthew Barth79cb8312018-11-14 15:20:31 -0600339 auto& signals = sigIter->second;
340 for (auto it = signals.begin(); it != signals.end(); ++it)
Matthew Barth336f18a2017-09-26 09:15:56 -0500341 {
Matthew Barth33bfe762018-11-05 11:13:25 -0600342 removeSignal(it);
Matthew Barth336f18a2017-09-26 09:15:56 -0500343 }
Matthew Barth79cb8312018-11-14 15:20:31 -0600344 _signalEvents.erase(sigIter);
Matthew Barthf6b76d82017-08-04 12:58:02 -0500345 }
Matthew Barth79cb8312018-11-14 15:20:31 -0600346
Matthew Barthd7b716a2018-11-16 13:37:57 -0600347 // Remove event timers
348 auto timIter = _timerEvents.find(std::get<sseNamePos>(event));
349 if (timIter != _timerEvents.end())
Matthew Barth33bfe762018-11-05 11:13:25 -0600350 {
Matthew Barthd7b716a2018-11-16 13:37:57 -0600351 _timerEvents.erase(timIter);
Matthew Barth33bfe762018-11-05 11:13:25 -0600352 }
353}
354
Patrick Williamsdfddd642024-08-16 15:21:51 -0400355std::vector<TimerEvent>::iterator Zone::findTimer(
356 const Group& eventGroup, const std::vector<Action>& eventActions,
357 std::vector<TimerEvent>& eventTimers)
Matthew Barthbfb1a562017-10-05 17:03:40 -0500358{
Matthew Barthd7b716a2018-11-16 13:37:57 -0600359 for (auto it = eventTimers.begin(); it != eventTimers.end(); ++it)
Matthew Barthbfb1a562017-10-05 17:03:40 -0500360 {
William A. Kennington III0420a932018-10-30 19:53:16 -0700361 const auto& teEventData = *std::get<timerEventDataPos>(*it);
Matthew Barthd7b716a2018-11-16 13:37:57 -0600362 if (std::get<eventGroupPos>(teEventData) == eventGroup &&
363 std::get<eventActionsPos>(teEventData).size() ==
Matthew Barth3e1bb272020-05-26 11:09:21 -0500364 eventActions.size())
Matthew Barthbfb1a562017-10-05 17:03:40 -0500365 {
366 // TODO openbmc/openbmc#2328 - Use the action function target
367 // for comparison
Patrick Williams61b73292023-05-10 07:50:12 -0500368 auto actsEqual = [](const auto& a1, const auto& a2) {
Matthew Barth3e1bb272020-05-26 11:09:21 -0500369 return a1.target_type().name() == a2.target_type().name();
370 };
371 if (std::equal(eventActions.begin(), eventActions.end(),
Matthew Barthbfb1a562017-10-05 17:03:40 -0500372 std::get<eventActionsPos>(teEventData).begin(),
373 actsEqual))
374 {
375 return it;
376 }
377 }
378 }
379
Matthew Barthd7b716a2018-11-16 13:37:57 -0600380 return eventTimers.end();
Matthew Barthbfb1a562017-10-05 17:03:40 -0500381}
382
Matthew Barth3e1bb272020-05-26 11:09:21 -0500383void Zone::addTimer(const std::string& name, const Group& group,
384 const std::vector<Action>& actions, const TimerConf& tConf)
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700385{
Matthew Barth3e1bb272020-05-26 11:09:21 -0500386 auto eventData = std::make_unique<EventData>(group, "", nullptr, actions);
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700387 Timer timer(
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700388 _eventLoop,
Matthew Barth3e1bb272020-05-26 11:09:21 -0500389 std::bind(&Zone::timerExpired, this,
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700390 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 III94fe1a02018-10-30 19:00:27 -0700393 {
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700394 timer.restart(std::get<intervalPos>(tConf));
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700395 }
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700396 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 Barthd7b716a2018-11-16 13:37:57 -0600404 _timerEvents[name].emplace_back(std::move(eventData), std::move(timer));
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700405}
406
William A. Kennington IIIc0c5f072018-10-30 19:11:01 -0700407void Zone::timerExpired(const Group& eventGroup,
408 const std::vector<Action>& eventActions)
Matthew Barth90149802017-08-15 10:51:37 -0500409{
Matthew Barthf9201ab2017-09-11 16:07:58 -0500410 // Perform the actions
Patrick Williamsdfddd642024-08-16 15:21:51 -0400411 std::for_each(eventActions.begin(), eventActions.end(),
412 [this, &eventGroup](const auto& action) {
413 action(*this, eventGroup);
414 });
Matthew Barth90149802017-08-15 10:51:37 -0500415}
416
Patrick Williamscb356d42022-07-22 19:26:53 -0500417void Zone::handleEvent(sdbusplus::message_t& msg, const EventData* eventData)
Matthew Barth38a93a82017-05-11 14:12:27 -0500418{
419 // Handle the callback
Matthew Barth3e1bb272020-05-26 11:09:21 -0500420 std::get<eventHandlerPos> (*eventData)(_bus, msg, *this);
Matthew Barthf9201ab2017-09-11 16:07:58 -0500421 // Perform the actions
Matthew Barth3e1bb272020-05-26 11:09:21 -0500422 std::for_each(std::get<eventActionsPos>(*eventData).begin(),
423 std::get<eventActionsPos>(*eventData).end(),
Patrick Williams61b73292023-05-10 07:50:12 -0500424 [this, &eventData](const auto& action) {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400425 action(*this, std::get<eventGroupPos>(*eventData));
426 });
Matthew Barth38a93a82017-05-11 14:12:27 -0500427}
428
Matthew Bartha603ed02018-01-19 16:56:26 -0600429const 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 Williams5e15c3b2023-10-20 11:18:11 -0500438 auto it = std::find_if(
439 serv.second.begin(), serv.second.end(),
440 [&intf](const auto& interface) { return intf == interface; });
Matthew Bartha603ed02018-01-19 16:56:26 -0600441 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
457const std::string& Zone::addServices(const std::string& path,
Matthew Barth3e1bb272020-05-26 11:09:21 -0500458 const std::string& intf, int32_t depth)
Matthew Bartha603ed02018-01-19 16:56:26 -0600459{
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 Barthe8b340b2018-11-09 10:05:34 -0600480 if (std::find(servIter->second.begin(),
Patrick Williamsdfddd642024-08-16 15:21:51 -0400481 servIter->second.end(), iIter) ==
482 servIter->second.end())
Matthew Barthe8b340b2018-11-09 10:05:34 -0600483 {
484 // Add interface to cache
485 servIter->second.emplace_back(iIter);
486 }
Matthew Bartha603ed02018-01-19 16:56:26 -0600487 }
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 Barth3e1bb272020-05-26 11:09:21 -0500516auto Zone::getPersisted(const std::string& intf, const std::string& prop)
Matthew Barth70b2e7d2019-02-18 11:03:07 -0600517{
518 auto persisted = false;
519
520 auto it = _persisted.find(intf);
521 if (it != _persisted.end())
522 {
Matthew Barth3e1bb272020-05-26 11:09:21 -0500523 return std::any_of(it->second.begin(), it->second.end(),
Matthew Barth9f93bd32020-05-28 16:57:14 -0500524 [&prop](const auto& p) { return prop == p; });
Matthew Barth70b2e7d2019-02-18 11:03:07 -0600525 }
526
527 return persisted;
528}
529
Matthew Barth6faf8942019-01-22 09:26:09 -0600530std::string Zone::current(std::string value)
531{
Matthew Barthb390df12019-02-14 15:18:27 -0600532 auto current = ThermalObject::current();
533 std::transform(value.begin(), value.end(), value.begin(), toupper);
534
Matthew Barth221c90c2019-02-15 10:35:50 -0600535 auto supported = ThermalObject::supported();
Patrick Williamsdfddd642024-08-16 15:21:51 -0400536 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 Barth221c90c2019-02-15 10:35:50 -0600541
542 if (value != current && isSupported)
Matthew Barth6faf8942019-01-22 09:26:09 -0600543 {
544 current = ThermalObject::current(value);
Matthew Barth70b2e7d2019-02-18 11:03:07 -0600545 if (getPersisted("xyz.openbmc_project.Control.ThermalMode", "Current"))
546 {
547 saveCurrentMode();
548 }
Matthew Barthb390df12019-02-14 15:18:27 -0600549 // Trigger event(s) for current mode property change
Matthew Barth3e1bb272020-05-26 11:09:21 -0500550 auto eData = _objects[_path]["xyz.openbmc_project.Control.ThermalMode"]
Matthew Barthbaea6c32019-01-29 14:20:19 -0600551 ["Current"];
552 if (eData != nullptr)
553 {
Patrick Williamscb356d42022-07-22 19:26:53 -0500554 sdbusplus::message_t nullMsg{nullptr};
Matthew Barthbaea6c32019-01-29 14:20:19 -0600555 handleEvent(nullMsg, eData);
556 }
Matthew Barth6faf8942019-01-22 09:26:09 -0600557 }
Matthew Barthb390df12019-02-14 15:18:27 -0600558
Matthew Barth6faf8942019-01-22 09:26:09 -0600559 return current;
560}
561
Matthew Barthcc8912e2019-01-21 11:35:27 -0600562void 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 Barth9e4db252019-01-21 13:08:02 -0600573void Zone::restoreCurrentMode()
574{
Matthew Bartha2bed6e2019-02-14 16:07:00 -0600575 auto current = ThermalObject::current();
Matthew Barth9e4db252019-01-21 13:08:02 -0600576 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 Williamsddb773b2021-10-06 11:24:49 -0500590 catch (const std::exception& e)
Matthew Barth9e4db252019-01-21 13:08:02 -0600591 {
Anwaar Hadi64b5ac22025-04-04 23:54:53 +0000592 lg2::error("Exception restoring current zone mode: {ERROR}", "ERROR",
593 e);
Matthew Barth9e4db252019-01-21 13:08:02 -0600594 fs::remove(path);
Matthew Bartha2bed6e2019-02-14 16:07:00 -0600595 current = ThermalObject::current();
Matthew Barth9e4db252019-01-21 13:08:02 -0600596 }
597
598 this->current(current);
599}
600
Matthew Barth3e1bb272020-05-26 11:09:21 -0500601} // namespace control
602} // namespace fan
603} // namespace phosphor