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 */ 16 #include "dbusconfiguration.hpp" 17 18 #include "conf.hpp" 19 #include "dbushelper.hpp" 20 #include "dbusutil.hpp" 21 #include "util.hpp" 22 23 #include <boost/asio/steady_timer.hpp> 24 #include <sdbusplus/bus.hpp> 25 #include <sdbusplus/bus/match.hpp> 26 #include <sdbusplus/exception.hpp> 27 28 #include <algorithm> 29 #include <chrono> 30 #include <functional> 31 #include <iostream> 32 #include <list> 33 #include <set> 34 #include <unordered_map> 35 #include <variant> 36 37 namespace pid_control 38 { 39 40 constexpr const char* pidConfigurationInterface = 41 "xyz.openbmc_project.Configuration.Pid"; 42 constexpr const char* objectManagerInterface = 43 "org.freedesktop.DBus.ObjectManager"; 44 constexpr const char* pidZoneConfigurationInterface = 45 "xyz.openbmc_project.Configuration.Pid.Zone"; 46 constexpr const char* stepwiseConfigurationInterface = 47 "xyz.openbmc_project.Configuration.Stepwise"; 48 constexpr const char* thermalControlIface = 49 "xyz.openbmc_project.Control.ThermalMode"; 50 constexpr const char* sensorInterface = "xyz.openbmc_project.Sensor.Value"; 51 constexpr const char* defaultPwmInterface = 52 "xyz.openbmc_project.Control.FanPwm"; 53 54 using Association = std::tuple<std::string, std::string, std::string>; 55 using Associations = std::vector<Association>; 56 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 68 namespace dbus_configuration 69 { 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 } 83 84 std::vector<std::string> getSelectedProfiles(sdbusplus::bus_t& bus) 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 } 101 catch (const sdbusplus::exception_t&) 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 } 132 catch (const sdbusplus::exception_t&) 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 } 140 if constexpr (pid_control::conf::DEBUG) 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 152 int eventHandler(sd_bus_message* m, void* context, sd_bus_error*) 153 { 154 155 if (context == nullptr || m == nullptr) 156 { 157 throw std::runtime_error("Invalid match"); 158 } 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 164 sdbusplus::message_t message(m); 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 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."; 203 tryRestartControlLoops(); 204 }); 205 206 return 1; 207 } 208 209 void createMatches(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer) 210 { 211 // this is a list because the matches can't be moved 212 static std::list<sdbusplus::bus::match_t> matches; 213 214 const std::array<std::string, 4> interfaces = { 215 thermalControlIface, pidConfigurationInterface, 216 pidZoneConfigurationInterface, stepwiseConfigurationInterface}; 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 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 260 void populatePidInfo( 261 [[maybe_unused]] sdbusplus::bus_t& bus, 262 const std::unordered_map<std::string, DbusVariantType>& base, 263 conf::ControllerInfo& info, const std::string* thresholdProperty, 264 const std::map<std::string, conf::SensorConfig>& sensorConfig) 265 { 266 info.type = std::get<std::string>(getPIDAttribute(base, "Class")); 267 if (info.type == "fan") 268 { 269 info.setpoint = 0; 270 } 271 else 272 { 273 info.setpoint = std::visit(VariantToDoubleVisitor(), 274 getPIDAttribute(base, "SetPoint")); 275 } 276 277 if (thresholdProperty != nullptr) 278 { 279 std::string interface; 280 if (*thresholdProperty == "WarningHigh" || 281 *thresholdProperty == "WarningLow") 282 { 283 interface = thresholds::warningInterface; 284 } 285 else 286 { 287 interface = thresholds::criticalInterface; 288 } 289 const std::string& path = sensorConfig.at(info.inputs.front()).readPath; 290 291 DbusHelper helper(sdbusplus::bus::new_system()); 292 std::string service = helper.getService(interface, path); 293 double reading = 0; 294 try 295 { 296 helper.getProperty(service, path, interface, *thresholdProperty, 297 reading); 298 } 299 catch (const sdbusplus::exception_t& ex) 300 { 301 // unsupported threshold, leaving reading at 0 302 } 303 304 info.setpoint += reading; 305 } 306 307 info.pidInfo.ts = 1.0; // currently unused 308 info.pidInfo.proportionalCoeff = std::visit( 309 VariantToDoubleVisitor(), getPIDAttribute(base, "PCoefficient")); 310 info.pidInfo.integralCoeff = std::visit( 311 VariantToDoubleVisitor(), getPIDAttribute(base, "ICoefficient")); 312 info.pidInfo.feedFwdOffset = std::visit( 313 VariantToDoubleVisitor(), getPIDAttribute(base, "FFOffCoefficient")); 314 info.pidInfo.feedFwdGain = std::visit( 315 VariantToDoubleVisitor(), getPIDAttribute(base, "FFGainCoefficient")); 316 info.pidInfo.integralLimit.max = std::visit( 317 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMax")); 318 info.pidInfo.integralLimit.min = std::visit( 319 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMin")); 320 info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(), 321 getPIDAttribute(base, "OutLimitMax")); 322 info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(), 323 getPIDAttribute(base, "OutLimitMin")); 324 info.pidInfo.slewNeg = 325 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewNeg")); 326 info.pidInfo.slewPos = 327 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewPos")); 328 double negativeHysteresis = 0; 329 double positiveHysteresis = 0; 330 331 auto findNeg = base.find("NegativeHysteresis"); 332 auto findPos = base.find("PositiveHysteresis"); 333 334 if (findNeg != base.end()) 335 { 336 negativeHysteresis = 337 std::visit(VariantToDoubleVisitor(), findNeg->second); 338 } 339 340 if (findPos != base.end()) 341 { 342 positiveHysteresis = 343 std::visit(VariantToDoubleVisitor(), findPos->second); 344 } 345 info.pidInfo.negativeHysteresis = negativeHysteresis; 346 info.pidInfo.positiveHysteresis = positiveHysteresis; 347 } 348 349 bool init(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer, 350 std::map<std::string, conf::SensorConfig>& sensorConfig, 351 std::map<int64_t, conf::PIDConf>& zoneConfig, 352 std::map<int64_t, conf::ZoneConfig>& zoneDetailsConfig) 353 { 354 355 sensorConfig.clear(); 356 zoneConfig.clear(); 357 zoneDetailsConfig.clear(); 358 359 createMatches(bus, timer); 360 361 auto mapper = 362 bus.new_method_call("xyz.openbmc_project.ObjectMapper", 363 "/xyz/openbmc_project/object_mapper", 364 "xyz.openbmc_project.ObjectMapper", "GetSubTree"); 365 mapper.append("/", 0, 366 std::array<const char*, 6>{ 367 objectManagerInterface, pidConfigurationInterface, 368 pidZoneConfigurationInterface, 369 stepwiseConfigurationInterface, sensorInterface, 370 defaultPwmInterface}); 371 std::unordered_map< 372 std::string, std::unordered_map<std::string, std::vector<std::string>>> 373 respData; 374 try 375 { 376 auto resp = bus.call(mapper); 377 resp.read(respData); 378 } 379 catch (const sdbusplus::exception_t&) 380 { 381 // can't do anything without mapper call data 382 throw std::runtime_error("ObjectMapper Call Failure"); 383 } 384 385 if (respData.empty()) 386 { 387 // can't do anything without mapper call data 388 throw std::runtime_error("No configuration data available from Mapper"); 389 } 390 // create a map of pair of <has pid configuration, ObjectManager path> 391 std::unordered_map<std::string, std::pair<bool, std::string>> owners; 392 // and a map of <path, interface> for sensors 393 std::unordered_map<std::string, std::string> sensors; 394 for (const auto& objectPair : respData) 395 { 396 for (const auto& ownerPair : objectPair.second) 397 { 398 auto& owner = owners[ownerPair.first]; 399 for (const std::string& interface : ownerPair.second) 400 { 401 402 if (interface == objectManagerInterface) 403 { 404 owner.second = objectPair.first; 405 } 406 if (interface == pidConfigurationInterface || 407 interface == pidZoneConfigurationInterface || 408 interface == stepwiseConfigurationInterface) 409 { 410 owner.first = true; 411 } 412 if (interface == sensorInterface || 413 interface == defaultPwmInterface) 414 { 415 // we're not interested in pwm sensors, just pwm control 416 if (interface == sensorInterface && 417 objectPair.first.find("pwm") != std::string::npos) 418 { 419 continue; 420 } 421 sensors[objectPair.first] = interface; 422 } 423 } 424 } 425 } 426 ManagedObjectType configurations; 427 for (const auto& owner : owners) 428 { 429 // skip if no pid configuration (means probably a sensor) 430 if (!owner.second.first) 431 { 432 continue; 433 } 434 auto endpoint = bus.new_method_call( 435 owner.first.c_str(), owner.second.second.c_str(), 436 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 437 ManagedObjectType configuration; 438 try 439 { 440 auto responce = bus.call(endpoint); 441 responce.read(configuration); 442 } 443 catch (const sdbusplus::exception_t&) 444 { 445 // this shouldn't happen, probably means daemon crashed 446 throw std::runtime_error("Error getting managed objects from " + 447 owner.first); 448 } 449 450 for (auto& pathPair : configuration) 451 { 452 if (pathPair.second.find(pidConfigurationInterface) != 453 pathPair.second.end() || 454 pathPair.second.find(pidZoneConfigurationInterface) != 455 pathPair.second.end() || 456 pathPair.second.find(stepwiseConfigurationInterface) != 457 pathPair.second.end()) 458 { 459 configurations.emplace(pathPair); 460 } 461 } 462 } 463 464 // remove controllers from config that aren't in the current profile(s) 465 std::vector<std::string> selectedProfiles = getSelectedProfiles(bus); 466 if (selectedProfiles.size()) 467 { 468 for (auto pathIt = configurations.begin(); 469 pathIt != configurations.end();) 470 { 471 for (auto confIt = pathIt->second.begin(); 472 confIt != pathIt->second.end();) 473 { 474 auto profilesFind = confIt->second.find("Profiles"); 475 if (profilesFind == confIt->second.end()) 476 { 477 confIt++; 478 continue; // if no profiles selected, apply always 479 } 480 auto profiles = 481 std::get<std::vector<std::string>>(profilesFind->second); 482 if (profiles.empty()) 483 { 484 confIt++; 485 continue; 486 } 487 488 bool found = false; 489 for (const std::string& profile : profiles) 490 { 491 if (std::find(selectedProfiles.begin(), 492 selectedProfiles.end(), 493 profile) != selectedProfiles.end()) 494 { 495 found = true; 496 break; 497 } 498 } 499 if (found) 500 { 501 confIt++; 502 } 503 else 504 { 505 confIt = pathIt->second.erase(confIt); 506 } 507 } 508 if (pathIt->second.empty()) 509 { 510 pathIt = configurations.erase(pathIt); 511 } 512 else 513 { 514 pathIt++; 515 } 516 } 517 } 518 519 // On D-Bus, although not necessary, 520 // having the "zoneID" field can still be useful, 521 // as it is used for diagnostic messages, 522 // logging file names, and so on. 523 // Accept optional "ZoneIndex" parameter to explicitly specify. 524 // If not present, or not unique, auto-assign index, 525 // using 0-based numbering, ensuring uniqueness. 526 std::map<std::string, int64_t> foundZones; 527 for (const auto& configuration : configurations) 528 { 529 auto findZone = 530 configuration.second.find(pidZoneConfigurationInterface); 531 if (findZone != configuration.second.end()) 532 { 533 const auto& zone = findZone->second; 534 535 const std::string& name = std::get<std::string>(zone.at("Name")); 536 537 auto findZoneIndex = zone.find("ZoneIndex"); 538 if (findZoneIndex == zone.end()) 539 { 540 continue; 541 } 542 543 auto ptrZoneIndex = std::get_if<double>(&(findZoneIndex->second)); 544 if (!ptrZoneIndex) 545 { 546 continue; 547 } 548 549 auto desiredIndex = static_cast<int64_t>(*ptrZoneIndex); 550 auto grantedIndex = setZoneIndex(name, foundZones, desiredIndex); 551 std::cout << "Zone " << name << " is at ZoneIndex " << grantedIndex 552 << "\n"; 553 } 554 } 555 556 for (const auto& configuration : configurations) 557 { 558 auto findZone = 559 configuration.second.find(pidZoneConfigurationInterface); 560 if (findZone != configuration.second.end()) 561 { 562 const auto& zone = findZone->second; 563 564 const std::string& name = std::get<std::string>(zone.at("Name")); 565 566 auto index = getZoneIndex(name, foundZones); 567 568 auto& details = zoneDetailsConfig[index]; 569 570 details.minThermalOutput = std::visit(VariantToDoubleVisitor(), 571 zone.at("MinThermalOutput")); 572 details.failsafePercent = std::visit(VariantToDoubleVisitor(), 573 zone.at("FailSafePercent")); 574 } 575 auto findBase = configuration.second.find(pidConfigurationInterface); 576 // loop through all the PID configurations and fill out a sensor config 577 if (findBase != configuration.second.end()) 578 { 579 const auto& base = 580 configuration.second.at(pidConfigurationInterface); 581 const std::string pidName = std::get<std::string>(base.at("Name")); 582 const std::string pidClass = 583 std::get<std::string>(base.at("Class")); 584 const std::vector<std::string>& zones = 585 std::get<std::vector<std::string>>(base.at("Zones")); 586 for (const std::string& zone : zones) 587 { 588 auto index = getZoneIndex(zone, foundZones); 589 590 conf::PIDConf& conf = zoneConfig[index]; 591 std::vector<std::string> inputSensorNames( 592 std::get<std::vector<std::string>>(base.at("Inputs"))); 593 std::vector<std::string> outputSensorNames; 594 595 // assumption: all fan pids must have at least one output 596 if (pidClass == "fan") 597 { 598 outputSensorNames = std::get<std::vector<std::string>>( 599 getPIDAttribute(base, "Outputs")); 600 } 601 602 bool unavailableAsFailed = true; 603 auto findUnavailableAsFailed = 604 base.find("InputUnavailableAsFailed"); 605 if (findUnavailableAsFailed != base.end()) 606 { 607 unavailableAsFailed = 608 std::get<bool>(findUnavailableAsFailed->second); 609 } 610 611 std::vector<SensorInterfaceType> inputSensorInterfaces; 612 std::vector<SensorInterfaceType> outputSensorInterfaces; 613 /* populate an interface list for different sensor direction 614 * types (input,output) 615 */ 616 /* take the Inputs from the configuration and generate 617 * a list of dbus descriptors (path, interface). 618 * Mapping can be many-to-one since an element of Inputs can be 619 * a regex 620 */ 621 for (const std::string& sensorName : inputSensorNames) 622 { 623 findSensors(sensors, sensorNameToDbusName(sensorName), 624 inputSensorInterfaces); 625 } 626 for (const std::string& sensorName : outputSensorNames) 627 { 628 findSensors(sensors, sensorNameToDbusName(sensorName), 629 outputSensorInterfaces); 630 } 631 632 inputSensorNames.clear(); 633 for (const SensorInterfaceType& inputSensorInterface : 634 inputSensorInterfaces) 635 { 636 const std::string& dbusInterface = 637 inputSensorInterface.second; 638 const std::string& inputSensorPath = 639 inputSensorInterface.first; 640 641 // Setting timeout to 0 is intentional, as D-Bus passive 642 // sensor updates are pushed in, not pulled by timer poll. 643 // Setting ignoreDbusMinMax is intentional, as this 644 // prevents normalization of values to [0.0, 1.0] range, 645 // which would mess up the PID loop math. 646 // All non-fan PID classes should be initialized this way. 647 // As for why a fan should not use this code path, see 648 // the ed1dafdf168def37c65bfb7a5efd18d9dbe04727 commit. 649 if ((pidClass == "temp") || (pidClass == "margin") || 650 (pidClass == "power") || (pidClass == "powersum")) 651 { 652 std::string inputSensorName = 653 getSensorNameFromPath(inputSensorPath); 654 auto& config = sensorConfig[inputSensorName]; 655 inputSensorNames.push_back(inputSensorName); 656 config.type = pidClass; 657 config.readPath = inputSensorInterface.first; 658 config.timeout = 0; 659 config.ignoreDbusMinMax = true; 660 config.unavailableAsFailed = unavailableAsFailed; 661 } 662 663 if (dbusInterface != sensorInterface) 664 { 665 /* all expected inputs in the configuration are expected 666 * to be sensor interfaces 667 */ 668 throw std::runtime_error( 669 "sensor at dbus path [" + inputSensorPath + 670 "] has an interface [" + dbusInterface + 671 "] that does not match the expected interface of " + 672 sensorInterface); 673 } 674 } 675 676 /* fan pids need to pair up tach sensors with their pwm 677 * counterparts 678 */ 679 if (pidClass == "fan") 680 { 681 /* If a PID is a fan there should be either 682 * (1) one output(pwm) per input(tach) 683 * OR 684 * (2) one putput(pwm) for all inputs(tach) 685 * everything else indicates a bad configuration. 686 */ 687 bool singlePwm = false; 688 if (outputSensorInterfaces.size() == 1) 689 { 690 /* one pwm, set write paths for all fan sensors to it */ 691 singlePwm = true; 692 } 693 else if (inputSensorInterfaces.size() == 694 outputSensorInterfaces.size()) 695 { 696 /* one to one mapping, each fan sensor gets its own pwm 697 * control */ 698 singlePwm = false; 699 } 700 else 701 { 702 throw std::runtime_error( 703 "fan PID has invalid number of Outputs"); 704 } 705 std::string fanSensorName; 706 std::string pwmPath; 707 std::string pwmInterface; 708 std::string pwmSensorName; 709 if (singlePwm) 710 { 711 /* if just a single output(pwm) is provided then use 712 * that pwm control path for all the fan sensor write 713 * path configs 714 */ 715 pwmPath = outputSensorInterfaces.at(0).first; 716 pwmInterface = outputSensorInterfaces.at(0).second; 717 } 718 for (uint32_t idx = 0; idx < inputSensorInterfaces.size(); 719 idx++) 720 { 721 if (!singlePwm) 722 { 723 pwmPath = outputSensorInterfaces.at(idx).first; 724 pwmInterface = 725 outputSensorInterfaces.at(idx).second; 726 } 727 if (defaultPwmInterface != pwmInterface) 728 { 729 throw std::runtime_error( 730 "fan pwm control at dbus path [" + pwmPath + 731 "] has an interface [" + pwmInterface + 732 "] that does not match the expected interface " 733 "of " + 734 defaultPwmInterface); 735 } 736 const std::string& fanPath = 737 inputSensorInterfaces.at(idx).first; 738 fanSensorName = getSensorNameFromPath(fanPath); 739 pwmSensorName = getSensorNameFromPath(pwmPath); 740 std::string fanPwmIndex = fanSensorName + pwmSensorName; 741 inputSensorNames.push_back(fanPwmIndex); 742 auto& fanConfig = sensorConfig[fanPwmIndex]; 743 fanConfig.type = pidClass; 744 fanConfig.readPath = fanPath; 745 fanConfig.writePath = pwmPath; 746 // todo: un-hardcode this if there are fans with 747 // different ranges 748 fanConfig.max = 255; 749 fanConfig.min = 0; 750 } 751 } 752 // if the sensors aren't available in the current state, don't 753 // add them to the configuration. 754 if (inputSensorNames.empty()) 755 { 756 continue; 757 } 758 759 std::string offsetType; 760 761 // SetPointOffset is a threshold value to pull from the sensor 762 // to apply an offset. For upper thresholds this means the 763 // setpoint is usually negative. 764 auto findSetpointOffset = base.find("SetPointOffset"); 765 if (findSetpointOffset != base.end()) 766 { 767 offsetType = 768 std::get<std::string>(findSetpointOffset->second); 769 if (std::find(thresholds::types.begin(), 770 thresholds::types.end(), 771 offsetType) == thresholds::types.end()) 772 { 773 throw std::runtime_error("Unsupported type: " + 774 offsetType); 775 } 776 } 777 778 if (offsetType.empty()) 779 { 780 conf::ControllerInfo& info = 781 conf[std::get<std::string>(base.at("Name"))]; 782 info.inputs = std::move(inputSensorNames); 783 populatePidInfo(bus, base, info, nullptr, sensorConfig); 784 } 785 else 786 { 787 // we have to split up the inputs, as in practice t-control 788 // values will differ, making setpoints differ 789 for (const std::string& input : inputSensorNames) 790 { 791 conf::ControllerInfo& info = conf[input]; 792 info.inputs.emplace_back(input); 793 populatePidInfo(bus, base, info, &offsetType, 794 sensorConfig); 795 } 796 } 797 } 798 } 799 auto findStepwise = 800 configuration.second.find(stepwiseConfigurationInterface); 801 if (findStepwise != configuration.second.end()) 802 { 803 const auto& base = findStepwise->second; 804 const std::vector<std::string>& zones = 805 std::get<std::vector<std::string>>(base.at("Zones")); 806 for (const std::string& zone : zones) 807 { 808 auto index = getZoneIndex(zone, foundZones); 809 810 conf::PIDConf& conf = zoneConfig[index]; 811 812 std::vector<std::string> inputs; 813 std::vector<std::string> sensorNames = 814 std::get<std::vector<std::string>>(base.at("Inputs")); 815 816 bool unavailableAsFailed = true; 817 auto findUnavailableAsFailed = 818 base.find("InputUnavailableAsFailed"); 819 if (findUnavailableAsFailed != base.end()) 820 { 821 unavailableAsFailed = 822 std::get<bool>(findUnavailableAsFailed->second); 823 } 824 825 bool sensorFound = false; 826 for (const std::string& sensorName : sensorNames) 827 { 828 std::vector<std::pair<std::string, std::string>> 829 sensorPathIfacePairs; 830 if (!findSensors(sensors, sensorNameToDbusName(sensorName), 831 sensorPathIfacePairs)) 832 { 833 break; 834 } 835 836 for (const auto& sensorPathIfacePair : sensorPathIfacePairs) 837 { 838 size_t idx = 839 sensorPathIfacePair.first.find_last_of("/") + 1; 840 std::string shortName = 841 sensorPathIfacePair.first.substr(idx); 842 843 inputs.push_back(shortName); 844 auto& config = sensorConfig[shortName]; 845 config.readPath = sensorPathIfacePair.first; 846 config.type = "temp"; 847 config.ignoreDbusMinMax = true; 848 config.unavailableAsFailed = unavailableAsFailed; 849 // todo: maybe un-hardcode this if we run into slower 850 // timeouts with sensors 851 852 config.timeout = 0; 853 sensorFound = true; 854 } 855 } 856 if (!sensorFound) 857 { 858 continue; 859 } 860 conf::ControllerInfo& info = 861 conf[std::get<std::string>(base.at("Name"))]; 862 info.inputs = std::move(inputs); 863 864 info.type = "stepwise"; 865 info.stepwiseInfo.ts = 1.0; // currently unused 866 info.stepwiseInfo.positiveHysteresis = 0.0; 867 info.stepwiseInfo.negativeHysteresis = 0.0; 868 std::string subtype = std::get<std::string>(base.at("Class")); 869 870 info.stepwiseInfo.isCeiling = (subtype == "Ceiling"); 871 auto findPosHyst = base.find("PositiveHysteresis"); 872 auto findNegHyst = base.find("NegativeHysteresis"); 873 if (findPosHyst != base.end()) 874 { 875 info.stepwiseInfo.positiveHysteresis = std::visit( 876 VariantToDoubleVisitor(), findPosHyst->second); 877 } 878 if (findNegHyst != base.end()) 879 { 880 info.stepwiseInfo.negativeHysteresis = std::visit( 881 VariantToDoubleVisitor(), findNegHyst->second); 882 } 883 std::vector<double> readings = 884 std::get<std::vector<double>>(base.at("Reading")); 885 if (readings.size() > ec::maxStepwisePoints) 886 { 887 throw std::invalid_argument("Too many stepwise points."); 888 } 889 if (readings.empty()) 890 { 891 throw std::invalid_argument( 892 "Must have one stepwise point."); 893 } 894 std::copy(readings.begin(), readings.end(), 895 info.stepwiseInfo.reading); 896 if (readings.size() < ec::maxStepwisePoints) 897 { 898 info.stepwiseInfo.reading[readings.size()] = 899 std::numeric_limits<double>::quiet_NaN(); 900 } 901 std::vector<double> outputs = 902 std::get<std::vector<double>>(base.at("Output")); 903 if (readings.size() != outputs.size()) 904 { 905 throw std::invalid_argument( 906 "Outputs size must match readings"); 907 } 908 std::copy(outputs.begin(), outputs.end(), 909 info.stepwiseInfo.output); 910 if (outputs.size() < ec::maxStepwisePoints) 911 { 912 info.stepwiseInfo.output[outputs.size()] = 913 std::numeric_limits<double>::quiet_NaN(); 914 } 915 } 916 } 917 } 918 if constexpr (pid_control::conf::DEBUG) 919 { 920 debugPrint(sensorConfig, zoneConfig, zoneDetailsConfig); 921 } 922 if (zoneConfig.empty() || zoneDetailsConfig.empty()) 923 { 924 std::cerr 925 << "No fan zones, application pausing until new configuration\n"; 926 return false; 927 } 928 return true; 929 } 930 931 } // namespace dbus_configuration 932 } // namespace pid_control 933