blob: a57174d6e1bb6567c46b9e7047e470920886eb70 [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 Barthdf3e8d62017-05-31 11:07:24 -050016#include <phosphor-logging/elog.hpp>
Matt Spinler7f88fe62017-04-10 14:39:02 -050017#include "zone.hpp"
Matthew Barthdf3e8d62017-05-31 11:07:24 -050018#include "utility.hpp"
Matt Spinler7f88fe62017-04-10 14:39:02 -050019
20namespace phosphor
21{
22namespace fan
23{
24namespace control
25{
26
Matthew Barthdf3e8d62017-05-31 11:07:24 -050027using namespace phosphor::logging;
Matt Spinler7f88fe62017-04-10 14:39:02 -050028
Matthew Barth14184132017-05-19 14:37:30 -050029Zone::Zone(Mode mode,
30 sdbusplus::bus::bus& bus,
Matt Spinler7f88fe62017-04-10 14:39:02 -050031 const ZoneDefinition& def) :
32 _bus(bus),
33 _fullSpeed(std::get<fullSpeedPos>(def)),
34 _zoneNum(std::get<zoneNumPos>(def))
35{
36 auto& fanDefs = std::get<fanListPos>(def);
37
38 for (auto& def : fanDefs)
39 {
40 _fans.emplace_back(std::make_unique<Fan>(bus, def));
41 }
Matthew Barth38a93a82017-05-11 14:12:27 -050042
Matthew Barth14184132017-05-19 14:37:30 -050043 // Do not enable set speed events when in init mode
44 if (mode != Mode::init)
Matthew Barth17d1fe22017-05-11 15:00:36 -050045 {
Matthew Barth14184132017-05-19 14:37:30 -050046 // Setup signal trigger for set speed events
47 for (auto& event : std::get<setSpeedEventsPos>(def))
Matthew Barth17d1fe22017-05-11 15:00:36 -050048 {
Matthew Barthdf3e8d62017-05-31 11:07:24 -050049 // Get the current value for each property
50 for (auto& entry : std::get<groupPos>(event))
51 {
52 try
53 {
Matthew Barth9e741ed2017-06-02 16:29:09 -050054 PropertyVariantType property;
Matthew Barthdf3e8d62017-05-31 11:07:24 -050055 getProperty(_bus,
56 entry.first,
57 std::get<intfPos>(entry.second),
58 std::get<propPos>(entry.second),
Matthew Barth9e741ed2017-06-02 16:29:09 -050059 property);
Matthew Barthdf3e8d62017-05-31 11:07:24 -050060 setPropertyValue(entry.first.c_str(),
Matthew Barthcec5ab72017-06-02 15:20:56 -050061 std::get<intfPos>(entry.second).c_str(),
Matthew Barthdf3e8d62017-05-31 11:07:24 -050062 std::get<propPos>(entry.second).c_str(),
Matthew Barth9e741ed2017-06-02 16:29:09 -050063 property);
Matthew Barthdf3e8d62017-05-31 11:07:24 -050064 }
65 catch (const std::exception& e)
66 {
67 log<level::ERR>(e.what());
68 }
69 }
70 // Setup signal matches for property change events
Matthew Barth14184132017-05-19 14:37:30 -050071 for (auto& prop : std::get<propChangeListPos>(event))
72 {
73 _signalEvents.emplace_back(
Matthew Barth34f1bda2017-05-31 13:45:36 -050074 std::make_unique<EventData>(
Matthew Barth14184132017-05-19 14:37:30 -050075 EventData
76 {
77 std::get<groupPos>(event),
78 std::get<handlerObjPos>(prop),
79 std::get<actionPos>(event)
80 }));
81 _matches.emplace_back(
82 bus,
83 std::get<signaturePos>(prop).c_str(),
Matthew Barth34f1bda2017-05-31 13:45:36 -050084 std::bind(std::mem_fn(&Zone::handleEvent),
85 this,
86 std::placeholders::_1,
87 _signalEvents.back().get()));
Matthew Barth14184132017-05-19 14:37:30 -050088 }
Matthew Barthdf3e8d62017-05-31 11:07:24 -050089 // Run action function for initial event state
90 std::get<actionPos>(event)(*this,
91 std::get<groupPos>(event));
Matthew Barth17d1fe22017-05-11 15:00:36 -050092 }
Matthew Barth38a93a82017-05-11 14:12:27 -050093 }
Matt Spinler7f88fe62017-04-10 14:39:02 -050094}
95
96
97void Zone::setSpeed(uint64_t speed)
98{
99 for (auto& fan : _fans)
100 {
101 fan->setSpeed(speed);
102 }
103}
104
Matthew Barth861d77c2017-05-22 14:18:25 -0500105void Zone::setActiveAllow(const Group* group, bool isActiveAllow)
106{
107 _active[group] = isActiveAllow;
108 if (!isActiveAllow)
109 {
110 _isActive = false;
111 }
112 else
113 {
114 // Check all entries are set to allow control active
115 auto actPred = [](auto const& entry) {return entry.second;};
116 _isActive = std::all_of(_active.begin(),
117 _active.end(),
118 actPred);
119 }
120}
121
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500122void Zone::getProperty(sdbusplus::bus::bus& bus,
123 const std::string& path,
124 const std::string& iface,
125 const std::string& prop,
Matthew Barth9e741ed2017-06-02 16:29:09 -0500126 PropertyVariantType& value)
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500127{
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500128 auto serv = phosphor::fan::util::getService(path, iface, bus);
129 auto hostCall = bus.new_method_call(serv.c_str(),
130 path.c_str(),
131 "org.freedesktop.DBus.Properties",
132 "Get");
133 hostCall.append(iface);
134 hostCall.append(prop);
135 auto hostResponseMsg = bus.call(hostCall);
136 if (hostResponseMsg.is_method_error())
137 {
138 throw std::runtime_error(
139 "Error in host call response for retrieving property");
140 }
Matthew Barth9e741ed2017-06-02 16:29:09 -0500141 hostResponseMsg.read(value);
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500142}
143
Matthew Barth38a93a82017-05-11 14:12:27 -0500144void Zone::handleEvent(sdbusplus::message::message& msg,
Matthew Barth34f1bda2017-05-31 13:45:36 -0500145 const EventData* eventData)
Matthew Barth38a93a82017-05-11 14:12:27 -0500146{
147 // Handle the callback
Matthew Barth34f1bda2017-05-31 13:45:36 -0500148 std::get<eventHandlerPos>(*eventData)(_bus, msg, *this);
Matthew Barth17d1fe22017-05-11 15:00:36 -0500149 // Perform the action
Matthew Barth34f1bda2017-05-31 13:45:36 -0500150 std::get<eventActionPos>(*eventData)(*this,
151 std::get<eventGroupPos>(*eventData));
Matthew Barth38a93a82017-05-11 14:12:27 -0500152}
153
Matt Spinler7f88fe62017-04-10 14:39:02 -0500154}
155}
156}