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