| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 1 | /* |
| 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_o | 765a6d8 | 2025-07-23 21:44:14 -0700 | [diff] [blame] | 16 | #include "config.h" |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 17 | |
| Patrick Venture | 7e952d9 | 2020-10-05 15:58:52 -0700 | [diff] [blame] | 18 | #include "dbusconfiguration.hpp" |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 19 | |
| Patrick Venture | 0771659 | 2018-10-14 11:46:40 -0700 | [diff] [blame] | 20 | #include "conf.hpp" |
| Patrick Venture | ef1f886 | 2020-08-17 09:34:35 -0700 | [diff] [blame] | 21 | #include "dbushelper.hpp" |
| 22 | #include "dbusutil.hpp" |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 23 | #include "ec/stepwise.hpp" |
| James Feist | 0c8223b | 2019-05-08 15:33:33 -0700 | [diff] [blame] | 24 | #include "util.hpp" |
| Patrick Venture | 0771659 | 2018-10-14 11:46:40 -0700 | [diff] [blame] | 25 | |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 26 | #include <systemd/sd-bus.h> |
| 27 | |
| 28 | #include <boost/asio/error.hpp> |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 29 | #include <boost/asio/steady_timer.hpp> |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 30 | #include <sdbusplus/bus.hpp> |
| 31 | #include <sdbusplus/bus/match.hpp> |
| 32 | #include <sdbusplus/exception.hpp> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 33 | #include <sdbusplus/message.hpp> |
| 34 | #include <sdbusplus/message/native_types.hpp> |
| Alexander Hansen | bd2c98e | 2025-11-12 12:47:00 +0100 | [diff] [blame] | 35 | #include <xyz/openbmc_project/Control/FanPwm/client.hpp> |
| Alexander Hansen | d766543 | 2025-11-12 14:01:48 +0100 | [diff] [blame] | 36 | #include <xyz/openbmc_project/Control/ThermalMode/common.hpp> |
| Alexander Hansen | 1ec3d13 | 2025-10-27 17:19:50 +0100 | [diff] [blame] | 37 | #include <xyz/openbmc_project/ObjectMapper/common.hpp> |
| Alexander Hansen | d3e71ce | 2025-11-12 16:20:53 +0100 | [diff] [blame^] | 38 | #include <xyz/openbmc_project/Sensor/Threshold/Critical/common.hpp> |
| 39 | #include <xyz/openbmc_project/Sensor/Threshold/Warning/common.hpp> |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame] | 40 | #include <xyz/openbmc_project/Sensor/Value/client.hpp> |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 41 | |
| 42 | #include <algorithm> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 43 | #include <array> |
| James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 44 | #include <chrono> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 45 | #include <cstdint> |
| 46 | #include <format> |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 47 | #include <iostream> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 48 | #include <limits> |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 49 | #include <list> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 50 | #include <map> |
| 51 | #include <stdexcept> |
| 52 | #include <string> |
| 53 | #include <tuple> |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 54 | #include <unordered_map> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 55 | #include <utility> |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 56 | #include <variant> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 57 | #include <vector> |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 58 | |
| Alexander Hansen | 1ec3d13 | 2025-10-27 17:19:50 +0100 | [diff] [blame] | 59 | using ObjectMapper = sdbusplus::common::xyz::openbmc_project::ObjectMapper; |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame] | 60 | using SensorValue = sdbusplus::common::xyz::openbmc_project::sensor::Value; |
| Alexander Hansen | bd2c98e | 2025-11-12 12:47:00 +0100 | [diff] [blame] | 61 | using ControlFanPwm = sdbusplus::common::xyz::openbmc_project::control::FanPwm; |
| Alexander Hansen | d766543 | 2025-11-12 14:01:48 +0100 | [diff] [blame] | 62 | using ControlThermalMode = |
| 63 | sdbusplus::common::xyz::openbmc_project::control::ThermalMode; |
| Alexander Hansen | d3e71ce | 2025-11-12 16:20:53 +0100 | [diff] [blame^] | 64 | using SensorThresholdWarning = |
| 65 | sdbusplus::common::xyz::openbmc_project::sensor::threshold::Warning; |
| 66 | using SensorThresholdCritical = |
| 67 | sdbusplus::common::xyz::openbmc_project::sensor::threshold::Critical; |
| Alexander Hansen | 1ec3d13 | 2025-10-27 17:19:50 +0100 | [diff] [blame] | 68 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 69 | namespace pid_control |
| 70 | { |
| 71 | |
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 72 | constexpr const char* pidConfigurationInterface = |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 73 | "xyz.openbmc_project.Configuration.Pid"; |
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 74 | constexpr const char* objectManagerInterface = |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 75 | "org.freedesktop.DBus.ObjectManager"; |
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 76 | constexpr const char* pidZoneConfigurationInterface = |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 77 | "xyz.openbmc_project.Configuration.Pid.Zone"; |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 78 | constexpr const char* stepwiseConfigurationInterface = |
| 79 | "xyz.openbmc_project.Configuration.Stepwise"; |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 80 | |
| James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 81 | using Association = std::tuple<std::string, std::string, std::string>; |
| 82 | using Associations = std::vector<Association>; |
| 83 | |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 84 | namespace thresholds |
| 85 | { |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 86 | const std::array<const char*, 4> types = {"CriticalLow", "CriticalHigh", |
| 87 | "WarningLow", "WarningHigh"}; |
| 88 | |
| 89 | } // namespace thresholds |
| 90 | |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 91 | namespace dbus_configuration |
| 92 | { |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 93 | using SensorInterfaceType = std::pair<std::string, std::string>; |
| 94 | |
| 95 | inline std::string getSensorNameFromPath(const std::string& dbusPath) |
| 96 | { |
| Ed Tanous | d2768c5 | 2025-06-26 11:42:57 -0700 | [diff] [blame] | 97 | return dbusPath.substr(dbusPath.find_last_of('/') + 1); |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | inline std::string sensorNameToDbusName(const std::string& sensorName) |
| 101 | { |
| 102 | std::string retString = sensorName; |
| 103 | std::replace(retString.begin(), retString.end(), ' ', '_'); |
| 104 | return retString; |
| 105 | } |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 106 | |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 107 | std::vector<std::string> getSelectedProfiles(sdbusplus::bus_t& bus) |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 108 | { |
| 109 | std::vector<std::string> ret; |
| Alexander Hansen | 1ec3d13 | 2025-10-27 17:19:50 +0100 | [diff] [blame] | 110 | auto mapper = bus.new_method_call( |
| 111 | ObjectMapper::default_service, ObjectMapper::instance_path, |
| 112 | ObjectMapper::interface, ObjectMapper::method_names::get_sub_tree); |
| Alexander Hansen | d766543 | 2025-11-12 14:01:48 +0100 | [diff] [blame] | 113 | mapper.append("/", 0, |
| 114 | std::array<const char*, 1>{ControlThermalMode::interface}); |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 115 | std::unordered_map< |
| 116 | std::string, std::unordered_map<std::string, std::vector<std::string>>> |
| 117 | respData; |
| 118 | |
| 119 | try |
| 120 | { |
| 121 | auto resp = bus.call(mapper); |
| 122 | resp.read(respData); |
| 123 | } |
| Patrick Williams | 0001ee0 | 2021-10-06 14:44:22 -0500 | [diff] [blame] | 124 | catch (const sdbusplus::exception_t&) |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 125 | { |
| 126 | // can't do anything without mapper call data |
| 127 | throw std::runtime_error("ObjectMapper Call Failure"); |
| 128 | } |
| 129 | if (respData.empty()) |
| 130 | { |
| 131 | // if the user has profiles but doesn't expose the interface to select |
| 132 | // one, just go ahead without using profiles |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | // assumption is that we should only have a small handful of selected |
| 137 | // profiles at a time (probably only 1), so calling each individually should |
| 138 | // not incur a large cost |
| 139 | for (const auto& objectPair : respData) |
| 140 | { |
| 141 | const std::string& path = objectPair.first; |
| 142 | for (const auto& ownerPair : objectPair.second) |
| 143 | { |
| 144 | const std::string& busName = ownerPair.first; |
| 145 | auto getProfile = |
| 146 | bus.new_method_call(busName.c_str(), path.c_str(), |
| 147 | "org.freedesktop.DBus.Properties", "Get"); |
| Alexander Hansen | d766543 | 2025-11-12 14:01:48 +0100 | [diff] [blame] | 148 | getProfile.append(ControlThermalMode::interface, |
| 149 | ControlThermalMode::property_names::current); |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 150 | std::variant<std::string> variantResp; |
| 151 | try |
| 152 | { |
| 153 | auto resp = bus.call(getProfile); |
| 154 | resp.read(variantResp); |
| 155 | } |
| Patrick Williams | 0001ee0 | 2021-10-06 14:44:22 -0500 | [diff] [blame] | 156 | catch (const sdbusplus::exception_t&) |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 157 | { |
| 158 | throw std::runtime_error("Failure getting profile"); |
| 159 | } |
| 160 | std::string mode = std::get<std::string>(variantResp); |
| 161 | ret.emplace_back(std::move(mode)); |
| 162 | } |
| 163 | } |
| Patrick Venture | 39199b4 | 2020-10-08 14:40:29 -0700 | [diff] [blame] | 164 | if constexpr (pid_control::conf::DEBUG) |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 165 | { |
| 166 | std::cout << "Profiles selected: "; |
| 167 | for (const auto& profile : ret) |
| 168 | { |
| 169 | std::cout << profile << " "; |
| 170 | } |
| 171 | std::cout << "\n"; |
| 172 | } |
| 173 | return ret; |
| 174 | } |
| 175 | |
| James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 176 | int eventHandler(sd_bus_message* m, void* context, sd_bus_error*) |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 177 | { |
| James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 178 | if (context == nullptr || m == nullptr) |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 179 | { |
| 180 | throw std::runtime_error("Invalid match"); |
| 181 | } |
| James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 182 | |
| 183 | // we skip associations because the mapper populates these, not the sensors |
| Josh Lehan | 10e46ef | 2023-02-01 18:25:58 -0800 | [diff] [blame] | 184 | const std::array<const char*, 2> skipList = { |
| 185 | "xyz.openbmc_project.Association", |
| 186 | "xyz.openbmc_project.Association.Definitions"}; |
| James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 187 | |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 188 | sdbusplus::message_t message(m); |
| James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 189 | if (std::string(message.get_member()) == "InterfacesAdded") |
| 190 | { |
| 191 | sdbusplus::message::object_path path; |
| 192 | std::unordered_map< |
| 193 | std::string, |
| 194 | std::unordered_map<std::string, std::variant<Associations, bool>>> |
| 195 | data; |
| 196 | |
| 197 | message.read(path, data); |
| 198 | |
| 199 | for (const char* skip : skipList) |
| 200 | { |
| 201 | auto find = data.find(skip); |
| 202 | if (find != data.end()) |
| 203 | { |
| 204 | data.erase(find); |
| 205 | if (data.empty()) |
| 206 | { |
| 207 | return 1; |
| 208 | } |
| 209 | } |
| 210 | } |
| Josh Lehan | 10e46ef | 2023-02-01 18:25:58 -0800 | [diff] [blame] | 211 | |
| 212 | if constexpr (pid_control::conf::DEBUG) |
| 213 | { |
| 214 | std::cout << "New config detected: " << path.str << std::endl; |
| 215 | for (auto& d : data) |
| 216 | { |
| 217 | std::cout << "\tdata is " << d.first << std::endl; |
| 218 | for (auto& second : d.second) |
| 219 | { |
| 220 | std::cout << "\t\tdata is " << second.first << std::endl; |
| 221 | } |
| 222 | } |
| 223 | } |
| James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 226 | boost::asio::steady_timer* timer = |
| 227 | static_cast<boost::asio::steady_timer*>(context); |
| 228 | |
| 229 | // do a brief sleep as we tend to get a bunch of these events at |
| 230 | // once |
| 231 | timer->expires_after(std::chrono::seconds(2)); |
| 232 | timer->async_wait([](const boost::system::error_code ec) { |
| 233 | if (ec == boost::asio::error::operation_aborted) |
| 234 | { |
| 235 | /* another timer started*/ |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | std::cout << "New configuration detected, reloading\n."; |
| Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 240 | tryRestartControlLoops(); |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 241 | }); |
| 242 | |
| 243 | return 1; |
| 244 | } |
| 245 | |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 246 | void createMatches(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer) |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 247 | { |
| 248 | // this is a list because the matches can't be moved |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 249 | static std::list<sdbusplus::bus::match_t> matches; |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 250 | |
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 251 | const std::array<std::string, 4> interfaces = { |
| Alexander Hansen | d766543 | 2025-11-12 14:01:48 +0100 | [diff] [blame] | 252 | ControlThermalMode::interface, pidConfigurationInterface, |
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 253 | pidZoneConfigurationInterface, stepwiseConfigurationInterface}; |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 254 | |
| 255 | // this list only needs to be created once |
| 256 | if (!matches.empty()) |
| 257 | { |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | // we restart when the configuration changes or there are new sensors |
| 262 | for (const auto& interface : interfaces) |
| 263 | { |
| 264 | matches.emplace_back( |
| 265 | bus, |
| 266 | "type='signal',member='PropertiesChanged',arg0namespace='" + |
| 267 | interface + "'", |
| 268 | eventHandler, &timer); |
| 269 | } |
| 270 | matches.emplace_back( |
| 271 | bus, |
| 272 | "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/" |
| 273 | "sensors/'", |
| 274 | eventHandler, &timer); |
| Jinliang Wang | c2a311b | 2023-04-26 18:36:56 +0000 | [diff] [blame] | 275 | matches.emplace_back(bus, |
| 276 | "type='signal',member='InterfacesRemoved',arg0path='/" |
| 277 | "xyz/openbmc_project/sensors/'", |
| 278 | eventHandler, &timer); |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 281 | /** |
| 282 | * retrieve an attribute from the pid configuration map |
| 283 | * @param[in] base - the PID configuration map, keys are the attributes and |
| 284 | * value is the variant associated with that attribute. |
| 285 | * @param attributeName - the name of the attribute |
| 286 | * @return a variant holding the value associated with a key |
| 287 | * @throw runtime_error : attributeName is not in base |
| 288 | */ |
| 289 | inline DbusVariantType getPIDAttribute( |
| 290 | const std::unordered_map<std::string, DbusVariantType>& base, |
| 291 | const std::string& attributeName) |
| 292 | { |
| 293 | auto search = base.find(attributeName); |
| 294 | if (search == base.end()) |
| 295 | { |
| 296 | throw std::runtime_error("missing attribute " + attributeName); |
| 297 | } |
| 298 | return search->second; |
| 299 | } |
| 300 | |
| Harvey Wu | 239aa7d | 2022-11-18 08:43:34 +0800 | [diff] [blame] | 301 | inline void getCycleTimeSetting( |
| 302 | const std::unordered_map<std::string, DbusVariantType>& zone, |
| 303 | const int zoneIndex, const std::string& attributeName, uint64_t& value) |
| 304 | { |
| 305 | auto findAttributeName = zone.find(attributeName); |
| 306 | if (findAttributeName != zone.end()) |
| 307 | { |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 308 | double tmpAttributeValue = |
| 309 | std::visit(VariantToDoubleVisitor(), zone.at(attributeName)); |
| Harvey Wu | 239aa7d | 2022-11-18 08:43:34 +0800 | [diff] [blame] | 310 | if (tmpAttributeValue >= 1.0) |
| 311 | { |
| 312 | value = static_cast<uint64_t>(tmpAttributeValue); |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | std::cerr << "Zone " << zoneIndex << ": " << attributeName |
| 317 | << " is invalid. Use default " << value << " ms\n"; |
| 318 | } |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | std::cerr << "Zone " << zoneIndex << ": " << attributeName |
| 323 | << " cannot find setting. Use default " << value << " ms\n"; |
| 324 | } |
| 325 | } |
| 326 | |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 327 | void populatePidInfo( |
| Patrick Williams | cd1e78a | 2025-04-07 17:21:05 -0400 | [diff] [blame] | 328 | sdbusplus::bus_t& bus, |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 329 | const std::unordered_map<std::string, DbusVariantType>& base, |
| Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 330 | conf::ControllerInfo& info, const std::string* thresholdProperty, |
| Patrick Venture | 7382318 | 2020-10-08 15:12:51 -0700 | [diff] [blame] | 331 | const std::map<std::string, conf::SensorConfig>& sensorConfig) |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 332 | { |
| Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 333 | info.type = std::get<std::string>(getPIDAttribute(base, "Class")); |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 334 | if (info.type == "fan") |
| 335 | { |
| 336 | info.setpoint = 0; |
| 337 | } |
| 338 | else |
| 339 | { |
| Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 340 | info.setpoint = std::visit(VariantToDoubleVisitor(), |
| 341 | getPIDAttribute(base, "SetPoint")); |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 344 | int failsafepercent = 0; |
| 345 | auto findFailSafe = base.find("FailSafePercent"); |
| 346 | if (findFailSafe != base.end()) |
| 347 | { |
| 348 | failsafepercent = std::visit(VariantToDoubleVisitor(), |
| 349 | getPIDAttribute(base, "FailSafePercent")); |
| 350 | } |
| 351 | info.failSafePercent = failsafepercent; |
| 352 | |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 353 | if (thresholdProperty != nullptr) |
| 354 | { |
| 355 | std::string interface; |
| Alexander Hansen | d3e71ce | 2025-11-12 16:20:53 +0100 | [diff] [blame^] | 356 | if (*thresholdProperty == |
| 357 | SensorThresholdWarning::property_names::warning_high || |
| 358 | *thresholdProperty == |
| 359 | SensorThresholdWarning::property_names::warning_low) |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 360 | { |
| Alexander Hansen | d3e71ce | 2025-11-12 16:20:53 +0100 | [diff] [blame^] | 361 | interface = SensorThresholdWarning::interface; |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 362 | } |
| 363 | else |
| 364 | { |
| Alexander Hansen | d3e71ce | 2025-11-12 16:20:53 +0100 | [diff] [blame^] | 365 | interface = SensorThresholdCritical::interface; |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 366 | } |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 367 | |
| 368 | // Although this checks only the first vector element for the |
| 369 | // named threshold, it is OK, because the SetPointOffset parser |
| 370 | // splits up the input into individual vectors, each with only a |
| 371 | // single element, if it detects that SetPointOffset is in use. |
| 372 | const std::string& path = |
| 373 | sensorConfig.at(info.inputs.front().name).readPath; |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 374 | |
| Patrick Williams | cd1e78a | 2025-04-07 17:21:05 -0400 | [diff] [blame] | 375 | DbusHelper helper(bus); |
| Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 376 | std::string service = helper.getService(interface, path); |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 377 | double reading = 0; |
| 378 | try |
| 379 | { |
| Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 380 | helper.getProperty(service, path, interface, *thresholdProperty, |
| 381 | reading); |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 382 | } |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 383 | catch (const sdbusplus::exception_t& ex) |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 384 | { |
| 385 | // unsupported threshold, leaving reading at 0 |
| 386 | } |
| 387 | |
| 388 | info.setpoint += reading; |
| 389 | } |
| 390 | |
| 391 | info.pidInfo.ts = 1.0; // currently unused |
| Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 392 | info.pidInfo.proportionalCoeff = std::visit( |
| 393 | VariantToDoubleVisitor(), getPIDAttribute(base, "PCoefficient")); |
| 394 | info.pidInfo.integralCoeff = std::visit( |
| 395 | VariantToDoubleVisitor(), getPIDAttribute(base, "ICoefficient")); |
| Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 396 | // DCoefficient is below, it is optional, same reason as in buildjson.cpp |
| Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 397 | info.pidInfo.feedFwdOffset = std::visit( |
| 398 | VariantToDoubleVisitor(), getPIDAttribute(base, "FFOffCoefficient")); |
| 399 | info.pidInfo.feedFwdGain = std::visit( |
| 400 | VariantToDoubleVisitor(), getPIDAttribute(base, "FFGainCoefficient")); |
| 401 | info.pidInfo.integralLimit.max = std::visit( |
| 402 | VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMax")); |
| 403 | info.pidInfo.integralLimit.min = std::visit( |
| 404 | VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMin")); |
| 405 | info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(), |
| 406 | getPIDAttribute(base, "OutLimitMax")); |
| 407 | info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(), |
| 408 | getPIDAttribute(base, "OutLimitMin")); |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 409 | info.pidInfo.slewNeg = |
| 410 | std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewNeg")); |
| 411 | info.pidInfo.slewPos = |
| 412 | std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewPos")); |
| Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 413 | |
| Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 414 | bool checkHysterWithSetpt = false; |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 415 | double negativeHysteresis = 0; |
| 416 | double positiveHysteresis = 0; |
| Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 417 | double derivativeCoeff = 0; |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 418 | |
| Delphine CC Chiu | 5d897e2 | 2024-06-04 13:33:22 +0800 | [diff] [blame] | 419 | auto findCheckHysterFlag = base.find("CheckHysteresisWithSetpoint"); |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 420 | auto findNeg = base.find("NegativeHysteresis"); |
| 421 | auto findPos = base.find("PositiveHysteresis"); |
| Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 422 | auto findDerivative = base.find("DCoefficient"); |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 423 | |
| Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 424 | if (findCheckHysterFlag != base.end()) |
| 425 | { |
| 426 | checkHysterWithSetpt = std::get<bool>(findCheckHysterFlag->second); |
| 427 | } |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 428 | if (findNeg != base.end()) |
| 429 | { |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 430 | negativeHysteresis = |
| 431 | std::visit(VariantToDoubleVisitor(), findNeg->second); |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 432 | } |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 433 | if (findPos != base.end()) |
| 434 | { |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 435 | positiveHysteresis = |
| 436 | std::visit(VariantToDoubleVisitor(), findPos->second); |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 437 | } |
| Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 438 | if (findDerivative != base.end()) |
| 439 | { |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 440 | derivativeCoeff = |
| 441 | std::visit(VariantToDoubleVisitor(), findDerivative->second); |
| Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 444 | info.pidInfo.checkHysterWithSetpt = checkHysterWithSetpt; |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 445 | info.pidInfo.negativeHysteresis = negativeHysteresis; |
| 446 | info.pidInfo.positiveHysteresis = positiveHysteresis; |
| Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 447 | info.pidInfo.derivativeCoeff = derivativeCoeff; |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 450 | bool init(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer, |
| Patrick Venture | 7382318 | 2020-10-08 15:12:51 -0700 | [diff] [blame] | 451 | std::map<std::string, conf::SensorConfig>& sensorConfig, |
| 452 | std::map<int64_t, conf::PIDConf>& zoneConfig, |
| 453 | std::map<int64_t, conf::ZoneConfig>& zoneDetailsConfig) |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 454 | { |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 455 | sensorConfig.clear(); |
| 456 | zoneConfig.clear(); |
| 457 | zoneDetailsConfig.clear(); |
| 458 | |
| 459 | createMatches(bus, timer); |
| 460 | |
| Alexander Hansen | 1ec3d13 | 2025-10-27 17:19:50 +0100 | [diff] [blame] | 461 | auto mapper = bus.new_method_call( |
| 462 | ObjectMapper::default_service, ObjectMapper::instance_path, |
| 463 | ObjectMapper::interface, ObjectMapper::method_names::get_sub_tree); |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 464 | mapper.append( |
| 465 | "/", 0, |
| 466 | std::array<const char*, 6>{ |
| 467 | objectManagerInterface, pidConfigurationInterface, |
| 468 | pidZoneConfigurationInterface, stepwiseConfigurationInterface, |
| Alexander Hansen | bd2c98e | 2025-11-12 12:47:00 +0100 | [diff] [blame] | 469 | SensorValue::interface, ControlFanPwm::interface}); |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 470 | std::unordered_map< |
| 471 | std::string, std::unordered_map<std::string, std::vector<std::string>>> |
| 472 | respData; |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 473 | try |
| 474 | { |
| 475 | auto resp = bus.call(mapper); |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 476 | resp.read(respData); |
| 477 | } |
| Patrick Williams | 0001ee0 | 2021-10-06 14:44:22 -0500 | [diff] [blame] | 478 | catch (const sdbusplus::exception_t&) |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 479 | { |
| 480 | // can't do anything without mapper call data |
| 481 | throw std::runtime_error("ObjectMapper Call Failure"); |
| 482 | } |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 483 | |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 484 | if (respData.empty()) |
| 485 | { |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 486 | // can't do anything without mapper call data |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 487 | throw std::runtime_error("No configuration data available from Mapper"); |
| 488 | } |
| 489 | // create a map of pair of <has pid configuration, ObjectManager path> |
| 490 | std::unordered_map<std::string, std::pair<bool, std::string>> owners; |
| 491 | // and a map of <path, interface> for sensors |
| 492 | std::unordered_map<std::string, std::string> sensors; |
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 493 | for (const auto& objectPair : respData) |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 494 | { |
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 495 | for (const auto& ownerPair : objectPair.second) |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 496 | { |
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 497 | auto& owner = owners[ownerPair.first]; |
| 498 | for (const std::string& interface : ownerPair.second) |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 499 | { |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 500 | if (interface == objectManagerInterface) |
| 501 | { |
| 502 | owner.second = objectPair.first; |
| 503 | } |
| 504 | if (interface == pidConfigurationInterface || |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 505 | interface == pidZoneConfigurationInterface || |
| 506 | interface == stepwiseConfigurationInterface) |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 507 | { |
| 508 | owner.first = true; |
| 509 | } |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame] | 510 | if (interface == SensorValue::interface || |
| Alexander Hansen | bd2c98e | 2025-11-12 12:47:00 +0100 | [diff] [blame] | 511 | interface == ControlFanPwm::interface) |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 512 | { |
| 513 | // we're not interested in pwm sensors, just pwm control |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame] | 514 | if (interface == SensorValue::interface && |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 515 | objectPair.first.find("pwm") != std::string::npos) |
| 516 | { |
| 517 | continue; |
| 518 | } |
| 519 | sensors[objectPair.first] = interface; |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | ManagedObjectType configurations; |
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 525 | for (const auto& owner : owners) |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 526 | { |
| 527 | // skip if no pid configuration (means probably a sensor) |
| 528 | if (!owner.second.first) |
| 529 | { |
| 530 | continue; |
| 531 | } |
| 532 | auto endpoint = bus.new_method_call( |
| 533 | owner.first.c_str(), owner.second.second.c_str(), |
| 534 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 535 | ManagedObjectType configuration; |
| 536 | try |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 537 | { |
| Manojkiran Eda | 7ca8887 | 2024-06-17 11:55:48 +0530 | [diff] [blame] | 538 | auto response = bus.call(endpoint); |
| 539 | response.read(configuration); |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 540 | } |
| Patrick Williams | 0001ee0 | 2021-10-06 14:44:22 -0500 | [diff] [blame] | 541 | catch (const sdbusplus::exception_t&) |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 542 | { |
| 543 | // this shouldn't happen, probably means daemon crashed |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 544 | throw std::runtime_error( |
| 545 | "Error getting managed objects from " + owner.first); |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 546 | } |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 547 | |
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 548 | for (auto& pathPair : configuration) |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 549 | { |
| 550 | if (pathPair.second.find(pidConfigurationInterface) != |
| 551 | pathPair.second.end() || |
| 552 | pathPair.second.find(pidZoneConfigurationInterface) != |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 553 | pathPair.second.end() || |
| 554 | pathPair.second.find(stepwiseConfigurationInterface) != |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 555 | pathPair.second.end()) |
| 556 | { |
| 557 | configurations.emplace(pathPair); |
| 558 | } |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
| 562 | // remove controllers from config that aren't in the current profile(s) |
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 563 | std::vector<std::string> selectedProfiles = getSelectedProfiles(bus); |
| 564 | if (selectedProfiles.size()) |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 565 | { |
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 566 | for (auto pathIt = configurations.begin(); |
| 567 | pathIt != configurations.end();) |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 568 | { |
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 569 | for (auto confIt = pathIt->second.begin(); |
| 570 | confIt != pathIt->second.end();) |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 571 | { |
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 572 | auto profilesFind = confIt->second.find("Profiles"); |
| 573 | if (profilesFind == confIt->second.end()) |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 574 | { |
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 575 | confIt++; |
| 576 | continue; // if no profiles selected, apply always |
| 577 | } |
| 578 | auto profiles = |
| 579 | std::get<std::vector<std::string>>(profilesFind->second); |
| 580 | if (profiles.empty()) |
| 581 | { |
| 582 | confIt++; |
| 583 | continue; |
| 584 | } |
| 585 | |
| 586 | bool found = false; |
| 587 | for (const std::string& profile : profiles) |
| 588 | { |
| 589 | if (std::find(selectedProfiles.begin(), |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 590 | selectedProfiles.end(), profile) != |
| 591 | selectedProfiles.end()) |
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 592 | { |
| 593 | found = true; |
| 594 | break; |
| 595 | } |
| 596 | } |
| 597 | if (found) |
| 598 | { |
| 599 | confIt++; |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 600 | } |
| 601 | else |
| 602 | { |
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 603 | confIt = pathIt->second.erase(confIt); |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 604 | } |
| 605 | } |
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 606 | if (pathIt->second.empty()) |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 607 | { |
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 608 | pathIt = configurations.erase(pathIt); |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 609 | } |
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 610 | else |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 611 | { |
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 612 | pathIt++; |
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 613 | } |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 614 | } |
| 615 | } |
| James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 616 | |
| Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 617 | // On D-Bus, although not necessary, |
| 618 | // having the "zoneID" field can still be useful, |
| 619 | // as it is used for diagnostic messages, |
| 620 | // logging file names, and so on. |
| 621 | // Accept optional "ZoneIndex" parameter to explicitly specify. |
| 622 | // If not present, or not unique, auto-assign index, |
| 623 | // using 0-based numbering, ensuring uniqueness. |
| 624 | std::map<std::string, int64_t> foundZones; |
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 625 | for (const auto& configuration : configurations) |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 626 | { |
| 627 | auto findZone = |
| 628 | configuration.second.find(pidZoneConfigurationInterface); |
| 629 | if (findZone != configuration.second.end()) |
| 630 | { |
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 631 | const auto& zone = findZone->second; |
| James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 632 | |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 633 | const std::string& name = std::get<std::string>(zone.at("Name")); |
| Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 634 | |
| 635 | auto findZoneIndex = zone.find("ZoneIndex"); |
| 636 | if (findZoneIndex == zone.end()) |
| 637 | { |
| 638 | continue; |
| 639 | } |
| 640 | |
| 641 | auto ptrZoneIndex = std::get_if<double>(&(findZoneIndex->second)); |
| 642 | if (!ptrZoneIndex) |
| 643 | { |
| 644 | continue; |
| 645 | } |
| 646 | |
| 647 | auto desiredIndex = static_cast<int64_t>(*ptrZoneIndex); |
| 648 | auto grantedIndex = setZoneIndex(name, foundZones, desiredIndex); |
| 649 | std::cout << "Zone " << name << " is at ZoneIndex " << grantedIndex |
| 650 | << "\n"; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | for (const auto& configuration : configurations) |
| 655 | { |
| 656 | auto findZone = |
| 657 | configuration.second.find(pidZoneConfigurationInterface); |
| 658 | if (findZone != configuration.second.end()) |
| 659 | { |
| 660 | const auto& zone = findZone->second; |
| 661 | |
| 662 | const std::string& name = std::get<std::string>(zone.at("Name")); |
| 663 | |
| 664 | auto index = getZoneIndex(name, foundZones); |
| James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 665 | |
| Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 666 | auto& details = zoneDetailsConfig[index]; |
| Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 667 | |
| James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 668 | details.minThermalOutput = std::visit(VariantToDoubleVisitor(), |
| 669 | zone.at("MinThermalOutput")); |
| ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 670 | |
| 671 | int failsafepercent = 0; |
| 672 | auto findFailSafe = zone.find("FailSafePercent"); |
| 673 | if (findFailSafe != zone.end()) |
| 674 | { |
| 675 | failsafepercent = std::visit(VariantToDoubleVisitor(), |
| 676 | zone.at("FailSafePercent")); |
| 677 | } |
| 678 | details.failsafePercent = failsafepercent; |
| Josh Lehan | 9f9a06a | 2022-12-14 10:39:45 -0800 | [diff] [blame] | 679 | |
| Harvey Wu | 239aa7d | 2022-11-18 08:43:34 +0800 | [diff] [blame] | 680 | getCycleTimeSetting(zone, index, "CycleIntervalTimeMS", |
| 681 | details.cycleTime.cycleIntervalTimeMS); |
| 682 | getCycleTimeSetting(zone, index, "UpdateThermalsTimeMS", |
| 683 | details.cycleTime.updateThermalsTimeMS); |
| Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 684 | |
| 685 | bool accumulateSetPoint = false; |
| 686 | auto findAccSetPoint = zone.find("AccumulateSetPoint"); |
| 687 | if (findAccSetPoint != zone.end()) |
| 688 | { |
| 689 | accumulateSetPoint = std::get<bool>(findAccSetPoint->second); |
| 690 | } |
| 691 | details.accumulateSetPoint = accumulateSetPoint; |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 692 | } |
| 693 | auto findBase = configuration.second.find(pidConfigurationInterface); |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 694 | // loop through all the PID configurations and fill out a sensor config |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 695 | if (findBase != configuration.second.end()) |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 696 | { |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 697 | const auto& base = |
| 698 | configuration.second.at(pidConfigurationInterface); |
| ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 699 | const std::string pidName = |
| 700 | sensorNameToDbusName(std::get<std::string>(base.at("Name"))); |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 701 | const std::string pidClass = |
| 702 | std::get<std::string>(base.at("Class")); |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 703 | const std::vector<std::string>& zones = |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 704 | std::get<std::vector<std::string>>(base.at("Zones")); |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 705 | for (const std::string& zone : zones) |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 706 | { |
| Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 707 | auto index = getZoneIndex(zone, foundZones); |
| 708 | |
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 709 | conf::PIDConf& conf = zoneConfig[index]; |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 710 | std::vector<std::string> inputSensorNames( |
| 711 | std::get<std::vector<std::string>>(base.at("Inputs"))); |
| 712 | std::vector<std::string> outputSensorNames; |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 713 | std::vector<std::string> missingAcceptableSensorNames; |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 714 | std::vector<std::string> archivedInputSensorNames; |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 715 | |
| 716 | auto findMissingAcceptable = base.find("MissingIsAcceptable"); |
| 717 | if (findMissingAcceptable != base.end()) |
| 718 | { |
| 719 | missingAcceptableSensorNames = |
| 720 | std::get<std::vector<std::string>>( |
| 721 | findMissingAcceptable->second); |
| 722 | } |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 723 | |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 724 | // assumption: all fan pids must have at least one output |
| 725 | if (pidClass == "fan") |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 726 | { |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 727 | outputSensorNames = std::get<std::vector<std::string>>( |
| 728 | getPIDAttribute(base, "Outputs")); |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 729 | } |
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 730 | |
| Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 731 | bool unavailableAsFailed = true; |
| 732 | auto findUnavailableAsFailed = |
| 733 | base.find("InputUnavailableAsFailed"); |
| 734 | if (findUnavailableAsFailed != base.end()) |
| 735 | { |
| 736 | unavailableAsFailed = |
| 737 | std::get<bool>(findUnavailableAsFailed->second); |
| 738 | } |
| 739 | |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 740 | std::vector<SensorInterfaceType> inputSensorInterfaces; |
| 741 | std::vector<SensorInterfaceType> outputSensorInterfaces; |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 742 | std::vector<SensorInterfaceType> |
| 743 | missingAcceptableSensorInterfaces; |
| 744 | |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 745 | /* populate an interface list for different sensor direction |
| 746 | * types (input,output) |
| 747 | */ |
| 748 | /* take the Inputs from the configuration and generate |
| 749 | * a list of dbus descriptors (path, interface). |
| 750 | * Mapping can be many-to-one since an element of Inputs can be |
| 751 | * a regex |
| 752 | */ |
| 753 | for (const std::string& sensorName : inputSensorNames) |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 754 | { |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 755 | #ifndef HANDLE_MISSING_OBJECT_PATHS |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 756 | findSensors(sensors, sensorNameToDbusName(sensorName), |
| 757 | inputSensorInterfaces); |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 758 | #else |
| 759 | std::vector<std::pair<std::string, std::string>> |
| 760 | sensorPathIfacePairs; |
| 761 | auto found = |
| 762 | findSensors(sensors, sensorNameToDbusName(sensorName), |
| 763 | sensorPathIfacePairs); |
| 764 | if (found) |
| 765 | { |
| 766 | inputSensorInterfaces.insert( |
| 767 | inputSensorInterfaces.end(), |
| 768 | sensorPathIfacePairs.begin(), |
| 769 | sensorPathIfacePairs.end()); |
| 770 | } |
| 771 | else if (pidClass != "fan") |
| 772 | { |
| 773 | if (std::find(missingAcceptableSensorNames.begin(), |
| 774 | missingAcceptableSensorNames.end(), |
| 775 | sensorName) == |
| 776 | missingAcceptableSensorNames.end()) |
| 777 | { |
| 778 | std::cerr |
| 779 | << "Pid controller: Missing a missing-unacceptable sensor from D-Bus " |
| 780 | << sensorName << "\n"; |
| 781 | std::string inputSensorName = |
| 782 | sensorNameToDbusName(sensorName); |
| 783 | auto& config = sensorConfig[inputSensorName]; |
| 784 | archivedInputSensorNames.push_back(inputSensorName); |
| 785 | config.type = pidClass; |
| 786 | config.readPath = |
| 787 | getSensorPath(config.type, inputSensorName); |
| 788 | config.timeout = 0; |
| 789 | config.ignoreDbusMinMax = true; |
| 790 | config.unavailableAsFailed = unavailableAsFailed; |
| 791 | } |
| 792 | else |
| 793 | { |
| 794 | // When an input sensor is NOT on DBus, and it's in |
| 795 | // the MissingIsAcceptable list. Ignore it and |
| 796 | // continue with the next input sensor. |
| 797 | std::cout |
| 798 | << "Pid controller: Missing a missing-acceptable sensor from D-Bus " |
| 799 | << sensorName << "\n"; |
| 800 | continue; |
| 801 | } |
| 802 | } |
| 803 | #endif |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 804 | } |
| 805 | for (const std::string& sensorName : outputSensorNames) |
| 806 | { |
| 807 | findSensors(sensors, sensorNameToDbusName(sensorName), |
| 808 | outputSensorInterfaces); |
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 809 | } |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 810 | for (const std::string& sensorName : |
| 811 | missingAcceptableSensorNames) |
| 812 | { |
| 813 | findSensors(sensors, sensorNameToDbusName(sensorName), |
| 814 | missingAcceptableSensorInterfaces); |
| 815 | } |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 816 | |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 817 | for (const SensorInterfaceType& inputSensorInterface : |
| 818 | inputSensorInterfaces) |
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 819 | { |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 820 | const std::string& dbusInterface = |
| 821 | inputSensorInterface.second; |
| 822 | const std::string& inputSensorPath = |
| 823 | inputSensorInterface.first; |
| Josh Lehan | fb82a87 | 2020-09-20 21:48:22 -0700 | [diff] [blame] | 824 | |
| 825 | // Setting timeout to 0 is intentional, as D-Bus passive |
| 826 | // sensor updates are pushed in, not pulled by timer poll. |
| 827 | // Setting ignoreDbusMinMax is intentional, as this |
| 828 | // prevents normalization of values to [0.0, 1.0] range, |
| 829 | // which would mess up the PID loop math. |
| 830 | // All non-fan PID classes should be initialized this way. |
| 831 | // As for why a fan should not use this code path, see |
| 832 | // the ed1dafdf168def37c65bfb7a5efd18d9dbe04727 commit. |
| Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 833 | if ((pidClass == "temp") || (pidClass == "margin") || |
| 834 | (pidClass == "power") || (pidClass == "powersum")) |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 835 | { |
| Harvey.Wu | ed1dafd | 2022-02-09 13:53:20 +0800 | [diff] [blame] | 836 | std::string inputSensorName = |
| 837 | getSensorNameFromPath(inputSensorPath); |
| 838 | auto& config = sensorConfig[inputSensorName]; |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 839 | archivedInputSensorNames.push_back(inputSensorName); |
| Harvey.Wu | ed1dafd | 2022-02-09 13:53:20 +0800 | [diff] [blame] | 840 | config.type = pidClass; |
| 841 | config.readPath = inputSensorInterface.first; |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 842 | config.timeout = 0; |
| 843 | config.ignoreDbusMinMax = true; |
| Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 844 | config.unavailableAsFailed = unavailableAsFailed; |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 845 | } |
| Josh Lehan | fb82a87 | 2020-09-20 21:48:22 -0700 | [diff] [blame] | 846 | |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame] | 847 | if (dbusInterface != SensorValue::interface) |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 848 | { |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 849 | /* all expected inputs in the configuration are expected |
| 850 | * to be sensor interfaces |
| 851 | */ |
| Ed Tanous | 7d6e225 | 2025-06-27 10:45:25 -0700 | [diff] [blame] | 852 | throw std::runtime_error(std::format( |
| 853 | "sensor at dbus path [{}] has an interface [{}] that does not match the expected interface of {}", |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame] | 854 | inputSensorPath, dbusInterface, |
| 855 | SensorValue::interface)); |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 856 | } |
| 857 | } |
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 858 | |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 859 | // MissingIsAcceptable same postprocessing as Inputs |
| 860 | missingAcceptableSensorNames.clear(); |
| 861 | for (const SensorInterfaceType& |
| 862 | missingAcceptableSensorInterface : |
| 863 | missingAcceptableSensorInterfaces) |
| 864 | { |
| 865 | const std::string& dbusInterface = |
| 866 | missingAcceptableSensorInterface.second; |
| 867 | const std::string& missingAcceptableSensorPath = |
| 868 | missingAcceptableSensorInterface.first; |
| 869 | |
| 870 | std::string missingAcceptableSensorName = |
| 871 | getSensorNameFromPath(missingAcceptableSensorPath); |
| 872 | missingAcceptableSensorNames.push_back( |
| 873 | missingAcceptableSensorName); |
| 874 | |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame] | 875 | if (dbusInterface != SensorValue::interface) |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 876 | { |
| 877 | /* MissingIsAcceptable same error checking as Inputs |
| 878 | */ |
| Ed Tanous | 7d6e225 | 2025-06-27 10:45:25 -0700 | [diff] [blame] | 879 | throw std::runtime_error(std::format( |
| 880 | "sensor at dbus path [{}] has an interface [{}] that does not match the expected interface of {}", |
| 881 | missingAcceptableSensorPath, dbusInterface, |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame] | 882 | SensorValue::interface)); |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 886 | /* fan pids need to pair up tach sensors with their pwm |
| 887 | * counterparts |
| 888 | */ |
| 889 | if (pidClass == "fan") |
| 890 | { |
| 891 | /* If a PID is a fan there should be either |
| 892 | * (1) one output(pwm) per input(tach) |
| 893 | * OR |
| 894 | * (2) one putput(pwm) for all inputs(tach) |
| 895 | * everything else indicates a bad configuration. |
| 896 | */ |
| 897 | bool singlePwm = false; |
| 898 | if (outputSensorInterfaces.size() == 1) |
| 899 | { |
| 900 | /* one pwm, set write paths for all fan sensors to it */ |
| 901 | singlePwm = true; |
| 902 | } |
| 903 | else if (inputSensorInterfaces.size() == |
| 904 | outputSensorInterfaces.size()) |
| 905 | { |
| 906 | /* one to one mapping, each fan sensor gets its own pwm |
| 907 | * control */ |
| 908 | singlePwm = false; |
| 909 | } |
| 910 | else |
| 911 | { |
| 912 | throw std::runtime_error( |
| 913 | "fan PID has invalid number of Outputs"); |
| 914 | } |
| 915 | std::string fanSensorName; |
| 916 | std::string pwmPath; |
| 917 | std::string pwmInterface; |
| Harvey.Wu | ed1dafd | 2022-02-09 13:53:20 +0800 | [diff] [blame] | 918 | std::string pwmSensorName; |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 919 | if (singlePwm) |
| 920 | { |
| 921 | /* if just a single output(pwm) is provided then use |
| 922 | * that pwm control path for all the fan sensor write |
| 923 | * path configs |
| 924 | */ |
| 925 | pwmPath = outputSensorInterfaces.at(0).first; |
| 926 | pwmInterface = outputSensorInterfaces.at(0).second; |
| 927 | } |
| 928 | for (uint32_t idx = 0; idx < inputSensorInterfaces.size(); |
| 929 | idx++) |
| 930 | { |
| 931 | if (!singlePwm) |
| 932 | { |
| 933 | pwmPath = outputSensorInterfaces.at(idx).first; |
| 934 | pwmInterface = |
| 935 | outputSensorInterfaces.at(idx).second; |
| 936 | } |
| Alexander Hansen | bd2c98e | 2025-11-12 12:47:00 +0100 | [diff] [blame] | 937 | if (ControlFanPwm::interface != pwmInterface) |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 938 | { |
| Ed Tanous | 7d6e225 | 2025-06-27 10:45:25 -0700 | [diff] [blame] | 939 | throw std::runtime_error(std::format( |
| 940 | "fan pwm control at dbus path [{}] has an interface [{}] that does not match the expected interface of {}", |
| Alexander Hansen | bd2c98e | 2025-11-12 12:47:00 +0100 | [diff] [blame] | 941 | pwmPath, pwmInterface, |
| 942 | ControlFanPwm::interface)); |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 943 | } |
| 944 | const std::string& fanPath = |
| 945 | inputSensorInterfaces.at(idx).first; |
| 946 | fanSensorName = getSensorNameFromPath(fanPath); |
| Harvey.Wu | ed1dafd | 2022-02-09 13:53:20 +0800 | [diff] [blame] | 947 | pwmSensorName = getSensorNameFromPath(pwmPath); |
| 948 | std::string fanPwmIndex = fanSensorName + pwmSensorName; |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 949 | archivedInputSensorNames.push_back(fanPwmIndex); |
| Harvey.Wu | ed1dafd | 2022-02-09 13:53:20 +0800 | [diff] [blame] | 950 | auto& fanConfig = sensorConfig[fanPwmIndex]; |
| 951 | fanConfig.type = pidClass; |
| 952 | fanConfig.readPath = fanPath; |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 953 | fanConfig.writePath = pwmPath; |
| 954 | // todo: un-hardcode this if there are fans with |
| 955 | // different ranges |
| 956 | fanConfig.max = 255; |
| 957 | fanConfig.min = 0; |
| 958 | } |
| 959 | } |
| James Feist | 11d243d | 2019-06-24 16:18:40 -0700 | [diff] [blame] | 960 | // if the sensors aren't available in the current state, don't |
| 961 | // add them to the configuration. |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 962 | if (archivedInputSensorNames.empty()) |
| James Feist | 11d243d | 2019-06-24 16:18:40 -0700 | [diff] [blame] | 963 | { |
| 964 | continue; |
| 965 | } |
| 966 | |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 967 | std::string offsetType; |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 968 | |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 969 | // SetPointOffset is a threshold value to pull from the sensor |
| 970 | // to apply an offset. For upper thresholds this means the |
| 971 | // setpoint is usually negative. |
| 972 | auto findSetpointOffset = base.find("SetPointOffset"); |
| 973 | if (findSetpointOffset != base.end()) |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 974 | { |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 975 | offsetType = |
| 976 | std::get<std::string>(findSetpointOffset->second); |
| 977 | if (std::find(thresholds::types.begin(), |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 978 | thresholds::types.end(), offsetType) == |
| 979 | thresholds::types.end()) |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 980 | { |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 981 | throw std::runtime_error( |
| 982 | "Unsupported type: " + offsetType); |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 986 | std::vector<double> inputTempToMargin; |
| 987 | |
| 988 | auto findTempToMargin = base.find("TempToMargin"); |
| 989 | if (findTempToMargin != base.end()) |
| 990 | { |
| 991 | inputTempToMargin = |
| 992 | std::get<std::vector<double>>(findTempToMargin->second); |
| 993 | } |
| 994 | |
| 995 | std::vector<pid_control::conf::SensorInput> sensorInputs = |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 996 | spliceInputs(archivedInputSensorNames, inputTempToMargin, |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 997 | missingAcceptableSensorNames); |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 998 | |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 999 | if (offsetType.empty()) |
| 1000 | { |
| ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 1001 | conf::ControllerInfo& info = conf[pidName]; |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 1002 | info.inputs = std::move(sensorInputs); |
| Patrick Venture | 7382318 | 2020-10-08 15:12:51 -0700 | [diff] [blame] | 1003 | populatePidInfo(bus, base, info, nullptr, sensorConfig); |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 1004 | } |
| 1005 | else |
| 1006 | { |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 1007 | // we have to split up the inputs, as in practice t-control |
| 1008 | // values will differ, making setpoints differ |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 1009 | for (const pid_control::conf::SensorInput& input : |
| 1010 | sensorInputs) |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 1011 | { |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 1012 | conf::ControllerInfo& info = conf[input.name]; |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 1013 | info.inputs.emplace_back(input); |
| Patrick Venture | 7382318 | 2020-10-08 15:12:51 -0700 | [diff] [blame] | 1014 | populatePidInfo(bus, base, info, &offsetType, |
| 1015 | sensorConfig); |
| James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 1016 | } |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 1017 | } |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | auto findStepwise = |
| 1021 | configuration.second.find(stepwiseConfigurationInterface); |
| 1022 | if (findStepwise != configuration.second.end()) |
| 1023 | { |
| 1024 | const auto& base = findStepwise->second; |
| ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 1025 | const std::string pidName = |
| 1026 | sensorNameToDbusName(std::get<std::string>(base.at("Name"))); |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 1027 | const std::vector<std::string>& zones = |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 1028 | std::get<std::vector<std::string>>(base.at("Zones")); |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 1029 | for (const std::string& zone : zones) |
| 1030 | { |
| Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 1031 | auto index = getZoneIndex(zone, foundZones); |
| 1032 | |
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 1033 | conf::PIDConf& conf = zoneConfig[index]; |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 1034 | |
| 1035 | std::vector<std::string> inputs; |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 1036 | std::vector<std::string> missingAcceptableSensors; |
| 1037 | std::vector<std::string> missingAcceptableSensorNames; |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 1038 | std::vector<std::string> sensorNames = |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 1039 | std::get<std::vector<std::string>>(base.at("Inputs")); |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 1040 | |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 1041 | auto findMissingAcceptable = base.find("MissingIsAcceptable"); |
| 1042 | if (findMissingAcceptable != base.end()) |
| 1043 | { |
| 1044 | missingAcceptableSensorNames = |
| 1045 | std::get<std::vector<std::string>>( |
| 1046 | findMissingAcceptable->second); |
| 1047 | } |
| 1048 | |
| Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 1049 | bool unavailableAsFailed = true; |
| 1050 | auto findUnavailableAsFailed = |
| 1051 | base.find("InputUnavailableAsFailed"); |
| 1052 | if (findUnavailableAsFailed != base.end()) |
| 1053 | { |
| 1054 | unavailableAsFailed = |
| 1055 | std::get<bool>(findUnavailableAsFailed->second); |
| 1056 | } |
| 1057 | |
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 1058 | bool sensorFound = false; |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 1059 | for (const std::string& sensorName : sensorNames) |
| 1060 | { |
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 1061 | std::vector<std::pair<std::string, std::string>> |
| 1062 | sensorPathIfacePairs; |
| Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 1063 | if (!findSensors(sensors, sensorNameToDbusName(sensorName), |
| 1064 | sensorPathIfacePairs)) |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 1065 | { |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 1066 | #ifndef HANDLE_MISSING_OBJECT_PATHS |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 1067 | break; |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 1068 | #else |
| 1069 | if (std::find(missingAcceptableSensorNames.begin(), |
| 1070 | missingAcceptableSensorNames.end(), |
| 1071 | sensorName) == |
| 1072 | missingAcceptableSensorNames.end()) |
| 1073 | { |
| 1074 | // When an input sensor is NOT on DBus, and it's NOT |
| 1075 | // in the MissingIsAcceptable list. Build it as a |
| 1076 | // failed sensor with default information (temp |
| 1077 | // sensor path, temp type, ...) |
| 1078 | std::cerr |
| 1079 | << "Stepwise controller: Missing a missing-unacceptable sensor from D-Bus " |
| 1080 | << sensorName << "\n"; |
| 1081 | std::string shortName = |
| 1082 | sensorNameToDbusName(sensorName); |
| 1083 | |
| 1084 | inputs.push_back(shortName); |
| 1085 | auto& config = sensorConfig[shortName]; |
| 1086 | config.type = "temp"; |
| 1087 | config.readPath = |
| 1088 | getSensorPath(config.type, shortName); |
| 1089 | config.ignoreDbusMinMax = true; |
| 1090 | config.unavailableAsFailed = unavailableAsFailed; |
| 1091 | // todo: maybe un-hardcode this if we run into |
| 1092 | // slower timeouts with sensors |
| 1093 | |
| 1094 | config.timeout = 0; |
| 1095 | sensorFound = true; |
| 1096 | } |
| 1097 | else |
| 1098 | { |
| 1099 | // When an input sensor is NOT on DBus, and it's in |
| 1100 | // the MissingIsAcceptable list. Ignore it and |
| 1101 | // continue with the next input sensor. |
| 1102 | std::cout |
| 1103 | << "Stepwise controller: Missing a missing-acceptable sensor from D-Bus " |
| 1104 | << sensorName << "\n"; |
| 1105 | continue; |
| 1106 | } |
| 1107 | #endif |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 1108 | } |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 1109 | else |
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 1110 | { |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 1111 | for (const auto& sensorPathIfacePair : |
| 1112 | sensorPathIfacePairs) |
| 1113 | { |
| 1114 | std::string shortName = getSensorNameFromPath( |
| 1115 | sensorPathIfacePair.first); |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 1116 | |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 1117 | inputs.push_back(shortName); |
| 1118 | auto& config = sensorConfig[shortName]; |
| 1119 | config.readPath = sensorPathIfacePair.first; |
| 1120 | config.type = "temp"; |
| 1121 | config.ignoreDbusMinMax = true; |
| 1122 | config.unavailableAsFailed = unavailableAsFailed; |
| 1123 | // todo: maybe un-hardcode this if we run into |
| 1124 | // slower timeouts with sensors |
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 1125 | |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 1126 | config.timeout = 0; |
| 1127 | sensorFound = true; |
| 1128 | } |
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 1129 | } |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 1130 | } |
| 1131 | if (!sensorFound) |
| 1132 | { |
| 1133 | continue; |
| 1134 | } |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 1135 | |
| 1136 | // MissingIsAcceptable same postprocessing as Inputs |
| 1137 | for (const std::string& missingAcceptableSensorName : |
| 1138 | missingAcceptableSensorNames) |
| 1139 | { |
| 1140 | std::vector<std::pair<std::string, std::string>> |
| 1141 | sensorPathIfacePairs; |
| 1142 | if (!findSensors( |
| 1143 | sensors, |
| 1144 | sensorNameToDbusName(missingAcceptableSensorName), |
| 1145 | sensorPathIfacePairs)) |
| 1146 | { |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 1147 | #ifndef HANDLE_MISSING_OBJECT_PATHS |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 1148 | break; |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 1149 | #else |
| 1150 | // When a sensor in the MissingIsAcceptable list is NOT |
| 1151 | // on DBus and it still reaches here, which contradicts |
| 1152 | // to what we did in the Input sensor building step. |
| 1153 | // Continue. |
| 1154 | continue; |
| 1155 | #endif |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | for (const auto& sensorPathIfacePair : sensorPathIfacePairs) |
| 1159 | { |
| 1160 | std::string shortName = |
| 1161 | getSensorNameFromPath(sensorPathIfacePair.first); |
| 1162 | |
| 1163 | missingAcceptableSensors.push_back(shortName); |
| 1164 | } |
| 1165 | } |
| 1166 | |
| ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 1167 | conf::ControllerInfo& info = conf[pidName]; |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 1168 | |
| 1169 | std::vector<double> inputTempToMargin; |
| 1170 | |
| 1171 | auto findTempToMargin = base.find("TempToMargin"); |
| 1172 | if (findTempToMargin != base.end()) |
| 1173 | { |
| 1174 | inputTempToMargin = |
| 1175 | std::get<std::vector<double>>(findTempToMargin->second); |
| 1176 | } |
| 1177 | |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 1178 | info.inputs = spliceInputs(inputs, inputTempToMargin, |
| 1179 | missingAcceptableSensors); |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 1180 | |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 1181 | info.type = "stepwise"; |
| 1182 | info.stepwiseInfo.ts = 1.0; // currently unused |
| James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 1183 | info.stepwiseInfo.positiveHysteresis = 0.0; |
| 1184 | info.stepwiseInfo.negativeHysteresis = 0.0; |
| James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 1185 | std::string subtype = std::get<std::string>(base.at("Class")); |
| 1186 | |
| 1187 | info.stepwiseInfo.isCeiling = (subtype == "Ceiling"); |
| James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 1188 | auto findPosHyst = base.find("PositiveHysteresis"); |
| 1189 | auto findNegHyst = base.find("NegativeHysteresis"); |
| 1190 | if (findPosHyst != base.end()) |
| 1191 | { |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 1192 | info.stepwiseInfo.positiveHysteresis = std::visit( |
| James Feist | 208abce | 2018-12-06 09:59:10 -0800 | [diff] [blame] | 1193 | VariantToDoubleVisitor(), findPosHyst->second); |
| James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 1194 | } |
| 1195 | if (findNegHyst != base.end()) |
| 1196 | { |
| James Feist | 5782ab8 | 2019-04-02 08:38:48 -0700 | [diff] [blame] | 1197 | info.stepwiseInfo.negativeHysteresis = std::visit( |
| James Feist | 208abce | 2018-12-06 09:59:10 -0800 | [diff] [blame] | 1198 | VariantToDoubleVisitor(), findNegHyst->second); |
| James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 1199 | } |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 1200 | std::vector<double> readings = |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 1201 | std::get<std::vector<double>>(base.at("Reading")); |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 1202 | if (readings.size() > ec::maxStepwisePoints) |
| 1203 | { |
| 1204 | throw std::invalid_argument("Too many stepwise points."); |
| 1205 | } |
| 1206 | if (readings.empty()) |
| 1207 | { |
| 1208 | throw std::invalid_argument( |
| 1209 | "Must have one stepwise point."); |
| 1210 | } |
| 1211 | std::copy(readings.begin(), readings.end(), |
| 1212 | info.stepwiseInfo.reading); |
| 1213 | if (readings.size() < ec::maxStepwisePoints) |
| 1214 | { |
| 1215 | info.stepwiseInfo.reading[readings.size()] = |
| Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 1216 | std::numeric_limits<double>::quiet_NaN(); |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 1217 | } |
| 1218 | std::vector<double> outputs = |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 1219 | std::get<std::vector<double>>(base.at("Output")); |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 1220 | if (readings.size() != outputs.size()) |
| 1221 | { |
| 1222 | throw std::invalid_argument( |
| 1223 | "Outputs size must match readings"); |
| 1224 | } |
| 1225 | std::copy(outputs.begin(), outputs.end(), |
| 1226 | info.stepwiseInfo.output); |
| 1227 | if (outputs.size() < ec::maxStepwisePoints) |
| 1228 | { |
| 1229 | info.stepwiseInfo.output[outputs.size()] = |
| Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 1230 | std::numeric_limits<double>::quiet_NaN(); |
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 1231 | } |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | } |
| Patrick Venture | 39199b4 | 2020-10-08 14:40:29 -0700 | [diff] [blame] | 1235 | if constexpr (pid_control::conf::DEBUG) |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 1236 | { |
| Patrick Venture | 39199b4 | 2020-10-08 14:40:29 -0700 | [diff] [blame] | 1237 | debugPrint(sensorConfig, zoneConfig, zoneDetailsConfig); |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 1238 | } |
| James Feist | c959c42 | 2018-11-01 12:33:40 -0700 | [diff] [blame] | 1239 | if (zoneConfig.empty() || zoneDetailsConfig.empty()) |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 1240 | { |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 1241 | std::cerr |
| 1242 | << "No fan zones, application pausing until new configuration\n"; |
| 1243 | return false; |
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 1244 | } |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 1245 | return true; |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 1246 | } |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 1247 | |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 1248 | } // namespace dbus_configuration |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 1249 | } // namespace pid_control |