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