| Alexander Hansen | 46a755f | 2025-10-27 16:31:08 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2017 Google Inc |
| 3 | |
| Pete O_o | 765a6d8 | 2025-07-23 21:44:14 -0700 | [diff] [blame] | 4 | #include "config.h" |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 5 | |
| Harvey Wu | 3dcfd54 | 2022-10-31 17:11:02 +0800 | [diff] [blame] | 6 | #include "buildjson/buildjson.hpp" |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 7 | #include "conf.hpp" |
| Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 8 | #include "dbus/dbusconfiguration.hpp" |
| James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame] | 9 | #include "failsafeloggers/builder.hpp" |
| Potin Lai | e1fa859 | 2025-08-29 15:27:08 +0800 | [diff] [blame] | 10 | #include "hoststatemonitor.hpp" |
| Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 11 | #include "pid/builder.hpp" |
| Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 12 | #include "pid/buildjson.hpp" |
| James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 13 | #include "pid/pidloop.hpp" |
| Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 14 | #include "pid/tuning.hpp" |
| Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 15 | #include "sensors/builder.hpp" |
| Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 16 | #include "sensors/buildjson.hpp" |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 17 | #include "sensors/manager.hpp" |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 18 | #include "util.hpp" |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 19 | #include "zone_interface.hpp" |
| 20 | |
| 21 | #include <signal.h> |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 22 | |
| Patrick Venture | b5cc37c | 2019-03-11 09:11:55 -0700 | [diff] [blame] | 23 | #include <CLI/CLI.hpp> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 24 | #include <boost/asio/error.hpp> |
| James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 25 | #include <boost/asio/io_context.hpp> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 26 | #include <boost/asio/post.hpp> |
| Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 27 | #include <boost/asio/signal_set.hpp> |
| James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 28 | #include <boost/asio/steady_timer.hpp> |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 29 | #include <sdbusplus/asio/connection.hpp> |
| 30 | #include <sdbusplus/bus.hpp> |
| Bruce Lee | cb4c1a2 | 2021-04-20 16:06:38 +0800 | [diff] [blame] | 31 | #include <sdbusplus/server/manager.hpp> |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 32 | |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 33 | #include <chrono> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 34 | #include <csignal> |
| 35 | #include <cstdint> |
| 36 | #include <cstdlib> |
| 37 | #include <exception> |
| Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 38 | #include <filesystem> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 39 | #include <fstream> |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 40 | #include <iostream> |
| 41 | #include <map> |
| 42 | #include <memory> |
| Patrick Williams | e3c6077 | 2025-04-07 17:53:42 -0400 | [diff] [blame] | 43 | #include <optional> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 44 | #include <stdexcept> |
| 45 | #include <string> |
| 46 | #include <tuple> |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 47 | #include <unordered_map> |
| Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 48 | #include <utility> |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 49 | #include <vector> |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 50 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 51 | namespace pid_control |
| 52 | { |
| 53 | |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 54 | /* The configuration converted sensor list. */ |
| Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 55 | std::map<std::string, conf::SensorConfig> sensorConfig = {}; |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 56 | /* The configuration converted PID list. */ |
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 57 | std::map<int64_t, conf::PIDConf> zoneConfig = {}; |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 58 | /* The configuration converted Zone configuration. */ |
| Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 59 | std::map<int64_t, conf::ZoneConfig> zoneDetailsConfig = {}; |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 60 | |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 61 | namespace state |
| 62 | { |
| 63 | /* Set to true while canceling is in progress */ |
| 64 | static bool isCanceling = false; |
| 65 | /* The zones build from configuration */ |
| 66 | static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones; |
| 67 | /* The timers used by the PID loop */ |
| 68 | static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers; |
| 69 | /* The sensors build from configuration */ |
| Patrick Williams | e3c6077 | 2025-04-07 17:53:42 -0400 | [diff] [blame] | 70 | static std::optional<SensorManager> mgmr; |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 71 | } // namespace state |
| 72 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 73 | } // namespace pid_control |
| 74 | |
| Potin Lai | 79cde00 | 2023-02-16 13:53:10 +0800 | [diff] [blame] | 75 | std::filesystem::path configPath = ""; |
| Patrick Venture | c19f5d4 | 2019-02-14 10:59:47 -0800 | [diff] [blame] | 76 | |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 77 | /* async io context for operation */ |
| 78 | boost::asio::io_context io; |
| Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 79 | /* async signal_set for signal handling */ |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 80 | boost::asio::signal_set signals(io, SIGHUP, SIGTERM); |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 81 | |
| 82 | /* buses for system control */ |
| 83 | static sdbusplus::asio::connection modeControlBus(io); |
| Patrick Williams | 7dea66c | 2025-04-07 14:23:50 -0400 | [diff] [blame] | 84 | static sdbusplus::asio::connection hostBus(io, sdbusplus::bus::new_bus()); |
| 85 | static sdbusplus::asio::connection passiveBus(io, sdbusplus::bus::new_bus()); |
| Potin Lai | e1fa859 | 2025-08-29 15:27:08 +0800 | [diff] [blame] | 86 | static sdbusplus::asio::connection hostMatchBus(io, sdbusplus::bus::new_bus()); |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 87 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 88 | namespace pid_control |
| 89 | { |
| 90 | |
| Potin Lai | 79cde00 | 2023-02-16 13:53:10 +0800 | [diff] [blame] | 91 | std::filesystem::path searchConfigurationPath() |
| 92 | { |
| 93 | static constexpr auto name = "config.json"; |
| 94 | |
| Ed Tanous | d2768c5 | 2025-06-26 11:42:57 -0700 | [diff] [blame] | 95 | for (const auto& pathSeg : {std::filesystem::current_path(), |
| 96 | std::filesystem::path{"/var/lib/swampd"}, |
| 97 | std::filesystem::path{"/usr/share/swampd"}}) |
| Potin Lai | 79cde00 | 2023-02-16 13:53:10 +0800 | [diff] [blame] | 98 | { |
| 99 | auto file = pathSeg / name; |
| 100 | if (std::filesystem::exists(file)) |
| 101 | { |
| 102 | return file; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return name; |
| 107 | } |
| 108 | |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 109 | void stopControlLoops() |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 110 | { |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 111 | for (const auto& timer : state::timers) |
| Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 112 | { |
| 113 | timer->cancel(); |
| 114 | } |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 115 | state::isCanceling = true; |
| 116 | state::timers.clear(); |
| Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 117 | |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 118 | if (state::zones.size() > 0 && state::zones.begin()->second.use_count() > 1) |
| Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 119 | { |
| 120 | throw std::runtime_error("wait for count back to 1"); |
| 121 | } |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 122 | |
| 123 | state::zones.clear(); |
| 124 | state::isCanceling = false; |
| 125 | } |
| 126 | |
| 127 | void restartControlLoops() |
| 128 | { |
| 129 | stopControlLoops(); |
| Patrick Venture | 4cb7c05 | 2019-02-14 11:11:33 -0800 | [diff] [blame] | 130 | |
| Potin Lai | 79cde00 | 2023-02-16 13:53:10 +0800 | [diff] [blame] | 131 | const std::filesystem::path path = |
| 132 | (!configPath.empty()) ? configPath : searchConfigurationPath(); |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 133 | |
| Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 134 | if (std::filesystem::exists(path)) |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 135 | { |
| Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 136 | /* |
| 137 | * When building the sensors, if any of the dbus passive ones aren't on |
| 138 | * the bus, it'll fail immediately. |
| 139 | */ |
| 140 | try |
| 141 | { |
| 142 | auto jsonData = parseValidateJson(path); |
| 143 | sensorConfig = buildSensorsFromJson(jsonData); |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 144 | std::tie(zoneConfig, zoneDetailsConfig) = |
| 145 | buildPIDsFromJson(jsonData); |
| Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 146 | } |
| 147 | catch (const std::exception& e) |
| 148 | { |
| 149 | std::cerr << "Failed during building: " << e.what() << "\n"; |
| 150 | exit(EXIT_FAILURE); /* fatal error. */ |
| 151 | } |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 152 | } |
| Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 153 | else |
| Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 154 | { |
| Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 155 | static boost::asio::steady_timer reloadTimer(io); |
| Patrick Venture | 7382318 | 2020-10-08 15:12:51 -0700 | [diff] [blame] | 156 | if (!dbus_configuration::init(modeControlBus, reloadTimer, sensorConfig, |
| 157 | zoneConfig, zoneDetailsConfig)) |
| Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 158 | { |
| 159 | return; // configuration not ready |
| 160 | } |
| Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 161 | } |
| Patrick Venture | 4cb7c05 | 2019-02-14 11:11:33 -0800 | [diff] [blame] | 162 | |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 163 | state::mgmr = buildSensors(sensorConfig, passiveBus, hostBus); |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 164 | state::zones = |
| Patrick Williams | e3c6077 | 2025-04-07 17:53:42 -0400 | [diff] [blame] | 165 | buildZones(zoneConfig, zoneDetailsConfig, *state::mgmr, modeControlBus); |
| James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame] | 166 | // Set `logMaxCountPerSecond` to 20 will limit the number of logs output per |
| 167 | // second in each zone. Using 20 here would limit the output rate to be no |
| 168 | // larger than 100 per sec for most platforms as the number of zones are |
| 169 | // usually <=3. This will effectively avoid resource exhaustion. |
| 170 | buildFailsafeLoggers(state::zones, /* logMaxCountPerSecond = */ 20); |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 171 | |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 172 | if (0 == state::zones.size()) |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 173 | { |
| 174 | std::cerr << "No zones defined, exiting.\n"; |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 175 | std::exit(EXIT_FAILURE); |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 178 | for (const auto& i : state::zones) |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 179 | { |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 180 | std::shared_ptr<boost::asio::steady_timer> timer = |
| 181 | state::timers.emplace_back( |
| 182 | std::make_shared<boost::asio::steady_timer>(io)); |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 183 | std::cerr << "pushing zone " << i.first << "\n"; |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 184 | pidControlLoop(i.second, timer, &state::isCanceling); |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 188 | void tryRestartControlLoops(bool first) |
| Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 189 | { |
| Hao Jiang | d11a732 | 2021-03-24 11:25:57 -0700 | [diff] [blame] | 190 | static const auto delayTime = std::chrono::seconds(10); |
| Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 191 | static boost::asio::steady_timer timer(io); |
| Hao Jiang | d11a732 | 2021-03-24 11:25:57 -0700 | [diff] [blame] | 192 | |
| 193 | auto restartLbd = [](const boost::system::error_code& error) { |
| Hao Jiang | 232ffe4 | 2021-02-21 20:11:49 -0800 | [diff] [blame] | 194 | if (error == boost::asio::error::operation_aborted) |
| 195 | { |
| 196 | return; |
| 197 | } |
| Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 198 | |
| Hao Jiang | 232ffe4 | 2021-02-21 20:11:49 -0800 | [diff] [blame] | 199 | // retry when restartControlLoops() has some failure. |
| 200 | try |
| 201 | { |
| 202 | restartControlLoops(); |
| Hao Jiang | 232ffe4 | 2021-02-21 20:11:49 -0800 | [diff] [blame] | 203 | } |
| 204 | catch (const std::exception& e) |
| 205 | { |
| Zev Weiss | 5293ec2 | 2023-03-06 18:58:36 -0800 | [diff] [blame] | 206 | std::cerr << "Failed during restartControlLoops, try again: " |
| Hao Jiang | 232ffe4 | 2021-02-21 20:11:49 -0800 | [diff] [blame] | 207 | << e.what() << "\n"; |
| 208 | tryRestartControlLoops(false); |
| 209 | } |
| Hao Jiang | d11a732 | 2021-03-24 11:25:57 -0700 | [diff] [blame] | 210 | }; |
| Zev Weiss | 5293ec2 | 2023-03-06 18:58:36 -0800 | [diff] [blame] | 211 | |
| Hao Jiang | d11a732 | 2021-03-24 11:25:57 -0700 | [diff] [blame] | 212 | // first time of trying to restart the control loop without a delay |
| 213 | if (first) |
| 214 | { |
| Ed Tanous | d2768c5 | 2025-06-26 11:42:57 -0700 | [diff] [blame] | 215 | boost::asio::post(io, [restartLbd] { |
| 216 | restartLbd(boost::system::error_code()); |
| 217 | }); |
| Hao Jiang | d11a732 | 2021-03-24 11:25:57 -0700 | [diff] [blame] | 218 | } |
| 219 | // re-try control loop, set up a delay. |
| 220 | else |
| 221 | { |
| 222 | timer.expires_after(delayTime); |
| 223 | timer.async_wait(restartLbd); |
| 224 | } |
| Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 225 | |
| 226 | return; |
| 227 | } |
| 228 | |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 229 | void tryTerminateControlLoops(bool first) |
| 230 | { |
| 231 | static const auto delayTime = std::chrono::milliseconds(50); |
| 232 | static boost::asio::steady_timer timer(io); |
| 233 | |
| 234 | auto stopLbd = [](const boost::system::error_code& error) { |
| 235 | if (error == boost::asio::error::operation_aborted) |
| 236 | { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | // retry when stopControlLoops() has some failure. |
| 241 | try |
| 242 | { |
| 243 | stopControlLoops(); |
| 244 | } |
| 245 | catch (const std::exception& e) |
| 246 | { |
| 247 | std::cerr << "Failed during stopControlLoops, try again: " |
| 248 | << e.what() << "\n"; |
| 249 | tryTerminateControlLoops(false); |
| 250 | return; |
| 251 | } |
| 252 | io.stop(); |
| 253 | }; |
| 254 | |
| 255 | // first time of trying to stop the control loop without a delay |
| 256 | if (first) |
| 257 | { |
| Ed Tanous | d2768c5 | 2025-06-26 11:42:57 -0700 | [diff] [blame] | 258 | boost::asio::post(io, |
| 259 | [stopLbd] { stopLbd(boost::system::error_code()); }); |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 260 | } |
| 261 | // re-try control loop, set up a delay. |
| 262 | else |
| 263 | { |
| 264 | timer.expires_after(delayTime); |
| 265 | timer.async_wait(stopLbd); |
| 266 | } |
| 267 | |
| 268 | return; |
| 269 | } |
| 270 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 271 | } // namespace pid_control |
| 272 | |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 273 | void signalHandler(const boost::system::error_code& error, int signal_number) |
| Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 274 | { |
| 275 | static boost::asio::steady_timer timer(io); |
| 276 | |
| 277 | if (error) |
| 278 | { |
| 279 | std::cout << "Signal " << signal_number |
| 280 | << " handler error: " << error.message() << "\n"; |
| 281 | return; |
| 282 | } |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 283 | if (signal_number == SIGTERM) |
| 284 | { |
| 285 | pid_control::tryTerminateControlLoops(true); |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | timer.expires_after(std::chrono::seconds(1)); |
| 290 | timer.async_wait([](const boost::system::error_code ec) { |
| 291 | if (ec) |
| 292 | { |
| 293 | std::cout << "Signal timer error: " << ec.message() << "\n"; |
| 294 | return; |
| 295 | } |
| Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 296 | |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 297 | std::cout << "reloading configuration\n"; |
| 298 | pid_control::tryRestartControlLoops(); |
| 299 | }); |
| 300 | } |
| Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 301 | |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 302 | signals.async_wait(signalHandler); |
| Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 303 | } |
| 304 | |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 305 | int main(int argc, char* argv[]) |
| 306 | { |
| 307 | loggingPath = ""; |
| 308 | loggingEnabled = false; |
| 309 | tuningEnabled = false; |
| Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 310 | debugEnabled = false; |
| Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 311 | coreLoggingEnabled = false; |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 312 | |
| 313 | CLI::App app{"OpenBMC Fan Control Daemon"}; |
| 314 | |
| 315 | app.add_option("-c,--conf", configPath, |
| 316 | "Optional parameter to specify configuration at run-time") |
| 317 | ->check(CLI::ExistingFile); |
| 318 | app.add_option("-l,--log", loggingPath, |
| 319 | "Optional parameter to specify logging folder") |
| 320 | ->check(CLI::ExistingDirectory); |
| 321 | app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning"); |
| Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 322 | app.add_flag("-d,--debug", debugEnabled, "Enable or disable debug mode"); |
| Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 323 | app.add_flag("-g,--corelogging", coreLoggingEnabled, |
| 324 | "Enable or disable logging of core PID loop computations"); |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 325 | |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 326 | CLI11_PARSE(app, argc, argv); |
| 327 | |
| Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 328 | static constexpr auto loggingEnablePath = "/etc/thermal.d/logging"; |
| 329 | static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning"; |
| Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 330 | static constexpr auto debugEnablePath = "/etc/thermal.d/debugging"; |
| Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 331 | static constexpr auto coreLoggingEnablePath = "/etc/thermal.d/corelogging"; |
| Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 332 | |
| Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 333 | // Set up default logging path, preferring command line if it was given |
| 334 | std::string defLoggingPath(loggingPath); |
| 335 | if (defLoggingPath.empty()) |
| 336 | { |
| 337 | defLoggingPath = std::filesystem::temp_directory_path(); |
| 338 | } |
| 339 | else |
| 340 | { |
| 341 | // Enable logging, if user explicitly gave path on command line |
| 342 | loggingEnabled = true; |
| 343 | } |
| 344 | |
| Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 345 | // If this file exists, enable logging at runtime |
| 346 | std::ifstream fsLogging(loggingEnablePath); |
| 347 | if (fsLogging) |
| 348 | { |
| Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 349 | // Allow logging path to be changed by file content |
| 350 | std::string altPath; |
| 351 | std::getline(fsLogging, altPath); |
| Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 352 | fsLogging.close(); |
| 353 | |
| Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 354 | if (std::filesystem::exists(altPath)) |
| Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 355 | { |
| Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 356 | loggingPath = altPath; |
| Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 357 | } |
| Jinliang Wang | cdc8dca | 2023-04-20 14:36:22 -0700 | [diff] [blame] | 358 | else |
| 359 | { |
| 360 | loggingPath = defLoggingPath; |
| 361 | } |
| Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 362 | |
| Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 363 | loggingEnabled = true; |
| Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 364 | } |
| Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 365 | if (loggingEnabled) |
| 366 | { |
| Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 367 | std::cerr << "Logging enabled: " << loggingPath << "\n"; |
| 368 | } |
| 369 | |
| 370 | // If this file exists, enable tuning at runtime |
| 371 | if (std::filesystem::exists(tuningEnablePath)) |
| 372 | { |
| 373 | tuningEnabled = true; |
| Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 374 | } |
| Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 375 | if (tuningEnabled) |
| 376 | { |
| Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 377 | std::cerr << "Tuning enabled\n"; |
| 378 | } |
| Kun Yi | d469559 | 2019-05-17 10:42:18 -0700 | [diff] [blame] | 379 | |
| Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 380 | // If this file exists, enable debug mode at runtime |
| 381 | if (std::filesystem::exists(debugEnablePath)) |
| 382 | { |
| 383 | debugEnabled = true; |
| 384 | } |
| 385 | |
| 386 | if (debugEnabled) |
| 387 | { |
| 388 | std::cerr << "Debug mode enabled\n"; |
| 389 | } |
| 390 | |
| Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 391 | // If this file exists, enable core logging at runtime |
| 392 | if (std::filesystem::exists(coreLoggingEnablePath)) |
| 393 | { |
| 394 | coreLoggingEnabled = true; |
| 395 | } |
| 396 | if (coreLoggingEnabled) |
| 397 | { |
| 398 | std::cerr << "Core logging enabled\n"; |
| 399 | } |
| 400 | |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 401 | static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; |
| 402 | // Create a manager for the ModeBus because we own it. |
| Ed Tanous | d2768c5 | 2025-06-26 11:42:57 -0700 | [diff] [blame] | 403 | sdbusplus::server::manager_t manager( |
| 404 | static_cast<sdbusplus::bus_t&>(modeControlBus), modeRoot); |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 405 | hostBus.request_name("xyz.openbmc_project.Hwmon.external"); |
| 406 | modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl"); |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 407 | sdbusplus::server::manager_t objManager(modeControlBus, modeRoot); |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 408 | |
| Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 409 | // Enable SIGHUP handling to reload JSON config |
| Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 410 | signals.async_wait(signalHandler); |
| Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 411 | |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 412 | /* |
| 413 | * All sensors are managed by one manager, but each zone has a pointer to |
| 414 | * it. |
| 415 | */ |
| 416 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 417 | pid_control::tryRestartControlLoops(); |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 418 | |
| Potin Lai | e1fa859 | 2025-08-29 15:27:08 +0800 | [diff] [blame] | 419 | /* setup host state monitor */ |
| 420 | auto& monitor = HostStateMonitor::getInstance(hostMatchBus); |
| 421 | monitor.startMonitoring(); |
| 422 | |
| James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 423 | io.run(); |
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 424 | return 0; |
| Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 425 | } |