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