Matt Spinler | e10416e | 2017-04-10 14:15:53 -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 | */ |
Matt Spinler | 57352a3 | 2017-04-10 14:48:35 -0500 | [diff] [blame] | 16 | #include <algorithm> |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 17 | #include <phosphor-logging/log.hpp> |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 18 | #include <phosphor-logging/elog.hpp> |
| 19 | #include <phosphor-logging/elog-errors.hpp> |
| 20 | #include <xyz/openbmc_project/Common/error.hpp> |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 21 | #include <unistd.h> |
Matt Spinler | e10416e | 2017-04-10 14:15:53 -0500 | [diff] [blame] | 22 | #include "manager.hpp" |
Gunnar Mills | f96b01e | 2017-06-02 16:32:19 -0500 | [diff] [blame] | 23 | #include "utility.hpp" |
Matthew Barth | 803d35b | 2018-05-09 12:15:46 -0500 | [diff] [blame^] | 24 | #include "sdbusplus.hpp" |
Matt Spinler | e10416e | 2017-04-10 14:15:53 -0500 | [diff] [blame] | 25 | |
| 26 | namespace phosphor |
| 27 | { |
| 28 | namespace fan |
| 29 | { |
| 30 | namespace control |
| 31 | { |
| 32 | |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 33 | using namespace phosphor::logging; |
| 34 | |
| 35 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 36 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 37 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 38 | constexpr auto FAN_CONTROL_READY_TARGET = "obmc-fan-control-ready@0.target"; |
| 39 | |
Gunnar Mills | f96b01e | 2017-06-02 16:32:19 -0500 | [diff] [blame] | 40 | /** |
| 41 | * Check if a condition is true. Conditions are used to determine |
| 42 | * which fan zone to use. |
| 43 | * |
| 44 | * @param[in] bus - The D-Bus bus object |
| 45 | * @param[in] condition - The condition to check if true |
| 46 | * @return result - True if the condition is true |
| 47 | */ |
| 48 | bool checkCondition(sdbusplus::bus::bus& bus, const auto& c) |
| 49 | { |
| 50 | auto& type = std::get<conditionTypePos>(c); |
| 51 | auto& properties = std::get<conditionPropertyListPos>(c); |
| 52 | |
| 53 | for (auto& p : properties) |
| 54 | { |
Matthew Barth | 803d35b | 2018-05-09 12:15:46 -0500 | [diff] [blame^] | 55 | auto value = std::get<propertyValuePos>(p); |
Gunnar Mills | f96b01e | 2017-06-02 16:32:19 -0500 | [diff] [blame] | 56 | |
| 57 | // TODO openbmc/openbmc#1769: Support more types than just getProperty. |
| 58 | if (type.compare("getProperty") == 0) |
| 59 | { |
Matthew Barth | 803d35b | 2018-05-09 12:15:46 -0500 | [diff] [blame^] | 60 | auto propertyValue = util::SDBusPlus::getProperty<decltype(value)>( |
| 61 | bus, |
| 62 | std::get<propertyPathPos>(p), |
| 63 | std::get<propertyInterfacePos>(p), |
| 64 | std::get<propertyNamePos>(p)); |
Gunnar Mills | f96b01e | 2017-06-02 16:32:19 -0500 | [diff] [blame] | 65 | |
| 66 | if (value != propertyValue) |
| 67 | { |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 76 | //Note: Future code will check 'mode' before starting control algorithm |
| 77 | Manager::Manager(sdbusplus::bus::bus& bus, |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 78 | phosphor::fan::event::EventPtr& events, |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 79 | Mode mode) : |
Matt Spinler | e10416e | 2017-04-10 14:15:53 -0500 | [diff] [blame] | 80 | _bus(bus) |
| 81 | { |
Matt Spinler | 57352a3 | 2017-04-10 14:48:35 -0500 | [diff] [blame] | 82 | //Create the appropriate Zone objects based on the |
| 83 | //actual system configuration. |
| 84 | |
| 85 | //Find the 1 ZoneGroup that meets all of its conditions |
| 86 | for (auto& group : _zoneLayouts) |
| 87 | { |
| 88 | auto& conditions = std::get<conditionListPos>(group); |
| 89 | |
| 90 | if (std::all_of(conditions.begin(), conditions.end(), |
Gunnar Mills | f96b01e | 2017-06-02 16:32:19 -0500 | [diff] [blame] | 91 | [&bus](const auto& condition) |
| 92 | { |
| 93 | return checkCondition(bus, condition); |
| 94 | })) |
Matt Spinler | 57352a3 | 2017-04-10 14:48:35 -0500 | [diff] [blame] | 95 | { |
| 96 | //Create a Zone object for each zone in this group |
| 97 | auto& zones = std::get<zoneListPos>(group); |
| 98 | |
| 99 | for (auto& z : zones) |
| 100 | { |
| 101 | _zones.emplace(std::get<zoneNumPos>(z), |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 102 | std::make_unique<Zone>(mode, _bus, events, z)); |
Matt Spinler | 57352a3 | 2017-04-10 14:48:35 -0500 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
| 112 | void Manager::doInit() |
| 113 | { |
Gunnar Mills | f96b01e | 2017-06-02 16:32:19 -0500 | [diff] [blame] | 114 | for (auto& z : _zones) |
Matt Spinler | 57352a3 | 2017-04-10 14:48:35 -0500 | [diff] [blame] | 115 | { |
| 116 | z.second->setFullSpeed(); |
| 117 | } |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 118 | |
| 119 | auto delay = _powerOnDelay; |
| 120 | while (delay > 0) |
| 121 | { |
| 122 | delay = sleep(delay); |
| 123 | } |
| 124 | |
| 125 | startFanControlReadyTarget(); |
Matt Spinler | e10416e | 2017-04-10 14:15:53 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 129 | void Manager::startFanControlReadyTarget() |
| 130 | { |
| 131 | auto method = _bus.new_method_call(SYSTEMD_SERVICE, |
Gunnar Mills | f96b01e | 2017-06-02 16:32:19 -0500 | [diff] [blame] | 132 | SYSTEMD_OBJ_PATH, |
| 133 | SYSTEMD_INTERFACE, |
| 134 | "StartUnit"); |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 135 | |
| 136 | method.append(FAN_CONTROL_READY_TARGET); |
| 137 | method.append("replace"); |
| 138 | |
| 139 | auto response = _bus.call(method); |
| 140 | if (response.is_method_error()) |
| 141 | { |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 142 | log<level::ERR>("Failed to start fan control ready target"); |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 143 | elog<InternalFailure>(); |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Gunnar Mills | f96b01e | 2017-06-02 16:32:19 -0500 | [diff] [blame] | 147 | } // namespace control |
| 148 | } // namespace fan |
| 149 | } // namespace phosphor |