blob: f5099c12cdd4c498a74d5f6aa19c0c590085a3a6 [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>
Dinesh Chinari618027a2017-06-26 23:26:50 -050017#include <phosphor-logging/log.hpp>
Matthew Barthdf3e8d62017-05-31 11:07:24 -050018#include <phosphor-logging/elog.hpp>
Dinesh Chinari618027a2017-06-26 23:26:50 -050019#include <phosphor-logging/elog-errors.hpp>
20#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinler7f88fe62017-04-10 14:39:02 -050021#include "zone.hpp"
Matthew Barthdf3e8d62017-05-31 11:07:24 -050022#include "utility.hpp"
Matt Spinler7f88fe62017-04-10 14:39:02 -050023
24namespace phosphor
25{
26namespace fan
27{
28namespace control
29{
30
Matthew Barth8600d9a2017-06-23 14:38:05 -050031using namespace std::chrono;
Matthew Barthdf3e8d62017-05-31 11:07:24 -050032using namespace phosphor::logging;
Dinesh Chinari618027a2017-06-26 23:26:50 -050033using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
34 Error::InternalFailure;
Matt Spinler7f88fe62017-04-10 14:39:02 -050035
Matthew Barth14184132017-05-19 14:37:30 -050036Zone::Zone(Mode mode,
37 sdbusplus::bus::bus& bus,
Matthew Barth8600d9a2017-06-23 14:38:05 -050038 phosphor::fan::event::EventPtr& events,
Matt Spinler7f88fe62017-04-10 14:39:02 -050039 const ZoneDefinition& def) :
40 _bus(bus),
41 _fullSpeed(std::get<fullSpeedPos>(def)),
Matthew Barth1de66622017-06-12 13:13:02 -050042 _zoneNum(std::get<zoneNumPos>(def)),
Matthew Barthe0ca13e2017-06-13 16:29:09 -050043 _defFloorSpeed(std::get<floorSpeedPos>(def)),
Matthew Barth8600d9a2017-06-23 14:38:05 -050044 _defCeilingSpeed(std::get<fullSpeedPos>(def)),
45 _decTimer(events, [this](){ this->decTimerExpired(); })
Matt Spinler7f88fe62017-04-10 14:39:02 -050046{
47 auto& fanDefs = std::get<fanListPos>(def);
48
49 for (auto& def : fanDefs)
50 {
51 _fans.emplace_back(std::make_unique<Fan>(bus, def));
52 }
Matthew Barth38a93a82017-05-11 14:12:27 -050053
Matthew Barth14184132017-05-19 14:37:30 -050054 // Do not enable set speed events when in init mode
55 if (mode != Mode::init)
Matthew Barth17d1fe22017-05-11 15:00:36 -050056 {
Matthew Barth1bf0ce42017-06-23 16:16:30 -050057 initEvents(def);
Matthew Barth8600d9a2017-06-23 14:38:05 -050058 // Start timer for fan speed decreases
59 if (!_decTimer.running())
60 {
61 //TODO Update time value to what's given in zones yaml
62 _decTimer.start(seconds(30),
63 phosphor::fan::util::Timer::TimerType::repeating);
64 }
Matthew Barth38a93a82017-05-11 14:12:27 -050065 }
Matt Spinler7f88fe62017-04-10 14:39:02 -050066}
67
68
69void Zone::setSpeed(uint64_t speed)
70{
71 for (auto& fan : _fans)
72 {
73 fan->setSpeed(speed);
74 }
75}
76
Matthew Barth861d77c2017-05-22 14:18:25 -050077void Zone::setActiveAllow(const Group* group, bool isActiveAllow)
78{
79 _active[group] = isActiveAllow;
80 if (!isActiveAllow)
81 {
82 _isActive = false;
83 }
84 else
85 {
86 // Check all entries are set to allow control active
87 auto actPred = [](auto const& entry) {return entry.second;};
88 _isActive = std::all_of(_active.begin(),
89 _active.end(),
90 actPred);
91 }
92}
93
Matthew Barth240397b2017-06-22 11:23:30 -050094void Zone::requestSpeedIncrease(uint64_t targetDelta)
95{
96 // Only increase speed when delta is higher than
97 // the current increase delta for the zone and currently under ceiling
98 if (targetDelta > _incSpeedDelta &&
99 _targetSpeed < _ceilingSpeed)
100 {
101 _targetSpeed = (targetDelta - _incSpeedDelta) + _targetSpeed;
102 _incSpeedDelta = targetDelta;
103 //TODO openbmc/openbmc#1625 Cancel current timer countdown
104 //TODO Floor speed above target, update target to floor speed
105 if (_targetSpeed < _floorSpeed)
106 {
107 _targetSpeed = _floorSpeed;
108 }
109 // Target speed can not go above a defined ceiling speed
110 if (_targetSpeed > _ceilingSpeed)
111 {
112 _targetSpeed = _ceilingSpeed;
113 }
114
115 setSpeed(_targetSpeed);
116 //TODO openbmc/openbmc#1625 Start timer countdown for fan speed increase
117 }
118 //TODO openbmc/openbmc#1625 Clear increase delta when timer expires
119 _incSpeedDelta = 0;
120}
121
Matthew Barth0ce99d82017-06-22 15:07:29 -0500122void Zone::requestSpeedDecrease(uint64_t targetDelta)
123{
124 // Only decrease the lowest target delta requested
125 if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta)
126 {
127 _decSpeedDelta = targetDelta;
128 }
Matthew Barth8600d9a2017-06-23 14:38:05 -0500129}
Matthew Barth0ce99d82017-06-22 15:07:29 -0500130
Matthew Barth8600d9a2017-06-23 14:38:05 -0500131void Zone::decTimerExpired()
132{
Matthew Barth0ce99d82017-06-22 15:07:29 -0500133 // Only decrease speeds when no requested increases exist
Matthew Barth8600d9a2017-06-23 14:38:05 -0500134 //TODO Add increase timer not running (i.e. not in the middle of increasing)
Matthew Barth0ce99d82017-06-22 15:07:29 -0500135 if (_incSpeedDelta == 0)
136 {
137 // Target speed can not go below the defined floor speed
138 if ((_targetSpeed < _decSpeedDelta) ||
139 (_targetSpeed - _decSpeedDelta < _floorSpeed))
140 {
141 _targetSpeed = _floorSpeed;
142 }
143 else
144 {
145 _targetSpeed = _targetSpeed - _decSpeedDelta;
146 }
147 setSpeed(_targetSpeed);
148 }
149 // Clear decrease delta when timer expires
150 _decSpeedDelta = 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500151 // Decrease timer is restarted since its repeating
Matthew Barth0ce99d82017-06-22 15:07:29 -0500152}
153
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500154void Zone::initEvents(const ZoneDefinition& def)
155{
156 // Setup signal trigger for set speed events
157 for (auto& event : std::get<setSpeedEventsPos>(def))
158 {
159 // Get the current value for each property
160 for (auto& entry : std::get<groupPos>(event))
161 {
162 refreshProperty(_bus,
163 entry.first,
164 std::get<intfPos>(entry.second),
165 std::get<propPos>(entry.second));
166 }
167 // Setup signal matches for property change events
168 for (auto& prop : std::get<propChangeListPos>(event))
169 {
170 _signalEvents.emplace_back(
171 std::make_unique<EventData>(
172 EventData
173 {
174 std::get<groupPos>(event),
175 std::get<handlerObjPos>(prop),
176 std::get<actionPos>(event)
177 }));
178 _matches.emplace_back(
179 _bus,
180 std::get<signaturePos>(prop).c_str(),
181 std::bind(std::mem_fn(&Zone::handleEvent),
182 this,
183 std::placeholders::_1,
184 _signalEvents.back().get()));
185 }
186 // Run action function for initial event state
187 std::get<actionPos>(event)(*this,
188 std::get<groupPos>(event));
189 }
190}
191
192void Zone::refreshProperty(sdbusplus::bus::bus& bus,
193 const std::string& path,
194 const std::string& iface,
195 const std::string& prop)
196{
197 PropertyVariantType property;
198 getProperty(_bus, path, iface, prop, property);
199 setPropertyValue(path.c_str(), iface.c_str(), prop.c_str(), property);
200}
201
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500202void Zone::getProperty(sdbusplus::bus::bus& bus,
203 const std::string& path,
204 const std::string& iface,
205 const std::string& prop,
Matthew Barth9e741ed2017-06-02 16:29:09 -0500206 PropertyVariantType& value)
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500207{
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500208 auto serv = phosphor::fan::util::getService(path, iface, bus);
209 auto hostCall = bus.new_method_call(serv.c_str(),
210 path.c_str(),
211 "org.freedesktop.DBus.Properties",
212 "Get");
213 hostCall.append(iface);
214 hostCall.append(prop);
215 auto hostResponseMsg = bus.call(hostCall);
216 if (hostResponseMsg.is_method_error())
217 {
Dinesh Chinari618027a2017-06-26 23:26:50 -0500218 log<level::ERR>("Error in host call response for retrieving property");
219 elog<InternalFailure>();
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500220 }
Matthew Barth9e741ed2017-06-02 16:29:09 -0500221 hostResponseMsg.read(value);
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500222}
223
Matthew Barth38a93a82017-05-11 14:12:27 -0500224void Zone::handleEvent(sdbusplus::message::message& msg,
Matthew Barth34f1bda2017-05-31 13:45:36 -0500225 const EventData* eventData)
Matthew Barth38a93a82017-05-11 14:12:27 -0500226{
227 // Handle the callback
Matthew Barth34f1bda2017-05-31 13:45:36 -0500228 std::get<eventHandlerPos>(*eventData)(_bus, msg, *this);
Matthew Barth17d1fe22017-05-11 15:00:36 -0500229 // Perform the action
Matthew Barth34f1bda2017-05-31 13:45:36 -0500230 std::get<eventActionPos>(*eventData)(*this,
231 std::get<eventGroupPos>(*eventData));
Matthew Barth38a93a82017-05-11 14:12:27 -0500232}
233
Matt Spinler7f88fe62017-04-10 14:39:02 -0500234}
235}
236}