Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 IBM Corporation |
| 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 | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 18 | #ifndef MONITOR_USE_JSON |
George Liu | cb6129e | 2023-08-14 20:42:41 +0800 | [diff] [blame] | 19 | #include <CLI/CLI.hpp> |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 20 | #endif |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 21 | #include "fan.hpp" |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 22 | #ifdef MONITOR_USE_JSON |
Mike Capps | bf8e56f | 2022-06-29 14:23:07 -0400 | [diff] [blame] | 23 | #include "dbus_paths.hpp" |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 24 | #include "json_config.hpp" |
| 25 | #include "json_parser.hpp" |
| 26 | #endif |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 27 | #include "system.hpp" |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 28 | #include "trust_manager.hpp" |
Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -0500 | [diff] [blame] | 29 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 30 | #include <sdbusplus/bus.hpp> |
| 31 | #include <sdeventplus/event.hpp> |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 32 | #include <sdeventplus/source/signal.hpp> |
| 33 | #include <stdplus/signal.hpp> |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 34 | |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 35 | using namespace phosphor::fan::monitor; |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 36 | |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 37 | int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[]) |
Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -0500 | [diff] [blame] | 38 | { |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 39 | auto event = sdeventplus::Event::get_default(); |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 40 | auto bus = sdbusplus::bus::new_default(); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 41 | Mode mode = Mode::init; |
| 42 | |
| 43 | #ifndef MONITOR_USE_JSON |
George Liu | cb6129e | 2023-08-14 20:42:41 +0800 | [diff] [blame] | 44 | CLI::App app{"Phosphor Fan Monitor"}; |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 45 | |
George Liu | cb6129e | 2023-08-14 20:42:41 +0800 | [diff] [blame] | 46 | bool init = false; |
| 47 | bool monitor = false; |
| 48 | app.add_flag("-i,--init", init, "Set fans to functional"); |
| 49 | app.add_flag("-m,--monitor", monitor, "Start fan functional monitoring"); |
| 50 | app.require_option(); |
| 51 | |
| 52 | try |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 53 | { |
George Liu | cb6129e | 2023-08-14 20:42:41 +0800 | [diff] [blame] | 54 | app.parse(argc, argv); |
| 55 | } |
| 56 | catch (const CLI::Error& e) |
| 57 | { |
| 58 | return app.exit(e); |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 59 | } |
| 60 | |
George Liu | cb6129e | 2023-08-14 20:42:41 +0800 | [diff] [blame] | 61 | if (init) |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 62 | { |
| 63 | mode = Mode::init; |
| 64 | } |
George Liu | cb6129e | 2023-08-14 20:42:41 +0800 | [diff] [blame] | 65 | else if (monitor) |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 66 | { |
| 67 | mode = Mode::monitor; |
| 68 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 69 | #endif |
| 70 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 71 | // Attach the event object to the bus object so we can |
| 72 | // handle both sd_events (for the timers) and dbus signals. |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 73 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 74 | |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 75 | System system(mode, bus, event); |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 76 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 77 | #ifdef MONITOR_USE_JSON |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 78 | |
Matthew Barth | 823bc49 | 2021-06-21 14:19:09 -0500 | [diff] [blame] | 79 | phosphor::fan::JsonConfig config(std::bind(&System::start, &system)); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 80 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 81 | // Enable SIGHUP handling to reload JSON config |
| 82 | stdplus::signal::block(SIGHUP); |
| 83 | sdeventplus::source::Signal signal(event, SIGHUP, |
| 84 | std::bind(&System::sighupHandler, |
| 85 | &system, std::placeholders::_1, |
| 86 | std::placeholders::_2)); |
Matt Spinler | 4f472a8 | 2022-08-26 13:55:34 -0500 | [diff] [blame] | 87 | |
| 88 | // Enable SIGUSR1 handling to dump debug data |
| 89 | stdplus::signal::block(SIGUSR1); |
| 90 | sdeventplus::source::Signal sigUsr1( |
| 91 | event, SIGUSR1, |
| 92 | std::bind(&System::dumpDebugData, &system, std::placeholders::_1, |
| 93 | std::placeholders::_2)); |
| 94 | |
Matt Spinler | c8d3c51 | 2021-01-06 14:22:25 -0600 | [diff] [blame] | 95 | bus.request_name(THERMAL_ALERT_BUSNAME); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 96 | #else |
| 97 | system.start(); |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 98 | |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 99 | if (mode == Mode::init) |
Matt Spinler | a5763ff | 2017-06-14 15:54:12 -0500 | [diff] [blame] | 100 | { |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 101 | // Fans were initialized to be functional, exit |
| 102 | return 0; |
Matt Spinler | a5763ff | 2017-06-14 15:54:12 -0500 | [diff] [blame] | 103 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 104 | #endif |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 105 | |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 106 | return event.loop(); |
Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -0500 | [diff] [blame] | 107 | } |