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