blob: fbdcd0d28619d3eb2ec743002baa43a8323cd320 [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
Matthew Barth6ad28432017-08-22 11:18:19 -050019#include "argument.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
Matthew Barth6ad28432017-08-22 11:18:19 -050044 phosphor::fan::util::ArgumentParser args(argc, argv);
45
46 if (argc != 2)
47 {
48 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070049 return 1;
Matthew Barth6ad28432017-08-22 11:18:19 -050050 }
51
Matthew Barth6ad28432017-08-22 11:18:19 -050052 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 III3e781062018-10-19 17:18:34 -070063 return 1;
Matthew Barth6ad28432017-08-22 11:18:19 -050064 }
Matt Spinlerb0412d02020-10-12 16:53:52 -050065#endif
66
Matthew Barth177fe982020-05-26 11:05:19 -050067 // 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 III1cfc2f12018-10-19 17:29:46 -070069 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Matt Spinlerc36168a2017-04-27 14:32:43 -050070
Matthew Barthc95c5272020-06-15 19:51:13 -050071 System system(mode, bus, event);
Matt Spinlerc36168a2017-04-27 14:32:43 -050072
Matthew Barthd06905c2020-06-12 08:13:06 -050073#ifdef MONITOR_USE_JSON
Matt Spinler7d135642021-02-04 12:44:17 -060074
Matthew Barth823bc492021-06-21 14:19:09 -050075 phosphor::fan::JsonConfig config(std::bind(&System::start, &system));
Matt Spinler7d135642021-02-04 12:44:17 -060076
Matthew Barthd06905c2020-06-12 08:13:06 -050077 // 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 Spinler4f472a82022-08-26 13:55:34 -050083
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 Spinlerc8d3c512021-01-06 14:22:25 -060091 bus.request_name(THERMAL_ALERT_BUSNAME);
Matt Spinler7d135642021-02-04 12:44:17 -060092#else
93 system.start();
Matthew Barthd06905c2020-06-12 08:13:06 -050094
Matthew Barth6ad28432017-08-22 11:18:19 -050095 if (mode == Mode::init)
Matt Spinlera5763ff2017-06-14 15:54:12 -050096 {
Matthew Barth6ad28432017-08-22 11:18:19 -050097 // Fans were initialized to be functional, exit
98 return 0;
Matt Spinlera5763ff2017-06-14 15:54:12 -050099 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500100#endif
Matt Spinlerc36168a2017-04-27 14:32:43 -0500101
William A. Kennington III1cfc2f12018-10-19 17:29:46 -0700102 return event.loop();
Matt Spinlere567dd22017-04-27 12:27:17 -0500103}