xref: /openbmc/phosphor-pid-control/main.cpp (revision a076487aab0ee71ea32d53f798d5ca6b31677278)
1e620656cSPatrick Venture /**
2e620656cSPatrick Venture  * Copyright 2017 Google Inc.
3e620656cSPatrick Venture  *
4e620656cSPatrick Venture  * Licensed under the Apache License, Version 2.0 (the "License");
5e620656cSPatrick Venture  * you may not use this file except in compliance with the License.
6e620656cSPatrick Venture  * You may obtain a copy of the License at
7e620656cSPatrick Venture  *
8e620656cSPatrick Venture  *     http://www.apache.org/licenses/LICENSE-2.0
9e620656cSPatrick Venture  *
10e620656cSPatrick Venture  * Unless required by applicable law or agreed to in writing, software
11e620656cSPatrick Venture  * distributed under the License is distributed on an "AS IS" BASIS,
12e620656cSPatrick Venture  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e620656cSPatrick Venture  * See the License for the specific language governing permissions and
14e620656cSPatrick Venture  * limitations under the License.
15e620656cSPatrick Venture  */
16e620656cSPatrick Venture 
177136a5aeSJames Feist #include "config.h"
18e620656cSPatrick Venture 
19ba8ffa73SPatrick Venture #include "build/buildjson.hpp"
20da4a5dd1SPatrick Venture #include "conf.hpp"
21e620656cSPatrick Venture #include "interfaces.hpp"
225c7cc545SPatrick Venture #include "pid/builder.hpp"
23ba8ffa73SPatrick Venture #include "pid/buildjson.hpp"
24ce6a3f36SJames Feist #include "pid/pidloop.hpp"
25c32e3fc5SPatrick Venture #include "pid/tuning.hpp"
26e620656cSPatrick Venture #include "pid/zone.hpp"
275e929093SPatrick Venture #include "sensors/builder.hpp"
28ba8ffa73SPatrick Venture #include "sensors/buildjson.hpp"
29e620656cSPatrick Venture #include "sensors/manager.hpp"
30da4a5dd1SPatrick Venture #include "util.hpp"
31e620656cSPatrick Venture 
32b5cc37ceSPatrick Venture #include <CLI/CLI.hpp>
33ce6a3f36SJames Feist #include <boost/asio/io_context.hpp>
34ce6a3f36SJames Feist #include <boost/asio/steady_timer.hpp>
35a83a3eccSPatrick Venture #include <sdbusplus/asio/connection.hpp>
36a83a3eccSPatrick Venture #include <sdbusplus/bus.hpp>
37a83a3eccSPatrick Venture 
38da4a5dd1SPatrick Venture #include <chrono>
39da4a5dd1SPatrick Venture #include <iostream>
40ce6a3f36SJames Feist #include <list>
41da4a5dd1SPatrick Venture #include <map>
42da4a5dd1SPatrick Venture #include <memory>
43da4a5dd1SPatrick Venture #include <thread>
44da4a5dd1SPatrick Venture #include <unordered_map>
45ba8ffa73SPatrick Venture #include <utility>
46da4a5dd1SPatrick Venture #include <vector>
47e620656cSPatrick Venture 
489f044415SPatrick Venture #if CONFIGURE_DBUS
499f044415SPatrick Venture #include "dbus/dbusconfiguration.hpp"
509f044415SPatrick Venture #endif
519f044415SPatrick Venture 
52*a076487aSPatrick Venture namespace pid_control
53*a076487aSPatrick Venture {
54*a076487aSPatrick Venture 
551fe08952SJames Feist /* The configuration converted sensor list. */
56f81f2886SJames Feist std::map<std::string, struct conf::SensorConfig> sensorConfig = {};
571fe08952SJames Feist /* The configuration converted PID list. */
58f81f2886SJames Feist std::map<int64_t, conf::PIDConf> zoneConfig = {};
591fe08952SJames Feist /* The configuration converted Zone configuration. */
60f81f2886SJames Feist std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {};
61e620656cSPatrick Venture 
62*a076487aSPatrick Venture } // namespace pid_control
63*a076487aSPatrick Venture 
64c19f5d4dSPatrick Venture /** the swampd daemon will check for the existence of this file. */
65c19f5d4dSPatrick Venture constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json";
66e620656cSPatrick Venture std::string configPath = "";
67e620656cSPatrick Venture 
681fe08952SJames Feist /* async io context for operation */
691fe08952SJames Feist boost::asio::io_context io;
70e620656cSPatrick Venture 
711fe08952SJames Feist /* buses for system control */
721fe08952SJames Feist static sdbusplus::asio::connection modeControlBus(io);
731fe08952SJames Feist static sdbusplus::asio::connection
741fe08952SJames Feist     hostBus(io, sdbusplus::bus::new_system().release());
751fe08952SJames Feist static sdbusplus::asio::connection
761fe08952SJames Feist     passiveBus(io, sdbusplus::bus::new_system().release());
77de79ee05SPatrick Venture 
78*a076487aSPatrick Venture namespace pid_control
79*a076487aSPatrick Venture {
80*a076487aSPatrick Venture 
811fe08952SJames Feist void restartControlLoops()
821fe08952SJames Feist {
831fe08952SJames Feist     static SensorManager mgmr;
841fe08952SJames Feist     static std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones;
851fe08952SJames Feist     static std::list<boost::asio::steady_timer> timers;
86e620656cSPatrick Venture 
871fe08952SJames Feist     timers.clear();
884cb7c058SPatrick Venture 
899f044415SPatrick Venture #if CONFIGURE_DBUS
901fe08952SJames Feist 
911fe08952SJames Feist     static boost::asio::steady_timer reloadTimer(io);
921fe08952SJames Feist     if (!dbus_configuration::init(modeControlBus, reloadTimer))
937136a5aeSJames Feist     {
941fe08952SJames Feist         return; // configuration not ready
957136a5aeSJames Feist     }
961fe08952SJames Feist 
9718b1311eSPatrick Venture #else
9818b1311eSPatrick Venture     const std::string& path =
9918b1311eSPatrick Venture         (configPath.length() > 0) ? configPath : jsonConfigurationPath;
100e620656cSPatrick Venture 
101e620656cSPatrick Venture     /*
102e620656cSPatrick Venture      * When building the sensors, if any of the dbus passive ones aren't on the
103e620656cSPatrick Venture      * bus, it'll fail immediately.
104e620656cSPatrick Venture      */
105e620656cSPatrick Venture     try
106e620656cSPatrick Venture     {
10718b1311eSPatrick Venture         auto jsonData = parseValidateJson(path);
1084cb7c058SPatrick Venture         sensorConfig = buildSensorsFromJson(jsonData);
10918b1311eSPatrick Venture         std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData);
110e620656cSPatrick Venture     }
111e620656cSPatrick Venture     catch (const std::exception& e)
112e620656cSPatrick Venture     {
113e620656cSPatrick Venture         std::cerr << "Failed during building: " << e.what() << "\n";
114e620656cSPatrick Venture         exit(EXIT_FAILURE); /* fatal error. */
115e620656cSPatrick Venture     }
11618b1311eSPatrick Venture #endif
1174cb7c058SPatrick Venture 
1181fe08952SJames Feist     mgmr = buildSensors(sensorConfig, passiveBus, hostBus);
1191fe08952SJames Feist     zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
120e620656cSPatrick Venture 
121e620656cSPatrick Venture     if (0 == zones.size())
122e620656cSPatrick Venture     {
123e620656cSPatrick Venture         std::cerr << "No zones defined, exiting.\n";
1241fe08952SJames Feist         std::exit(EXIT_FAILURE);
125e620656cSPatrick Venture     }
126e620656cSPatrick Venture 
1271fe08952SJames Feist     for (const auto& i : zones)
1281fe08952SJames Feist     {
1291fe08952SJames Feist         auto& timer = timers.emplace_back(io);
1301fe08952SJames Feist         std::cerr << "pushing zone " << i.first << "\n";
1311fe08952SJames Feist         pidControlLoop(i.second.get(), timer);
1321fe08952SJames Feist     }
1331fe08952SJames Feist }
1341fe08952SJames Feist 
135298a95cbSYong Li void tryRestartControlLoops()
136298a95cbSYong Li {
137298a95cbSYong Li     int count = 0;
138298a95cbSYong Li     for (count = 0; count <= 5; count++)
139298a95cbSYong Li     {
140298a95cbSYong Li         try
141298a95cbSYong Li         {
142298a95cbSYong Li             restartControlLoops();
143298a95cbSYong Li             break;
144298a95cbSYong Li         }
145298a95cbSYong Li         catch (const std::exception& e)
146298a95cbSYong Li         {
147298a95cbSYong Li             std::cerr << count
148298a95cbSYong Li                       << " Failed during restartControlLoops, try again: "
149298a95cbSYong Li                       << e.what() << "\n";
150298a95cbSYong Li             if (count >= 5)
151298a95cbSYong Li             {
152298a95cbSYong Li                 throw std::runtime_error(e.what());
153298a95cbSYong Li             }
154298a95cbSYong Li         }
155298a95cbSYong Li         std::this_thread::sleep_for(std::chrono::seconds(10));
156298a95cbSYong Li     }
157298a95cbSYong Li 
158298a95cbSYong Li     return;
159298a95cbSYong Li }
160298a95cbSYong Li 
161*a076487aSPatrick Venture } // namespace pid_control
162*a076487aSPatrick Venture 
1631fe08952SJames Feist int main(int argc, char* argv[])
1641fe08952SJames Feist {
1651fe08952SJames Feist     loggingPath = "";
1661fe08952SJames Feist     loggingEnabled = false;
1671fe08952SJames Feist     tuningEnabled = false;
1681fe08952SJames Feist 
1691fe08952SJames Feist     CLI::App app{"OpenBMC Fan Control Daemon"};
1701fe08952SJames Feist 
1711fe08952SJames Feist     app.add_option("-c,--conf", configPath,
1721fe08952SJames Feist                    "Optional parameter to specify configuration at run-time")
1731fe08952SJames Feist         ->check(CLI::ExistingFile);
1741fe08952SJames Feist     app.add_option("-l,--log", loggingPath,
1751fe08952SJames Feist                    "Optional parameter to specify logging folder")
1761fe08952SJames Feist         ->check(CLI::ExistingDirectory);
1771fe08952SJames Feist     app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
1781fe08952SJames Feist 
1791fe08952SJames Feist     CLI11_PARSE(app, argc, argv);
1801fe08952SJames Feist 
181d4695590SKun Yi     loggingEnabled = (!loggingPath.empty());
182d4695590SKun Yi 
1831fe08952SJames Feist     static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
1841fe08952SJames Feist     // Create a manager for the ModeBus because we own it.
1851fe08952SJames Feist     sdbusplus::server::manager::manager(
1861fe08952SJames Feist         static_cast<sdbusplus::bus::bus&>(modeControlBus), modeRoot);
1871fe08952SJames Feist     hostBus.request_name("xyz.openbmc_project.Hwmon.external");
1881fe08952SJames Feist     modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl");
1891fe08952SJames Feist 
190e620656cSPatrick Venture     /*
191e620656cSPatrick Venture      * All sensors are managed by one manager, but each zone has a pointer to
192e620656cSPatrick Venture      * it.
193e620656cSPatrick Venture      */
194e620656cSPatrick Venture 
195*a076487aSPatrick Venture     pid_control::tryRestartControlLoops();
196e620656cSPatrick Venture 
197ce6a3f36SJames Feist     io.run();
1981fe08952SJames Feist     return 0;
199e620656cSPatrick Venture }
200