Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 1 | /** |
Mike Capps | b2e9a4f | 2022-06-13 10:15:42 -0400 | [diff] [blame] | 2 | * Copyright © 2022 IBM Corporation |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 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 | */ |
Matthew Barth | f8ae7a5 | 2021-03-05 10:23:43 -0600 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
| 18 | #ifndef CONTROL_USE_JSON |
Matt Spinler | e10416e | 2017-04-10 14:15:53 -0500 | [diff] [blame] | 19 | #include "manager.hpp" |
George Liu | 854abad | 2023-08-15 17:33:12 +0800 | [diff] [blame] | 20 | |
| 21 | #include <CLI/CLI.hpp> |
Matthew Barth | f8ae7a5 | 2021-03-05 10:23:43 -0600 | [diff] [blame] | 22 | #else |
Mike Capps | a081956 | 2022-06-13 10:17:10 -0400 | [diff] [blame] | 23 | #include "../utils/flight_recorder.hpp" |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 24 | #include "json/manager.hpp" |
| 25 | #endif |
Mike Capps | b2e9a4f | 2022-06-13 10:15:42 -0400 | [diff] [blame] | 26 | |
Mike Capps | bf8e56f | 2022-06-29 14:23:07 -0400 | [diff] [blame] | 27 | #include "dbus_paths.hpp" |
Matthew Barth | f8ae7a5 | 2021-03-05 10:23:43 -0600 | [diff] [blame] | 28 | #include "sdbusplus.hpp" |
Matthew Barth | 2feab08 | 2021-06-29 14:50:14 -0500 | [diff] [blame] | 29 | #include "sdeventplus.hpp" |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 30 | |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 31 | #include <phosphor-logging/log.hpp> |
| 32 | #include <sdbusplus/bus.hpp> |
| 33 | #include <sdeventplus/event.hpp> |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 34 | #include <sdeventplus/source/signal.hpp> |
| 35 | #include <stdplus/signal.hpp> |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 36 | |
Matt Spinler | 7787def | 2021-10-14 16:33:16 -0500 | [diff] [blame] | 37 | #include <fstream> |
| 38 | |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 39 | using namespace phosphor::fan::control; |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 40 | using namespace phosphor::logging; |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 41 | |
Matt Spinler | 7787def | 2021-10-14 16:33:16 -0500 | [diff] [blame] | 42 | #ifdef CONTROL_USE_JSON |
| 43 | void 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 Capps | b2e9a4f | 2022-06-13 10:15:42 -0400 | [diff] [blame] | 52 | int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[]) |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 53 | { |
Matthew Barth | 2feab08 | 2021-06-29 14:50:14 -0500 | [diff] [blame] | 54 | auto event = phosphor::fan::util::SDEventPlus::getEvent(); |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 55 | |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 56 | #ifndef CONTROL_USE_JSON |
George Liu | 854abad | 2023-08-15 17:33:12 +0800 | [diff] [blame] | 57 | 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 Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 66 | { |
George Liu | 854abad | 2023-08-15 17:33:12 +0800 | [diff] [blame] | 67 | app.parse(argc, argv); |
| 68 | } |
| 69 | catch (const CLI::Error& e) |
| 70 | { |
| 71 | return app.exit(e); |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 74 | Mode mode; |
George Liu | 854abad | 2023-08-15 17:33:12 +0800 | [diff] [blame] | 75 | if (init) |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 76 | { |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 77 | mode = Mode::init; |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 78 | } |
George Liu | 854abad | 2023-08-15 17:33:12 +0800 | [diff] [blame] | 79 | else if (control) |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 80 | { |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 81 | mode = Mode::control; |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 82 | } |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 83 | #endif |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 84 | |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 85 | // Attach the event object to the bus object so we can |
| 86 | // handle both sd_events (for the timers) and dbus signals. |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 87 | phosphor::fan::util::SDBusPlus::getBus().attach_event( |
| 88 | event.get(), SD_EVENT_PRIORITY_NORMAL); |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 89 | |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 90 | try |
| 91 | { |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 92 | #ifdef CONTROL_USE_JSON |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame^] | 93 | phosphor::fan::control::json::FlightRecorder::instance().log( |
| 94 | "main", "Startup"); |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 95 | json::Manager manager(event); |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 96 | |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 97 | // Handle loading fan control's config file(s) |
| 98 | phosphor::fan::JsonConfig config( |
| 99 | std::bind(&json::Manager::load, &manager)); |
| 100 | |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 101 | // 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 Spinler | 2fc0a35 | 2021-10-04 15:10:57 -0500 | [diff] [blame] | 108 | // Enable SIGUSR1 handling to dump the flight recorder |
| 109 | stdplus::signal::block(SIGUSR1); |
| 110 | sdeventplus::source::Signal sigUsr1( |
| 111 | event, SIGUSR1, |
Matt Spinler | 27f5f4e | 2022-09-01 14:57:39 -0500 | [diff] [blame] | 112 | std::bind(&json::Manager::dumpDebugData, &manager, |
Matt Spinler | 2fc0a35 | 2021-10-04 15:10:57 -0500 | [diff] [blame] | 113 | std::placeholders::_1, std::placeholders::_2)); |
| 114 | |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 115 | phosphor::fan::util::SDBusPlus::getBus().request_name(CONTROL_BUSNAME); |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 116 | #else |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 117 | Manager manager(phosphor::fan::util::SDBusPlus::getBus(), event, mode); |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 118 | |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 119 | // Init mode will just set fans to max and delay |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 120 | if (mode == Mode::init) |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 121 | { |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 122 | manager.doInit(event); |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 123 | return 0; |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 124 | } |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 125 | #endif |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 126 | return event.loop(); |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 127 | } |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 128 | // Log the useful metadata on these exceptions and let the app |
| 129 | // return 1 so it is restarted without a core dump. |
Patrick Williams | ddb773b | 2021-10-06 11:24:49 -0500 | [diff] [blame] | 130 | catch (const phosphor::fan::util::DBusServiceError& e) |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 131 | { |
| 132 | log<level::ERR>("Uncaught DBus service lookup failure exception", |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 133 | entry("PATH=%s", e.path.c_str()), |
| 134 | entry("INTERFACE=%s", e.interface.c_str())); |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 135 | } |
Patrick Williams | ddb773b | 2021-10-06 11:24:49 -0500 | [diff] [blame] | 136 | catch (const phosphor::fan::util::DBusMethodError& e) |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 137 | { |
| 138 | log<level::ERR>("Uncaught DBus method failure exception", |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 139 | 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 Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 143 | } |
Patrick Williams | ddb773b | 2021-10-06 11:24:49 -0500 | [diff] [blame] | 144 | catch (const phosphor::fan::util::DBusPropertyError& e) |
Matthew Barth | 88923a0 | 2018-05-11 10:14:44 -0500 | [diff] [blame] | 145 | { |
| 146 | log<level::ERR>("Uncaught DBus property access failure exception", |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 147 | 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 Barth | 88923a0 | 2018-05-11 10:14:44 -0500 | [diff] [blame] | 151 | } |
Matt Spinler | 3ac9902 | 2021-10-04 16:02:49 -0500 | [diff] [blame] | 152 | catch (std::exception& e) |
| 153 | { |
| 154 | #ifdef CONTROL_USE_JSON |
| 155 | phosphor::fan::control::json::FlightRecorder::instance().log( |
| 156 | "main", "Unexpected exception exit"); |
Matt Spinler | 7787def | 2021-10-14 16:33:16 -0500 | [diff] [blame] | 157 | dumpFlightRecorder(); |
Matt Spinler | 3ac9902 | 2021-10-04 16:02:49 -0500 | [diff] [blame] | 158 | #endif |
| 159 | throw; |
| 160 | } |
| 161 | |
| 162 | #ifdef CONTROL_USE_JSON |
| 163 | phosphor::fan::control::json::FlightRecorder::instance().log( |
| 164 | "main", "Abnormal exit"); |
Matt Spinler | 7787def | 2021-10-14 16:33:16 -0500 | [diff] [blame] | 165 | dumpFlightRecorder(); |
Matt Spinler | 3ac9902 | 2021-10-04 16:02:49 -0500 | [diff] [blame] | 166 | #endif |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 167 | |
William A. Kennington III | 3e78106 | 2018-10-19 17:18:34 -0700 | [diff] [blame] | 168 | return 1; |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 169 | } |