blob: d37701e187403c9b1c185ed45ff07d549c446e79 [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>
21#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinler7f88fe62017-04-10 14:39:02 -050022#include "zone.hpp"
Matthew Barthdf3e8d62017-05-31 11:07:24 -050023#include "utility.hpp"
Matthew Barthd953bb22017-08-22 10:45:28 -050024#include "sdbusplus.hpp"
Matt Spinler7f88fe62017-04-10 14:39:02 -050025
26namespace phosphor
27{
28namespace fan
29{
30namespace control
31{
32
Matthew Barth8600d9a2017-06-23 14:38:05 -050033using namespace std::chrono;
Matthew Barth90149802017-08-15 10:51:37 -050034using namespace phosphor::fan;
Matthew Barthdf3e8d62017-05-31 11:07:24 -050035using namespace phosphor::logging;
Dinesh Chinari618027a2017-06-26 23:26:50 -050036using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
37 Error::InternalFailure;
Matt Spinler7f88fe62017-04-10 14:39:02 -050038
Matthew Barth14184132017-05-19 14:37:30 -050039Zone::Zone(Mode mode,
40 sdbusplus::bus::bus& bus,
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070041 const sdeventplus::Event& event,
Matt Spinler7f88fe62017-04-10 14:39:02 -050042 const ZoneDefinition& def) :
43 _bus(bus),
44 _fullSpeed(std::get<fullSpeedPos>(def)),
Matthew Barth1de66622017-06-12 13:13:02 -050045 _zoneNum(std::get<zoneNumPos>(def)),
Matthew Barthe0ca13e2017-06-13 16:29:09 -050046 _defFloorSpeed(std::get<floorSpeedPos>(def)),
Matthew Barth8600d9a2017-06-23 14:38:05 -050047 _defCeilingSpeed(std::get<fullSpeedPos>(def)),
Matthew Bartha9561842017-06-29 11:43:45 -050048 _incDelay(std::get<incDelayPos>(def)),
49 _decInterval(std::get<decIntervalPos>(def)),
William A. Kennington III22c36ab2018-10-30 19:50:57 -070050 _incTimer(event, std::bind(&Zone::incTimerExpired, this)),
51 _decTimer(event, std::bind(&Zone::decTimerExpired, this)),
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070052 _eventLoop(event)
Matt Spinler7f88fe62017-04-10 14:39:02 -050053{
54 auto& fanDefs = std::get<fanListPos>(def);
55
56 for (auto& def : fanDefs)
57 {
58 _fans.emplace_back(std::make_unique<Fan>(bus, def));
59 }
Matthew Barth38a93a82017-05-11 14:12:27 -050060
Matthew Barth14184132017-05-19 14:37:30 -050061 // Do not enable set speed events when in init mode
62 if (mode != Mode::init)
Matthew Barth17d1fe22017-05-11 15:00:36 -050063 {
Matthew Barth2b3db612017-10-25 10:56:51 -050064 // Update target speed to current zone target speed
65 if (!_fans.empty())
66 {
67 _targetSpeed = _fans.front()->getTargetSpeed();
68 }
Matthew Barthccc77702017-07-28 13:43:04 -050069 // Setup signal trigger for set speed events
70 for (auto& event : std::get<setSpeedEventsPos>(def))
71 {
72 initEvent(event);
73 }
Matthew Barth8600d9a2017-06-23 14:38:05 -050074 // Start timer for fan speed decreases
Matthew Bartha9561842017-06-29 11:43:45 -050075 if (!_decTimer.running() && _decInterval != seconds::zero())
Matthew Barth8600d9a2017-06-23 14:38:05 -050076 {
William A. Kennington III0ce353e2018-10-30 18:30:36 -070077 _decTimer.start(_decInterval, TimerType::repeating);
Matthew Barth8600d9a2017-06-23 14:38:05 -050078 }
Matthew Barth38a93a82017-05-11 14:12:27 -050079 }
Matt Spinler7f88fe62017-04-10 14:39:02 -050080}
81
Matt Spinler7f88fe62017-04-10 14:39:02 -050082void Zone::setSpeed(uint64_t speed)
83{
Matthew Barth60b00762017-08-15 13:39:06 -050084 if (_isActive)
Matt Spinler7f88fe62017-04-10 14:39:02 -050085 {
Matthew Barth60b00762017-08-15 13:39:06 -050086 _targetSpeed = speed;
87 for (auto& fan : _fans)
88 {
89 fan->setSpeed(_targetSpeed);
90 }
91 }
92}
93
94void Zone::setFullSpeed()
95{
96 if (_fullSpeed != 0)
97 {
98 _targetSpeed = _fullSpeed;
99 for (auto& fan : _fans)
100 {
101 fan->setSpeed(_targetSpeed);
102 }
Matt Spinler7f88fe62017-04-10 14:39:02 -0500103 }
104}
105
Matthew Barth861d77c2017-05-22 14:18:25 -0500106void Zone::setActiveAllow(const Group* group, bool isActiveAllow)
107{
Matthew Barth60b00762017-08-15 13:39:06 -0500108 _active[*(group)] = isActiveAllow;
Matthew Barth861d77c2017-05-22 14:18:25 -0500109 if (!isActiveAllow)
110 {
111 _isActive = false;
112 }
113 else
114 {
115 // Check all entries are set to allow control active
116 auto actPred = [](auto const& entry) {return entry.second;};
117 _isActive = std::all_of(_active.begin(),
118 _active.end(),
119 actPred);
120 }
121}
122
Matthew Barth55dea642017-11-06 13:34:32 -0600123void Zone::removeService(const Group* group,
124 const std::string& name)
125{
126 try
127 {
128 auto& sNames = _services.at(*group);
129 auto it = std::find_if(
130 sNames.begin(),
131 sNames.end(),
132 [&name](auto const& entry)
133 {
134 return name == std::get<namePos>(entry);
135 }
136 );
137 if (it != std::end(sNames))
138 {
139 // Remove service name from group
140 sNames.erase(it);
141 }
142 }
143 catch (const std::out_of_range& oore)
144 {
145 // No services for group found
146 }
147}
148
Matthew Barthe59fdf72017-09-27 09:33:42 -0500149void Zone::setServiceOwner(const Group* group,
150 const std::string& name,
151 const bool hasOwner)
152{
153 try
154 {
155 auto& sNames = _services.at(*group);
156 auto it = std::find_if(
157 sNames.begin(),
158 sNames.end(),
159 [&name](auto const& entry)
160 {
161 return name == std::get<namePos>(entry);
162 }
163 );
164 if (it != std::end(sNames))
165 {
166 std::get<hasOwnerPos>(*it) = hasOwner;
167 }
168 else
169 {
170 _services[*group].emplace_back(name, hasOwner);
171 }
172 }
173 catch (const std::out_of_range& oore)
174 {
175 _services[*group].emplace_back(name, hasOwner);
176 }
177}
178
Matthew Barth480787c2017-11-06 11:00:00 -0600179void Zone::setServices(const Group* group)
180{
Matthew Barth55dea642017-11-06 13:34:32 -0600181 // Remove the empty service name if exists
182 removeService(group, "");
Matthew Barth480787c2017-11-06 11:00:00 -0600183 for (auto it = group->begin(); it != group->end(); ++it)
184 {
185 std::string name;
186 bool hasOwner = false;
187 try
188 {
Matthew Barthc72b8912018-01-19 17:28:18 -0600189 name = getService(it->first,
190 std::get<intfPos>(it->second));
Matthew Barth480787c2017-11-06 11:00:00 -0600191 hasOwner = util::SDBusPlus::callMethodAndRead<bool>(
192 _bus,
193 "org.freedesktop.DBus",
194 "/org/freedesktop/DBus",
195 "org.freedesktop.DBus",
196 "NameHasOwner",
197 name);
198 }
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500199 catch (const util::DBusMethodError& e)
Matthew Barth480787c2017-11-06 11:00:00 -0600200 {
201 // Failed to get service name owner state
202 hasOwner = false;
203 }
204 setServiceOwner(group, name, hasOwner);
205 }
206}
207
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500208void Zone::setFloor(uint64_t speed)
209{
Matthew Barth98726c42017-10-17 10:35:20 -0500210 // Check all entries are set to allow floor to be set
211 auto pred = [](auto const& entry) {return entry.second;};
212 auto setFloor = std::all_of(_floorChange.begin(),
213 _floorChange.end(),
214 pred);
215 if (setFloor)
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500216 {
Matthew Barth98726c42017-10-17 10:35:20 -0500217 _floorSpeed = speed;
218 // Floor speed above target, update target to floor speed
219 if (_targetSpeed < _floorSpeed)
220 {
221 requestSpeedIncrease(_floorSpeed - _targetSpeed);
222 }
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500223 }
224}
225
Matthew Barth240397b2017-06-22 11:23:30 -0500226void Zone::requestSpeedIncrease(uint64_t targetDelta)
227{
228 // Only increase speed when delta is higher than
229 // the current increase delta for the zone and currently under ceiling
230 if (targetDelta > _incSpeedDelta &&
231 _targetSpeed < _ceilingSpeed)
232 {
Matthew Barth4e728542017-09-14 16:47:55 -0500233 auto requestTarget = getRequestSpeedBase();
Matthew Barth60b00762017-08-15 13:39:06 -0500234 requestTarget = (targetDelta - _incSpeedDelta) + requestTarget;
Matthew Barth240397b2017-06-22 11:23:30 -0500235 _incSpeedDelta = targetDelta;
Matthew Barth240397b2017-06-22 11:23:30 -0500236 // Target speed can not go above a defined ceiling speed
Matthew Barth60b00762017-08-15 13:39:06 -0500237 if (requestTarget > _ceilingSpeed)
Matthew Barth240397b2017-06-22 11:23:30 -0500238 {
Matthew Barth60b00762017-08-15 13:39:06 -0500239 requestTarget = _ceilingSpeed;
Matthew Barth240397b2017-06-22 11:23:30 -0500240 }
Matthew Barth1ee48f22017-06-27 15:14:48 -0500241 // Cancel current timer countdown
242 if (_incTimer.running())
243 {
244 _incTimer.stop();
245 }
Matthew Barth60b00762017-08-15 13:39:06 -0500246 setSpeed(requestTarget);
Matthew Barth1ee48f22017-06-27 15:14:48 -0500247 // Start timer countdown for fan speed increase
William A. Kennington III0ce353e2018-10-30 18:30:36 -0700248 _incTimer.start(_incDelay, TimerType::oneshot);
Matthew Barth240397b2017-06-22 11:23:30 -0500249 }
Matthew Barth1ee48f22017-06-27 15:14:48 -0500250}
251
252void Zone::incTimerExpired()
253{
254 // Clear increase delta when timer expires allowing additional speed
255 // increase requests or speed decreases to occur
Matthew Barth240397b2017-06-22 11:23:30 -0500256 _incSpeedDelta = 0;
257}
258
Matthew Barth0ce99d82017-06-22 15:07:29 -0500259void Zone::requestSpeedDecrease(uint64_t targetDelta)
260{
261 // Only decrease the lowest target delta requested
262 if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta)
263 {
264 _decSpeedDelta = targetDelta;
265 }
Matthew Barth8600d9a2017-06-23 14:38:05 -0500266}
Matthew Barth0ce99d82017-06-22 15:07:29 -0500267
Matthew Barth8600d9a2017-06-23 14:38:05 -0500268void Zone::decTimerExpired()
269{
Matthew Barthe4338cd2017-12-14 11:14:30 -0600270 // Check all entries are set to allow a decrease
271 auto pred = [](auto const& entry) {return entry.second;};
272 auto decAllowed = std::all_of(_decAllowed.begin(),
273 _decAllowed.end(),
274 pred);
275
276 // Only decrease speeds when allowed,
277 // where no requested increases exist and
278 // the increase timer is not running
279 // (i.e. not in the middle of increasing)
280 if (decAllowed && _incSpeedDelta == 0 && !_incTimer.running())
Matthew Barth0ce99d82017-06-22 15:07:29 -0500281 {
Matthew Barth4e728542017-09-14 16:47:55 -0500282 auto requestTarget = getRequestSpeedBase();
Matthew Barthc63973a2017-12-08 15:31:35 -0600283 // Request target speed should not start above ceiling
284 if (requestTarget > _ceilingSpeed)
285 {
286 requestTarget = _ceilingSpeed;
287 }
Matthew Barth0ce99d82017-06-22 15:07:29 -0500288 // Target speed can not go below the defined floor speed
Matthew Barth60b00762017-08-15 13:39:06 -0500289 if ((requestTarget < _decSpeedDelta) ||
290 (requestTarget - _decSpeedDelta < _floorSpeed))
Matthew Barth0ce99d82017-06-22 15:07:29 -0500291 {
Matthew Barth60b00762017-08-15 13:39:06 -0500292 requestTarget = _floorSpeed;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500293 }
294 else
295 {
Matthew Barth60b00762017-08-15 13:39:06 -0500296 requestTarget = requestTarget - _decSpeedDelta;
Matthew Barth0ce99d82017-06-22 15:07:29 -0500297 }
Matthew Barth60b00762017-08-15 13:39:06 -0500298 setSpeed(requestTarget);
Matthew Barth0ce99d82017-06-22 15:07:29 -0500299 }
300 // Clear decrease delta when timer expires
301 _decSpeedDelta = 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500302 // Decrease timer is restarted since its repeating
Matthew Barth0ce99d82017-06-22 15:07:29 -0500303}
304
Matthew Barthccc77702017-07-28 13:43:04 -0500305void Zone::initEvent(const SetSpeedEvent& event)
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500306{
Matthew Barth336f18a2017-09-26 09:15:56 -0500307 sdbusplus::message::message nullMsg{nullptr};
308
Matthew Barth67967f92017-09-22 12:43:57 -0500309 for (auto& sig : std::get<signalsPos>(event))
Matthew Barthccc77702017-07-28 13:43:04 -0500310 {
Matthew Barth336f18a2017-09-26 09:15:56 -0500311 // Initialize the event signal using handler
312 std::get<sigHandlerPos>(sig)(_bus, nullMsg, *this);
313 // Setup signal matches of the property for event
Matthew Barthf6b76d82017-08-04 12:58:02 -0500314 std::unique_ptr<EventData> eventData =
315 std::make_unique<EventData>(
Matthew Barthf6b76d82017-08-04 12:58:02 -0500316 std::get<groupPos>(event),
Matthew Barth336f18a2017-09-26 09:15:56 -0500317 std::get<sigMatchPos>(sig),
318 std::get<sigHandlerPos>(sig),
Matthew Barthf9201ab2017-09-11 16:07:58 -0500319 std::get<actionsPos>(event)
Matthew Barthf6b76d82017-08-04 12:58:02 -0500320 );
Matthew Barth336f18a2017-09-26 09:15:56 -0500321 std::unique_ptr<sdbusplus::server::match::match> match = nullptr;
322 if (!std::get<sigMatchPos>(sig).empty())
323 {
324 match = std::make_unique<sdbusplus::server::match::match>(
325 _bus,
326 std::get<sigMatchPos>(sig).c_str(),
327 std::bind(std::mem_fn(&Zone::handleEvent),
328 this,
329 std::placeholders::_1,
330 eventData.get())
331 );
332 }
Matthew Barthf6b76d82017-08-04 12:58:02 -0500333 _signalEvents.emplace_back(std::move(eventData), std::move(match));
Matthew Barthccc77702017-07-28 13:43:04 -0500334 }
Matthew Barth90149802017-08-15 10:51:37 -0500335 // Attach a timer to run the action of an event
William A. Kennington III122b8432018-10-30 18:39:21 -0700336 auto timerConf = std::get<timerConfPos>(event);
337 if (std::get<intervalPos>(timerConf) != seconds(0))
Matthew Barth90149802017-08-15 10:51:37 -0500338 {
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700339 addTimer(std::get<groupPos>(event),
340 std::get<actionsPos>(event),
341 timerConf);
Matthew Barth90149802017-08-15 10:51:37 -0500342 }
Matthew Barthf9201ab2017-09-11 16:07:58 -0500343 // Run action functions for initial event state
344 std::for_each(
345 std::get<actionsPos>(event).begin(),
346 std::get<actionsPos>(event).end(),
347 [this, &event](auto const& action)
348 {
349 action(*this,
350 std::get<groupPos>(event));
351 });
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500352}
353
Matthew Barthf6b76d82017-08-04 12:58:02 -0500354void Zone::removeEvent(const SetSpeedEvent& event)
355{
Matthew Barth33bfe762018-11-05 11:13:25 -0600356 // Remove signals of the event
357 for (auto& sig : std::get<signalsPos>(event))
Matthew Barthf6b76d82017-08-04 12:58:02 -0500358 {
Matthew Barth33bfe762018-11-05 11:13:25 -0600359 auto it = findSignal(sig,
360 std::get<groupPos>(event),
361 std::get<actionsPos>(event));
362 if (it != std::end(getSignalEvents()))
Matthew Barth336f18a2017-09-26 09:15:56 -0500363 {
Matthew Barth33bfe762018-11-05 11:13:25 -0600364 removeSignal(it);
Matthew Barth336f18a2017-09-26 09:15:56 -0500365 }
Matthew Barthf6b76d82017-08-04 12:58:02 -0500366 }
Matthew Barth33bfe762018-11-05 11:13:25 -0600367 // Remove timers of the event
368 if (std::get<intervalPos>(std::get<timerPos>(event)) != seconds(0))
369 {
370 auto it = findTimer(std::get<groupPos>(event),
371 std::get<actionsPos>(event));
372 if (it != std::end(getTimerEvents()))
373 {
374 removeTimer(it);
375 }
376 }
377}
378
379std::vector<SignalEvent>::iterator Zone::findSignal(
380 const Signal& signal,
381 const Group& eGroup,
382 const std::vector<Action>& eActions)
383{
384 // Find the signal in the event to be removed
385 for (auto it = _signalEvents.begin(); it != _signalEvents.end(); ++ it)
386 {
387 const auto& seEventData = *std::get<signalEventDataPos>(*it);
388 if (eGroup == std::get<eventGroupPos>(seEventData) &&
389 std::get<sigMatchPos>(signal) ==
390 std::get<eventMatchPos>(seEventData) &&
391 std::get<sigHandlerPos>(signal).target_type().name() ==
392 std::get<eventHandlerPos>(seEventData).target_type().name() &&
393 eActions.size() == std::get<eventActionsPos>(seEventData).size())
394 {
395 // TODO openbmc/openbmc#2328 - Use the function target
396 // for comparison
397 auto actsEqual = [](auto const& a1,
398 auto const& a2)
399 {
400 return a1.target_type().name() ==
401 a2.target_type().name();
402 };
403 if (std::equal(eActions.begin(),
404 eActions.end(),
405 std::get<eventActionsPos>(seEventData).begin(),
406 actsEqual))
407 {
408 return it;
409 }
410 }
411 }
412
413 return _signalEvents.end();
Matthew Barthf6b76d82017-08-04 12:58:02 -0500414}
415
Matthew Barthbfb1a562017-10-05 17:03:40 -0500416std::vector<TimerEvent>::iterator Zone::findTimer(
417 const Group& eventGroup,
418 const std::vector<Action>& eventActions)
419{
420 for (auto it = _timerEvents.begin(); it != _timerEvents.end(); ++it)
421 {
422 auto teEventData = *std::get<timerEventDataPos>(*it);
423 if (std::get<eventActionsPos>(teEventData).size() ==
424 eventActions.size())
425 {
426 // TODO openbmc/openbmc#2328 - Use the action function target
427 // for comparison
428 auto actsEqual = [](auto const& a1,
429 auto const& a2)
430 {
431 return a1.target_type().name() ==
432 a2.target_type().name();
433 };
434 if (std::get<eventGroupPos>(teEventData) == eventGroup &&
435 std::equal(eventActions.begin(),
436 eventActions.end(),
437 std::get<eventActionsPos>(teEventData).begin(),
438 actsEqual))
439 {
440 return it;
441 }
442 }
443 }
444
445 return _timerEvents.end();
446}
447
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700448void Zone::addTimer(const Group& group,
449 const std::vector<Action>& actions,
450 const TimerConf& tConf)
451{
452 // Associate event data with timer
453 auto data = std::make_unique<EventData>(
454 group,
455 "",
456 nullptr,
457 actions
458 );
459 auto timer = std::make_unique<util::Timer>(
460 _eventLoop,
William A. Kennington IIIc0c5f072018-10-30 19:11:01 -0700461 std::bind(&Zone::timerExpired,
462 this,
463 std::cref(std::get<Group>(*data)),
464 std::cref(std::get<std::vector<Action>>(*data)))
William A. Kennington III94fe1a02018-10-30 19:00:27 -0700465 );
466 if (!timer->running())
467 {
468 timer->start(std::get<intervalPos>(tConf),
469 std::get<typePos>(tConf));
470 }
471 _timerEvents.emplace_back(std::move(data), std::move(timer));
472}
473
William A. Kennington IIIc0c5f072018-10-30 19:11:01 -0700474void Zone::timerExpired(const Group& eventGroup,
475 const std::vector<Action>& eventActions)
Matthew Barth90149802017-08-15 10:51:37 -0500476{
Matthew Barthf9201ab2017-09-11 16:07:58 -0500477 // Perform the actions
478 std::for_each(eventActions.begin(),
479 eventActions.end(),
480 [this, &eventGroup](auto const& action)
481 {
482 action(*this, eventGroup);
483 });
Matthew Barth90149802017-08-15 10:51:37 -0500484}
485
Matthew Barth38a93a82017-05-11 14:12:27 -0500486void Zone::handleEvent(sdbusplus::message::message& msg,
Matthew Barth34f1bda2017-05-31 13:45:36 -0500487 const EventData* eventData)
Matthew Barth38a93a82017-05-11 14:12:27 -0500488{
489 // Handle the callback
Matthew Barth34f1bda2017-05-31 13:45:36 -0500490 std::get<eventHandlerPos>(*eventData)(_bus, msg, *this);
Matthew Barthf9201ab2017-09-11 16:07:58 -0500491 // Perform the actions
492 std::for_each(
493 std::get<eventActionsPos>(*eventData).begin(),
494 std::get<eventActionsPos>(*eventData).end(),
495 [this, &eventData](auto const& action)
496 {
497 action(*this,
498 std::get<eventGroupPos>(*eventData));
499 });
Matthew Barth38a93a82017-05-11 14:12:27 -0500500}
501
Matthew Bartha603ed02018-01-19 16:56:26 -0600502const std::string& Zone::getService(const std::string& path,
503 const std::string& intf)
504{
505 // Retrieve service from cache
506 auto srvIter = _servTree.find(path);
507 if (srvIter != _servTree.end())
508 {
509 for (auto& serv : srvIter->second)
510 {
511 auto it = std::find_if(
512 serv.second.begin(),
513 serv.second.end(),
514 [&intf](auto const& interface)
515 {
516 return intf == interface;
517 });
518 if (it != std::end(serv.second))
519 {
520 // Service found
521 return serv.first;
522 }
523 }
524 // Interface not found in cache, add and return
525 return addServices(path, intf, 0);
526 }
527 else
528 {
529 // Path not found in cache, add and return
530 return addServices(path, intf, 0);
531 }
532}
533
534const std::string& Zone::addServices(const std::string& path,
535 const std::string& intf,
536 int32_t depth)
537{
538 static const std::string empty = "";
539 auto it = _servTree.end();
540
541 // Get all subtree objects for the given interface
542 auto objects = util::SDBusPlus::getSubTree(_bus, "/", intf, depth);
543 // Add what's returned to the cache of path->services
544 for (auto& pIter : objects)
545 {
546 auto pathIter = _servTree.find(pIter.first);
547 if (pathIter != _servTree.end())
548 {
549 // Path found in cache
550 for (auto& sIter : pIter.second)
551 {
552 auto servIter = pathIter->second.find(sIter.first);
553 if (servIter != pathIter->second.end())
554 {
555 // Service found in cache
556 for (auto& iIter : sIter.second)
557 {
558 // Add interface to cache
559 servIter->second.emplace_back(iIter);
560 }
561 }
562 else
563 {
564 // Service not found in cache
565 pathIter->second.insert(sIter);
566 }
567 }
568 }
569 else
570 {
571 _servTree.insert(pIter);
572 }
573 // When the paths match, since a single interface constraint is given,
574 // that is the service to return
575 if (path == pIter.first)
576 {
577 it = _servTree.find(pIter.first);
578 }
579 }
580
581 if (it != _servTree.end())
582 {
583 return it->second.begin()->first;
584 }
585
586 return empty;
587}
588
Matt Spinler7f88fe62017-04-10 14:39:02 -0500589}
590}
591}