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