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