Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 1 | /** |
| 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 Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 17 | #include "config.h" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 18 | |
Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 19 | #include "build/buildjson.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 20 | #include "conf.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 21 | #include "interfaces.hpp" |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 22 | #include "pid/builder.hpp" |
Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 23 | #include "pid/buildjson.hpp" |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 24 | #include "pid/pidloop.hpp" |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 25 | #include "pid/tuning.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 26 | #include "pid/zone.hpp" |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 27 | #include "sensors/builder.hpp" |
Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 28 | #include "sensors/buildjson.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 29 | #include "sensors/manager.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 30 | #include "util.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 31 | |
Patrick Venture | b5cc37c | 2019-03-11 09:11:55 -0700 | [diff] [blame] | 32 | #include <CLI/CLI.hpp> |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 33 | #include <boost/asio/io_context.hpp> |
| 34 | #include <boost/asio/steady_timer.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame^] | 35 | #include <sdbusplus/asio/connection.hpp> |
| 36 | #include <sdbusplus/bus.hpp> |
| 37 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 38 | #include <chrono> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 39 | #include <iostream> |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 40 | #include <list> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 41 | #include <map> |
| 42 | #include <memory> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 43 | #include <thread> |
| 44 | #include <unordered_map> |
Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 45 | #include <utility> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 46 | #include <vector> |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 47 | |
Patrick Venture | 9f04441 | 2018-09-20 19:49:55 -0700 | [diff] [blame] | 48 | #if CONFIGURE_DBUS |
| 49 | #include "dbus/dbusconfiguration.hpp" |
| 50 | #endif |
| 51 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 52 | /* The configuration converted sensor list. */ |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 53 | std::map<std::string, struct conf::SensorConfig> sensorConfig = {}; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 54 | /* The configuration converted PID list. */ |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 55 | std::map<int64_t, conf::PIDConf> zoneConfig = {}; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 56 | /* The configuration converted Zone configuration. */ |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 57 | std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {}; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 58 | |
Patrick Venture | c19f5d4 | 2019-02-14 10:59:47 -0800 | [diff] [blame] | 59 | /** the swampd daemon will check for the existence of this file. */ |
| 60 | constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json"; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 61 | std::string configPath = ""; |
Patrick Venture | c19f5d4 | 2019-02-14 10:59:47 -0800 | [diff] [blame] | 62 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 63 | /* async io context for operation */ |
| 64 | boost::asio::io_context io; |
| 65 | |
| 66 | /* buses for system control */ |
| 67 | static sdbusplus::asio::connection modeControlBus(io); |
| 68 | static sdbusplus::asio::connection |
| 69 | hostBus(io, sdbusplus::bus::new_system().release()); |
| 70 | static sdbusplus::asio::connection |
| 71 | passiveBus(io, sdbusplus::bus::new_system().release()); |
| 72 | |
| 73 | void restartControlLoops() |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 74 | { |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 75 | static SensorManager mgmr; |
| 76 | static std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones; |
| 77 | static std::list<boost::asio::steady_timer> timers; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 78 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 79 | timers.clear(); |
Patrick Venture | 4cb7c05 | 2019-02-14 11:11:33 -0800 | [diff] [blame] | 80 | |
Patrick Venture | 9f04441 | 2018-09-20 19:49:55 -0700 | [diff] [blame] | 81 | #if CONFIGURE_DBUS |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 82 | |
| 83 | static boost::asio::steady_timer reloadTimer(io); |
| 84 | if (!dbus_configuration::init(modeControlBus, reloadTimer)) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 85 | { |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 86 | return; // configuration not ready |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 87 | } |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 88 | |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 89 | #else |
| 90 | const std::string& path = |
| 91 | (configPath.length() > 0) ? configPath : jsonConfigurationPath; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 92 | |
| 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 Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 97 | try |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 98 | { |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 99 | auto jsonData = parseValidateJson(path); |
| 100 | sensorConfig = buildSensorsFromJson(jsonData); |
| 101 | std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 102 | } |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 103 | 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 Venture | 4cb7c05 | 2019-02-14 11:11:33 -0800 | [diff] [blame] | 109 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 110 | mgmr = buildSensors(sensorConfig, passiveBus, hostBus); |
| 111 | zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 112 | |
| 113 | if (0 == zones.size()) |
| 114 | { |
| 115 | std::cerr << "No zones defined, exiting.\n"; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 116 | std::exit(EXIT_FAILURE); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 117 | } |
| 118 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 119 | for (const auto& i : zones) |
| 120 | { |
| 121 | auto& timer = timers.emplace_back(io); |
| 122 | std::cerr << "pushing zone " << i.first << "\n"; |
| 123 | pidControlLoop(i.second.get(), timer); |
| 124 | } |
| 125 | } |
| 126 | |
Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 127 | void tryRestartControlLoops() |
| 128 | { |
| 129 | int count = 0; |
| 130 | for (count = 0; count <= 5; count++) |
| 131 | { |
| 132 | try |
| 133 | { |
| 134 | restartControlLoops(); |
| 135 | break; |
| 136 | } |
| 137 | catch (const std::exception& e) |
| 138 | { |
| 139 | std::cerr << count |
| 140 | << " Failed during restartControlLoops, try again: " |
| 141 | << e.what() << "\n"; |
| 142 | if (count >= 5) |
| 143 | { |
| 144 | throw std::runtime_error(e.what()); |
| 145 | } |
| 146 | } |
| 147 | std::this_thread::sleep_for(std::chrono::seconds(10)); |
| 148 | } |
| 149 | |
| 150 | return; |
| 151 | } |
| 152 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 153 | int main(int argc, char* argv[]) |
| 154 | { |
| 155 | loggingPath = ""; |
| 156 | loggingEnabled = false; |
| 157 | tuningEnabled = false; |
| 158 | |
| 159 | CLI::App app{"OpenBMC Fan Control Daemon"}; |
| 160 | |
| 161 | app.add_option("-c,--conf", configPath, |
| 162 | "Optional parameter to specify configuration at run-time") |
| 163 | ->check(CLI::ExistingFile); |
| 164 | app.add_option("-l,--log", loggingPath, |
| 165 | "Optional parameter to specify logging folder") |
| 166 | ->check(CLI::ExistingDirectory); |
| 167 | app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning"); |
| 168 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 169 | CLI11_PARSE(app, argc, argv); |
| 170 | |
Kun Yi | d469559 | 2019-05-17 10:42:18 -0700 | [diff] [blame] | 171 | loggingEnabled = (!loggingPath.empty()); |
| 172 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 173 | static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; |
| 174 | // Create a manager for the ModeBus because we own it. |
| 175 | sdbusplus::server::manager::manager( |
| 176 | static_cast<sdbusplus::bus::bus&>(modeControlBus), modeRoot); |
| 177 | hostBus.request_name("xyz.openbmc_project.Hwmon.external"); |
| 178 | modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl"); |
| 179 | |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 180 | /* |
| 181 | * All sensors are managed by one manager, but each zone has a pointer to |
| 182 | * it. |
| 183 | */ |
| 184 | |
Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 185 | tryRestartControlLoops(); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 186 | |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 187 | io.run(); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 188 | return 0; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 189 | } |