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