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