blob: 6e95477cee5e9cd3b0b7bc416bd28688bcaf7224 [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>
27#include <phosphor-logging/log.hpp>
28#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;
Matthew Barthdf3e8d62017-05-31 11:07:24 -050045using namespace phosphor::logging;
Matt Spinler2ea9a592022-04-08 14:36:22 -050046namespace fs = std::filesystem;
Matthew Barth3e1bb272020-05-26 11:09:21 -050047using InternalFailure =
48 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Matt Spinler7f88fe62017-04-10 14:39:02 -050049
Patrick Williamscb356d42022-07-22 19:26:53 -050050Zone::Zone(Mode mode, sdbusplus::bus_t& bus, const std::string& path,
Matthew Barth3e1bb272020-05-26 11:09:21 -050051 const sdeventplus::Event& event, const ZoneDefinition& def) :
Patrick Williams0850e312022-04-05 16:24:52 -050052 ThermalObject(bus, path.c_str(), ThermalObject::action::defer_emit),
Matthew Barth3e1bb272020-05-26 11:09:21 -050053 _bus(bus), _path(path),
Matthew Barth766f8542019-01-29 12:44:13 -060054 _ifaces({"xyz.openbmc_project.Control.ThermalMode"}),
Matt Spinler7f88fe62017-04-10 14:39:02 -050055 _fullSpeed(std::get<fullSpeedPos>(def)),
Matthew Barth1de66622017-06-12 13:13:02 -050056 _zoneNum(std::get<zoneNumPos>(def)),
Matthew Barthe0ca13e2017-06-13 16:29:09 -050057 _defFloorSpeed(std::get<floorSpeedPos>(def)),
Matthew Barth8600d9a2017-06-23 14:38:05 -050058 _defCeilingSpeed(std::get<fullSpeedPos>(def)),
Matthew Bartha9561842017-06-29 11:43:45 -050059 _incDelay(std::get<incDelayPos>(def)),
60 _decInterval(std::get<decIntervalPos>(def)),
William A. Kennington III22c36ab2018-10-30 19:50:57 -070061 _incTimer(event, std::bind(&Zone::incTimerExpired, this)),
Matthew Barth3e1bb272020-05-26 11:09:21 -050062 _decTimer(event, std::bind(&Zone::decTimerExpired, this)), _eventLoop(event)
Matt Spinler7f88fe62017-04-10 14:39:02 -050063{
64 auto& fanDefs = std::get<fanListPos>(def);
65
Matthew Barth9f93bd32020-05-28 16:57:14 -050066 for (auto& fanDef : fanDefs)
Matt Spinler7f88fe62017-04-10 14:39:02 -050067 {
Matthew Barth9f93bd32020-05-28 16:57:14 -050068 _fans.emplace_back(std::make_unique<Fan>(bus, fanDef));
Matt Spinler7f88fe62017-04-10 14:39:02 -050069 }
Matthew Barth38a93a82017-05-11 14:12:27 -050070
Matthew Barth14184132017-05-19 14:37:30 -050071 // Do not enable set speed events when in init mode
Matthew Barth93af4192019-01-18 09:30:57 -060072 if (mode == Mode::control)
Matthew Barth17d1fe22017-05-11 15:00:36 -050073 {
Matthew Barth1b3e9602019-02-13 11:37:03 -060074 // Process any zone handlers defined
75 for (auto& hand : std::get<handlerPos>(def))
76 {
77 hand(*this);
78 }
79
Matthew Barth9e4db252019-01-21 13:08:02 -060080 // Restore thermal control current mode state
81 restoreCurrentMode();
Matthew Barth93af4192019-01-18 09:30:57 -060082
83 // Emit objects added in control mode only
84 this->emit_object_added();
85
Matthew Barth2b3db612017-10-25 10:56:51 -050086 // Update target speed to current zone target speed
87 if (!_fans.empty())
88 {
89 _targetSpeed = _fans.front()->getTargetSpeed();
90 }
Matthew Barthccc77702017-07-28 13:43:04 -050091 // Setup signal trigger for set speed events
Matthew Barth9f93bd32020-05-28 16:57:14 -050092 for (auto& ssEvent : std::get<setSpeedEventsPos>(def))
Matthew Barthccc77702017-07-28 13:43:04 -050093 {
Matthew Barth9f93bd32020-05-28 16:57:14 -050094 initEvent(ssEvent);
Matthew Barthccc77702017-07-28 13:43:04 -050095 }
Matthew Barth8600d9a2017-06-23 14:38:05 -050096 // Start timer for fan speed decreases
William A. Kennington III8fd879f2018-10-30 19:49:29 -070097 _decTimer.restart(_decInterval);
Matthew Barth38a93a82017-05-11 14:12:27 -050098 }
Matt Spinler7f88fe62017-04-10 14:39:02 -050099}
100
Matt Spinler7f88fe62017-04-10 14:39:02 -0500101void Zone::setSpeed(uint64_t speed)
102{
Matthew Barth60b00762017-08-15 13:39:06 -0500103 if (_isActive)
Matt Spinler7f88fe62017-04-10 14:39:02 -0500104 {
Matthew Barth60b00762017-08-15 13:39:06 -0500105 _targetSpeed = speed;
106 for (auto& fan : _fans)
107 {
108 fan->setSpeed(_targetSpeed);
109 }
110 }
111}
112
113void Zone::setFullSpeed()
114{
115 if (_fullSpeed != 0)
116 {
117 _targetSpeed = _fullSpeed;
118 for (auto& fan : _fans)
119 {
120 fan->setSpeed(_targetSpeed);
121 }
Matt Spinler7f88fe62017-04-10 14:39:02 -0500122 }
123}
124
Matthew Barth861d77c2017-05-22 14:18:25 -0500125void Zone::setActiveAllow(const Group* group, bool isActiveAllow)
126{
Matthew Barth60b00762017-08-15 13:39:06 -0500127 _active[*(group)] = isActiveAllow;
Matthew Barth861d77c2017-05-22 14:18:25 -0500128 if (!isActiveAllow)
129 {
130 _isActive = false;
131 }
132 else
133 {
134 // Check all entries are set to allow control active
Matthew Barth3e1bb272020-05-26 11:09:21 -0500135 auto actPred = [](auto const& entry) { return entry.second; };
136 _isActive = std::all_of(_active.begin(), _active.end(), actPred);
Matthew Barth861d77c2017-05-22 14:18:25 -0500137 }
138}
139
Matthew Barth3e1bb272020-05-26 11:09:21 -0500140void Zone::removeService(const Group* group, const std::string& name)
Matthew Barth55dea642017-11-06 13:34:32 -0600141{
142 try
143 {
144 auto& sNames = _services.at(*group);
Matthew Barth3e1bb272020-05-26 11:09:21 -0500145 auto it = std::find_if(sNames.begin(), sNames.end(),
146 [&name](auto const& entry) {
147 return name == std::get<namePos>(entry);
148 });
Matthew Barth55dea642017-11-06 13:34:32 -0600149 if (it != std::end(sNames))
150 {
151 // Remove service name from group
152 sNames.erase(it);
153 }
154 }
155 catch (const std::out_of_range& oore)
156 {
157 // No services for group found
158 }
159}
160
Matthew Barth3e1bb272020-05-26 11:09:21 -0500161void Zone::setServiceOwner(const Group* group, const std::string& name,
Matthew Barthe59fdf72017-09-27 09:33:42 -0500162 const bool hasOwner)
163{
164 try
165 {
166 auto& sNames = _services.at(*group);
Matthew Barth3e1bb272020-05-26 11:09:21 -0500167 auto it = std::find_if(sNames.begin(), sNames.end(),
168 [&name](auto const& entry) {
169 return name == std::get<namePos>(entry);
170 });
Matthew Barthe59fdf72017-09-27 09:33:42 -0500171 if (it != std::end(sNames))
172 {
173 std::get<hasOwnerPos>(*it) = hasOwner;
174 }
175 else
176 {
177 _services[*group].emplace_back(name, hasOwner);
178 }
179 }
180 catch (const std::out_of_range& oore)
181 {
182 _services[*group].emplace_back(name, hasOwner);
183 }
184}
185
Matthew Barth480787c2017-11-06 11:00:00 -0600186void Zone::setServices(const Group* group)
187{
Matthew Barth55dea642017-11-06 13:34:32 -0600188 // Remove the empty service name if exists
189 removeService(group, "");
Matthew Barth480787c2017-11-06 11:00:00 -0600190 for (auto it = group->begin(); it != group->end(); ++it)
191 {
192 std::string name;
193 bool hasOwner = false;
194 try
195 {
Matthew Barth3e1bb272020-05-26 11:09:21 -0500196 name = getService(std::get<pathPos>(*it), std::get<intfPos>(*it));
Matthew Barth480787c2017-11-06 11:00:00 -0600197 hasOwner = util::SDBusPlus::callMethodAndRead<bool>(
Matthew Barth3e1bb272020-05-26 11:09:21 -0500198 _bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
199 "org.freedesktop.DBus", "NameHasOwner", name);
Matthew Barth480787c2017-11-06 11:00:00 -0600200 }
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500201 catch (const util::DBusMethodError& e)
Matthew Barth480787c2017-11-06 11:00:00 -0600202 {
203 // Failed to get service name owner state
204 hasOwner = false;
205 }
206 setServiceOwner(group, name, hasOwner);
207 }
208}
209
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500210void Zone::setFloor(uint64_t speed)
211{
Matthew Barth98726c42017-10-17 10:35:20 -0500212 // Check all entries are set to allow floor to be set
Matthew Barth3e1bb272020-05-26 11:09:21 -0500213 auto pred = [](auto const& entry) { return entry.second; };
214 auto setFloor = std::all_of(_floorChange.begin(), _floorChange.end(), pred);
Matthew Barth98726c42017-10-17 10:35:20 -0500215 if (setFloor)
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500216 {
Matthew Barth98726c42017-10-17 10:35:20 -0500217 _floorSpeed = speed;
218 // Floor speed above target, update target to floor speed
219 if (_targetSpeed < _floorSpeed)
220 {
221 requestSpeedIncrease(_floorSpeed - _targetSpeed);
222 }
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500223 }
224}
225
Matthew Barth240397b2017-06-22 11:23:30 -0500226void Zone::requestSpeedIncrease(uint64_t targetDelta)
227{
228 // Only increase speed when delta is higher than
229 // the current increase delta for the zone and currently under ceiling
Matthew Barth3e1bb272020-05-26 11:09:21 -0500230 if (targetDelta > _incSpeedDelta && _targetSpeed < _ceilingSpeed)
Matthew Barth240397b2017-06-22 11:23:30 -0500231 {
Matthew Barth4e728542017-09-14 16:47:55 -0500232 auto requestTarget = getRequestSpeedBase();
Matthew Barth60b00762017-08-15 13:39:06 -0500233 requestTarget = (targetDelta - _incSpeedDelta) + requestTarget;
Matthew Barth240397b2017-06-22 11:23:30 -0500234 _incSpeedDelta = targetDelta;
Matthew Barth240397b2017-06-22 11:23:30 -0500235 // Target speed can not go above a defined ceiling speed
Matthew Barth60b00762017-08-15 13:39:06 -0500236 if (requestTarget > _ceilingSpeed)
Matthew Barth240397b2017-06-22 11:23:30 -0500237 {
Matthew Barth60b00762017-08-15 13:39:06 -0500238 requestTarget = _ceilingSpeed;
Matthew Barth240397b2017-06-22 11:23:30 -0500239 }
Matthew Barth60b00762017-08-15 13:39:06 -0500240 setSpeed(requestTarget);
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700241 // Retart timer countdown for fan speed increase
242 _incTimer.restartOnce(_incDelay);
Matthew Barth240397b2017-06-22 11:23:30 -0500243 }
Matthew Barth1ee48f22017-06-27 15:14:48 -0500244}
245
246void Zone::incTimerExpired()
247{
248 // Clear increase delta when timer expires allowing additional speed
249 // increase requests or speed decreases to occur
Matthew Barth240397b2017-06-22 11:23:30 -0500250 _incSpeedDelta = 0;
251}
252
Matthew Barth0ce99d82017-06-22 15:07:29 -0500253void Zone::requestSpeedDecrease(uint64_t targetDelta)
254{
255 // Only decrease the lowest target delta requested
256 if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta)
257 {
258 _decSpeedDelta = targetDelta;
259 }
Matthew Barth8600d9a2017-06-23 14:38:05 -0500260}
Matthew Barth0ce99d82017-06-22 15:07:29 -0500261
Matthew Barth8600d9a2017-06-23 14:38:05 -0500262void Zone::decTimerExpired()
263{
Matthew Barthe4338cd2017-12-14 11:14:30 -0600264 // Check all entries are set to allow a decrease
Matthew Barth3e1bb272020-05-26 11:09:21 -0500265 auto pred = [](auto const& entry) { return entry.second; };
266 auto decAllowed = std::all_of(_decAllowed.begin(), _decAllowed.end(), pred);
Matthew Barthe4338cd2017-12-14 11:14:30 -0600267
268 // Only decrease speeds when allowed,
Matthew Barthd4c001f2019-09-19 11:31:20 -0500269 // a requested decrease speed delta exists,
Matthew Barthe4338cd2017-12-14 11:14:30 -0600270 // where no requested increases exist and
271 // the increase timer is not running
272 // (i.e. not in the middle of increasing)
Matthew Barth3e1bb272020-05-26 11:09:21 -0500273 if (decAllowed && _decSpeedDelta != 0 && _incSpeedDelta == 0 &&
274 !_incTimer.isEnabled())
Matthew Barth0ce99d82017-06-22 15:07:29 -0500275 {
Matthew Barth4e728542017-09-14 16:47:55 -0500276 auto requestTarget = getRequestSpeedBase();
Matthew Barthc63973a2017-12-08 15:31:35 -0600277 // Request target speed should not start above ceiling
278 if (requestTarget > _ceilingSpeed)
279 {
280 requestTarget = _ceilingSpeed;
281 }
Matthew Barth0ce99d82017-06-22 15:07:29 -0500282 // Target speed can not go below the defined floor speed
Matthew Barth60b00762017-08-15 13:39:06 -0500283 if ((requestTarget < _decSpeedDelta) ||
284 (requestTarget - _decSpeedDelta < _floorSpeed))
Matthew Barth0ce99d82017-06-22 15:07:29 -0500285 {
Matthew Barth60b00762017-08-15 13:39:06 -0500286 requestTarget = _floorSpeed;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500287 }
288 else
289 {
Matthew Barth60b00762017-08-15 13:39:06 -0500290 requestTarget = requestTarget - _decSpeedDelta;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500291 }
Matthew Barth60b00762017-08-15 13:39:06 -0500292 setSpeed(requestTarget);
Matthew Barth0ce99d82017-06-22 15:07:29 -0500293 }
294 // Clear decrease delta when timer expires
295 _decSpeedDelta = 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500296 // Decrease timer is restarted since its repeating
Matthew Barth0ce99d82017-06-22 15:07:29 -0500297}
298
Matthew Barthccc77702017-07-28 13:43:04 -0500299void Zone::initEvent(const SetSpeedEvent& event)
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500300{
Matthew Barth1b4de262018-03-06 13:03:16 -0600301 // Enable event triggers
302 std::for_each(
Matthew Barth3e1bb272020-05-26 11:09:21 -0500303 std::get<triggerPos>(event).begin(), std::get<triggerPos>(event).end(),
304 [this, &event](auto const& trigger) {
Matthew Barth75d97352018-11-01 10:34:49 -0500305 if (!std::get<actionsPos>(event).empty())
306 {
307 std::for_each(
308 std::get<actionsPos>(event).begin(),
309 std::get<actionsPos>(event).end(),
Matthew Barth3e1bb272020-05-26 11:09:21 -0500310 [this, &trigger, &event](auto const& action) {
Matthew Barth75d97352018-11-01 10:34:49 -0500311 // Default to use group defined with action if exists
312 if (!std::get<adGroupPos>(action).empty())
313 {
Matthew Barth3e1bb272020-05-26 11:09:21 -0500314 trigger(*this, std::get<sseNamePos>(event),
Matthew Barth75d97352018-11-01 10:34:49 -0500315 std::get<adGroupPos>(action),
316 std::get<adActionsPos>(action));
317 }
318 else
319 {
Matthew Barth3e1bb272020-05-26 11:09:21 -0500320 trigger(*this, std::get<sseNamePos>(event),
Matthew Barth75d97352018-11-01 10:34:49 -0500321 std::get<groupPos>(event),
322 std::get<adActionsPos>(action));
323 }
Matthew Barth3e1bb272020-05-26 11:09:21 -0500324 });
Matthew Barth75d97352018-11-01 10:34:49 -0500325 }
326 else
327 {
Matthew Barth3e1bb272020-05-26 11:09:21 -0500328 trigger(*this, std::get<sseNamePos>(event),
329 std::get<groupPos>(event), {});
Matthew Barth75d97352018-11-01 10:34:49 -0500330 }
Matthew Barth3e1bb272020-05-26 11:09:21 -0500331 });
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500332}
333
Matthew Barthf6b76d82017-08-04 12:58:02 -0500334void Zone::removeEvent(const SetSpeedEvent& event)
335{
Matthew Barth79cb8312018-11-14 15:20:31 -0600336 // Remove event signals
337 auto sigIter = _signalEvents.find(std::get<sseNamePos>(event));
338 if (sigIter != _signalEvents.end())
Matthew Barthf6b76d82017-08-04 12:58:02 -0500339 {
Matthew Barth79cb8312018-11-14 15:20:31 -0600340 auto& signals = sigIter->second;
341 for (auto it = signals.begin(); it != signals.end(); ++it)
Matthew Barth336f18a2017-09-26 09:15:56 -0500342 {
Matthew Barth33bfe762018-11-05 11:13:25 -0600343 removeSignal(it);
Matthew Barth336f18a2017-09-26 09:15:56 -0500344 }
Matthew Barth79cb8312018-11-14 15:20:31 -0600345 _signalEvents.erase(sigIter);
Matthew Barthf6b76d82017-08-04 12:58:02 -0500346 }
Matthew Barth79cb8312018-11-14 15:20:31 -0600347
Matthew Barthd7b716a2018-11-16 13:37:57 -0600348 // Remove event timers
349 auto timIter = _timerEvents.find(std::get<sseNamePos>(event));
350 if (timIter != _timerEvents.end())
Matthew Barth33bfe762018-11-05 11:13:25 -0600351 {
Matthew Barthd7b716a2018-11-16 13:37:57 -0600352 _timerEvents.erase(timIter);
Matthew Barth33bfe762018-11-05 11:13:25 -0600353 }
354}
355
Matthew Barth3e1bb272020-05-26 11:09:21 -0500356std::vector<TimerEvent>::iterator
357 Zone::findTimer(const Group& eventGroup,
358 const std::vector<Action>& eventActions,
359 std::vector<TimerEvent>& eventTimers)
Matthew Barthbfb1a562017-10-05 17:03:40 -0500360{
Matthew Barthd7b716a2018-11-16 13:37:57 -0600361 for (auto it = eventTimers.begin(); it != eventTimers.end(); ++it)
Matthew Barthbfb1a562017-10-05 17:03:40 -0500362 {
William A. Kennington III0420a932018-10-30 19:53:16 -0700363 const auto& teEventData = *std::get<timerEventDataPos>(*it);
Matthew Barthd7b716a2018-11-16 13:37:57 -0600364 if (std::get<eventGroupPos>(teEventData) == eventGroup &&
365 std::get<eventActionsPos>(teEventData).size() ==
Matthew Barth3e1bb272020-05-26 11:09:21 -0500366 eventActions.size())
Matthew Barthbfb1a562017-10-05 17:03:40 -0500367 {
368 // TODO openbmc/openbmc#2328 - Use the action function target
369 // for comparison
Matthew Barth3e1bb272020-05-26 11:09:21 -0500370 auto actsEqual = [](auto const& a1, auto const& a2) {
371 return a1.target_type().name() == a2.target_type().name();
372 };
373 if (std::equal(eventActions.begin(), eventActions.end(),
Matthew Barthbfb1a562017-10-05 17:03:40 -0500374 std::get<eventActionsPos>(teEventData).begin(),
375 actsEqual))
376 {
377 return it;
378 }
379 }
380 }
381
Matthew Barthd7b716a2018-11-16 13:37:57 -0600382 return eventTimers.end();
Matthew Barthbfb1a562017-10-05 17:03:40 -0500383}
384
Matthew Barth3e1bb272020-05-26 11:09:21 -0500385void Zone::addTimer(const std::string& name, const Group& group,
386 const std::vector<Action>& actions, const TimerConf& tConf)
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700387{
Matthew Barth3e1bb272020-05-26 11:09:21 -0500388 auto eventData = std::make_unique<EventData>(group, "", nullptr, actions);
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700389 Timer timer(
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700390 _eventLoop,
Matthew Barth3e1bb272020-05-26 11:09:21 -0500391 std::bind(&Zone::timerExpired, this,
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700392 std::cref(std::get<Group>(*eventData)),
393 std::cref(std::get<std::vector<Action>>(*eventData))));
394 if (std::get<TimerType>(tConf) == TimerType::repeating)
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700395 {
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700396 timer.restart(std::get<intervalPos>(tConf));
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700397 }
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700398 else if (std::get<TimerType>(tConf) == TimerType::oneshot)
399 {
400 timer.restartOnce(std::get<intervalPos>(tConf));
401 }
402 else
403 {
404 throw std::invalid_argument("Invalid Timer Type");
405 }
Matthew Barthd7b716a2018-11-16 13:37:57 -0600406 _timerEvents[name].emplace_back(std::move(eventData), std::move(timer));
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700407}
408
William A. Kennington IIIc0c5f072018-10-30 19:11:01 -0700409void Zone::timerExpired(const Group& eventGroup,
410 const std::vector<Action>& eventActions)
Matthew Barth90149802017-08-15 10:51:37 -0500411{
Matthew Barthf9201ab2017-09-11 16:07:58 -0500412 // Perform the actions
Matthew Barth3e1bb272020-05-26 11:09:21 -0500413 std::for_each(
414 eventActions.begin(), eventActions.end(),
415 [this, &eventGroup](auto const& action) { action(*this, eventGroup); });
Matthew Barth90149802017-08-15 10:51:37 -0500416}
417
Patrick Williamscb356d42022-07-22 19:26:53 -0500418void Zone::handleEvent(sdbusplus::message_t& msg, const EventData* eventData)
Matthew Barth38a93a82017-05-11 14:12:27 -0500419{
420 // Handle the callback
Matthew Barth3e1bb272020-05-26 11:09:21 -0500421 std::get<eventHandlerPos> (*eventData)(_bus, msg, *this);
Matthew Barthf9201ab2017-09-11 16:07:58 -0500422 // Perform the actions
Matthew Barth3e1bb272020-05-26 11:09:21 -0500423 std::for_each(std::get<eventActionsPos>(*eventData).begin(),
424 std::get<eventActionsPos>(*eventData).end(),
425 [this, &eventData](auto const& action) {
426 action(*this, std::get<eventGroupPos>(*eventData));
427 });
Matthew Barth38a93a82017-05-11 14:12:27 -0500428}
429
Matthew Bartha603ed02018-01-19 16:56:26 -0600430const std::string& Zone::getService(const std::string& path,
431 const std::string& intf)
432{
433 // Retrieve service from cache
434 auto srvIter = _servTree.find(path);
435 if (srvIter != _servTree.end())
436 {
437 for (auto& serv : srvIter->second)
438 {
439 auto it = std::find_if(
Matthew Barth3e1bb272020-05-26 11:09:21 -0500440 serv.second.begin(), serv.second.end(),
441 [&intf](auto const& interface) { return intf == interface; });
Matthew Bartha603ed02018-01-19 16:56:26 -0600442 if (it != std::end(serv.second))
443 {
444 // Service found
445 return serv.first;
446 }
447 }
448 // Interface not found in cache, add and return
449 return addServices(path, intf, 0);
450 }
451 else
452 {
453 // Path not found in cache, add and return
454 return addServices(path, intf, 0);
455 }
456}
457
458const std::string& Zone::addServices(const std::string& path,
Matthew Barth3e1bb272020-05-26 11:09:21 -0500459 const std::string& intf, int32_t depth)
Matthew Bartha603ed02018-01-19 16:56:26 -0600460{
461 static const std::string empty = "";
462 auto it = _servTree.end();
463
464 // Get all subtree objects for the given interface
465 auto objects = util::SDBusPlus::getSubTree(_bus, "/", intf, depth);
466 // Add what's returned to the cache of path->services
467 for (auto& pIter : objects)
468 {
469 auto pathIter = _servTree.find(pIter.first);
470 if (pathIter != _servTree.end())
471 {
472 // Path found in cache
473 for (auto& sIter : pIter.second)
474 {
475 auto servIter = pathIter->second.find(sIter.first);
476 if (servIter != pathIter->second.end())
477 {
478 // Service found in cache
479 for (auto& iIter : sIter.second)
480 {
Matthew Barthe8b340b2018-11-09 10:05:34 -0600481 if (std::find(servIter->second.begin(),
482 servIter->second.end(),
483 iIter) == servIter->second.end())
484 {
485 // Add interface to cache
486 servIter->second.emplace_back(iIter);
487 }
Matthew Bartha603ed02018-01-19 16:56:26 -0600488 }
489 }
490 else
491 {
492 // Service not found in cache
493 pathIter->second.insert(sIter);
494 }
495 }
496 }
497 else
498 {
499 _servTree.insert(pIter);
500 }
501 // When the paths match, since a single interface constraint is given,
502 // that is the service to return
503 if (path == pIter.first)
504 {
505 it = _servTree.find(pIter.first);
506 }
507 }
508
509 if (it != _servTree.end())
510 {
511 return it->second.begin()->first;
512 }
513
514 return empty;
515}
516
Matthew Barth3e1bb272020-05-26 11:09:21 -0500517auto Zone::getPersisted(const std::string& intf, const std::string& prop)
Matthew Barth70b2e7d2019-02-18 11:03:07 -0600518{
519 auto persisted = false;
520
521 auto it = _persisted.find(intf);
522 if (it != _persisted.end())
523 {
Matthew Barth3e1bb272020-05-26 11:09:21 -0500524 return std::any_of(it->second.begin(), it->second.end(),
Matthew Barth9f93bd32020-05-28 16:57:14 -0500525 [&prop](const auto& p) { return prop == p; });
Matthew Barth70b2e7d2019-02-18 11:03:07 -0600526 }
527
528 return persisted;
529}
530
Matthew Barth6faf8942019-01-22 09:26:09 -0600531std::string Zone::current(std::string value)
532{
Matthew Barthb390df12019-02-14 15:18:27 -0600533 auto current = ThermalObject::current();
534 std::transform(value.begin(), value.end(), value.begin(), toupper);
535
Matthew Barth221c90c2019-02-15 10:35:50 -0600536 auto supported = ThermalObject::supported();
Matthew Barth3e1bb272020-05-26 11:09:21 -0500537 auto isSupported =
538 std::any_of(supported.begin(), supported.end(), [&value](auto& s) {
Matthew Barth221c90c2019-02-15 10:35:50 -0600539 std::transform(s.begin(), s.end(), s.begin(), toupper);
540 return value == s;
541 });
542
543 if (value != current && isSupported)
Matthew Barth6faf8942019-01-22 09:26:09 -0600544 {
545 current = ThermalObject::current(value);
Matthew Barth70b2e7d2019-02-18 11:03:07 -0600546 if (getPersisted("xyz.openbmc_project.Control.ThermalMode", "Current"))
547 {
548 saveCurrentMode();
549 }
Matthew Barthb390df12019-02-14 15:18:27 -0600550 // Trigger event(s) for current mode property change
Matthew Barth3e1bb272020-05-26 11:09:21 -0500551 auto eData = _objects[_path]["xyz.openbmc_project.Control.ThermalMode"]
Matthew Barthbaea6c32019-01-29 14:20:19 -0600552 ["Current"];
553 if (eData != nullptr)
554 {
Patrick Williamscb356d42022-07-22 19:26:53 -0500555 sdbusplus::message_t nullMsg{nullptr};
Matthew Barthbaea6c32019-01-29 14:20:19 -0600556 handleEvent(nullMsg, eData);
557 }
Matthew Barth6faf8942019-01-22 09:26:09 -0600558 }
Matthew Barthb390df12019-02-14 15:18:27 -0600559
Matthew Barth6faf8942019-01-22 09:26:09 -0600560 return current;
561}
562
Matthew Barthcc8912e2019-01-21 11:35:27 -0600563void Zone::saveCurrentMode()
564{
565 fs::path path{CONTROL_PERSIST_ROOT_PATH};
566 // Append zone and property description
567 path /= std::to_string(_zoneNum);
568 path /= "CurrentMode";
569 std::ofstream ofs(path.c_str(), std::ios::binary);
570 cereal::JSONOutputArchive oArch(ofs);
571 oArch(ThermalObject::current());
572}
573
Matthew Barth9e4db252019-01-21 13:08:02 -0600574void Zone::restoreCurrentMode()
575{
Matthew Bartha2bed6e2019-02-14 16:07:00 -0600576 auto current = ThermalObject::current();
Matthew Barth9e4db252019-01-21 13:08:02 -0600577 fs::path path{CONTROL_PERSIST_ROOT_PATH};
578 path /= std::to_string(_zoneNum);
579 path /= "CurrentMode";
580 fs::create_directories(path.parent_path());
581
582 try
583 {
584 if (fs::exists(path))
585 {
586 std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary);
587 cereal::JSONInputArchive iArch(ifs);
588 iArch(current);
589 }
590 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500591 catch (const std::exception& e)
Matthew Barth9e4db252019-01-21 13:08:02 -0600592 {
593 log<level::ERR>(e.what());
594 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