blob: 90350a78d124d11dec5adae41f3ac74a7fdf46a6 [file] [log] [blame]
Matt Spinler7f88fe62017-04-10 14:39:02 -05001/**
2 * Copyright © 2017 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Matthew Barth8600d9a2017-06-23 14:38:05 -050016#include <chrono>
William A. Kennington III22c36ab2018-10-30 19:50:57 -070017#include <functional>
Matthew Barthcc8912e2019-01-21 11:35:27 -060018#include <fstream>
19#include <cereal/cereal.hpp>
20#include <cereal/archives/json.hpp>
21#include <experimental/filesystem>
Dinesh Chinari618027a2017-06-26 23:26:50 -050022#include <phosphor-logging/log.hpp>
Matthew Barthdf3e8d62017-05-31 11:07:24 -050023#include <phosphor-logging/elog.hpp>
Dinesh Chinari618027a2017-06-26 23:26:50 -050024#include <phosphor-logging/elog-errors.hpp>
William A. Kennington III8fd879f2018-10-30 19:49:29 -070025#include <stdexcept>
Dinesh Chinari618027a2017-06-26 23:26:50 -050026#include <xyz/openbmc_project/Common/error.hpp>
Matthew Barthcc8912e2019-01-21 11:35:27 -060027#include "config.h"
Matt Spinler7f88fe62017-04-10 14:39:02 -050028#include "zone.hpp"
Matthew Barthdf3e8d62017-05-31 11:07:24 -050029#include "utility.hpp"
Matthew Barthd953bb22017-08-22 10:45:28 -050030#include "sdbusplus.hpp"
Matt Spinler7f88fe62017-04-10 14:39:02 -050031
32namespace phosphor
33{
34namespace fan
35{
36namespace control
37{
38
Matthew Barth8600d9a2017-06-23 14:38:05 -050039using namespace std::chrono;
Matthew Barth90149802017-08-15 10:51:37 -050040using namespace phosphor::fan;
Matthew Barthdf3e8d62017-05-31 11:07:24 -050041using namespace phosphor::logging;
Matthew Barthcc8912e2019-01-21 11:35:27 -060042namespace fs = std::experimental::filesystem;
Dinesh Chinari618027a2017-06-26 23:26:50 -050043using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
44 Error::InternalFailure;
Matt Spinler7f88fe62017-04-10 14:39:02 -050045
Matthew Barth14184132017-05-19 14:37:30 -050046Zone::Zone(Mode mode,
47 sdbusplus::bus::bus& bus,
Matthew Barth93af4192019-01-18 09:30:57 -060048 const std::string& path,
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070049 const sdeventplus::Event& event,
Matt Spinler7f88fe62017-04-10 14:39:02 -050050 const ZoneDefinition& def) :
Matthew Barth93af4192019-01-18 09:30:57 -060051 ThermalObject(bus, path.c_str(), true),
Matt Spinler7f88fe62017-04-10 14:39:02 -050052 _bus(bus),
Matthew Barth93af4192019-01-18 09:30:57 -060053 _path(path),
Matthew Barth766f8542019-01-29 12:44:13 -060054 _ifaces({"xyz.openbmc_project.Control.ThermalMode"}),
Matt Spinler7f88fe62017-04-10 14:39:02 -050055 _fullSpeed(std::get<fullSpeedPos>(def)),
Matthew Barth1de66622017-06-12 13:13:02 -050056 _zoneNum(std::get<zoneNumPos>(def)),
Matthew Barthe0ca13e2017-06-13 16:29:09 -050057 _defFloorSpeed(std::get<floorSpeedPos>(def)),
Matthew Barth8600d9a2017-06-23 14:38:05 -050058 _defCeilingSpeed(std::get<fullSpeedPos>(def)),
Matthew Bartha9561842017-06-29 11:43:45 -050059 _incDelay(std::get<incDelayPos>(def)),
60 _decInterval(std::get<decIntervalPos>(def)),
William A. Kennington III22c36ab2018-10-30 19:50:57 -070061 _incTimer(event, std::bind(&Zone::incTimerExpired, this)),
62 _decTimer(event, std::bind(&Zone::decTimerExpired, this)),
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070063 _eventLoop(event)
Matt Spinler7f88fe62017-04-10 14:39:02 -050064{
65 auto& fanDefs = std::get<fanListPos>(def);
66
67 for (auto& def : fanDefs)
68 {
69 _fans.emplace_back(std::make_unique<Fan>(bus, def));
70 }
Matthew Barth38a93a82017-05-11 14:12:27 -050071
Matthew Barth14184132017-05-19 14:37:30 -050072 // Do not enable set speed events when in init mode
Matthew Barth93af4192019-01-18 09:30:57 -060073 if (mode == Mode::control)
Matthew Barth17d1fe22017-05-11 15:00:36 -050074 {
Matthew Barth1b3e9602019-02-13 11:37:03 -060075 // Process any zone handlers defined
76 for (auto& hand : std::get<handlerPos>(def))
77 {
78 hand(*this);
79 }
80
Matthew Barth9e4db252019-01-21 13:08:02 -060081 // Restore thermal control current mode state
82 restoreCurrentMode();
Matthew Barth93af4192019-01-18 09:30:57 -060083
84 // Emit objects added in control mode only
85 this->emit_object_added();
86
Matthew Barth2b3db612017-10-25 10:56:51 -050087 // Update target speed to current zone target speed
88 if (!_fans.empty())
89 {
90 _targetSpeed = _fans.front()->getTargetSpeed();
91 }
Matthew Barthccc77702017-07-28 13:43:04 -050092 // Setup signal trigger for set speed events
93 for (auto& event : std::get<setSpeedEventsPos>(def))
94 {
95 initEvent(event);
96 }
Matthew Barth8600d9a2017-06-23 14:38:05 -050097 // Start timer for fan speed decreases
William A. Kennington III8fd879f2018-10-30 19:49:29 -070098 _decTimer.restart(_decInterval);
Matthew Barth38a93a82017-05-11 14:12:27 -050099 }
Matt Spinler7f88fe62017-04-10 14:39:02 -0500100}
101
Matt Spinler7f88fe62017-04-10 14:39:02 -0500102void Zone::setSpeed(uint64_t speed)
103{
Matthew Barth60b00762017-08-15 13:39:06 -0500104 if (_isActive)
Matt Spinler7f88fe62017-04-10 14:39:02 -0500105 {
Matthew Barth60b00762017-08-15 13:39:06 -0500106 _targetSpeed = speed;
107 for (auto& fan : _fans)
108 {
109 fan->setSpeed(_targetSpeed);
110 }
111 }
112}
113
114void Zone::setFullSpeed()
115{
116 if (_fullSpeed != 0)
117 {
118 _targetSpeed = _fullSpeed;
119 for (auto& fan : _fans)
120 {
121 fan->setSpeed(_targetSpeed);
122 }
Matt Spinler7f88fe62017-04-10 14:39:02 -0500123 }
124}
125
Matthew Barth861d77c2017-05-22 14:18:25 -0500126void Zone::setActiveAllow(const Group* group, bool isActiveAllow)
127{
Matthew Barth60b00762017-08-15 13:39:06 -0500128 _active[*(group)] = isActiveAllow;
Matthew Barth861d77c2017-05-22 14:18:25 -0500129 if (!isActiveAllow)
130 {
131 _isActive = false;
132 }
133 else
134 {
135 // Check all entries are set to allow control active
136 auto actPred = [](auto const& entry) {return entry.second;};
137 _isActive = std::all_of(_active.begin(),
138 _active.end(),
139 actPred);
140 }
141}
142
Matthew Barth55dea642017-11-06 13:34:32 -0600143void Zone::removeService(const Group* group,
144 const std::string& name)
145{
146 try
147 {
148 auto& sNames = _services.at(*group);
149 auto it = std::find_if(
150 sNames.begin(),
151 sNames.end(),
152 [&name](auto const& entry)
153 {
154 return name == std::get<namePos>(entry);
155 }
156 );
157 if (it != std::end(sNames))
158 {
159 // Remove service name from group
160 sNames.erase(it);
161 }
162 }
163 catch (const std::out_of_range& oore)
164 {
165 // No services for group found
166 }
167}
168
Matthew Barthe59fdf72017-09-27 09:33:42 -0500169void Zone::setServiceOwner(const Group* group,
170 const std::string& name,
171 const bool hasOwner)
172{
173 try
174 {
175 auto& sNames = _services.at(*group);
176 auto it = std::find_if(
177 sNames.begin(),
178 sNames.end(),
179 [&name](auto const& entry)
180 {
181 return name == std::get<namePos>(entry);
182 }
183 );
184 if (it != std::end(sNames))
185 {
186 std::get<hasOwnerPos>(*it) = hasOwner;
187 }
188 else
189 {
190 _services[*group].emplace_back(name, hasOwner);
191 }
192 }
193 catch (const std::out_of_range& oore)
194 {
195 _services[*group].emplace_back(name, hasOwner);
196 }
197}
198
Matthew Barth480787c2017-11-06 11:00:00 -0600199void Zone::setServices(const Group* group)
200{
Matthew Barth55dea642017-11-06 13:34:32 -0600201 // Remove the empty service name if exists
202 removeService(group, "");
Matthew Barth480787c2017-11-06 11:00:00 -0600203 for (auto it = group->begin(); it != group->end(); ++it)
204 {
205 std::string name;
206 bool hasOwner = false;
207 try
208 {
Matthew Barthc72b8912018-01-19 17:28:18 -0600209 name = getService(it->first,
210 std::get<intfPos>(it->second));
Matthew Barth480787c2017-11-06 11:00:00 -0600211 hasOwner = util::SDBusPlus::callMethodAndRead<bool>(
212 _bus,
213 "org.freedesktop.DBus",
214 "/org/freedesktop/DBus",
215 "org.freedesktop.DBus",
216 "NameHasOwner",
217 name);
218 }
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500219 catch (const util::DBusMethodError& e)
Matthew Barth480787c2017-11-06 11:00:00 -0600220 {
221 // Failed to get service name owner state
222 hasOwner = false;
223 }
224 setServiceOwner(group, name, hasOwner);
225 }
226}
227
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500228void Zone::setFloor(uint64_t speed)
229{
Matthew Barth98726c42017-10-17 10:35:20 -0500230 // Check all entries are set to allow floor to be set
231 auto pred = [](auto const& entry) {return entry.second;};
232 auto setFloor = std::all_of(_floorChange.begin(),
233 _floorChange.end(),
234 pred);
235 if (setFloor)
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500236 {
Matthew Barth98726c42017-10-17 10:35:20 -0500237 _floorSpeed = speed;
238 // Floor speed above target, update target to floor speed
239 if (_targetSpeed < _floorSpeed)
240 {
241 requestSpeedIncrease(_floorSpeed - _targetSpeed);
242 }
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500243 }
244}
245
Matthew Barth240397b2017-06-22 11:23:30 -0500246void Zone::requestSpeedIncrease(uint64_t targetDelta)
247{
248 // Only increase speed when delta is higher than
249 // the current increase delta for the zone and currently under ceiling
250 if (targetDelta > _incSpeedDelta &&
251 _targetSpeed < _ceilingSpeed)
252 {
Matthew Barth4e728542017-09-14 16:47:55 -0500253 auto requestTarget = getRequestSpeedBase();
Matthew Barth60b00762017-08-15 13:39:06 -0500254 requestTarget = (targetDelta - _incSpeedDelta) + requestTarget;
Matthew Barth240397b2017-06-22 11:23:30 -0500255 _incSpeedDelta = targetDelta;
Matthew Barth240397b2017-06-22 11:23:30 -0500256 // Target speed can not go above a defined ceiling speed
Matthew Barth60b00762017-08-15 13:39:06 -0500257 if (requestTarget > _ceilingSpeed)
Matthew Barth240397b2017-06-22 11:23:30 -0500258 {
Matthew Barth60b00762017-08-15 13:39:06 -0500259 requestTarget = _ceilingSpeed;
Matthew Barth240397b2017-06-22 11:23:30 -0500260 }
Matthew Barth60b00762017-08-15 13:39:06 -0500261 setSpeed(requestTarget);
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700262 // Retart timer countdown for fan speed increase
263 _incTimer.restartOnce(_incDelay);
Matthew Barth240397b2017-06-22 11:23:30 -0500264 }
Matthew Barth1ee48f22017-06-27 15:14:48 -0500265}
266
267void Zone::incTimerExpired()
268{
269 // Clear increase delta when timer expires allowing additional speed
270 // increase requests or speed decreases to occur
Matthew Barth240397b2017-06-22 11:23:30 -0500271 _incSpeedDelta = 0;
272}
273
Matthew Barth0ce99d82017-06-22 15:07:29 -0500274void Zone::requestSpeedDecrease(uint64_t targetDelta)
275{
276 // Only decrease the lowest target delta requested
277 if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta)
278 {
279 _decSpeedDelta = targetDelta;
280 }
Matthew Barth8600d9a2017-06-23 14:38:05 -0500281}
Matthew Barth0ce99d82017-06-22 15:07:29 -0500282
Matthew Barth8600d9a2017-06-23 14:38:05 -0500283void Zone::decTimerExpired()
284{
Matthew Barthe4338cd2017-12-14 11:14:30 -0600285 // Check all entries are set to allow a decrease
286 auto pred = [](auto const& entry) {return entry.second;};
287 auto decAllowed = std::all_of(_decAllowed.begin(),
288 _decAllowed.end(),
289 pred);
290
291 // Only decrease speeds when allowed,
292 // where no requested increases exist and
293 // the increase timer is not running
294 // (i.e. not in the middle of increasing)
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700295 if (decAllowed && _incSpeedDelta == 0 && !_incTimer.isEnabled())
Matthew Barth0ce99d82017-06-22 15:07:29 -0500296 {
Matthew Barth4e728542017-09-14 16:47:55 -0500297 auto requestTarget = getRequestSpeedBase();
Matthew Barthc63973a2017-12-08 15:31:35 -0600298 // Request target speed should not start above ceiling
299 if (requestTarget > _ceilingSpeed)
300 {
301 requestTarget = _ceilingSpeed;
302 }
Matthew Barth0ce99d82017-06-22 15:07:29 -0500303 // Target speed can not go below the defined floor speed
Matthew Barth60b00762017-08-15 13:39:06 -0500304 if ((requestTarget < _decSpeedDelta) ||
305 (requestTarget - _decSpeedDelta < _floorSpeed))
Matthew Barth0ce99d82017-06-22 15:07:29 -0500306 {
Matthew Barth60b00762017-08-15 13:39:06 -0500307 requestTarget = _floorSpeed;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500308 }
309 else
310 {
Matthew Barth60b00762017-08-15 13:39:06 -0500311 requestTarget = requestTarget - _decSpeedDelta;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500312 }
Matthew Barth60b00762017-08-15 13:39:06 -0500313 setSpeed(requestTarget);
Matthew Barth0ce99d82017-06-22 15:07:29 -0500314 }
315 // Clear decrease delta when timer expires
316 _decSpeedDelta = 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500317 // Decrease timer is restarted since its repeating
Matthew Barth0ce99d82017-06-22 15:07:29 -0500318}
319
Matthew Barthccc77702017-07-28 13:43:04 -0500320void Zone::initEvent(const SetSpeedEvent& event)
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500321{
Matthew Barth336f18a2017-09-26 09:15:56 -0500322 sdbusplus::message::message nullMsg{nullptr};
323
Matthew Barth67967f92017-09-22 12:43:57 -0500324 for (auto& sig : std::get<signalsPos>(event))
Matthew Barthccc77702017-07-28 13:43:04 -0500325 {
Matthew Barth336f18a2017-09-26 09:15:56 -0500326 // Setup signal matches of the property for event
Matthew Barthf6b76d82017-08-04 12:58:02 -0500327 std::unique_ptr<EventData> eventData =
328 std::make_unique<EventData>(
Matthew Barthf6b76d82017-08-04 12:58:02 -0500329 std::get<groupPos>(event),
Matthew Barth336f18a2017-09-26 09:15:56 -0500330 std::get<sigMatchPos>(sig),
331 std::get<sigHandlerPos>(sig),
Matthew Barthf9201ab2017-09-11 16:07:58 -0500332 std::get<actionsPos>(event)
Matthew Barthf6b76d82017-08-04 12:58:02 -0500333 );
Matthew Barth766f8542019-01-29 12:44:13 -0600334
335 // When match is empty, handle if zone object member
336 if (std::get<sigMatchPos>(sig).empty())
337 {
Matthew Barth766f8542019-01-29 12:44:13 -0600338 // Set event data for each host group member
339 for (auto it = std::get<groupPos>(event).begin();
340 it != std::get<groupPos>(event).end(); ++it)
341 {
Matthew Barth0a1f6862019-01-30 16:27:38 -0600342 if (it->first == _path)
Matthew Barth766f8542019-01-29 12:44:13 -0600343 {
344 // Group member interface in list owned by zone
345 if (std::find(_ifaces.begin(), _ifaces.end(),
346 std::get<intfPos>(it->second)) != _ifaces.end())
347 {
348 // Store path,interface,property as a managed object
349 _objects[it->first]
350 [std::get<intfPos>(it->second)]
351 [std::get<propPos>(it->second)] =
352 eventData.get();
353 }
354 }
355 }
356 }
357
358 // Initialize the event signal using handler
359 std::get<sigHandlerPos>(sig)(_bus, nullMsg, *this);
360
361 // Subscribe to signal match
Matthew Barth336f18a2017-09-26 09:15:56 -0500362 std::unique_ptr<sdbusplus::server::match::match> match = nullptr;
363 if (!std::get<sigMatchPos>(sig).empty())
364 {
365 match = std::make_unique<sdbusplus::server::match::match>(
366 _bus,
367 std::get<sigMatchPos>(sig).c_str(),
368 std::bind(std::mem_fn(&Zone::handleEvent),
369 this,
370 std::placeholders::_1,
371 eventData.get())
372 );
373 }
Matthew Barth766f8542019-01-29 12:44:13 -0600374
Matthew Barthf6b76d82017-08-04 12:58:02 -0500375 _signalEvents.emplace_back(std::move(eventData), std::move(match));
Matthew Barthccc77702017-07-28 13:43:04 -0500376 }
Matthew Barth1b4de262018-03-06 13:03:16 -0600377 // Enable event triggers
378 std::for_each(
379 std::get<triggerPos>(event).begin(),
380 std::get<triggerPos>(event).end(),
381 [this, &event](auto const& trigger)
382 {
383 trigger(*this,
384 std::get<groupPos>(event),
385 std::get<actionsPos>(event));
386 }
387 );
388
Matthew Barthf9201ab2017-09-11 16:07:58 -0500389 // Run action functions for initial event state
390 std::for_each(
391 std::get<actionsPos>(event).begin(),
392 std::get<actionsPos>(event).end(),
393 [this, &event](auto const& action)
394 {
395 action(*this,
396 std::get<groupPos>(event));
397 });
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500398}
399
Matthew Barthf6b76d82017-08-04 12:58:02 -0500400void Zone::removeEvent(const SetSpeedEvent& event)
401{
Matthew Barth33bfe762018-11-05 11:13:25 -0600402 // Remove signals of the event
403 for (auto& sig : std::get<signalsPos>(event))
Matthew Barthf6b76d82017-08-04 12:58:02 -0500404 {
Matthew Barth33bfe762018-11-05 11:13:25 -0600405 auto it = findSignal(sig,
406 std::get<groupPos>(event),
407 std::get<actionsPos>(event));
408 if (it != std::end(getSignalEvents()))
Matthew Barth336f18a2017-09-26 09:15:56 -0500409 {
Matthew Barth33bfe762018-11-05 11:13:25 -0600410 removeSignal(it);
Matthew Barth336f18a2017-09-26 09:15:56 -0500411 }
Matthew Barthf6b76d82017-08-04 12:58:02 -0500412 }
Matthew Barth33bfe762018-11-05 11:13:25 -0600413 // Remove timers of the event
Matthew Barth1b4de262018-03-06 13:03:16 -0600414 auto it = findTimer(std::get<groupPos>(event),
415 std::get<actionsPos>(event));
416 if (it != std::end(getTimerEvents()))
Matthew Barth33bfe762018-11-05 11:13:25 -0600417 {
Matthew Barth1b4de262018-03-06 13:03:16 -0600418 removeTimer(it);
Matthew Barth33bfe762018-11-05 11:13:25 -0600419 }
420}
421
422std::vector<SignalEvent>::iterator Zone::findSignal(
423 const Signal& signal,
424 const Group& eGroup,
425 const std::vector<Action>& eActions)
426{
427 // Find the signal in the event to be removed
428 for (auto it = _signalEvents.begin(); it != _signalEvents.end(); ++ it)
429 {
430 const auto& seEventData = *std::get<signalEventDataPos>(*it);
431 if (eGroup == std::get<eventGroupPos>(seEventData) &&
432 std::get<sigMatchPos>(signal) ==
433 std::get<eventMatchPos>(seEventData) &&
434 std::get<sigHandlerPos>(signal).target_type().name() ==
435 std::get<eventHandlerPos>(seEventData).target_type().name() &&
436 eActions.size() == std::get<eventActionsPos>(seEventData).size())
437 {
438 // TODO openbmc/openbmc#2328 - Use the function target
439 // for comparison
440 auto actsEqual = [](auto const& a1,
441 auto const& a2)
442 {
443 return a1.target_type().name() ==
444 a2.target_type().name();
445 };
446 if (std::equal(eActions.begin(),
447 eActions.end(),
448 std::get<eventActionsPos>(seEventData).begin(),
449 actsEqual))
450 {
451 return it;
452 }
453 }
454 }
455
456 return _signalEvents.end();
Matthew Barthf6b76d82017-08-04 12:58:02 -0500457}
458
Matthew Barthbfb1a562017-10-05 17:03:40 -0500459std::vector<TimerEvent>::iterator Zone::findTimer(
460 const Group& eventGroup,
461 const std::vector<Action>& eventActions)
462{
463 for (auto it = _timerEvents.begin(); it != _timerEvents.end(); ++it)
464 {
William A. Kennington III0420a932018-10-30 19:53:16 -0700465 const auto& teEventData = *std::get<timerEventDataPos>(*it);
Matthew Barthbfb1a562017-10-05 17:03:40 -0500466 if (std::get<eventActionsPos>(teEventData).size() ==
467 eventActions.size())
468 {
469 // TODO openbmc/openbmc#2328 - Use the action function target
470 // for comparison
471 auto actsEqual = [](auto const& a1,
472 auto const& a2)
473 {
474 return a1.target_type().name() ==
475 a2.target_type().name();
476 };
477 if (std::get<eventGroupPos>(teEventData) == eventGroup &&
478 std::equal(eventActions.begin(),
479 eventActions.end(),
480 std::get<eventActionsPos>(teEventData).begin(),
481 actsEqual))
482 {
483 return it;
484 }
485 }
486 }
487
488 return _timerEvents.end();
489}
490
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700491void Zone::addTimer(const Group& group,
492 const std::vector<Action>& actions,
493 const TimerConf& tConf)
494{
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700495 auto eventData = std::make_unique<EventData>(
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700496 group,
497 "",
498 nullptr,
499 actions
500 );
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700501 Timer timer(
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700502 _eventLoop,
William A. Kennington IIIc0c5f072018-10-30 19:11:01 -0700503 std::bind(&Zone::timerExpired,
504 this,
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700505 std::cref(std::get<Group>(*eventData)),
506 std::cref(std::get<std::vector<Action>>(*eventData))));
507 if (std::get<TimerType>(tConf) == TimerType::repeating)
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700508 {
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700509 timer.restart(std::get<intervalPos>(tConf));
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700510 }
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700511 else if (std::get<TimerType>(tConf) == TimerType::oneshot)
512 {
513 timer.restartOnce(std::get<intervalPos>(tConf));
514 }
515 else
516 {
517 throw std::invalid_argument("Invalid Timer Type");
518 }
519 _timerEvents.emplace_back(std::move(eventData), std::move(timer));
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700520}
521
William A. Kennington IIIc0c5f072018-10-30 19:11:01 -0700522void Zone::timerExpired(const Group& eventGroup,
523 const std::vector<Action>& eventActions)
Matthew Barth90149802017-08-15 10:51:37 -0500524{
Matthew Barthf9201ab2017-09-11 16:07:58 -0500525 // Perform the actions
526 std::for_each(eventActions.begin(),
527 eventActions.end(),
528 [this, &eventGroup](auto const& action)
529 {
530 action(*this, eventGroup);
531 });
Matthew Barth90149802017-08-15 10:51:37 -0500532}
533
Matthew Barth38a93a82017-05-11 14:12:27 -0500534void Zone::handleEvent(sdbusplus::message::message& msg,
Matthew Barth34f1bda2017-05-31 13:45:36 -0500535 const EventData* eventData)
Matthew Barth38a93a82017-05-11 14:12:27 -0500536{
537 // Handle the callback
Matthew Barth34f1bda2017-05-31 13:45:36 -0500538 std::get<eventHandlerPos>(*eventData)(_bus, msg, *this);
Matthew Barthf9201ab2017-09-11 16:07:58 -0500539 // Perform the actions
540 std::for_each(
541 std::get<eventActionsPos>(*eventData).begin(),
542 std::get<eventActionsPos>(*eventData).end(),
543 [this, &eventData](auto const& action)
544 {
545 action(*this,
546 std::get<eventGroupPos>(*eventData));
547 });
Matthew Barth38a93a82017-05-11 14:12:27 -0500548}
549
Matthew Bartha603ed02018-01-19 16:56:26 -0600550const std::string& Zone::getService(const std::string& path,
551 const std::string& intf)
552{
553 // Retrieve service from cache
554 auto srvIter = _servTree.find(path);
555 if (srvIter != _servTree.end())
556 {
557 for (auto& serv : srvIter->second)
558 {
559 auto it = std::find_if(
560 serv.second.begin(),
561 serv.second.end(),
562 [&intf](auto const& interface)
563 {
564 return intf == interface;
565 });
566 if (it != std::end(serv.second))
567 {
568 // Service found
569 return serv.first;
570 }
571 }
572 // Interface not found in cache, add and return
573 return addServices(path, intf, 0);
574 }
575 else
576 {
577 // Path not found in cache, add and return
578 return addServices(path, intf, 0);
579 }
580}
581
582const std::string& Zone::addServices(const std::string& path,
583 const std::string& intf,
584 int32_t depth)
585{
586 static const std::string empty = "";
587 auto it = _servTree.end();
588
589 // Get all subtree objects for the given interface
590 auto objects = util::SDBusPlus::getSubTree(_bus, "/", intf, depth);
591 // Add what's returned to the cache of path->services
592 for (auto& pIter : objects)
593 {
594 auto pathIter = _servTree.find(pIter.first);
595 if (pathIter != _servTree.end())
596 {
597 // Path found in cache
598 for (auto& sIter : pIter.second)
599 {
600 auto servIter = pathIter->second.find(sIter.first);
601 if (servIter != pathIter->second.end())
602 {
603 // Service found in cache
604 for (auto& iIter : sIter.second)
605 {
Matthew Barthe8b340b2018-11-09 10:05:34 -0600606 if (std::find(servIter->second.begin(),
607 servIter->second.end(),
608 iIter) == servIter->second.end())
609 {
610 // Add interface to cache
611 servIter->second.emplace_back(iIter);
612 }
Matthew Bartha603ed02018-01-19 16:56:26 -0600613 }
614 }
615 else
616 {
617 // Service not found in cache
618 pathIter->second.insert(sIter);
619 }
620 }
621 }
622 else
623 {
624 _servTree.insert(pIter);
625 }
626 // When the paths match, since a single interface constraint is given,
627 // that is the service to return
628 if (path == pIter.first)
629 {
630 it = _servTree.find(pIter.first);
631 }
632 }
633
634 if (it != _servTree.end())
635 {
636 return it->second.begin()->first;
637 }
638
639 return empty;
640}
641
Matthew Barth70b2e7d2019-02-18 11:03:07 -0600642auto Zone::getPersisted(const std::string& intf,
643 const std::string& prop)
644{
645 auto persisted = false;
646
647 auto it = _persisted.find(intf);
648 if (it != _persisted.end())
649 {
650 return std::any_of(it->second.begin(),
651 it->second.end(),
652 [&prop](auto& p)
653 {
654 return prop == p;
655 });
656 }
657
658 return persisted;
659}
660
Matthew Barth6faf8942019-01-22 09:26:09 -0600661std::string Zone::current(std::string value)
662{
Matthew Barthb390df12019-02-14 15:18:27 -0600663 auto current = ThermalObject::current();
664 std::transform(value.begin(), value.end(), value.begin(), toupper);
665
Matthew Barth221c90c2019-02-15 10:35:50 -0600666 auto supported = ThermalObject::supported();
667 auto isSupported = std::any_of(
668 supported.begin(),
669 supported.end(),
670 [&value](auto& s)
671 {
672 std::transform(s.begin(), s.end(), s.begin(), toupper);
673 return value == s;
674 });
675
676 if (value != current && isSupported)
Matthew Barth6faf8942019-01-22 09:26:09 -0600677 {
678 current = ThermalObject::current(value);
Matthew Barth70b2e7d2019-02-18 11:03:07 -0600679 if (getPersisted("xyz.openbmc_project.Control.ThermalMode", "Current"))
680 {
681 saveCurrentMode();
682 }
Matthew Barthb390df12019-02-14 15:18:27 -0600683 // Trigger event(s) for current mode property change
Matthew Barth0a1f6862019-01-30 16:27:38 -0600684 auto eData = _objects[_path]
Matthew Barthbaea6c32019-01-29 14:20:19 -0600685 ["xyz.openbmc_project.Control.ThermalMode"]
686 ["Current"];
687 if (eData != nullptr)
688 {
689 sdbusplus::message::message nullMsg{nullptr};
690 handleEvent(nullMsg, eData);
691 }
Matthew Barth6faf8942019-01-22 09:26:09 -0600692 }
Matthew Barthb390df12019-02-14 15:18:27 -0600693
Matthew Barth6faf8942019-01-22 09:26:09 -0600694 return current;
695}
696
Matthew Barthcc8912e2019-01-21 11:35:27 -0600697void Zone::saveCurrentMode()
698{
699 fs::path path{CONTROL_PERSIST_ROOT_PATH};
700 // Append zone and property description
701 path /= std::to_string(_zoneNum);
702 path /= "CurrentMode";
703 std::ofstream ofs(path.c_str(), std::ios::binary);
704 cereal::JSONOutputArchive oArch(ofs);
705 oArch(ThermalObject::current());
706}
707
Matthew Barth9e4db252019-01-21 13:08:02 -0600708void Zone::restoreCurrentMode()
709{
Matthew Bartha2bed6e2019-02-14 16:07:00 -0600710 auto current = ThermalObject::current();
Matthew Barth9e4db252019-01-21 13:08:02 -0600711 fs::path path{CONTROL_PERSIST_ROOT_PATH};
712 path /= std::to_string(_zoneNum);
713 path /= "CurrentMode";
714 fs::create_directories(path.parent_path());
715
716 try
717 {
718 if (fs::exists(path))
719 {
720 std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary);
721 cereal::JSONInputArchive iArch(ifs);
722 iArch(current);
723 }
724 }
725 catch (std::exception& e)
726 {
727 log<level::ERR>(e.what());
728 fs::remove(path);
Matthew Bartha2bed6e2019-02-14 16:07:00 -0600729 current = ThermalObject::current();
Matthew Barth9e4db252019-01-21 13:08:02 -0600730 }
731
732 this->current(current);
733}
734
Matt Spinler7f88fe62017-04-10 14:39:02 -0500735}
736}
737}