blob: a80800308707c80d0b899c59e9a40c99e09a564f [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>
Matthew Barthbff10802020-08-25 10:07:57 -050021#include <phosphor-logging/log.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;
Matthew Barthbff10802020-08-25 10:07:57 -050030using namespace phosphor::logging;
Matthew Barthfcfa0522020-08-24 16:40:24 -050031
Matthew Bartha3a8cc52021-01-15 12:40:25 -060032constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/";
33constexpr auto FAN_TARGET_PROPERTY = "Target";
34
Matthew Barth9403a212021-05-17 09:31:50 -050035Fan::Fan(const json& jsonObj) :
36 ConfigBase(jsonObj), _bus(util::SDBusPlus::getBus())
Matthew Barthbff10802020-08-25 10:07:57 -050037{
Matthew Barthce957262020-08-27 10:35:57 -050038 setInterface(jsonObj);
Matthew Bartha3a8cc52021-01-15 12:40:25 -060039 setSensors(jsonObj);
40 setZone(jsonObj);
41}
42
43void Fan::setInterface(const json& jsonObj)
44{
45 if (!jsonObj.contains("target_interface"))
46 {
47 log<level::ERR>("Missing required fan sensor target interface",
48 entry("JSON=%s", jsonObj.dump().c_str()));
49 throw std::runtime_error(
50 "Missing required fan sensor target interface");
51 }
52 _interface = jsonObj["target_interface"].get<std::string>();
53}
54
55void Fan::setSensors(const json& jsonObj)
56{
57 if (!jsonObj.contains("sensors"))
58 {
59 log<level::ERR>("Missing required fan sensors list",
60 entry("JSON=%s", jsonObj.dump().c_str()));
61 throw std::runtime_error("Missing required fan sensors list");
62 }
63 std::string path;
64 for (const auto& sensor : jsonObj["sensors"])
65 {
Chau Ly44872b02022-09-19 07:34:08 +000066 if (!jsonObj.contains("target_path"))
67 {
68 // If target_path is not set in configuration,
69 // it is default to /xyz/openbmc_project/sensors/fan_tach/
70 path = FAN_SENSOR_PATH + sensor.get<std::string>();
71 }
72 else
73 {
74 path = jsonObj["target_path"].get<std::string>() +
75 sensor.get<std::string>();
76 }
77
Matthew Bartha3a8cc52021-01-15 12:40:25 -060078 auto service = util::SDBusPlus::getService(_bus, path, _interface);
79 _sensors[path] = service;
80 }
Matthew Barthe47c9582021-03-09 14:24:02 -060081 // All sensors associated with this fan are set to the same target,
Matthew Bartha3a8cc52021-01-15 12:40:25 -060082 // so only need to read target property from one of them
83 if (!path.empty())
84 {
85 _target = util::SDBusPlus::getProperty<uint64_t>(
86 _bus, _sensors.at(path), path, _interface, FAN_TARGET_PROPERTY);
87 }
Matthew Barthbff10802020-08-25 10:07:57 -050088}
89
90void Fan::setZone(const json& jsonObj)
91{
92 if (!jsonObj.contains("zone"))
93 {
94 log<level::ERR>("Missing required fan zone",
95 entry("JSON=%s", jsonObj.dump().c_str()));
96 throw std::runtime_error("Missing required fan zone");
97 }
98 _zone = jsonObj["zone"].get<std::string>();
99}
100
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600101void Fan::setTarget(uint64_t target)
Matthew Barthbff10802020-08-25 10:07:57 -0500102{
Mike Capps70c26b22021-10-07 15:24:29 -0400103 if ((_target == target) || !_lockedTargets.empty())
Matthew Barth6f787302021-03-25 15:01:01 -0500104 {
105 return;
106 }
107
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600108 for (const auto& sensor : _sensors)
Matthew Barthbff10802020-08-25 10:07:57 -0500109 {
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600110 auto value = target;
111 try
112 {
113 util::SDBusPlus::setProperty<uint64_t>(
114 _bus, sensor.second, sensor.first, _interface,
115 FAN_TARGET_PROPERTY, std::move(value));
116 }
Patrick Williamscb356d42022-07-22 19:26:53 -0500117 catch (const sdbusplus::exception_t&)
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600118 {
119 throw util::DBusPropertyError{
Patrick Williamsfbf47032023-07-17 12:27:34 -0500120 std::format("Failed to set target for fan {}", _name).c_str(),
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600121 sensor.second, sensor.first, _interface, FAN_TARGET_PROPERTY};
122 }
Matthew Barthbff10802020-08-25 10:07:57 -0500123 }
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600124 _target = target;
Matthew Barthbff10802020-08-25 10:07:57 -0500125}
Matthew Barthfcfa0522020-08-24 16:40:24 -0500126
Mike Capps70c26b22021-10-07 15:24:29 -0400127void Fan::lockTarget(uint64_t target)
128{
129 // if multiple locks, take highest, else allow only the
130 // first lock to lower the target
131 if (target >= _target || _lockedTargets.empty())
132 {
133 // setTarget wont work if any locked targets exist
134 decltype(_lockedTargets) temp;
135 _lockedTargets.swap(temp);
136
137 setTarget(target);
138 _lockedTargets.swap(temp);
139 }
140
141 _lockedTargets.push_back(target);
142}
143
144void Fan::unlockTarget(uint64_t target)
145{
146 // find and remove the requested lock
Patrick Williams5e15c3b2023-10-20 11:18:11 -0500147 auto itr(std::find_if(
148 _lockedTargets.begin(), _lockedTargets.end(),
149 [target](auto lockedTarget) { return target == lockedTarget; }));
Mike Capps70c26b22021-10-07 15:24:29 -0400150
151 if (_lockedTargets.end() != itr)
152 {
153 _lockedTargets.erase(itr);
154
155 // if additional locks, re-lock at next-highest target
156 if (!_lockedTargets.empty())
157 {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400158 itr =
159 std::max_element(_lockedTargets.begin(), _lockedTargets.end());
Mike Capps70c26b22021-10-07 15:24:29 -0400160
161 // setTarget wont work if any locked targets exist
162 decltype(_lockedTargets) temp;
163 _lockedTargets.swap(temp);
164 setTarget(*itr);
165 _lockedTargets.swap(temp);
166 }
167 }
168}
169
Matthew Barthfcfa0522020-08-24 16:40:24 -0500170} // namespace phosphor::fan::control::json