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