xref: /openbmc/phosphor-pid-control/main.cpp (revision d4695590d2f2b9f6295097714624b8043d25a701)
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>
35da4a5dd1SPatrick Venture #include <chrono>
36da4a5dd1SPatrick Venture #include <iostream>
37ce6a3f36SJames Feist #include <list>
38da4a5dd1SPatrick Venture #include <map>
39da4a5dd1SPatrick Venture #include <memory>
40ce6a3f36SJames Feist #include <sdbusplus/asio/connection.hpp>
41da4a5dd1SPatrick Venture #include <sdbusplus/bus.hpp>
42da4a5dd1SPatrick Venture #include <thread>
43da4a5dd1SPatrick Venture #include <unordered_map>
44ba8ffa73SPatrick Venture #include <utility>
45da4a5dd1SPatrick Venture #include <vector>
46e620656cSPatrick Venture 
479f044415SPatrick Venture #if CONFIGURE_DBUS
489f044415SPatrick Venture #include "dbus/dbusconfiguration.hpp"
499f044415SPatrick Venture #endif
509f044415SPatrick Venture 
511fe08952SJames Feist /* The configuration converted sensor list. */
52f81f2886SJames Feist std::map<std::string, struct conf::SensorConfig> sensorConfig = {};
531fe08952SJames Feist /* The configuration converted PID list. */
54f81f2886SJames Feist std::map<int64_t, conf::PIDConf> zoneConfig = {};
551fe08952SJames Feist /* The configuration converted Zone configuration. */
56f81f2886SJames Feist std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {};
57e620656cSPatrick Venture 
58c19f5d4dSPatrick Venture /** the swampd daemon will check for the existence of this file. */
59c19f5d4dSPatrick Venture constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json";
60e620656cSPatrick Venture std::string configPath = "";
61e620656cSPatrick Venture 
621fe08952SJames Feist /* async io context for operation */
631fe08952SJames Feist boost::asio::io_context io;
64e620656cSPatrick Venture 
651fe08952SJames Feist /* buses for system control */
661fe08952SJames Feist static sdbusplus::asio::connection modeControlBus(io);
671fe08952SJames Feist static sdbusplus::asio::connection
681fe08952SJames Feist     hostBus(io, sdbusplus::bus::new_system().release());
691fe08952SJames Feist static sdbusplus::asio::connection
701fe08952SJames Feist     passiveBus(io, sdbusplus::bus::new_system().release());
71de79ee05SPatrick Venture 
721fe08952SJames Feist void restartControlLoops()
731fe08952SJames Feist {
741fe08952SJames Feist     static SensorManager mgmr;
751fe08952SJames Feist     static std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones;
761fe08952SJames Feist     static std::list<boost::asio::steady_timer> timers;
77e620656cSPatrick Venture 
781fe08952SJames Feist     timers.clear();
794cb7c058SPatrick Venture 
809f044415SPatrick Venture #if CONFIGURE_DBUS
811fe08952SJames Feist 
821fe08952SJames Feist     static boost::asio::steady_timer reloadTimer(io);
831fe08952SJames Feist     if (!dbus_configuration::init(modeControlBus, reloadTimer))
847136a5aeSJames Feist     {
851fe08952SJames Feist         return; // configuration not ready
867136a5aeSJames Feist     }
871fe08952SJames Feist 
8818b1311eSPatrick Venture #else
8918b1311eSPatrick Venture     const std::string& path =
9018b1311eSPatrick Venture         (configPath.length() > 0) ? configPath : jsonConfigurationPath;
91e620656cSPatrick Venture 
92e620656cSPatrick Venture     /*
93e620656cSPatrick Venture      * When building the sensors, if any of the dbus passive ones aren't on the
94e620656cSPatrick Venture      * bus, it'll fail immediately.
95e620656cSPatrick Venture      */
96e620656cSPatrick Venture     try
97e620656cSPatrick Venture     {
9818b1311eSPatrick Venture         auto jsonData = parseValidateJson(path);
994cb7c058SPatrick Venture         sensorConfig = buildSensorsFromJson(jsonData);
10018b1311eSPatrick Venture         std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData);
101e620656cSPatrick Venture     }
102e620656cSPatrick Venture     catch (const std::exception& e)
103e620656cSPatrick Venture     {
104e620656cSPatrick Venture         std::cerr << "Failed during building: " << e.what() << "\n";
105e620656cSPatrick Venture         exit(EXIT_FAILURE); /* fatal error. */
106e620656cSPatrick Venture     }
10718b1311eSPatrick Venture #endif
1084cb7c058SPatrick Venture 
1091fe08952SJames Feist     mgmr = buildSensors(sensorConfig, passiveBus, hostBus);
1101fe08952SJames Feist     zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
111e620656cSPatrick Venture 
112e620656cSPatrick Venture     if (0 == zones.size())
113e620656cSPatrick Venture     {
114e620656cSPatrick Venture         std::cerr << "No zones defined, exiting.\n";
1151fe08952SJames Feist         std::exit(EXIT_FAILURE);
116e620656cSPatrick Venture     }
117e620656cSPatrick Venture 
1181fe08952SJames Feist     for (const auto& i : zones)
1191fe08952SJames Feist     {
1201fe08952SJames Feist         auto& timer = timers.emplace_back(io);
1211fe08952SJames Feist         std::cerr << "pushing zone " << i.first << "\n";
1221fe08952SJames Feist         pidControlLoop(i.second.get(), timer);
1231fe08952SJames Feist     }
1241fe08952SJames Feist }
1251fe08952SJames Feist 
1261fe08952SJames Feist int main(int argc, char* argv[])
1271fe08952SJames Feist {
1281fe08952SJames Feist     loggingPath = "";
1291fe08952SJames Feist     loggingEnabled = false;
1301fe08952SJames Feist     tuningEnabled = false;
1311fe08952SJames Feist 
1321fe08952SJames Feist     CLI::App app{"OpenBMC Fan Control Daemon"};
1331fe08952SJames Feist 
1341fe08952SJames Feist     app.add_option("-c,--conf", configPath,
1351fe08952SJames Feist                    "Optional parameter to specify configuration at run-time")
1361fe08952SJames Feist         ->check(CLI::ExistingFile);
1371fe08952SJames Feist     app.add_option("-l,--log", loggingPath,
1381fe08952SJames Feist                    "Optional parameter to specify logging folder")
1391fe08952SJames Feist         ->check(CLI::ExistingDirectory);
1401fe08952SJames Feist     app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
1411fe08952SJames Feist 
1421fe08952SJames Feist     CLI11_PARSE(app, argc, argv);
1431fe08952SJames Feist 
144*d4695590SKun Yi     loggingEnabled = (!loggingPath.empty());
145*d4695590SKun Yi 
1461fe08952SJames Feist     static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
1471fe08952SJames Feist     // Create a manager for the ModeBus because we own it.
1481fe08952SJames Feist     sdbusplus::server::manager::manager(
1491fe08952SJames Feist         static_cast<sdbusplus::bus::bus&>(modeControlBus), modeRoot);
1501fe08952SJames Feist     hostBus.request_name("xyz.openbmc_project.Hwmon.external");
1511fe08952SJames Feist     modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl");
1521fe08952SJames Feist 
153e620656cSPatrick Venture     /*
154e620656cSPatrick Venture      * All sensors are managed by one manager, but each zone has a pointer to
155e620656cSPatrick Venture      * it.
156e620656cSPatrick Venture      */
157e620656cSPatrick Venture 
1581fe08952SJames Feist     restartControlLoops();
159e620656cSPatrick Venture 
160ce6a3f36SJames Feist     io.run();
1611fe08952SJames Feist     return 0;
162e620656cSPatrick Venture }
163