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