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