xref: /openbmc/phosphor-fan-presence/control/manager.cpp (revision 14cc043f48a23f6e7e9cf46b6475b0b32880c984)
1e10416ecSMatt Spinler /**
2e10416ecSMatt Spinler  * Copyright © 2017 IBM Corporation
3e10416ecSMatt Spinler  *
4e10416ecSMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
5e10416ecSMatt Spinler  * you may not use this file except in compliance with the License.
6e10416ecSMatt Spinler  * You may obtain a copy of the License at
7e10416ecSMatt Spinler  *
8e10416ecSMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
9e10416ecSMatt Spinler  *
10e10416ecSMatt Spinler  * Unless required by applicable law or agreed to in writing, software
11e10416ecSMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
12e10416ecSMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e10416ecSMatt Spinler  * See the License for the specific language governing permissions and
14e10416ecSMatt Spinler  * limitations under the License.
15e10416ecSMatt Spinler  */
1657352a31SMatt Spinler #include <algorithm>
17ee7f6428SMatt Spinler #include <phosphor-logging/log.hpp>
18618027abSDinesh Chinari #include <phosphor-logging/elog.hpp>
19618027abSDinesh Chinari #include <phosphor-logging/elog-errors.hpp>
20618027abSDinesh Chinari #include <xyz/openbmc_project/Common/error.hpp>
21ee7f6428SMatt Spinler #include <unistd.h>
22*14cc043fSMatthew Barth #include "config.h"
23e10416ecSMatt Spinler #include "manager.hpp"
24f96b01e2SGunnar Mills #include "utility.hpp"
25803d35bcSMatthew Barth #include "sdbusplus.hpp"
26e10416ecSMatt Spinler 
27e10416ecSMatt Spinler namespace phosphor
28e10416ecSMatt Spinler {
29e10416ecSMatt Spinler namespace fan
30e10416ecSMatt Spinler {
31e10416ecSMatt Spinler namespace control
32e10416ecSMatt Spinler {
33e10416ecSMatt Spinler 
34ee7f6428SMatt Spinler using namespace phosphor::logging;
35ee7f6428SMatt Spinler 
36ee7f6428SMatt Spinler constexpr auto SYSTEMD_SERVICE   = "org.freedesktop.systemd1";
37ee7f6428SMatt Spinler constexpr auto SYSTEMD_OBJ_PATH  = "/org/freedesktop/systemd1";
38ee7f6428SMatt Spinler constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
39ee7f6428SMatt Spinler constexpr auto FAN_CONTROL_READY_TARGET = "obmc-fan-control-ready@0.target";
40ee7f6428SMatt Spinler 
41f96b01e2SGunnar Mills /**
42f96b01e2SGunnar Mills  * Check if a condition is true. Conditions are used to determine
43f96b01e2SGunnar Mills  * which fan zone to use.
44f96b01e2SGunnar Mills  *
45f96b01e2SGunnar Mills  * @param[in] bus       - The D-Bus bus object
46f96b01e2SGunnar Mills  * @param[in] condition - The condition to check if true
47f96b01e2SGunnar Mills  * @return result       - True if the condition is true
48f96b01e2SGunnar Mills  */
49f0b020fbSBrad Bishop bool checkCondition(sdbusplus::bus::bus& bus, const Condition& c)
50f96b01e2SGunnar Mills {
51f96b01e2SGunnar Mills     auto& type = std::get<conditionTypePos>(c);
52f96b01e2SGunnar Mills     auto& properties = std::get<conditionPropertyListPos>(c);
53f96b01e2SGunnar Mills 
54f96b01e2SGunnar Mills     for (auto& p : properties)
55f96b01e2SGunnar Mills     {
56803d35bcSMatthew Barth         auto value = std::get<propertyValuePos>(p);
57f96b01e2SGunnar Mills 
58f96b01e2SGunnar Mills         // TODO openbmc/openbmc#1769: Support more types than just getProperty.
59f96b01e2SGunnar Mills         if (type.compare("getProperty") == 0)
60f96b01e2SGunnar Mills         {
61803d35bcSMatthew Barth             auto propertyValue = util::SDBusPlus::getProperty<decltype(value)>(
62803d35bcSMatthew Barth                     bus,
63f96b01e2SGunnar Mills                     std::get<propertyPathPos>(p),
64f96b01e2SGunnar Mills                     std::get<propertyInterfacePos>(p),
65803d35bcSMatthew Barth                     std::get<propertyNamePos>(p));
66f96b01e2SGunnar Mills 
67f96b01e2SGunnar Mills             if (value != propertyValue)
68f96b01e2SGunnar Mills             {
69f96b01e2SGunnar Mills                 return false;
70f96b01e2SGunnar Mills             }
71f96b01e2SGunnar Mills         }
72f96b01e2SGunnar Mills     }
73f96b01e2SGunnar Mills     return true;
74f96b01e2SGunnar Mills }
75f96b01e2SGunnar Mills 
76f96b01e2SGunnar Mills 
77ee7f6428SMatt Spinler //Note: Future code will check 'mode' before starting control algorithm
78ee7f6428SMatt Spinler Manager::Manager(sdbusplus::bus::bus& bus,
791cfc2f11SWilliam A. Kennington III                  const sdeventplus::Event& event,
80ee7f6428SMatt Spinler                  Mode mode) :
81e10416ecSMatt Spinler     _bus(bus)
82e10416ecSMatt Spinler {
8357352a31SMatt Spinler     //Create the appropriate Zone objects based on the
8457352a31SMatt Spinler     //actual system configuration.
8557352a31SMatt Spinler 
8657352a31SMatt Spinler     //Find the 1 ZoneGroup that meets all of its conditions
8757352a31SMatt Spinler     for (auto& group : _zoneLayouts)
8857352a31SMatt Spinler     {
8957352a31SMatt Spinler         auto& conditions = std::get<conditionListPos>(group);
9057352a31SMatt Spinler 
9157352a31SMatt Spinler         if (std::all_of(conditions.begin(), conditions.end(),
92f96b01e2SGunnar Mills                         [&bus](const auto& condition)
9357352a31SMatt Spinler         {
94f96b01e2SGunnar Mills             return checkCondition(bus, condition);
9557352a31SMatt Spinler         }))
9657352a31SMatt Spinler         {
9757352a31SMatt Spinler             //Create a Zone object for each zone in this group
9857352a31SMatt Spinler             auto& zones = std::get<zoneListPos>(group);
9957352a31SMatt Spinler 
10057352a31SMatt Spinler             for (auto& z : zones)
10157352a31SMatt Spinler             {
10257352a31SMatt Spinler                 _zones.emplace(std::get<zoneNumPos>(z),
1031cfc2f11SWilliam A. Kennington III                                std::make_unique<Zone>(mode, _bus, event, z));
10457352a31SMatt Spinler             }
10557352a31SMatt Spinler 
10657352a31SMatt Spinler             break;
10757352a31SMatt Spinler         }
10857352a31SMatt Spinler     }
10957352a31SMatt Spinler 
110*14cc043fSMatthew Barth     bus.request_name(CONTROL_BUSNAME);
111ee7f6428SMatt Spinler }
112ee7f6428SMatt Spinler 
113ee7f6428SMatt Spinler 
114ee7f6428SMatt Spinler void Manager::doInit()
115ee7f6428SMatt Spinler {
11657352a31SMatt Spinler     for (auto& z : _zones)
11757352a31SMatt Spinler     {
11857352a31SMatt Spinler         z.second->setFullSpeed();
11957352a31SMatt Spinler     }
120ee7f6428SMatt Spinler 
121ee7f6428SMatt Spinler     auto delay = _powerOnDelay;
122ee7f6428SMatt Spinler     while (delay > 0)
123ee7f6428SMatt Spinler     {
124ee7f6428SMatt Spinler         delay = sleep(delay);
125e10416ecSMatt Spinler     }
126e10416ecSMatt Spinler 
1272029106aSMatthew Barth     util::SDBusPlus::callMethod(
1282029106aSMatthew Barth             _bus,
1292029106aSMatthew Barth             SYSTEMD_SERVICE,
130ee7f6428SMatt Spinler             SYSTEMD_OBJ_PATH,
131ee7f6428SMatt Spinler             SYSTEMD_INTERFACE,
1322029106aSMatthew Barth             "StartUnit",
1332029106aSMatthew Barth             FAN_CONTROL_READY_TARGET,
1342029106aSMatthew Barth             "replace");
135ee7f6428SMatt Spinler }
136e10416ecSMatt Spinler 
137f96b01e2SGunnar Mills } // namespace control
138f96b01e2SGunnar Mills } // namespace fan
139f96b01e2SGunnar Mills } // namespace phosphor
140