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 |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 19 | #include "argument.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 |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 44 | phosphor::fan::util::ArgumentParser args(argc, argv); |
| 45 | |
| 46 | if (argc != 2) |
| 47 | { |
| 48 | args.usage(argv); |
William A. Kennington III | 3e78106 | 2018-10-19 17:18:34 -0700 | [diff] [blame] | 49 | return 1; |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 50 | } |
| 51 | |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 52 | if (args["init"] == "true") |
| 53 | { |
| 54 | mode = Mode::init; |
| 55 | } |
| 56 | else if (args["monitor"] == "true") |
| 57 | { |
| 58 | mode = Mode::monitor; |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | args.usage(argv); |
William A. Kennington III | 3e78106 | 2018-10-19 17:18:34 -0700 | [diff] [blame] | 63 | return 1; |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 64 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 65 | #endif |
| 66 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 67 | // Attach the event object to the bus object so we can |
| 68 | // handle both sd_events (for the timers) and dbus signals. |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 69 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 70 | |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 71 | System system(mode, bus, event); |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 72 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 73 | #ifdef MONITOR_USE_JSON |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 74 | |
Matthew Barth | 823bc49 | 2021-06-21 14:19:09 -0500 | [diff] [blame] | 75 | phosphor::fan::JsonConfig config(std::bind(&System::start, &system)); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 76 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 77 | // Enable SIGHUP handling to reload JSON config |
| 78 | stdplus::signal::block(SIGHUP); |
| 79 | sdeventplus::source::Signal signal(event, SIGHUP, |
| 80 | std::bind(&System::sighupHandler, |
| 81 | &system, std::placeholders::_1, |
| 82 | std::placeholders::_2)); |
Matt Spinler | 4f472a8 | 2022-08-26 13:55:34 -0500 | [diff] [blame] | 83 | |
| 84 | // Enable SIGUSR1 handling to dump debug data |
| 85 | stdplus::signal::block(SIGUSR1); |
| 86 | sdeventplus::source::Signal sigUsr1( |
| 87 | event, SIGUSR1, |
| 88 | std::bind(&System::dumpDebugData, &system, std::placeholders::_1, |
| 89 | std::placeholders::_2)); |
| 90 | |
Matt Spinler | c8d3c51 | 2021-01-06 14:22:25 -0600 | [diff] [blame] | 91 | bus.request_name(THERMAL_ALERT_BUSNAME); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 92 | #else |
| 93 | system.start(); |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 94 | |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 95 | if (mode == Mode::init) |
Matt Spinler | a5763ff | 2017-06-14 15:54:12 -0500 | [diff] [blame] | 96 | { |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 97 | // Fans were initialized to be functional, exit |
| 98 | return 0; |
Matt Spinler | a5763ff | 2017-06-14 15:54:12 -0500 | [diff] [blame] | 99 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 100 | #endif |
Matt Spinler | c36168a | 2017-04-27 14:32:43 -0500 | [diff] [blame] | 101 | |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 102 | return event.loop(); |
Matt Spinler | e567dd2 | 2017-04-27 12:27:17 -0500 | [diff] [blame] | 103 | } |