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