1 /** 2 * Copyright 2017 Google Inc. 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 17 #include "pid/builder.hpp" 18 19 #include "conf.hpp" 20 #include "pid/controller.hpp" 21 #include "pid/fancontroller.hpp" 22 #include "pid/stepwisecontroller.hpp" 23 #include "pid/thermalcontroller.hpp" 24 #include "pid/zone.hpp" 25 #include "pid/zone_interface.hpp" 26 27 #include <sdbusplus/bus.hpp> 28 29 #include <cstdint> 30 #include <iostream> 31 #include <memory> 32 #include <string> 33 #include <unordered_map> 34 #include <vector> 35 36 namespace pid_control 37 { 38 39 static constexpr bool deferSignals = true; 40 static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone"; 41 42 static std::string getControlPath(int64_t zone) 43 { 44 return std::string(objectPath) + std::to_string(zone); 45 } 46 47 std::unordered_map<int64_t, std::unique_ptr<ZoneInterface>> 48 buildZones(const std::map<int64_t, conf::PIDConf>& zonePids, 49 std::map<int64_t, struct conf::ZoneConfig>& zoneConfigs, 50 SensorManager& mgr, sdbusplus::bus::bus& modeControlBus) 51 { 52 std::unordered_map<int64_t, std::unique_ptr<ZoneInterface>> zones; 53 54 for (const auto& [zoneId, pidConfig] : zonePids) 55 { 56 /* The above shouldn't be necessary but is, and I am having trouble 57 * locating my notes on why. If I recall correctly it was casting it 58 * down to a byte in at least some cases causing weird behaviors. 59 */ 60 auto zoneConf = zoneConfigs.find(zoneId); 61 if (zoneConf == zoneConfigs.end()) 62 { 63 /* The Zone doesn't have a configuration, bail. */ 64 static constexpr auto err = 65 "Bailing during load, missing Zone Configuration"; 66 std::cerr << err << std::endl; 67 throw std::runtime_error(err); 68 } 69 70 auto zone = std::make_unique<DbusPidZone>( 71 zoneId, zoneConf->second.minThermalOutput, 72 zoneConf->second.failsafePercent, mgr, modeControlBus, 73 getControlPath(zoneId).c_str(), deferSignals); 74 75 std::cerr << "Zone Id: " << zone->getZoneID() << "\n"; 76 77 // For each PID create a Controller and a Sensor. 78 for (const auto& [name, info] : pidConfig) 79 { 80 std::vector<std::string> inputs; 81 std::cerr << "PID name: " << name << "\n"; 82 83 /* 84 * TODO(venture): Need to check if input is known to the 85 * SensorManager. 86 */ 87 if (info.type == "fan") 88 { 89 for (const auto& i : info.inputs) 90 { 91 inputs.push_back(i); 92 zone->addFanInput(i); 93 } 94 95 auto pid = FanController::createFanPid(zone.get(), name, inputs, 96 info.pidInfo); 97 zone->addFanPID(std::move(pid)); 98 } 99 else if (isThermalType(info.type)) 100 { 101 for (const auto& i : info.inputs) 102 { 103 inputs.push_back(i); 104 zone->addThermalInput(i); 105 } 106 107 auto pid = ThermalController::createThermalPid( 108 zone.get(), name, inputs, info.setpoint, info.pidInfo, 109 getThermalType(info.type)); 110 111 zone->addThermalPID(std::move(pid)); 112 } 113 else if (info.type == "stepwise") 114 { 115 for (const auto& i : info.inputs) 116 { 117 inputs.push_back(i); 118 zone->addThermalInput(i); 119 } 120 auto stepwise = StepwiseController::createStepwiseController( 121 zone.get(), name, inputs, info.stepwiseInfo); 122 zone->addThermalPID(std::move(stepwise)); 123 } 124 125 std::cerr << "inputs: "; 126 for (const auto& i : inputs) 127 { 128 std::cerr << i << ", "; 129 } 130 std::cerr << "\n"; 131 } 132 133 zone->emit_object_added(); 134 zones[zoneId] = std::move(zone); 135 } 136 137 return zones; 138 } 139 140 } // namespace pid_control 141