blob: abcb8aed0d60b1933f6fbb0cf0e1b2c731d4494f [file] [log] [blame]
James Feist7136a5a2018-07-19 09:52:05 -07001/*
2// Copyright (c) 2018 Intel 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*/
Pete O_o765a6d82025-07-23 21:44:14 -070016#include "config.h"
Chaul Lya552fe22024-11-15 10:20:28 +000017
Patrick Venture7e952d92020-10-05 15:58:52 -070018#include "dbusconfiguration.hpp"
James Feist7136a5a2018-07-19 09:52:05 -070019
Patrick Venture07716592018-10-14 11:46:40 -070020#include "conf.hpp"
Patrick Ventureef1f8862020-08-17 09:34:35 -070021#include "dbushelper.hpp"
22#include "dbusutil.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -070023#include "ec/stepwise.hpp"
James Feist0c8223b2019-05-08 15:33:33 -070024#include "util.hpp"
Patrick Venture07716592018-10-14 11:46:40 -070025
Ed Tanousf8b6e552025-06-27 13:27:50 -070026#include <systemd/sd-bus.h>
27
28#include <boost/asio/error.hpp>
James Feist1fe08952019-05-07 09:17:16 -070029#include <boost/asio/steady_timer.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070030#include <sdbusplus/bus.hpp>
31#include <sdbusplus/bus/match.hpp>
32#include <sdbusplus/exception.hpp>
Ed Tanousf8b6e552025-06-27 13:27:50 -070033#include <sdbusplus/message.hpp>
34#include <sdbusplus/message/native_types.hpp>
Alexander Hanseneb1a35c2025-11-12 16:41:34 +010035#include <xyz/openbmc_project/Association/Definitions/common.hpp>
36#include <xyz/openbmc_project/Association/common.hpp>
Alexander Hansenbd2c98e2025-11-12 12:47:00 +010037#include <xyz/openbmc_project/Control/FanPwm/client.hpp>
Alexander Hansend7665432025-11-12 14:01:48 +010038#include <xyz/openbmc_project/Control/ThermalMode/common.hpp>
Alexander Hansen1ec3d132025-10-27 17:19:50 +010039#include <xyz/openbmc_project/ObjectMapper/common.hpp>
Alexander Hansend3e71ce2025-11-12 16:20:53 +010040#include <xyz/openbmc_project/Sensor/Threshold/Critical/common.hpp>
41#include <xyz/openbmc_project/Sensor/Threshold/Warning/common.hpp>
Alexander Hansendae4a3a2025-11-11 17:12:01 +010042#include <xyz/openbmc_project/Sensor/Value/client.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070043
44#include <algorithm>
Ed Tanousf8b6e552025-06-27 13:27:50 -070045#include <array>
James Feist64f072a2018-08-10 16:39:24 -070046#include <chrono>
Ed Tanousf8b6e552025-06-27 13:27:50 -070047#include <cstdint>
48#include <format>
James Feist7136a5a2018-07-19 09:52:05 -070049#include <iostream>
Ed Tanousf8b6e552025-06-27 13:27:50 -070050#include <limits>
James Feist1fe08952019-05-07 09:17:16 -070051#include <list>
Ed Tanousf8b6e552025-06-27 13:27:50 -070052#include <map>
53#include <stdexcept>
54#include <string>
55#include <tuple>
James Feist7136a5a2018-07-19 09:52:05 -070056#include <unordered_map>
Ed Tanousf8b6e552025-06-27 13:27:50 -070057#include <utility>
James Feist1f802f52019-02-08 13:51:43 -080058#include <variant>
Ed Tanousf8b6e552025-06-27 13:27:50 -070059#include <vector>
James Feist7136a5a2018-07-19 09:52:05 -070060
Alexander Hansen1ec3d132025-10-27 17:19:50 +010061using ObjectMapper = sdbusplus::common::xyz::openbmc_project::ObjectMapper;
Alexander Hansendae4a3a2025-11-11 17:12:01 +010062using SensorValue = sdbusplus::common::xyz::openbmc_project::sensor::Value;
Alexander Hansenbd2c98e2025-11-12 12:47:00 +010063using ControlFanPwm = sdbusplus::common::xyz::openbmc_project::control::FanPwm;
Alexander Hansend7665432025-11-12 14:01:48 +010064using ControlThermalMode =
65 sdbusplus::common::xyz::openbmc_project::control::ThermalMode;
Alexander Hansend3e71ce2025-11-12 16:20:53 +010066using SensorThresholdWarning =
67 sdbusplus::common::xyz::openbmc_project::sensor::threshold::Warning;
68using SensorThresholdCritical =
69 sdbusplus::common::xyz::openbmc_project::sensor::threshold::Critical;
Alexander Hansen1ec3d132025-10-27 17:19:50 +010070
Patrick Venturea0764872020-08-08 07:48:43 -070071namespace pid_control
72{
73
Patrick Venturee2ec0f62018-09-04 12:30:27 -070074constexpr const char* pidConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070075 "xyz.openbmc_project.Configuration.Pid";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070076constexpr const char* objectManagerInterface =
James Feist7136a5a2018-07-19 09:52:05 -070077 "org.freedesktop.DBus.ObjectManager";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070078constexpr const char* pidZoneConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070079 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feist22c257a2018-08-31 14:07:12 -070080constexpr const char* stepwiseConfigurationInterface =
81 "xyz.openbmc_project.Configuration.Stepwise";
James Feist7136a5a2018-07-19 09:52:05 -070082
James Feist991ebd82020-07-21 11:14:52 -070083using Association = std::tuple<std::string, std::string, std::string>;
84using Associations = std::vector<Association>;
85
James Feist5ec20272019-07-10 11:59:57 -070086namespace thresholds
87{
James Feist5ec20272019-07-10 11:59:57 -070088const std::array<const char*, 4> types = {"CriticalLow", "CriticalHigh",
89 "WarningLow", "WarningHigh"};
90
91} // namespace thresholds
92
James Feist7136a5a2018-07-19 09:52:05 -070093namespace dbus_configuration
94{
Jason Lingf3b04fd2020-07-24 09:33:04 -070095using SensorInterfaceType = std::pair<std::string, std::string>;
96
97inline std::string getSensorNameFromPath(const std::string& dbusPath)
98{
Ed Tanousd2768c52025-06-26 11:42:57 -070099 return dbusPath.substr(dbusPath.find_last_of('/') + 1);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700100}
101
102inline std::string sensorNameToDbusName(const std::string& sensorName)
103{
104 std::string retString = sensorName;
105 std::replace(retString.begin(), retString.end(), ' ', '_');
106 return retString;
107}
James Feist5ec20272019-07-10 11:59:57 -0700108
Patrick Williamsb228bc32022-07-22 19:26:56 -0500109std::vector<std::string> getSelectedProfiles(sdbusplus::bus_t& bus)
James Feistf0096a02019-02-21 11:25:22 -0800110{
111 std::vector<std::string> ret;
Alexander Hansen1ec3d132025-10-27 17:19:50 +0100112 auto mapper = bus.new_method_call(
113 ObjectMapper::default_service, ObjectMapper::instance_path,
114 ObjectMapper::interface, ObjectMapper::method_names::get_sub_tree);
Alexander Hansend7665432025-11-12 14:01:48 +0100115 mapper.append("/", 0,
116 std::array<const char*, 1>{ControlThermalMode::interface});
James Feistf0096a02019-02-21 11:25:22 -0800117 std::unordered_map<
118 std::string, std::unordered_map<std::string, std::vector<std::string>>>
119 respData;
120
121 try
122 {
123 auto resp = bus.call(mapper);
124 resp.read(respData);
125 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500126 catch (const sdbusplus::exception_t&)
James Feistf0096a02019-02-21 11:25:22 -0800127 {
128 // can't do anything without mapper call data
129 throw std::runtime_error("ObjectMapper Call Failure");
130 }
131 if (respData.empty())
132 {
133 // if the user has profiles but doesn't expose the interface to select
134 // one, just go ahead without using profiles
135 return ret;
136 }
137
138 // assumption is that we should only have a small handful of selected
139 // profiles at a time (probably only 1), so calling each individually should
140 // not incur a large cost
141 for (const auto& objectPair : respData)
142 {
143 const std::string& path = objectPair.first;
144 for (const auto& ownerPair : objectPair.second)
145 {
146 const std::string& busName = ownerPair.first;
147 auto getProfile =
148 bus.new_method_call(busName.c_str(), path.c_str(),
149 "org.freedesktop.DBus.Properties", "Get");
Alexander Hansend7665432025-11-12 14:01:48 +0100150 getProfile.append(ControlThermalMode::interface,
151 ControlThermalMode::property_names::current);
James Feistf0096a02019-02-21 11:25:22 -0800152 std::variant<std::string> variantResp;
153 try
154 {
155 auto resp = bus.call(getProfile);
156 resp.read(variantResp);
157 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500158 catch (const sdbusplus::exception_t&)
James Feistf0096a02019-02-21 11:25:22 -0800159 {
160 throw std::runtime_error("Failure getting profile");
161 }
162 std::string mode = std::get<std::string>(variantResp);
163 ret.emplace_back(std::move(mode));
164 }
165 }
Patrick Venture39199b42020-10-08 14:40:29 -0700166 if constexpr (pid_control::conf::DEBUG)
James Feistf0096a02019-02-21 11:25:22 -0800167 {
168 std::cout << "Profiles selected: ";
169 for (const auto& profile : ret)
170 {
171 std::cout << profile << " ";
172 }
173 std::cout << "\n";
174 }
175 return ret;
176}
177
James Feist991ebd82020-07-21 11:14:52 -0700178int eventHandler(sd_bus_message* m, void* context, sd_bus_error*)
James Feist7136a5a2018-07-19 09:52:05 -0700179{
James Feist991ebd82020-07-21 11:14:52 -0700180 if (context == nullptr || m == nullptr)
James Feist1fe08952019-05-07 09:17:16 -0700181 {
182 throw std::runtime_error("Invalid match");
183 }
James Feist991ebd82020-07-21 11:14:52 -0700184
185 // we skip associations because the mapper populates these, not the sensors
Josh Lehan10e46ef2023-02-01 18:25:58 -0800186 const std::array<const char*, 2> skipList = {
Alexander Hanseneb1a35c2025-11-12 16:41:34 +0100187 sdbusplus::common::xyz::openbmc_project::Association::interface,
188 sdbusplus::common::xyz::openbmc_project::association::Definitions::
189 interface};
James Feist991ebd82020-07-21 11:14:52 -0700190
Patrick Williamsb228bc32022-07-22 19:26:56 -0500191 sdbusplus::message_t message(m);
James Feist991ebd82020-07-21 11:14:52 -0700192 if (std::string(message.get_member()) == "InterfacesAdded")
193 {
194 sdbusplus::message::object_path path;
195 std::unordered_map<
196 std::string,
197 std::unordered_map<std::string, std::variant<Associations, bool>>>
198 data;
199
200 message.read(path, data);
201
202 for (const char* skip : skipList)
203 {
204 auto find = data.find(skip);
205 if (find != data.end())
206 {
207 data.erase(find);
208 if (data.empty())
209 {
210 return 1;
211 }
212 }
213 }
Josh Lehan10e46ef2023-02-01 18:25:58 -0800214
215 if constexpr (pid_control::conf::DEBUG)
216 {
217 std::cout << "New config detected: " << path.str << std::endl;
218 for (auto& d : data)
219 {
220 std::cout << "\tdata is " << d.first << std::endl;
221 for (auto& second : d.second)
222 {
223 std::cout << "\t\tdata is " << second.first << std::endl;
224 }
225 }
226 }
James Feist991ebd82020-07-21 11:14:52 -0700227 }
228
James Feist1fe08952019-05-07 09:17:16 -0700229 boost::asio::steady_timer* timer =
230 static_cast<boost::asio::steady_timer*>(context);
231
232 // do a brief sleep as we tend to get a bunch of these events at
233 // once
234 timer->expires_after(std::chrono::seconds(2));
235 timer->async_wait([](const boost::system::error_code ec) {
236 if (ec == boost::asio::error::operation_aborted)
237 {
238 /* another timer started*/
239 return;
240 }
241
242 std::cout << "New configuration detected, reloading\n.";
Yong Li298a95c2020-04-07 15:11:02 +0800243 tryRestartControlLoops();
James Feist1fe08952019-05-07 09:17:16 -0700244 });
245
246 return 1;
247}
248
Patrick Williamsb228bc32022-07-22 19:26:56 -0500249void createMatches(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer)
James Feist1fe08952019-05-07 09:17:16 -0700250{
251 // this is a list because the matches can't be moved
Patrick Williamsb228bc32022-07-22 19:26:56 -0500252 static std::list<sdbusplus::bus::match_t> matches;
James Feist1fe08952019-05-07 09:17:16 -0700253
James Feist3987c8b2019-05-13 10:43:17 -0700254 const std::array<std::string, 4> interfaces = {
Alexander Hansend7665432025-11-12 14:01:48 +0100255 ControlThermalMode::interface, pidConfigurationInterface,
James Feist3987c8b2019-05-13 10:43:17 -0700256 pidZoneConfigurationInterface, stepwiseConfigurationInterface};
James Feist1fe08952019-05-07 09:17:16 -0700257
258 // this list only needs to be created once
259 if (!matches.empty())
260 {
261 return;
262 }
263
264 // we restart when the configuration changes or there are new sensors
265 for (const auto& interface : interfaces)
266 {
267 matches.emplace_back(
268 bus,
269 "type='signal',member='PropertiesChanged',arg0namespace='" +
270 interface + "'",
271 eventHandler, &timer);
272 }
273 matches.emplace_back(
274 bus,
275 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
276 "sensors/'",
277 eventHandler, &timer);
Jinliang Wangc2a311b2023-04-26 18:36:56 +0000278 matches.emplace_back(bus,
279 "type='signal',member='InterfacesRemoved',arg0path='/"
280 "xyz/openbmc_project/sensors/'",
281 eventHandler, &timer);
James Feist1fe08952019-05-07 09:17:16 -0700282}
283
Jason Ling6fc301f2020-07-23 12:39:57 -0700284/**
285 * retrieve an attribute from the pid configuration map
286 * @param[in] base - the PID configuration map, keys are the attributes and
287 * value is the variant associated with that attribute.
288 * @param attributeName - the name of the attribute
289 * @return a variant holding the value associated with a key
290 * @throw runtime_error : attributeName is not in base
291 */
292inline DbusVariantType getPIDAttribute(
293 const std::unordered_map<std::string, DbusVariantType>& base,
294 const std::string& attributeName)
295{
296 auto search = base.find(attributeName);
297 if (search == base.end())
298 {
299 throw std::runtime_error("missing attribute " + attributeName);
300 }
301 return search->second;
302}
303
Harvey Wu239aa7d2022-11-18 08:43:34 +0800304inline void getCycleTimeSetting(
305 const std::unordered_map<std::string, DbusVariantType>& zone,
306 const int zoneIndex, const std::string& attributeName, uint64_t& value)
307{
308 auto findAttributeName = zone.find(attributeName);
309 if (findAttributeName != zone.end())
310 {
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400311 double tmpAttributeValue =
312 std::visit(VariantToDoubleVisitor(), zone.at(attributeName));
Harvey Wu239aa7d2022-11-18 08:43:34 +0800313 if (tmpAttributeValue >= 1.0)
314 {
315 value = static_cast<uint64_t>(tmpAttributeValue);
316 }
317 else
318 {
319 std::cerr << "Zone " << zoneIndex << ": " << attributeName
320 << " is invalid. Use default " << value << " ms\n";
321 }
322 }
323 else
324 {
325 std::cerr << "Zone " << zoneIndex << ": " << attributeName
326 << " cannot find setting. Use default " << value << " ms\n";
327 }
328}
329
James Feist5ec20272019-07-10 11:59:57 -0700330void populatePidInfo(
Patrick Williamscd1e78a2025-04-07 17:21:05 -0400331 sdbusplus::bus_t& bus,
James Feist5ec20272019-07-10 11:59:57 -0700332 const std::unordered_map<std::string, DbusVariantType>& base,
Patrick Venture1df9e872020-10-08 15:35:01 -0700333 conf::ControllerInfo& info, const std::string* thresholdProperty,
Patrick Venture73823182020-10-08 15:12:51 -0700334 const std::map<std::string, conf::SensorConfig>& sensorConfig)
James Feist5ec20272019-07-10 11:59:57 -0700335{
Jason Ling6fc301f2020-07-23 12:39:57 -0700336 info.type = std::get<std::string>(getPIDAttribute(base, "Class"));
James Feist5ec20272019-07-10 11:59:57 -0700337 if (info.type == "fan")
338 {
339 info.setpoint = 0;
340 }
341 else
342 {
Jason Ling6fc301f2020-07-23 12:39:57 -0700343 info.setpoint = std::visit(VariantToDoubleVisitor(),
344 getPIDAttribute(base, "SetPoint"));
James Feist5ec20272019-07-10 11:59:57 -0700345 }
346
ykchiu9fe3a3c2023-05-11 13:43:54 +0800347 int failsafepercent = 0;
348 auto findFailSafe = base.find("FailSafePercent");
349 if (findFailSafe != base.end())
350 {
351 failsafepercent = std::visit(VariantToDoubleVisitor(),
352 getPIDAttribute(base, "FailSafePercent"));
353 }
354 info.failSafePercent = failsafepercent;
355
James Feist5ec20272019-07-10 11:59:57 -0700356 if (thresholdProperty != nullptr)
357 {
358 std::string interface;
Alexander Hansend3e71ce2025-11-12 16:20:53 +0100359 if (*thresholdProperty ==
360 SensorThresholdWarning::property_names::warning_high ||
361 *thresholdProperty ==
362 SensorThresholdWarning::property_names::warning_low)
James Feist5ec20272019-07-10 11:59:57 -0700363 {
Alexander Hansend3e71ce2025-11-12 16:20:53 +0100364 interface = SensorThresholdWarning::interface;
James Feist5ec20272019-07-10 11:59:57 -0700365 }
366 else
367 {
Alexander Hansend3e71ce2025-11-12 16:20:53 +0100368 interface = SensorThresholdCritical::interface;
James Feist5ec20272019-07-10 11:59:57 -0700369 }
Josh Lehan31058fd2023-01-13 11:06:16 -0800370
371 // Although this checks only the first vector element for the
372 // named threshold, it is OK, because the SetPointOffset parser
373 // splits up the input into individual vectors, each with only a
374 // single element, if it detects that SetPointOffset is in use.
375 const std::string& path =
376 sensorConfig.at(info.inputs.front().name).readPath;
James Feist5ec20272019-07-10 11:59:57 -0700377
Patrick Williamscd1e78a2025-04-07 17:21:05 -0400378 DbusHelper helper(bus);
Patrick Venture9b936922020-08-10 11:28:39 -0700379 std::string service = helper.getService(interface, path);
James Feist5ec20272019-07-10 11:59:57 -0700380 double reading = 0;
381 try
382 {
Patrick Venture9b936922020-08-10 11:28:39 -0700383 helper.getProperty(service, path, interface, *thresholdProperty,
384 reading);
James Feist5ec20272019-07-10 11:59:57 -0700385 }
Patrick Williamsb228bc32022-07-22 19:26:56 -0500386 catch (const sdbusplus::exception_t& ex)
James Feist5ec20272019-07-10 11:59:57 -0700387 {
388 // unsupported threshold, leaving reading at 0
389 }
390
391 info.setpoint += reading;
392 }
393
394 info.pidInfo.ts = 1.0; // currently unused
Jason Ling6fc301f2020-07-23 12:39:57 -0700395 info.pidInfo.proportionalCoeff = std::visit(
396 VariantToDoubleVisitor(), getPIDAttribute(base, "PCoefficient"));
397 info.pidInfo.integralCoeff = std::visit(
398 VariantToDoubleVisitor(), getPIDAttribute(base, "ICoefficient"));
Josh Lehanc612c052022-12-12 09:56:47 -0800399 // DCoefficient is below, it is optional, same reason as in buildjson.cpp
Jason Ling6fc301f2020-07-23 12:39:57 -0700400 info.pidInfo.feedFwdOffset = std::visit(
401 VariantToDoubleVisitor(), getPIDAttribute(base, "FFOffCoefficient"));
402 info.pidInfo.feedFwdGain = std::visit(
403 VariantToDoubleVisitor(), getPIDAttribute(base, "FFGainCoefficient"));
404 info.pidInfo.integralLimit.max = std::visit(
405 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMax"));
406 info.pidInfo.integralLimit.min = std::visit(
407 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMin"));
408 info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(),
409 getPIDAttribute(base, "OutLimitMax"));
410 info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(),
411 getPIDAttribute(base, "OutLimitMin"));
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400412 info.pidInfo.slewNeg =
413 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewNeg"));
414 info.pidInfo.slewPos =
415 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewPos"));
Josh Lehanc612c052022-12-12 09:56:47 -0800416
Delphine CC Chiu97889632023-11-06 11:32:46 +0800417 bool checkHysterWithSetpt = false;
James Feist5ec20272019-07-10 11:59:57 -0700418 double negativeHysteresis = 0;
419 double positiveHysteresis = 0;
Josh Lehanc612c052022-12-12 09:56:47 -0800420 double derivativeCoeff = 0;
James Feist5ec20272019-07-10 11:59:57 -0700421
Delphine CC Chiu5d897e22024-06-04 13:33:22 +0800422 auto findCheckHysterFlag = base.find("CheckHysteresisWithSetpoint");
James Feist5ec20272019-07-10 11:59:57 -0700423 auto findNeg = base.find("NegativeHysteresis");
424 auto findPos = base.find("PositiveHysteresis");
Josh Lehanc612c052022-12-12 09:56:47 -0800425 auto findDerivative = base.find("DCoefficient");
James Feist5ec20272019-07-10 11:59:57 -0700426
Delphine CC Chiu97889632023-11-06 11:32:46 +0800427 if (findCheckHysterFlag != base.end())
428 {
429 checkHysterWithSetpt = std::get<bool>(findCheckHysterFlag->second);
430 }
James Feist5ec20272019-07-10 11:59:57 -0700431 if (findNeg != base.end())
432 {
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400433 negativeHysteresis =
434 std::visit(VariantToDoubleVisitor(), findNeg->second);
James Feist5ec20272019-07-10 11:59:57 -0700435 }
James Feist5ec20272019-07-10 11:59:57 -0700436 if (findPos != base.end())
437 {
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400438 positiveHysteresis =
439 std::visit(VariantToDoubleVisitor(), findPos->second);
James Feist5ec20272019-07-10 11:59:57 -0700440 }
Josh Lehanc612c052022-12-12 09:56:47 -0800441 if (findDerivative != base.end())
442 {
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400443 derivativeCoeff =
444 std::visit(VariantToDoubleVisitor(), findDerivative->second);
Josh Lehanc612c052022-12-12 09:56:47 -0800445 }
446
Delphine CC Chiu97889632023-11-06 11:32:46 +0800447 info.pidInfo.checkHysterWithSetpt = checkHysterWithSetpt;
James Feist5ec20272019-07-10 11:59:57 -0700448 info.pidInfo.negativeHysteresis = negativeHysteresis;
449 info.pidInfo.positiveHysteresis = positiveHysteresis;
Josh Lehanc612c052022-12-12 09:56:47 -0800450 info.pidInfo.derivativeCoeff = derivativeCoeff;
James Feist5ec20272019-07-10 11:59:57 -0700451}
452
Patrick Williamsb228bc32022-07-22 19:26:56 -0500453bool init(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer,
Patrick Venture73823182020-10-08 15:12:51 -0700454 std::map<std::string, conf::SensorConfig>& sensorConfig,
455 std::map<int64_t, conf::PIDConf>& zoneConfig,
456 std::map<int64_t, conf::ZoneConfig>& zoneDetailsConfig)
James Feist1fe08952019-05-07 09:17:16 -0700457{
James Feist1fe08952019-05-07 09:17:16 -0700458 sensorConfig.clear();
459 zoneConfig.clear();
460 zoneDetailsConfig.clear();
461
462 createMatches(bus, timer);
463
Alexander Hansen1ec3d132025-10-27 17:19:50 +0100464 auto mapper = bus.new_method_call(
465 ObjectMapper::default_service, ObjectMapper::instance_path,
466 ObjectMapper::interface, ObjectMapper::method_names::get_sub_tree);
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400467 mapper.append(
468 "/", 0,
469 std::array<const char*, 6>{
470 objectManagerInterface, pidConfigurationInterface,
471 pidZoneConfigurationInterface, stepwiseConfigurationInterface,
Alexander Hansenbd2c98e2025-11-12 12:47:00 +0100472 SensorValue::interface, ControlFanPwm::interface});
James Feist7136a5a2018-07-19 09:52:05 -0700473 std::unordered_map<
474 std::string, std::unordered_map<std::string, std::vector<std::string>>>
475 respData;
James Feist22c257a2018-08-31 14:07:12 -0700476 try
477 {
478 auto resp = bus.call(mapper);
James Feist22c257a2018-08-31 14:07:12 -0700479 resp.read(respData);
480 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500481 catch (const sdbusplus::exception_t&)
James Feist22c257a2018-08-31 14:07:12 -0700482 {
483 // can't do anything without mapper call data
484 throw std::runtime_error("ObjectMapper Call Failure");
485 }
James Feist7136a5a2018-07-19 09:52:05 -0700486
James Feist7136a5a2018-07-19 09:52:05 -0700487 if (respData.empty())
488 {
James Feist22c257a2018-08-31 14:07:12 -0700489 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700490 throw std::runtime_error("No configuration data available from Mapper");
491 }
492 // create a map of pair of <has pid configuration, ObjectManager path>
493 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
494 // and a map of <path, interface> for sensors
495 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700496 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700497 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700498 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700499 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700500 auto& owner = owners[ownerPair.first];
501 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700502 {
James Feist7136a5a2018-07-19 09:52:05 -0700503 if (interface == objectManagerInterface)
504 {
505 owner.second = objectPair.first;
506 }
507 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700508 interface == pidZoneConfigurationInterface ||
509 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700510 {
511 owner.first = true;
512 }
Alexander Hansendae4a3a2025-11-11 17:12:01 +0100513 if (interface == SensorValue::interface ||
Alexander Hansenbd2c98e2025-11-12 12:47:00 +0100514 interface == ControlFanPwm::interface)
James Feist7136a5a2018-07-19 09:52:05 -0700515 {
516 // we're not interested in pwm sensors, just pwm control
Alexander Hansendae4a3a2025-11-11 17:12:01 +0100517 if (interface == SensorValue::interface &&
James Feist7136a5a2018-07-19 09:52:05 -0700518 objectPair.first.find("pwm") != std::string::npos)
519 {
520 continue;
521 }
522 sensors[objectPair.first] = interface;
523 }
524 }
525 }
526 }
527 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700528 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700529 {
530 // skip if no pid configuration (means probably a sensor)
531 if (!owner.second.first)
532 {
533 continue;
534 }
535 auto endpoint = bus.new_method_call(
536 owner.first.c_str(), owner.second.second.c_str(),
537 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700538 ManagedObjectType configuration;
539 try
James Feist7136a5a2018-07-19 09:52:05 -0700540 {
Manojkiran Eda7ca88872024-06-17 11:55:48 +0530541 auto response = bus.call(endpoint);
542 response.read(configuration);
James Feist22c257a2018-08-31 14:07:12 -0700543 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500544 catch (const sdbusplus::exception_t&)
James Feist22c257a2018-08-31 14:07:12 -0700545 {
546 // this shouldn't happen, probably means daemon crashed
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400547 throw std::runtime_error(
548 "Error getting managed objects from " + owner.first);
James Feist7136a5a2018-07-19 09:52:05 -0700549 }
James Feist22c257a2018-08-31 14:07:12 -0700550
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700551 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700552 {
553 if (pathPair.second.find(pidConfigurationInterface) !=
554 pathPair.second.end() ||
555 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700556 pathPair.second.end() ||
557 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700558 pathPair.second.end())
559 {
560 configurations.emplace(pathPair);
561 }
James Feistf0096a02019-02-21 11:25:22 -0800562 }
563 }
564
565 // remove controllers from config that aren't in the current profile(s)
James Feist3987c8b2019-05-13 10:43:17 -0700566 std::vector<std::string> selectedProfiles = getSelectedProfiles(bus);
567 if (selectedProfiles.size())
James Feistf0096a02019-02-21 11:25:22 -0800568 {
James Feist3987c8b2019-05-13 10:43:17 -0700569 for (auto pathIt = configurations.begin();
570 pathIt != configurations.end();)
James Feistf0096a02019-02-21 11:25:22 -0800571 {
James Feist3987c8b2019-05-13 10:43:17 -0700572 for (auto confIt = pathIt->second.begin();
573 confIt != pathIt->second.end();)
James Feistf0096a02019-02-21 11:25:22 -0800574 {
James Feist3987c8b2019-05-13 10:43:17 -0700575 auto profilesFind = confIt->second.find("Profiles");
576 if (profilesFind == confIt->second.end())
James Feistf0096a02019-02-21 11:25:22 -0800577 {
James Feist3987c8b2019-05-13 10:43:17 -0700578 confIt++;
579 continue; // if no profiles selected, apply always
580 }
581 auto profiles =
582 std::get<std::vector<std::string>>(profilesFind->second);
583 if (profiles.empty())
584 {
585 confIt++;
586 continue;
587 }
588
589 bool found = false;
590 for (const std::string& profile : profiles)
591 {
592 if (std::find(selectedProfiles.begin(),
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400593 selectedProfiles.end(), profile) !=
594 selectedProfiles.end())
James Feist3987c8b2019-05-13 10:43:17 -0700595 {
596 found = true;
597 break;
598 }
599 }
600 if (found)
601 {
602 confIt++;
James Feistf0096a02019-02-21 11:25:22 -0800603 }
604 else
605 {
James Feist3987c8b2019-05-13 10:43:17 -0700606 confIt = pathIt->second.erase(confIt);
James Feistf0096a02019-02-21 11:25:22 -0800607 }
608 }
James Feist3987c8b2019-05-13 10:43:17 -0700609 if (pathIt->second.empty())
James Feistf0096a02019-02-21 11:25:22 -0800610 {
James Feist3987c8b2019-05-13 10:43:17 -0700611 pathIt = configurations.erase(pathIt);
James Feistf0096a02019-02-21 11:25:22 -0800612 }
James Feist3987c8b2019-05-13 10:43:17 -0700613 else
James Feistf0096a02019-02-21 11:25:22 -0800614 {
James Feist3987c8b2019-05-13 10:43:17 -0700615 pathIt++;
James Feistf0096a02019-02-21 11:25:22 -0800616 }
James Feist7136a5a2018-07-19 09:52:05 -0700617 }
618 }
James Feist8c3c51e2018-08-08 16:31:43 -0700619
Josh Lehan998fbe62020-09-20 21:21:05 -0700620 // On D-Bus, although not necessary,
621 // having the "zoneID" field can still be useful,
622 // as it is used for diagnostic messages,
623 // logging file names, and so on.
624 // Accept optional "ZoneIndex" parameter to explicitly specify.
625 // If not present, or not unique, auto-assign index,
626 // using 0-based numbering, ensuring uniqueness.
627 std::map<std::string, int64_t> foundZones;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700628 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700629 {
630 auto findZone =
631 configuration.second.find(pidZoneConfigurationInterface);
632 if (findZone != configuration.second.end())
633 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700634 const auto& zone = findZone->second;
James Feistffd418b2018-11-15 14:46:36 -0800635
James Feist1f802f52019-02-08 13:51:43 -0800636 const std::string& name = std::get<std::string>(zone.at("Name"));
Josh Lehan998fbe62020-09-20 21:21:05 -0700637
638 auto findZoneIndex = zone.find("ZoneIndex");
639 if (findZoneIndex == zone.end())
640 {
641 continue;
642 }
643
644 auto ptrZoneIndex = std::get_if<double>(&(findZoneIndex->second));
645 if (!ptrZoneIndex)
646 {
647 continue;
648 }
649
650 auto desiredIndex = static_cast<int64_t>(*ptrZoneIndex);
651 auto grantedIndex = setZoneIndex(name, foundZones, desiredIndex);
652 std::cout << "Zone " << name << " is at ZoneIndex " << grantedIndex
653 << "\n";
654 }
655 }
656
657 for (const auto& configuration : configurations)
658 {
659 auto findZone =
660 configuration.second.find(pidZoneConfigurationInterface);
661 if (findZone != configuration.second.end())
662 {
663 const auto& zone = findZone->second;
664
665 const std::string& name = std::get<std::string>(zone.at("Name"));
666
667 auto index = getZoneIndex(name, foundZones);
James Feist8c3c51e2018-08-08 16:31:43 -0700668
Patrick Venturec54fbd82018-10-30 19:40:05 -0700669 auto& details = zoneDetailsConfig[index];
Josh Lehan998fbe62020-09-20 21:21:05 -0700670
James Feist3484bed2019-02-25 13:28:18 -0800671 details.minThermalOutput = std::visit(VariantToDoubleVisitor(),
672 zone.at("MinThermalOutput"));
ykchiu9fe3a3c2023-05-11 13:43:54 +0800673
674 int failsafepercent = 0;
675 auto findFailSafe = zone.find("FailSafePercent");
676 if (findFailSafe != zone.end())
677 {
678 failsafepercent = std::visit(VariantToDoubleVisitor(),
679 zone.at("FailSafePercent"));
680 }
681 details.failsafePercent = failsafepercent;
Josh Lehan9f9a06a2022-12-14 10:39:45 -0800682
Harvey Wu239aa7d2022-11-18 08:43:34 +0800683 getCycleTimeSetting(zone, index, "CycleIntervalTimeMS",
684 details.cycleTime.cycleIntervalTimeMS);
685 getCycleTimeSetting(zone, index, "UpdateThermalsTimeMS",
686 details.cycleTime.updateThermalsTimeMS);
Delphine CC Chiu97889632023-11-06 11:32:46 +0800687
688 bool accumulateSetPoint = false;
689 auto findAccSetPoint = zone.find("AccumulateSetPoint");
690 if (findAccSetPoint != zone.end())
691 {
692 accumulateSetPoint = std::get<bool>(findAccSetPoint->second);
693 }
694 details.accumulateSetPoint = accumulateSetPoint;
James Feist7136a5a2018-07-19 09:52:05 -0700695 }
696 auto findBase = configuration.second.find(pidConfigurationInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700697 // loop through all the PID configurations and fill out a sensor config
James Feist22c257a2018-08-31 14:07:12 -0700698 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700699 {
James Feist22c257a2018-08-31 14:07:12 -0700700 const auto& base =
701 configuration.second.at(pidConfigurationInterface);
ykchiu7c6d35d2023-05-10 17:01:46 +0800702 const std::string pidName =
703 sensorNameToDbusName(std::get<std::string>(base.at("Name")));
Jason Lingf3b04fd2020-07-24 09:33:04 -0700704 const std::string pidClass =
705 std::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700706 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800707 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700708 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700709 {
Josh Lehan998fbe62020-09-20 21:21:05 -0700710 auto index = getZoneIndex(zone, foundZones);
711
James Feistf81f2882019-02-26 11:26:36 -0800712 conf::PIDConf& conf = zoneConfig[index];
Jason Lingf3b04fd2020-07-24 09:33:04 -0700713 std::vector<std::string> inputSensorNames(
714 std::get<std::vector<std::string>>(base.at("Inputs")));
715 std::vector<std::string> outputSensorNames;
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800716 std::vector<std::string> missingAcceptableSensorNames;
Chaul Lya552fe22024-11-15 10:20:28 +0000717 std::vector<std::string> archivedInputSensorNames;
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800718
719 auto findMissingAcceptable = base.find("MissingIsAcceptable");
720 if (findMissingAcceptable != base.end())
721 {
722 missingAcceptableSensorNames =
723 std::get<std::vector<std::string>>(
724 findMissingAcceptable->second);
725 }
James Feist50fdfe32018-09-24 15:51:09 -0700726
Jason Lingf3b04fd2020-07-24 09:33:04 -0700727 // assumption: all fan pids must have at least one output
728 if (pidClass == "fan")
James Feist50fdfe32018-09-24 15:51:09 -0700729 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700730 outputSensorNames = std::get<std::vector<std::string>>(
731 getPIDAttribute(base, "Outputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700732 }
James Feist1738e2a2019-02-04 15:57:03 -0800733
Alex.Song8f73ad72021-10-07 00:18:27 +0800734 bool unavailableAsFailed = true;
735 auto findUnavailableAsFailed =
736 base.find("InputUnavailableAsFailed");
737 if (findUnavailableAsFailed != base.end())
738 {
739 unavailableAsFailed =
740 std::get<bool>(findUnavailableAsFailed->second);
741 }
742
Jason Lingf3b04fd2020-07-24 09:33:04 -0700743 std::vector<SensorInterfaceType> inputSensorInterfaces;
744 std::vector<SensorInterfaceType> outputSensorInterfaces;
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800745 std::vector<SensorInterfaceType>
746 missingAcceptableSensorInterfaces;
747
Jason Lingf3b04fd2020-07-24 09:33:04 -0700748 /* populate an interface list for different sensor direction
749 * types (input,output)
750 */
751 /* take the Inputs from the configuration and generate
752 * a list of dbus descriptors (path, interface).
753 * Mapping can be many-to-one since an element of Inputs can be
754 * a regex
755 */
756 for (const std::string& sensorName : inputSensorNames)
James Feist50fdfe32018-09-24 15:51:09 -0700757 {
Chaul Lya552fe22024-11-15 10:20:28 +0000758#ifndef HANDLE_MISSING_OBJECT_PATHS
Jason Lingf3b04fd2020-07-24 09:33:04 -0700759 findSensors(sensors, sensorNameToDbusName(sensorName),
760 inputSensorInterfaces);
Chaul Lya552fe22024-11-15 10:20:28 +0000761#else
762 std::vector<std::pair<std::string, std::string>>
763 sensorPathIfacePairs;
764 auto found =
765 findSensors(sensors, sensorNameToDbusName(sensorName),
766 sensorPathIfacePairs);
767 if (found)
768 {
769 inputSensorInterfaces.insert(
770 inputSensorInterfaces.end(),
771 sensorPathIfacePairs.begin(),
772 sensorPathIfacePairs.end());
773 }
774 else if (pidClass != "fan")
775 {
776 if (std::find(missingAcceptableSensorNames.begin(),
777 missingAcceptableSensorNames.end(),
778 sensorName) ==
779 missingAcceptableSensorNames.end())
780 {
781 std::cerr
782 << "Pid controller: Missing a missing-unacceptable sensor from D-Bus "
783 << sensorName << "\n";
784 std::string inputSensorName =
785 sensorNameToDbusName(sensorName);
786 auto& config = sensorConfig[inputSensorName];
787 archivedInputSensorNames.push_back(inputSensorName);
788 config.type = pidClass;
789 config.readPath =
790 getSensorPath(config.type, inputSensorName);
791 config.timeout = 0;
792 config.ignoreDbusMinMax = true;
793 config.unavailableAsFailed = unavailableAsFailed;
794 }
795 else
796 {
797 // When an input sensor is NOT on DBus, and it's in
798 // the MissingIsAcceptable list. Ignore it and
799 // continue with the next input sensor.
800 std::cout
801 << "Pid controller: Missing a missing-acceptable sensor from D-Bus "
802 << sensorName << "\n";
803 continue;
804 }
805 }
806#endif
Jason Lingf3b04fd2020-07-24 09:33:04 -0700807 }
808 for (const std::string& sensorName : outputSensorNames)
809 {
810 findSensors(sensors, sensorNameToDbusName(sensorName),
811 outputSensorInterfaces);
James Feist1738e2a2019-02-04 15:57:03 -0800812 }
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800813 for (const std::string& sensorName :
814 missingAcceptableSensorNames)
815 {
816 findSensors(sensors, sensorNameToDbusName(sensorName),
817 missingAcceptableSensorInterfaces);
818 }
James Feist50fdfe32018-09-24 15:51:09 -0700819
Jason Lingf3b04fd2020-07-24 09:33:04 -0700820 for (const SensorInterfaceType& inputSensorInterface :
821 inputSensorInterfaces)
James Feist1738e2a2019-02-04 15:57:03 -0800822 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700823 const std::string& dbusInterface =
824 inputSensorInterface.second;
825 const std::string& inputSensorPath =
826 inputSensorInterface.first;
Josh Lehanfb82a872020-09-20 21:48:22 -0700827
828 // Setting timeout to 0 is intentional, as D-Bus passive
829 // sensor updates are pushed in, not pulled by timer poll.
830 // Setting ignoreDbusMinMax is intentional, as this
831 // prevents normalization of values to [0.0, 1.0] range,
832 // which would mess up the PID loop math.
833 // All non-fan PID classes should be initialized this way.
834 // As for why a fan should not use this code path, see
835 // the ed1dafdf168def37c65bfb7a5efd18d9dbe04727 commit.
Josh Lehan23e22b92022-11-12 22:37:58 -0800836 if ((pidClass == "temp") || (pidClass == "margin") ||
837 (pidClass == "power") || (pidClass == "powersum"))
James Feist50fdfe32018-09-24 15:51:09 -0700838 {
Harvey.Wued1dafd2022-02-09 13:53:20 +0800839 std::string inputSensorName =
840 getSensorNameFromPath(inputSensorPath);
841 auto& config = sensorConfig[inputSensorName];
Chaul Lya552fe22024-11-15 10:20:28 +0000842 archivedInputSensorNames.push_back(inputSensorName);
Harvey.Wued1dafd2022-02-09 13:53:20 +0800843 config.type = pidClass;
844 config.readPath = inputSensorInterface.first;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700845 config.timeout = 0;
846 config.ignoreDbusMinMax = true;
Alex.Song8f73ad72021-10-07 00:18:27 +0800847 config.unavailableAsFailed = unavailableAsFailed;
James Feist50fdfe32018-09-24 15:51:09 -0700848 }
Josh Lehanfb82a872020-09-20 21:48:22 -0700849
Alexander Hansendae4a3a2025-11-11 17:12:01 +0100850 if (dbusInterface != SensorValue::interface)
James Feist50fdfe32018-09-24 15:51:09 -0700851 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700852 /* all expected inputs in the configuration are expected
853 * to be sensor interfaces
854 */
Ed Tanous7d6e2252025-06-27 10:45:25 -0700855 throw std::runtime_error(std::format(
856 "sensor at dbus path [{}] has an interface [{}] that does not match the expected interface of {}",
Alexander Hansendae4a3a2025-11-11 17:12:01 +0100857 inputSensorPath, dbusInterface,
858 SensorValue::interface));
James Feist50fdfe32018-09-24 15:51:09 -0700859 }
860 }
James Feist1738e2a2019-02-04 15:57:03 -0800861
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800862 // MissingIsAcceptable same postprocessing as Inputs
863 missingAcceptableSensorNames.clear();
864 for (const SensorInterfaceType&
865 missingAcceptableSensorInterface :
866 missingAcceptableSensorInterfaces)
867 {
868 const std::string& dbusInterface =
869 missingAcceptableSensorInterface.second;
870 const std::string& missingAcceptableSensorPath =
871 missingAcceptableSensorInterface.first;
872
873 std::string missingAcceptableSensorName =
874 getSensorNameFromPath(missingAcceptableSensorPath);
875 missingAcceptableSensorNames.push_back(
876 missingAcceptableSensorName);
877
Alexander Hansendae4a3a2025-11-11 17:12:01 +0100878 if (dbusInterface != SensorValue::interface)
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800879 {
880 /* MissingIsAcceptable same error checking as Inputs
881 */
Ed Tanous7d6e2252025-06-27 10:45:25 -0700882 throw std::runtime_error(std::format(
883 "sensor at dbus path [{}] has an interface [{}] that does not match the expected interface of {}",
884 missingAcceptableSensorPath, dbusInterface,
Alexander Hansendae4a3a2025-11-11 17:12:01 +0100885 SensorValue::interface));
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800886 }
887 }
888
Jason Lingf3b04fd2020-07-24 09:33:04 -0700889 /* fan pids need to pair up tach sensors with their pwm
890 * counterparts
891 */
892 if (pidClass == "fan")
893 {
894 /* If a PID is a fan there should be either
895 * (1) one output(pwm) per input(tach)
896 * OR
897 * (2) one putput(pwm) for all inputs(tach)
898 * everything else indicates a bad configuration.
899 */
900 bool singlePwm = false;
901 if (outputSensorInterfaces.size() == 1)
902 {
903 /* one pwm, set write paths for all fan sensors to it */
904 singlePwm = true;
905 }
906 else if (inputSensorInterfaces.size() ==
907 outputSensorInterfaces.size())
908 {
909 /* one to one mapping, each fan sensor gets its own pwm
910 * control */
911 singlePwm = false;
912 }
913 else
914 {
915 throw std::runtime_error(
916 "fan PID has invalid number of Outputs");
917 }
918 std::string fanSensorName;
919 std::string pwmPath;
920 std::string pwmInterface;
Harvey.Wued1dafd2022-02-09 13:53:20 +0800921 std::string pwmSensorName;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700922 if (singlePwm)
923 {
924 /* if just a single output(pwm) is provided then use
925 * that pwm control path for all the fan sensor write
926 * path configs
927 */
928 pwmPath = outputSensorInterfaces.at(0).first;
929 pwmInterface = outputSensorInterfaces.at(0).second;
930 }
931 for (uint32_t idx = 0; idx < inputSensorInterfaces.size();
932 idx++)
933 {
934 if (!singlePwm)
935 {
936 pwmPath = outputSensorInterfaces.at(idx).first;
937 pwmInterface =
938 outputSensorInterfaces.at(idx).second;
939 }
Alexander Hansenbd2c98e2025-11-12 12:47:00 +0100940 if (ControlFanPwm::interface != pwmInterface)
Jason Lingf3b04fd2020-07-24 09:33:04 -0700941 {
Ed Tanous7d6e2252025-06-27 10:45:25 -0700942 throw std::runtime_error(std::format(
943 "fan pwm control at dbus path [{}] has an interface [{}] that does not match the expected interface of {}",
Alexander Hansenbd2c98e2025-11-12 12:47:00 +0100944 pwmPath, pwmInterface,
945 ControlFanPwm::interface));
Jason Lingf3b04fd2020-07-24 09:33:04 -0700946 }
947 const std::string& fanPath =
948 inputSensorInterfaces.at(idx).first;
949 fanSensorName = getSensorNameFromPath(fanPath);
Harvey.Wued1dafd2022-02-09 13:53:20 +0800950 pwmSensorName = getSensorNameFromPath(pwmPath);
951 std::string fanPwmIndex = fanSensorName + pwmSensorName;
Chaul Lya552fe22024-11-15 10:20:28 +0000952 archivedInputSensorNames.push_back(fanPwmIndex);
Harvey.Wued1dafd2022-02-09 13:53:20 +0800953 auto& fanConfig = sensorConfig[fanPwmIndex];
954 fanConfig.type = pidClass;
955 fanConfig.readPath = fanPath;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700956 fanConfig.writePath = pwmPath;
957 // todo: un-hardcode this if there are fans with
958 // different ranges
959 fanConfig.max = 255;
960 fanConfig.min = 0;
961 }
962 }
James Feist11d243d2019-06-24 16:18:40 -0700963 // if the sensors aren't available in the current state, don't
964 // add them to the configuration.
Chaul Lya552fe22024-11-15 10:20:28 +0000965 if (archivedInputSensorNames.empty())
James Feist11d243d2019-06-24 16:18:40 -0700966 {
967 continue;
968 }
969
James Feist5ec20272019-07-10 11:59:57 -0700970 std::string offsetType;
James Feist50fdfe32018-09-24 15:51:09 -0700971
James Feist5ec20272019-07-10 11:59:57 -0700972 // SetPointOffset is a threshold value to pull from the sensor
973 // to apply an offset. For upper thresholds this means the
974 // setpoint is usually negative.
975 auto findSetpointOffset = base.find("SetPointOffset");
976 if (findSetpointOffset != base.end())
James Feist22c257a2018-08-31 14:07:12 -0700977 {
James Feist5ec20272019-07-10 11:59:57 -0700978 offsetType =
979 std::get<std::string>(findSetpointOffset->second);
980 if (std::find(thresholds::types.begin(),
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400981 thresholds::types.end(), offsetType) ==
982 thresholds::types.end())
James Feist5ec20272019-07-10 11:59:57 -0700983 {
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400984 throw std::runtime_error(
985 "Unsupported type: " + offsetType);
James Feist5ec20272019-07-10 11:59:57 -0700986 }
987 }
988
Josh Lehan31058fd2023-01-13 11:06:16 -0800989 std::vector<double> inputTempToMargin;
990
991 auto findTempToMargin = base.find("TempToMargin");
992 if (findTempToMargin != base.end())
993 {
994 inputTempToMargin =
995 std::get<std::vector<double>>(findTempToMargin->second);
996 }
997
998 std::vector<pid_control::conf::SensorInput> sensorInputs =
Chaul Lya552fe22024-11-15 10:20:28 +0000999 spliceInputs(archivedInputSensorNames, inputTempToMargin,
Josh Lehan3f0f7bc2023-02-13 01:45:29 -08001000 missingAcceptableSensorNames);
Josh Lehan31058fd2023-01-13 11:06:16 -08001001
James Feist5ec20272019-07-10 11:59:57 -07001002 if (offsetType.empty())
1003 {
ykchiu7c6d35d2023-05-10 17:01:46 +08001004 conf::ControllerInfo& info = conf[pidName];
Josh Lehan31058fd2023-01-13 11:06:16 -08001005 info.inputs = std::move(sensorInputs);
Patrick Venture73823182020-10-08 15:12:51 -07001006 populatePidInfo(bus, base, info, nullptr, sensorConfig);
James Feist22c257a2018-08-31 14:07:12 -07001007 }
1008 else
1009 {
James Feist5ec20272019-07-10 11:59:57 -07001010 // we have to split up the inputs, as in practice t-control
1011 // values will differ, making setpoints differ
Josh Lehan31058fd2023-01-13 11:06:16 -08001012 for (const pid_control::conf::SensorInput& input :
1013 sensorInputs)
James Feist5ec20272019-07-10 11:59:57 -07001014 {
Josh Lehan31058fd2023-01-13 11:06:16 -08001015 conf::ControllerInfo& info = conf[input.name];
James Feist5ec20272019-07-10 11:59:57 -07001016 info.inputs.emplace_back(input);
Patrick Venture73823182020-10-08 15:12:51 -07001017 populatePidInfo(bus, base, info, &offsetType,
1018 sensorConfig);
James Feist5ec20272019-07-10 11:59:57 -07001019 }
James Feist22c257a2018-08-31 14:07:12 -07001020 }
James Feist22c257a2018-08-31 14:07:12 -07001021 }
1022 }
1023 auto findStepwise =
1024 configuration.second.find(stepwiseConfigurationInterface);
1025 if (findStepwise != configuration.second.end())
1026 {
1027 const auto& base = findStepwise->second;
ykchiu7c6d35d2023-05-10 17:01:46 +08001028 const std::string pidName =
1029 sensorNameToDbusName(std::get<std::string>(base.at("Name")));
James Feist22c257a2018-08-31 14:07:12 -07001030 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -08001031 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -07001032 for (const std::string& zone : zones)
1033 {
Josh Lehan998fbe62020-09-20 21:21:05 -07001034 auto index = getZoneIndex(zone, foundZones);
1035
James Feistf81f2882019-02-26 11:26:36 -08001036 conf::PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -07001037
1038 std::vector<std::string> inputs;
Josh Lehan3f0f7bc2023-02-13 01:45:29 -08001039 std::vector<std::string> missingAcceptableSensors;
1040 std::vector<std::string> missingAcceptableSensorNames;
James Feist50fdfe32018-09-24 15:51:09 -07001041 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -08001042 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -07001043
Josh Lehan3f0f7bc2023-02-13 01:45:29 -08001044 auto findMissingAcceptable = base.find("MissingIsAcceptable");
1045 if (findMissingAcceptable != base.end())
1046 {
1047 missingAcceptableSensorNames =
1048 std::get<std::vector<std::string>>(
1049 findMissingAcceptable->second);
1050 }
1051
Alex.Song8f73ad72021-10-07 00:18:27 +08001052 bool unavailableAsFailed = true;
1053 auto findUnavailableAsFailed =
1054 base.find("InputUnavailableAsFailed");
1055 if (findUnavailableAsFailed != base.end())
1056 {
1057 unavailableAsFailed =
1058 std::get<bool>(findUnavailableAsFailed->second);
1059 }
1060
James Feist1738e2a2019-02-04 15:57:03 -08001061 bool sensorFound = false;
James Feist50fdfe32018-09-24 15:51:09 -07001062 for (const std::string& sensorName : sensorNames)
1063 {
James Feist1738e2a2019-02-04 15:57:03 -08001064 std::vector<std::pair<std::string, std::string>>
1065 sensorPathIfacePairs;
Jason Lingf3b04fd2020-07-24 09:33:04 -07001066 if (!findSensors(sensors, sensorNameToDbusName(sensorName),
1067 sensorPathIfacePairs))
James Feist50fdfe32018-09-24 15:51:09 -07001068 {
Chaul Lya552fe22024-11-15 10:20:28 +00001069#ifndef HANDLE_MISSING_OBJECT_PATHS
James Feist50fdfe32018-09-24 15:51:09 -07001070 break;
Chaul Lya552fe22024-11-15 10:20:28 +00001071#else
1072 if (std::find(missingAcceptableSensorNames.begin(),
1073 missingAcceptableSensorNames.end(),
1074 sensorName) ==
1075 missingAcceptableSensorNames.end())
1076 {
1077 // When an input sensor is NOT on DBus, and it's NOT
1078 // in the MissingIsAcceptable list. Build it as a
1079 // failed sensor with default information (temp
1080 // sensor path, temp type, ...)
1081 std::cerr
1082 << "Stepwise controller: Missing a missing-unacceptable sensor from D-Bus "
1083 << sensorName << "\n";
1084 std::string shortName =
1085 sensorNameToDbusName(sensorName);
1086
1087 inputs.push_back(shortName);
1088 auto& config = sensorConfig[shortName];
1089 config.type = "temp";
1090 config.readPath =
1091 getSensorPath(config.type, shortName);
1092 config.ignoreDbusMinMax = true;
1093 config.unavailableAsFailed = unavailableAsFailed;
1094 // todo: maybe un-hardcode this if we run into
1095 // slower timeouts with sensors
1096
1097 config.timeout = 0;
1098 sensorFound = true;
1099 }
1100 else
1101 {
1102 // When an input sensor is NOT on DBus, and it's in
1103 // the MissingIsAcceptable list. Ignore it and
1104 // continue with the next input sensor.
1105 std::cout
1106 << "Stepwise controller: Missing a missing-acceptable sensor from D-Bus "
1107 << sensorName << "\n";
1108 continue;
1109 }
1110#endif
James Feist50fdfe32018-09-24 15:51:09 -07001111 }
Chaul Lya552fe22024-11-15 10:20:28 +00001112 else
James Feist1738e2a2019-02-04 15:57:03 -08001113 {
Chaul Lya552fe22024-11-15 10:20:28 +00001114 for (const auto& sensorPathIfacePair :
1115 sensorPathIfacePairs)
1116 {
1117 std::string shortName = getSensorNameFromPath(
1118 sensorPathIfacePair.first);
James Feist50fdfe32018-09-24 15:51:09 -07001119
Chaul Lya552fe22024-11-15 10:20:28 +00001120 inputs.push_back(shortName);
1121 auto& config = sensorConfig[shortName];
1122 config.readPath = sensorPathIfacePair.first;
1123 config.type = "temp";
1124 config.ignoreDbusMinMax = true;
1125 config.unavailableAsFailed = unavailableAsFailed;
1126 // todo: maybe un-hardcode this if we run into
1127 // slower timeouts with sensors
James Feist1738e2a2019-02-04 15:57:03 -08001128
Chaul Lya552fe22024-11-15 10:20:28 +00001129 config.timeout = 0;
1130 sensorFound = true;
1131 }
James Feist1738e2a2019-02-04 15:57:03 -08001132 }
James Feist50fdfe32018-09-24 15:51:09 -07001133 }
1134 if (!sensorFound)
1135 {
1136 continue;
1137 }
Josh Lehan3f0f7bc2023-02-13 01:45:29 -08001138
1139 // MissingIsAcceptable same postprocessing as Inputs
1140 for (const std::string& missingAcceptableSensorName :
1141 missingAcceptableSensorNames)
1142 {
1143 std::vector<std::pair<std::string, std::string>>
1144 sensorPathIfacePairs;
1145 if (!findSensors(
1146 sensors,
1147 sensorNameToDbusName(missingAcceptableSensorName),
1148 sensorPathIfacePairs))
1149 {
Chaul Lya552fe22024-11-15 10:20:28 +00001150#ifndef HANDLE_MISSING_OBJECT_PATHS
Josh Lehan3f0f7bc2023-02-13 01:45:29 -08001151 break;
Chaul Lya552fe22024-11-15 10:20:28 +00001152#else
1153 // When a sensor in the MissingIsAcceptable list is NOT
1154 // on DBus and it still reaches here, which contradicts
1155 // to what we did in the Input sensor building step.
1156 // Continue.
1157 continue;
1158#endif
Josh Lehan3f0f7bc2023-02-13 01:45:29 -08001159 }
1160
1161 for (const auto& sensorPathIfacePair : sensorPathIfacePairs)
1162 {
1163 std::string shortName =
1164 getSensorNameFromPath(sensorPathIfacePair.first);
1165
1166 missingAcceptableSensors.push_back(shortName);
1167 }
1168 }
1169
ykchiu7c6d35d2023-05-10 17:01:46 +08001170 conf::ControllerInfo& info = conf[pidName];
Josh Lehan31058fd2023-01-13 11:06:16 -08001171
1172 std::vector<double> inputTempToMargin;
1173
1174 auto findTempToMargin = base.find("TempToMargin");
1175 if (findTempToMargin != base.end())
1176 {
1177 inputTempToMargin =
1178 std::get<std::vector<double>>(findTempToMargin->second);
1179 }
1180
Josh Lehan3f0f7bc2023-02-13 01:45:29 -08001181 info.inputs = spliceInputs(inputs, inputTempToMargin,
1182 missingAcceptableSensors);
James Feist50fdfe32018-09-24 15:51:09 -07001183
James Feist22c257a2018-08-31 14:07:12 -07001184 info.type = "stepwise";
1185 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -07001186 info.stepwiseInfo.positiveHysteresis = 0.0;
1187 info.stepwiseInfo.negativeHysteresis = 0.0;
James Feist608304d2019-02-25 10:01:42 -08001188 std::string subtype = std::get<std::string>(base.at("Class"));
1189
1190 info.stepwiseInfo.isCeiling = (subtype == "Ceiling");
James Feist3dfaafd2018-09-20 15:46:58 -07001191 auto findPosHyst = base.find("PositiveHysteresis");
1192 auto findNegHyst = base.find("NegativeHysteresis");
1193 if (findPosHyst != base.end())
1194 {
James Feist1f802f52019-02-08 13:51:43 -08001195 info.stepwiseInfo.positiveHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -08001196 VariantToDoubleVisitor(), findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -07001197 }
1198 if (findNegHyst != base.end())
1199 {
James Feist5782ab82019-04-02 08:38:48 -07001200 info.stepwiseInfo.negativeHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -08001201 VariantToDoubleVisitor(), findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -07001202 }
James Feist22c257a2018-08-31 14:07:12 -07001203 std::vector<double> readings =
James Feist1f802f52019-02-08 13:51:43 -08001204 std::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -07001205 if (readings.size() > ec::maxStepwisePoints)
1206 {
1207 throw std::invalid_argument("Too many stepwise points.");
1208 }
1209 if (readings.empty())
1210 {
1211 throw std::invalid_argument(
1212 "Must have one stepwise point.");
1213 }
1214 std::copy(readings.begin(), readings.end(),
1215 info.stepwiseInfo.reading);
1216 if (readings.size() < ec::maxStepwisePoints)
1217 {
1218 info.stepwiseInfo.reading[readings.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -08001219 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -07001220 }
1221 std::vector<double> outputs =
James Feist1f802f52019-02-08 13:51:43 -08001222 std::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -07001223 if (readings.size() != outputs.size())
1224 {
1225 throw std::invalid_argument(
1226 "Outputs size must match readings");
1227 }
1228 std::copy(outputs.begin(), outputs.end(),
1229 info.stepwiseInfo.output);
1230 if (outputs.size() < ec::maxStepwisePoints)
1231 {
1232 info.stepwiseInfo.output[outputs.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -08001233 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -07001234 }
James Feist7136a5a2018-07-19 09:52:05 -07001235 }
1236 }
1237 }
Patrick Venture39199b42020-10-08 14:40:29 -07001238 if constexpr (pid_control::conf::DEBUG)
James Feist7136a5a2018-07-19 09:52:05 -07001239 {
Patrick Venture39199b42020-10-08 14:40:29 -07001240 debugPrint(sensorConfig, zoneConfig, zoneDetailsConfig);
James Feist7136a5a2018-07-19 09:52:05 -07001241 }
James Feistc959c422018-11-01 12:33:40 -07001242 if (zoneConfig.empty() || zoneDetailsConfig.empty())
James Feist50fdfe32018-09-24 15:51:09 -07001243 {
James Feist1fe08952019-05-07 09:17:16 -07001244 std::cerr
1245 << "No fan zones, application pausing until new configuration\n";
1246 return false;
James Feist50fdfe32018-09-24 15:51:09 -07001247 }
James Feist1fe08952019-05-07 09:17:16 -07001248 return true;
James Feist7136a5a2018-07-19 09:52:05 -07001249}
Patrick Venturea0764872020-08-08 07:48:43 -07001250
James Feist7136a5a2018-07-19 09:52:05 -07001251} // namespace dbus_configuration
Patrick Venturea0764872020-08-08 07:48:43 -07001252} // namespace pid_control