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