blob: d5f97d7a8f8ac31d5fd723e1fe785b6a6b1c3f4f [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>
Dinesh Chinari618027a2017-06-26 23:26:50 -050018#include <phosphor-logging/elog.hpp>
19#include <phosphor-logging/elog-errors.hpp>
20#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinleree7f6422017-05-09 11:03:14 -050021#include <unistd.h>
Matt Spinlere10416e2017-04-10 14:15:53 -050022#include "manager.hpp"
Gunnar Millsf96b01e2017-06-02 16:32:19 -050023#include "utility.hpp"
Matt Spinlere10416e2017-04-10 14:15:53 -050024
25namespace phosphor
26{
27namespace fan
28{
29namespace control
30{
31
Matt Spinleree7f6422017-05-09 11:03:14 -050032using namespace phosphor::logging;
33
34constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
35constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
36constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
37constexpr auto FAN_CONTROL_READY_TARGET = "obmc-fan-control-ready@0.target";
38
Gunnar Millsf96b01e2017-06-02 16:32:19 -050039constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
40constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
41constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
42constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
43
44
45/**
46 * Get the current value of the D-Bus property under the specified path
47 * and interface.
48 *
49 * @param[in] bus - The D-Bus bus object
50 * @param[in] path - The D-Bus path
51 * @param[in] interface - The D-Bus interface
52 * @param[in] propertyName - The D-Bus property
53 * @param[out] value - The D-Bus property's value
54 */
55template <typename T>
56void getProperty(sdbusplus::bus::bus& bus,
57 const std::string& path,
58 const std::string& interface,
59 const std::string& propertyName,
60 T& value)
61{
62 sdbusplus::message::variant<T> property;
63 std::string service = phosphor::fan::util::getService(path, interface, bus);
64
65 auto method = bus.new_method_call(service.c_str(),
66 path.c_str(),
67 PROPERTY_INTERFACE,
68 "Get");
69
70 method.append(interface, propertyName);
71 auto reply = bus.call(method);
72
73 if (reply.is_method_error())
74 {
Dinesh Chinari618027a2017-06-26 23:26:50 -050075 log<level::ERR>("Error in call response for retrieving property");
76 elog<InternalFailure>();
Gunnar Millsf96b01e2017-06-02 16:32:19 -050077 }
78 reply.read(property);
79 value = sdbusplus::message::variant_ns::get<T>(property);
80}
81
82
83/**
84 * Check if a condition is true. Conditions are used to determine
85 * which fan zone to use.
86 *
87 * @param[in] bus - The D-Bus bus object
88 * @param[in] condition - The condition to check if true
89 * @return result - True if the condition is true
90 */
91bool checkCondition(sdbusplus::bus::bus& bus, const auto& c)
92{
93 auto& type = std::get<conditionTypePos>(c);
94 auto& properties = std::get<conditionPropertyListPos>(c);
95
96 for (auto& p : properties)
97 {
98 bool value = std::get<propertyValuePos>(p);
99 bool propertyValue;
100
101 // TODO openbmc/openbmc#1769: Support more types than just getProperty.
102 if (type.compare("getProperty") == 0)
103 {
104 getProperty(bus,
105 std::get<propertyPathPos>(p),
106 std::get<propertyInterfacePos>(p),
107 std::get<propertyNamePos>(p),
108 propertyValue);
109
110 if (value != propertyValue)
111 {
112 return false;
113 }
114 }
115 }
116 return true;
117}
118
119
Matt Spinleree7f6422017-05-09 11:03:14 -0500120//Note: Future code will check 'mode' before starting control algorithm
121Manager::Manager(sdbusplus::bus::bus& bus,
Matthew Barth8600d9a2017-06-23 14:38:05 -0500122 phosphor::fan::event::EventPtr& events,
Matt Spinleree7f6422017-05-09 11:03:14 -0500123 Mode mode) :
Matt Spinlere10416e2017-04-10 14:15:53 -0500124 _bus(bus)
125{
Matt Spinler57352a32017-04-10 14:48:35 -0500126 //Create the appropriate Zone objects based on the
127 //actual system configuration.
128
129 //Find the 1 ZoneGroup that meets all of its conditions
130 for (auto& group : _zoneLayouts)
131 {
132 auto& conditions = std::get<conditionListPos>(group);
133
134 if (std::all_of(conditions.begin(), conditions.end(),
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500135 [&bus](const auto& condition)
136 {
137 return checkCondition(bus, condition);
138 }))
Matt Spinler57352a32017-04-10 14:48:35 -0500139 {
140 //Create a Zone object for each zone in this group
141 auto& zones = std::get<zoneListPos>(group);
142
143 for (auto& z : zones)
144 {
145 _zones.emplace(std::get<zoneNumPos>(z),
Matthew Barth8600d9a2017-06-23 14:38:05 -0500146 std::make_unique<Zone>(mode, _bus, events, z));
Matt Spinler57352a32017-04-10 14:48:35 -0500147 }
148
149 break;
150 }
151 }
152
Matt Spinleree7f6422017-05-09 11:03:14 -0500153}
154
155
156void Manager::doInit()
157{
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500158 for (auto& z : _zones)
Matt Spinler57352a32017-04-10 14:48:35 -0500159 {
160 z.second->setFullSpeed();
161 }
Matt Spinleree7f6422017-05-09 11:03:14 -0500162
163 auto delay = _powerOnDelay;
164 while (delay > 0)
165 {
166 delay = sleep(delay);
167 }
168
169 startFanControlReadyTarget();
Matt Spinlere10416e2017-04-10 14:15:53 -0500170}
171
172
Matt Spinleree7f6422017-05-09 11:03:14 -0500173void Manager::startFanControlReadyTarget()
174{
175 auto method = _bus.new_method_call(SYSTEMD_SERVICE,
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500176 SYSTEMD_OBJ_PATH,
177 SYSTEMD_INTERFACE,
178 "StartUnit");
Matt Spinleree7f6422017-05-09 11:03:14 -0500179
180 method.append(FAN_CONTROL_READY_TARGET);
181 method.append("replace");
182
183 auto response = _bus.call(method);
184 if (response.is_method_error())
185 {
Matt Spinleree7f6422017-05-09 11:03:14 -0500186 log<level::ERR>("Failed to start fan control ready target");
Dinesh Chinari618027a2017-06-26 23:26:50 -0500187 elog<InternalFailure>();
Matt Spinleree7f6422017-05-09 11:03:14 -0500188 }
189}
190
Gunnar Millsf96b01e2017-06-02 16:32:19 -0500191} // namespace control
192} // namespace fan
193} // namespace phosphor