blob: 75b55fdad25120856495557ab94d4494d45cf5f5 [file] [log] [blame]
Patrick Venturee6206562018-03-08 15:36:53 -08001/**
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>
Patrick Venture5c7cc542018-06-11 14:29:38 -070025#include <unordered_map>
Patrick Venturee6206562018-03-08 15:36:53 -080026#include <vector>
27
28#include <sdbusplus/bus.hpp>
29
30/* Configuration. */
31#include "conf.hpp"
James Feist7136a5a2018-07-19 09:52:05 -070032#include "config.h"
33#include <dbus/dbusconfiguration.hpp>
Patrick Venturee6206562018-03-08 15:36:53 -080034
35/* Misc. */
36#include "util.hpp"
37
38/* Controllers & Sensors. */
39#include "interfaces.hpp"
Patrick Venture5c7cc542018-06-11 14:29:38 -070040#include "pid/builder.hpp"
41#include "pid/builderconfig.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080042#include "pid/zone.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070043#include "sensors/builder.hpp"
44#include "sensors/builderconfig.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080045#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. */
53extern std::map<std::string, struct sensor> SensorConfig;
54/* The YAML converted PID list. */
55extern std::map<int64_t, PIDConf> ZoneConfig;
56/* The YAML converted Zone configuration. */
57extern std::map<int64_t, struct zone> ZoneDetailsConfig;
58
59int 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();
James Feist7136a5a2018-07-19 09:52:05 -070093 if (configureDbus)
94 {
95 dbus_configuration::init(ModeControlBus);
96 }
Patrick Venturefe75b192018-06-08 11:19:43 -070097 SensorManager mgmr;
Patrick Venture5c7cc542018-06-11 14:29:38 -070098 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones;
Patrick Venturee6206562018-03-08 15:36:53 -080099
Gunnar Mills08afbb22018-06-14 08:50:53 -0500100 // Create a manager for the ModeBus because we own it.
Patrick Venturee6206562018-03-08 15:36:53 -0800101 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
Patrick Venturefe75b192018-06-08 11:19:43 -0700138 auto& HostSensorBus = mgmr.getHostBus();
139 auto& PassiveListeningBus = mgmr.getPassiveBus();
Patrick Venturee6206562018-03-08 15:36:53 -0800140
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;
Patrick Venture5c7cc542018-06-11 14:29:38 -0700179 zoneThreads.push_back(std::thread(PIDControlThread, i.second.get()));
Patrick Venturee6206562018-03-08 15:36:53 -0800180 }
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