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