blob: 1dcc0b550bae6066abdbe04e5f28d7461e691851 [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>
29
Matt Spinleree7f6422017-05-09 11:03:14 -050030using namespace phosphor::fan::control;
Matthew Barth8600d9a2017-06-23 14:38:05 -050031using namespace phosphor::logging;
Matt Spinleree7f6422017-05-09 11:03:14 -050032
Matt Spinlere73446e2017-04-10 13:55:52 -050033int main(int argc, char* argv[])
34{
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070035 auto event = sdeventplus::Event::get_default();
Matt Spinlere73446e2017-04-10 13:55:52 -050036
Matthew Barth06764942021-03-04 09:28:40 -060037#ifndef CONTROL_USE_JSON
38 phosphor::fan::util::ArgumentParser args(argc, argv);
Matt Spinleree7f6422017-05-09 11:03:14 -050039 if (argc != 2)
40 {
41 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070042 return 1;
Matt Spinleree7f6422017-05-09 11:03:14 -050043 }
44
Matthew Barth14184132017-05-19 14:37:30 -050045 Mode mode;
Matt Spinleree7f6422017-05-09 11:03:14 -050046
47 if (args["init"] == "true")
48 {
Matthew Barth14184132017-05-19 14:37:30 -050049 mode = Mode::init;
Matt Spinleree7f6422017-05-09 11:03:14 -050050 }
51 else if (args["control"] == "true")
52 {
Matthew Barth14184132017-05-19 14:37:30 -050053 mode = Mode::control;
Matt Spinleree7f6422017-05-09 11:03:14 -050054 }
55 else
56 {
57 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070058 return 1;
Matt Spinleree7f6422017-05-09 11:03:14 -050059 }
Matthew Barth06764942021-03-04 09:28:40 -060060#endif
Matt Spinleree7f6422017-05-09 11:03:14 -050061
Matthew Barth3e1bb272020-05-26 11:09:21 -050062 // Attach the event object to the bus object so we can
63 // handle both sd_events (for the timers) and dbus signals.
Matthew Barth9403a212021-05-17 09:31:50 -050064 phosphor::fan::util::SDBusPlus::getBus().attach_event(
65 event.get(), SD_EVENT_PRIORITY_NORMAL);
Matthew Barth8600d9a2017-06-23 14:38:05 -050066
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050067 try
68 {
Matthew Barth06764942021-03-04 09:28:40 -060069#ifdef CONTROL_USE_JSON
Matthew Barth9403a212021-05-17 09:31:50 -050070 json::Manager manager(event);
Matthew Barth06764942021-03-04 09:28:40 -060071#else
Matthew Barth9403a212021-05-17 09:31:50 -050072 Manager manager(phosphor::fan::util::SDBusPlus::getBus(), event, mode);
Matt Spinleree7f6422017-05-09 11:03:14 -050073
Matthew Barth3e1bb272020-05-26 11:09:21 -050074 // Init mode will just set fans to max and delay
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050075 if (mode == Mode::init)
Matthew Barth8600d9a2017-06-23 14:38:05 -050076 {
Matthew Barth06764942021-03-04 09:28:40 -060077 manager.doInit(event);
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050078 return 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -050079 }
Matthew Barth06764942021-03-04 09:28:40 -060080#endif
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070081 return event.loop();
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050082 }
Matthew Barth3e1bb272020-05-26 11:09:21 -050083 // Log the useful metadata on these exceptions and let the app
84 // return 1 so it is restarted without a core dump.
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050085 catch (phosphor::fan::util::DBusServiceError& e)
86 {
87 log<level::ERR>("Uncaught DBus service lookup failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -050088 entry("PATH=%s", e.path.c_str()),
89 entry("INTERFACE=%s", e.interface.c_str()));
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050090 }
91 catch (phosphor::fan::util::DBusMethodError& e)
92 {
93 log<level::ERR>("Uncaught DBus method failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -050094 entry("BUSNAME=%s", e.busName.c_str()),
95 entry("PATH=%s", e.path.c_str()),
96 entry("INTERFACE=%s", e.interface.c_str()),
97 entry("METHOD=%s", e.method.c_str()));
Matt Spinlere73446e2017-04-10 13:55:52 -050098 }
Matthew Barth88923a02018-05-11 10:14:44 -050099 catch (phosphor::fan::util::DBusPropertyError& e)
100 {
101 log<level::ERR>("Uncaught DBus property access failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500102 entry("BUSNAME=%s", e.busName.c_str()),
103 entry("PATH=%s", e.path.c_str()),
104 entry("INTERFACE=%s", e.interface.c_str()),
105 entry("PROPERTY=%s", e.property.c_str()));
Matthew Barth88923a02018-05-11 10:14:44 -0500106 }
Matt Spinlere73446e2017-04-10 13:55:52 -0500107
William A. Kennington III3e781062018-10-19 17:18:34 -0700108 return 1;
Matt Spinlere73446e2017-04-10 13:55:52 -0500109}