xref: /openbmc/phosphor-pid-control/main.cpp (revision b5cc37cebec74f0bd2a8d378d5f911fc4fa3439e)
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"
24da4a5dd1SPatrick Venture #include "pid/pidthread.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"
30e620656cSPatrick Venture #include "threads/busthread.hpp"
31da4a5dd1SPatrick Venture #include "util.hpp"
32e620656cSPatrick Venture 
33*b5cc37ceSPatrick Venture #include <CLI/CLI.hpp>
34da4a5dd1SPatrick Venture #include <chrono>
35da4a5dd1SPatrick Venture #include <iostream>
36da4a5dd1SPatrick Venture #include <map>
37da4a5dd1SPatrick Venture #include <memory>
38da4a5dd1SPatrick Venture #include <sdbusplus/bus.hpp>
39da4a5dd1SPatrick Venture #include <thread>
40da4a5dd1SPatrick Venture #include <unordered_map>
41ba8ffa73SPatrick Venture #include <utility>
42da4a5dd1SPatrick Venture #include <vector>
43e620656cSPatrick Venture 
449f044415SPatrick Venture #if CONFIGURE_DBUS
459f044415SPatrick Venture #include "dbus/dbusconfiguration.hpp"
469f044415SPatrick Venture #endif
479f044415SPatrick Venture 
48e620656cSPatrick Venture /* The YAML converted sensor list. */
49f81f2886SJames Feist std::map<std::string, struct conf::SensorConfig> sensorConfig = {};
50e620656cSPatrick Venture /* The YAML converted PID list. */
51f81f2886SJames Feist std::map<int64_t, conf::PIDConf> zoneConfig = {};
52e620656cSPatrick Venture /* The YAML converted Zone configuration. */
53f81f2886SJames Feist std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {};
54e620656cSPatrick Venture 
55c19f5d4dSPatrick Venture /** the swampd daemon will check for the existence of this file. */
56c19f5d4dSPatrick Venture constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json";
57c19f5d4dSPatrick Venture 
58e620656cSPatrick Venture int main(int argc, char* argv[])
59e620656cSPatrick Venture {
60e620656cSPatrick Venture     int rc = 0;
61e620656cSPatrick Venture     std::string configPath = "";
62*b5cc37ceSPatrick Venture     tuningLoggingPath = "";
63e620656cSPatrick Venture 
64*b5cc37ceSPatrick Venture     CLI::App app{"OpenBMC Fan Control Daemon"};
65e620656cSPatrick Venture 
66*b5cc37ceSPatrick Venture     app.add_option("-c,--conf", configPath,
67*b5cc37ceSPatrick Venture                    "Optional parameter to specify configuration at run-time")
68*b5cc37ceSPatrick Venture         ->check(CLI::ExistingFile);
69*b5cc37ceSPatrick Venture     app.add_option("-t,--tuning", tuningLoggingPath,
70*b5cc37ceSPatrick Venture                    "Optional parameter to specify tuning logging path, and "
71*b5cc37ceSPatrick Venture                    "enable tuning")
72*b5cc37ceSPatrick Venture         ->check(CLI::ExistingFile);
73e620656cSPatrick Venture 
74*b5cc37ceSPatrick Venture     CLI11_PARSE(app, argc, argv);
75e620656cSPatrick Venture 
76*b5cc37ceSPatrick Venture     tuningLoggingEnabled = (tuningLoggingPath.length() > 0);
77e620656cSPatrick Venture 
789fa90c19SJames Feist     auto modeControlBus = sdbusplus::bus::new_system();
794cb7c058SPatrick Venture     static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
804cb7c058SPatrick Venture     // Create a manager for the ModeBus because we own it.
814cb7c058SPatrick Venture     sdbusplus::server::manager::manager(modeControlBus, modeRoot);
824cb7c058SPatrick Venture 
839f044415SPatrick Venture #if CONFIGURE_DBUS
847136a5aeSJames Feist     {
85c179d406SPatrick Venture         dbus_configuration::init(modeControlBus);
867136a5aeSJames Feist     }
8718b1311eSPatrick Venture #else
8818b1311eSPatrick Venture     const std::string& path =
8918b1311eSPatrick Venture         (configPath.length() > 0) ? configPath : jsonConfigurationPath;
90e620656cSPatrick Venture 
91e620656cSPatrick Venture     /*
92e620656cSPatrick Venture      * When building the sensors, if any of the dbus passive ones aren't on the
93e620656cSPatrick Venture      * bus, it'll fail immediately.
94e620656cSPatrick Venture      */
95e620656cSPatrick Venture     try
96e620656cSPatrick Venture     {
9718b1311eSPatrick Venture         auto jsonData = parseValidateJson(path);
984cb7c058SPatrick Venture         sensorConfig = buildSensorsFromJson(jsonData);
9918b1311eSPatrick Venture         std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData);
100e620656cSPatrick Venture     }
101e620656cSPatrick Venture     catch (const std::exception& e)
102e620656cSPatrick Venture     {
103e620656cSPatrick Venture         std::cerr << "Failed during building: " << e.what() << "\n";
104e620656cSPatrick Venture         exit(EXIT_FAILURE); /* fatal error. */
105e620656cSPatrick Venture     }
10618b1311eSPatrick Venture #endif
1074cb7c058SPatrick Venture 
1084cb7c058SPatrick Venture     SensorManager mgmr = buildSensors(sensorConfig);
1094cb7c058SPatrick Venture     std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones =
1104cb7c058SPatrick Venture         buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
111e620656cSPatrick Venture 
112e620656cSPatrick Venture     if (0 == zones.size())
113e620656cSPatrick Venture     {
114e620656cSPatrick Venture         std::cerr << "No zones defined, exiting.\n";
115e620656cSPatrick Venture         return rc;
116e620656cSPatrick Venture     }
117e620656cSPatrick Venture 
118e620656cSPatrick Venture     /*
119e620656cSPatrick Venture      * All sensors are managed by one manager, but each zone has a pointer to
120e620656cSPatrick Venture      * it.
121e620656cSPatrick Venture      */
122e620656cSPatrick Venture 
123c179d406SPatrick Venture     auto& hostSensorBus = mgmr.getHostBus();
124c179d406SPatrick Venture     auto& passiveListeningBus = mgmr.getPassiveBus();
125e620656cSPatrick Venture 
126e620656cSPatrick Venture     std::cerr << "Starting threads\n";
127e620656cSPatrick Venture 
128e620656cSPatrick Venture     /* TODO(venture): Ask SensorManager if we have any passive sensors. */
129c179d406SPatrick Venture     struct ThreadParams p = {std::ref(passiveListeningBus), ""};
130c179d406SPatrick Venture     std::thread l(busThread, std::ref(p));
131e620656cSPatrick Venture 
132e620656cSPatrick Venture     /* TODO(venture): Ask SensorManager if we have any host sensors. */
133e620656cSPatrick Venture     static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external";
134c179d406SPatrick Venture     struct ThreadParams e = {std::ref(hostSensorBus), hostBus};
135c179d406SPatrick Venture     std::thread te(busThread, std::ref(e));
136e620656cSPatrick Venture 
137e620656cSPatrick Venture     static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl";
138c179d406SPatrick Venture     struct ThreadParams m = {std::ref(modeControlBus), modeBus};
139c179d406SPatrick Venture     std::thread tm(busThread, std::ref(m));
140e620656cSPatrick Venture 
141e620656cSPatrick Venture     std::vector<std::thread> zoneThreads;
142e620656cSPatrick Venture 
143e620656cSPatrick Venture     /* TODO(venture): This was designed to have one thread per zone, but really
144e620656cSPatrick Venture      * it could have one thread for all the zones and iterate through each
145e620656cSPatrick Venture      * sequentially as it goes -- and it'd probably be fast enough to do that,
146e620656cSPatrick Venture      * however, a system isn't likely going to have more than a couple zones.
147e620656cSPatrick Venture      * If it only has a couple zones, then this is fine.
148e620656cSPatrick Venture      */
1494a2dc4d8SPatrick Venture     for (const auto& i : zones)
150e620656cSPatrick Venture     {
151e620656cSPatrick Venture         std::cerr << "pushing zone" << std::endl;
1527af157b1SPatrick Venture         zoneThreads.push_back(std::thread(pidControlThread, i.second.get()));
153e620656cSPatrick Venture     }
154e620656cSPatrick Venture 
155e620656cSPatrick Venture     l.join();
156e620656cSPatrick Venture     te.join();
157e620656cSPatrick Venture     tm.join();
158e620656cSPatrick Venture     for (auto& t : zoneThreads)
159e620656cSPatrick Venture     {
160e620656cSPatrick Venture         t.join();
161e620656cSPatrick Venture     }
162e620656cSPatrick Venture 
163e620656cSPatrick Venture     return rc;
164e620656cSPatrick Venture }
165