blob: 9284ae41f9640041adbe63c19762c9eea71613ca [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
Matthew Barth3770a1d2021-06-10 15:09:37 -050074 // Handle loading fan control's config file(s)
75 phosphor::fan::JsonConfig config(
76 std::bind(&json::Manager::load, &manager));
77
Matthew Barthe91ac862021-05-25 16:22:17 -050078 // Enable SIGHUP handling to reload JSON configs
79 stdplus::signal::block(SIGHUP);
80 sdeventplus::source::Signal signal(
81 event, SIGHUP,
82 std::bind(&json::Manager::sighupHandler, &manager,
83 std::placeholders::_1, std::placeholders::_2));
84
85 phosphor::fan::util::SDBusPlus::getBus().request_name(CONTROL_BUSNAME);
Matthew Barth06764942021-03-04 09:28:40 -060086#else
Matthew Barth9403a212021-05-17 09:31:50 -050087 Manager manager(phosphor::fan::util::SDBusPlus::getBus(), event, mode);
Matt Spinleree7f6422017-05-09 11:03:14 -050088
Matthew Barth3e1bb272020-05-26 11:09:21 -050089 // Init mode will just set fans to max and delay
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050090 if (mode == Mode::init)
Matthew Barth8600d9a2017-06-23 14:38:05 -050091 {
Matthew Barth06764942021-03-04 09:28:40 -060092 manager.doInit(event);
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050093 return 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -050094 }
Matthew Barth06764942021-03-04 09:28:40 -060095#endif
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070096 return event.loop();
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050097 }
Matthew Barth3e1bb272020-05-26 11:09:21 -050098 // Log the useful metadata on these exceptions and let the app
99 // return 1 so it is restarted without a core dump.
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500100 catch (phosphor::fan::util::DBusServiceError& e)
101 {
102 log<level::ERR>("Uncaught DBus service lookup failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500103 entry("PATH=%s", e.path.c_str()),
104 entry("INTERFACE=%s", e.interface.c_str()));
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500105 }
106 catch (phosphor::fan::util::DBusMethodError& e)
107 {
108 log<level::ERR>("Uncaught DBus method failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500109 entry("BUSNAME=%s", e.busName.c_str()),
110 entry("PATH=%s", e.path.c_str()),
111 entry("INTERFACE=%s", e.interface.c_str()),
112 entry("METHOD=%s", e.method.c_str()));
Matt Spinlere73446e2017-04-10 13:55:52 -0500113 }
Matthew Barth88923a02018-05-11 10:14:44 -0500114 catch (phosphor::fan::util::DBusPropertyError& e)
115 {
116 log<level::ERR>("Uncaught DBus property access failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500117 entry("BUSNAME=%s", e.busName.c_str()),
118 entry("PATH=%s", e.path.c_str()),
119 entry("INTERFACE=%s", e.interface.c_str()),
120 entry("PROPERTY=%s", e.property.c_str()));
Matthew Barth88923a02018-05-11 10:14:44 -0500121 }
Matt Spinlere73446e2017-04-10 13:55:52 -0500122
William A. Kennington III3e781062018-10-19 17:18:34 -0700123 return 1;
Matt Spinlere73446e2017-04-10 13:55:52 -0500124}