blob: 2db1ff98d06405afe602b1b69805b7cbd15c1629 [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 Ventureba8ffa72019-02-11 12:03:56 -080019#include "build/buildjson.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070020#include "conf.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080021#include "interfaces.hpp"
Patrick Venture5c7cc542018-06-11 14:29:38 -070022#include "pid/builder.hpp"
Patrick Ventureba8ffa72019-02-11 12:03:56 -080023#include "pid/buildjson.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070024#include "pid/pidthread.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -080025#include "pid/tuning.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080026#include "pid/zone.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070027#include "sensors/builder.hpp"
Patrick Ventureba8ffa72019-02-11 12:03:56 -080028#include "sensors/buildjson.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080029#include "sensors/manager.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080030#include "threads/busthread.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070031#include "util.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080032
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070033#include <CLI/CLI.hpp>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070034#include <chrono>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070035#include <iostream>
36#include <map>
37#include <memory>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070038#include <sdbusplus/bus.hpp>
39#include <thread>
40#include <unordered_map>
Patrick Ventureba8ffa72019-02-11 12:03:56 -080041#include <utility>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070042#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. */
James Feistf81f2882019-02-26 11:26:36 -080049std::map<std::string, struct conf::SensorConfig> sensorConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080050/* The YAML converted PID list. */
James Feistf81f2882019-02-26 11:26:36 -080051std::map<int64_t, conf::PIDConf> zoneConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080052/* The YAML converted Zone configuration. */
James Feistf81f2882019-02-26 11:26:36 -080053std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080054
Patrick Venturec19f5d42019-02-14 10:59:47 -080055/** the swampd daemon will check for the existence of this file. */
56constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json";
57
Patrick Venturee6206562018-03-08 15:36:53 -080058int main(int argc, char* argv[])
59{
60 int rc = 0;
Patrick Venturee6206562018-03-08 15:36:53 -080061 std::string configPath = "";
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070062 tuningLoggingPath = "";
Patrick Venturee6206562018-03-08 15:36:53 -080063
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070064 CLI::App app{"OpenBMC Fan Control Daemon"};
Patrick Venturee6206562018-03-08 15:36:53 -080065
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070066 app.add_option("-c,--conf", configPath,
67 "Optional parameter to specify configuration at run-time")
68 ->check(CLI::ExistingFile);
69 app.add_option("-t,--tuning", tuningLoggingPath,
70 "Optional parameter to specify tuning logging path, and "
71 "enable tuning")
72 ->check(CLI::ExistingFile);
Patrick Venturee6206562018-03-08 15:36:53 -080073
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070074 CLI11_PARSE(app, argc, argv);
Patrick Venturee6206562018-03-08 15:36:53 -080075
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070076 tuningLoggingEnabled = (tuningLoggingPath.length() > 0);
Patrick Venturee6206562018-03-08 15:36:53 -080077
James Feist9fa90c12019-01-11 15:35:22 -080078 auto modeControlBus = sdbusplus::bus::new_system();
Patrick Venture4cb7c052019-02-14 11:11:33 -080079 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
80 // Create a manager for the ModeBus because we own it.
81 sdbusplus::server::manager::manager(modeControlBus, modeRoot);
82
Patrick Venture9f044412018-09-20 19:49:55 -070083#if CONFIGURE_DBUS
James Feist7136a5a2018-07-19 09:52:05 -070084 {
Patrick Venturec179d402018-10-30 19:51:55 -070085 dbus_configuration::init(modeControlBus);
James Feist7136a5a2018-07-19 09:52:05 -070086 }
Patrick Venture18b13112019-02-14 11:43:59 -080087#else
88 const std::string& path =
89 (configPath.length() > 0) ? configPath : jsonConfigurationPath;
Patrick Venturee6206562018-03-08 15:36:53 -080090
91 /*
92 * When building the sensors, if any of the dbus passive ones aren't on the
93 * bus, it'll fail immediately.
94 */
Patrick Venture18b13112019-02-14 11:43:59 -080095 try
Patrick Venturee6206562018-03-08 15:36:53 -080096 {
Patrick Venture18b13112019-02-14 11:43:59 -080097 auto jsonData = parseValidateJson(path);
98 sensorConfig = buildSensorsFromJson(jsonData);
99 std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData);
Patrick Venturee6206562018-03-08 15:36:53 -0800100 }
Patrick Venture18b13112019-02-14 11:43:59 -0800101 catch (const std::exception& e)
102 {
103 std::cerr << "Failed during building: " << e.what() << "\n";
104 exit(EXIT_FAILURE); /* fatal error. */
105 }
106#endif
Patrick Venture4cb7c052019-02-14 11:11:33 -0800107
108 SensorManager mgmr = buildSensors(sensorConfig);
109 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones =
110 buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
Patrick Venturee6206562018-03-08 15:36:53 -0800111
112 if (0 == zones.size())
113 {
114 std::cerr << "No zones defined, exiting.\n";
115 return rc;
116 }
117
118 /*
119 * All sensors are managed by one manager, but each zone has a pointer to
120 * it.
121 */
122
Patrick Venturec179d402018-10-30 19:51:55 -0700123 auto& hostSensorBus = mgmr.getHostBus();
124 auto& passiveListeningBus = mgmr.getPassiveBus();
Patrick Venturee6206562018-03-08 15:36:53 -0800125
126 std::cerr << "Starting threads\n";
127
128 /* TODO(venture): Ask SensorManager if we have any passive sensors. */
Patrick Venturec179d402018-10-30 19:51:55 -0700129 struct ThreadParams p = {std::ref(passiveListeningBus), ""};
130 std::thread l(busThread, std::ref(p));
Patrick Venturee6206562018-03-08 15:36:53 -0800131
132 /* TODO(venture): Ask SensorManager if we have any host sensors. */
133 static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external";
Patrick Venturec179d402018-10-30 19:51:55 -0700134 struct ThreadParams e = {std::ref(hostSensorBus), hostBus};
135 std::thread te(busThread, std::ref(e));
Patrick Venturee6206562018-03-08 15:36:53 -0800136
137 static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl";
Patrick Venturec179d402018-10-30 19:51:55 -0700138 struct ThreadParams m = {std::ref(modeControlBus), modeBus};
139 std::thread tm(busThread, std::ref(m));
Patrick Venturee6206562018-03-08 15:36:53 -0800140
141 std::vector<std::thread> zoneThreads;
142
143 /* TODO(venture): This was designed to have one thread per zone, but really
144 * it could have one thread for all the zones and iterate through each
145 * sequentially as it goes -- and it'd probably be fast enough to do that,
146 * however, a system isn't likely going to have more than a couple zones.
147 * If it only has a couple zones, then this is fine.
148 */
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700149 for (const auto& i : zones)
Patrick Venturee6206562018-03-08 15:36:53 -0800150 {
151 std::cerr << "pushing zone" << std::endl;
Patrick Venture7af157b2018-10-30 11:24:40 -0700152 zoneThreads.push_back(std::thread(pidControlThread, i.second.get()));
Patrick Venturee6206562018-03-08 15:36:53 -0800153 }
154
155 l.join();
156 te.join();
157 tm.join();
158 for (auto& t : zoneThreads)
159 {
160 t.join();
161 }
162
163 return rc;
164}