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; |
| 87 | auto mapper = |
| 88 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 89 | "/xyz/openbmc_project/object_mapper", |
| 90 | "xyz.openbmc_project.ObjectMapper", "GetSubTree"); |
| 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 | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 154 | |
James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 155 | if (context == nullptr || m == nullptr) |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 156 | { |
| 157 | throw std::runtime_error("Invalid match"); |
| 158 | } |
James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 159 | |
| 160 | // we skip associations because the mapper populates these, not the sensors |
| 161 | const std::array<const char*, 1> skipList = { |
| 162 | "xyz.openbmc_project.Association"}; |
| 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 | } |
| 187 | } |
| 188 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 189 | boost::asio::steady_timer* timer = |
| 190 | static_cast<boost::asio::steady_timer*>(context); |
| 191 | |
| 192 | // do a brief sleep as we tend to get a bunch of these events at |
| 193 | // once |
| 194 | timer->expires_after(std::chrono::seconds(2)); |
| 195 | timer->async_wait([](const boost::system::error_code ec) { |
| 196 | if (ec == boost::asio::error::operation_aborted) |
| 197 | { |
| 198 | /* another timer started*/ |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | std::cout << "New configuration detected, reloading\n."; |
Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 203 | tryRestartControlLoops(); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 204 | }); |
| 205 | |
| 206 | return 1; |
| 207 | } |
| 208 | |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 209 | void createMatches(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer) |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 210 | { |
| 211 | // this is a list because the matches can't be moved |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 212 | static std::list<sdbusplus::bus::match_t> matches; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 213 | |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 214 | const std::array<std::string, 4> interfaces = { |
| 215 | thermalControlIface, pidConfigurationInterface, |
| 216 | pidZoneConfigurationInterface, stepwiseConfigurationInterface}; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 217 | |
| 218 | // this list only needs to be created once |
| 219 | if (!matches.empty()) |
| 220 | { |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | // we restart when the configuration changes or there are new sensors |
| 225 | for (const auto& interface : interfaces) |
| 226 | { |
| 227 | matches.emplace_back( |
| 228 | bus, |
| 229 | "type='signal',member='PropertiesChanged',arg0namespace='" + |
| 230 | interface + "'", |
| 231 | eventHandler, &timer); |
| 232 | } |
| 233 | matches.emplace_back( |
| 234 | bus, |
| 235 | "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/" |
| 236 | "sensors/'", |
| 237 | eventHandler, &timer); |
| 238 | } |
| 239 | |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 240 | /** |
| 241 | * retrieve an attribute from the pid configuration map |
| 242 | * @param[in] base - the PID configuration map, keys are the attributes and |
| 243 | * value is the variant associated with that attribute. |
| 244 | * @param attributeName - the name of the attribute |
| 245 | * @return a variant holding the value associated with a key |
| 246 | * @throw runtime_error : attributeName is not in base |
| 247 | */ |
| 248 | inline DbusVariantType getPIDAttribute( |
| 249 | const std::unordered_map<std::string, DbusVariantType>& base, |
| 250 | const std::string& attributeName) |
| 251 | { |
| 252 | auto search = base.find(attributeName); |
| 253 | if (search == base.end()) |
| 254 | { |
| 255 | throw std::runtime_error("missing attribute " + attributeName); |
| 256 | } |
| 257 | return search->second; |
| 258 | } |
| 259 | |
Harvey Wu | 239aa7d | 2022-11-18 08:43:34 +0800 | [diff] [blame^] | 260 | inline void getCycleTimeSetting( |
| 261 | const std::unordered_map<std::string, DbusVariantType>& zone, |
| 262 | const int zoneIndex, const std::string& attributeName, uint64_t& value) |
| 263 | { |
| 264 | auto findAttributeName = zone.find(attributeName); |
| 265 | if (findAttributeName != zone.end()) |
| 266 | { |
| 267 | double tmpAttributeValue = |
| 268 | std::visit(VariantToDoubleVisitor(), zone.at(attributeName)); |
| 269 | if (tmpAttributeValue >= 1.0) |
| 270 | { |
| 271 | value = static_cast<uint64_t>(tmpAttributeValue); |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | std::cerr << "Zone " << zoneIndex << ": " << attributeName |
| 276 | << " is invalid. Use default " << value << " ms\n"; |
| 277 | } |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | std::cerr << "Zone " << zoneIndex << ": " << attributeName |
| 282 | << " cannot find setting. Use default " << value << " ms\n"; |
| 283 | } |
| 284 | } |
| 285 | |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 286 | void populatePidInfo( |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 287 | [[maybe_unused]] sdbusplus::bus_t& bus, |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 288 | const std::unordered_map<std::string, DbusVariantType>& base, |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 289 | conf::ControllerInfo& info, const std::string* thresholdProperty, |
Patrick Venture | 7382318 | 2020-10-08 15:12:51 -0700 | [diff] [blame] | 290 | const std::map<std::string, conf::SensorConfig>& sensorConfig) |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 291 | { |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 292 | info.type = std::get<std::string>(getPIDAttribute(base, "Class")); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 293 | if (info.type == "fan") |
| 294 | { |
| 295 | info.setpoint = 0; |
| 296 | } |
| 297 | else |
| 298 | { |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 299 | info.setpoint = std::visit(VariantToDoubleVisitor(), |
| 300 | getPIDAttribute(base, "SetPoint")); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | if (thresholdProperty != nullptr) |
| 304 | { |
| 305 | std::string interface; |
| 306 | if (*thresholdProperty == "WarningHigh" || |
| 307 | *thresholdProperty == "WarningLow") |
| 308 | { |
| 309 | interface = thresholds::warningInterface; |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | interface = thresholds::criticalInterface; |
| 314 | } |
Patrick Venture | 7382318 | 2020-10-08 15:12:51 -0700 | [diff] [blame] | 315 | const std::string& path = sensorConfig.at(info.inputs.front()).readPath; |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 316 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 317 | DbusHelper helper(sdbusplus::bus::new_system()); |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 318 | std::string service = helper.getService(interface, path); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 319 | double reading = 0; |
| 320 | try |
| 321 | { |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 322 | helper.getProperty(service, path, interface, *thresholdProperty, |
| 323 | reading); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 324 | } |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 325 | catch (const sdbusplus::exception_t& ex) |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 326 | { |
| 327 | // unsupported threshold, leaving reading at 0 |
| 328 | } |
| 329 | |
| 330 | info.setpoint += reading; |
| 331 | } |
| 332 | |
| 333 | info.pidInfo.ts = 1.0; // currently unused |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 334 | info.pidInfo.proportionalCoeff = std::visit( |
| 335 | VariantToDoubleVisitor(), getPIDAttribute(base, "PCoefficient")); |
| 336 | info.pidInfo.integralCoeff = std::visit( |
| 337 | VariantToDoubleVisitor(), getPIDAttribute(base, "ICoefficient")); |
Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 338 | // DCoefficient is below, it is optional, same reason as in buildjson.cpp |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 339 | info.pidInfo.feedFwdOffset = std::visit( |
| 340 | VariantToDoubleVisitor(), getPIDAttribute(base, "FFOffCoefficient")); |
| 341 | info.pidInfo.feedFwdGain = std::visit( |
| 342 | VariantToDoubleVisitor(), getPIDAttribute(base, "FFGainCoefficient")); |
| 343 | info.pidInfo.integralLimit.max = std::visit( |
| 344 | VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMax")); |
| 345 | info.pidInfo.integralLimit.min = std::visit( |
| 346 | VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMin")); |
| 347 | info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(), |
| 348 | getPIDAttribute(base, "OutLimitMax")); |
| 349 | info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(), |
| 350 | getPIDAttribute(base, "OutLimitMin")); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 351 | info.pidInfo.slewNeg = |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 352 | std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewNeg")); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 353 | info.pidInfo.slewPos = |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 354 | std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewPos")); |
Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 355 | |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 356 | double negativeHysteresis = 0; |
| 357 | double positiveHysteresis = 0; |
Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 358 | double derivativeCoeff = 0; |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 359 | |
| 360 | auto findNeg = base.find("NegativeHysteresis"); |
| 361 | auto findPos = base.find("PositiveHysteresis"); |
Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 362 | auto findDerivative = base.find("DCoefficient"); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 363 | |
| 364 | if (findNeg != base.end()) |
| 365 | { |
| 366 | negativeHysteresis = |
| 367 | std::visit(VariantToDoubleVisitor(), findNeg->second); |
| 368 | } |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 369 | if (findPos != base.end()) |
| 370 | { |
| 371 | positiveHysteresis = |
| 372 | std::visit(VariantToDoubleVisitor(), findPos->second); |
| 373 | } |
Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 374 | if (findDerivative != base.end()) |
| 375 | { |
| 376 | derivativeCoeff = |
| 377 | std::visit(VariantToDoubleVisitor(), findDerivative->second); |
| 378 | } |
| 379 | |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 380 | info.pidInfo.negativeHysteresis = negativeHysteresis; |
| 381 | info.pidInfo.positiveHysteresis = positiveHysteresis; |
Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 382 | info.pidInfo.derivativeCoeff = derivativeCoeff; |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 385 | bool init(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer, |
Patrick Venture | 7382318 | 2020-10-08 15:12:51 -0700 | [diff] [blame] | 386 | std::map<std::string, conf::SensorConfig>& sensorConfig, |
| 387 | std::map<int64_t, conf::PIDConf>& zoneConfig, |
| 388 | std::map<int64_t, conf::ZoneConfig>& zoneDetailsConfig) |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 389 | { |
| 390 | |
| 391 | sensorConfig.clear(); |
| 392 | zoneConfig.clear(); |
| 393 | zoneDetailsConfig.clear(); |
| 394 | |
| 395 | createMatches(bus, timer); |
| 396 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 397 | auto mapper = |
| 398 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 399 | "/xyz/openbmc_project/object_mapper", |
| 400 | "xyz.openbmc_project.ObjectMapper", "GetSubTree"); |
James Feist | 26e8c6a | 2018-10-25 10:38:26 -0700 | [diff] [blame] | 401 | mapper.append("/", 0, |
Patrick Venture | 0911bfe | 2020-08-10 12:51:40 -0700 | [diff] [blame] | 402 | std::array<const char*, 6>{ |
| 403 | objectManagerInterface, pidConfigurationInterface, |
| 404 | pidZoneConfigurationInterface, |
| 405 | stepwiseConfigurationInterface, sensorInterface, |
| 406 | defaultPwmInterface}); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 407 | std::unordered_map< |
| 408 | std::string, std::unordered_map<std::string, std::vector<std::string>>> |
| 409 | respData; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 410 | try |
| 411 | { |
| 412 | auto resp = bus.call(mapper); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 413 | resp.read(respData); |
| 414 | } |
Patrick Williams | 0001ee0 | 2021-10-06 14:44:22 -0500 | [diff] [blame] | 415 | catch (const sdbusplus::exception_t&) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 416 | { |
| 417 | // can't do anything without mapper call data |
| 418 | throw std::runtime_error("ObjectMapper Call Failure"); |
| 419 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 420 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 421 | if (respData.empty()) |
| 422 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 423 | // can't do anything without mapper call data |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 424 | throw std::runtime_error("No configuration data available from Mapper"); |
| 425 | } |
| 426 | // create a map of pair of <has pid configuration, ObjectManager path> |
| 427 | std::unordered_map<std::string, std::pair<bool, std::string>> owners; |
| 428 | // and a map of <path, interface> for sensors |
| 429 | std::unordered_map<std::string, std::string> sensors; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 430 | for (const auto& objectPair : respData) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 431 | { |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 432 | for (const auto& ownerPair : objectPair.second) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 433 | { |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 434 | auto& owner = owners[ownerPair.first]; |
| 435 | for (const std::string& interface : ownerPair.second) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 436 | { |
| 437 | |
| 438 | if (interface == objectManagerInterface) |
| 439 | { |
| 440 | owner.second = objectPair.first; |
| 441 | } |
| 442 | if (interface == pidConfigurationInterface || |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 443 | interface == pidZoneConfigurationInterface || |
| 444 | interface == stepwiseConfigurationInterface) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 445 | { |
| 446 | owner.first = true; |
| 447 | } |
Patrick Venture | 0911bfe | 2020-08-10 12:51:40 -0700 | [diff] [blame] | 448 | if (interface == sensorInterface || |
| 449 | interface == defaultPwmInterface) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 450 | { |
| 451 | // we're not interested in pwm sensors, just pwm control |
| 452 | if (interface == sensorInterface && |
| 453 | objectPair.first.find("pwm") != std::string::npos) |
| 454 | { |
| 455 | continue; |
| 456 | } |
| 457 | sensors[objectPair.first] = interface; |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | ManagedObjectType configurations; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 463 | for (const auto& owner : owners) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 464 | { |
| 465 | // skip if no pid configuration (means probably a sensor) |
| 466 | if (!owner.second.first) |
| 467 | { |
| 468 | continue; |
| 469 | } |
| 470 | auto endpoint = bus.new_method_call( |
| 471 | owner.first.c_str(), owner.second.second.c_str(), |
| 472 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 473 | ManagedObjectType configuration; |
| 474 | try |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 475 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 476 | auto responce = bus.call(endpoint); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 477 | responce.read(configuration); |
| 478 | } |
Patrick Williams | 0001ee0 | 2021-10-06 14:44:22 -0500 | [diff] [blame] | 479 | catch (const sdbusplus::exception_t&) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 480 | { |
| 481 | // this shouldn't happen, probably means daemon crashed |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 482 | throw std::runtime_error("Error getting managed objects from " + |
| 483 | owner.first); |
| 484 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 485 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 486 | for (auto& pathPair : configuration) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 487 | { |
| 488 | if (pathPair.second.find(pidConfigurationInterface) != |
| 489 | pathPair.second.end() || |
| 490 | pathPair.second.find(pidZoneConfigurationInterface) != |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 491 | pathPair.second.end() || |
| 492 | pathPair.second.find(stepwiseConfigurationInterface) != |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 493 | pathPair.second.end()) |
| 494 | { |
| 495 | configurations.emplace(pathPair); |
| 496 | } |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | // remove controllers from config that aren't in the current profile(s) |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 501 | std::vector<std::string> selectedProfiles = getSelectedProfiles(bus); |
| 502 | if (selectedProfiles.size()) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 503 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 504 | for (auto pathIt = configurations.begin(); |
| 505 | pathIt != configurations.end();) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 506 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 507 | for (auto confIt = pathIt->second.begin(); |
| 508 | confIt != pathIt->second.end();) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 509 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 510 | auto profilesFind = confIt->second.find("Profiles"); |
| 511 | if (profilesFind == confIt->second.end()) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 512 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 513 | confIt++; |
| 514 | continue; // if no profiles selected, apply always |
| 515 | } |
| 516 | auto profiles = |
| 517 | std::get<std::vector<std::string>>(profilesFind->second); |
| 518 | if (profiles.empty()) |
| 519 | { |
| 520 | confIt++; |
| 521 | continue; |
| 522 | } |
| 523 | |
| 524 | bool found = false; |
| 525 | for (const std::string& profile : profiles) |
| 526 | { |
| 527 | if (std::find(selectedProfiles.begin(), |
| 528 | selectedProfiles.end(), |
| 529 | profile) != selectedProfiles.end()) |
| 530 | { |
| 531 | found = true; |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | if (found) |
| 536 | { |
| 537 | confIt++; |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 538 | } |
| 539 | else |
| 540 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 541 | confIt = pathIt->second.erase(confIt); |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 542 | } |
| 543 | } |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 544 | if (pathIt->second.empty()) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 545 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 546 | pathIt = configurations.erase(pathIt); |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 547 | } |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 548 | else |
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 | pathIt++; |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 551 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 552 | } |
| 553 | } |
James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 554 | |
Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 555 | // On D-Bus, although not necessary, |
| 556 | // having the "zoneID" field can still be useful, |
| 557 | // as it is used for diagnostic messages, |
| 558 | // logging file names, and so on. |
| 559 | // Accept optional "ZoneIndex" parameter to explicitly specify. |
| 560 | // If not present, or not unique, auto-assign index, |
| 561 | // using 0-based numbering, ensuring uniqueness. |
| 562 | std::map<std::string, int64_t> foundZones; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 563 | for (const auto& configuration : configurations) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 564 | { |
| 565 | auto findZone = |
| 566 | configuration.second.find(pidZoneConfigurationInterface); |
| 567 | if (findZone != configuration.second.end()) |
| 568 | { |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 569 | const auto& zone = findZone->second; |
James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 570 | |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 571 | const std::string& name = std::get<std::string>(zone.at("Name")); |
Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 572 | |
| 573 | auto findZoneIndex = zone.find("ZoneIndex"); |
| 574 | if (findZoneIndex == zone.end()) |
| 575 | { |
| 576 | continue; |
| 577 | } |
| 578 | |
| 579 | auto ptrZoneIndex = std::get_if<double>(&(findZoneIndex->second)); |
| 580 | if (!ptrZoneIndex) |
| 581 | { |
| 582 | continue; |
| 583 | } |
| 584 | |
| 585 | auto desiredIndex = static_cast<int64_t>(*ptrZoneIndex); |
| 586 | auto grantedIndex = setZoneIndex(name, foundZones, desiredIndex); |
| 587 | std::cout << "Zone " << name << " is at ZoneIndex " << grantedIndex |
| 588 | << "\n"; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | for (const auto& configuration : configurations) |
| 593 | { |
| 594 | auto findZone = |
| 595 | configuration.second.find(pidZoneConfigurationInterface); |
| 596 | if (findZone != configuration.second.end()) |
| 597 | { |
| 598 | const auto& zone = findZone->second; |
| 599 | |
| 600 | const std::string& name = std::get<std::string>(zone.at("Name")); |
| 601 | |
| 602 | auto index = getZoneIndex(name, foundZones); |
James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 603 | |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 604 | auto& details = zoneDetailsConfig[index]; |
Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 605 | |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 606 | details.minThermalOutput = std::visit(VariantToDoubleVisitor(), |
| 607 | zone.at("MinThermalOutput")); |
Patrick Venture | 8e2fdb3 | 2019-02-11 09:39:59 -0800 | [diff] [blame] | 608 | details.failsafePercent = std::visit(VariantToDoubleVisitor(), |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 609 | zone.at("FailSafePercent")); |
Josh Lehan | 9f9a06a | 2022-12-14 10:39:45 -0800 | [diff] [blame] | 610 | |
Harvey Wu | 239aa7d | 2022-11-18 08:43:34 +0800 | [diff] [blame^] | 611 | getCycleTimeSetting(zone, index, "CycleIntervalTimeMS", |
| 612 | details.cycleTime.cycleIntervalTimeMS); |
| 613 | getCycleTimeSetting(zone, index, "UpdateThermalsTimeMS", |
| 614 | details.cycleTime.updateThermalsTimeMS); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 615 | } |
| 616 | auto findBase = configuration.second.find(pidConfigurationInterface); |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 617 | // loop through all the PID configurations and fill out a sensor config |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 618 | if (findBase != configuration.second.end()) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 619 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 620 | const auto& base = |
| 621 | configuration.second.at(pidConfigurationInterface); |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 622 | const std::string pidName = std::get<std::string>(base.at("Name")); |
| 623 | const std::string pidClass = |
| 624 | std::get<std::string>(base.at("Class")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 625 | const std::vector<std::string>& zones = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 626 | std::get<std::vector<std::string>>(base.at("Zones")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 627 | for (const std::string& zone : zones) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 628 | { |
Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 629 | auto index = getZoneIndex(zone, foundZones); |
| 630 | |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 631 | conf::PIDConf& conf = zoneConfig[index]; |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 632 | std::vector<std::string> inputSensorNames( |
| 633 | std::get<std::vector<std::string>>(base.at("Inputs"))); |
| 634 | std::vector<std::string> outputSensorNames; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 635 | |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 636 | // assumption: all fan pids must have at least one output |
| 637 | if (pidClass == "fan") |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 638 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 639 | outputSensorNames = std::get<std::vector<std::string>>( |
| 640 | getPIDAttribute(base, "Outputs")); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 641 | } |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 642 | |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 643 | bool unavailableAsFailed = true; |
| 644 | auto findUnavailableAsFailed = |
| 645 | base.find("InputUnavailableAsFailed"); |
| 646 | if (findUnavailableAsFailed != base.end()) |
| 647 | { |
| 648 | unavailableAsFailed = |
| 649 | std::get<bool>(findUnavailableAsFailed->second); |
| 650 | } |
| 651 | |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 652 | std::vector<SensorInterfaceType> inputSensorInterfaces; |
| 653 | std::vector<SensorInterfaceType> outputSensorInterfaces; |
| 654 | /* populate an interface list for different sensor direction |
| 655 | * types (input,output) |
| 656 | */ |
| 657 | /* take the Inputs from the configuration and generate |
| 658 | * a list of dbus descriptors (path, interface). |
| 659 | * Mapping can be many-to-one since an element of Inputs can be |
| 660 | * a regex |
| 661 | */ |
| 662 | for (const std::string& sensorName : inputSensorNames) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 663 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 664 | findSensors(sensors, sensorNameToDbusName(sensorName), |
| 665 | inputSensorInterfaces); |
| 666 | } |
| 667 | for (const std::string& sensorName : outputSensorNames) |
| 668 | { |
| 669 | findSensors(sensors, sensorNameToDbusName(sensorName), |
| 670 | outputSensorInterfaces); |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 671 | } |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 672 | |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 673 | inputSensorNames.clear(); |
| 674 | for (const SensorInterfaceType& inputSensorInterface : |
| 675 | inputSensorInterfaces) |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 676 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 677 | const std::string& dbusInterface = |
| 678 | inputSensorInterface.second; |
| 679 | const std::string& inputSensorPath = |
| 680 | inputSensorInterface.first; |
Josh Lehan | fb82a87 | 2020-09-20 21:48:22 -0700 | [diff] [blame] | 681 | |
| 682 | // Setting timeout to 0 is intentional, as D-Bus passive |
| 683 | // sensor updates are pushed in, not pulled by timer poll. |
| 684 | // Setting ignoreDbusMinMax is intentional, as this |
| 685 | // prevents normalization of values to [0.0, 1.0] range, |
| 686 | // which would mess up the PID loop math. |
| 687 | // All non-fan PID classes should be initialized this way. |
| 688 | // As for why a fan should not use this code path, see |
| 689 | // the ed1dafdf168def37c65bfb7a5efd18d9dbe04727 commit. |
Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 690 | if ((pidClass == "temp") || (pidClass == "margin") || |
| 691 | (pidClass == "power") || (pidClass == "powersum")) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 692 | { |
Harvey.Wu | ed1dafd | 2022-02-09 13:53:20 +0800 | [diff] [blame] | 693 | std::string inputSensorName = |
| 694 | getSensorNameFromPath(inputSensorPath); |
| 695 | auto& config = sensorConfig[inputSensorName]; |
| 696 | inputSensorNames.push_back(inputSensorName); |
| 697 | config.type = pidClass; |
| 698 | config.readPath = inputSensorInterface.first; |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 699 | config.timeout = 0; |
| 700 | config.ignoreDbusMinMax = true; |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 701 | config.unavailableAsFailed = unavailableAsFailed; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 702 | } |
Josh Lehan | fb82a87 | 2020-09-20 21:48:22 -0700 | [diff] [blame] | 703 | |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 704 | if (dbusInterface != sensorInterface) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 705 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 706 | /* all expected inputs in the configuration are expected |
| 707 | * to be sensor interfaces |
| 708 | */ |
| 709 | throw std::runtime_error( |
| 710 | "sensor at dbus path [" + inputSensorPath + |
| 711 | "] has an interface [" + dbusInterface + |
| 712 | "] that does not match the expected interface of " + |
| 713 | sensorInterface); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 714 | } |
| 715 | } |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 716 | |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 717 | /* fan pids need to pair up tach sensors with their pwm |
| 718 | * counterparts |
| 719 | */ |
| 720 | if (pidClass == "fan") |
| 721 | { |
| 722 | /* If a PID is a fan there should be either |
| 723 | * (1) one output(pwm) per input(tach) |
| 724 | * OR |
| 725 | * (2) one putput(pwm) for all inputs(tach) |
| 726 | * everything else indicates a bad configuration. |
| 727 | */ |
| 728 | bool singlePwm = false; |
| 729 | if (outputSensorInterfaces.size() == 1) |
| 730 | { |
| 731 | /* one pwm, set write paths for all fan sensors to it */ |
| 732 | singlePwm = true; |
| 733 | } |
| 734 | else if (inputSensorInterfaces.size() == |
| 735 | outputSensorInterfaces.size()) |
| 736 | { |
| 737 | /* one to one mapping, each fan sensor gets its own pwm |
| 738 | * control */ |
| 739 | singlePwm = false; |
| 740 | } |
| 741 | else |
| 742 | { |
| 743 | throw std::runtime_error( |
| 744 | "fan PID has invalid number of Outputs"); |
| 745 | } |
| 746 | std::string fanSensorName; |
| 747 | std::string pwmPath; |
| 748 | std::string pwmInterface; |
Harvey.Wu | ed1dafd | 2022-02-09 13:53:20 +0800 | [diff] [blame] | 749 | std::string pwmSensorName; |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 750 | if (singlePwm) |
| 751 | { |
| 752 | /* if just a single output(pwm) is provided then use |
| 753 | * that pwm control path for all the fan sensor write |
| 754 | * path configs |
| 755 | */ |
| 756 | pwmPath = outputSensorInterfaces.at(0).first; |
| 757 | pwmInterface = outputSensorInterfaces.at(0).second; |
| 758 | } |
| 759 | for (uint32_t idx = 0; idx < inputSensorInterfaces.size(); |
| 760 | idx++) |
| 761 | { |
| 762 | if (!singlePwm) |
| 763 | { |
| 764 | pwmPath = outputSensorInterfaces.at(idx).first; |
| 765 | pwmInterface = |
| 766 | outputSensorInterfaces.at(idx).second; |
| 767 | } |
Patrick Venture | 0911bfe | 2020-08-10 12:51:40 -0700 | [diff] [blame] | 768 | if (defaultPwmInterface != pwmInterface) |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 769 | { |
| 770 | throw std::runtime_error( |
| 771 | "fan pwm control at dbus path [" + pwmPath + |
| 772 | "] has an interface [" + pwmInterface + |
| 773 | "] that does not match the expected interface " |
| 774 | "of " + |
Patrick Venture | 0911bfe | 2020-08-10 12:51:40 -0700 | [diff] [blame] | 775 | defaultPwmInterface); |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 776 | } |
| 777 | const std::string& fanPath = |
| 778 | inputSensorInterfaces.at(idx).first; |
| 779 | fanSensorName = getSensorNameFromPath(fanPath); |
Harvey.Wu | ed1dafd | 2022-02-09 13:53:20 +0800 | [diff] [blame] | 780 | pwmSensorName = getSensorNameFromPath(pwmPath); |
| 781 | std::string fanPwmIndex = fanSensorName + pwmSensorName; |
| 782 | inputSensorNames.push_back(fanPwmIndex); |
| 783 | auto& fanConfig = sensorConfig[fanPwmIndex]; |
| 784 | fanConfig.type = pidClass; |
| 785 | fanConfig.readPath = fanPath; |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 786 | fanConfig.writePath = pwmPath; |
| 787 | // todo: un-hardcode this if there are fans with |
| 788 | // different ranges |
| 789 | fanConfig.max = 255; |
| 790 | fanConfig.min = 0; |
| 791 | } |
| 792 | } |
James Feist | 11d243d | 2019-06-24 16:18:40 -0700 | [diff] [blame] | 793 | // if the sensors aren't available in the current state, don't |
| 794 | // add them to the configuration. |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 795 | if (inputSensorNames.empty()) |
James Feist | 11d243d | 2019-06-24 16:18:40 -0700 | [diff] [blame] | 796 | { |
| 797 | continue; |
| 798 | } |
| 799 | |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 800 | std::string offsetType; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 801 | |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 802 | // SetPointOffset is a threshold value to pull from the sensor |
| 803 | // to apply an offset. For upper thresholds this means the |
| 804 | // setpoint is usually negative. |
| 805 | auto findSetpointOffset = base.find("SetPointOffset"); |
| 806 | if (findSetpointOffset != base.end()) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 807 | { |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 808 | offsetType = |
| 809 | std::get<std::string>(findSetpointOffset->second); |
| 810 | if (std::find(thresholds::types.begin(), |
| 811 | thresholds::types.end(), |
| 812 | offsetType) == thresholds::types.end()) |
| 813 | { |
| 814 | throw std::runtime_error("Unsupported type: " + |
| 815 | offsetType); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | if (offsetType.empty()) |
| 820 | { |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 821 | conf::ControllerInfo& info = |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 822 | conf[std::get<std::string>(base.at("Name"))]; |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 823 | info.inputs = std::move(inputSensorNames); |
Patrick Venture | 7382318 | 2020-10-08 15:12:51 -0700 | [diff] [blame] | 824 | populatePidInfo(bus, base, info, nullptr, sensorConfig); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 825 | } |
| 826 | else |
| 827 | { |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 828 | // we have to split up the inputs, as in practice t-control |
| 829 | // values will differ, making setpoints differ |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 830 | for (const std::string& input : inputSensorNames) |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 831 | { |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 832 | conf::ControllerInfo& info = conf[input]; |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 833 | info.inputs.emplace_back(input); |
Patrick Venture | 7382318 | 2020-10-08 15:12:51 -0700 | [diff] [blame] | 834 | populatePidInfo(bus, base, info, &offsetType, |
| 835 | sensorConfig); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 836 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 837 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | auto findStepwise = |
| 841 | configuration.second.find(stepwiseConfigurationInterface); |
| 842 | if (findStepwise != configuration.second.end()) |
| 843 | { |
| 844 | const auto& base = findStepwise->second; |
| 845 | const std::vector<std::string>& zones = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 846 | std::get<std::vector<std::string>>(base.at("Zones")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 847 | for (const std::string& zone : zones) |
| 848 | { |
Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 849 | auto index = getZoneIndex(zone, foundZones); |
| 850 | |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 851 | conf::PIDConf& conf = zoneConfig[index]; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 852 | |
| 853 | std::vector<std::string> inputs; |
| 854 | std::vector<std::string> sensorNames = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 855 | std::get<std::vector<std::string>>(base.at("Inputs")); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 856 | |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 857 | bool unavailableAsFailed = true; |
| 858 | auto findUnavailableAsFailed = |
| 859 | base.find("InputUnavailableAsFailed"); |
| 860 | if (findUnavailableAsFailed != base.end()) |
| 861 | { |
| 862 | unavailableAsFailed = |
| 863 | std::get<bool>(findUnavailableAsFailed->second); |
| 864 | } |
| 865 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 866 | bool sensorFound = false; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 867 | for (const std::string& sensorName : sensorNames) |
| 868 | { |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 869 | std::vector<std::pair<std::string, std::string>> |
| 870 | sensorPathIfacePairs; |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 871 | if (!findSensors(sensors, sensorNameToDbusName(sensorName), |
| 872 | sensorPathIfacePairs)) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 873 | { |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 874 | break; |
| 875 | } |
| 876 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 877 | for (const auto& sensorPathIfacePair : sensorPathIfacePairs) |
| 878 | { |
| 879 | size_t idx = |
| 880 | sensorPathIfacePair.first.find_last_of("/") + 1; |
| 881 | std::string shortName = |
| 882 | sensorPathIfacePair.first.substr(idx); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 883 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 884 | inputs.push_back(shortName); |
| 885 | auto& config = sensorConfig[shortName]; |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 886 | config.readPath = sensorPathIfacePair.first; |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 887 | config.type = "temp"; |
James Feist | 3660b38 | 2019-11-11 16:29:19 -0800 | [diff] [blame] | 888 | config.ignoreDbusMinMax = true; |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 889 | config.unavailableAsFailed = unavailableAsFailed; |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 890 | // todo: maybe un-hardcode this if we run into slower |
| 891 | // timeouts with sensors |
| 892 | |
James Feist | 2642cb5 | 2019-02-25 13:00:16 -0800 | [diff] [blame] | 893 | config.timeout = 0; |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 894 | sensorFound = true; |
| 895 | } |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 896 | } |
| 897 | if (!sensorFound) |
| 898 | { |
| 899 | continue; |
| 900 | } |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 901 | conf::ControllerInfo& info = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 902 | conf[std::get<std::string>(base.at("Name"))]; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 903 | info.inputs = std::move(inputs); |
| 904 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 905 | info.type = "stepwise"; |
| 906 | info.stepwiseInfo.ts = 1.0; // currently unused |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 907 | info.stepwiseInfo.positiveHysteresis = 0.0; |
| 908 | info.stepwiseInfo.negativeHysteresis = 0.0; |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 909 | std::string subtype = std::get<std::string>(base.at("Class")); |
| 910 | |
| 911 | info.stepwiseInfo.isCeiling = (subtype == "Ceiling"); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 912 | auto findPosHyst = base.find("PositiveHysteresis"); |
| 913 | auto findNegHyst = base.find("NegativeHysteresis"); |
| 914 | if (findPosHyst != base.end()) |
| 915 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 916 | info.stepwiseInfo.positiveHysteresis = std::visit( |
James Feist | 208abce | 2018-12-06 09:59:10 -0800 | [diff] [blame] | 917 | VariantToDoubleVisitor(), findPosHyst->second); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 918 | } |
| 919 | if (findNegHyst != base.end()) |
| 920 | { |
James Feist | 5782ab8 | 2019-04-02 08:38:48 -0700 | [diff] [blame] | 921 | info.stepwiseInfo.negativeHysteresis = std::visit( |
James Feist | 208abce | 2018-12-06 09:59:10 -0800 | [diff] [blame] | 922 | VariantToDoubleVisitor(), findNegHyst->second); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 923 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 924 | std::vector<double> readings = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 925 | std::get<std::vector<double>>(base.at("Reading")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 926 | if (readings.size() > ec::maxStepwisePoints) |
| 927 | { |
| 928 | throw std::invalid_argument("Too many stepwise points."); |
| 929 | } |
| 930 | if (readings.empty()) |
| 931 | { |
| 932 | throw std::invalid_argument( |
| 933 | "Must have one stepwise point."); |
| 934 | } |
| 935 | std::copy(readings.begin(), readings.end(), |
| 936 | info.stepwiseInfo.reading); |
| 937 | if (readings.size() < ec::maxStepwisePoints) |
| 938 | { |
| 939 | info.stepwiseInfo.reading[readings.size()] = |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 940 | std::numeric_limits<double>::quiet_NaN(); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 941 | } |
| 942 | std::vector<double> outputs = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 943 | std::get<std::vector<double>>(base.at("Output")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 944 | if (readings.size() != outputs.size()) |
| 945 | { |
| 946 | throw std::invalid_argument( |
| 947 | "Outputs size must match readings"); |
| 948 | } |
| 949 | std::copy(outputs.begin(), outputs.end(), |
| 950 | info.stepwiseInfo.output); |
| 951 | if (outputs.size() < ec::maxStepwisePoints) |
| 952 | { |
| 953 | info.stepwiseInfo.output[outputs.size()] = |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 954 | std::numeric_limits<double>::quiet_NaN(); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 955 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | } |
Patrick Venture | 39199b4 | 2020-10-08 14:40:29 -0700 | [diff] [blame] | 959 | if constexpr (pid_control::conf::DEBUG) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 960 | { |
Patrick Venture | 39199b4 | 2020-10-08 14:40:29 -0700 | [diff] [blame] | 961 | debugPrint(sensorConfig, zoneConfig, zoneDetailsConfig); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 962 | } |
James Feist | c959c42 | 2018-11-01 12:33:40 -0700 | [diff] [blame] | 963 | if (zoneConfig.empty() || zoneDetailsConfig.empty()) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 964 | { |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 965 | std::cerr |
| 966 | << "No fan zones, application pausing until new configuration\n"; |
| 967 | return false; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 968 | } |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 969 | return true; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 970 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 971 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 972 | } // namespace dbus_configuration |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 973 | } // namespace pid_control |