blob: 5a94efb44e607c1860c7516a9d0245118dc0181b [file] [log] [blame]
Patrick Venturee6206562018-03-08 15:36:53 -08001/**
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
James Feist7136a5a2018-07-19 09:52:05 -070017#include "config.h"
Patrick Venturee6206562018-03-08 15:36:53 -080018
Patrick Ventureba8ffa72019-02-11 12:03:56 -080019#include "build/buildjson.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070020#include "conf.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080021#include "interfaces.hpp"
Patrick Venture5c7cc542018-06-11 14:29:38 -070022#include "pid/builder.hpp"
Patrick Ventureba8ffa72019-02-11 12:03:56 -080023#include "pid/buildjson.hpp"
James Feistce6a3f32019-03-12 11:20:16 -070024#include "pid/pidloop.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -080025#include "pid/tuning.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080026#include "pid/zone.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070027#include "sensors/builder.hpp"
Patrick Ventureba8ffa72019-02-11 12:03:56 -080028#include "sensors/buildjson.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080029#include "sensors/manager.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070030#include "util.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080031
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070032#include <CLI/CLI.hpp>
James Feistce6a3f32019-03-12 11:20:16 -070033#include <boost/asio/io_context.hpp>
34#include <boost/asio/steady_timer.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070035#include <sdbusplus/asio/connection.hpp>
36#include <sdbusplus/bus.hpp>
37
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070038#include <chrono>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070039#include <iostream>
James Feistce6a3f32019-03-12 11:20:16 -070040#include <list>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070041#include <map>
42#include <memory>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070043#include <thread>
44#include <unordered_map>
Patrick Ventureba8ffa72019-02-11 12:03:56 -080045#include <utility>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070046#include <vector>
Patrick Venturee6206562018-03-08 15:36:53 -080047
Patrick Venture9f044412018-09-20 19:49:55 -070048#if CONFIGURE_DBUS
49#include "dbus/dbusconfiguration.hpp"
50#endif
51
Patrick Venturea0764872020-08-08 07:48:43 -070052namespace pid_control
53{
54
James Feist1fe08952019-05-07 09:17:16 -070055/* The configuration converted sensor list. */
James Feistf81f2882019-02-26 11:26:36 -080056std::map<std::string, struct conf::SensorConfig> sensorConfig = {};
James Feist1fe08952019-05-07 09:17:16 -070057/* The configuration converted PID list. */
James Feistf81f2882019-02-26 11:26:36 -080058std::map<int64_t, conf::PIDConf> zoneConfig = {};
James Feist1fe08952019-05-07 09:17:16 -070059/* The configuration converted Zone configuration. */
James Feistf81f2882019-02-26 11:26:36 -080060std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080061
Patrick Venturea0764872020-08-08 07:48:43 -070062} // namespace pid_control
63
Patrick Venturec19f5d42019-02-14 10:59:47 -080064/** the swampd daemon will check for the existence of this file. */
65constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json";
James Feist1fe08952019-05-07 09:17:16 -070066std::string configPath = "";
Patrick Venturec19f5d42019-02-14 10:59:47 -080067
James Feist1fe08952019-05-07 09:17:16 -070068/* async io context for operation */
69boost::asio::io_context io;
70
71/* buses for system control */
72static sdbusplus::asio::connection modeControlBus(io);
73static sdbusplus::asio::connection
74 hostBus(io, sdbusplus::bus::new_system().release());
75static sdbusplus::asio::connection
76 passiveBus(io, sdbusplus::bus::new_system().release());
77
Patrick Venturea0764872020-08-08 07:48:43 -070078namespace pid_control
79{
80
James Feist1fe08952019-05-07 09:17:16 -070081void restartControlLoops()
Patrick Venturee6206562018-03-08 15:36:53 -080082{
James Feist1fe08952019-05-07 09:17:16 -070083 static SensorManager mgmr;
84 static std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones;
85 static std::list<boost::asio::steady_timer> timers;
Patrick Venturee6206562018-03-08 15:36:53 -080086
James Feist1fe08952019-05-07 09:17:16 -070087 timers.clear();
Patrick Venture4cb7c052019-02-14 11:11:33 -080088
Patrick Venture9f044412018-09-20 19:49:55 -070089#if CONFIGURE_DBUS
James Feist1fe08952019-05-07 09:17:16 -070090
91 static boost::asio::steady_timer reloadTimer(io);
92 if (!dbus_configuration::init(modeControlBus, reloadTimer))
James Feist7136a5a2018-07-19 09:52:05 -070093 {
James Feist1fe08952019-05-07 09:17:16 -070094 return; // configuration not ready
James Feist7136a5a2018-07-19 09:52:05 -070095 }
James Feist1fe08952019-05-07 09:17:16 -070096
Patrick Venture18b13112019-02-14 11:43:59 -080097#else
98 const std::string& path =
99 (configPath.length() > 0) ? configPath : jsonConfigurationPath;
Patrick Venturee6206562018-03-08 15:36:53 -0800100
101 /*
102 * When building the sensors, if any of the dbus passive ones aren't on the
103 * bus, it'll fail immediately.
104 */
Patrick Venture18b13112019-02-14 11:43:59 -0800105 try
Patrick Venturee6206562018-03-08 15:36:53 -0800106 {
Patrick Venture18b13112019-02-14 11:43:59 -0800107 auto jsonData = parseValidateJson(path);
108 sensorConfig = buildSensorsFromJson(jsonData);
109 std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData);
Patrick Venturee6206562018-03-08 15:36:53 -0800110 }
Patrick Venture18b13112019-02-14 11:43:59 -0800111 catch (const std::exception& e)
112 {
113 std::cerr << "Failed during building: " << e.what() << "\n";
114 exit(EXIT_FAILURE); /* fatal error. */
115 }
116#endif
Patrick Venture4cb7c052019-02-14 11:11:33 -0800117
James Feist1fe08952019-05-07 09:17:16 -0700118 mgmr = buildSensors(sensorConfig, passiveBus, hostBus);
119 zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
Patrick Venturee6206562018-03-08 15:36:53 -0800120
121 if (0 == zones.size())
122 {
123 std::cerr << "No zones defined, exiting.\n";
James Feist1fe08952019-05-07 09:17:16 -0700124 std::exit(EXIT_FAILURE);
Patrick Venturee6206562018-03-08 15:36:53 -0800125 }
126
James Feist1fe08952019-05-07 09:17:16 -0700127 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
Yong Li298a95c2020-04-07 15:11:02 +0800135void 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
Patrick Venturea0764872020-08-08 07:48:43 -0700161} // namespace pid_control
162
James Feist1fe08952019-05-07 09:17:16 -0700163int 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
James Feist1fe08952019-05-07 09:17:16 -0700179 CLI11_PARSE(app, argc, argv);
180
Kun Yid4695592019-05-17 10:42:18 -0700181 loggingEnabled = (!loggingPath.empty());
182
James Feist1fe08952019-05-07 09:17:16 -0700183 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
Patrick Venturee6206562018-03-08 15:36:53 -0800190 /*
191 * All sensors are managed by one manager, but each zone has a pointer to
192 * it.
193 */
194
Patrick Venturea0764872020-08-08 07:48:43 -0700195 pid_control::tryRestartControlLoops();
Patrick Venturee6206562018-03-08 15:36:53 -0800196
James Feistce6a3f32019-03-12 11:20:16 -0700197 io.run();
James Feist1fe08952019-05-07 09:17:16 -0700198 return 0;
Patrick Venturee6206562018-03-08 15:36:53 -0800199}