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  */
168600d9a0SMatthew Barth #include <chrono>
1722c36ab6SWilliam A. Kennington III #include <functional>
18cc8912e9SMatthew Barth #include <fstream>
19cc8912e9SMatthew Barth #include <cereal/cereal.hpp>
20cc8912e9SMatthew Barth #include <cereal/archives/json.hpp>
21cc8912e9SMatthew Barth #include <experimental/filesystem>
22618027abSDinesh Chinari #include <phosphor-logging/log.hpp>
23df3e8d67SMatthew Barth #include <phosphor-logging/elog.hpp>
24618027abSDinesh Chinari #include <phosphor-logging/elog-errors.hpp>
258fd879fbSWilliam A. Kennington III #include <stdexcept>
26618027abSDinesh Chinari #include <xyz/openbmc_project/Common/error.hpp>
27cc8912e9SMatthew Barth #include "config.h"
287f88fe61SMatt Spinler #include "zone.hpp"
29df3e8d67SMatthew Barth #include "utility.hpp"
30d953bb25SMatthew Barth #include "sdbusplus.hpp"
317f88fe61SMatt Spinler 
327f88fe61SMatt Spinler namespace phosphor
337f88fe61SMatt Spinler {
347f88fe61SMatt Spinler namespace fan
357f88fe61SMatt Spinler {
367f88fe61SMatt Spinler namespace control
377f88fe61SMatt Spinler {
387f88fe61SMatt Spinler 
398600d9a0SMatthew Barth using namespace std::chrono;
409014980aSMatthew Barth using namespace phosphor::fan;
41df3e8d67SMatthew Barth using namespace phosphor::logging;
42cc8912e9SMatthew Barth namespace fs = std::experimental::filesystem;
43618027abSDinesh Chinari using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
44618027abSDinesh Chinari                              Error::InternalFailure;
457f88fe61SMatt Spinler 
4614184131SMatthew Barth Zone::Zone(Mode mode,
4714184131SMatthew Barth            sdbusplus::bus::bus& bus,
4893af4194SMatthew Barth            const std::string& path,
491cfc2f11SWilliam A. Kennington III            const sdeventplus::Event& event,
507f88fe61SMatt Spinler            const ZoneDefinition& def) :
5193af4194SMatthew Barth     ThermalObject(bus, path.c_str(), true),
527f88fe61SMatt Spinler     _bus(bus),
5393af4194SMatthew Barth     _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)),
6222c36ab6SWilliam A. Kennington III     _decTimer(event, std::bind(&Zone::decTimerExpired, this)),
631cfc2f11SWilliam A. Kennington III     _eventLoop(event)
647f88fe61SMatt Spinler {
657f88fe61SMatt Spinler     auto& fanDefs = std::get<fanListPos>(def);
667f88fe61SMatt Spinler 
677f88fe61SMatt Spinler     for (auto& def : fanDefs)
687f88fe61SMatt Spinler     {
697f88fe61SMatt Spinler         _fans.emplace_back(std::make_unique<Fan>(bus, def));
707f88fe61SMatt Spinler     }
7138a93a8aSMatthew Barth 
7214184131SMatthew Barth     // Do not enable set speed events when in init mode
7393af4194SMatthew Barth     if (mode == Mode::control)
7414184131SMatthew Barth     {
751b3e9602SMatthew Barth         // Process any zone handlers defined
761b3e9602SMatthew Barth         for (auto& hand : std::get<handlerPos>(def))
771b3e9602SMatthew Barth         {
781b3e9602SMatthew Barth             hand(*this);
791b3e9602SMatthew Barth         }
801b3e9602SMatthew Barth 
819e4db25cSMatthew Barth         // Restore thermal control current mode state
829e4db25cSMatthew Barth         restoreCurrentMode();
8393af4194SMatthew Barth 
8493af4194SMatthew Barth         // Emit objects added in control mode only
8593af4194SMatthew Barth         this->emit_object_added();
8693af4194SMatthew Barth 
872b3db618SMatthew Barth         // Update target speed to current zone target speed
882b3db618SMatthew Barth         if (!_fans.empty())
892b3db618SMatthew Barth         {
902b3db618SMatthew Barth             _targetSpeed = _fans.front()->getTargetSpeed();
912b3db618SMatthew Barth         }
92ccc7770eSMatthew Barth         // Setup signal trigger for set speed events
93ccc7770eSMatthew Barth         for (auto& event : std::get<setSpeedEventsPos>(def))
94ccc7770eSMatthew Barth         {
95ccc7770eSMatthew Barth             initEvent(event);
96ccc7770eSMatthew Barth         }
978600d9a0SMatthew Barth         // Start timer for fan speed decreases
988fd879fbSWilliam A. Kennington III         _decTimer.restart(_decInterval);
997f88fe61SMatt Spinler     }
10014184131SMatthew Barth }
1017f88fe61SMatt Spinler 
1027f88fe61SMatt Spinler void Zone::setSpeed(uint64_t speed)
1037f88fe61SMatt Spinler {
10460b00766SMatthew Barth     if (_isActive)
10560b00766SMatthew Barth     {
10660b00766SMatthew Barth         _targetSpeed = speed;
1077f88fe61SMatt Spinler         for (auto& fan : _fans)
1087f88fe61SMatt Spinler         {
10960b00766SMatthew Barth             fan->setSpeed(_targetSpeed);
11060b00766SMatthew Barth         }
11160b00766SMatthew Barth     }
11260b00766SMatthew Barth }
11360b00766SMatthew Barth 
11460b00766SMatthew Barth void Zone::setFullSpeed()
11560b00766SMatthew Barth {
11660b00766SMatthew Barth     if (_fullSpeed != 0)
11760b00766SMatthew Barth     {
11860b00766SMatthew Barth         _targetSpeed = _fullSpeed;
11960b00766SMatthew Barth         for (auto& fan : _fans)
12060b00766SMatthew Barth         {
12160b00766SMatthew Barth             fan->setSpeed(_targetSpeed);
12260b00766SMatthew Barth         }
1237f88fe61SMatt Spinler     }
1247f88fe61SMatt Spinler }
1257f88fe61SMatt Spinler 
126861d77c3SMatthew Barth void Zone::setActiveAllow(const Group* group, bool isActiveAllow)
127861d77c3SMatthew Barth {
12860b00766SMatthew Barth     _active[*(group)] = isActiveAllow;
129861d77c3SMatthew Barth     if (!isActiveAllow)
130861d77c3SMatthew Barth     {
131861d77c3SMatthew Barth         _isActive = false;
132861d77c3SMatthew Barth     }
133861d77c3SMatthew Barth     else
134861d77c3SMatthew Barth     {
135861d77c3SMatthew Barth         // Check all entries are set to allow control active
136861d77c3SMatthew Barth         auto actPred = [](auto const& entry) {return entry.second;};
137861d77c3SMatthew Barth         _isActive = std::all_of(_active.begin(),
138861d77c3SMatthew Barth                                 _active.end(),
139861d77c3SMatthew Barth                                 actPred);
140861d77c3SMatthew Barth     }
141861d77c3SMatthew Barth }
142861d77c3SMatthew Barth 
14355dea643SMatthew Barth void Zone::removeService(const Group* group,
14455dea643SMatthew Barth                          const std::string& name)
14555dea643SMatthew Barth {
14655dea643SMatthew Barth     try
14755dea643SMatthew Barth     {
14855dea643SMatthew Barth         auto& sNames = _services.at(*group);
14955dea643SMatthew Barth         auto it = std::find_if(
15055dea643SMatthew Barth             sNames.begin(),
15155dea643SMatthew Barth             sNames.end(),
15255dea643SMatthew Barth             [&name](auto const& entry)
15355dea643SMatthew Barth             {
15455dea643SMatthew Barth                 return name == std::get<namePos>(entry);
15555dea643SMatthew Barth             }
15655dea643SMatthew Barth         );
15755dea643SMatthew Barth         if (it != std::end(sNames))
15855dea643SMatthew Barth         {
15955dea643SMatthew Barth             // Remove service name from group
16055dea643SMatthew Barth             sNames.erase(it);
16155dea643SMatthew Barth         }
16255dea643SMatthew Barth     }
16355dea643SMatthew Barth     catch (const std::out_of_range& oore)
16455dea643SMatthew Barth     {
16555dea643SMatthew Barth         // No services for group found
16655dea643SMatthew Barth     }
16755dea643SMatthew Barth }
16855dea643SMatthew Barth 
169e59fdf70SMatthew Barth void Zone::setServiceOwner(const Group* group,
170e59fdf70SMatthew Barth                            const std::string& name,
171e59fdf70SMatthew Barth                            const bool hasOwner)
172e59fdf70SMatthew Barth {
173e59fdf70SMatthew Barth     try
174e59fdf70SMatthew Barth     {
175e59fdf70SMatthew Barth         auto& sNames = _services.at(*group);
176e59fdf70SMatthew Barth         auto it = std::find_if(
177e59fdf70SMatthew Barth             sNames.begin(),
178e59fdf70SMatthew Barth             sNames.end(),
179e59fdf70SMatthew Barth             [&name](auto const& entry)
180e59fdf70SMatthew Barth             {
181e59fdf70SMatthew Barth                 return name == std::get<namePos>(entry);
182e59fdf70SMatthew Barth             }
183e59fdf70SMatthew Barth         );
184e59fdf70SMatthew Barth         if (it != std::end(sNames))
185e59fdf70SMatthew Barth         {
186e59fdf70SMatthew Barth             std::get<hasOwnerPos>(*it) = hasOwner;
187e59fdf70SMatthew Barth         }
188e59fdf70SMatthew Barth         else
189e59fdf70SMatthew Barth         {
190e59fdf70SMatthew Barth             _services[*group].emplace_back(name, hasOwner);
191e59fdf70SMatthew Barth         }
192e59fdf70SMatthew Barth     }
193e59fdf70SMatthew Barth     catch (const std::out_of_range& oore)
194e59fdf70SMatthew Barth     {
195e59fdf70SMatthew Barth         _services[*group].emplace_back(name, hasOwner);
196e59fdf70SMatthew Barth     }
197e59fdf70SMatthew Barth }
198e59fdf70SMatthew Barth 
199480787c1SMatthew Barth void Zone::setServices(const Group* group)
200480787c1SMatthew Barth {
20155dea643SMatthew Barth     // Remove the empty service name if exists
20255dea643SMatthew Barth     removeService(group, "");
203480787c1SMatthew Barth     for (auto it = group->begin(); it != group->end(); ++it)
204480787c1SMatthew Barth     {
205480787c1SMatthew Barth         std::string name;
206480787c1SMatthew Barth         bool hasOwner = false;
207480787c1SMatthew Barth         try
208480787c1SMatthew Barth         {
209146b7390SMatthew Barth             name = getService(std::get<pathPos>(*it),
210146b7390SMatthew Barth                               std::get<intfPos>(*it));
211480787c1SMatthew Barth             hasOwner = util::SDBusPlus::callMethodAndRead<bool>(
212480787c1SMatthew Barth                     _bus,
213480787c1SMatthew Barth                     "org.freedesktop.DBus",
214480787c1SMatthew Barth                     "/org/freedesktop/DBus",
215480787c1SMatthew Barth                     "org.freedesktop.DBus",
216480787c1SMatthew Barth                     "NameHasOwner",
217480787c1SMatthew Barth                     name);
218480787c1SMatthew Barth         }
219ba7b5feaSMatt Spinler         catch (const util::DBusMethodError& e)
220480787c1SMatthew Barth         {
221480787c1SMatthew Barth             // Failed to get service name owner state
222480787c1SMatthew Barth             hasOwner = false;
223480787c1SMatthew Barth         }
224480787c1SMatthew Barth         setServiceOwner(group, name, hasOwner);
225480787c1SMatthew Barth     }
226480787c1SMatthew Barth }
227480787c1SMatthew Barth 
228b4a7cb99SMatthew Barth void Zone::setFloor(uint64_t speed)
229b4a7cb99SMatthew Barth {
23098726c45SMatthew Barth     // Check all entries are set to allow floor to be set
23198726c45SMatthew Barth     auto pred = [](auto const& entry) {return entry.second;};
23298726c45SMatthew Barth     auto setFloor = std::all_of(_floorChange.begin(),
23398726c45SMatthew Barth                                 _floorChange.end(),
23498726c45SMatthew Barth                                 pred);
23598726c45SMatthew Barth     if (setFloor)
23698726c45SMatthew Barth     {
237b4a7cb99SMatthew Barth         _floorSpeed = speed;
238b4a7cb99SMatthew Barth         // Floor speed above target, update target to floor speed
239b4a7cb99SMatthew Barth         if (_targetSpeed < _floorSpeed)
240b4a7cb99SMatthew Barth         {
241b4a7cb99SMatthew Barth             requestSpeedIncrease(_floorSpeed - _targetSpeed);
242b4a7cb99SMatthew Barth         }
243b4a7cb99SMatthew Barth     }
24498726c45SMatthew Barth }
245b4a7cb99SMatthew Barth 
246240397b9SMatthew Barth void Zone::requestSpeedIncrease(uint64_t targetDelta)
247240397b9SMatthew Barth {
248240397b9SMatthew Barth     // Only increase speed when delta is higher than
249240397b9SMatthew Barth     // the current increase delta for the zone and currently under ceiling
250240397b9SMatthew Barth     if (targetDelta > _incSpeedDelta &&
251240397b9SMatthew Barth         _targetSpeed < _ceilingSpeed)
252240397b9SMatthew Barth     {
2534e728542SMatthew Barth         auto requestTarget = getRequestSpeedBase();
25460b00766SMatthew Barth         requestTarget = (targetDelta - _incSpeedDelta) + requestTarget;
255240397b9SMatthew Barth         _incSpeedDelta = targetDelta;
256240397b9SMatthew Barth         // Target speed can not go above a defined ceiling speed
25760b00766SMatthew Barth         if (requestTarget > _ceilingSpeed)
258240397b9SMatthew Barth         {
25960b00766SMatthew Barth             requestTarget = _ceilingSpeed;
260240397b9SMatthew Barth         }
26160b00766SMatthew Barth         setSpeed(requestTarget);
2628fd879fbSWilliam A. Kennington III         // Retart timer countdown for fan speed increase
2638fd879fbSWilliam A. Kennington III         _incTimer.restartOnce(_incDelay);
2641ee48f2bSMatthew Barth     }
2651ee48f2bSMatthew Barth }
2661ee48f2bSMatthew Barth 
2671ee48f2bSMatthew Barth void Zone::incTimerExpired()
2681ee48f2bSMatthew Barth {
2691ee48f2bSMatthew Barth     // Clear increase delta when timer expires allowing additional speed
2701ee48f2bSMatthew Barth     // increase requests or speed decreases to occur
271240397b9SMatthew Barth     _incSpeedDelta = 0;
272240397b9SMatthew Barth }
273240397b9SMatthew Barth 
2740ce99d8bSMatthew Barth void Zone::requestSpeedDecrease(uint64_t targetDelta)
2750ce99d8bSMatthew Barth {
2760ce99d8bSMatthew Barth     // Only decrease the lowest target delta requested
2770ce99d8bSMatthew Barth     if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta)
2780ce99d8bSMatthew Barth     {
2790ce99d8bSMatthew Barth         _decSpeedDelta = targetDelta;
2800ce99d8bSMatthew Barth     }
2818600d9a0SMatthew Barth }
2820ce99d8bSMatthew Barth 
2838600d9a0SMatthew Barth void Zone::decTimerExpired()
2848600d9a0SMatthew Barth {
285e4338cdbSMatthew Barth     // Check all entries are set to allow a decrease
286e4338cdbSMatthew Barth     auto pred = [](auto const& entry) {return entry.second;};
287e4338cdbSMatthew Barth     auto decAllowed = std::all_of(_decAllowed.begin(),
288e4338cdbSMatthew Barth                                   _decAllowed.end(),
289e4338cdbSMatthew Barth                                   pred);
290e4338cdbSMatthew Barth 
291e4338cdbSMatthew Barth     // Only decrease speeds when allowed,
292e4338cdbSMatthew Barth     // where no requested increases exist and
293e4338cdbSMatthew Barth     // the increase timer is not running
294e4338cdbSMatthew Barth     // (i.e. not in the middle of increasing)
2958fd879fbSWilliam A. Kennington III     if (decAllowed && _incSpeedDelta == 0 && !_incTimer.isEnabled())
2960ce99d8bSMatthew Barth     {
2974e728542SMatthew Barth         auto requestTarget = getRequestSpeedBase();
298c63973a1SMatthew Barth         // Request target speed should not start above ceiling
299c63973a1SMatthew Barth         if (requestTarget > _ceilingSpeed)
300c63973a1SMatthew Barth         {
301c63973a1SMatthew Barth             requestTarget = _ceilingSpeed;
302c63973a1SMatthew Barth         }
3030ce99d8bSMatthew Barth         // Target speed can not go below the defined floor speed
30460b00766SMatthew Barth         if ((requestTarget < _decSpeedDelta) ||
30560b00766SMatthew Barth             (requestTarget - _decSpeedDelta < _floorSpeed))
3060ce99d8bSMatthew Barth         {
30760b00766SMatthew Barth             requestTarget = _floorSpeed;
3080ce99d8bSMatthew Barth         }
3090ce99d8bSMatthew Barth         else
3100ce99d8bSMatthew Barth         {
31160b00766SMatthew Barth             requestTarget = requestTarget - _decSpeedDelta;
3120ce99d8bSMatthew Barth         }
31360b00766SMatthew Barth         setSpeed(requestTarget);
3140ce99d8bSMatthew Barth     }
3150ce99d8bSMatthew Barth     // Clear decrease delta when timer expires
3160ce99d8bSMatthew Barth     _decSpeedDelta = 0;
3178600d9a0SMatthew Barth     // Decrease timer is restarted since its repeating
3180ce99d8bSMatthew Barth }
3190ce99d8bSMatthew Barth 
320ccc7770eSMatthew Barth void Zone::initEvent(const SetSpeedEvent& event)
3211bf0ce4bSMatthew Barth {
3221b4de26aSMatthew Barth     // Enable event triggers
3231b4de26aSMatthew Barth     std::for_each(
3241b4de26aSMatthew Barth         std::get<triggerPos>(event).begin(),
3251b4de26aSMatthew Barth         std::get<triggerPos>(event).end(),
3261b4de26aSMatthew Barth         [this, &event](auto const& trigger)
3279014980aSMatthew Barth         {
3281b4de26aSMatthew Barth             trigger(*this,
3292885ba90SMatthew Barth                     std::get<sseNamePos>(event),
3301b4de26aSMatthew Barth                     std::get<groupPos>(event),
3311b4de26aSMatthew Barth                     std::get<actionsPos>(event));
3329014980aSMatthew Barth         }
3331b4de26aSMatthew Barth     );
3341bf0ce4bSMatthew Barth }
3351bf0ce4bSMatthew Barth 
336f6b76d8eSMatthew Barth void Zone::removeEvent(const SetSpeedEvent& event)
337f6b76d8eSMatthew Barth {
33879cb8312SMatthew Barth     // Remove event signals
33979cb8312SMatthew Barth     auto sigIter = _signalEvents.find(std::get<sseNamePos>(event));
34079cb8312SMatthew Barth     if (sigIter != _signalEvents.end())
341f6b76d8eSMatthew Barth     {
34279cb8312SMatthew Barth         auto& signals = sigIter->second;
34379cb8312SMatthew Barth         for (auto it = signals.begin(); it != signals.end(); ++it)
344f9201abbSMatthew Barth         {
34533bfe761SMatthew Barth             removeSignal(it);
346f9201abbSMatthew Barth         }
34779cb8312SMatthew Barth         _signalEvents.erase(sigIter);
34833bfe761SMatthew Barth     }
34979cb8312SMatthew Barth 
350*d7b716a6SMatthew Barth     // Remove event timers
351*d7b716a6SMatthew Barth     auto timIter = _timerEvents.find(std::get<sseNamePos>(event));
352*d7b716a6SMatthew Barth     if (timIter != _timerEvents.end())
35333bfe761SMatthew Barth     {
354*d7b716a6SMatthew Barth         _timerEvents.erase(timIter);
35533bfe761SMatthew Barth     }
35633bfe761SMatthew Barth }
35733bfe761SMatthew Barth 
358bfb1a566SMatthew Barth std::vector<TimerEvent>::iterator Zone::findTimer(
359bfb1a566SMatthew Barth         const Group& eventGroup,
360*d7b716a6SMatthew Barth         const std::vector<Action>& eventActions,
361*d7b716a6SMatthew Barth         std::vector<TimerEvent>& eventTimers)
362bfb1a566SMatthew Barth {
363*d7b716a6SMatthew Barth     for (auto it = eventTimers.begin(); it != eventTimers.end(); ++it)
364bfb1a566SMatthew Barth     {
3650420a932SWilliam A. Kennington III         const auto& teEventData = *std::get<timerEventDataPos>(*it);
366*d7b716a6SMatthew Barth         if (std::get<eventGroupPos>(teEventData) == eventGroup &&
367*d7b716a6SMatthew Barth             std::get<eventActionsPos>(teEventData).size() ==
368bfb1a566SMatthew Barth             eventActions.size())
369bfb1a566SMatthew Barth         {
370bfb1a566SMatthew Barth             // TODO openbmc/openbmc#2328 - Use the action function target
371bfb1a566SMatthew Barth             // for comparison
372bfb1a566SMatthew Barth             auto actsEqual = [](auto const& a1,
373bfb1a566SMatthew Barth                                 auto const& a2)
374bfb1a566SMatthew Barth                     {
375bfb1a566SMatthew Barth                         return a1.target_type().name() ==
376bfb1a566SMatthew Barth                                a2.target_type().name();
377bfb1a566SMatthew Barth                     };
378*d7b716a6SMatthew Barth             if (std::equal(eventActions.begin(),
379bfb1a566SMatthew Barth                            eventActions.end(),
380bfb1a566SMatthew Barth                            std::get<eventActionsPos>(teEventData).begin(),
381bfb1a566SMatthew Barth                            actsEqual))
382bfb1a566SMatthew Barth             {
383bfb1a566SMatthew Barth                 return it;
384bfb1a566SMatthew Barth             }
385bfb1a566SMatthew Barth         }
386bfb1a566SMatthew Barth     }
387bfb1a566SMatthew Barth 
388*d7b716a6SMatthew Barth     return eventTimers.end();
389bfb1a566SMatthew Barth }
390bfb1a566SMatthew Barth 
391*d7b716a6SMatthew Barth void Zone::addTimer(const std::string& name,
392*d7b716a6SMatthew Barth                     const Group& group,
39394fe1a0cSWilliam A. Kennington III                     const std::vector<Action>& actions,
39494fe1a0cSWilliam A. Kennington III                     const TimerConf& tConf)
39594fe1a0cSWilliam A. Kennington III {
3968fd879fbSWilliam A. Kennington III     auto eventData = std::make_unique<EventData>(
39794fe1a0cSWilliam A. Kennington III             group,
39894fe1a0cSWilliam A. Kennington III             "",
39994fe1a0cSWilliam A. Kennington III             nullptr,
40094fe1a0cSWilliam A. Kennington III             actions
40194fe1a0cSWilliam A. Kennington III     );
4028fd879fbSWilliam A. Kennington III     Timer timer(
40394fe1a0cSWilliam A. Kennington III         _eventLoop,
404c0c5f07fSWilliam A. Kennington III         std::bind(&Zone::timerExpired,
405c0c5f07fSWilliam A. Kennington III                   this,
4068fd879fbSWilliam A. Kennington III                   std::cref(std::get<Group>(*eventData)),
4078fd879fbSWilliam A. Kennington III                   std::cref(std::get<std::vector<Action>>(*eventData))));
4088fd879fbSWilliam A. Kennington III     if (std::get<TimerType>(tConf) == TimerType::repeating)
40994fe1a0cSWilliam A. Kennington III     {
4108fd879fbSWilliam A. Kennington III         timer.restart(std::get<intervalPos>(tConf));
41194fe1a0cSWilliam A. Kennington III     }
4128fd879fbSWilliam A. Kennington III     else if (std::get<TimerType>(tConf) == TimerType::oneshot)
4138fd879fbSWilliam A. Kennington III     {
4148fd879fbSWilliam A. Kennington III         timer.restartOnce(std::get<intervalPos>(tConf));
4158fd879fbSWilliam A. Kennington III     }
4168fd879fbSWilliam A. Kennington III     else
4178fd879fbSWilliam A. Kennington III     {
4188fd879fbSWilliam A. Kennington III         throw std::invalid_argument("Invalid Timer Type");
4198fd879fbSWilliam A. Kennington III     }
420*d7b716a6SMatthew Barth     _timerEvents[name].emplace_back(std::move(eventData), std::move(timer));
42194fe1a0cSWilliam A. Kennington III }
42294fe1a0cSWilliam A. Kennington III 
423c0c5f07fSWilliam A. Kennington III void Zone::timerExpired(const Group& eventGroup,
424c0c5f07fSWilliam A. Kennington III                         const std::vector<Action>& eventActions)
4259014980aSMatthew Barth {
426f9201abbSMatthew Barth     // Perform the actions
427f9201abbSMatthew Barth     std::for_each(eventActions.begin(),
428f9201abbSMatthew Barth                   eventActions.end(),
429f9201abbSMatthew Barth                   [this, &eventGroup](auto const& action)
430f9201abbSMatthew Barth                   {
431f9201abbSMatthew Barth                       action(*this, eventGroup);
432f9201abbSMatthew Barth                   });
4339014980aSMatthew Barth }
4349014980aSMatthew Barth 
43538a93a8aSMatthew Barth void Zone::handleEvent(sdbusplus::message::message& msg,
43634f1bda2SMatthew Barth                        const EventData* eventData)
43738a93a8aSMatthew Barth {
43838a93a8aSMatthew Barth     // Handle the callback
43934f1bda2SMatthew Barth     std::get<eventHandlerPos>(*eventData)(_bus, msg, *this);
440f9201abbSMatthew Barth     // Perform the actions
441f9201abbSMatthew Barth     std::for_each(
442f9201abbSMatthew Barth         std::get<eventActionsPos>(*eventData).begin(),
443f9201abbSMatthew Barth         std::get<eventActionsPos>(*eventData).end(),
444f9201abbSMatthew Barth         [this, &eventData](auto const& action)
445f9201abbSMatthew Barth         {
446f9201abbSMatthew Barth             action(*this,
44734f1bda2SMatthew Barth                    std::get<eventGroupPos>(*eventData));
448f9201abbSMatthew Barth         });
44938a93a8aSMatthew Barth }
45038a93a8aSMatthew Barth 
451a603ed01SMatthew Barth const std::string& Zone::getService(const std::string& path,
452a603ed01SMatthew Barth                                     const std::string& intf)
453a603ed01SMatthew Barth {
454a603ed01SMatthew Barth     // Retrieve service from cache
455a603ed01SMatthew Barth     auto srvIter = _servTree.find(path);
456a603ed01SMatthew Barth     if (srvIter != _servTree.end())
457a603ed01SMatthew Barth     {
458a603ed01SMatthew Barth         for (auto& serv : srvIter->second)
459a603ed01SMatthew Barth         {
460a603ed01SMatthew Barth             auto it = std::find_if(
461a603ed01SMatthew Barth                 serv.second.begin(),
462a603ed01SMatthew Barth                 serv.second.end(),
463a603ed01SMatthew Barth                 [&intf](auto const& interface)
464a603ed01SMatthew Barth                 {
465a603ed01SMatthew Barth                     return intf == interface;
466a603ed01SMatthew Barth                 });
467a603ed01SMatthew Barth             if (it != std::end(serv.second))
468a603ed01SMatthew Barth             {
469a603ed01SMatthew Barth                 // Service found
470a603ed01SMatthew Barth                 return serv.first;
471a603ed01SMatthew Barth             }
472a603ed01SMatthew Barth         }
473a603ed01SMatthew Barth         // Interface not found in cache, add and return
474a603ed01SMatthew Barth         return addServices(path, intf, 0);
475a603ed01SMatthew Barth     }
476a603ed01SMatthew Barth     else
477a603ed01SMatthew Barth     {
478a603ed01SMatthew Barth         // Path not found in cache, add and return
479a603ed01SMatthew Barth         return addServices(path, intf, 0);
480a603ed01SMatthew Barth     }
481a603ed01SMatthew Barth }
482a603ed01SMatthew Barth 
483a603ed01SMatthew Barth const std::string& Zone::addServices(const std::string& path,
484a603ed01SMatthew Barth                                      const std::string& intf,
485a603ed01SMatthew Barth                                      int32_t depth)
486a603ed01SMatthew Barth {
487a603ed01SMatthew Barth     static const std::string empty = "";
488a603ed01SMatthew Barth     auto it = _servTree.end();
489a603ed01SMatthew Barth 
490a603ed01SMatthew Barth     // Get all subtree objects for the given interface
491a603ed01SMatthew Barth     auto objects = util::SDBusPlus::getSubTree(_bus, "/", intf, depth);
492a603ed01SMatthew Barth     // Add what's returned to the cache of path->services
493a603ed01SMatthew Barth     for (auto& pIter : objects)
494a603ed01SMatthew Barth     {
495a603ed01SMatthew Barth         auto pathIter = _servTree.find(pIter.first);
496a603ed01SMatthew Barth         if (pathIter != _servTree.end())
497a603ed01SMatthew Barth         {
498a603ed01SMatthew Barth             // Path found in cache
499a603ed01SMatthew Barth             for (auto& sIter : pIter.second)
500a603ed01SMatthew Barth             {
501a603ed01SMatthew Barth                 auto servIter = pathIter->second.find(sIter.first);
502a603ed01SMatthew Barth                 if (servIter != pathIter->second.end())
503a603ed01SMatthew Barth                 {
504a603ed01SMatthew Barth                     // Service found in cache
505a603ed01SMatthew Barth                     for (auto& iIter : sIter.second)
506a603ed01SMatthew Barth                     {
507e8b340bdSMatthew Barth                         if (std::find(servIter->second.begin(),
508e8b340bdSMatthew Barth                                       servIter->second.end(),
509e8b340bdSMatthew Barth                                       iIter) == servIter->second.end())
510e8b340bdSMatthew Barth                         {
511a603ed01SMatthew Barth                             // Add interface to cache
512a603ed01SMatthew Barth                             servIter->second.emplace_back(iIter);
513a603ed01SMatthew Barth                         }
514a603ed01SMatthew Barth                     }
515e8b340bdSMatthew Barth                 }
516a603ed01SMatthew Barth                 else
517a603ed01SMatthew Barth                 {
518a603ed01SMatthew Barth                     // Service not found in cache
519a603ed01SMatthew Barth                     pathIter->second.insert(sIter);
520a603ed01SMatthew Barth                 }
521a603ed01SMatthew Barth             }
522a603ed01SMatthew Barth         }
523a603ed01SMatthew Barth         else
524a603ed01SMatthew Barth         {
525a603ed01SMatthew Barth             _servTree.insert(pIter);
526a603ed01SMatthew Barth         }
527a603ed01SMatthew Barth         // When the paths match, since a single interface constraint is given,
528a603ed01SMatthew Barth         // that is the service to return
529a603ed01SMatthew Barth         if (path == pIter.first)
530a603ed01SMatthew Barth         {
531a603ed01SMatthew Barth             it = _servTree.find(pIter.first);
532a603ed01SMatthew Barth         }
533a603ed01SMatthew Barth     }
534a603ed01SMatthew Barth 
535a603ed01SMatthew Barth     if (it != _servTree.end())
536a603ed01SMatthew Barth     {
537a603ed01SMatthew Barth         return it->second.begin()->first;
538a603ed01SMatthew Barth     }
539a603ed01SMatthew Barth 
540a603ed01SMatthew Barth     return empty;
541a603ed01SMatthew Barth }
542a603ed01SMatthew Barth 
54370b2e7daSMatthew Barth auto Zone::getPersisted(const std::string& intf,
54470b2e7daSMatthew Barth                         const std::string& prop)
54570b2e7daSMatthew Barth {
54670b2e7daSMatthew Barth     auto persisted = false;
54770b2e7daSMatthew Barth 
54870b2e7daSMatthew Barth     auto it = _persisted.find(intf);
54970b2e7daSMatthew Barth     if (it != _persisted.end())
55070b2e7daSMatthew Barth     {
55170b2e7daSMatthew Barth         return std::any_of(it->second.begin(),
55270b2e7daSMatthew Barth                            it->second.end(),
55370b2e7daSMatthew Barth                            [&prop](auto& p)
55470b2e7daSMatthew Barth                            {
55570b2e7daSMatthew Barth                                return prop == p;
55670b2e7daSMatthew Barth                            });
55770b2e7daSMatthew Barth     }
55870b2e7daSMatthew Barth 
55970b2e7daSMatthew Barth     return persisted;
56070b2e7daSMatthew Barth }
56170b2e7daSMatthew Barth 
5626faf8943SMatthew Barth std::string Zone::current(std::string value)
5636faf8943SMatthew Barth {
564b390df1eSMatthew Barth     auto current = ThermalObject::current();
565b390df1eSMatthew Barth     std::transform(value.begin(), value.end(), value.begin(), toupper);
566b390df1eSMatthew Barth 
567221c90c3SMatthew Barth     auto supported = ThermalObject::supported();
568221c90c3SMatthew Barth     auto isSupported = std::any_of(
569221c90c3SMatthew Barth         supported.begin(),
570221c90c3SMatthew Barth         supported.end(),
571221c90c3SMatthew Barth         [&value](auto& s)
572221c90c3SMatthew Barth         {
573221c90c3SMatthew Barth             std::transform(s.begin(), s.end(), s.begin(), toupper);
574221c90c3SMatthew Barth             return value == s;
575221c90c3SMatthew Barth         });
576221c90c3SMatthew Barth 
577221c90c3SMatthew Barth     if (value != current && isSupported)
5786faf8943SMatthew Barth     {
5796faf8943SMatthew Barth         current = ThermalObject::current(value);
58070b2e7daSMatthew Barth         if (getPersisted("xyz.openbmc_project.Control.ThermalMode", "Current"))
58170b2e7daSMatthew Barth         {
5826faf8943SMatthew Barth             saveCurrentMode();
58370b2e7daSMatthew Barth         }
584b390df1eSMatthew Barth         // Trigger event(s) for current mode property change
5850a1f686cSMatthew Barth         auto eData = _objects[_path]
586baea6c3fSMatthew Barth                              ["xyz.openbmc_project.Control.ThermalMode"]
587baea6c3fSMatthew Barth                              ["Current"];
588baea6c3fSMatthew Barth         if (eData != nullptr)
589baea6c3fSMatthew Barth         {
590baea6c3fSMatthew Barth             sdbusplus::message::message nullMsg{nullptr};
591baea6c3fSMatthew Barth             handleEvent(nullMsg, eData);
592baea6c3fSMatthew Barth         }
5936faf8943SMatthew Barth     }
594b390df1eSMatthew Barth 
5956faf8943SMatthew Barth     return current;
5966faf8943SMatthew Barth }
5976faf8943SMatthew Barth 
598cc8912e9SMatthew Barth void Zone::saveCurrentMode()
599cc8912e9SMatthew Barth {
600cc8912e9SMatthew Barth     fs::path path{CONTROL_PERSIST_ROOT_PATH};
601cc8912e9SMatthew Barth     // Append zone and property description
602cc8912e9SMatthew Barth     path /= std::to_string(_zoneNum);
603cc8912e9SMatthew Barth     path /= "CurrentMode";
604cc8912e9SMatthew Barth     std::ofstream ofs(path.c_str(), std::ios::binary);
605cc8912e9SMatthew Barth     cereal::JSONOutputArchive oArch(ofs);
606cc8912e9SMatthew Barth     oArch(ThermalObject::current());
607cc8912e9SMatthew Barth }
608cc8912e9SMatthew Barth 
6099e4db25cSMatthew Barth void Zone::restoreCurrentMode()
6109e4db25cSMatthew Barth {
611a2bed6edSMatthew Barth     auto current = ThermalObject::current();
6129e4db25cSMatthew Barth     fs::path path{CONTROL_PERSIST_ROOT_PATH};
6139e4db25cSMatthew Barth     path /= std::to_string(_zoneNum);
6149e4db25cSMatthew Barth     path /= "CurrentMode";
6159e4db25cSMatthew Barth     fs::create_directories(path.parent_path());
6169e4db25cSMatthew Barth 
6179e4db25cSMatthew Barth     try
6189e4db25cSMatthew Barth     {
6199e4db25cSMatthew Barth         if (fs::exists(path))
6209e4db25cSMatthew Barth         {
6219e4db25cSMatthew Barth             std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary);
6229e4db25cSMatthew Barth             cereal::JSONInputArchive iArch(ifs);
6239e4db25cSMatthew Barth             iArch(current);
6249e4db25cSMatthew Barth         }
6259e4db25cSMatthew Barth     }
6269e4db25cSMatthew Barth     catch (std::exception& e)
6279e4db25cSMatthew Barth     {
6289e4db25cSMatthew Barth         log<level::ERR>(e.what());
6299e4db25cSMatthew Barth         fs::remove(path);
630a2bed6edSMatthew Barth         current = ThermalObject::current();
6319e4db25cSMatthew Barth     }
6329e4db25cSMatthew Barth 
6339e4db25cSMatthew Barth     this->current(current);
6349e4db25cSMatthew Barth }
6359e4db25cSMatthew Barth 
6367f88fe61SMatt Spinler }
6377f88fe61SMatt Spinler }
6387f88fe61SMatt Spinler }
639