blob: afbab79db026f884233518cf6ba2928652c8c8ad [file] [log] [blame]
Matthew Barthfcfa0522020-08-24 16:40:24 -05001/**
Mike Capps70c26b22021-10-07 15:24:29 -04002 * Copyright © 2022 IBM Corporation
Matthew Barthfcfa0522020-08-24 16:40:24 -05003 *
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 Bartha3a8cc52021-01-15 12:40:25 -060018#include "sdbusplus.hpp"
19
Matthew Barthfcfa0522020-08-24 16:40:24 -050020#include <nlohmann/json.hpp>
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000021#include <phosphor-logging/lg2.hpp>
Matthew Barthfcfa0522020-08-24 16:40:24 -050022#include <sdbusplus/bus.hpp>
23
Patrick Williamsfbf47032023-07-17 12:27:34 -050024#include <format>
25
Matthew Barthfcfa0522020-08-24 16:40:24 -050026namespace phosphor::fan::control::json
27{
28
29using json = nlohmann::json;
30
Matthew Bartha3a8cc52021-01-15 12:40:25 -060031constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/";
32constexpr auto FAN_TARGET_PROPERTY = "Target";
33
Matthew Barth9403a212021-05-17 09:31:50 -050034Fan::Fan(const json& jsonObj) :
35 ConfigBase(jsonObj), _bus(util::SDBusPlus::getBus())
Matthew Barthbff10802020-08-25 10:07:57 -050036{
Matthew Barthce957262020-08-27 10:35:57 -050037 setInterface(jsonObj);
Matthew Bartha3a8cc52021-01-15 12:40:25 -060038 setSensors(jsonObj);
39 setZone(jsonObj);
40}
41
42void Fan::setInterface(const json& jsonObj)
43{
44 if (!jsonObj.contains("target_interface"))
45 {
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000046 lg2::error("Missing required fan sensor target interface", "JSON",
47 jsonObj.dump());
Matthew Bartha3a8cc52021-01-15 12:40:25 -060048 throw std::runtime_error(
49 "Missing required fan sensor target interface");
50 }
51 _interface = jsonObj["target_interface"].get<std::string>();
52}
53
54void Fan::setSensors(const json& jsonObj)
55{
56 if (!jsonObj.contains("sensors"))
57 {
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000058 lg2::error("Missing required fan sensors list", "JSON", jsonObj.dump());
Matthew Bartha3a8cc52021-01-15 12:40:25 -060059 throw std::runtime_error("Missing required fan sensors list");
60 }
61 std::string path;
62 for (const auto& sensor : jsonObj["sensors"])
63 {
Chau Ly44872b02022-09-19 07:34:08 +000064 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
Matthew Bartha3a8cc52021-01-15 12:40:25 -060076 auto service = util::SDBusPlus::getService(_bus, path, _interface);
77 _sensors[path] = service;
78 }
Matthew Barthe47c9582021-03-09 14:24:02 -060079 // All sensors associated with this fan are set to the same target,
Matthew Bartha3a8cc52021-01-15 12:40:25 -060080 // so only need to read target property from one of them
81 if (!path.empty())
82 {
83 _target = util::SDBusPlus::getProperty<uint64_t>(
84 _bus, _sensors.at(path), path, _interface, FAN_TARGET_PROPERTY);
85 }
Matthew Barthbff10802020-08-25 10:07:57 -050086}
87
88void Fan::setZone(const json& jsonObj)
89{
90 if (!jsonObj.contains("zone"))
91 {
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000092 lg2::error("Missing required fan zone", "JSON", jsonObj.dump());
Matthew Barthbff10802020-08-25 10:07:57 -050093 throw std::runtime_error("Missing required fan zone");
94 }
95 _zone = jsonObj["zone"].get<std::string>();
96}
97
Matthew Bartha3a8cc52021-01-15 12:40:25 -060098void Fan::setTarget(uint64_t target)
Matthew Barthbff10802020-08-25 10:07:57 -050099{
Mike Capps70c26b22021-10-07 15:24:29 -0400100 if ((_target == target) || !_lockedTargets.empty())
Matthew Barth6f787302021-03-25 15:01:01 -0500101 {
102 return;
103 }
104
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600105 for (const auto& sensor : _sensors)
Matthew Barthbff10802020-08-25 10:07:57 -0500106 {
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600107 auto value = target;
108 try
109 {
110 util::SDBusPlus::setProperty<uint64_t>(
111 _bus, sensor.second, sensor.first, _interface,
112 FAN_TARGET_PROPERTY, std::move(value));
113 }
Patrick Williamscb356d42022-07-22 19:26:53 -0500114 catch (const sdbusplus::exception_t&)
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600115 {
116 throw util::DBusPropertyError{
Patrick Williamsfbf47032023-07-17 12:27:34 -0500117 std::format("Failed to set target for fan {}", _name).c_str(),
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600118 sensor.second, sensor.first, _interface, FAN_TARGET_PROPERTY};
119 }
Matthew Barthbff10802020-08-25 10:07:57 -0500120 }
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600121 _target = target;
Matthew Barthbff10802020-08-25 10:07:57 -0500122}
Matthew Barthfcfa0522020-08-24 16:40:24 -0500123
Mike Capps70c26b22021-10-07 15:24:29 -0400124void Fan::lockTarget(uint64_t target)
125{
126 // if multiple locks, take highest, else allow only the
127 // first lock to lower the target
128 if (target >= _target || _lockedTargets.empty())
129 {
130 // setTarget wont work if any locked targets exist
131 decltype(_lockedTargets) temp;
132 _lockedTargets.swap(temp);
133
134 setTarget(target);
135 _lockedTargets.swap(temp);
136 }
137
138 _lockedTargets.push_back(target);
139}
140
141void Fan::unlockTarget(uint64_t target)
142{
143 // find and remove the requested lock
Patrick Williams5e15c3b2023-10-20 11:18:11 -0500144 auto itr(std::find_if(
145 _lockedTargets.begin(), _lockedTargets.end(),
146 [target](auto lockedTarget) { return target == lockedTarget; }));
Mike Capps70c26b22021-10-07 15:24:29 -0400147
148 if (_lockedTargets.end() != itr)
149 {
150 _lockedTargets.erase(itr);
151
152 // if additional locks, re-lock at next-highest target
153 if (!_lockedTargets.empty())
154 {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400155 itr =
156 std::max_element(_lockedTargets.begin(), _lockedTargets.end());
Mike Capps70c26b22021-10-07 15:24:29 -0400157
158 // setTarget wont work if any locked targets exist
159 decltype(_lockedTargets) temp;
160 _lockedTargets.swap(temp);
161 setTarget(*itr);
162 _lockedTargets.swap(temp);
163 }
164 }
165}
166
Matthew Barthfcfa0522020-08-24 16:40:24 -0500167} // namespace phosphor::fan::control::json