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