Matthew Barth | fcfa052 | 2020-08-24 16:40:24 -0500 | [diff] [blame] | 1 | /** |
Mike Capps | 70c26b2 | 2021-10-07 15:24:29 -0400 | [diff] [blame] | 2 | * Copyright © 2022 IBM Corporation |
Matthew Barth | fcfa052 | 2020-08-24 16:40:24 -0500 | [diff] [blame] | 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 | */ |
| 16 | #include "fan.hpp" |
| 17 | |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 18 | #include "sdbusplus.hpp" |
| 19 | |
Matthew Barth | fcfa052 | 2020-08-24 16:40:24 -0500 | [diff] [blame] | 20 | #include <nlohmann/json.hpp> |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 21 | #include <phosphor-logging/lg2.hpp> |
Matthew Barth | fcfa052 | 2020-08-24 16:40:24 -0500 | [diff] [blame] | 22 | #include <sdbusplus/bus.hpp> |
| 23 | |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 24 | #include <format> |
| 25 | |
Matthew Barth | fcfa052 | 2020-08-24 16:40:24 -0500 | [diff] [blame] | 26 | namespace phosphor::fan::control::json |
| 27 | { |
| 28 | |
| 29 | using json = nlohmann::json; |
| 30 | |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 31 | constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/"; |
| 32 | constexpr auto FAN_TARGET_PROPERTY = "Target"; |
| 33 | |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 34 | Fan::Fan(const json& jsonObj) : |
| 35 | ConfigBase(jsonObj), _bus(util::SDBusPlus::getBus()) |
Matthew Barth | bff1080 | 2020-08-25 10:07:57 -0500 | [diff] [blame] | 36 | { |
Matthew Barth | ce95726 | 2020-08-27 10:35:57 -0500 | [diff] [blame] | 37 | setInterface(jsonObj); |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 38 | setSensors(jsonObj); |
| 39 | setZone(jsonObj); |
| 40 | } |
| 41 | |
| 42 | void Fan::setInterface(const json& jsonObj) |
| 43 | { |
| 44 | if (!jsonObj.contains("target_interface")) |
| 45 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 46 | lg2::error("Missing required fan sensor target interface", "JSON", |
| 47 | jsonObj.dump()); |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 48 | throw std::runtime_error( |
| 49 | "Missing required fan sensor target interface"); |
| 50 | } |
| 51 | _interface = jsonObj["target_interface"].get<std::string>(); |
| 52 | } |
| 53 | |
| 54 | void Fan::setSensors(const json& jsonObj) |
| 55 | { |
| 56 | if (!jsonObj.contains("sensors")) |
| 57 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 58 | lg2::error("Missing required fan sensors list", "JSON", jsonObj.dump()); |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 59 | throw std::runtime_error("Missing required fan sensors list"); |
| 60 | } |
| 61 | std::string path; |
| 62 | for (const auto& sensor : jsonObj["sensors"]) |
| 63 | { |
Chau Ly | 44872b0 | 2022-09-19 07:34:08 +0000 | [diff] [blame] | 64 | if (!jsonObj.contains("target_path")) |
| 65 | { |
| 66 | // If target_path is not set in configuration, |
| 67 | // it is default to /xyz/openbmc_project/sensors/fan_tach/ |
| 68 | path = FAN_SENSOR_PATH + sensor.get<std::string>(); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | path = jsonObj["target_path"].get<std::string>() + |
| 73 | sensor.get<std::string>(); |
| 74 | } |
| 75 | |
Matt Spinler | b5fda8d | 2025-07-24 10:23:10 -0500 | [diff] [blame] | 76 | std::string service; |
| 77 | int attempts = 0; |
| 78 | constexpr int maxAttempts = 5; |
| 79 | while (true) |
| 80 | { |
| 81 | try |
| 82 | { |
| 83 | service = util::SDBusPlus::getService(_bus, path, _interface); |
| 84 | break; |
| 85 | } |
| 86 | catch (const std::exception&) |
| 87 | { |
| 88 | lg2::warning("No service for {PATH} {INTERFACE}", "PATH", path, |
| 89 | "INTERFACE", _interface); |
| 90 | attempts++; |
| 91 | if (attempts == maxAttempts) |
| 92 | { |
| 93 | lg2::error("Giving up"); |
| 94 | throw; |
| 95 | } |
| 96 | lg2::info("Retrying"); |
| 97 | std::this_thread::sleep_for(std::chrono::seconds(2)); |
| 98 | } |
| 99 | } |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 100 | _sensors[path] = service; |
| 101 | } |
Matthew Barth | e47c958 | 2021-03-09 14:24:02 -0600 | [diff] [blame] | 102 | // All sensors associated with this fan are set to the same target, |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 103 | // so only need to read target property from one of them |
| 104 | if (!path.empty()) |
| 105 | { |
| 106 | _target = util::SDBusPlus::getProperty<uint64_t>( |
| 107 | _bus, _sensors.at(path), path, _interface, FAN_TARGET_PROPERTY); |
| 108 | } |
Matthew Barth | bff1080 | 2020-08-25 10:07:57 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void Fan::setZone(const json& jsonObj) |
| 112 | { |
| 113 | if (!jsonObj.contains("zone")) |
| 114 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 115 | lg2::error("Missing required fan zone", "JSON", jsonObj.dump()); |
Matthew Barth | bff1080 | 2020-08-25 10:07:57 -0500 | [diff] [blame] | 116 | throw std::runtime_error("Missing required fan zone"); |
| 117 | } |
| 118 | _zone = jsonObj["zone"].get<std::string>(); |
| 119 | } |
| 120 | |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 121 | void Fan::setTarget(uint64_t target) |
Matthew Barth | bff1080 | 2020-08-25 10:07:57 -0500 | [diff] [blame] | 122 | { |
Mike Capps | 70c26b2 | 2021-10-07 15:24:29 -0400 | [diff] [blame] | 123 | if ((_target == target) || !_lockedTargets.empty()) |
Matthew Barth | 6f78730 | 2021-03-25 15:01:01 -0500 | [diff] [blame] | 124 | { |
| 125 | return; |
| 126 | } |
| 127 | |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 128 | for (const auto& sensor : _sensors) |
Matthew Barth | bff1080 | 2020-08-25 10:07:57 -0500 | [diff] [blame] | 129 | { |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 130 | auto value = target; |
| 131 | try |
| 132 | { |
| 133 | util::SDBusPlus::setProperty<uint64_t>( |
| 134 | _bus, sensor.second, sensor.first, _interface, |
| 135 | FAN_TARGET_PROPERTY, std::move(value)); |
| 136 | } |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 137 | catch (const sdbusplus::exception_t&) |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 138 | { |
| 139 | throw util::DBusPropertyError{ |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 140 | std::format("Failed to set target for fan {}", _name).c_str(), |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 141 | sensor.second, sensor.first, _interface, FAN_TARGET_PROPERTY}; |
| 142 | } |
Matthew Barth | bff1080 | 2020-08-25 10:07:57 -0500 | [diff] [blame] | 143 | } |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 144 | _target = target; |
Matthew Barth | bff1080 | 2020-08-25 10:07:57 -0500 | [diff] [blame] | 145 | } |
Matthew Barth | fcfa052 | 2020-08-24 16:40:24 -0500 | [diff] [blame] | 146 | |
Mike Capps | 70c26b2 | 2021-10-07 15:24:29 -0400 | [diff] [blame] | 147 | void Fan::lockTarget(uint64_t target) |
| 148 | { |
| 149 | // if multiple locks, take highest, else allow only the |
| 150 | // first lock to lower the target |
| 151 | if (target >= _target || _lockedTargets.empty()) |
| 152 | { |
| 153 | // setTarget wont work if any locked targets exist |
| 154 | decltype(_lockedTargets) temp; |
| 155 | _lockedTargets.swap(temp); |
| 156 | |
| 157 | setTarget(target); |
| 158 | _lockedTargets.swap(temp); |
| 159 | } |
| 160 | |
| 161 | _lockedTargets.push_back(target); |
| 162 | } |
| 163 | |
| 164 | void Fan::unlockTarget(uint64_t target) |
| 165 | { |
| 166 | // find and remove the requested lock |
Patrick Williams | 5e15c3b | 2023-10-20 11:18:11 -0500 | [diff] [blame] | 167 | auto itr(std::find_if( |
| 168 | _lockedTargets.begin(), _lockedTargets.end(), |
| 169 | [target](auto lockedTarget) { return target == lockedTarget; })); |
Mike Capps | 70c26b2 | 2021-10-07 15:24:29 -0400 | [diff] [blame] | 170 | |
| 171 | if (_lockedTargets.end() != itr) |
| 172 | { |
| 173 | _lockedTargets.erase(itr); |
| 174 | |
| 175 | // if additional locks, re-lock at next-highest target |
| 176 | if (!_lockedTargets.empty()) |
| 177 | { |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 178 | itr = |
| 179 | std::max_element(_lockedTargets.begin(), _lockedTargets.end()); |
Mike Capps | 70c26b2 | 2021-10-07 15:24:29 -0400 | [diff] [blame] | 180 | |
| 181 | // setTarget wont work if any locked targets exist |
| 182 | decltype(_lockedTargets) temp; |
| 183 | _lockedTargets.swap(temp); |
| 184 | setTarget(*itr); |
| 185 | _lockedTargets.swap(temp); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
Matthew Barth | fcfa052 | 2020-08-24 16:40:24 -0500 | [diff] [blame] | 190 | } // namespace phosphor::fan::control::json |