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