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