blob: 477be66f987afbb0aa383f39ede8bb57ee391a49 [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"
James Feistce6a3f32019-03-12 11:20:16 -070024#include "pid/pidloop.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 Ventureda4a5dd2018-08-31 09:42:48 -070030#include "util.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080031
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070032#include <CLI/CLI.hpp>
James Feistce6a3f32019-03-12 11:20:16 -070033#include <boost/asio/io_context.hpp>
34#include <boost/asio/steady_timer.hpp>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070035#include <chrono>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070036#include <iostream>
James Feistce6a3f32019-03-12 11:20:16 -070037#include <list>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070038#include <map>
39#include <memory>
James Feistce6a3f32019-03-12 11:20:16 -070040#include <sdbusplus/asio/connection.hpp>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070041#include <sdbusplus/bus.hpp>
42#include <thread>
43#include <unordered_map>
Patrick Ventureba8ffa72019-02-11 12:03:56 -080044#include <utility>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070045#include <vector>
Patrick Venturee6206562018-03-08 15:36:53 -080046
Patrick Venture9f044412018-09-20 19:49:55 -070047#if CONFIGURE_DBUS
48#include "dbus/dbusconfiguration.hpp"
49#endif
50
Patrick Venturee6206562018-03-08 15:36:53 -080051/* The YAML converted sensor list. */
James Feistf81f2882019-02-26 11:26:36 -080052std::map<std::string, struct conf::SensorConfig> sensorConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080053/* The YAML converted PID list. */
James Feistf81f2882019-02-26 11:26:36 -080054std::map<int64_t, conf::PIDConf> zoneConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080055/* The YAML converted Zone configuration. */
James Feistf81f2882019-02-26 11:26:36 -080056std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080057
Patrick Venturec19f5d42019-02-14 10:59:47 -080058/** the swampd daemon will check for the existence of this file. */
59constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json";
60
Patrick Venturee6206562018-03-08 15:36:53 -080061int main(int argc, char* argv[])
62{
63 int rc = 0;
Patrick Venturee6206562018-03-08 15:36:53 -080064 std::string configPath = "";
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070065 tuningLoggingPath = "";
Patrick Venturee6206562018-03-08 15:36:53 -080066
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070067 CLI::App app{"OpenBMC Fan Control Daemon"};
Patrick Venturee6206562018-03-08 15:36:53 -080068
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070069 app.add_option("-c,--conf", configPath,
70 "Optional parameter to specify configuration at run-time")
71 ->check(CLI::ExistingFile);
72 app.add_option("-t,--tuning", tuningLoggingPath,
73 "Optional parameter to specify tuning logging path, and "
Patrick Venturea3cac532019-05-08 13:31:54 -070074 "enable tuning");
Patrick Venturee6206562018-03-08 15:36:53 -080075
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070076 CLI11_PARSE(app, argc, argv);
Patrick Venturee6206562018-03-08 15:36:53 -080077
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070078 tuningLoggingEnabled = (tuningLoggingPath.length() > 0);
Patrick Venturee6206562018-03-08 15:36:53 -080079
James Feist9fa90c12019-01-11 15:35:22 -080080 auto modeControlBus = sdbusplus::bus::new_system();
Patrick Venture4cb7c052019-02-14 11:11:33 -080081 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
82 // Create a manager for the ModeBus because we own it.
83 sdbusplus::server::manager::manager(modeControlBus, modeRoot);
84
Patrick Venture9f044412018-09-20 19:49:55 -070085#if CONFIGURE_DBUS
James Feist7136a5a2018-07-19 09:52:05 -070086 {
Patrick Venturec179d402018-10-30 19:51:55 -070087 dbus_configuration::init(modeControlBus);
James Feist7136a5a2018-07-19 09:52:05 -070088 }
Patrick Venture18b13112019-02-14 11:43:59 -080089#else
90 const std::string& path =
91 (configPath.length() > 0) ? configPath : jsonConfigurationPath;
Patrick Venturee6206562018-03-08 15:36:53 -080092
93 /*
94 * When building the sensors, if any of the dbus passive ones aren't on the
95 * bus, it'll fail immediately.
96 */
Patrick Venture18b13112019-02-14 11:43:59 -080097 try
Patrick Venturee6206562018-03-08 15:36:53 -080098 {
Patrick Venture18b13112019-02-14 11:43:59 -080099 auto jsonData = parseValidateJson(path);
100 sensorConfig = buildSensorsFromJson(jsonData);
101 std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData);
Patrick Venturee6206562018-03-08 15:36:53 -0800102 }
Patrick Venture18b13112019-02-14 11:43:59 -0800103 catch (const std::exception& e)
104 {
105 std::cerr << "Failed during building: " << e.what() << "\n";
106 exit(EXIT_FAILURE); /* fatal error. */
107 }
108#endif
Patrick Venture4cb7c052019-02-14 11:11:33 -0800109
110 SensorManager mgmr = buildSensors(sensorConfig);
111 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones =
112 buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
Patrick Venturee6206562018-03-08 15:36:53 -0800113
114 if (0 == zones.size())
115 {
116 std::cerr << "No zones defined, exiting.\n";
117 return rc;
118 }
119
120 /*
121 * All sensors are managed by one manager, but each zone has a pointer to
122 * it.
123 */
124
Patrick Venturec179d402018-10-30 19:51:55 -0700125 auto& hostSensorBus = mgmr.getHostBus();
126 auto& passiveListeningBus = mgmr.getPassiveBus();
Patrick Venturee6206562018-03-08 15:36:53 -0800127
James Feistce6a3f32019-03-12 11:20:16 -0700128 boost::asio::io_context io;
129 sdbusplus::asio::connection passiveBus(io, passiveListeningBus.release());
Patrick Venturee6206562018-03-08 15:36:53 -0800130
James Feistce6a3f32019-03-12 11:20:16 -0700131 sdbusplus::asio::connection hostBus(io, hostSensorBus.release());
132 hostBus.request_name("xyz.openbmc_project.Hwmon.external");
Patrick Venturee6206562018-03-08 15:36:53 -0800133
James Feistce6a3f32019-03-12 11:20:16 -0700134 sdbusplus::asio::connection modeBus(io, modeControlBus.release());
135 modeBus.request_name("xyz.openbmc_project.State.FanCtrl");
Patrick Venturee6206562018-03-08 15:36:53 -0800136
James Feistce6a3f32019-03-12 11:20:16 -0700137 std::list<boost::asio::steady_timer> timers;
Patrick Venturee6206562018-03-08 15:36:53 -0800138
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700139 for (const auto& i : zones)
Patrick Venturee6206562018-03-08 15:36:53 -0800140 {
James Feistce6a3f32019-03-12 11:20:16 -0700141 auto& timer = timers.emplace_back(io);
Patrick Venturee6206562018-03-08 15:36:53 -0800142 std::cerr << "pushing zone" << std::endl;
James Feistce6a3f32019-03-12 11:20:16 -0700143 pidControlLoop(i.second.get(), timer);
Patrick Venturee6206562018-03-08 15:36:53 -0800144 }
145
James Feistce6a3f32019-03-12 11:20:16 -0700146 io.run();
Patrick Venturee6206562018-03-08 15:36:53 -0800147 return rc;
148}