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