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 
81         std::cerr << "Zone Id: " << zone->getZoneID() << "\n";
82 
83         // For each PID create a Controller and a Sensor.
84         for (const auto& [name, info] : pidConfig)
85         {
86             std::vector<pid_control::conf::SensorInput> inputs;
87             std::cerr << "PID name: " << name << "\n";
88 
89             /*
90              * TODO(venture): Need to check if input is known to the
91              * SensorManager.
92              */
93             if (info.type == "fan")
94             {
95                 for (const auto& i : info.inputs)
96                 {
97                     inputs.push_back(i);
98                     zone->addFanInput(i.name, i.missingIsAcceptable);
99                 }
100 
101                 auto pid = FanController::createFanPid(
102                     zone.get(), name, splitNames(inputs), info.pidInfo);
103                 zone->addFanPID(std::move(pid));
104                 zone->addPidFailSafePercent(name, info.failSafePercent);
105             }
106             else if (isThermalType(info.type))
107             {
108                 for (const auto& i : info.inputs)
109                 {
110                     inputs.push_back(i);
111                     zone->addThermalInput(i.name, i.missingIsAcceptable);
112                 }
113 
114                 auto pid = ThermalController::createThermalPid(
115                     zone.get(), name, inputs, info.setpoint, info.pidInfo,
116                     getThermalType(info.type));
117 
118                 zone->addThermalPID(std::move(pid));
119                 zone->addPidControlProcess(
120                     name, info.type, info.setpoint, modeControlBus,
121                     getPidControlPath(zoneId, name), deferSignals);
122                 zone->addPidFailSafePercent(name, info.failSafePercent);
123             }
124             else if (info.type == "stepwise")
125             {
126                 for (const auto& i : info.inputs)
127                 {
128                     inputs.push_back(i);
129                     zone->addThermalInput(i.name, i.missingIsAcceptable);
130                 }
131                 auto stepwise = StepwiseController::createStepwiseController(
132                     zone.get(), name, splitNames(inputs), info.stepwiseInfo);
133                 zone->addThermalPID(std::move(stepwise));
134                 zone->addPidControlProcess(
135                     name, info.type, info.setpoint, modeControlBus,
136                     getPidControlPath(zoneId, name), deferSignals);
137                 zone->addPidFailSafePercent(name, info.failSafePercent);
138             }
139 
140             std::cerr << "inputs: ";
141             for (const auto& i : inputs)
142             {
143                 std::cerr << i.name;
144                 if (i.convertTempToMargin)
145                 {
146                     std::cerr << "[" << i.convertMarginZero << "]";
147                 }
148                 if (i.missingIsAcceptable)
149                 {
150                     std::cerr << "?";
151                 }
152                 std::cerr << ", ";
153             }
154             std::cerr << "\n";
155         }
156 
157         zone->emit_object_added();
158         zones[zoneId] = std::move(zone);
159     }
160 
161     return zones;
162 }
163 
164 } // namespace pid_control
165