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         {
328*75d97350SMatthew Barth             if (!std::get<actionsPos>(event).empty())
329*75d97350SMatthew Barth             {
330*75d97350SMatthew Barth                 std::for_each(
331*75d97350SMatthew Barth                     std::get<actionsPos>(event).begin(),
332*75d97350SMatthew Barth                     std::get<actionsPos>(event).end(),
333*75d97350SMatthew Barth                     [this, &trigger, &event](auto const& action)
334*75d97350SMatthew Barth                     {
335*75d97350SMatthew Barth                         // Default to use group defined with action if exists
336*75d97350SMatthew Barth                         if (!std::get<adGroupPos>(action).empty())
337*75d97350SMatthew Barth                         {
338*75d97350SMatthew Barth                             trigger(*this,
339*75d97350SMatthew Barth                                     std::get<sseNamePos>(event),
340*75d97350SMatthew Barth                                     std::get<adGroupPos>(action),
341*75d97350SMatthew Barth                                     std::get<adActionsPos>(action));
342*75d97350SMatthew Barth                         }
343*75d97350SMatthew Barth                         else
344*75d97350SMatthew Barth                         {
3451b4de26aSMatthew Barth                             trigger(*this,
3462885ba90SMatthew Barth                                     std::get<sseNamePos>(event),
3471b4de26aSMatthew Barth                                     std::get<groupPos>(event),
348*75d97350SMatthew Barth                                     std::get<adActionsPos>(action));
349*75d97350SMatthew Barth                         }
350*75d97350SMatthew Barth                     }
351*75d97350SMatthew Barth                 );
352*75d97350SMatthew Barth             }
353*75d97350SMatthew Barth             else
354*75d97350SMatthew Barth             {
355*75d97350SMatthew Barth                 trigger(*this,
356*75d97350SMatthew Barth                         std::get<sseNamePos>(event),
357*75d97350SMatthew Barth                         std::get<groupPos>(event),
358*75d97350SMatthew Barth                         {});
359*75d97350SMatthew Barth             }
3609014980aSMatthew Barth         }
3611b4de26aSMatthew Barth     );
3621bf0ce4bSMatthew Barth }
3631bf0ce4bSMatthew Barth 
364f6b76d8eSMatthew Barth void Zone::removeEvent(const SetSpeedEvent& event)
365f6b76d8eSMatthew Barth {
36679cb8312SMatthew Barth     // Remove event signals
36779cb8312SMatthew Barth     auto sigIter = _signalEvents.find(std::get<sseNamePos>(event));
36879cb8312SMatthew Barth     if (sigIter != _signalEvents.end())
369f6b76d8eSMatthew Barth     {
37079cb8312SMatthew Barth         auto& signals = sigIter->second;
37179cb8312SMatthew Barth         for (auto it = signals.begin(); it != signals.end(); ++it)
372f9201abbSMatthew Barth         {
37333bfe761SMatthew Barth             removeSignal(it);
374f9201abbSMatthew Barth         }
37579cb8312SMatthew Barth         _signalEvents.erase(sigIter);
37633bfe761SMatthew Barth     }
37779cb8312SMatthew Barth 
378d7b716a6SMatthew Barth     // Remove event timers
379d7b716a6SMatthew Barth     auto timIter = _timerEvents.find(std::get<sseNamePos>(event));
380d7b716a6SMatthew Barth     if (timIter != _timerEvents.end())
38133bfe761SMatthew Barth     {
382d7b716a6SMatthew Barth         _timerEvents.erase(timIter);
38333bfe761SMatthew Barth     }
38433bfe761SMatthew Barth }
38533bfe761SMatthew Barth 
386bfb1a566SMatthew Barth std::vector<TimerEvent>::iterator Zone::findTimer(
387bfb1a566SMatthew Barth         const Group& eventGroup,
388d7b716a6SMatthew Barth         const std::vector<Action>& eventActions,
389d7b716a6SMatthew Barth         std::vector<TimerEvent>& eventTimers)
390bfb1a566SMatthew Barth {
391d7b716a6SMatthew Barth     for (auto it = eventTimers.begin(); it != eventTimers.end(); ++it)
392bfb1a566SMatthew Barth     {
3930420a932SWilliam A. Kennington III         const auto& teEventData = *std::get<timerEventDataPos>(*it);
394d7b716a6SMatthew Barth         if (std::get<eventGroupPos>(teEventData) == eventGroup &&
395d7b716a6SMatthew Barth             std::get<eventActionsPos>(teEventData).size() ==
396bfb1a566SMatthew Barth             eventActions.size())
397bfb1a566SMatthew Barth         {
398bfb1a566SMatthew Barth             // TODO openbmc/openbmc#2328 - Use the action function target
399bfb1a566SMatthew Barth             // for comparison
400bfb1a566SMatthew Barth             auto actsEqual = [](auto const& a1,
401bfb1a566SMatthew Barth                                 auto const& a2)
402bfb1a566SMatthew Barth                     {
403bfb1a566SMatthew Barth                         return a1.target_type().name() ==
404bfb1a566SMatthew Barth                                a2.target_type().name();
405bfb1a566SMatthew Barth                     };
406d7b716a6SMatthew Barth             if (std::equal(eventActions.begin(),
407bfb1a566SMatthew Barth                            eventActions.end(),
408bfb1a566SMatthew Barth                            std::get<eventActionsPos>(teEventData).begin(),
409bfb1a566SMatthew Barth                            actsEqual))
410bfb1a566SMatthew Barth             {
411bfb1a566SMatthew Barth                 return it;
412bfb1a566SMatthew Barth             }
413bfb1a566SMatthew Barth         }
414bfb1a566SMatthew Barth     }
415bfb1a566SMatthew Barth 
416d7b716a6SMatthew Barth     return eventTimers.end();
417bfb1a566SMatthew Barth }
418bfb1a566SMatthew Barth 
419d7b716a6SMatthew Barth void Zone::addTimer(const std::string& name,
420d7b716a6SMatthew Barth                     const Group& group,
42194fe1a0cSWilliam A. Kennington III                     const std::vector<Action>& actions,
42294fe1a0cSWilliam A. Kennington III                     const TimerConf& tConf)
42394fe1a0cSWilliam A. Kennington III {
4248fd879fbSWilliam A. Kennington III     auto eventData = std::make_unique<EventData>(
42594fe1a0cSWilliam A. Kennington III             group,
42694fe1a0cSWilliam A. Kennington III             "",
42794fe1a0cSWilliam A. Kennington III             nullptr,
42894fe1a0cSWilliam A. Kennington III             actions
42994fe1a0cSWilliam A. Kennington III     );
4308fd879fbSWilliam A. Kennington III     Timer timer(
43194fe1a0cSWilliam A. Kennington III         _eventLoop,
432c0c5f07fSWilliam A. Kennington III         std::bind(&Zone::timerExpired,
433c0c5f07fSWilliam A. Kennington III                   this,
4348fd879fbSWilliam A. Kennington III                   std::cref(std::get<Group>(*eventData)),
4358fd879fbSWilliam A. Kennington III                   std::cref(std::get<std::vector<Action>>(*eventData))));
4368fd879fbSWilliam A. Kennington III     if (std::get<TimerType>(tConf) == TimerType::repeating)
43794fe1a0cSWilliam A. Kennington III     {
4388fd879fbSWilliam A. Kennington III         timer.restart(std::get<intervalPos>(tConf));
43994fe1a0cSWilliam A. Kennington III     }
4408fd879fbSWilliam A. Kennington III     else if (std::get<TimerType>(tConf) == TimerType::oneshot)
4418fd879fbSWilliam A. Kennington III     {
4428fd879fbSWilliam A. Kennington III         timer.restartOnce(std::get<intervalPos>(tConf));
4438fd879fbSWilliam A. Kennington III     }
4448fd879fbSWilliam A. Kennington III     else
4458fd879fbSWilliam A. Kennington III     {
4468fd879fbSWilliam A. Kennington III         throw std::invalid_argument("Invalid Timer Type");
4478fd879fbSWilliam A. Kennington III     }
448d7b716a6SMatthew Barth     _timerEvents[name].emplace_back(std::move(eventData), std::move(timer));
44994fe1a0cSWilliam A. Kennington III }
45094fe1a0cSWilliam A. Kennington III 
451c0c5f07fSWilliam A. Kennington III void Zone::timerExpired(const Group& eventGroup,
452c0c5f07fSWilliam A. Kennington III                         const std::vector<Action>& eventActions)
4539014980aSMatthew Barth {
454f9201abbSMatthew Barth     // Perform the actions
455f9201abbSMatthew Barth     std::for_each(eventActions.begin(),
456f9201abbSMatthew Barth                   eventActions.end(),
457f9201abbSMatthew Barth                   [this, &eventGroup](auto const& action)
458f9201abbSMatthew Barth                   {
459f9201abbSMatthew Barth                       action(*this, eventGroup);
460f9201abbSMatthew Barth                   });
4619014980aSMatthew Barth }
4629014980aSMatthew Barth 
46338a93a8aSMatthew Barth void Zone::handleEvent(sdbusplus::message::message& msg,
46434f1bda2SMatthew Barth                        const EventData* eventData)
46538a93a8aSMatthew Barth {
46638a93a8aSMatthew Barth     // Handle the callback
46734f1bda2SMatthew Barth     std::get<eventHandlerPos>(*eventData)(_bus, msg, *this);
468f9201abbSMatthew Barth     // Perform the actions
469f9201abbSMatthew Barth     std::for_each(
470f9201abbSMatthew Barth         std::get<eventActionsPos>(*eventData).begin(),
471f9201abbSMatthew Barth         std::get<eventActionsPos>(*eventData).end(),
472f9201abbSMatthew Barth         [this, &eventData](auto const& action)
473f9201abbSMatthew Barth         {
474f9201abbSMatthew Barth             action(*this,
47534f1bda2SMatthew Barth                    std::get<eventGroupPos>(*eventData));
476f9201abbSMatthew Barth         });
47738a93a8aSMatthew Barth }
47838a93a8aSMatthew Barth 
479a603ed01SMatthew Barth const std::string& Zone::getService(const std::string& path,
480a603ed01SMatthew Barth                                     const std::string& intf)
481a603ed01SMatthew Barth {
482a603ed01SMatthew Barth     // Retrieve service from cache
483a603ed01SMatthew Barth     auto srvIter = _servTree.find(path);
484a603ed01SMatthew Barth     if (srvIter != _servTree.end())
485a603ed01SMatthew Barth     {
486a603ed01SMatthew Barth         for (auto& serv : srvIter->second)
487a603ed01SMatthew Barth         {
488a603ed01SMatthew Barth             auto it = std::find_if(
489a603ed01SMatthew Barth                 serv.second.begin(),
490a603ed01SMatthew Barth                 serv.second.end(),
491a603ed01SMatthew Barth                 [&intf](auto const& interface)
492a603ed01SMatthew Barth                 {
493a603ed01SMatthew Barth                     return intf == interface;
494a603ed01SMatthew Barth                 });
495a603ed01SMatthew Barth             if (it != std::end(serv.second))
496a603ed01SMatthew Barth             {
497a603ed01SMatthew Barth                 // Service found
498a603ed01SMatthew Barth                 return serv.first;
499a603ed01SMatthew Barth             }
500a603ed01SMatthew Barth         }
501a603ed01SMatthew Barth         // Interface not found in cache, add and return
502a603ed01SMatthew Barth         return addServices(path, intf, 0);
503a603ed01SMatthew Barth     }
504a603ed01SMatthew Barth     else
505a603ed01SMatthew Barth     {
506a603ed01SMatthew Barth         // Path not found in cache, add and return
507a603ed01SMatthew Barth         return addServices(path, intf, 0);
508a603ed01SMatthew Barth     }
509a603ed01SMatthew Barth }
510a603ed01SMatthew Barth 
511a603ed01SMatthew Barth const std::string& Zone::addServices(const std::string& path,
512a603ed01SMatthew Barth                                      const std::string& intf,
513a603ed01SMatthew Barth                                      int32_t depth)
514a603ed01SMatthew Barth {
515a603ed01SMatthew Barth     static const std::string empty = "";
516a603ed01SMatthew Barth     auto it = _servTree.end();
517a603ed01SMatthew Barth 
518a603ed01SMatthew Barth     // Get all subtree objects for the given interface
519a603ed01SMatthew Barth     auto objects = util::SDBusPlus::getSubTree(_bus, "/", intf, depth);
520a603ed01SMatthew Barth     // Add what's returned to the cache of path->services
521a603ed01SMatthew Barth     for (auto& pIter : objects)
522a603ed01SMatthew Barth     {
523a603ed01SMatthew Barth         auto pathIter = _servTree.find(pIter.first);
524a603ed01SMatthew Barth         if (pathIter != _servTree.end())
525a603ed01SMatthew Barth         {
526a603ed01SMatthew Barth             // Path found in cache
527a603ed01SMatthew Barth             for (auto& sIter : pIter.second)
528a603ed01SMatthew Barth             {
529a603ed01SMatthew Barth                 auto servIter = pathIter->second.find(sIter.first);
530a603ed01SMatthew Barth                 if (servIter != pathIter->second.end())
531a603ed01SMatthew Barth                 {
532a603ed01SMatthew Barth                     // Service found in cache
533a603ed01SMatthew Barth                     for (auto& iIter : sIter.second)
534a603ed01SMatthew Barth                     {
535e8b340bdSMatthew Barth                         if (std::find(servIter->second.begin(),
536e8b340bdSMatthew Barth                                       servIter->second.end(),
537e8b340bdSMatthew Barth                                       iIter) == servIter->second.end())
538e8b340bdSMatthew Barth                         {
539a603ed01SMatthew Barth                             // Add interface to cache
540a603ed01SMatthew Barth                             servIter->second.emplace_back(iIter);
541a603ed01SMatthew Barth                         }
542a603ed01SMatthew Barth                     }
543e8b340bdSMatthew Barth                 }
544a603ed01SMatthew Barth                 else
545a603ed01SMatthew Barth                 {
546a603ed01SMatthew Barth                     // Service not found in cache
547a603ed01SMatthew Barth                     pathIter->second.insert(sIter);
548a603ed01SMatthew Barth                 }
549a603ed01SMatthew Barth             }
550a603ed01SMatthew Barth         }
551a603ed01SMatthew Barth         else
552a603ed01SMatthew Barth         {
553a603ed01SMatthew Barth             _servTree.insert(pIter);
554a603ed01SMatthew Barth         }
555a603ed01SMatthew Barth         // When the paths match, since a single interface constraint is given,
556a603ed01SMatthew Barth         // that is the service to return
557a603ed01SMatthew Barth         if (path == pIter.first)
558a603ed01SMatthew Barth         {
559a603ed01SMatthew Barth             it = _servTree.find(pIter.first);
560a603ed01SMatthew Barth         }
561a603ed01SMatthew Barth     }
562a603ed01SMatthew Barth 
563a603ed01SMatthew Barth     if (it != _servTree.end())
564a603ed01SMatthew Barth     {
565a603ed01SMatthew Barth         return it->second.begin()->first;
566a603ed01SMatthew Barth     }
567a603ed01SMatthew Barth 
568a603ed01SMatthew Barth     return empty;
569a603ed01SMatthew Barth }
570a603ed01SMatthew Barth 
57170b2e7daSMatthew Barth auto Zone::getPersisted(const std::string& intf,
57270b2e7daSMatthew Barth                         const std::string& prop)
57370b2e7daSMatthew Barth {
57470b2e7daSMatthew Barth     auto persisted = false;
57570b2e7daSMatthew Barth 
57670b2e7daSMatthew Barth     auto it = _persisted.find(intf);
57770b2e7daSMatthew Barth     if (it != _persisted.end())
57870b2e7daSMatthew Barth     {
57970b2e7daSMatthew Barth         return std::any_of(it->second.begin(),
58070b2e7daSMatthew Barth                            it->second.end(),
58170b2e7daSMatthew Barth                            [&prop](auto& p)
58270b2e7daSMatthew Barth                            {
58370b2e7daSMatthew Barth                                return prop == p;
58470b2e7daSMatthew Barth                            });
58570b2e7daSMatthew Barth     }
58670b2e7daSMatthew Barth 
58770b2e7daSMatthew Barth     return persisted;
58870b2e7daSMatthew Barth }
58970b2e7daSMatthew Barth 
5906faf8943SMatthew Barth std::string Zone::current(std::string value)
5916faf8943SMatthew Barth {
592b390df1eSMatthew Barth     auto current = ThermalObject::current();
593b390df1eSMatthew Barth     std::transform(value.begin(), value.end(), value.begin(), toupper);
594b390df1eSMatthew Barth 
595221c90c3SMatthew Barth     auto supported = ThermalObject::supported();
596221c90c3SMatthew Barth     auto isSupported = std::any_of(
597221c90c3SMatthew Barth         supported.begin(),
598221c90c3SMatthew Barth         supported.end(),
599221c90c3SMatthew Barth         [&value](auto& s)
600221c90c3SMatthew Barth         {
601221c90c3SMatthew Barth             std::transform(s.begin(), s.end(), s.begin(), toupper);
602221c90c3SMatthew Barth             return value == s;
603221c90c3SMatthew Barth         });
604221c90c3SMatthew Barth 
605221c90c3SMatthew Barth     if (value != current && isSupported)
6066faf8943SMatthew Barth     {
6076faf8943SMatthew Barth         current = ThermalObject::current(value);
60870b2e7daSMatthew Barth         if (getPersisted("xyz.openbmc_project.Control.ThermalMode", "Current"))
60970b2e7daSMatthew Barth         {
6106faf8943SMatthew Barth             saveCurrentMode();
61170b2e7daSMatthew Barth         }
612b390df1eSMatthew Barth         // Trigger event(s) for current mode property change
6130a1f686cSMatthew Barth         auto eData = _objects[_path]
614baea6c3fSMatthew Barth                              ["xyz.openbmc_project.Control.ThermalMode"]
615baea6c3fSMatthew Barth                              ["Current"];
616baea6c3fSMatthew Barth         if (eData != nullptr)
617baea6c3fSMatthew Barth         {
618baea6c3fSMatthew Barth             sdbusplus::message::message nullMsg{nullptr};
619baea6c3fSMatthew Barth             handleEvent(nullMsg, eData);
620baea6c3fSMatthew Barth         }
6216faf8943SMatthew Barth     }
622b390df1eSMatthew Barth 
6236faf8943SMatthew Barth     return current;
6246faf8943SMatthew Barth }
6256faf8943SMatthew Barth 
626cc8912e9SMatthew Barth void Zone::saveCurrentMode()
627cc8912e9SMatthew Barth {
628cc8912e9SMatthew Barth     fs::path path{CONTROL_PERSIST_ROOT_PATH};
629cc8912e9SMatthew Barth     // Append zone and property description
630cc8912e9SMatthew Barth     path /= std::to_string(_zoneNum);
631cc8912e9SMatthew Barth     path /= "CurrentMode";
632cc8912e9SMatthew Barth     std::ofstream ofs(path.c_str(), std::ios::binary);
633cc8912e9SMatthew Barth     cereal::JSONOutputArchive oArch(ofs);
634cc8912e9SMatthew Barth     oArch(ThermalObject::current());
635cc8912e9SMatthew Barth }
636cc8912e9SMatthew Barth 
6379e4db25cSMatthew Barth void Zone::restoreCurrentMode()
6389e4db25cSMatthew Barth {
639a2bed6edSMatthew Barth     auto current = ThermalObject::current();
6409e4db25cSMatthew Barth     fs::path path{CONTROL_PERSIST_ROOT_PATH};
6419e4db25cSMatthew Barth     path /= std::to_string(_zoneNum);
6429e4db25cSMatthew Barth     path /= "CurrentMode";
6439e4db25cSMatthew Barth     fs::create_directories(path.parent_path());
6449e4db25cSMatthew Barth 
6459e4db25cSMatthew Barth     try
6469e4db25cSMatthew Barth     {
6479e4db25cSMatthew Barth         if (fs::exists(path))
6489e4db25cSMatthew Barth         {
6499e4db25cSMatthew Barth             std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary);
6509e4db25cSMatthew Barth             cereal::JSONInputArchive iArch(ifs);
6519e4db25cSMatthew Barth             iArch(current);
6529e4db25cSMatthew Barth         }
6539e4db25cSMatthew Barth     }
6549e4db25cSMatthew Barth     catch (std::exception& e)
6559e4db25cSMatthew Barth     {
6569e4db25cSMatthew Barth         log<level::ERR>(e.what());
6579e4db25cSMatthew Barth         fs::remove(path);
658a2bed6edSMatthew Barth         current = ThermalObject::current();
6599e4db25cSMatthew Barth     }
6609e4db25cSMatthew Barth 
6619e4db25cSMatthew Barth     this->current(current);
6629e4db25cSMatthew Barth }
6639e4db25cSMatthew Barth 
6647f88fe61SMatt Spinler }
6657f88fe61SMatt Spinler }
6667f88fe61SMatt Spinler }
667