blob: 9c7f8a216411a507ed5ac2702dde45a7ffc6f3bf [file] [log] [blame]
Matt Spinlere73446e2017-04-10 13:55:52 -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 Barthf8ae7a52021-03-05 10:23:43 -060016#include "config.h"
17
18#ifndef CONTROL_USE_JSON
Matt Spinleree7f6422017-05-09 11:03:14 -050019#include "argument.hpp"
Matt Spinlere10416e2017-04-10 14:15:53 -050020#include "manager.hpp"
Matthew Barthf8ae7a52021-03-05 10:23:43 -060021#else
Matthew Barth06764942021-03-04 09:28:40 -060022#include "json/manager.hpp"
23#endif
Matthew Barthf8ae7a52021-03-05 10:23:43 -060024#include "sdbusplus.hpp"
Matt Spinlere73446e2017-04-10 13:55:52 -050025
Matthew Barth3e1bb272020-05-26 11:09:21 -050026#include <phosphor-logging/log.hpp>
27#include <sdbusplus/bus.hpp>
28#include <sdeventplus/event.hpp>
Matthew Barthe91ac862021-05-25 16:22:17 -050029#include <sdeventplus/source/signal.hpp>
30#include <stdplus/signal.hpp>
Matthew Barth3e1bb272020-05-26 11:09:21 -050031
Matt Spinleree7f6422017-05-09 11:03:14 -050032using namespace phosphor::fan::control;
Matthew Barth8600d9a2017-06-23 14:38:05 -050033using namespace phosphor::logging;
Matt Spinleree7f6422017-05-09 11:03:14 -050034
Matt Spinlere73446e2017-04-10 13:55:52 -050035int main(int argc, char* argv[])
36{
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070037 auto event = sdeventplus::Event::get_default();
Matt Spinlere73446e2017-04-10 13:55:52 -050038
Matthew Barth06764942021-03-04 09:28:40 -060039#ifndef CONTROL_USE_JSON
40 phosphor::fan::util::ArgumentParser args(argc, argv);
Matt Spinleree7f6422017-05-09 11:03:14 -050041 if (argc != 2)
42 {
43 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070044 return 1;
Matt Spinleree7f6422017-05-09 11:03:14 -050045 }
46
Matthew Barth14184132017-05-19 14:37:30 -050047 Mode mode;
Matt Spinleree7f6422017-05-09 11:03:14 -050048
49 if (args["init"] == "true")
50 {
Matthew Barth14184132017-05-19 14:37:30 -050051 mode = Mode::init;
Matt Spinleree7f6422017-05-09 11:03:14 -050052 }
53 else if (args["control"] == "true")
54 {
Matthew Barth14184132017-05-19 14:37:30 -050055 mode = Mode::control;
Matt Spinleree7f6422017-05-09 11:03:14 -050056 }
57 else
58 {
59 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070060 return 1;
Matt Spinleree7f6422017-05-09 11:03:14 -050061 }
Matthew Barth06764942021-03-04 09:28:40 -060062#endif
Matt Spinleree7f6422017-05-09 11:03:14 -050063
Matthew Barth3e1bb272020-05-26 11:09:21 -050064 // Attach the event object to the bus object so we can
65 // handle both sd_events (for the timers) and dbus signals.
Matthew Barth9403a212021-05-17 09:31:50 -050066 phosphor::fan::util::SDBusPlus::getBus().attach_event(
67 event.get(), SD_EVENT_PRIORITY_NORMAL);
Matthew Barth8600d9a2017-06-23 14:38:05 -050068
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050069 try
70 {
Matthew Barth06764942021-03-04 09:28:40 -060071#ifdef CONTROL_USE_JSON
Matthew Barth9403a212021-05-17 09:31:50 -050072 json::Manager manager(event);
Matthew Barthe91ac862021-05-25 16:22:17 -050073
74 // Enable SIGHUP handling to reload JSON configs
75 stdplus::signal::block(SIGHUP);
76 sdeventplus::source::Signal signal(
77 event, SIGHUP,
78 std::bind(&json::Manager::sighupHandler, &manager,
79 std::placeholders::_1, std::placeholders::_2));
80
81 phosphor::fan::util::SDBusPlus::getBus().request_name(CONTROL_BUSNAME);
Matthew Barth06764942021-03-04 09:28:40 -060082#else
Matthew Barth9403a212021-05-17 09:31:50 -050083 Manager manager(phosphor::fan::util::SDBusPlus::getBus(), event, mode);
Matt Spinleree7f6422017-05-09 11:03:14 -050084
Matthew Barth3e1bb272020-05-26 11:09:21 -050085 // Init mode will just set fans to max and delay
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050086 if (mode == Mode::init)
Matthew Barth8600d9a2017-06-23 14:38:05 -050087 {
Matthew Barth06764942021-03-04 09:28:40 -060088 manager.doInit(event);
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050089 return 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -050090 }
Matthew Barth06764942021-03-04 09:28:40 -060091#endif
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070092 return event.loop();
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050093 }
Matthew Barth3e1bb272020-05-26 11:09:21 -050094 // Log the useful metadata on these exceptions and let the app
95 // return 1 so it is restarted without a core dump.
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050096 catch (phosphor::fan::util::DBusServiceError& e)
97 {
98 log<level::ERR>("Uncaught DBus service lookup failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -050099 entry("PATH=%s", e.path.c_str()),
100 entry("INTERFACE=%s", e.interface.c_str()));
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500101 }
102 catch (phosphor::fan::util::DBusMethodError& e)
103 {
104 log<level::ERR>("Uncaught DBus method failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500105 entry("BUSNAME=%s", e.busName.c_str()),
106 entry("PATH=%s", e.path.c_str()),
107 entry("INTERFACE=%s", e.interface.c_str()),
108 entry("METHOD=%s", e.method.c_str()));
Matt Spinlere73446e2017-04-10 13:55:52 -0500109 }
Matthew Barth88923a02018-05-11 10:14:44 -0500110 catch (phosphor::fan::util::DBusPropertyError& e)
111 {
112 log<level::ERR>("Uncaught DBus property access failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500113 entry("BUSNAME=%s", e.busName.c_str()),
114 entry("PATH=%s", e.path.c_str()),
115 entry("INTERFACE=%s", e.interface.c_str()),
116 entry("PROPERTY=%s", e.property.c_str()));
Matthew Barth88923a02018-05-11 10:14:44 -0500117 }
Matt Spinlere73446e2017-04-10 13:55:52 -0500118
William A. Kennington III3e781062018-10-19 17:18:34 -0700119 return 1;
Matt Spinlere73446e2017-04-10 13:55:52 -0500120}