xref: /openbmc/phosphor-pid-control/main.cpp (revision 9bf5cef4)
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 "config.h"
18 
19 #include "build/buildjson.hpp"
20 #include "conf.hpp"
21 #include "interfaces.hpp"
22 #include "pid/builder.hpp"
23 #include "pid/buildjson.hpp"
24 #include "pid/pidloop.hpp"
25 #include "pid/tuning.hpp"
26 #include "pid/zone.hpp"
27 #include "sensors/builder.hpp"
28 #include "sensors/buildjson.hpp"
29 #include "sensors/manager.hpp"
30 #include "util.hpp"
31 
32 #include <CLI/CLI.hpp>
33 #include <boost/asio/io_context.hpp>
34 #include <boost/asio/steady_timer.hpp>
35 #include <sdbusplus/asio/connection.hpp>
36 #include <sdbusplus/bus.hpp>
37 
38 #include <chrono>
39 #include <iostream>
40 #include <list>
41 #include <map>
42 #include <memory>
43 #include <thread>
44 #include <unordered_map>
45 #include <utility>
46 #include <vector>
47 
48 #if CONFIGURE_DBUS
49 #include "dbus/dbusconfiguration.hpp"
50 #endif
51 
52 namespace pid_control
53 {
54 
55 /* The configuration converted sensor list. */
56 std::map<std::string, struct conf::SensorConfig> sensorConfig = {};
57 /* The configuration converted PID list. */
58 std::map<int64_t, conf::PIDConf> zoneConfig = {};
59 /* The configuration converted Zone configuration. */
60 std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {};
61 
62 } // namespace pid_control
63 
64 /** the swampd daemon will check for the existence of this file. */
65 constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json";
66 std::string configPath = "";
67 
68 /* async io context for operation */
69 boost::asio::io_context io;
70 
71 /* buses for system control */
72 static sdbusplus::asio::connection modeControlBus(io);
73 static sdbusplus::asio::connection
74     hostBus(io, sdbusplus::bus::new_system().release());
75 static sdbusplus::asio::connection
76     passiveBus(io, sdbusplus::bus::new_system().release());
77 
78 namespace pid_control
79 {
80 
81 void restartControlLoops()
82 {
83     static SensorManager mgmr;
84     static std::unordered_map<int64_t, std::unique_ptr<ZoneInterface>> zones;
85     static std::list<boost::asio::steady_timer> timers;
86 
87     timers.clear();
88 
89 #if CONFIGURE_DBUS
90 
91     static boost::asio::steady_timer reloadTimer(io);
92     if (!dbus_configuration::init(modeControlBus, reloadTimer))
93     {
94         return; // configuration not ready
95     }
96 
97 #else
98     const std::string& path =
99         (configPath.length() > 0) ? configPath : jsonConfigurationPath;
100 
101     /*
102      * When building the sensors, if any of the dbus passive ones aren't on the
103      * bus, it'll fail immediately.
104      */
105     try
106     {
107         auto jsonData = parseValidateJson(path);
108         sensorConfig = buildSensorsFromJson(jsonData);
109         std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData);
110     }
111     catch (const std::exception& e)
112     {
113         std::cerr << "Failed during building: " << e.what() << "\n";
114         exit(EXIT_FAILURE); /* fatal error. */
115     }
116 #endif
117 
118     mgmr = buildSensors(sensorConfig, passiveBus, hostBus);
119     zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
120 
121     if (0 == zones.size())
122     {
123         std::cerr << "No zones defined, exiting.\n";
124         std::exit(EXIT_FAILURE);
125     }
126 
127     for (const auto& i : zones)
128     {
129         auto& timer = timers.emplace_back(io);
130         std::cerr << "pushing zone " << i.first << "\n";
131         pidControlLoop(i.second.get(), timer);
132     }
133 }
134 
135 void tryRestartControlLoops()
136 {
137     int count = 0;
138     for (count = 0; count <= 5; count++)
139     {
140         try
141         {
142             restartControlLoops();
143             break;
144         }
145         catch (const std::exception& e)
146         {
147             std::cerr << count
148                       << " Failed during restartControlLoops, try again: "
149                       << e.what() << "\n";
150             if (count >= 5)
151             {
152                 throw std::runtime_error(e.what());
153             }
154         }
155         std::this_thread::sleep_for(std::chrono::seconds(10));
156     }
157 
158     return;
159 }
160 
161 } // namespace pid_control
162 
163 int main(int argc, char* argv[])
164 {
165     loggingPath = "";
166     loggingEnabled = false;
167     tuningEnabled = false;
168 
169     CLI::App app{"OpenBMC Fan Control Daemon"};
170 
171     app.add_option("-c,--conf", configPath,
172                    "Optional parameter to specify configuration at run-time")
173         ->check(CLI::ExistingFile);
174     app.add_option("-l,--log", loggingPath,
175                    "Optional parameter to specify logging folder")
176         ->check(CLI::ExistingDirectory);
177     app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
178 
179     CLI11_PARSE(app, argc, argv);
180 
181     loggingEnabled = (!loggingPath.empty());
182 
183     static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
184     // Create a manager for the ModeBus because we own it.
185     sdbusplus::server::manager::manager(
186         static_cast<sdbusplus::bus::bus&>(modeControlBus), modeRoot);
187     hostBus.request_name("xyz.openbmc_project.Hwmon.external");
188     modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl");
189 
190     /*
191      * All sensors are managed by one manager, but each zone has a pointer to
192      * it.
193      */
194 
195     pid_control::tryRestartControlLoops();
196 
197     io.run();
198     return 0;
199 }
200