xref: /openbmc/phosphor-pid-control/main.cpp (revision 9fa90c1923b8842bd1aab8116335862849fedf79)
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 
19da4a5dd1SPatrick Venture #include "conf.hpp"
20e620656cSPatrick Venture #include "interfaces.hpp"
215c7cc545SPatrick Venture #include "pid/builder.hpp"
225c7cc545SPatrick Venture #include "pid/builderconfig.hpp"
23da4a5dd1SPatrick Venture #include "pid/pidthread.hpp"
24e620656cSPatrick Venture #include "pid/zone.hpp"
255e929093SPatrick Venture #include "sensors/builder.hpp"
265e929093SPatrick Venture #include "sensors/builderconfig.hpp"
27e620656cSPatrick Venture #include "sensors/manager.hpp"
28e620656cSPatrick Venture #include "threads/busthread.hpp"
29da4a5dd1SPatrick Venture #include "util.hpp"
30e620656cSPatrick Venture 
31da4a5dd1SPatrick Venture #include <getopt.h>
32da4a5dd1SPatrick Venture 
33da4a5dd1SPatrick Venture #include <chrono>
34da4a5dd1SPatrick Venture #include <iostream>
35da4a5dd1SPatrick Venture #include <map>
36da4a5dd1SPatrick Venture #include <memory>
37da4a5dd1SPatrick Venture #include <sdbusplus/bus.hpp>
38da4a5dd1SPatrick Venture #include <thread>
39da4a5dd1SPatrick Venture #include <unordered_map>
40da4a5dd1SPatrick Venture #include <vector>
41e620656cSPatrick Venture 
429f044415SPatrick Venture #if CONFIGURE_DBUS
439f044415SPatrick Venture #include "dbus/dbusconfiguration.hpp"
449f044415SPatrick Venture #endif
459f044415SPatrick Venture 
46e620656cSPatrick Venture /* The YAML converted sensor list. */
47c54fbd88SPatrick Venture extern std::map<std::string, struct SensorConfig> sensorConfig;
48e620656cSPatrick Venture /* The YAML converted PID list. */
49c54fbd88SPatrick Venture extern std::map<int64_t, PIDConf> zoneConfig;
50e620656cSPatrick Venture /* The YAML converted Zone configuration. */
51c54fbd88SPatrick Venture extern std::map<int64_t, struct ZoneConfig> zoneDetailsConfig;
52e620656cSPatrick Venture 
53e620656cSPatrick Venture int main(int argc, char* argv[])
54e620656cSPatrick Venture {
55e620656cSPatrick Venture     int rc = 0;
56e620656cSPatrick Venture     std::string configPath = "";
57e620656cSPatrick Venture 
58e620656cSPatrick Venture     while (1)
59e620656cSPatrick Venture     {
60da4a5dd1SPatrick Venture         // clang-format off
61da4a5dd1SPatrick Venture         static struct option long_options[] = {
62e620656cSPatrick Venture             {"conf", required_argument, 0, 'c'},
63e620656cSPatrick Venture             {0, 0, 0, 0}
64e620656cSPatrick Venture         };
65da4a5dd1SPatrick Venture         // clang-format on
66e620656cSPatrick Venture 
67e620656cSPatrick Venture         int option_index = 0;
68df766f25SPatrick Venture         int c = getopt_long(argc, argv, "c:", long_options, &option_index);
69e620656cSPatrick Venture 
70e620656cSPatrick Venture         if (c == -1)
71e620656cSPatrick Venture         {
72e620656cSPatrick Venture             break;
73e620656cSPatrick Venture         }
74e620656cSPatrick Venture 
75e620656cSPatrick Venture         switch (c)
76e620656cSPatrick Venture         {
77e620656cSPatrick Venture             case 'c':
78e620656cSPatrick Venture                 configPath = std::string{optarg};
79e620656cSPatrick Venture                 break;
80e620656cSPatrick Venture             default:
81e620656cSPatrick Venture                 /* skip garbage. */
82e620656cSPatrick Venture                 continue;
83e620656cSPatrick Venture         }
84e620656cSPatrick Venture     }
85e620656cSPatrick Venture 
86*9fa90c19SJames Feist     auto modeControlBus = sdbusplus::bus::new_system();
879f044415SPatrick Venture #if CONFIGURE_DBUS
887136a5aeSJames Feist     {
89c179d406SPatrick Venture         dbus_configuration::init(modeControlBus);
907136a5aeSJames Feist     }
919f044415SPatrick Venture #endif
92fe75b193SPatrick Venture     SensorManager mgmr;
935c7cc545SPatrick Venture     std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones;
94e620656cSPatrick Venture 
9508afbb25SGunnar Mills     // Create a manager for the ModeBus because we own it.
96e620656cSPatrick Venture     static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
97c179d406SPatrick Venture     sdbusplus::server::manager::manager(modeControlBus, modeRoot);
98e620656cSPatrick Venture 
99e620656cSPatrick Venture     /*
100e620656cSPatrick Venture      * When building the sensors, if any of the dbus passive ones aren't on the
101e620656cSPatrick Venture      * bus, it'll fail immediately.
102e620656cSPatrick Venture      */
103e620656cSPatrick Venture     if (configPath.length() > 0)
104e620656cSPatrick Venture     {
105e620656cSPatrick Venture         try
106e620656cSPatrick Venture         {
1077af157b1SPatrick Venture             mgmr = buildSensorsFromConfig(configPath);
108c179d406SPatrick Venture             zones = buildZonesFromConfig(configPath, mgmr, modeControlBus);
109e620656cSPatrick Venture         }
110e620656cSPatrick Venture         catch (const std::exception& e)
111e620656cSPatrick Venture         {
112e620656cSPatrick Venture             std::cerr << "Failed during building: " << e.what() << "\n";
113e620656cSPatrick Venture             exit(EXIT_FAILURE); /* fatal error. */
114e620656cSPatrick Venture         }
115e620656cSPatrick Venture     }
116e620656cSPatrick Venture     else
117e620656cSPatrick Venture     {
118c54fbd88SPatrick Venture         mgmr = buildSensors(sensorConfig);
119c179d406SPatrick Venture         zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
120e620656cSPatrick Venture     }
121e620656cSPatrick Venture 
122e620656cSPatrick Venture     if (0 == zones.size())
123e620656cSPatrick Venture     {
124e620656cSPatrick Venture         std::cerr << "No zones defined, exiting.\n";
125e620656cSPatrick Venture         return rc;
126e620656cSPatrick Venture     }
127e620656cSPatrick Venture 
128e620656cSPatrick Venture     /*
129e620656cSPatrick Venture      * All sensors are managed by one manager, but each zone has a pointer to
130e620656cSPatrick Venture      * it.
131e620656cSPatrick Venture      */
132e620656cSPatrick Venture 
133c179d406SPatrick Venture     auto& hostSensorBus = mgmr.getHostBus();
134c179d406SPatrick Venture     auto& passiveListeningBus = mgmr.getPassiveBus();
135e620656cSPatrick Venture 
136e620656cSPatrick Venture     std::cerr << "Starting threads\n";
137e620656cSPatrick Venture 
138e620656cSPatrick Venture     /* TODO(venture): Ask SensorManager if we have any passive sensors. */
139c179d406SPatrick Venture     struct ThreadParams p = {std::ref(passiveListeningBus), ""};
140c179d406SPatrick Venture     std::thread l(busThread, std::ref(p));
141e620656cSPatrick Venture 
142e620656cSPatrick Venture     /* TODO(venture): Ask SensorManager if we have any host sensors. */
143e620656cSPatrick Venture     static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external";
144c179d406SPatrick Venture     struct ThreadParams e = {std::ref(hostSensorBus), hostBus};
145c179d406SPatrick Venture     std::thread te(busThread, std::ref(e));
146e620656cSPatrick Venture 
147e620656cSPatrick Venture     static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl";
148c179d406SPatrick Venture     struct ThreadParams m = {std::ref(modeControlBus), modeBus};
149c179d406SPatrick Venture     std::thread tm(busThread, std::ref(m));
150e620656cSPatrick Venture 
151e620656cSPatrick Venture     std::vector<std::thread> zoneThreads;
152e620656cSPatrick Venture 
153e620656cSPatrick Venture     /* TODO(venture): This was designed to have one thread per zone, but really
154e620656cSPatrick Venture      * it could have one thread for all the zones and iterate through each
155e620656cSPatrick Venture      * sequentially as it goes -- and it'd probably be fast enough to do that,
156e620656cSPatrick Venture      * however, a system isn't likely going to have more than a couple zones.
157e620656cSPatrick Venture      * If it only has a couple zones, then this is fine.
158e620656cSPatrick Venture      */
1594a2dc4d8SPatrick Venture     for (const auto& i : zones)
160e620656cSPatrick Venture     {
161e620656cSPatrick Venture         std::cerr << "pushing zone" << std::endl;
1627af157b1SPatrick Venture         zoneThreads.push_back(std::thread(pidControlThread, i.second.get()));
163e620656cSPatrick Venture     }
164e620656cSPatrick Venture 
165e620656cSPatrick Venture     l.join();
166e620656cSPatrick Venture     te.join();
167e620656cSPatrick Venture     tm.join();
168e620656cSPatrick Venture     for (auto& t : zoneThreads)
169e620656cSPatrick Venture     {
170e620656cSPatrick Venture         t.join();
171e620656cSPatrick Venture     }
172e620656cSPatrick Venture 
173e620656cSPatrick Venture     return rc;
174e620656cSPatrick Venture }
175