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