blob: c3c1226b11b05e52acaff703b94b237572da46ec [file] [log] [blame]
Matt Spinlere10416e2017-04-10 14:15:53 -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 */
Matt Spinler57352a32017-04-10 14:48:35 -050016#include <algorithm>
Matt Spinleree7f6422017-05-09 11:03:14 -050017#include <phosphor-logging/log.hpp>
18#include <unistd.h>
Matt Spinlere10416e2017-04-10 14:15:53 -050019#include "manager.hpp"
Gunnar Millsf96b01e2017-06-02 16:32:19 -050020#include "utility.hpp"
Matt Spinlere10416e2017-04-10 14:15:53 -050021
22namespace phosphor
23{
24namespace fan
25{
26namespace control
27{
28
Matt Spinleree7f6422017-05-09 11:03:14 -050029using namespace phosphor::logging;
30
31constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
32constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
33constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
34constexpr auto FAN_CONTROL_READY_TARGET = "obmc-fan-control-ready@0.target";
35
Gunnar Millsf96b01e2017-06-02 16:32:19 -050036constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
37constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
38constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
39constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
40
41
42/**
43 * Get the current value of the D-Bus property under the specified path
44 * and interface.
45 *
46 * @param[in] bus - The D-Bus bus object
47 * @param[in] path - The D-Bus path
48 * @param[in] interface - The D-Bus interface
49 * @param[in] propertyName - The D-Bus property
50 * @param[out] value - The D-Bus property's value
51 */
52template <typename T>
53void getProperty(sdbusplus::bus::bus& bus,
54 const std::string& path,
55 const std::string& interface,
56 const std::string& propertyName,
57 T& value)
58{
59 sdbusplus::message::variant<T> property;
60 std::string service = phosphor::fan::util::getService(path, interface, bus);
61
62 auto method = bus.new_method_call(service.c_str(),
63 path.c_str(),
64 PROPERTY_INTERFACE,
65 "Get");
66
67 method.append(interface, propertyName);
68 auto reply = bus.call(method);
69
70 if (reply.is_method_error())
71 {
72 throw std::runtime_error(
73 "Error in call response for retrieving property");
74 }
75 reply.read(property);
76 value = sdbusplus::message::variant_ns::get<T>(property);
77}
78
79
80/**
81 * Check if a condition is true. Conditions are used to determine
82 * which fan zone to use.
83 *
84 * @param[in] bus - The D-Bus bus object
85 * @param[in] condition - The condition to check if true
86 * @return result - True if the condition is true
87 */
88bool checkCondition(sdbusplus::bus::bus& bus, const auto& c)
89{
90 auto& type = std::get<conditionTypePos>(c);
91 auto& properties = std::get<conditionPropertyListPos>(c);
92
93 for (auto& p : properties)
94 {
95 bool value = std::get<propertyValuePos>(p);
96 bool propertyValue;
97
98 // TODO openbmc/openbmc#1769: Support more types than just getProperty.
99 if (type.compare("getProperty") == 0)
100 {
101 getProperty(bus,
102 std::get<propertyPathPos>(p),
103 std::get<propertyInterfacePos>(p),
104 std::get<propertyNamePos>(p),
105 propertyValue);
106
107 if (value != propertyValue)
108 {
109 return false;
110 }
111 }
112 }
113 return true;
114}
115
116
Matt Spinleree7f6422017-05-09 11:03:14 -0500117//Note: Future code will check 'mode' before starting control algorithm
118Manager::Manager(sdbusplus::bus::bus& bus,
119 Mode mode) :
Matt Spinlere10416e2017-04-10 14:15:53 -0500120 _bus(bus)
121{
Matt Spinler57352a32017-04-10 14:48:35 -0500122 //Create the appropriate Zone objects based on the
123 //actual system configuration.
124
125 //Find the 1 ZoneGroup that meets all of its conditions
126 for (auto& group : _zoneLayouts)
127 {
128 auto& conditions = std::get<conditionListPos>(group);
129
130 if (std::all_of(conditions.begin(), conditions.end(),
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500131 [&bus](const auto& condition)
132 {
133 return checkCondition(bus, condition);
134 }))
Matt Spinler57352a32017-04-10 14:48:35 -0500135 {
136 //Create a Zone object for each zone in this group
137 auto& zones = std::get<zoneListPos>(group);
138
139 for (auto& z : zones)
140 {
141 _zones.emplace(std::get<zoneNumPos>(z),
Matthew Barth14184132017-05-19 14:37:30 -0500142 std::make_unique<Zone>(mode, _bus, z));
Matt Spinler57352a32017-04-10 14:48:35 -0500143 }
144
145 break;
146 }
147 }
148
Matt Spinleree7f6422017-05-09 11:03:14 -0500149}
150
151
152void Manager::doInit()
153{
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500154 for (auto& z : _zones)
Matt Spinler57352a32017-04-10 14:48:35 -0500155 {
156 z.second->setFullSpeed();
157 }
Matt Spinleree7f6422017-05-09 11:03:14 -0500158
159 auto delay = _powerOnDelay;
160 while (delay > 0)
161 {
162 delay = sleep(delay);
163 }
164
165 startFanControlReadyTarget();
Matt Spinlere10416e2017-04-10 14:15:53 -0500166}
167
168
Matt Spinleree7f6422017-05-09 11:03:14 -0500169void Manager::startFanControlReadyTarget()
170{
171 auto method = _bus.new_method_call(SYSTEMD_SERVICE,
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500172 SYSTEMD_OBJ_PATH,
173 SYSTEMD_INTERFACE,
174 "StartUnit");
Matt Spinleree7f6422017-05-09 11:03:14 -0500175
176 method.append(FAN_CONTROL_READY_TARGET);
177 method.append("replace");
178
179 auto response = _bus.call(method);
180 if (response.is_method_error())
181 {
182 //TODO openbmc/openbmc#1555 create an elog
183 log<level::ERR>("Failed to start fan control ready target");
184 throw std::runtime_error("Failed to start fan control ready target");
185 }
186}
187
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500188} // namespace control
189} // namespace fan
190} // namespace phosphor