blob: 8505c76dd8cbb1723c6f13de1baa2f96d439a92f [file] [log] [blame]
Matt Spinlere567dd22017-04-27 12:27:17 -05001/**
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 Barthd06905c2020-06-12 08:13:06 -050016#include "config.h"
17
Matt Spinler7d135642021-02-04 12:44:17 -060018#ifndef MONITOR_USE_JSON
George Liucb6129e2023-08-14 20:42:41 +080019#include <CLI/CLI.hpp>
Matt Spinler7d135642021-02-04 12:44:17 -060020#endif
Matt Spinlerc36168a2017-04-27 14:32:43 -050021#include "fan.hpp"
Matt Spinler7d135642021-02-04 12:44:17 -060022#ifdef MONITOR_USE_JSON
Mike Cappsbf8e56f2022-06-29 14:23:07 -040023#include "dbus_paths.hpp"
Matt Spinler7d135642021-02-04 12:44:17 -060024#include "json_config.hpp"
25#include "json_parser.hpp"
26#endif
Matthew Barthc95c5272020-06-15 19:51:13 -050027#include "system.hpp"
Matt Spinlerc39e8592017-09-28 13:13:08 -050028#include "trust_manager.hpp"
Matt Spinlere567dd22017-04-27 12:27:17 -050029
Matthew Barth177fe982020-05-26 11:05:19 -050030#include <sdbusplus/bus.hpp>
31#include <sdeventplus/event.hpp>
Matthew Barthd06905c2020-06-12 08:13:06 -050032#include <sdeventplus/source/signal.hpp>
33#include <stdplus/signal.hpp>
Matthew Barth177fe982020-05-26 11:05:19 -050034
Matt Spinlerc36168a2017-04-27 14:32:43 -050035using namespace phosphor::fan::monitor;
Matt Spinlerc36168a2017-04-27 14:32:43 -050036
Mike Capps808d7fe2022-06-13 10:12:16 -040037int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
Matt Spinlere567dd22017-04-27 12:27:17 -050038{
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070039 auto event = sdeventplus::Event::get_default();
Matt Spinlerc36168a2017-04-27 14:32:43 -050040 auto bus = sdbusplus::bus::new_default();
Matt Spinler7d135642021-02-04 12:44:17 -060041 Mode mode = Mode::init;
42
43#ifndef MONITOR_USE_JSON
George Liucb6129e2023-08-14 20:42:41 +080044 CLI::App app{"Phosphor Fan Monitor"};
Matthew Barth6ad28432017-08-22 11:18:19 -050045
George Liucb6129e2023-08-14 20:42:41 +080046 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 Barth6ad28432017-08-22 11:18:19 -050053 {
George Liucb6129e2023-08-14 20:42:41 +080054 app.parse(argc, argv);
55 }
56 catch (const CLI::Error& e)
57 {
58 return app.exit(e);
Matthew Barth6ad28432017-08-22 11:18:19 -050059 }
60
George Liucb6129e2023-08-14 20:42:41 +080061 if (init)
Matthew Barth6ad28432017-08-22 11:18:19 -050062 {
63 mode = Mode::init;
64 }
George Liucb6129e2023-08-14 20:42:41 +080065 else if (monitor)
Matthew Barth6ad28432017-08-22 11:18:19 -050066 {
67 mode = Mode::monitor;
68 }
Matt Spinlerb0412d02020-10-12 16:53:52 -050069#endif
70
Matthew Barth177fe982020-05-26 11:05:19 -050071 // 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 III1cfc2f12018-10-19 17:29:46 -070073 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Matt Spinlerc36168a2017-04-27 14:32:43 -050074
Matthew Barthc95c5272020-06-15 19:51:13 -050075 System system(mode, bus, event);
Matt Spinlerc36168a2017-04-27 14:32:43 -050076
Matthew Barthd06905c2020-06-12 08:13:06 -050077#ifdef MONITOR_USE_JSON
Matt Spinler7d135642021-02-04 12:44:17 -060078
Matthew Barth823bc492021-06-21 14:19:09 -050079 phosphor::fan::JsonConfig config(std::bind(&System::start, &system));
Matt Spinler7d135642021-02-04 12:44:17 -060080
Matthew Barthd06905c2020-06-12 08:13:06 -050081 // 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 Spinler4f472a82022-08-26 13:55:34 -050087
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 Spinlerc8d3c512021-01-06 14:22:25 -060095 bus.request_name(THERMAL_ALERT_BUSNAME);
Matt Spinler7d135642021-02-04 12:44:17 -060096#else
97 system.start();
Matthew Barthd06905c2020-06-12 08:13:06 -050098
Matthew Barth6ad28432017-08-22 11:18:19 -050099 if (mode == Mode::init)
Matt Spinlera5763ff2017-06-14 15:54:12 -0500100 {
Matthew Barth6ad28432017-08-22 11:18:19 -0500101 // Fans were initialized to be functional, exit
102 return 0;
Matt Spinlera5763ff2017-06-14 15:54:12 -0500103 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500104#endif
Matt Spinlerc36168a2017-04-27 14:32:43 -0500105
William A. Kennington III1cfc2f12018-10-19 17:29:46 -0700106 return event.loop();
Matt Spinlere567dd22017-04-27 12:27:17 -0500107}