blob: 98edfb940702f38e5f9097a1feb87bcdd4963f1b [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 */
Matt Spinleree7f6422017-05-09 11:03:14 -050016#include "argument.hpp"
Matt Spinlere10416e2017-04-10 14:15:53 -050017#include "manager.hpp"
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050018#include "sdbusplus.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060019#ifdef CONTROL_USE_JSON
20#include "json/manager.hpp"
21#endif
Matt Spinlere73446e2017-04-10 13:55:52 -050022
Matthew Barth3e1bb272020-05-26 11:09:21 -050023#include <phosphor-logging/log.hpp>
24#include <sdbusplus/bus.hpp>
25#include <sdeventplus/event.hpp>
26
Matt Spinleree7f6422017-05-09 11:03:14 -050027using namespace phosphor::fan::control;
Matthew Barth8600d9a2017-06-23 14:38:05 -050028using namespace phosphor::logging;
Matt Spinleree7f6422017-05-09 11:03:14 -050029
Matt Spinlere73446e2017-04-10 13:55:52 -050030int main(int argc, char* argv[])
31{
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070032 auto event = sdeventplus::Event::get_default();
Matt Spinlere73446e2017-04-10 13:55:52 -050033 auto bus = sdbusplus::bus::new_default();
34
Matthew Barth06764942021-03-04 09:28:40 -060035#ifndef CONTROL_USE_JSON
36 phosphor::fan::util::ArgumentParser args(argc, argv);
Matt Spinleree7f6422017-05-09 11:03:14 -050037 if (argc != 2)
38 {
39 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070040 return 1;
Matt Spinleree7f6422017-05-09 11:03:14 -050041 }
42
Matthew Barth14184132017-05-19 14:37:30 -050043 Mode mode;
Matt Spinleree7f6422017-05-09 11:03:14 -050044
45 if (args["init"] == "true")
46 {
Matthew Barth14184132017-05-19 14:37:30 -050047 mode = Mode::init;
Matt Spinleree7f6422017-05-09 11:03:14 -050048 }
49 else if (args["control"] == "true")
50 {
Matthew Barth14184132017-05-19 14:37:30 -050051 mode = Mode::control;
Matt Spinleree7f6422017-05-09 11:03:14 -050052 }
53 else
54 {
55 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070056 return 1;
Matt Spinleree7f6422017-05-09 11:03:14 -050057 }
Matthew Barth06764942021-03-04 09:28:40 -060058#endif
Matt Spinleree7f6422017-05-09 11:03:14 -050059
Matthew Barth3e1bb272020-05-26 11:09:21 -050060 // Attach the event object to the bus object so we can
61 // handle both sd_events (for the timers) and dbus signals.
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070062 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Matthew Barth8600d9a2017-06-23 14:38:05 -050063
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050064 try
65 {
Matthew Barth06764942021-03-04 09:28:40 -060066#ifdef CONTROL_USE_JSON
67 json::Manager manager(bus, event);
68#else
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070069 Manager manager(bus, event, mode);
Matt Spinleree7f6422017-05-09 11:03:14 -050070
Matthew Barth3e1bb272020-05-26 11:09:21 -050071 // Init mode will just set fans to max and delay
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050072 if (mode == Mode::init)
Matthew Barth8600d9a2017-06-23 14:38:05 -050073 {
Matthew Barth06764942021-03-04 09:28:40 -060074 manager.doInit(event);
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050075 return 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -050076 }
Matthew Barth06764942021-03-04 09:28:40 -060077#endif
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070078 return event.loop();
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050079 }
Matthew Barth3e1bb272020-05-26 11:09:21 -050080 // Log the useful metadata on these exceptions and let the app
81 // return 1 so it is restarted without a core dump.
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050082 catch (phosphor::fan::util::DBusServiceError& e)
83 {
84 log<level::ERR>("Uncaught DBus service lookup failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -050085 entry("PATH=%s", e.path.c_str()),
86 entry("INTERFACE=%s", e.interface.c_str()));
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050087 }
88 catch (phosphor::fan::util::DBusMethodError& e)
89 {
90 log<level::ERR>("Uncaught DBus method failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -050091 entry("BUSNAME=%s", e.busName.c_str()),
92 entry("PATH=%s", e.path.c_str()),
93 entry("INTERFACE=%s", e.interface.c_str()),
94 entry("METHOD=%s", e.method.c_str()));
Matt Spinlere73446e2017-04-10 13:55:52 -050095 }
Matthew Barth88923a02018-05-11 10:14:44 -050096 catch (phosphor::fan::util::DBusPropertyError& e)
97 {
98 log<level::ERR>("Uncaught DBus property access failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -050099 entry("BUSNAME=%s", e.busName.c_str()),
100 entry("PATH=%s", e.path.c_str()),
101 entry("INTERFACE=%s", e.interface.c_str()),
102 entry("PROPERTY=%s", e.property.c_str()));
Matthew Barth88923a02018-05-11 10:14:44 -0500103 }
Matt Spinlere73446e2017-04-10 13:55:52 -0500104
William A. Kennington III3e781062018-10-19 17:18:34 -0700105 return 1;
Matt Spinlere73446e2017-04-10 13:55:52 -0500106}