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