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