blob: 8c99154a562caa4e4bdccbe39dba86e34dc974f9 [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 Venturede79ee02019-05-08 14:50:00 -070065 loggingPath = "";
66 loggingEnabled = false;
67 tuningEnabled = false;
Patrick Venturee6206562018-03-08 15:36:53 -080068
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070069 CLI::App app{"OpenBMC Fan Control Daemon"};
Patrick Venturee6206562018-03-08 15:36:53 -080070
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070071 app.add_option("-c,--conf", configPath,
72 "Optional parameter to specify configuration at run-time")
73 ->check(CLI::ExistingFile);
Patrick Venturede79ee02019-05-08 14:50:00 -070074 app.add_option("-l,--log", loggingPath,
Patrick Venture89002db2019-05-08 15:02:55 -070075 "Optional parameter to specify logging folder")
76 ->check(CLI::ExistingDirectory);
Patrick Venturede79ee02019-05-08 14:50:00 -070077 app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
78
79 loggingEnabled = (!loggingPath.empty());
Patrick Venturee6206562018-03-08 15:36:53 -080080
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070081 CLI11_PARSE(app, argc, argv);
Patrick Venturee6206562018-03-08 15:36:53 -080082
James Feist9fa90c12019-01-11 15:35:22 -080083 auto modeControlBus = sdbusplus::bus::new_system();
Patrick Venture4cb7c052019-02-14 11:11:33 -080084 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
85 // Create a manager for the ModeBus because we own it.
86 sdbusplus::server::manager::manager(modeControlBus, modeRoot);
87
Patrick Venture9f044412018-09-20 19:49:55 -070088#if CONFIGURE_DBUS
James Feist7136a5a2018-07-19 09:52:05 -070089 {
Patrick Venturec179d402018-10-30 19:51:55 -070090 dbus_configuration::init(modeControlBus);
James Feist7136a5a2018-07-19 09:52:05 -070091 }
Patrick Venture18b13112019-02-14 11:43:59 -080092#else
93 const std::string& path =
94 (configPath.length() > 0) ? configPath : jsonConfigurationPath;
Patrick Venturee6206562018-03-08 15:36:53 -080095
96 /*
97 * When building the sensors, if any of the dbus passive ones aren't on the
98 * bus, it'll fail immediately.
99 */
Patrick Venture18b13112019-02-14 11:43:59 -0800100 try
Patrick Venturee6206562018-03-08 15:36:53 -0800101 {
Patrick Venture18b13112019-02-14 11:43:59 -0800102 auto jsonData = parseValidateJson(path);
103 sensorConfig = buildSensorsFromJson(jsonData);
104 std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData);
Patrick Venturee6206562018-03-08 15:36:53 -0800105 }
Patrick Venture18b13112019-02-14 11:43:59 -0800106 catch (const std::exception& e)
107 {
108 std::cerr << "Failed during building: " << e.what() << "\n";
109 exit(EXIT_FAILURE); /* fatal error. */
110 }
111#endif
Patrick Venture4cb7c052019-02-14 11:11:33 -0800112
113 SensorManager mgmr = buildSensors(sensorConfig);
114 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones =
115 buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
Patrick Venturee6206562018-03-08 15:36:53 -0800116
117 if (0 == zones.size())
118 {
119 std::cerr << "No zones defined, exiting.\n";
120 return rc;
121 }
122
123 /*
124 * All sensors are managed by one manager, but each zone has a pointer to
125 * it.
126 */
127
Patrick Venturec179d402018-10-30 19:51:55 -0700128 auto& hostSensorBus = mgmr.getHostBus();
129 auto& passiveListeningBus = mgmr.getPassiveBus();
Patrick Venturee6206562018-03-08 15:36:53 -0800130
James Feistce6a3f32019-03-12 11:20:16 -0700131 boost::asio::io_context io;
132 sdbusplus::asio::connection passiveBus(io, passiveListeningBus.release());
Patrick Venturee6206562018-03-08 15:36:53 -0800133
James Feistce6a3f32019-03-12 11:20:16 -0700134 sdbusplus::asio::connection hostBus(io, hostSensorBus.release());
135 hostBus.request_name("xyz.openbmc_project.Hwmon.external");
Patrick Venturee6206562018-03-08 15:36:53 -0800136
James Feistce6a3f32019-03-12 11:20:16 -0700137 sdbusplus::asio::connection modeBus(io, modeControlBus.release());
138 modeBus.request_name("xyz.openbmc_project.State.FanCtrl");
Patrick Venturee6206562018-03-08 15:36:53 -0800139
James Feistce6a3f32019-03-12 11:20:16 -0700140 std::list<boost::asio::steady_timer> timers;
Patrick Venturee6206562018-03-08 15:36:53 -0800141
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700142 for (const auto& i : zones)
Patrick Venturee6206562018-03-08 15:36:53 -0800143 {
James Feistce6a3f32019-03-12 11:20:16 -0700144 auto& timer = timers.emplace_back(io);
Patrick Venturee6206562018-03-08 15:36:53 -0800145 std::cerr << "pushing zone" << std::endl;
James Feistce6a3f32019-03-12 11:20:16 -0700146 pidControlLoop(i.second.get(), timer);
Patrick Venturee6206562018-03-08 15:36:53 -0800147 }
148
James Feistce6a3f32019-03-12 11:20:16 -0700149 io.run();
Patrick Venturee6206562018-03-08 15:36:53 -0800150 return rc;
151}