blob: 9286dc7bdf8b4fc6b72e0891475322f4f6138a25 [file] [log] [blame]
Matt Spinlere73446e2017-04-10 13:55:52 -05001/**
Mike Cappsb2e9a4f2022-06-13 10:15:42 -04002 * Copyright © 2022 IBM Corporation
Matt Spinlere73446e2017-04-10 13:55:52 -05003 *
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 */
Matthew Barthf8ae7a52021-03-05 10:23:43 -060016#include "config.h"
17
18#ifndef CONTROL_USE_JSON
Matt Spinlere10416e2017-04-10 14:15:53 -050019#include "manager.hpp"
George Liu854abad2023-08-15 17:33:12 +080020
21#include <CLI/CLI.hpp>
Matthew Barthf8ae7a52021-03-05 10:23:43 -060022#else
Mike Cappsa0819562022-06-13 10:17:10 -040023#include "../utils/flight_recorder.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060024#include "json/manager.hpp"
25#endif
Mike Cappsb2e9a4f2022-06-13 10:15:42 -040026
Mike Cappsbf8e56f2022-06-29 14:23:07 -040027#include "dbus_paths.hpp"
Matthew Barthf8ae7a52021-03-05 10:23:43 -060028#include "sdbusplus.hpp"
Matthew Barth2feab082021-06-29 14:50:14 -050029#include "sdeventplus.hpp"
Matt Spinlere73446e2017-04-10 13:55:52 -050030
Matthew Barth3e1bb272020-05-26 11:09:21 -050031#include <phosphor-logging/log.hpp>
32#include <sdbusplus/bus.hpp>
33#include <sdeventplus/event.hpp>
Matthew Barthe91ac862021-05-25 16:22:17 -050034#include <sdeventplus/source/signal.hpp>
35#include <stdplus/signal.hpp>
Matthew Barth3e1bb272020-05-26 11:09:21 -050036
Matt Spinler7787def2021-10-14 16:33:16 -050037#include <fstream>
38
Matt Spinleree7f6422017-05-09 11:03:14 -050039using namespace phosphor::fan::control;
Matthew Barth8600d9a2017-06-23 14:38:05 -050040using namespace phosphor::logging;
Matt Spinleree7f6422017-05-09 11:03:14 -050041
Matt Spinler7787def2021-10-14 16:33:16 -050042#ifdef CONTROL_USE_JSON
43void dumpFlightRecorder()
44{
45 nlohmann::json data;
46 phosphor::fan::control::json::FlightRecorder::instance().dump(data);
47 std::ofstream file{json::Manager::dumpFile};
48 file << std::setw(4) << data;
49}
50#endif
51
Mike Cappsb2e9a4f2022-06-13 10:15:42 -040052int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
Matt Spinlere73446e2017-04-10 13:55:52 -050053{
Matthew Barth2feab082021-06-29 14:50:14 -050054 auto event = phosphor::fan::util::SDEventPlus::getEvent();
Matt Spinlere73446e2017-04-10 13:55:52 -050055
Matthew Barth06764942021-03-04 09:28:40 -060056#ifndef CONTROL_USE_JSON
George Liu854abad2023-08-15 17:33:12 +080057 CLI::App app{"Phosphor Fan Control"};
58
59 bool init = false;
60 bool control = false;
61 app.add_flag("-i,--init", init, "Sets fans to full speed, delays, exits");
62 app.add_flag("-c,--control", control, "Start fan control algorithm");
63 app.require_option();
64
65 try
Matt Spinleree7f6422017-05-09 11:03:14 -050066 {
George Liu854abad2023-08-15 17:33:12 +080067 app.parse(argc, argv);
68 }
69 catch (const CLI::Error& e)
70 {
71 return app.exit(e);
Matt Spinleree7f6422017-05-09 11:03:14 -050072 }
73
Matthew Barth14184132017-05-19 14:37:30 -050074 Mode mode;
George Liu854abad2023-08-15 17:33:12 +080075 if (init)
Matt Spinleree7f6422017-05-09 11:03:14 -050076 {
Matthew Barth14184132017-05-19 14:37:30 -050077 mode = Mode::init;
Matt Spinleree7f6422017-05-09 11:03:14 -050078 }
George Liu854abad2023-08-15 17:33:12 +080079 else if (control)
Matt Spinleree7f6422017-05-09 11:03:14 -050080 {
Matthew Barth14184132017-05-19 14:37:30 -050081 mode = Mode::control;
Matt Spinleree7f6422017-05-09 11:03:14 -050082 }
Matthew Barth06764942021-03-04 09:28:40 -060083#endif
Matt Spinleree7f6422017-05-09 11:03:14 -050084
Matthew Barth3e1bb272020-05-26 11:09:21 -050085 // Attach the event object to the bus object so we can
86 // handle both sd_events (for the timers) and dbus signals.
Matthew Barth9403a212021-05-17 09:31:50 -050087 phosphor::fan::util::SDBusPlus::getBus().attach_event(
88 event.get(), SD_EVENT_PRIORITY_NORMAL);
Matthew Barth8600d9a2017-06-23 14:38:05 -050089
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050090 try
91 {
Matthew Barth06764942021-03-04 09:28:40 -060092#ifdef CONTROL_USE_JSON
Patrick Williamsdfddd642024-08-16 15:21:51 -040093 phosphor::fan::control::json::FlightRecorder::instance().log(
94 "main", "Startup");
Matthew Barth9403a212021-05-17 09:31:50 -050095 json::Manager manager(event);
Matthew Barthe91ac862021-05-25 16:22:17 -050096
Matthew Barth3770a1d2021-06-10 15:09:37 -050097 // Handle loading fan control's config file(s)
98 phosphor::fan::JsonConfig config(
99 std::bind(&json::Manager::load, &manager));
100
Matthew Barthe91ac862021-05-25 16:22:17 -0500101 // Enable SIGHUP handling to reload JSON configs
102 stdplus::signal::block(SIGHUP);
103 sdeventplus::source::Signal signal(
104 event, SIGHUP,
105 std::bind(&json::Manager::sighupHandler, &manager,
106 std::placeholders::_1, std::placeholders::_2));
107
Matt Spinler2fc0a352021-10-04 15:10:57 -0500108 // Enable SIGUSR1 handling to dump the flight recorder
109 stdplus::signal::block(SIGUSR1);
110 sdeventplus::source::Signal sigUsr1(
111 event, SIGUSR1,
Matt Spinler27f5f4e2022-09-01 14:57:39 -0500112 std::bind(&json::Manager::dumpDebugData, &manager,
Matt Spinler2fc0a352021-10-04 15:10:57 -0500113 std::placeholders::_1, std::placeholders::_2));
114
Matthew Barthe91ac862021-05-25 16:22:17 -0500115 phosphor::fan::util::SDBusPlus::getBus().request_name(CONTROL_BUSNAME);
Matthew Barth06764942021-03-04 09:28:40 -0600116#else
Matthew Barth9403a212021-05-17 09:31:50 -0500117 Manager manager(phosphor::fan::util::SDBusPlus::getBus(), event, mode);
Matt Spinleree7f6422017-05-09 11:03:14 -0500118
Matthew Barth3e1bb272020-05-26 11:09:21 -0500119 // Init mode will just set fans to max and delay
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500120 if (mode == Mode::init)
Matthew Barth8600d9a2017-06-23 14:38:05 -0500121 {
Matthew Barth06764942021-03-04 09:28:40 -0600122 manager.doInit(event);
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500123 return 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500124 }
Matthew Barth06764942021-03-04 09:28:40 -0600125#endif
William A. Kennington III1cfc2f12018-10-19 17:29:46 -0700126 return event.loop();
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500127 }
Matthew Barth3e1bb272020-05-26 11:09:21 -0500128 // Log the useful metadata on these exceptions and let the app
129 // return 1 so it is restarted without a core dump.
Patrick Williamsddb773b2021-10-06 11:24:49 -0500130 catch (const phosphor::fan::util::DBusServiceError& e)
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500131 {
132 log<level::ERR>("Uncaught DBus service lookup failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500133 entry("PATH=%s", e.path.c_str()),
134 entry("INTERFACE=%s", e.interface.c_str()));
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500135 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500136 catch (const phosphor::fan::util::DBusMethodError& e)
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500137 {
138 log<level::ERR>("Uncaught DBus method failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500139 entry("BUSNAME=%s", e.busName.c_str()),
140 entry("PATH=%s", e.path.c_str()),
141 entry("INTERFACE=%s", e.interface.c_str()),
142 entry("METHOD=%s", e.method.c_str()));
Matt Spinlere73446e2017-04-10 13:55:52 -0500143 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500144 catch (const phosphor::fan::util::DBusPropertyError& e)
Matthew Barth88923a02018-05-11 10:14:44 -0500145 {
146 log<level::ERR>("Uncaught DBus property access failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500147 entry("BUSNAME=%s", e.busName.c_str()),
148 entry("PATH=%s", e.path.c_str()),
149 entry("INTERFACE=%s", e.interface.c_str()),
150 entry("PROPERTY=%s", e.property.c_str()));
Matthew Barth88923a02018-05-11 10:14:44 -0500151 }
Matt Spinler3ac99022021-10-04 16:02:49 -0500152 catch (std::exception& e)
153 {
154#ifdef CONTROL_USE_JSON
155 phosphor::fan::control::json::FlightRecorder::instance().log(
156 "main", "Unexpected exception exit");
Matt Spinler7787def2021-10-14 16:33:16 -0500157 dumpFlightRecorder();
Matt Spinler3ac99022021-10-04 16:02:49 -0500158#endif
159 throw;
160 }
161
162#ifdef CONTROL_USE_JSON
163 phosphor::fan::control::json::FlightRecorder::instance().log(
164 "main", "Abnormal exit");
Matt Spinler7787def2021-10-14 16:33:16 -0500165 dumpFlightRecorder();
Matt Spinler3ac99022021-10-04 16:02:49 -0500166#endif
Matt Spinlere73446e2017-04-10 13:55:52 -0500167
William A. Kennington III3e781062018-10-19 17:18:34 -0700168 return 1;
Matt Spinlere73446e2017-04-10 13:55:52 -0500169}