blob: 6dca3091cb45b2c8a3f0d33d35be39b754afaca9 [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 "
74 "enable tuning")
75 ->check(CLI::ExistingFile);
Patrick Venturee6206562018-03-08 15:36:53 -080076
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070077 CLI11_PARSE(app, argc, argv);
Patrick Venturee6206562018-03-08 15:36:53 -080078
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070079 tuningLoggingEnabled = (tuningLoggingPath.length() > 0);
Patrick Venturee6206562018-03-08 15:36:53 -080080
James Feist9fa90c12019-01-11 15:35:22 -080081 auto modeControlBus = sdbusplus::bus::new_system();
Patrick Venture4cb7c052019-02-14 11:11:33 -080082 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
83 // Create a manager for the ModeBus because we own it.
84 sdbusplus::server::manager::manager(modeControlBus, modeRoot);
85
Patrick Venture9f044412018-09-20 19:49:55 -070086#if CONFIGURE_DBUS
James Feist7136a5a2018-07-19 09:52:05 -070087 {
Patrick Venturec179d402018-10-30 19:51:55 -070088 dbus_configuration::init(modeControlBus);
James Feist7136a5a2018-07-19 09:52:05 -070089 }
Patrick Venture18b13112019-02-14 11:43:59 -080090#else
91 const std::string& path =
92 (configPath.length() > 0) ? configPath : jsonConfigurationPath;
Patrick Venturee6206562018-03-08 15:36:53 -080093
94 /*
95 * When building the sensors, if any of the dbus passive ones aren't on the
96 * bus, it'll fail immediately.
97 */
Patrick Venture18b13112019-02-14 11:43:59 -080098 try
Patrick Venturee6206562018-03-08 15:36:53 -080099 {
Patrick Venture18b13112019-02-14 11:43:59 -0800100 auto jsonData = parseValidateJson(path);
101 sensorConfig = buildSensorsFromJson(jsonData);
102 std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData);
Patrick Venturee6206562018-03-08 15:36:53 -0800103 }
Patrick Venture18b13112019-02-14 11:43:59 -0800104 catch (const std::exception& e)
105 {
106 std::cerr << "Failed during building: " << e.what() << "\n";
107 exit(EXIT_FAILURE); /* fatal error. */
108 }
109#endif
Patrick Venture4cb7c052019-02-14 11:11:33 -0800110
111 SensorManager mgmr = buildSensors(sensorConfig);
112 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones =
113 buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
Patrick Venturee6206562018-03-08 15:36:53 -0800114
115 if (0 == zones.size())
116 {
117 std::cerr << "No zones defined, exiting.\n";
118 return rc;
119 }
120
121 /*
122 * All sensors are managed by one manager, but each zone has a pointer to
123 * it.
124 */
125
Patrick Venturec179d402018-10-30 19:51:55 -0700126 auto& hostSensorBus = mgmr.getHostBus();
127 auto& passiveListeningBus = mgmr.getPassiveBus();
Patrick Venturee6206562018-03-08 15:36:53 -0800128
James Feistce6a3f32019-03-12 11:20:16 -0700129 boost::asio::io_context io;
130 sdbusplus::asio::connection passiveBus(io, passiveListeningBus.release());
Patrick Venturee6206562018-03-08 15:36:53 -0800131
James Feistce6a3f32019-03-12 11:20:16 -0700132 sdbusplus::asio::connection hostBus(io, hostSensorBus.release());
133 hostBus.request_name("xyz.openbmc_project.Hwmon.external");
Patrick Venturee6206562018-03-08 15:36:53 -0800134
James Feistce6a3f32019-03-12 11:20:16 -0700135 sdbusplus::asio::connection modeBus(io, modeControlBus.release());
136 modeBus.request_name("xyz.openbmc_project.State.FanCtrl");
Patrick Venturee6206562018-03-08 15:36:53 -0800137
James Feistce6a3f32019-03-12 11:20:16 -0700138 std::list<boost::asio::steady_timer> timers;
Patrick Venturee6206562018-03-08 15:36:53 -0800139
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700140 for (const auto& i : zones)
Patrick Venturee6206562018-03-08 15:36:53 -0800141 {
James Feistce6a3f32019-03-12 11:20:16 -0700142 auto& timer = timers.emplace_back(io);
Patrick Venturee6206562018-03-08 15:36:53 -0800143 std::cerr << "pushing zone" << std::endl;
James Feistce6a3f32019-03-12 11:20:16 -0700144 pidControlLoop(i.second.get(), timer);
Patrick Venturee6206562018-03-08 15:36:53 -0800145 }
146
James Feistce6a3f32019-03-12 11:20:16 -0700147 io.run();
Patrick Venturee6206562018-03-08 15:36:53 -0800148 return rc;
149}