xref: /openbmc/phosphor-pid-control/main.cpp (revision de79ee05abd7e1b29b3074b0150b5f4eabf7cf9e)
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 
51e620656cSPatrick Venture /* The YAML converted sensor list. */
52f81f2886SJames Feist std::map<std::string, struct conf::SensorConfig> sensorConfig = {};
53e620656cSPatrick Venture /* The YAML converted PID list. */
54f81f2886SJames Feist std::map<int64_t, conf::PIDConf> zoneConfig = {};
55e620656cSPatrick Venture /* The YAML 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";
60c19f5d4dSPatrick Venture 
61e620656cSPatrick Venture int main(int argc, char* argv[])
62e620656cSPatrick Venture {
63e620656cSPatrick Venture     int rc = 0;
64e620656cSPatrick Venture     std::string configPath = "";
65*de79ee05SPatrick Venture     loggingPath = "";
66*de79ee05SPatrick Venture     loggingEnabled = false;
67*de79ee05SPatrick Venture     tuningEnabled = false;
68e620656cSPatrick Venture 
69b5cc37ceSPatrick Venture     CLI::App app{"OpenBMC Fan Control Daemon"};
70e620656cSPatrick Venture 
71b5cc37ceSPatrick Venture     app.add_option("-c,--conf", configPath,
72b5cc37ceSPatrick Venture                    "Optional parameter to specify configuration at run-time")
73b5cc37ceSPatrick Venture         ->check(CLI::ExistingFile);
74*de79ee05SPatrick Venture     app.add_option("-l,--log", loggingPath,
75*de79ee05SPatrick Venture                    "Optional parameter to specify logging path");
76*de79ee05SPatrick Venture     app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
77*de79ee05SPatrick Venture 
78*de79ee05SPatrick Venture     loggingEnabled = (!loggingPath.empty());
79e620656cSPatrick Venture 
80b5cc37ceSPatrick Venture     CLI11_PARSE(app, argc, argv);
81e620656cSPatrick Venture 
829fa90c19SJames Feist     auto modeControlBus = sdbusplus::bus::new_system();
834cb7c058SPatrick Venture     static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
844cb7c058SPatrick Venture     // Create a manager for the ModeBus because we own it.
854cb7c058SPatrick Venture     sdbusplus::server::manager::manager(modeControlBus, modeRoot);
864cb7c058SPatrick Venture 
879f044415SPatrick Venture #if CONFIGURE_DBUS
887136a5aeSJames Feist     {
89c179d406SPatrick Venture         dbus_configuration::init(modeControlBus);
907136a5aeSJames Feist     }
9118b1311eSPatrick Venture #else
9218b1311eSPatrick Venture     const std::string& path =
9318b1311eSPatrick Venture         (configPath.length() > 0) ? configPath : jsonConfigurationPath;
94e620656cSPatrick Venture 
95e620656cSPatrick Venture     /*
96e620656cSPatrick Venture      * When building the sensors, if any of the dbus passive ones aren't on the
97e620656cSPatrick Venture      * bus, it'll fail immediately.
98e620656cSPatrick Venture      */
99e620656cSPatrick Venture     try
100e620656cSPatrick Venture     {
10118b1311eSPatrick Venture         auto jsonData = parseValidateJson(path);
1024cb7c058SPatrick Venture         sensorConfig = buildSensorsFromJson(jsonData);
10318b1311eSPatrick Venture         std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData);
104e620656cSPatrick Venture     }
105e620656cSPatrick Venture     catch (const std::exception& e)
106e620656cSPatrick Venture     {
107e620656cSPatrick Venture         std::cerr << "Failed during building: " << e.what() << "\n";
108e620656cSPatrick Venture         exit(EXIT_FAILURE); /* fatal error. */
109e620656cSPatrick Venture     }
11018b1311eSPatrick Venture #endif
1114cb7c058SPatrick Venture 
1124cb7c058SPatrick Venture     SensorManager mgmr = buildSensors(sensorConfig);
1134cb7c058SPatrick Venture     std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones =
1144cb7c058SPatrick Venture         buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
115e620656cSPatrick Venture 
116e620656cSPatrick Venture     if (0 == zones.size())
117e620656cSPatrick Venture     {
118e620656cSPatrick Venture         std::cerr << "No zones defined, exiting.\n";
119e620656cSPatrick Venture         return rc;
120e620656cSPatrick Venture     }
121e620656cSPatrick Venture 
122e620656cSPatrick Venture     /*
123e620656cSPatrick Venture      * All sensors are managed by one manager, but each zone has a pointer to
124e620656cSPatrick Venture      * it.
125e620656cSPatrick Venture      */
126e620656cSPatrick Venture 
127c179d406SPatrick Venture     auto& hostSensorBus = mgmr.getHostBus();
128c179d406SPatrick Venture     auto& passiveListeningBus = mgmr.getPassiveBus();
129e620656cSPatrick Venture 
130ce6a3f36SJames Feist     boost::asio::io_context io;
131ce6a3f36SJames Feist     sdbusplus::asio::connection passiveBus(io, passiveListeningBus.release());
132e620656cSPatrick Venture 
133ce6a3f36SJames Feist     sdbusplus::asio::connection hostBus(io, hostSensorBus.release());
134ce6a3f36SJames Feist     hostBus.request_name("xyz.openbmc_project.Hwmon.external");
135e620656cSPatrick Venture 
136ce6a3f36SJames Feist     sdbusplus::asio::connection modeBus(io, modeControlBus.release());
137ce6a3f36SJames Feist     modeBus.request_name("xyz.openbmc_project.State.FanCtrl");
138e620656cSPatrick Venture 
139ce6a3f36SJames Feist     std::list<boost::asio::steady_timer> timers;
140e620656cSPatrick Venture 
1414a2dc4d8SPatrick Venture     for (const auto& i : zones)
142e620656cSPatrick Venture     {
143ce6a3f36SJames Feist         auto& timer = timers.emplace_back(io);
144e620656cSPatrick Venture         std::cerr << "pushing zone" << std::endl;
145ce6a3f36SJames Feist         pidControlLoop(i.second.get(), timer);
146e620656cSPatrick Venture     }
147e620656cSPatrick Venture 
148ce6a3f36SJames Feist     io.run();
149e620656cSPatrick Venture     return rc;
150e620656cSPatrick Venture }
151