blob: bc40b7e6606739836412378a1ae502bb581a38f3 [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),
Matt Spinler7f88fe62017-04-10 14:39:02 -050054 _fullSpeed(std::get<fullSpeedPos>(def)),
Matthew Barth1de66622017-06-12 13:13:02 -050055 _zoneNum(std::get<zoneNumPos>(def)),
Matthew Barthe0ca13e2017-06-13 16:29:09 -050056 _defFloorSpeed(std::get<floorSpeedPos>(def)),
Matthew Barth8600d9a2017-06-23 14:38:05 -050057 _defCeilingSpeed(std::get<fullSpeedPos>(def)),
Matthew Bartha9561842017-06-29 11:43:45 -050058 _incDelay(std::get<incDelayPos>(def)),
59 _decInterval(std::get<decIntervalPos>(def)),
William A. Kennington III22c36ab2018-10-30 19:50:57 -070060 _incTimer(event, std::bind(&Zone::incTimerExpired, this)),
61 _decTimer(event, std::bind(&Zone::decTimerExpired, this)),
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070062 _eventLoop(event)
Matt Spinler7f88fe62017-04-10 14:39:02 -050063{
64 auto& fanDefs = std::get<fanListPos>(def);
65
66 for (auto& def : fanDefs)
67 {
68 _fans.emplace_back(std::make_unique<Fan>(bus, def));
69 }
Matthew Barth38a93a82017-05-11 14:12:27 -050070
Matthew Barth14184132017-05-19 14:37:30 -050071 // Do not enable set speed events when in init mode
Matthew Barth93af4192019-01-18 09:30:57 -060072 if (mode == Mode::control)
Matthew Barth17d1fe22017-05-11 15:00:36 -050073 {
Matthew Barth9e4db252019-01-21 13:08:02 -060074 // Restore thermal control current mode state
75 restoreCurrentMode();
Matthew Barth93af4192019-01-18 09:30:57 -060076
77 // Emit objects added in control mode only
78 this->emit_object_added();
79
Matthew Barth2b3db612017-10-25 10:56:51 -050080 // Update target speed to current zone target speed
81 if (!_fans.empty())
82 {
83 _targetSpeed = _fans.front()->getTargetSpeed();
84 }
Matthew Barthccc77702017-07-28 13:43:04 -050085 // Setup signal trigger for set speed events
86 for (auto& event : std::get<setSpeedEventsPos>(def))
87 {
88 initEvent(event);
89 }
Matthew Barth8600d9a2017-06-23 14:38:05 -050090 // Start timer for fan speed decreases
William A. Kennington III8fd879f2018-10-30 19:49:29 -070091 _decTimer.restart(_decInterval);
Matthew Barth38a93a82017-05-11 14:12:27 -050092 }
Matt Spinler7f88fe62017-04-10 14:39:02 -050093}
94
Matt Spinler7f88fe62017-04-10 14:39:02 -050095void Zone::setSpeed(uint64_t speed)
96{
Matthew Barth60b00762017-08-15 13:39:06 -050097 if (_isActive)
Matt Spinler7f88fe62017-04-10 14:39:02 -050098 {
Matthew Barth60b00762017-08-15 13:39:06 -050099 _targetSpeed = speed;
100 for (auto& fan : _fans)
101 {
102 fan->setSpeed(_targetSpeed);
103 }
104 }
105}
106
107void Zone::setFullSpeed()
108{
109 if (_fullSpeed != 0)
110 {
111 _targetSpeed = _fullSpeed;
112 for (auto& fan : _fans)
113 {
114 fan->setSpeed(_targetSpeed);
115 }
Matt Spinler7f88fe62017-04-10 14:39:02 -0500116 }
117}
118
Matthew Barth861d77c2017-05-22 14:18:25 -0500119void Zone::setActiveAllow(const Group* group, bool isActiveAllow)
120{
Matthew Barth60b00762017-08-15 13:39:06 -0500121 _active[*(group)] = isActiveAllow;
Matthew Barth861d77c2017-05-22 14:18:25 -0500122 if (!isActiveAllow)
123 {
124 _isActive = false;
125 }
126 else
127 {
128 // Check all entries are set to allow control active
129 auto actPred = [](auto const& entry) {return entry.second;};
130 _isActive = std::all_of(_active.begin(),
131 _active.end(),
132 actPred);
133 }
134}
135
Matthew Barth55dea642017-11-06 13:34:32 -0600136void Zone::removeService(const Group* group,
137 const std::string& name)
138{
139 try
140 {
141 auto& sNames = _services.at(*group);
142 auto it = std::find_if(
143 sNames.begin(),
144 sNames.end(),
145 [&name](auto const& entry)
146 {
147 return name == std::get<namePos>(entry);
148 }
149 );
150 if (it != std::end(sNames))
151 {
152 // Remove service name from group
153 sNames.erase(it);
154 }
155 }
156 catch (const std::out_of_range& oore)
157 {
158 // No services for group found
159 }
160}
161
Matthew Barthe59fdf72017-09-27 09:33:42 -0500162void Zone::setServiceOwner(const Group* group,
163 const std::string& name,
164 const bool hasOwner)
165{
166 try
167 {
168 auto& sNames = _services.at(*group);
169 auto it = std::find_if(
170 sNames.begin(),
171 sNames.end(),
172 [&name](auto const& entry)
173 {
174 return name == std::get<namePos>(entry);
175 }
176 );
177 if (it != std::end(sNames))
178 {
179 std::get<hasOwnerPos>(*it) = hasOwner;
180 }
181 else
182 {
183 _services[*group].emplace_back(name, hasOwner);
184 }
185 }
186 catch (const std::out_of_range& oore)
187 {
188 _services[*group].emplace_back(name, hasOwner);
189 }
190}
191
Matthew Barth480787c2017-11-06 11:00:00 -0600192void Zone::setServices(const Group* group)
193{
Matthew Barth55dea642017-11-06 13:34:32 -0600194 // Remove the empty service name if exists
195 removeService(group, "");
Matthew Barth480787c2017-11-06 11:00:00 -0600196 for (auto it = group->begin(); it != group->end(); ++it)
197 {
198 std::string name;
199 bool hasOwner = false;
200 try
201 {
Matthew Barthc72b8912018-01-19 17:28:18 -0600202 name = getService(it->first,
203 std::get<intfPos>(it->second));
Matthew Barth480787c2017-11-06 11:00:00 -0600204 hasOwner = util::SDBusPlus::callMethodAndRead<bool>(
205 _bus,
206 "org.freedesktop.DBus",
207 "/org/freedesktop/DBus",
208 "org.freedesktop.DBus",
209 "NameHasOwner",
210 name);
211 }
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500212 catch (const util::DBusMethodError& e)
Matthew Barth480787c2017-11-06 11:00:00 -0600213 {
214 // Failed to get service name owner state
215 hasOwner = false;
216 }
217 setServiceOwner(group, name, hasOwner);
218 }
219}
220
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500221void Zone::setFloor(uint64_t speed)
222{
Matthew Barth98726c42017-10-17 10:35:20 -0500223 // Check all entries are set to allow floor to be set
224 auto pred = [](auto const& entry) {return entry.second;};
225 auto setFloor = std::all_of(_floorChange.begin(),
226 _floorChange.end(),
227 pred);
228 if (setFloor)
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500229 {
Matthew Barth98726c42017-10-17 10:35:20 -0500230 _floorSpeed = speed;
231 // Floor speed above target, update target to floor speed
232 if (_targetSpeed < _floorSpeed)
233 {
234 requestSpeedIncrease(_floorSpeed - _targetSpeed);
235 }
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500236 }
237}
238
Matthew Barth240397b2017-06-22 11:23:30 -0500239void Zone::requestSpeedIncrease(uint64_t targetDelta)
240{
241 // Only increase speed when delta is higher than
242 // the current increase delta for the zone and currently under ceiling
243 if (targetDelta > _incSpeedDelta &&
244 _targetSpeed < _ceilingSpeed)
245 {
Matthew Barth4e728542017-09-14 16:47:55 -0500246 auto requestTarget = getRequestSpeedBase();
Matthew Barth60b00762017-08-15 13:39:06 -0500247 requestTarget = (targetDelta - _incSpeedDelta) + requestTarget;
Matthew Barth240397b2017-06-22 11:23:30 -0500248 _incSpeedDelta = targetDelta;
Matthew Barth240397b2017-06-22 11:23:30 -0500249 // Target speed can not go above a defined ceiling speed
Matthew Barth60b00762017-08-15 13:39:06 -0500250 if (requestTarget > _ceilingSpeed)
Matthew Barth240397b2017-06-22 11:23:30 -0500251 {
Matthew Barth60b00762017-08-15 13:39:06 -0500252 requestTarget = _ceilingSpeed;
Matthew Barth240397b2017-06-22 11:23:30 -0500253 }
Matthew Barth60b00762017-08-15 13:39:06 -0500254 setSpeed(requestTarget);
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700255 // Retart timer countdown for fan speed increase
256 _incTimer.restartOnce(_incDelay);
Matthew Barth240397b2017-06-22 11:23:30 -0500257 }
Matthew Barth1ee48f22017-06-27 15:14:48 -0500258}
259
260void Zone::incTimerExpired()
261{
262 // Clear increase delta when timer expires allowing additional speed
263 // increase requests or speed decreases to occur
Matthew Barth240397b2017-06-22 11:23:30 -0500264 _incSpeedDelta = 0;
265}
266
Matthew Barth0ce99d82017-06-22 15:07:29 -0500267void Zone::requestSpeedDecrease(uint64_t targetDelta)
268{
269 // Only decrease the lowest target delta requested
270 if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta)
271 {
272 _decSpeedDelta = targetDelta;
273 }
Matthew Barth8600d9a2017-06-23 14:38:05 -0500274}
Matthew Barth0ce99d82017-06-22 15:07:29 -0500275
Matthew Barth8600d9a2017-06-23 14:38:05 -0500276void Zone::decTimerExpired()
277{
Matthew Barthe4338cd2017-12-14 11:14:30 -0600278 // Check all entries are set to allow a decrease
279 auto pred = [](auto const& entry) {return entry.second;};
280 auto decAllowed = std::all_of(_decAllowed.begin(),
281 _decAllowed.end(),
282 pred);
283
284 // Only decrease speeds when allowed,
285 // where no requested increases exist and
286 // the increase timer is not running
287 // (i.e. not in the middle of increasing)
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700288 if (decAllowed && _incSpeedDelta == 0 && !_incTimer.isEnabled())
Matthew Barth0ce99d82017-06-22 15:07:29 -0500289 {
Matthew Barth4e728542017-09-14 16:47:55 -0500290 auto requestTarget = getRequestSpeedBase();
Matthew Barthc63973a2017-12-08 15:31:35 -0600291 // Request target speed should not start above ceiling
292 if (requestTarget > _ceilingSpeed)
293 {
294 requestTarget = _ceilingSpeed;
295 }
Matthew Barth0ce99d82017-06-22 15:07:29 -0500296 // Target speed can not go below the defined floor speed
Matthew Barth60b00762017-08-15 13:39:06 -0500297 if ((requestTarget < _decSpeedDelta) ||
298 (requestTarget - _decSpeedDelta < _floorSpeed))
Matthew Barth0ce99d82017-06-22 15:07:29 -0500299 {
Matthew Barth60b00762017-08-15 13:39:06 -0500300 requestTarget = _floorSpeed;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500301 }
302 else
303 {
Matthew Barth60b00762017-08-15 13:39:06 -0500304 requestTarget = requestTarget - _decSpeedDelta;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500305 }
Matthew Barth60b00762017-08-15 13:39:06 -0500306 setSpeed(requestTarget);
Matthew Barth0ce99d82017-06-22 15:07:29 -0500307 }
308 // Clear decrease delta when timer expires
309 _decSpeedDelta = 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500310 // Decrease timer is restarted since its repeating
Matthew Barth0ce99d82017-06-22 15:07:29 -0500311}
312
Matthew Barthccc77702017-07-28 13:43:04 -0500313void Zone::initEvent(const SetSpeedEvent& event)
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500314{
Matthew Barth336f18a2017-09-26 09:15:56 -0500315 sdbusplus::message::message nullMsg{nullptr};
316
Matthew Barth67967f92017-09-22 12:43:57 -0500317 for (auto& sig : std::get<signalsPos>(event))
Matthew Barthccc77702017-07-28 13:43:04 -0500318 {
Matthew Barth336f18a2017-09-26 09:15:56 -0500319 // Initialize the event signal using handler
320 std::get<sigHandlerPos>(sig)(_bus, nullMsg, *this);
321 // Setup signal matches of the property for event
Matthew Barthf6b76d82017-08-04 12:58:02 -0500322 std::unique_ptr<EventData> eventData =
323 std::make_unique<EventData>(
Matthew Barthf6b76d82017-08-04 12:58:02 -0500324 std::get<groupPos>(event),
Matthew Barth336f18a2017-09-26 09:15:56 -0500325 std::get<sigMatchPos>(sig),
326 std::get<sigHandlerPos>(sig),
Matthew Barthf9201ab2017-09-11 16:07:58 -0500327 std::get<actionsPos>(event)
Matthew Barthf6b76d82017-08-04 12:58:02 -0500328 );
Matthew Barth336f18a2017-09-26 09:15:56 -0500329 std::unique_ptr<sdbusplus::server::match::match> match = nullptr;
330 if (!std::get<sigMatchPos>(sig).empty())
331 {
332 match = std::make_unique<sdbusplus::server::match::match>(
333 _bus,
334 std::get<sigMatchPos>(sig).c_str(),
335 std::bind(std::mem_fn(&Zone::handleEvent),
336 this,
337 std::placeholders::_1,
338 eventData.get())
339 );
340 }
Matthew Barthf6b76d82017-08-04 12:58:02 -0500341 _signalEvents.emplace_back(std::move(eventData), std::move(match));
Matthew Barthccc77702017-07-28 13:43:04 -0500342 }
Matthew Barth90149802017-08-15 10:51:37 -0500343 // Attach a timer to run the action of an event
William A. Kennington III122b8432018-10-30 18:39:21 -0700344 auto timerConf = std::get<timerConfPos>(event);
345 if (std::get<intervalPos>(timerConf) != seconds(0))
Matthew Barth90149802017-08-15 10:51:37 -0500346 {
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700347 addTimer(std::get<groupPos>(event),
348 std::get<actionsPos>(event),
349 timerConf);
Matthew Barth90149802017-08-15 10:51:37 -0500350 }
Matthew Barthf9201ab2017-09-11 16:07:58 -0500351 // Run action functions for initial event state
352 std::for_each(
353 std::get<actionsPos>(event).begin(),
354 std::get<actionsPos>(event).end(),
355 [this, &event](auto const& action)
356 {
357 action(*this,
358 std::get<groupPos>(event));
359 });
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500360}
361
Matthew Barthf6b76d82017-08-04 12:58:02 -0500362void Zone::removeEvent(const SetSpeedEvent& event)
363{
Matthew Barth33bfe762018-11-05 11:13:25 -0600364 // Remove signals of the event
365 for (auto& sig : std::get<signalsPos>(event))
Matthew Barthf6b76d82017-08-04 12:58:02 -0500366 {
Matthew Barth33bfe762018-11-05 11:13:25 -0600367 auto it = findSignal(sig,
368 std::get<groupPos>(event),
369 std::get<actionsPos>(event));
370 if (it != std::end(getSignalEvents()))
Matthew Barth336f18a2017-09-26 09:15:56 -0500371 {
Matthew Barth33bfe762018-11-05 11:13:25 -0600372 removeSignal(it);
Matthew Barth336f18a2017-09-26 09:15:56 -0500373 }
Matthew Barthf6b76d82017-08-04 12:58:02 -0500374 }
Matthew Barth33bfe762018-11-05 11:13:25 -0600375 // Remove timers of the event
Matthew Barth8ce4b5f2018-11-06 13:54:46 -0600376 if (std::get<intervalPos>(std::get<timerConfPos>(event)) != seconds(0))
Matthew Barth33bfe762018-11-05 11:13:25 -0600377 {
378 auto it = findTimer(std::get<groupPos>(event),
379 std::get<actionsPos>(event));
380 if (it != std::end(getTimerEvents()))
381 {
382 removeTimer(it);
383 }
384 }
385}
386
387std::vector<SignalEvent>::iterator Zone::findSignal(
388 const Signal& signal,
389 const Group& eGroup,
390 const std::vector<Action>& eActions)
391{
392 // Find the signal in the event to be removed
393 for (auto it = _signalEvents.begin(); it != _signalEvents.end(); ++ it)
394 {
395 const auto& seEventData = *std::get<signalEventDataPos>(*it);
396 if (eGroup == std::get<eventGroupPos>(seEventData) &&
397 std::get<sigMatchPos>(signal) ==
398 std::get<eventMatchPos>(seEventData) &&
399 std::get<sigHandlerPos>(signal).target_type().name() ==
400 std::get<eventHandlerPos>(seEventData).target_type().name() &&
401 eActions.size() == std::get<eventActionsPos>(seEventData).size())
402 {
403 // TODO openbmc/openbmc#2328 - Use the function target
404 // for comparison
405 auto actsEqual = [](auto const& a1,
406 auto const& a2)
407 {
408 return a1.target_type().name() ==
409 a2.target_type().name();
410 };
411 if (std::equal(eActions.begin(),
412 eActions.end(),
413 std::get<eventActionsPos>(seEventData).begin(),
414 actsEqual))
415 {
416 return it;
417 }
418 }
419 }
420
421 return _signalEvents.end();
Matthew Barthf6b76d82017-08-04 12:58:02 -0500422}
423
Matthew Barthbfb1a562017-10-05 17:03:40 -0500424std::vector<TimerEvent>::iterator Zone::findTimer(
425 const Group& eventGroup,
426 const std::vector<Action>& eventActions)
427{
428 for (auto it = _timerEvents.begin(); it != _timerEvents.end(); ++it)
429 {
William A. Kennington III0420a932018-10-30 19:53:16 -0700430 const auto& teEventData = *std::get<timerEventDataPos>(*it);
Matthew Barthbfb1a562017-10-05 17:03:40 -0500431 if (std::get<eventActionsPos>(teEventData).size() ==
432 eventActions.size())
433 {
434 // TODO openbmc/openbmc#2328 - Use the action function target
435 // for comparison
436 auto actsEqual = [](auto const& a1,
437 auto const& a2)
438 {
439 return a1.target_type().name() ==
440 a2.target_type().name();
441 };
442 if (std::get<eventGroupPos>(teEventData) == eventGroup &&
443 std::equal(eventActions.begin(),
444 eventActions.end(),
445 std::get<eventActionsPos>(teEventData).begin(),
446 actsEqual))
447 {
448 return it;
449 }
450 }
451 }
452
453 return _timerEvents.end();
454}
455
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700456void Zone::addTimer(const Group& group,
457 const std::vector<Action>& actions,
458 const TimerConf& tConf)
459{
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700460 auto eventData = std::make_unique<EventData>(
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700461 group,
462 "",
463 nullptr,
464 actions
465 );
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700466 Timer timer(
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700467 _eventLoop,
William A. Kennington IIIc0c5f072018-10-30 19:11:01 -0700468 std::bind(&Zone::timerExpired,
469 this,
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700470 std::cref(std::get<Group>(*eventData)),
471 std::cref(std::get<std::vector<Action>>(*eventData))));
472 if (std::get<TimerType>(tConf) == TimerType::repeating)
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700473 {
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700474 timer.restart(std::get<intervalPos>(tConf));
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700475 }
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700476 else if (std::get<TimerType>(tConf) == TimerType::oneshot)
477 {
478 timer.restartOnce(std::get<intervalPos>(tConf));
479 }
480 else
481 {
482 throw std::invalid_argument("Invalid Timer Type");
483 }
484 _timerEvents.emplace_back(std::move(eventData), std::move(timer));
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700485}
486
William A. Kennington IIIc0c5f072018-10-30 19:11:01 -0700487void Zone::timerExpired(const Group& eventGroup,
488 const std::vector<Action>& eventActions)
Matthew Barth90149802017-08-15 10:51:37 -0500489{
Matthew Barthf9201ab2017-09-11 16:07:58 -0500490 // Perform the actions
491 std::for_each(eventActions.begin(),
492 eventActions.end(),
493 [this, &eventGroup](auto const& action)
494 {
495 action(*this, eventGroup);
496 });
Matthew Barth90149802017-08-15 10:51:37 -0500497}
498
Matthew Barth38a93a82017-05-11 14:12:27 -0500499void Zone::handleEvent(sdbusplus::message::message& msg,
Matthew Barth34f1bda2017-05-31 13:45:36 -0500500 const EventData* eventData)
Matthew Barth38a93a82017-05-11 14:12:27 -0500501{
502 // Handle the callback
Matthew Barth34f1bda2017-05-31 13:45:36 -0500503 std::get<eventHandlerPos>(*eventData)(_bus, msg, *this);
Matthew Barthf9201ab2017-09-11 16:07:58 -0500504 // Perform the actions
505 std::for_each(
506 std::get<eventActionsPos>(*eventData).begin(),
507 std::get<eventActionsPos>(*eventData).end(),
508 [this, &eventData](auto const& action)
509 {
510 action(*this,
511 std::get<eventGroupPos>(*eventData));
512 });
Matthew Barth38a93a82017-05-11 14:12:27 -0500513}
514
Matthew Bartha603ed02018-01-19 16:56:26 -0600515const std::string& Zone::getService(const std::string& path,
516 const std::string& intf)
517{
518 // Retrieve service from cache
519 auto srvIter = _servTree.find(path);
520 if (srvIter != _servTree.end())
521 {
522 for (auto& serv : srvIter->second)
523 {
524 auto it = std::find_if(
525 serv.second.begin(),
526 serv.second.end(),
527 [&intf](auto const& interface)
528 {
529 return intf == interface;
530 });
531 if (it != std::end(serv.second))
532 {
533 // Service found
534 return serv.first;
535 }
536 }
537 // Interface not found in cache, add and return
538 return addServices(path, intf, 0);
539 }
540 else
541 {
542 // Path not found in cache, add and return
543 return addServices(path, intf, 0);
544 }
545}
546
547const std::string& Zone::addServices(const std::string& path,
548 const std::string& intf,
549 int32_t depth)
550{
551 static const std::string empty = "";
552 auto it = _servTree.end();
553
554 // Get all subtree objects for the given interface
555 auto objects = util::SDBusPlus::getSubTree(_bus, "/", intf, depth);
556 // Add what's returned to the cache of path->services
557 for (auto& pIter : objects)
558 {
559 auto pathIter = _servTree.find(pIter.first);
560 if (pathIter != _servTree.end())
561 {
562 // Path found in cache
563 for (auto& sIter : pIter.second)
564 {
565 auto servIter = pathIter->second.find(sIter.first);
566 if (servIter != pathIter->second.end())
567 {
568 // Service found in cache
569 for (auto& iIter : sIter.second)
570 {
Matthew Barthe8b340b2018-11-09 10:05:34 -0600571 if (std::find(servIter->second.begin(),
572 servIter->second.end(),
573 iIter) == servIter->second.end())
574 {
575 // Add interface to cache
576 servIter->second.emplace_back(iIter);
577 }
Matthew Bartha603ed02018-01-19 16:56:26 -0600578 }
579 }
580 else
581 {
582 // Service not found in cache
583 pathIter->second.insert(sIter);
584 }
585 }
586 }
587 else
588 {
589 _servTree.insert(pIter);
590 }
591 // When the paths match, since a single interface constraint is given,
592 // that is the service to return
593 if (path == pIter.first)
594 {
595 it = _servTree.find(pIter.first);
596 }
597 }
598
599 if (it != _servTree.end())
600 {
601 return it->second.begin()->first;
602 }
603
604 return empty;
605}
606
Matthew Barth6faf8942019-01-22 09:26:09 -0600607std::string Zone::current(std::string value)
608{
609 auto current = value;
610 if (current != ThermalObject::current())
611 {
612 current = ThermalObject::current(value);
613 saveCurrentMode();
614 // TODO Trigger event(s) for mode property change
615 }
616 return current;
617}
618
Matthew Barthcc8912e2019-01-21 11:35:27 -0600619void Zone::saveCurrentMode()
620{
621 fs::path path{CONTROL_PERSIST_ROOT_PATH};
622 // Append zone and property description
623 path /= std::to_string(_zoneNum);
624 path /= "CurrentMode";
625 std::ofstream ofs(path.c_str(), std::ios::binary);
626 cereal::JSONOutputArchive oArch(ofs);
627 oArch(ThermalObject::current());
628}
629
Matthew Barth9e4db252019-01-21 13:08:02 -0600630void Zone::restoreCurrentMode()
631{
632 std::string current = "Default";
633 fs::path path{CONTROL_PERSIST_ROOT_PATH};
634 path /= std::to_string(_zoneNum);
635 path /= "CurrentMode";
636 fs::create_directories(path.parent_path());
637
638 try
639 {
640 if (fs::exists(path))
641 {
642 std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary);
643 cereal::JSONInputArchive iArch(ifs);
644 iArch(current);
645 }
646 }
647 catch (std::exception& e)
648 {
649 log<level::ERR>(e.what());
650 fs::remove(path);
651 current = "Default";
652 }
653
654 this->current(current);
655}
656
Matt Spinler7f88fe62017-04-10 14:39:02 -0500657}
658}
659}