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 static std::string getPidControlPath(int64_t zone, std::string pidname) 48 { 49 return std::string(objectPath) + std::to_string(zone) + "/" + pidname; 50 } 51 52 std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> 53 buildZones(const std::map<int64_t, conf::PIDConf>& zonePids, 54 std::map<int64_t, conf::ZoneConfig>& zoneConfigs, 55 SensorManager& mgr, sdbusplus::bus_t& modeControlBus) 56 { 57 std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones; 58 59 for (const auto& [zoneId, pidConfig] : zonePids) 60 { 61 /* The above shouldn't be necessary but is, and I am having trouble 62 * locating my notes on why. If I recall correctly it was casting it 63 * down to a byte in at least some cases causing weird behaviors. 64 */ 65 auto zoneConf = zoneConfigs.find(zoneId); 66 if (zoneConf == zoneConfigs.end()) 67 { 68 /* The Zone doesn't have a configuration, bail. */ 69 static constexpr auto err = 70 "Bailing during load, missing Zone Configuration"; 71 std::cerr << err << std::endl; 72 throw std::runtime_error(err); 73 } 74 75 auto zone = std::make_shared<DbusPidZone>( 76 zoneId, zoneConf->second.minThermalOutput, 77 zoneConf->second.failsafePercent, zoneConf->second.cycleTime, mgr, 78 modeControlBus, getControlPath(zoneId).c_str(), deferSignals); 79 80 std::cerr << "Zone Id: " << zone->getZoneID() << "\n"; 81 82 // For each PID create a Controller and a Sensor. 83 for (const auto& [name, info] : pidConfig) 84 { 85 std::vector<std::string> inputs; 86 std::cerr << "PID name: " << name << "\n"; 87 88 /* 89 * TODO(venture): Need to check if input is known to the 90 * SensorManager. 91 */ 92 if (info.type == "fan") 93 { 94 for (const auto& i : info.inputs) 95 { 96 inputs.push_back(i); 97 zone->addFanInput(i); 98 } 99 100 auto pid = FanController::createFanPid(zone.get(), name, inputs, 101 info.pidInfo); 102 zone->addFanPID(std::move(pid)); 103 zone->addPidFailSafePercent(name, info.failSafePercent); 104 } 105 else if (isThermalType(info.type)) 106 { 107 for (const auto& i : info.inputs) 108 { 109 inputs.push_back(i); 110 zone->addThermalInput(i); 111 } 112 113 auto pid = ThermalController::createThermalPid( 114 zone.get(), name, inputs, info.setpoint, info.pidInfo, 115 getThermalType(info.type)); 116 117 zone->addThermalPID(std::move(pid)); 118 zone->addPidControlProcess(name, modeControlBus, 119 getPidControlPath(zoneId, name), 120 deferSignals); 121 zone->addPidFailSafePercent(name, info.failSafePercent); 122 } 123 else if (info.type == "stepwise") 124 { 125 for (const auto& i : info.inputs) 126 { 127 inputs.push_back(i); 128 zone->addThermalInput(i); 129 } 130 auto stepwise = StepwiseController::createStepwiseController( 131 zone.get(), name, inputs, info.stepwiseInfo); 132 zone->addThermalPID(std::move(stepwise)); 133 zone->addPidControlProcess(name, modeControlBus, 134 getPidControlPath(zoneId, name), 135 deferSignals); 136 zone->addPidFailSafePercent(name, info.failSafePercent); 137 } 138 139 std::cerr << "inputs: "; 140 for (const auto& i : inputs) 141 { 142 std::cerr << i << ", "; 143 } 144 std::cerr << "\n"; 145 } 146 147 zone->emit_object_added(); 148 zones[zoneId] = std::move(zone); 149 } 150 151 return zones; 152 } 153 154 } // namespace pid_control 155