17f88fe61SMatt Spinler /**
27f88fe61SMatt Spinler  * Copyright © 2017 IBM Corporation
37f88fe61SMatt Spinler  *
47f88fe61SMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
57f88fe61SMatt Spinler  * you may not use this file except in compliance with the License.
67f88fe61SMatt Spinler  * You may obtain a copy of the License at
77f88fe61SMatt Spinler  *
87f88fe61SMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
97f88fe61SMatt Spinler  *
107f88fe61SMatt Spinler  * Unless required by applicable law or agreed to in writing, software
117f88fe61SMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
127f88fe61SMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137f88fe61SMatt Spinler  * See the License for the specific language governing permissions and
147f88fe61SMatt Spinler  * limitations under the License.
157f88fe61SMatt Spinler  */
16cc8912e9SMatthew Barth #include "config.h"
173e1bb274SMatthew Barth 
187f88fe61SMatt Spinler #include "zone.hpp"
193e1bb274SMatthew Barth 
20d953bb25SMatthew Barth #include "sdbusplus.hpp"
213e1bb274SMatthew Barth #include "utility.hpp"
223e1bb274SMatthew Barth 
233e1bb274SMatthew Barth #include <cereal/archives/json.hpp>
243e1bb274SMatthew Barth #include <cereal/cereal.hpp>
253e1bb274SMatthew Barth #include <phosphor-logging/elog-errors.hpp>
263e1bb274SMatthew Barth #include <phosphor-logging/elog.hpp>
273e1bb274SMatthew Barth #include <phosphor-logging/log.hpp>
283e1bb274SMatthew Barth #include <xyz/openbmc_project/Common/error.hpp>
293e1bb274SMatthew Barth 
303e1bb274SMatthew Barth #include <chrono>
312ea9a596SMatt Spinler #include <filesystem>
323e1bb274SMatthew Barth #include <fstream>
333e1bb274SMatthew Barth #include <functional>
343e1bb274SMatthew Barth #include <stdexcept>
357f88fe61SMatt Spinler 
367f88fe61SMatt Spinler namespace phosphor
377f88fe61SMatt Spinler {
387f88fe61SMatt Spinler namespace fan
397f88fe61SMatt Spinler {
407f88fe61SMatt Spinler namespace control
417f88fe61SMatt Spinler {
427f88fe61SMatt Spinler 
438600d9a0SMatthew Barth using namespace std::chrono;
449014980aSMatthew Barth using namespace phosphor::fan;
45df3e8d67SMatthew Barth using namespace phosphor::logging;
462ea9a596SMatt Spinler namespace fs = std::filesystem;
473e1bb274SMatthew Barth using InternalFailure =
483e1bb274SMatthew Barth     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
497f88fe61SMatt Spinler 
Zone(Mode mode,sdbusplus::bus_t & bus,const std::string & path,const sdeventplus::Event & event,const ZoneDefinition & def)50cb356d48SPatrick Williams Zone::Zone(Mode mode, sdbusplus::bus_t& bus, const std::string& path,
513e1bb274SMatthew Barth            const sdeventplus::Event& event, const ZoneDefinition& def) :
520850e312SPatrick Williams     ThermalObject(bus, path.c_str(), ThermalObject::action::defer_emit),
533e1bb274SMatthew Barth     _bus(bus), _path(path),
54766f8545SMatthew Barth     _ifaces({"xyz.openbmc_project.Control.ThermalMode"}),
557f88fe61SMatt Spinler     _fullSpeed(std::get<fullSpeedPos>(def)),
561de66629SMatthew Barth     _zoneNum(std::get<zoneNumPos>(def)),
57e0ca13ebSMatthew Barth     _defFloorSpeed(std::get<floorSpeedPos>(def)),
588600d9a0SMatthew Barth     _defCeilingSpeed(std::get<fullSpeedPos>(def)),
59a956184bSMatthew Barth     _incDelay(std::get<incDelayPos>(def)),
60a956184bSMatthew Barth     _decInterval(std::get<decIntervalPos>(def)),
6122c36ab6SWilliam A. Kennington III     _incTimer(event, std::bind(&Zone::incTimerExpired, this)),
623e1bb274SMatthew Barth     _decTimer(event, std::bind(&Zone::decTimerExpired, this)), _eventLoop(event)
637f88fe61SMatt Spinler {
647f88fe61SMatt Spinler     auto& fanDefs = std::get<fanListPos>(def);
657f88fe61SMatt Spinler 
669f93bd35SMatthew Barth     for (auto& fanDef : fanDefs)
677f88fe61SMatt Spinler     {
689f93bd35SMatthew Barth         _fans.emplace_back(std::make_unique<Fan>(bus, fanDef));
697f88fe61SMatt Spinler     }
7038a93a8aSMatthew Barth 
7114184131SMatthew Barth     // Do not enable set speed events when in init mode
7293af4194SMatthew Barth     if (mode == Mode::control)
7314184131SMatthew Barth     {
741b3e9602SMatthew Barth         // Process any zone handlers defined
751b3e9602SMatthew Barth         for (auto& hand : std::get<handlerPos>(def))
761b3e9602SMatthew Barth         {
771b3e9602SMatthew Barth             hand(*this);
781b3e9602SMatthew Barth         }
791b3e9602SMatthew Barth 
809e4db25cSMatthew Barth         // Restore thermal control current mode state
819e4db25cSMatthew Barth         restoreCurrentMode();
8293af4194SMatthew Barth 
8393af4194SMatthew Barth         // Emit objects added in control mode only
8493af4194SMatthew Barth         this->emit_object_added();
8593af4194SMatthew Barth 
862b3db618SMatthew Barth         // Update target speed to current zone target speed
872b3db618SMatthew Barth         if (!_fans.empty())
882b3db618SMatthew Barth         {
892b3db618SMatthew Barth             _targetSpeed = _fans.front()->getTargetSpeed();
902b3db618SMatthew Barth         }
91ccc7770eSMatthew Barth         // Setup signal trigger for set speed events
929f93bd35SMatthew Barth         for (auto& ssEvent : std::get<setSpeedEventsPos>(def))
93ccc7770eSMatthew Barth         {
949f93bd35SMatthew Barth             initEvent(ssEvent);
95ccc7770eSMatthew Barth         }
968600d9a0SMatthew Barth         // Start timer for fan speed decreases
978fd879fbSWilliam A. Kennington III         _decTimer.restart(_decInterval);
987f88fe61SMatt Spinler     }
9914184131SMatthew Barth }
1007f88fe61SMatt Spinler 
setSpeed(uint64_t speed)1017f88fe61SMatt Spinler void Zone::setSpeed(uint64_t speed)
1027f88fe61SMatt Spinler {
10360b00766SMatthew Barth     if (_isActive)
10460b00766SMatthew Barth     {
10560b00766SMatthew Barth         _targetSpeed = speed;
1067f88fe61SMatt Spinler         for (auto& fan : _fans)
1077f88fe61SMatt Spinler         {
10860b00766SMatthew Barth             fan->setSpeed(_targetSpeed);
10960b00766SMatthew Barth         }
11060b00766SMatthew Barth     }
11160b00766SMatthew Barth }
11260b00766SMatthew Barth 
setFullSpeed()11360b00766SMatthew Barth void Zone::setFullSpeed()
11460b00766SMatthew Barth {
11560b00766SMatthew Barth     if (_fullSpeed != 0)
11660b00766SMatthew Barth     {
11760b00766SMatthew Barth         _targetSpeed = _fullSpeed;
11860b00766SMatthew Barth         for (auto& fan : _fans)
11960b00766SMatthew Barth         {
12060b00766SMatthew Barth             fan->setSpeed(_targetSpeed);
12160b00766SMatthew Barth         }
1227f88fe61SMatt Spinler     }
1237f88fe61SMatt Spinler }
1247f88fe61SMatt Spinler 
setActiveAllow(const Group * group,bool isActiveAllow)125861d77c3SMatthew Barth void Zone::setActiveAllow(const Group* group, bool isActiveAllow)
126861d77c3SMatthew Barth {
12760b00766SMatthew Barth     _active[*(group)] = isActiveAllow;
128861d77c3SMatthew Barth     if (!isActiveAllow)
129861d77c3SMatthew Barth     {
130861d77c3SMatthew Barth         _isActive = false;
131861d77c3SMatthew Barth     }
132861d77c3SMatthew Barth     else
133861d77c3SMatthew Barth     {
134861d77c3SMatthew Barth         // Check all entries are set to allow control active
13561b73296SPatrick Williams         auto actPred = [](const auto& entry) { return entry.second; };
1363e1bb274SMatthew Barth         _isActive = std::all_of(_active.begin(), _active.end(), actPred);
137861d77c3SMatthew Barth     }
138861d77c3SMatthew Barth }
139861d77c3SMatthew Barth 
removeService(const Group * group,const std::string & name)1403e1bb274SMatthew Barth void Zone::removeService(const Group* group, const std::string& name)
14155dea643SMatthew Barth {
14255dea643SMatthew Barth     try
14355dea643SMatthew Barth     {
14455dea643SMatthew Barth         auto& sNames = _services.at(*group);
1453e1bb274SMatthew Barth         auto it = std::find_if(sNames.begin(), sNames.end(),
14661b73296SPatrick Williams                                [&name](const auto& entry) {
14755dea643SMatthew Barth             return name == std::get<namePos>(entry);
1483e1bb274SMatthew Barth         });
14955dea643SMatthew Barth         if (it != std::end(sNames))
15055dea643SMatthew Barth         {
15155dea643SMatthew Barth             // Remove service name from group
15255dea643SMatthew Barth             sNames.erase(it);
15355dea643SMatthew Barth         }
15455dea643SMatthew Barth     }
15555dea643SMatthew Barth     catch (const std::out_of_range& oore)
15655dea643SMatthew Barth     {
15755dea643SMatthew Barth         // No services for group found
15855dea643SMatthew Barth     }
15955dea643SMatthew Barth }
16055dea643SMatthew Barth 
setServiceOwner(const Group * group,const std::string & name,const bool hasOwner)1613e1bb274SMatthew Barth void Zone::setServiceOwner(const Group* group, const std::string& name,
162e59fdf70SMatthew Barth                            const bool hasOwner)
163e59fdf70SMatthew Barth {
164e59fdf70SMatthew Barth     try
165e59fdf70SMatthew Barth     {
166e59fdf70SMatthew Barth         auto& sNames = _services.at(*group);
1673e1bb274SMatthew Barth         auto it = std::find_if(sNames.begin(), sNames.end(),
16861b73296SPatrick Williams                                [&name](const auto& entry) {
169e59fdf70SMatthew Barth             return name == std::get<namePos>(entry);
1703e1bb274SMatthew Barth         });
171e59fdf70SMatthew Barth         if (it != std::end(sNames))
172e59fdf70SMatthew Barth         {
173e59fdf70SMatthew Barth             std::get<hasOwnerPos>(*it) = hasOwner;
174e59fdf70SMatthew Barth         }
175e59fdf70SMatthew Barth         else
176e59fdf70SMatthew Barth         {
177e59fdf70SMatthew Barth             _services[*group].emplace_back(name, hasOwner);
178e59fdf70SMatthew Barth         }
179e59fdf70SMatthew Barth     }
180e59fdf70SMatthew Barth     catch (const std::out_of_range& oore)
181e59fdf70SMatthew Barth     {
182e59fdf70SMatthew Barth         _services[*group].emplace_back(name, hasOwner);
183e59fdf70SMatthew Barth     }
184e59fdf70SMatthew Barth }
185e59fdf70SMatthew Barth 
setServices(const Group * group)186480787c1SMatthew Barth void Zone::setServices(const Group* group)
187480787c1SMatthew Barth {
18855dea643SMatthew Barth     // Remove the empty service name if exists
18955dea643SMatthew Barth     removeService(group, "");
190480787c1SMatthew Barth     for (auto it = group->begin(); it != group->end(); ++it)
191480787c1SMatthew Barth     {
192480787c1SMatthew Barth         std::string name;
193480787c1SMatthew Barth         bool hasOwner = false;
194480787c1SMatthew Barth         try
195480787c1SMatthew Barth         {
1963e1bb274SMatthew Barth             name = getService(std::get<pathPos>(*it), std::get<intfPos>(*it));
197480787c1SMatthew Barth             hasOwner = util::SDBusPlus::callMethodAndRead<bool>(
1983e1bb274SMatthew Barth                 _bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
1993e1bb274SMatthew Barth                 "org.freedesktop.DBus", "NameHasOwner", name);
200480787c1SMatthew Barth         }
201ba7b5feaSMatt Spinler         catch (const util::DBusMethodError& e)
202480787c1SMatthew Barth         {
203480787c1SMatthew Barth             // Failed to get service name owner state
204480787c1SMatthew Barth             hasOwner = false;
205480787c1SMatthew Barth         }
206480787c1SMatthew Barth         setServiceOwner(group, name, hasOwner);
207480787c1SMatthew Barth     }
208480787c1SMatthew Barth }
209480787c1SMatthew Barth 
setFloor(uint64_t speed)210b4a7cb99SMatthew Barth void Zone::setFloor(uint64_t speed)
211b4a7cb99SMatthew Barth {
21298726c45SMatthew Barth     // Check all entries are set to allow floor to be set
21361b73296SPatrick Williams     auto pred = [](const auto& entry) { return entry.second; };
2143e1bb274SMatthew Barth     auto setFloor = std::all_of(_floorChange.begin(), _floorChange.end(), pred);
21598726c45SMatthew Barth     if (setFloor)
21698726c45SMatthew Barth     {
217b4a7cb99SMatthew Barth         _floorSpeed = speed;
218b4a7cb99SMatthew Barth         // Floor speed above target, update target to floor speed
219b4a7cb99SMatthew Barth         if (_targetSpeed < _floorSpeed)
220b4a7cb99SMatthew Barth         {
221b4a7cb99SMatthew Barth             requestSpeedIncrease(_floorSpeed - _targetSpeed);
222b4a7cb99SMatthew Barth         }
223b4a7cb99SMatthew Barth     }
22498726c45SMatthew Barth }
225b4a7cb99SMatthew Barth 
requestSpeedIncrease(uint64_t targetDelta)226240397b9SMatthew Barth void Zone::requestSpeedIncrease(uint64_t targetDelta)
227240397b9SMatthew Barth {
228240397b9SMatthew Barth     // Only increase speed when delta is higher than
229240397b9SMatthew Barth     // the current increase delta for the zone and currently under ceiling
2303e1bb274SMatthew Barth     if (targetDelta > _incSpeedDelta && _targetSpeed < _ceilingSpeed)
231240397b9SMatthew Barth     {
2324e728542SMatthew Barth         auto requestTarget = getRequestSpeedBase();
23360b00766SMatthew Barth         requestTarget = (targetDelta - _incSpeedDelta) + requestTarget;
234240397b9SMatthew Barth         _incSpeedDelta = targetDelta;
235240397b9SMatthew Barth         // Target speed can not go above a defined ceiling speed
23660b00766SMatthew Barth         if (requestTarget > _ceilingSpeed)
237240397b9SMatthew Barth         {
23860b00766SMatthew Barth             requestTarget = _ceilingSpeed;
239240397b9SMatthew Barth         }
24060b00766SMatthew Barth         setSpeed(requestTarget);
2418fd879fbSWilliam A. Kennington III         // Retart timer countdown for fan speed increase
2428fd879fbSWilliam A. Kennington III         _incTimer.restartOnce(_incDelay);
2431ee48f2bSMatthew Barth     }
2441ee48f2bSMatthew Barth }
2451ee48f2bSMatthew Barth 
incTimerExpired()2461ee48f2bSMatthew Barth void Zone::incTimerExpired()
2471ee48f2bSMatthew Barth {
2481ee48f2bSMatthew Barth     // Clear increase delta when timer expires allowing additional speed
2491ee48f2bSMatthew Barth     // increase requests or speed decreases to occur
250240397b9SMatthew Barth     _incSpeedDelta = 0;
251240397b9SMatthew Barth }
252240397b9SMatthew Barth 
requestSpeedDecrease(uint64_t targetDelta)2530ce99d8bSMatthew Barth void Zone::requestSpeedDecrease(uint64_t targetDelta)
2540ce99d8bSMatthew Barth {
2550ce99d8bSMatthew Barth     // Only decrease the lowest target delta requested
2560ce99d8bSMatthew Barth     if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta)
2570ce99d8bSMatthew Barth     {
2580ce99d8bSMatthew Barth         _decSpeedDelta = targetDelta;
2590ce99d8bSMatthew Barth     }
2608600d9a0SMatthew Barth }
2610ce99d8bSMatthew Barth 
decTimerExpired()2628600d9a0SMatthew Barth void Zone::decTimerExpired()
2638600d9a0SMatthew Barth {
264e4338cdbSMatthew Barth     // Check all entries are set to allow a decrease
26561b73296SPatrick Williams     auto pred = [](const auto& entry) { return entry.second; };
2663e1bb274SMatthew Barth     auto decAllowed = std::all_of(_decAllowed.begin(), _decAllowed.end(), pred);
267e4338cdbSMatthew Barth 
268e4338cdbSMatthew Barth     // Only decrease speeds when allowed,
269d4c001f2SMatthew Barth     // a requested decrease speed delta exists,
270e4338cdbSMatthew Barth     // where no requested increases exist and
271e4338cdbSMatthew Barth     // the increase timer is not running
272e4338cdbSMatthew Barth     // (i.e. not in the middle of increasing)
2733e1bb274SMatthew Barth     if (decAllowed && _decSpeedDelta != 0 && _incSpeedDelta == 0 &&
2743e1bb274SMatthew Barth         !_incTimer.isEnabled())
2750ce99d8bSMatthew Barth     {
2764e728542SMatthew Barth         auto requestTarget = getRequestSpeedBase();
277c63973a1SMatthew Barth         // Request target speed should not start above ceiling
278c63973a1SMatthew Barth         if (requestTarget > _ceilingSpeed)
279c63973a1SMatthew Barth         {
280c63973a1SMatthew Barth             requestTarget = _ceilingSpeed;
281c63973a1SMatthew Barth         }
2820ce99d8bSMatthew Barth         // Target speed can not go below the defined floor speed
28360b00766SMatthew Barth         if ((requestTarget < _decSpeedDelta) ||
28460b00766SMatthew Barth             (requestTarget - _decSpeedDelta < _floorSpeed))
2850ce99d8bSMatthew Barth         {
28660b00766SMatthew Barth             requestTarget = _floorSpeed;
2870ce99d8bSMatthew Barth         }
2880ce99d8bSMatthew Barth         else
2890ce99d8bSMatthew Barth         {
29060b00766SMatthew Barth             requestTarget = requestTarget - _decSpeedDelta;
2910ce99d8bSMatthew Barth         }
29260b00766SMatthew Barth         setSpeed(requestTarget);
2930ce99d8bSMatthew Barth     }
2940ce99d8bSMatthew Barth     // Clear decrease delta when timer expires
2950ce99d8bSMatthew Barth     _decSpeedDelta = 0;
2968600d9a0SMatthew Barth     // Decrease timer is restarted since its repeating
2970ce99d8bSMatthew Barth }
2980ce99d8bSMatthew Barth 
initEvent(const SetSpeedEvent & event)299ccc7770eSMatthew Barth void Zone::initEvent(const SetSpeedEvent& event)
3001bf0ce4bSMatthew Barth {
3011b4de26aSMatthew Barth     // Enable event triggers
30261b73296SPatrick Williams     std::for_each(std::get<triggerPos>(event).begin(),
30361b73296SPatrick Williams                   std::get<triggerPos>(event).end(),
30461b73296SPatrick Williams                   [this, &event](const auto& trigger) {
30575d97350SMatthew Barth         if (!std::get<actionsPos>(event).empty())
30675d97350SMatthew Barth         {
30761b73296SPatrick Williams             std::for_each(std::get<actionsPos>(event).begin(),
30875d97350SMatthew Barth                           std::get<actionsPos>(event).end(),
3093e1bb274SMatthew Barth                           [this, &trigger, &event](auto const& action) {
31075d97350SMatthew Barth                 // Default to use group defined with action if exists
31175d97350SMatthew Barth                 if (!std::get<adGroupPos>(action).empty())
31275d97350SMatthew Barth                 {
3133e1bb274SMatthew Barth                     trigger(*this, std::get<sseNamePos>(event),
31475d97350SMatthew Barth                             std::get<adGroupPos>(action),
31575d97350SMatthew Barth                             std::get<adActionsPos>(action));
31675d97350SMatthew Barth                 }
31775d97350SMatthew Barth                 else
31875d97350SMatthew Barth                 {
3193e1bb274SMatthew Barth                     trigger(*this, std::get<sseNamePos>(event),
3201b4de26aSMatthew Barth                             std::get<groupPos>(event),
32175d97350SMatthew Barth                             std::get<adActionsPos>(action));
32275d97350SMatthew Barth                 }
3233e1bb274SMatthew Barth             });
32475d97350SMatthew Barth         }
32575d97350SMatthew Barth         else
32675d97350SMatthew Barth         {
3273e1bb274SMatthew Barth             trigger(*this, std::get<sseNamePos>(event),
3283e1bb274SMatthew Barth                     std::get<groupPos>(event), {});
32975d97350SMatthew Barth         }
3303e1bb274SMatthew Barth     });
3311bf0ce4bSMatthew Barth }
3321bf0ce4bSMatthew Barth 
removeEvent(const SetSpeedEvent & event)333f6b76d8eSMatthew Barth void Zone::removeEvent(const SetSpeedEvent& event)
334f6b76d8eSMatthew Barth {
33579cb8312SMatthew Barth     // Remove event signals
33679cb8312SMatthew Barth     auto sigIter = _signalEvents.find(std::get<sseNamePos>(event));
33779cb8312SMatthew Barth     if (sigIter != _signalEvents.end())
338f6b76d8eSMatthew Barth     {
33979cb8312SMatthew Barth         auto& signals = sigIter->second;
34079cb8312SMatthew Barth         for (auto it = signals.begin(); it != signals.end(); ++it)
341f9201abbSMatthew Barth         {
34233bfe761SMatthew Barth             removeSignal(it);
343f9201abbSMatthew Barth         }
34479cb8312SMatthew Barth         _signalEvents.erase(sigIter);
34533bfe761SMatthew Barth     }
34679cb8312SMatthew Barth 
347d7b716a6SMatthew Barth     // Remove event timers
348d7b716a6SMatthew Barth     auto timIter = _timerEvents.find(std::get<sseNamePos>(event));
349d7b716a6SMatthew Barth     if (timIter != _timerEvents.end())
35033bfe761SMatthew Barth     {
351d7b716a6SMatthew Barth         _timerEvents.erase(timIter);
35233bfe761SMatthew Barth     }
35333bfe761SMatthew Barth }
35433bfe761SMatthew Barth 
3553e1bb274SMatthew Barth std::vector<TimerEvent>::iterator
findTimer(const Group & eventGroup,const std::vector<Action> & eventActions,std::vector<TimerEvent> & eventTimers)3563e1bb274SMatthew Barth     Zone::findTimer(const Group& eventGroup,
357d7b716a6SMatthew Barth                     const std::vector<Action>& eventActions,
358d7b716a6SMatthew Barth                     std::vector<TimerEvent>& eventTimers)
359bfb1a566SMatthew Barth {
360d7b716a6SMatthew Barth     for (auto it = eventTimers.begin(); it != eventTimers.end(); ++it)
361bfb1a566SMatthew Barth     {
3620420a932SWilliam A. Kennington III         const auto& teEventData = *std::get<timerEventDataPos>(*it);
363d7b716a6SMatthew Barth         if (std::get<eventGroupPos>(teEventData) == eventGroup &&
364d7b716a6SMatthew Barth             std::get<eventActionsPos>(teEventData).size() ==
365bfb1a566SMatthew Barth                 eventActions.size())
366bfb1a566SMatthew Barth         {
367bfb1a566SMatthew Barth             // TODO openbmc/openbmc#2328 - Use the action function target
368bfb1a566SMatthew Barth             // for comparison
36961b73296SPatrick Williams             auto actsEqual = [](const auto& a1, const auto& a2) {
3703e1bb274SMatthew Barth                 return a1.target_type().name() == a2.target_type().name();
371bfb1a566SMatthew Barth             };
3723e1bb274SMatthew Barth             if (std::equal(eventActions.begin(), eventActions.end(),
373bfb1a566SMatthew Barth                            std::get<eventActionsPos>(teEventData).begin(),
374bfb1a566SMatthew Barth                            actsEqual))
375bfb1a566SMatthew Barth             {
376bfb1a566SMatthew Barth                 return it;
377bfb1a566SMatthew Barth             }
378bfb1a566SMatthew Barth         }
379bfb1a566SMatthew Barth     }
380bfb1a566SMatthew Barth 
381d7b716a6SMatthew Barth     return eventTimers.end();
382bfb1a566SMatthew Barth }
383bfb1a566SMatthew Barth 
addTimer(const std::string & name,const Group & group,const std::vector<Action> & actions,const TimerConf & tConf)3843e1bb274SMatthew Barth void Zone::addTimer(const std::string& name, const Group& group,
3853e1bb274SMatthew Barth                     const std::vector<Action>& actions, const TimerConf& tConf)
38694fe1a0cSWilliam A. Kennington III {
3873e1bb274SMatthew Barth     auto eventData = std::make_unique<EventData>(group, "", nullptr, actions);
3888fd879fbSWilliam A. Kennington III     Timer timer(
38994fe1a0cSWilliam A. Kennington III         _eventLoop,
3903e1bb274SMatthew Barth         std::bind(&Zone::timerExpired, this,
3918fd879fbSWilliam A. Kennington III                   std::cref(std::get<Group>(*eventData)),
3928fd879fbSWilliam A. Kennington III                   std::cref(std::get<std::vector<Action>>(*eventData))));
3938fd879fbSWilliam A. Kennington III     if (std::get<TimerType>(tConf) == TimerType::repeating)
39494fe1a0cSWilliam A. Kennington III     {
3958fd879fbSWilliam A. Kennington III         timer.restart(std::get<intervalPos>(tConf));
39694fe1a0cSWilliam A. Kennington III     }
3978fd879fbSWilliam A. Kennington III     else if (std::get<TimerType>(tConf) == TimerType::oneshot)
3988fd879fbSWilliam A. Kennington III     {
3998fd879fbSWilliam A. Kennington III         timer.restartOnce(std::get<intervalPos>(tConf));
4008fd879fbSWilliam A. Kennington III     }
4018fd879fbSWilliam A. Kennington III     else
4028fd879fbSWilliam A. Kennington III     {
4038fd879fbSWilliam A. Kennington III         throw std::invalid_argument("Invalid Timer Type");
4048fd879fbSWilliam A. Kennington III     }
405d7b716a6SMatthew Barth     _timerEvents[name].emplace_back(std::move(eventData), std::move(timer));
40694fe1a0cSWilliam A. Kennington III }
40794fe1a0cSWilliam A. Kennington III 
timerExpired(const Group & eventGroup,const std::vector<Action> & eventActions)408c0c5f07fSWilliam A. Kennington III void Zone::timerExpired(const Group& eventGroup,
409c0c5f07fSWilliam A. Kennington III                         const std::vector<Action>& eventActions)
4109014980aSMatthew Barth {
411f9201abbSMatthew Barth     // Perform the actions
412*5e15c3baSPatrick Williams     std::for_each(
413*5e15c3baSPatrick Williams         eventActions.begin(), eventActions.end(),
414*5e15c3baSPatrick Williams         [this, &eventGroup](const auto& action) { action(*this, eventGroup); });
4159014980aSMatthew Barth }
4169014980aSMatthew Barth 
handleEvent(sdbusplus::message_t & msg,const EventData * eventData)417cb356d48SPatrick Williams void Zone::handleEvent(sdbusplus::message_t& msg, const EventData* eventData)
41838a93a8aSMatthew Barth {
41938a93a8aSMatthew Barth     // Handle the callback
42034f1bda2SMatthew Barth     std::get<eventHandlerPos> (*eventData)(_bus, msg, *this);
421f9201abbSMatthew Barth     // Perform the actions
4223e1bb274SMatthew Barth     std::for_each(std::get<eventActionsPos>(*eventData).begin(),
423f9201abbSMatthew Barth                   std::get<eventActionsPos>(*eventData).end(),
42461b73296SPatrick Williams                   [this, &eventData](const auto& action) {
4253e1bb274SMatthew Barth         action(*this, std::get<eventGroupPos>(*eventData));
426f9201abbSMatthew Barth     });
42738a93a8aSMatthew Barth }
42838a93a8aSMatthew Barth 
getService(const std::string & path,const std::string & intf)429a603ed01SMatthew Barth const std::string& Zone::getService(const std::string& path,
430a603ed01SMatthew Barth                                     const std::string& intf)
431a603ed01SMatthew Barth {
432a603ed01SMatthew Barth     // Retrieve service from cache
433a603ed01SMatthew Barth     auto srvIter = _servTree.find(path);
434a603ed01SMatthew Barth     if (srvIter != _servTree.end())
435a603ed01SMatthew Barth     {
436a603ed01SMatthew Barth         for (auto& serv : srvIter->second)
437a603ed01SMatthew Barth         {
438*5e15c3baSPatrick Williams             auto it = std::find_if(
439*5e15c3baSPatrick Williams                 serv.second.begin(), serv.second.end(),
440*5e15c3baSPatrick Williams                 [&intf](const auto& interface) { return intf == interface; });
441a603ed01SMatthew Barth             if (it != std::end(serv.second))
442a603ed01SMatthew Barth             {
443a603ed01SMatthew Barth                 // Service found
444a603ed01SMatthew Barth                 return serv.first;
445a603ed01SMatthew Barth             }
446a603ed01SMatthew Barth         }
447a603ed01SMatthew Barth         // Interface not found in cache, add and return
448a603ed01SMatthew Barth         return addServices(path, intf, 0);
449a603ed01SMatthew Barth     }
450a603ed01SMatthew Barth     else
451a603ed01SMatthew Barth     {
452a603ed01SMatthew Barth         // Path not found in cache, add and return
453a603ed01SMatthew Barth         return addServices(path, intf, 0);
454a603ed01SMatthew Barth     }
455a603ed01SMatthew Barth }
456a603ed01SMatthew Barth 
addServices(const std::string & path,const std::string & intf,int32_t depth)457a603ed01SMatthew Barth const std::string& Zone::addServices(const std::string& path,
4583e1bb274SMatthew Barth                                      const std::string& intf, int32_t depth)
459a603ed01SMatthew Barth {
460a603ed01SMatthew Barth     static const std::string empty = "";
461a603ed01SMatthew Barth     auto it = _servTree.end();
462a603ed01SMatthew Barth 
463a603ed01SMatthew Barth     // Get all subtree objects for the given interface
464a603ed01SMatthew Barth     auto objects = util::SDBusPlus::getSubTree(_bus, "/", intf, depth);
465a603ed01SMatthew Barth     // Add what's returned to the cache of path->services
466a603ed01SMatthew Barth     for (auto& pIter : objects)
467a603ed01SMatthew Barth     {
468a603ed01SMatthew Barth         auto pathIter = _servTree.find(pIter.first);
469a603ed01SMatthew Barth         if (pathIter != _servTree.end())
470a603ed01SMatthew Barth         {
471a603ed01SMatthew Barth             // Path found in cache
472a603ed01SMatthew Barth             for (auto& sIter : pIter.second)
473a603ed01SMatthew Barth             {
474a603ed01SMatthew Barth                 auto servIter = pathIter->second.find(sIter.first);
475a603ed01SMatthew Barth                 if (servIter != pathIter->second.end())
476a603ed01SMatthew Barth                 {
477a603ed01SMatthew Barth                     // Service found in cache
478a603ed01SMatthew Barth                     for (auto& iIter : sIter.second)
479a603ed01SMatthew Barth                     {
480e8b340bdSMatthew Barth                         if (std::find(servIter->second.begin(),
481e8b340bdSMatthew Barth                                       servIter->second.end(),
482e8b340bdSMatthew Barth                                       iIter) == servIter->second.end())
483e8b340bdSMatthew Barth                         {
484a603ed01SMatthew Barth                             // Add interface to cache
485a603ed01SMatthew Barth                             servIter->second.emplace_back(iIter);
486a603ed01SMatthew Barth                         }
487a603ed01SMatthew Barth                     }
488e8b340bdSMatthew Barth                 }
489a603ed01SMatthew Barth                 else
490a603ed01SMatthew Barth                 {
491a603ed01SMatthew Barth                     // Service not found in cache
492a603ed01SMatthew Barth                     pathIter->second.insert(sIter);
493a603ed01SMatthew Barth                 }
494a603ed01SMatthew Barth             }
495a603ed01SMatthew Barth         }
496a603ed01SMatthew Barth         else
497a603ed01SMatthew Barth         {
498a603ed01SMatthew Barth             _servTree.insert(pIter);
499a603ed01SMatthew Barth         }
500a603ed01SMatthew Barth         // When the paths match, since a single interface constraint is given,
501a603ed01SMatthew Barth         // that is the service to return
502a603ed01SMatthew Barth         if (path == pIter.first)
503a603ed01SMatthew Barth         {
504a603ed01SMatthew Barth             it = _servTree.find(pIter.first);
505a603ed01SMatthew Barth         }
506a603ed01SMatthew Barth     }
507a603ed01SMatthew Barth 
508a603ed01SMatthew Barth     if (it != _servTree.end())
509a603ed01SMatthew Barth     {
510a603ed01SMatthew Barth         return it->second.begin()->first;
511a603ed01SMatthew Barth     }
512a603ed01SMatthew Barth 
513a603ed01SMatthew Barth     return empty;
514a603ed01SMatthew Barth }
515a603ed01SMatthew Barth 
getPersisted(const std::string & intf,const std::string & prop)5163e1bb274SMatthew Barth auto Zone::getPersisted(const std::string& intf, const std::string& prop)
51770b2e7daSMatthew Barth {
51870b2e7daSMatthew Barth     auto persisted = false;
51970b2e7daSMatthew Barth 
52070b2e7daSMatthew Barth     auto it = _persisted.find(intf);
52170b2e7daSMatthew Barth     if (it != _persisted.end())
52270b2e7daSMatthew Barth     {
5233e1bb274SMatthew Barth         return std::any_of(it->second.begin(), it->second.end(),
5249f93bd35SMatthew Barth                            [&prop](const auto& p) { return prop == p; });
52570b2e7daSMatthew Barth     }
52670b2e7daSMatthew Barth 
52770b2e7daSMatthew Barth     return persisted;
52870b2e7daSMatthew Barth }
52970b2e7daSMatthew Barth 
current(std::string value)5306faf8943SMatthew Barth std::string Zone::current(std::string value)
5316faf8943SMatthew Barth {
532b390df1eSMatthew Barth     auto current = ThermalObject::current();
533b390df1eSMatthew Barth     std::transform(value.begin(), value.end(), value.begin(), toupper);
534b390df1eSMatthew Barth 
535221c90c3SMatthew Barth     auto supported = ThermalObject::supported();
53661b73296SPatrick Williams     auto isSupported = std::any_of(supported.begin(), supported.end(),
53761b73296SPatrick Williams                                    [&value](auto& s) {
538221c90c3SMatthew Barth         std::transform(s.begin(), s.end(), s.begin(), toupper);
539221c90c3SMatthew Barth         return value == s;
540221c90c3SMatthew Barth     });
541221c90c3SMatthew Barth 
542221c90c3SMatthew Barth     if (value != current && isSupported)
5436faf8943SMatthew Barth     {
5446faf8943SMatthew Barth         current = ThermalObject::current(value);
54570b2e7daSMatthew Barth         if (getPersisted("xyz.openbmc_project.Control.ThermalMode", "Current"))
54670b2e7daSMatthew Barth         {
5476faf8943SMatthew Barth             saveCurrentMode();
54870b2e7daSMatthew Barth         }
549b390df1eSMatthew Barth         // Trigger event(s) for current mode property change
5503e1bb274SMatthew Barth         auto eData = _objects[_path]["xyz.openbmc_project.Control.ThermalMode"]
551baea6c3fSMatthew Barth                              ["Current"];
552baea6c3fSMatthew Barth         if (eData != nullptr)
553baea6c3fSMatthew Barth         {
554cb356d48SPatrick Williams             sdbusplus::message_t nullMsg{nullptr};
555baea6c3fSMatthew Barth             handleEvent(nullMsg, eData);
556baea6c3fSMatthew Barth         }
5576faf8943SMatthew Barth     }
558b390df1eSMatthew Barth 
5596faf8943SMatthew Barth     return current;
5606faf8943SMatthew Barth }
5616faf8943SMatthew Barth 
saveCurrentMode()562cc8912e9SMatthew Barth void Zone::saveCurrentMode()
563cc8912e9SMatthew Barth {
564cc8912e9SMatthew Barth     fs::path path{CONTROL_PERSIST_ROOT_PATH};
565cc8912e9SMatthew Barth     // Append zone and property description
566cc8912e9SMatthew Barth     path /= std::to_string(_zoneNum);
567cc8912e9SMatthew Barth     path /= "CurrentMode";
568cc8912e9SMatthew Barth     std::ofstream ofs(path.c_str(), std::ios::binary);
569cc8912e9SMatthew Barth     cereal::JSONOutputArchive oArch(ofs);
570cc8912e9SMatthew Barth     oArch(ThermalObject::current());
571cc8912e9SMatthew Barth }
572cc8912e9SMatthew Barth 
restoreCurrentMode()5739e4db25cSMatthew Barth void Zone::restoreCurrentMode()
5749e4db25cSMatthew Barth {
575a2bed6edSMatthew Barth     auto current = ThermalObject::current();
5769e4db25cSMatthew Barth     fs::path path{CONTROL_PERSIST_ROOT_PATH};
5779e4db25cSMatthew Barth     path /= std::to_string(_zoneNum);
5789e4db25cSMatthew Barth     path /= "CurrentMode";
5799e4db25cSMatthew Barth     fs::create_directories(path.parent_path());
5809e4db25cSMatthew Barth 
5819e4db25cSMatthew Barth     try
5829e4db25cSMatthew Barth     {
5839e4db25cSMatthew Barth         if (fs::exists(path))
5849e4db25cSMatthew Barth         {
5859e4db25cSMatthew Barth             std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary);
5869e4db25cSMatthew Barth             cereal::JSONInputArchive iArch(ifs);
5879e4db25cSMatthew Barth             iArch(current);
5889e4db25cSMatthew Barth         }
5899e4db25cSMatthew Barth     }
590ddb773b2SPatrick Williams     catch (const std::exception& e)
5919e4db25cSMatthew Barth     {
5929e4db25cSMatthew Barth         log<level::ERR>(e.what());
5939e4db25cSMatthew Barth         fs::remove(path);
594a2bed6edSMatthew Barth         current = ThermalObject::current();
5959e4db25cSMatthew Barth     }
5969e4db25cSMatthew Barth 
5979e4db25cSMatthew Barth     this->current(current);
5989e4db25cSMatthew Barth }
5999e4db25cSMatthew Barth 
6003e1bb274SMatthew Barth } // namespace control
6013e1bb274SMatthew Barth } // namespace fan
6023e1bb274SMatthew Barth } // namespace phosphor
603