Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 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 | */ |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame^] | 16 | #include <phosphor-logging/log.hpp> |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 17 | #include <phosphor-logging/elog.hpp> |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame^] | 18 | #include <phosphor-logging/elog-errors.hpp> |
| 19 | #include <xyz/openbmc_project/Common/error.hpp> |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 20 | #include "zone.hpp" |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 21 | #include "utility.hpp" |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace fan |
| 26 | { |
| 27 | namespace control |
| 28 | { |
| 29 | |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 30 | using namespace phosphor::logging; |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame^] | 31 | using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: |
| 32 | Error::InternalFailure; |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 33 | |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 34 | Zone::Zone(Mode mode, |
| 35 | sdbusplus::bus::bus& bus, |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 36 | const ZoneDefinition& def) : |
| 37 | _bus(bus), |
| 38 | _fullSpeed(std::get<fullSpeedPos>(def)), |
Matthew Barth | 1de6662 | 2017-06-12 13:13:02 -0500 | [diff] [blame] | 39 | _zoneNum(std::get<zoneNumPos>(def)), |
| 40 | _defFloorSpeed(std::get<floorSpeedPos>(def)) |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 41 | { |
| 42 | auto& fanDefs = std::get<fanListPos>(def); |
| 43 | |
| 44 | for (auto& def : fanDefs) |
| 45 | { |
| 46 | _fans.emplace_back(std::make_unique<Fan>(bus, def)); |
| 47 | } |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 48 | |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 49 | // Do not enable set speed events when in init mode |
| 50 | if (mode != Mode::init) |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 51 | { |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 52 | // Setup signal trigger for set speed events |
| 53 | for (auto& event : std::get<setSpeedEventsPos>(def)) |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 54 | { |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 55 | // Get the current value for each property |
| 56 | for (auto& entry : std::get<groupPos>(event)) |
| 57 | { |
| 58 | try |
| 59 | { |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 60 | PropertyVariantType property; |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 61 | getProperty(_bus, |
| 62 | entry.first, |
| 63 | std::get<intfPos>(entry.second), |
| 64 | std::get<propPos>(entry.second), |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 65 | property); |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 66 | setPropertyValue(entry.first.c_str(), |
Matthew Barth | cec5ab7 | 2017-06-02 15:20:56 -0500 | [diff] [blame] | 67 | std::get<intfPos>(entry.second).c_str(), |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 68 | std::get<propPos>(entry.second).c_str(), |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 69 | property); |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 70 | } |
| 71 | catch (const std::exception& e) |
| 72 | { |
| 73 | log<level::ERR>(e.what()); |
| 74 | } |
| 75 | } |
| 76 | // Setup signal matches for property change events |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 77 | for (auto& prop : std::get<propChangeListPos>(event)) |
| 78 | { |
| 79 | _signalEvents.emplace_back( |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 80 | std::make_unique<EventData>( |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 81 | EventData |
| 82 | { |
| 83 | std::get<groupPos>(event), |
| 84 | std::get<handlerObjPos>(prop), |
| 85 | std::get<actionPos>(event) |
| 86 | })); |
| 87 | _matches.emplace_back( |
| 88 | bus, |
| 89 | std::get<signaturePos>(prop).c_str(), |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 90 | std::bind(std::mem_fn(&Zone::handleEvent), |
| 91 | this, |
| 92 | std::placeholders::_1, |
| 93 | _signalEvents.back().get())); |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 94 | } |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 95 | // Run action function for initial event state |
| 96 | std::get<actionPos>(event)(*this, |
| 97 | std::get<groupPos>(event)); |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 98 | } |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 99 | } |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
| 103 | void Zone::setSpeed(uint64_t speed) |
| 104 | { |
| 105 | for (auto& fan : _fans) |
| 106 | { |
Matthew Barth | 4af419c | 2017-06-12 13:39:31 -0500 | [diff] [blame] | 107 | //TODO openbmc/openbmc#1626 Move to control algorithm function |
| 108 | if (speed < _floorSpeed) |
| 109 | { |
| 110 | speed = _floorSpeed; |
| 111 | } |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 112 | fan->setSpeed(speed); |
| 113 | } |
| 114 | } |
| 115 | |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 116 | void Zone::setActiveAllow(const Group* group, bool isActiveAllow) |
| 117 | { |
| 118 | _active[group] = isActiveAllow; |
| 119 | if (!isActiveAllow) |
| 120 | { |
| 121 | _isActive = false; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | // Check all entries are set to allow control active |
| 126 | auto actPred = [](auto const& entry) {return entry.second;}; |
| 127 | _isActive = std::all_of(_active.begin(), |
| 128 | _active.end(), |
| 129 | actPred); |
| 130 | } |
| 131 | } |
| 132 | |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 133 | void Zone::getProperty(sdbusplus::bus::bus& bus, |
| 134 | const std::string& path, |
| 135 | const std::string& iface, |
| 136 | const std::string& prop, |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 137 | PropertyVariantType& value) |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 138 | { |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 139 | auto serv = phosphor::fan::util::getService(path, iface, bus); |
| 140 | auto hostCall = bus.new_method_call(serv.c_str(), |
| 141 | path.c_str(), |
| 142 | "org.freedesktop.DBus.Properties", |
| 143 | "Get"); |
| 144 | hostCall.append(iface); |
| 145 | hostCall.append(prop); |
| 146 | auto hostResponseMsg = bus.call(hostCall); |
| 147 | if (hostResponseMsg.is_method_error()) |
| 148 | { |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame^] | 149 | log<level::ERR>("Error in host call response for retrieving property"); |
| 150 | elog<InternalFailure>(); |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 151 | } |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 152 | hostResponseMsg.read(value); |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 153 | } |
| 154 | |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 155 | void Zone::handleEvent(sdbusplus::message::message& msg, |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 156 | const EventData* eventData) |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 157 | { |
| 158 | // Handle the callback |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 159 | std::get<eventHandlerPos>(*eventData)(_bus, msg, *this); |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 160 | // Perform the action |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 161 | std::get<eventActionPos>(*eventData)(*this, |
| 162 | std::get<eventGroupPos>(*eventData)); |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 163 | } |
| 164 | |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |