1 /**
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  */
16 #include "config.h"
17 
18 #include "manager.hpp"
19 
20 #include "sdbusplus.hpp"
21 #include "utility.hpp"
22 
23 #include <unistd.h>
24 
25 #include <phosphor-logging/elog-errors.hpp>
26 #include <phosphor-logging/elog.hpp>
27 #include <phosphor-logging/log.hpp>
28 #include <sdbusplus/bus.hpp>
29 #include <xyz/openbmc_project/Common/error.hpp>
30 
31 #include <algorithm>
32 #include <filesystem>
33 
34 namespace phosphor
35 {
36 namespace fan
37 {
38 namespace control
39 {
40 
41 using namespace phosphor::logging;
42 namespace fs = std::filesystem;
43 
44 constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
45 constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
46 constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
47 constexpr auto FAN_CONTROL_READY_TARGET = "obmc-fan-control-ready@0.target";
48 
49 /**
50  * Check if a condition is true. Conditions are used to determine
51  * which fan zone to use.
52  *
53  * @param[in] bus       - The D-Bus bus object
54  * @param[in] condition - The condition to check if true
55  * @return result       - True if the condition is true
56  */
57 bool checkCondition(sdbusplus::bus_t& bus, const Condition& c)
58 {
59     auto& type = std::get<conditionTypePos>(c);
60     auto& properties = std::get<conditionPropertyListPos>(c);
61 
62     for (auto& p : properties)
63     {
64         auto value = std::get<propertyValuePos>(p);
65 
66         // TODO openbmc/openbmc#1769: Support more types than just getProperty.
67         if (type.compare("getProperty") == 0)
68         {
69             auto propertyValue = util::SDBusPlus::getProperty<decltype(value)>(
70                 bus, std::get<propertyPathPos>(p),
71                 std::get<propertyInterfacePos>(p),
72                 std::get<propertyNamePos>(p));
73 
74             if (value != propertyValue)
75             {
76                 return false;
77             }
78         }
79     }
80     return true;
81 }
82 
83 // Note: Future code will check 'mode' before starting control algorithm
84 Manager::Manager(sdbusplus::bus_t& bus, const sdeventplus::Event& event,
85                  Mode mode) :
86     _bus(bus),
87     _objMgr(bus, CONTROL_OBJPATH)
88 {
89     // Create the appropriate Zone objects based on the
90     // actual system configuration.
91 
92     // Find the 1 ZoneGroup that meets all of its conditions
93     for (auto& group : _zoneLayouts)
94     {
95         auto& conditions = std::get<conditionListPos>(group);
96 
97         if (std::all_of(conditions.begin(), conditions.end(),
98                         [&bus](const auto& condition) {
99                             return checkCondition(bus, condition);
100                         }))
101         {
102             // Create a Zone object for each zone in this group
103             auto& zones = std::get<zoneListPos>(group);
104 
105             for (auto& z : zones)
106             {
107                 fs::path path{CONTROL_OBJPATH};
108                 path /= std::to_string(std::get<zoneNumPos>(z));
109                 _zones.emplace(std::get<zoneNumPos>(z),
110                                std::make_unique<Zone>(mode, _bus, path.string(),
111                                                       event, z));
112             }
113 
114             break;
115         }
116     }
117 
118     if (mode == Mode::control)
119     {
120         bus.request_name(CONTROL_BUSNAME);
121     }
122 }
123 
124 void Manager::doInit(const sdeventplus::Event& /*event*/)
125 {
126     for (auto& z : _zones)
127     {
128         z.second->setFullSpeed();
129     }
130     auto delay = _powerOnDelay;
131     while (delay > 0)
132     {
133         delay = sleep(delay);
134     }
135 
136     util::SDBusPlus::callMethod(_bus, SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
137                                 SYSTEMD_INTERFACE, "StartUnit",
138                                 FAN_CONTROL_READY_TARGET, "replace");
139 }
140 
141 } // namespace control
142 } // namespace fan
143 } // namespace phosphor
144