blob: 3726f506d1aa63279c6770acff6dfcc3979259f2 [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,
75 "Optional parameter to specify logging path");
76 app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
77
78 loggingEnabled = (!loggingPath.empty());
Patrick Venturee6206562018-03-08 15:36:53 -080079
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070080 CLI11_PARSE(app, argc, argv);
Patrick Venturee6206562018-03-08 15:36:53 -080081
James Feist9fa90c12019-01-11 15:35:22 -080082 auto modeControlBus = sdbusplus::bus::new_system();
Patrick Venture4cb7c052019-02-14 11:11:33 -080083 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
84 // Create a manager for the ModeBus because we own it.
85 sdbusplus::server::manager::manager(modeControlBus, modeRoot);
86
Patrick Venture9f044412018-09-20 19:49:55 -070087#if CONFIGURE_DBUS
James Feist7136a5a2018-07-19 09:52:05 -070088 {
Patrick Venturec179d402018-10-30 19:51:55 -070089 dbus_configuration::init(modeControlBus);
James Feist7136a5a2018-07-19 09:52:05 -070090 }
Patrick Venture18b13112019-02-14 11:43:59 -080091#else
92 const std::string& path =
93 (configPath.length() > 0) ? configPath : jsonConfigurationPath;
Patrick Venturee6206562018-03-08 15:36:53 -080094
95 /*
96 * When building the sensors, if any of the dbus passive ones aren't on the
97 * bus, it'll fail immediately.
98 */
Patrick Venture18b13112019-02-14 11:43:59 -080099 try
Patrick Venturee6206562018-03-08 15:36:53 -0800100 {
Patrick Venture18b13112019-02-14 11:43:59 -0800101 auto jsonData = parseValidateJson(path);
102 sensorConfig = buildSensorsFromJson(jsonData);
103 std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData);
Patrick Venturee6206562018-03-08 15:36:53 -0800104 }
Patrick Venture18b13112019-02-14 11:43:59 -0800105 catch (const std::exception& e)
106 {
107 std::cerr << "Failed during building: " << e.what() << "\n";
108 exit(EXIT_FAILURE); /* fatal error. */
109 }
110#endif
Patrick Venture4cb7c052019-02-14 11:11:33 -0800111
112 SensorManager mgmr = buildSensors(sensorConfig);
113 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones =
114 buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
Patrick Venturee6206562018-03-08 15:36:53 -0800115
116 if (0 == zones.size())
117 {
118 std::cerr << "No zones defined, exiting.\n";
119 return rc;
120 }
121
122 /*
123 * All sensors are managed by one manager, but each zone has a pointer to
124 * it.
125 */
126
Patrick Venturec179d402018-10-30 19:51:55 -0700127 auto& hostSensorBus = mgmr.getHostBus();
128 auto& passiveListeningBus = mgmr.getPassiveBus();
Patrick Venturee6206562018-03-08 15:36:53 -0800129
James Feistce6a3f32019-03-12 11:20:16 -0700130 boost::asio::io_context io;
131 sdbusplus::asio::connection passiveBus(io, passiveListeningBus.release());
Patrick Venturee6206562018-03-08 15:36:53 -0800132
James Feistce6a3f32019-03-12 11:20:16 -0700133 sdbusplus::asio::connection hostBus(io, hostSensorBus.release());
134 hostBus.request_name("xyz.openbmc_project.Hwmon.external");
Patrick Venturee6206562018-03-08 15:36:53 -0800135
James Feistce6a3f32019-03-12 11:20:16 -0700136 sdbusplus::asio::connection modeBus(io, modeControlBus.release());
137 modeBus.request_name("xyz.openbmc_project.State.FanCtrl");
Patrick Venturee6206562018-03-08 15:36:53 -0800138
James Feistce6a3f32019-03-12 11:20:16 -0700139 std::list<boost::asio::steady_timer> timers;
Patrick Venturee6206562018-03-08 15:36:53 -0800140
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700141 for (const auto& i : zones)
Patrick Venturee6206562018-03-08 15:36:53 -0800142 {
James Feistce6a3f32019-03-12 11:20:16 -0700143 auto& timer = timers.emplace_back(io);
Patrick Venturee6206562018-03-08 15:36:53 -0800144 std::cerr << "pushing zone" << std::endl;
James Feistce6a3f32019-03-12 11:20:16 -0700145 pidControlLoop(i.second.get(), timer);
Patrick Venturee6206562018-03-08 15:36:53 -0800146 }
147
James Feistce6a3f32019-03-12 11:20:16 -0700148 io.run();
Patrick Venturee6206562018-03-08 15:36:53 -0800149 return rc;
150}