blob: 5ed0953936362f76bfa9a82bacd19ed7755e2e46 [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
23#include "json_config.hpp"
24#include "json_parser.hpp"
25#endif
Matthew Barthc95c5272020-06-15 19:51:13 -050026#include "system.hpp"
Matt Spinlerc39e8592017-09-28 13:13:08 -050027#include "trust_manager.hpp"
Matt Spinlere567dd22017-04-27 12:27:17 -050028
Matthew Barth177fe982020-05-26 11:05:19 -050029#include <sdbusplus/bus.hpp>
30#include <sdeventplus/event.hpp>
Matthew Barthd06905c2020-06-12 08:13:06 -050031#include <sdeventplus/source/signal.hpp>
32#include <stdplus/signal.hpp>
Matthew Barth177fe982020-05-26 11:05:19 -050033
Matt Spinlerc36168a2017-04-27 14:32:43 -050034using namespace phosphor::fan::monitor;
Matt Spinlerc36168a2017-04-27 14:32:43 -050035
Mike Capps808d7fe2022-06-13 10:12:16 -040036int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
Matt Spinlere567dd22017-04-27 12:27:17 -050037{
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070038 auto event = sdeventplus::Event::get_default();
Matt Spinlerc36168a2017-04-27 14:32:43 -050039 auto bus = sdbusplus::bus::new_default();
Matt Spinler7d135642021-02-04 12:44:17 -060040 Mode mode = Mode::init;
41
42#ifndef MONITOR_USE_JSON
Matthew Barth6ad28432017-08-22 11:18:19 -050043 phosphor::fan::util::ArgumentParser args(argc, argv);
44
45 if (argc != 2)
46 {
47 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070048 return 1;
Matthew Barth6ad28432017-08-22 11:18:19 -050049 }
50
Matthew Barth6ad28432017-08-22 11:18:19 -050051 if (args["init"] == "true")
52 {
53 mode = Mode::init;
54 }
55 else if (args["monitor"] == "true")
56 {
57 mode = Mode::monitor;
58 }
59 else
60 {
61 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070062 return 1;
Matthew Barth6ad28432017-08-22 11:18:19 -050063 }
Matt Spinlerb0412d02020-10-12 16:53:52 -050064#endif
65
Matthew Barth177fe982020-05-26 11:05:19 -050066 // Attach the event object to the bus object so we can
67 // handle both sd_events (for the timers) and dbus signals.
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070068 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Matt Spinlerc36168a2017-04-27 14:32:43 -050069
Matthew Barthc95c5272020-06-15 19:51:13 -050070 System system(mode, bus, event);
Matt Spinlerc36168a2017-04-27 14:32:43 -050071
Matthew Barthd06905c2020-06-12 08:13:06 -050072#ifdef MONITOR_USE_JSON
Matt Spinler7d135642021-02-04 12:44:17 -060073
Matthew Barth823bc492021-06-21 14:19:09 -050074 phosphor::fan::JsonConfig config(std::bind(&System::start, &system));
Matt Spinler7d135642021-02-04 12:44:17 -060075
Matthew Barthd06905c2020-06-12 08:13:06 -050076 // Enable SIGHUP handling to reload JSON config
77 stdplus::signal::block(SIGHUP);
78 sdeventplus::source::Signal signal(event, SIGHUP,
79 std::bind(&System::sighupHandler,
80 &system, std::placeholders::_1,
81 std::placeholders::_2));
Matt Spinlerc8d3c512021-01-06 14:22:25 -060082 bus.request_name(THERMAL_ALERT_BUSNAME);
Matt Spinler7d135642021-02-04 12:44:17 -060083#else
84 system.start();
Matthew Barthd06905c2020-06-12 08:13:06 -050085
Matthew Barth6ad28432017-08-22 11:18:19 -050086 if (mode == Mode::init)
Matt Spinlera5763ff2017-06-14 15:54:12 -050087 {
Matthew Barth6ad28432017-08-22 11:18:19 -050088 // Fans were initialized to be functional, exit
89 return 0;
Matt Spinlera5763ff2017-06-14 15:54:12 -050090 }
Matt Spinlerb0412d02020-10-12 16:53:52 -050091#endif
Matt Spinlerc36168a2017-04-27 14:32:43 -050092
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070093 return event.loop();
Matt Spinlere567dd22017-04-27 12:27:17 -050094}