blob: 8e781bcb21bb869fb007cd68d6fed326105a2214 [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
James Feist7136a5a2018-07-19 09:52:05 -070017#include "config.h"
Patrick Venturee6206562018-03-08 15:36:53 -080018
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070019#include "conf.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080020#include "interfaces.hpp"
Patrick Venture5c7cc542018-06-11 14:29:38 -070021#include "pid/builder.hpp"
22#include "pid/builderconfig.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070023#include "pid/pidthread.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080024#include "pid/zone.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070025#include "sensors/builder.hpp"
26#include "sensors/builderconfig.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080027#include "sensors/manager.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080028#include "threads/busthread.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070029#include "util.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080030
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070031#include <getopt.h>
32
33#include <chrono>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070034#include <iostream>
35#include <map>
36#include <memory>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070037#include <sdbusplus/bus.hpp>
38#include <thread>
39#include <unordered_map>
40#include <vector>
Patrick Venturee6206562018-03-08 15:36:53 -080041
Patrick Venture9f044412018-09-20 19:49:55 -070042#if CONFIGURE_DBUS
43#include "dbus/dbusconfiguration.hpp"
44#endif
45
Patrick Venturee6206562018-03-08 15:36:53 -080046/* The YAML converted sensor list. */
Patrick Venturec54fbd82018-10-30 19:40:05 -070047extern std::map<std::string, struct SensorConfig> sensorConfig;
Patrick Venturee6206562018-03-08 15:36:53 -080048/* The YAML converted PID list. */
Patrick Venturec54fbd82018-10-30 19:40:05 -070049extern std::map<int64_t, PIDConf> zoneConfig;
Patrick Venturee6206562018-03-08 15:36:53 -080050/* The YAML converted Zone configuration. */
Patrick Venturec54fbd82018-10-30 19:40:05 -070051extern std::map<int64_t, struct ZoneConfig> zoneDetailsConfig;
Patrick Venturee6206562018-03-08 15:36:53 -080052
53int main(int argc, char* argv[])
54{
55 int rc = 0;
Patrick Venturee6206562018-03-08 15:36:53 -080056 std::string configPath = "";
57
58 while (1)
59 {
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070060 // clang-format off
61 static struct option long_options[] = {
Patrick Venturee6206562018-03-08 15:36:53 -080062 {"conf", required_argument, 0, 'c'},
63 {0, 0, 0, 0}
64 };
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070065 // clang-format on
Patrick Venturee6206562018-03-08 15:36:53 -080066
67 int option_index = 0;
Patrick Venturedf766f22018-10-13 09:30:58 -070068 int c = getopt_long(argc, argv, "c:", long_options, &option_index);
Patrick Venturee6206562018-03-08 15:36:53 -080069
70 if (c == -1)
71 {
72 break;
73 }
74
75 switch (c)
76 {
77 case 'c':
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070078 configPath = std::string{optarg};
Patrick Venturee6206562018-03-08 15:36:53 -080079 break;
80 default:
81 /* skip garbage. */
82 continue;
83 }
84 }
85
Patrick Venturec179d402018-10-30 19:51:55 -070086 auto modeControlBus = sdbusplus::bus::new_default();
Patrick Venture9f044412018-09-20 19:49:55 -070087#if CONFIGURE_DBUS
James Feist7136a5a2018-07-19 09:52:05 -070088 {
Patrick Venturec179d402018-10-30 19:51:55 -070089 dbus_configuration::init(modeControlBus);
James Feist7136a5a2018-07-19 09:52:05 -070090 }
Patrick Venture9f044412018-09-20 19:49:55 -070091#endif
Patrick Venturefe75b192018-06-08 11:19:43 -070092 SensorManager mgmr;
Patrick Venture5c7cc542018-06-11 14:29:38 -070093 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones;
Patrick Venturee6206562018-03-08 15:36:53 -080094
Gunnar Mills08afbb22018-06-14 08:50:53 -050095 // Create a manager for the ModeBus because we own it.
Patrick Venturee6206562018-03-08 15:36:53 -080096 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
Patrick Venturec179d402018-10-30 19:51:55 -070097 sdbusplus::server::manager::manager(modeControlBus, modeRoot);
Patrick Venturee6206562018-03-08 15:36:53 -080098
99 /*
100 * When building the sensors, if any of the dbus passive ones aren't on the
101 * bus, it'll fail immediately.
102 */
103 if (configPath.length() > 0)
104 {
105 try
106 {
Patrick Venture7af157b2018-10-30 11:24:40 -0700107 mgmr = buildSensorsFromConfig(configPath);
Patrick Venturec179d402018-10-30 19:51:55 -0700108 zones = buildZonesFromConfig(configPath, mgmr, modeControlBus);
Patrick Venturee6206562018-03-08 15:36:53 -0800109 }
110 catch (const std::exception& e)
111 {
112 std::cerr << "Failed during building: " << e.what() << "\n";
113 exit(EXIT_FAILURE); /* fatal error. */
114 }
115 }
116 else
117 {
Patrick Venturec54fbd82018-10-30 19:40:05 -0700118 mgmr = buildSensors(sensorConfig);
Patrick Venturec179d402018-10-30 19:51:55 -0700119 zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
Patrick Venturee6206562018-03-08 15:36:53 -0800120 }
121
122 if (0 == zones.size())
123 {
124 std::cerr << "No zones defined, exiting.\n";
125 return rc;
126 }
127
128 /*
129 * All sensors are managed by one manager, but each zone has a pointer to
130 * it.
131 */
132
Patrick Venturec179d402018-10-30 19:51:55 -0700133 auto& hostSensorBus = mgmr.getHostBus();
134 auto& passiveListeningBus = mgmr.getPassiveBus();
Patrick Venturee6206562018-03-08 15:36:53 -0800135
136 std::cerr << "Starting threads\n";
137
138 /* TODO(venture): Ask SensorManager if we have any passive sensors. */
Patrick Venturec179d402018-10-30 19:51:55 -0700139 struct ThreadParams p = {std::ref(passiveListeningBus), ""};
140 std::thread l(busThread, std::ref(p));
Patrick Venturee6206562018-03-08 15:36:53 -0800141
142 /* TODO(venture): Ask SensorManager if we have any host sensors. */
143 static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external";
Patrick Venturec179d402018-10-30 19:51:55 -0700144 struct ThreadParams e = {std::ref(hostSensorBus), hostBus};
145 std::thread te(busThread, std::ref(e));
Patrick Venturee6206562018-03-08 15:36:53 -0800146
147 static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl";
Patrick Venturec179d402018-10-30 19:51:55 -0700148 struct ThreadParams m = {std::ref(modeControlBus), modeBus};
149 std::thread tm(busThread, std::ref(m));
Patrick Venturee6206562018-03-08 15:36:53 -0800150
151 std::vector<std::thread> zoneThreads;
152
153 /* TODO(venture): This was designed to have one thread per zone, but really
154 * it could have one thread for all the zones and iterate through each
155 * sequentially as it goes -- and it'd probably be fast enough to do that,
156 * however, a system isn't likely going to have more than a couple zones.
157 * If it only has a couple zones, then this is fine.
158 */
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700159 for (const auto& i : zones)
Patrick Venturee6206562018-03-08 15:36:53 -0800160 {
161 std::cerr << "pushing zone" << std::endl;
Patrick Venture7af157b2018-10-30 11:24:40 -0700162 zoneThreads.push_back(std::thread(pidControlThread, i.second.get()));
Patrick Venturee6206562018-03-08 15:36:53 -0800163 }
164
165 l.join();
166 te.join();
167 tm.join();
168 for (auto& t : zoneThreads)
169 {
170 t.join();
171 }
172
173 return rc;
174}