xref: /openbmc/phosphor-pid-control/main.cpp (revision da4a5dd133b88ebfeb69e89d05b381f81ba70e50)
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 
19*da4a5dd1SPatrick Venture #include "conf.hpp"
20*da4a5dd1SPatrick Venture #include "dbus/dbusconfiguration.hpp"
21e620656cSPatrick Venture #include "interfaces.hpp"
225c7cc545SPatrick Venture #include "pid/builder.hpp"
235c7cc545SPatrick Venture #include "pid/builderconfig.hpp"
24*da4a5dd1SPatrick Venture #include "pid/pidthread.hpp"
25e620656cSPatrick Venture #include "pid/zone.hpp"
265e929093SPatrick Venture #include "sensors/builder.hpp"
275e929093SPatrick Venture #include "sensors/builderconfig.hpp"
28e620656cSPatrick Venture #include "sensors/manager.hpp"
29e620656cSPatrick Venture #include "threads/busthread.hpp"
30*da4a5dd1SPatrick Venture #include "util.hpp"
31e620656cSPatrick Venture 
32*da4a5dd1SPatrick Venture #include <getopt.h>
33*da4a5dd1SPatrick Venture 
34*da4a5dd1SPatrick Venture #include <chrono>
35*da4a5dd1SPatrick Venture #include <experimental/any>
36*da4a5dd1SPatrick Venture #include <iostream>
37*da4a5dd1SPatrick Venture #include <map>
38*da4a5dd1SPatrick Venture #include <memory>
39*da4a5dd1SPatrick Venture #include <mutex> /* not yet used. */
40*da4a5dd1SPatrick Venture #include <sdbusplus/bus.hpp>
41*da4a5dd1SPatrick Venture #include <thread>
42*da4a5dd1SPatrick Venture #include <unordered_map>
43*da4a5dd1SPatrick Venture #include <vector>
44e620656cSPatrick Venture 
45e620656cSPatrick Venture /* The YAML converted sensor list. */
46e620656cSPatrick Venture extern std::map<std::string, struct sensor> SensorConfig;
47e620656cSPatrick Venture /* The YAML converted PID list. */
48e620656cSPatrick Venture extern std::map<int64_t, PIDConf> ZoneConfig;
49e620656cSPatrick Venture /* The YAML converted Zone configuration. */
50e620656cSPatrick Venture extern std::map<int64_t, struct zone> ZoneDetailsConfig;
51e620656cSPatrick Venture 
52e620656cSPatrick Venture int main(int argc, char* argv[])
53e620656cSPatrick Venture {
54e620656cSPatrick Venture     int rc = 0;
55e620656cSPatrick Venture     int c;
56e620656cSPatrick Venture     std::string configPath = "";
57e620656cSPatrick Venture 
58e620656cSPatrick Venture     while (1)
59e620656cSPatrick Venture     {
60*da4a5dd1SPatrick Venture         // clang-format off
61*da4a5dd1SPatrick Venture         static struct option long_options[] = {
62e620656cSPatrick Venture             {"conf", required_argument, 0, 'c'},
63e620656cSPatrick Venture             {0, 0, 0, 0}
64e620656cSPatrick Venture         };
65*da4a5dd1SPatrick Venture         // clang-format on
66e620656cSPatrick Venture 
67e620656cSPatrick Venture         int option_index = 0;
68e620656cSPatrick Venture         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 
86e620656cSPatrick Venture     auto ModeControlBus = sdbusplus::bus::new_default();
877136a5aeSJames Feist     if (configureDbus)
887136a5aeSJames Feist     {
897136a5aeSJames Feist         dbus_configuration::init(ModeControlBus);
907136a5aeSJames Feist     }
91fe75b193SPatrick Venture     SensorManager mgmr;
925c7cc545SPatrick Venture     std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones;
93e620656cSPatrick Venture 
9408afbb25SGunnar Mills     // Create a manager for the ModeBus because we own it.
95e620656cSPatrick Venture     static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
96e620656cSPatrick Venture     sdbusplus::server::manager::manager(ModeControlBus, modeRoot);
97e620656cSPatrick Venture 
98e620656cSPatrick Venture     /*
99e620656cSPatrick Venture      * When building the sensors, if any of the dbus passive ones aren't on the
100e620656cSPatrick Venture      * bus, it'll fail immediately.
101e620656cSPatrick Venture      */
102e620656cSPatrick Venture     if (configPath.length() > 0)
103e620656cSPatrick Venture     {
104e620656cSPatrick Venture         try
105e620656cSPatrick Venture         {
106e620656cSPatrick Venture             mgmr = BuildSensorsFromConfig(configPath);
107e620656cSPatrick Venture             zones = BuildZonesFromConfig(configPath, mgmr, ModeControlBus);
108e620656cSPatrick Venture         }
109e620656cSPatrick Venture         catch (const std::exception& e)
110e620656cSPatrick Venture         {
111e620656cSPatrick Venture             std::cerr << "Failed during building: " << e.what() << "\n";
112e620656cSPatrick Venture             exit(EXIT_FAILURE); /* fatal error. */
113e620656cSPatrick Venture         }
114e620656cSPatrick Venture     }
115e620656cSPatrick Venture     else
116e620656cSPatrick Venture     {
117e620656cSPatrick Venture         mgmr = BuildSensors(SensorConfig);
118e620656cSPatrick Venture         zones = BuildZones(ZoneConfig, ZoneDetailsConfig, mgmr, ModeControlBus);
119e620656cSPatrick Venture     }
120e620656cSPatrick Venture 
121e620656cSPatrick Venture     if (0 == zones.size())
122e620656cSPatrick Venture     {
123e620656cSPatrick Venture         std::cerr << "No zones defined, exiting.\n";
124e620656cSPatrick Venture         return rc;
125e620656cSPatrick Venture     }
126e620656cSPatrick Venture 
127e620656cSPatrick Venture     /*
128e620656cSPatrick Venture      * All sensors are managed by one manager, but each zone has a pointer to
129e620656cSPatrick Venture      * it.
130e620656cSPatrick Venture      */
131e620656cSPatrick Venture 
132fe75b193SPatrick Venture     auto& HostSensorBus = mgmr.getHostBus();
133fe75b193SPatrick Venture     auto& PassiveListeningBus = mgmr.getPassiveBus();
134e620656cSPatrick Venture 
135e620656cSPatrick Venture     std::cerr << "Starting threads\n";
136e620656cSPatrick Venture 
137e620656cSPatrick Venture     /* TODO(venture): Ask SensorManager if we have any passive sensors. */
138*da4a5dd1SPatrick Venture     struct ThreadParams p = {std::ref(PassiveListeningBus), ""};
139e620656cSPatrick Venture     std::thread l(BusThread, std::ref(p));
140e620656cSPatrick Venture 
141e620656cSPatrick Venture     /* TODO(venture): Ask SensorManager if we have any host sensors. */
142e620656cSPatrick Venture     static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external";
143*da4a5dd1SPatrick Venture     struct ThreadParams e = {std::ref(HostSensorBus), hostBus};
144e620656cSPatrick Venture     std::thread te(BusThread, std::ref(e));
145e620656cSPatrick Venture 
146e620656cSPatrick Venture     static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl";
147*da4a5dd1SPatrick Venture     struct ThreadParams m = {std::ref(ModeControlBus), modeBus};
148e620656cSPatrick Venture     std::thread tm(BusThread, std::ref(m));
149e620656cSPatrick Venture 
150e620656cSPatrick Venture     std::vector<std::thread> zoneThreads;
151e620656cSPatrick Venture 
152e620656cSPatrick Venture     /* TODO(venture): This was designed to have one thread per zone, but really
153e620656cSPatrick Venture      * it could have one thread for all the zones and iterate through each
154e620656cSPatrick Venture      * sequentially as it goes -- and it'd probably be fast enough to do that,
155e620656cSPatrick Venture      * however, a system isn't likely going to have more than a couple zones.
156e620656cSPatrick Venture      * If it only has a couple zones, then this is fine.
157e620656cSPatrick Venture      */
158e620656cSPatrick Venture     for (auto& i : zones)
159e620656cSPatrick Venture     {
160e620656cSPatrick Venture         std::cerr << "pushing zone" << std::endl;
1615c7cc545SPatrick Venture         zoneThreads.push_back(std::thread(PIDControlThread, i.second.get()));
162e620656cSPatrick Venture     }
163e620656cSPatrick Venture 
164e620656cSPatrick Venture     l.join();
165e620656cSPatrick Venture     te.join();
166e620656cSPatrick Venture     tm.join();
167e620656cSPatrick Venture     for (auto& t : zoneThreads)
168e620656cSPatrick Venture     {
169e620656cSPatrick Venture         t.join();
170e620656cSPatrick Venture     }
171e620656cSPatrick Venture 
172e620656cSPatrick Venture     return rc;
173e620656cSPatrick Venture }
174