blob: 3ed84f6a18f7c4565e71439b7045ddbd9b7c31cf [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>
34#include <experimental/any>
35#include <iostream>
36#include <map>
37#include <memory>
38#include <mutex> /* not yet used. */
39#include <sdbusplus/bus.hpp>
40#include <thread>
41#include <unordered_map>
42#include <vector>
Patrick Venturee6206562018-03-08 15:36:53 -080043
Patrick Venture9f044412018-09-20 19:49:55 -070044#if CONFIGURE_DBUS
45#include "dbus/dbusconfiguration.hpp"
46#endif
47
Patrick Venturee6206562018-03-08 15:36:53 -080048/* The YAML converted sensor list. */
Patrick Venturec54fbd82018-10-30 19:40:05 -070049extern std::map<std::string, struct SensorConfig> sensorConfig;
Patrick Venturee6206562018-03-08 15:36:53 -080050/* The YAML converted PID list. */
Patrick Venturec54fbd82018-10-30 19:40:05 -070051extern std::map<int64_t, PIDConf> zoneConfig;
Patrick Venturee6206562018-03-08 15:36:53 -080052/* The YAML converted Zone configuration. */
Patrick Venturec54fbd82018-10-30 19:40:05 -070053extern std::map<int64_t, struct ZoneConfig> zoneDetailsConfig;
Patrick Venturee6206562018-03-08 15:36:53 -080054
55int main(int argc, char* argv[])
56{
57 int rc = 0;
Patrick Venturee6206562018-03-08 15:36:53 -080058 std::string configPath = "";
59
60 while (1)
61 {
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070062 // clang-format off
63 static struct option long_options[] = {
Patrick Venturee6206562018-03-08 15:36:53 -080064 {"conf", required_argument, 0, 'c'},
65 {0, 0, 0, 0}
66 };
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070067 // clang-format on
Patrick Venturee6206562018-03-08 15:36:53 -080068
69 int option_index = 0;
Patrick Venturedf766f22018-10-13 09:30:58 -070070 int c = getopt_long(argc, argv, "c:", long_options, &option_index);
Patrick Venturee6206562018-03-08 15:36:53 -080071
72 if (c == -1)
73 {
74 break;
75 }
76
77 switch (c)
78 {
79 case 'c':
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070080 configPath = std::string{optarg};
Patrick Venturee6206562018-03-08 15:36:53 -080081 break;
82 default:
83 /* skip garbage. */
84 continue;
85 }
86 }
87
Patrick Venturec179d402018-10-30 19:51:55 -070088 auto modeControlBus = sdbusplus::bus::new_default();
Patrick Venture9f044412018-09-20 19:49:55 -070089#if CONFIGURE_DBUS
James Feist7136a5a2018-07-19 09:52:05 -070090 {
Patrick Venturec179d402018-10-30 19:51:55 -070091 dbus_configuration::init(modeControlBus);
James Feist7136a5a2018-07-19 09:52:05 -070092 }
Patrick Venture9f044412018-09-20 19:49:55 -070093#endif
Patrick Venturefe75b192018-06-08 11:19:43 -070094 SensorManager mgmr;
Patrick Venture5c7cc542018-06-11 14:29:38 -070095 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones;
Patrick Venturee6206562018-03-08 15:36:53 -080096
Gunnar Mills08afbb22018-06-14 08:50:53 -050097 // Create a manager for the ModeBus because we own it.
Patrick Venturee6206562018-03-08 15:36:53 -080098 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
Patrick Venturec179d402018-10-30 19:51:55 -070099 sdbusplus::server::manager::manager(modeControlBus, modeRoot);
Patrick Venturee6206562018-03-08 15:36:53 -0800100
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 {
Patrick Venture7af157b2018-10-30 11:24:40 -0700109 mgmr = buildSensorsFromConfig(configPath);
Patrick Venturec179d402018-10-30 19:51:55 -0700110 zones = buildZonesFromConfig(configPath, mgmr, modeControlBus);
Patrick Venturee6206562018-03-08 15:36:53 -0800111 }
112 catch (const std::exception& e)
113 {
114 std::cerr << "Failed during building: " << e.what() << "\n";
115 exit(EXIT_FAILURE); /* fatal error. */
116 }
117 }
118 else
119 {
Patrick Venturec54fbd82018-10-30 19:40:05 -0700120 mgmr = buildSensors(sensorConfig);
Patrick Venturec179d402018-10-30 19:51:55 -0700121 zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
Patrick Venturee6206562018-03-08 15:36:53 -0800122 }
123
124 if (0 == zones.size())
125 {
126 std::cerr << "No zones defined, exiting.\n";
127 return rc;
128 }
129
130 /*
131 * All sensors are managed by one manager, but each zone has a pointer to
132 * it.
133 */
134
Patrick Venturec179d402018-10-30 19:51:55 -0700135 auto& hostSensorBus = mgmr.getHostBus();
136 auto& passiveListeningBus = mgmr.getPassiveBus();
Patrick Venturee6206562018-03-08 15:36:53 -0800137
138 std::cerr << "Starting threads\n";
139
140 /* TODO(venture): Ask SensorManager if we have any passive sensors. */
Patrick Venturec179d402018-10-30 19:51:55 -0700141 struct ThreadParams p = {std::ref(passiveListeningBus), ""};
142 std::thread l(busThread, std::ref(p));
Patrick Venturee6206562018-03-08 15:36:53 -0800143
144 /* TODO(venture): Ask SensorManager if we have any host sensors. */
145 static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external";
Patrick Venturec179d402018-10-30 19:51:55 -0700146 struct ThreadParams e = {std::ref(hostSensorBus), hostBus};
147 std::thread te(busThread, std::ref(e));
Patrick Venturee6206562018-03-08 15:36:53 -0800148
149 static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl";
Patrick Venturec179d402018-10-30 19:51:55 -0700150 struct ThreadParams m = {std::ref(modeControlBus), modeBus};
151 std::thread tm(busThread, std::ref(m));
Patrick Venturee6206562018-03-08 15:36:53 -0800152
153 std::vector<std::thread> zoneThreads;
154
155 /* TODO(venture): This was designed to have one thread per zone, but really
156 * it could have one thread for all the zones and iterate through each
157 * sequentially as it goes -- and it'd probably be fast enough to do that,
158 * however, a system isn't likely going to have more than a couple zones.
159 * If it only has a couple zones, then this is fine.
160 */
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700161 for (const auto& i : zones)
Patrick Venturee6206562018-03-08 15:36:53 -0800162 {
163 std::cerr << "pushing zone" << std::endl;
Patrick Venture7af157b2018-10-30 11:24:40 -0700164 zoneThreads.push_back(std::thread(pidControlThread, i.second.get()));
Patrick Venturee6206562018-03-08 15:36:53 -0800165 }
166
167 l.join();
168 te.join();
169 tm.join();
170 for (auto& t : zoneThreads)
171 {
172 t.join();
173 }
174
175 return rc;
176}