blob: 293945c932896a0e10d775167ebff89c76e9c41e [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>
Dinesh Chinari618027a2017-06-26 23:26:50 -050018#include <phosphor-logging/log.hpp>
Matthew Barthdf3e8d62017-05-31 11:07:24 -050019#include <phosphor-logging/elog.hpp>
Dinesh Chinari618027a2017-06-26 23:26:50 -050020#include <phosphor-logging/elog-errors.hpp>
William A. Kennington III8fd879f2018-10-30 19:49:29 -070021#include <stdexcept>
Dinesh Chinari618027a2017-06-26 23:26:50 -050022#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinler7f88fe62017-04-10 14:39:02 -050023#include "zone.hpp"
Matthew Barthdf3e8d62017-05-31 11:07:24 -050024#include "utility.hpp"
Matthew Barthd953bb22017-08-22 10:45:28 -050025#include "sdbusplus.hpp"
Matt Spinler7f88fe62017-04-10 14:39:02 -050026
27namespace phosphor
28{
29namespace fan
30{
31namespace control
32{
33
Matthew Barth8600d9a2017-06-23 14:38:05 -050034using namespace std::chrono;
Matthew Barth90149802017-08-15 10:51:37 -050035using namespace phosphor::fan;
Matthew Barthdf3e8d62017-05-31 11:07:24 -050036using namespace phosphor::logging;
Dinesh Chinari618027a2017-06-26 23:26:50 -050037using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
38 Error::InternalFailure;
Matt Spinler7f88fe62017-04-10 14:39:02 -050039
Matthew Barth14184132017-05-19 14:37:30 -050040Zone::Zone(Mode mode,
41 sdbusplus::bus::bus& bus,
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070042 const sdeventplus::Event& event,
Matt Spinler7f88fe62017-04-10 14:39:02 -050043 const ZoneDefinition& def) :
44 _bus(bus),
45 _fullSpeed(std::get<fullSpeedPos>(def)),
Matthew Barth1de66622017-06-12 13:13:02 -050046 _zoneNum(std::get<zoneNumPos>(def)),
Matthew Barthe0ca13e2017-06-13 16:29:09 -050047 _defFloorSpeed(std::get<floorSpeedPos>(def)),
Matthew Barth8600d9a2017-06-23 14:38:05 -050048 _defCeilingSpeed(std::get<fullSpeedPos>(def)),
Matthew Bartha9561842017-06-29 11:43:45 -050049 _incDelay(std::get<incDelayPos>(def)),
50 _decInterval(std::get<decIntervalPos>(def)),
William A. Kennington III22c36ab2018-10-30 19:50:57 -070051 _incTimer(event, std::bind(&Zone::incTimerExpired, this)),
52 _decTimer(event, std::bind(&Zone::decTimerExpired, this)),
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070053 _eventLoop(event)
Matt Spinler7f88fe62017-04-10 14:39:02 -050054{
55 auto& fanDefs = std::get<fanListPos>(def);
56
57 for (auto& def : fanDefs)
58 {
59 _fans.emplace_back(std::make_unique<Fan>(bus, def));
60 }
Matthew Barth38a93a82017-05-11 14:12:27 -050061
Matthew Barth14184132017-05-19 14:37:30 -050062 // Do not enable set speed events when in init mode
63 if (mode != Mode::init)
Matthew Barth17d1fe22017-05-11 15:00:36 -050064 {
Matthew Barth2b3db612017-10-25 10:56:51 -050065 // Update target speed to current zone target speed
66 if (!_fans.empty())
67 {
68 _targetSpeed = _fans.front()->getTargetSpeed();
69 }
Matthew Barthccc77702017-07-28 13:43:04 -050070 // Setup signal trigger for set speed events
71 for (auto& event : std::get<setSpeedEventsPos>(def))
72 {
73 initEvent(event);
74 }
Matthew Barth8600d9a2017-06-23 14:38:05 -050075 // Start timer for fan speed decreases
William A. Kennington III8fd879f2018-10-30 19:49:29 -070076 _decTimer.restart(_decInterval);
Matthew Barth38a93a82017-05-11 14:12:27 -050077 }
Matt Spinler7f88fe62017-04-10 14:39:02 -050078}
79
Matt Spinler7f88fe62017-04-10 14:39:02 -050080void Zone::setSpeed(uint64_t speed)
81{
Matthew Barth60b00762017-08-15 13:39:06 -050082 if (_isActive)
Matt Spinler7f88fe62017-04-10 14:39:02 -050083 {
Matthew Barth60b00762017-08-15 13:39:06 -050084 _targetSpeed = speed;
85 for (auto& fan : _fans)
86 {
87 fan->setSpeed(_targetSpeed);
88 }
89 }
90}
91
92void Zone::setFullSpeed()
93{
94 if (_fullSpeed != 0)
95 {
96 _targetSpeed = _fullSpeed;
97 for (auto& fan : _fans)
98 {
99 fan->setSpeed(_targetSpeed);
100 }
Matt Spinler7f88fe62017-04-10 14:39:02 -0500101 }
102}
103
Matthew Barth861d77c2017-05-22 14:18:25 -0500104void Zone::setActiveAllow(const Group* group, bool isActiveAllow)
105{
Matthew Barth60b00762017-08-15 13:39:06 -0500106 _active[*(group)] = isActiveAllow;
Matthew Barth861d77c2017-05-22 14:18:25 -0500107 if (!isActiveAllow)
108 {
109 _isActive = false;
110 }
111 else
112 {
113 // Check all entries are set to allow control active
114 auto actPred = [](auto const& entry) {return entry.second;};
115 _isActive = std::all_of(_active.begin(),
116 _active.end(),
117 actPred);
118 }
119}
120
Matthew Barth55dea642017-11-06 13:34:32 -0600121void Zone::removeService(const Group* group,
122 const std::string& name)
123{
124 try
125 {
126 auto& sNames = _services.at(*group);
127 auto it = std::find_if(
128 sNames.begin(),
129 sNames.end(),
130 [&name](auto const& entry)
131 {
132 return name == std::get<namePos>(entry);
133 }
134 );
135 if (it != std::end(sNames))
136 {
137 // Remove service name from group
138 sNames.erase(it);
139 }
140 }
141 catch (const std::out_of_range& oore)
142 {
143 // No services for group found
144 }
145}
146
Matthew Barthe59fdf72017-09-27 09:33:42 -0500147void Zone::setServiceOwner(const Group* group,
148 const std::string& name,
149 const bool hasOwner)
150{
151 try
152 {
153 auto& sNames = _services.at(*group);
154 auto it = std::find_if(
155 sNames.begin(),
156 sNames.end(),
157 [&name](auto const& entry)
158 {
159 return name == std::get<namePos>(entry);
160 }
161 );
162 if (it != std::end(sNames))
163 {
164 std::get<hasOwnerPos>(*it) = hasOwner;
165 }
166 else
167 {
168 _services[*group].emplace_back(name, hasOwner);
169 }
170 }
171 catch (const std::out_of_range& oore)
172 {
173 _services[*group].emplace_back(name, hasOwner);
174 }
175}
176
Matthew Barth480787c2017-11-06 11:00:00 -0600177void Zone::setServices(const Group* group)
178{
Matthew Barth55dea642017-11-06 13:34:32 -0600179 // Remove the empty service name if exists
180 removeService(group, "");
Matthew Barth480787c2017-11-06 11:00:00 -0600181 for (auto it = group->begin(); it != group->end(); ++it)
182 {
183 std::string name;
184 bool hasOwner = false;
185 try
186 {
Matthew Barthc72b8912018-01-19 17:28:18 -0600187 name = getService(it->first,
188 std::get<intfPos>(it->second));
Matthew Barth480787c2017-11-06 11:00:00 -0600189 hasOwner = util::SDBusPlus::callMethodAndRead<bool>(
190 _bus,
191 "org.freedesktop.DBus",
192 "/org/freedesktop/DBus",
193 "org.freedesktop.DBus",
194 "NameHasOwner",
195 name);
196 }
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500197 catch (const util::DBusMethodError& e)
Matthew Barth480787c2017-11-06 11:00:00 -0600198 {
199 // Failed to get service name owner state
200 hasOwner = false;
201 }
202 setServiceOwner(group, name, hasOwner);
203 }
204}
205
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500206void Zone::setFloor(uint64_t speed)
207{
Matthew Barth98726c42017-10-17 10:35:20 -0500208 // Check all entries are set to allow floor to be set
209 auto pred = [](auto const& entry) {return entry.second;};
210 auto setFloor = std::all_of(_floorChange.begin(),
211 _floorChange.end(),
212 pred);
213 if (setFloor)
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500214 {
Matthew Barth98726c42017-10-17 10:35:20 -0500215 _floorSpeed = speed;
216 // Floor speed above target, update target to floor speed
217 if (_targetSpeed < _floorSpeed)
218 {
219 requestSpeedIncrease(_floorSpeed - _targetSpeed);
220 }
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500221 }
222}
223
Matthew Barth240397b2017-06-22 11:23:30 -0500224void Zone::requestSpeedIncrease(uint64_t targetDelta)
225{
226 // Only increase speed when delta is higher than
227 // the current increase delta for the zone and currently under ceiling
228 if (targetDelta > _incSpeedDelta &&
229 _targetSpeed < _ceilingSpeed)
230 {
Matthew Barth4e728542017-09-14 16:47:55 -0500231 auto requestTarget = getRequestSpeedBase();
Matthew Barth60b00762017-08-15 13:39:06 -0500232 requestTarget = (targetDelta - _incSpeedDelta) + requestTarget;
Matthew Barth240397b2017-06-22 11:23:30 -0500233 _incSpeedDelta = targetDelta;
Matthew Barth240397b2017-06-22 11:23:30 -0500234 // Target speed can not go above a defined ceiling speed
Matthew Barth60b00762017-08-15 13:39:06 -0500235 if (requestTarget > _ceilingSpeed)
Matthew Barth240397b2017-06-22 11:23:30 -0500236 {
Matthew Barth60b00762017-08-15 13:39:06 -0500237 requestTarget = _ceilingSpeed;
Matthew Barth240397b2017-06-22 11:23:30 -0500238 }
Matthew Barth60b00762017-08-15 13:39:06 -0500239 setSpeed(requestTarget);
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700240 // Retart timer countdown for fan speed increase
241 _incTimer.restartOnce(_incDelay);
Matthew Barth240397b2017-06-22 11:23:30 -0500242 }
Matthew Barth1ee48f22017-06-27 15:14:48 -0500243}
244
245void Zone::incTimerExpired()
246{
247 // Clear increase delta when timer expires allowing additional speed
248 // increase requests or speed decreases to occur
Matthew Barth240397b2017-06-22 11:23:30 -0500249 _incSpeedDelta = 0;
250}
251
Matthew Barth0ce99d82017-06-22 15:07:29 -0500252void Zone::requestSpeedDecrease(uint64_t targetDelta)
253{
254 // Only decrease the lowest target delta requested
255 if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta)
256 {
257 _decSpeedDelta = targetDelta;
258 }
Matthew Barth8600d9a2017-06-23 14:38:05 -0500259}
Matthew Barth0ce99d82017-06-22 15:07:29 -0500260
Matthew Barth8600d9a2017-06-23 14:38:05 -0500261void Zone::decTimerExpired()
262{
Matthew Barthe4338cd2017-12-14 11:14:30 -0600263 // Check all entries are set to allow a decrease
264 auto pred = [](auto const& entry) {return entry.second;};
265 auto decAllowed = std::all_of(_decAllowed.begin(),
266 _decAllowed.end(),
267 pred);
268
269 // Only decrease speeds when allowed,
270 // where no requested increases exist and
271 // the increase timer is not running
272 // (i.e. not in the middle of increasing)
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700273 if (decAllowed && _incSpeedDelta == 0 && !_incTimer.isEnabled())
Matthew Barth0ce99d82017-06-22 15:07:29 -0500274 {
Matthew Barth4e728542017-09-14 16:47:55 -0500275 auto requestTarget = getRequestSpeedBase();
Matthew Barthc63973a2017-12-08 15:31:35 -0600276 // Request target speed should not start above ceiling
277 if (requestTarget > _ceilingSpeed)
278 {
279 requestTarget = _ceilingSpeed;
280 }
Matthew Barth0ce99d82017-06-22 15:07:29 -0500281 // Target speed can not go below the defined floor speed
Matthew Barth60b00762017-08-15 13:39:06 -0500282 if ((requestTarget < _decSpeedDelta) ||
283 (requestTarget - _decSpeedDelta < _floorSpeed))
Matthew Barth0ce99d82017-06-22 15:07:29 -0500284 {
Matthew Barth60b00762017-08-15 13:39:06 -0500285 requestTarget = _floorSpeed;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500286 }
287 else
288 {
Matthew Barth60b00762017-08-15 13:39:06 -0500289 requestTarget = requestTarget - _decSpeedDelta;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500290 }
Matthew Barth60b00762017-08-15 13:39:06 -0500291 setSpeed(requestTarget);
Matthew Barth0ce99d82017-06-22 15:07:29 -0500292 }
293 // Clear decrease delta when timer expires
294 _decSpeedDelta = 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500295 // Decrease timer is restarted since its repeating
Matthew Barth0ce99d82017-06-22 15:07:29 -0500296}
297
Matthew Barthccc77702017-07-28 13:43:04 -0500298void Zone::initEvent(const SetSpeedEvent& event)
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500299{
Matthew Barth336f18a2017-09-26 09:15:56 -0500300 sdbusplus::message::message nullMsg{nullptr};
301
Matthew Barth67967f92017-09-22 12:43:57 -0500302 for (auto& sig : std::get<signalsPos>(event))
Matthew Barthccc77702017-07-28 13:43:04 -0500303 {
Matthew Barth336f18a2017-09-26 09:15:56 -0500304 // Initialize the event signal using handler
305 std::get<sigHandlerPos>(sig)(_bus, nullMsg, *this);
306 // Setup signal matches of the property for event
Matthew Barthf6b76d82017-08-04 12:58:02 -0500307 std::unique_ptr<EventData> eventData =
308 std::make_unique<EventData>(
Matthew Barthf6b76d82017-08-04 12:58:02 -0500309 std::get<groupPos>(event),
Matthew Barth336f18a2017-09-26 09:15:56 -0500310 std::get<sigMatchPos>(sig),
311 std::get<sigHandlerPos>(sig),
Matthew Barthf9201ab2017-09-11 16:07:58 -0500312 std::get<actionsPos>(event)
Matthew Barthf6b76d82017-08-04 12:58:02 -0500313 );
Matthew Barth336f18a2017-09-26 09:15:56 -0500314 std::unique_ptr<sdbusplus::server::match::match> match = nullptr;
315 if (!std::get<sigMatchPos>(sig).empty())
316 {
317 match = std::make_unique<sdbusplus::server::match::match>(
318 _bus,
319 std::get<sigMatchPos>(sig).c_str(),
320 std::bind(std::mem_fn(&Zone::handleEvent),
321 this,
322 std::placeholders::_1,
323 eventData.get())
324 );
325 }
Matthew Barthf6b76d82017-08-04 12:58:02 -0500326 _signalEvents.emplace_back(std::move(eventData), std::move(match));
Matthew Barthccc77702017-07-28 13:43:04 -0500327 }
Matthew Barth90149802017-08-15 10:51:37 -0500328 // Attach a timer to run the action of an event
William A. Kennington III122b8432018-10-30 18:39:21 -0700329 auto timerConf = std::get<timerConfPos>(event);
330 if (std::get<intervalPos>(timerConf) != seconds(0))
Matthew Barth90149802017-08-15 10:51:37 -0500331 {
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700332 addTimer(std::get<groupPos>(event),
333 std::get<actionsPos>(event),
334 timerConf);
Matthew Barth90149802017-08-15 10:51:37 -0500335 }
Matthew Barthf9201ab2017-09-11 16:07:58 -0500336 // Run action functions for initial event state
337 std::for_each(
338 std::get<actionsPos>(event).begin(),
339 std::get<actionsPos>(event).end(),
340 [this, &event](auto const& action)
341 {
342 action(*this,
343 std::get<groupPos>(event));
344 });
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500345}
346
Matthew Barthf6b76d82017-08-04 12:58:02 -0500347void Zone::removeEvent(const SetSpeedEvent& event)
348{
Matthew Barth33bfe762018-11-05 11:13:25 -0600349 // Remove signals of the event
350 for (auto& sig : std::get<signalsPos>(event))
Matthew Barthf6b76d82017-08-04 12:58:02 -0500351 {
Matthew Barth33bfe762018-11-05 11:13:25 -0600352 auto it = findSignal(sig,
353 std::get<groupPos>(event),
354 std::get<actionsPos>(event));
355 if (it != std::end(getSignalEvents()))
Matthew Barth336f18a2017-09-26 09:15:56 -0500356 {
Matthew Barth33bfe762018-11-05 11:13:25 -0600357 removeSignal(it);
Matthew Barth336f18a2017-09-26 09:15:56 -0500358 }
Matthew Barthf6b76d82017-08-04 12:58:02 -0500359 }
Matthew Barth33bfe762018-11-05 11:13:25 -0600360 // Remove timers of the event
Matthew Barth8ce4b5f2018-11-06 13:54:46 -0600361 if (std::get<intervalPos>(std::get<timerConfPos>(event)) != seconds(0))
Matthew Barth33bfe762018-11-05 11:13:25 -0600362 {
363 auto it = findTimer(std::get<groupPos>(event),
364 std::get<actionsPos>(event));
365 if (it != std::end(getTimerEvents()))
366 {
367 removeTimer(it);
368 }
369 }
370}
371
372std::vector<SignalEvent>::iterator Zone::findSignal(
373 const Signal& signal,
374 const Group& eGroup,
375 const std::vector<Action>& eActions)
376{
377 // Find the signal in the event to be removed
378 for (auto it = _signalEvents.begin(); it != _signalEvents.end(); ++ it)
379 {
380 const auto& seEventData = *std::get<signalEventDataPos>(*it);
381 if (eGroup == std::get<eventGroupPos>(seEventData) &&
382 std::get<sigMatchPos>(signal) ==
383 std::get<eventMatchPos>(seEventData) &&
384 std::get<sigHandlerPos>(signal).target_type().name() ==
385 std::get<eventHandlerPos>(seEventData).target_type().name() &&
386 eActions.size() == std::get<eventActionsPos>(seEventData).size())
387 {
388 // TODO openbmc/openbmc#2328 - Use the function target
389 // for comparison
390 auto actsEqual = [](auto const& a1,
391 auto const& a2)
392 {
393 return a1.target_type().name() ==
394 a2.target_type().name();
395 };
396 if (std::equal(eActions.begin(),
397 eActions.end(),
398 std::get<eventActionsPos>(seEventData).begin(),
399 actsEqual))
400 {
401 return it;
402 }
403 }
404 }
405
406 return _signalEvents.end();
Matthew Barthf6b76d82017-08-04 12:58:02 -0500407}
408
Matthew Barthbfb1a562017-10-05 17:03:40 -0500409std::vector<TimerEvent>::iterator Zone::findTimer(
410 const Group& eventGroup,
411 const std::vector<Action>& eventActions)
412{
413 for (auto it = _timerEvents.begin(); it != _timerEvents.end(); ++it)
414 {
William A. Kennington III0420a932018-10-30 19:53:16 -0700415 const auto& teEventData = *std::get<timerEventDataPos>(*it);
Matthew Barthbfb1a562017-10-05 17:03:40 -0500416 if (std::get<eventActionsPos>(teEventData).size() ==
417 eventActions.size())
418 {
419 // TODO openbmc/openbmc#2328 - Use the action function target
420 // for comparison
421 auto actsEqual = [](auto const& a1,
422 auto const& a2)
423 {
424 return a1.target_type().name() ==
425 a2.target_type().name();
426 };
427 if (std::get<eventGroupPos>(teEventData) == eventGroup &&
428 std::equal(eventActions.begin(),
429 eventActions.end(),
430 std::get<eventActionsPos>(teEventData).begin(),
431 actsEqual))
432 {
433 return it;
434 }
435 }
436 }
437
438 return _timerEvents.end();
439}
440
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700441void Zone::addTimer(const Group& group,
442 const std::vector<Action>& actions,
443 const TimerConf& tConf)
444{
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700445 auto eventData = std::make_unique<EventData>(
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700446 group,
447 "",
448 nullptr,
449 actions
450 );
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700451 Timer timer(
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700452 _eventLoop,
William A. Kennington IIIc0c5f072018-10-30 19:11:01 -0700453 std::bind(&Zone::timerExpired,
454 this,
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700455 std::cref(std::get<Group>(*eventData)),
456 std::cref(std::get<std::vector<Action>>(*eventData))));
457 if (std::get<TimerType>(tConf) == TimerType::repeating)
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700458 {
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700459 timer.restart(std::get<intervalPos>(tConf));
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700460 }
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700461 else if (std::get<TimerType>(tConf) == TimerType::oneshot)
462 {
463 timer.restartOnce(std::get<intervalPos>(tConf));
464 }
465 else
466 {
467 throw std::invalid_argument("Invalid Timer Type");
468 }
469 _timerEvents.emplace_back(std::move(eventData), std::move(timer));
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700470}
471
William A. Kennington IIIc0c5f072018-10-30 19:11:01 -0700472void Zone::timerExpired(const Group& eventGroup,
473 const std::vector<Action>& eventActions)
Matthew Barth90149802017-08-15 10:51:37 -0500474{
Matthew Barthf9201ab2017-09-11 16:07:58 -0500475 // Perform the actions
476 std::for_each(eventActions.begin(),
477 eventActions.end(),
478 [this, &eventGroup](auto const& action)
479 {
480 action(*this, eventGroup);
481 });
Matthew Barth90149802017-08-15 10:51:37 -0500482}
483
Matthew Barth38a93a82017-05-11 14:12:27 -0500484void Zone::handleEvent(sdbusplus::message::message& msg,
Matthew Barth34f1bda2017-05-31 13:45:36 -0500485 const EventData* eventData)
Matthew Barth38a93a82017-05-11 14:12:27 -0500486{
487 // Handle the callback
Matthew Barth34f1bda2017-05-31 13:45:36 -0500488 std::get<eventHandlerPos>(*eventData)(_bus, msg, *this);
Matthew Barthf9201ab2017-09-11 16:07:58 -0500489 // Perform the actions
490 std::for_each(
491 std::get<eventActionsPos>(*eventData).begin(),
492 std::get<eventActionsPos>(*eventData).end(),
493 [this, &eventData](auto const& action)
494 {
495 action(*this,
496 std::get<eventGroupPos>(*eventData));
497 });
Matthew Barth38a93a82017-05-11 14:12:27 -0500498}
499
Matthew Bartha603ed02018-01-19 16:56:26 -0600500const std::string& Zone::getService(const std::string& path,
501 const std::string& intf)
502{
503 // Retrieve service from cache
504 auto srvIter = _servTree.find(path);
505 if (srvIter != _servTree.end())
506 {
507 for (auto& serv : srvIter->second)
508 {
509 auto it = std::find_if(
510 serv.second.begin(),
511 serv.second.end(),
512 [&intf](auto const& interface)
513 {
514 return intf == interface;
515 });
516 if (it != std::end(serv.second))
517 {
518 // Service found
519 return serv.first;
520 }
521 }
522 // Interface not found in cache, add and return
523 return addServices(path, intf, 0);
524 }
525 else
526 {
527 // Path not found in cache, add and return
528 return addServices(path, intf, 0);
529 }
530}
531
532const std::string& Zone::addServices(const std::string& path,
533 const std::string& intf,
534 int32_t depth)
535{
536 static const std::string empty = "";
537 auto it = _servTree.end();
538
539 // Get all subtree objects for the given interface
540 auto objects = util::SDBusPlus::getSubTree(_bus, "/", intf, depth);
541 // Add what's returned to the cache of path->services
542 for (auto& pIter : objects)
543 {
544 auto pathIter = _servTree.find(pIter.first);
545 if (pathIter != _servTree.end())
546 {
547 // Path found in cache
548 for (auto& sIter : pIter.second)
549 {
550 auto servIter = pathIter->second.find(sIter.first);
551 if (servIter != pathIter->second.end())
552 {
553 // Service found in cache
554 for (auto& iIter : sIter.second)
555 {
Matthew Barthe8b340b2018-11-09 10:05:34 -0600556 if (std::find(servIter->second.begin(),
557 servIter->second.end(),
558 iIter) == servIter->second.end())
559 {
560 // Add interface to cache
561 servIter->second.emplace_back(iIter);
562 }
Matthew Bartha603ed02018-01-19 16:56:26 -0600563 }
564 }
565 else
566 {
567 // Service not found in cache
568 pathIter->second.insert(sIter);
569 }
570 }
571 }
572 else
573 {
574 _servTree.insert(pIter);
575 }
576 // When the paths match, since a single interface constraint is given,
577 // that is the service to return
578 if (path == pIter.first)
579 {
580 it = _servTree.find(pIter.first);
581 }
582 }
583
584 if (it != _servTree.end())
585 {
586 return it->second.begin()->first;
587 }
588
589 return empty;
590}
591
Matt Spinler7f88fe62017-04-10 14:39:02 -0500592}
593}
594}